Skip to content

refactor(llm-access-kiro): split cache_sim.rs into submodules#7

Merged
acking-you merged 3 commits into
masterfrom
refactor/split-kiro-cache-sim
May 30, 2026
Merged

refactor(llm-access-kiro): split cache_sim.rs into submodules#7
acking-you merged 3 commits into
masterfrom
refactor/split-kiro-cache-sim

Conversation

@acking-you
Copy link
Copy Markdown
Owner

What

Split the 2190-line llm-access-kiro/src/cache_sim.rs into a cache_sim/mod.rs
facade plus 4 concern-focused submodules. Pure structural move — no logic
changes. This is the next step in the incremental llm-access* refactor
(follows #4 which split request.rs).

Module layout

 ConversationState
    -> [projection]   canonical token pages + conversation anchor segments
    -> [prefix_tree]  radix trie of shared stable-prefix pages
    -> [anchor_index] LRU conversation-id recovery keyed by anchor hash
    -> [simulator]    KiroCacheSimulator ties the three together
File Holds
projection.rs PromptProjection, RuntimePromptProjection, CanonicalTokenPage, PREFIX_CACHE_PAGE_SIZE, all canonicalize_*/tokenize/hash helpers + segment structs
prefix_tree.rs PrefixTree (+ node/edge internals & free-fn helpers), PrefixCacheMatch, PrefixTreeRuntimeStats
anchor_index.rs ConversationAnchorIndex (+ entry/estimate helpers), ConversationAnchorRuntimeStats
simulator.rs KiroCacheSimulator, KiroCacheSimulationMode, KiroCacheSimulationConfig, KiroCacheRuntimeStats

Module style (per the agreed standard)

  • mod.rs convention (matches the rest of the workspace).
  • No use super::* in submodules — each imports deps explicitly from their
    origin. No pub use X::* globs: mod.rs re-exports only the 10 items
    used outside the module, by name, so the cache_sim::X public surface
    (consumed by llm-access provider.rs/admin.rs and kiro config.rs) is
    unchanged.
  • Minimal visibility: items crossing a module boundary (PrefixTree + 4
    methods, ConversationAnchorIndex + 5 methods, PREFIX_CACHE_PAGE_SIZE) are
    pub with a /// doc; file-internal helpers/structs stay private. No
    pub(crate).
  • Tests inlined: 5 projection unit tests → projection.rs, 7 prefix-tree unit
    tests → prefix_tree.rs (so the private items they exercise need no extra
    exposure); the 4 simulator integration tests stay in mod.rs against the
    public API.

Verification

  • Item inventory diff vs original: 0 dropped, 0 added structural items;
    #[test] count 16 = 16.
  • cargo clippy -p llm-access-kiro -- -D warnings: clean.
  • cargo test -p llm-access-kiro: 160 passed.
  • Reverse-dep cargo build -p llm-access: compiles against the unchanged
    public surface.
  • rustfmt on the 5 new files only; deps/lance & deps/lancedb stayed clean.

🤖 Generated with Claude Code

Break the 2190-line cache_sim.rs into a cache_sim/mod.rs facade plus 4
concern-focused submodules, following the agreed module style:

- projection.rs: canonical prompt projection (PromptProjection,
  RuntimePromptProjection, CanonicalTokenPage, PREFIX_CACHE_PAGE_SIZE) and
  all canonicalize/tokenize/hash helpers + segment structs.
- prefix_tree.rs: the radix prefix-cache trie (PrefixTree) with its node/
  edge internals and free-fn helpers, plus PrefixCacheMatch and
  PrefixTreeRuntimeStats.
- anchor_index.rs: the TTL-bounded LRU conversation-anchor recovery index
  (ConversationAnchorIndex) plus ConversationAnchorRuntimeStats.
- simulator.rs: KiroCacheSimulator and its config/stats types
  (KiroCacheSimulationMode/Config, KiroCacheRuntimeStats).

Module style:
- mod.rs convention (matches the rest of the workspace).
- No `use super::*` in submodules: each imports its deps explicitly from
  their origin. No `pub use X::*` globs: mod.rs re-exports only the 10 items
  used outside the module, by name, so the cache_sim::X public surface
  (consumed by llm-access provider.rs/admin.rs and kiro config.rs) is
  unchanged.
- Minimal visibility: items crossing a module boundary (PrefixTree +
  4 methods, ConversationAnchorIndex + 5 methods, PREFIX_CACHE_PAGE_SIZE) are
  `pub` with a `///` doc; file-internal helpers/structs stay private. No
  `pub(crate)`.
- Tests inlined: the 5 projection unit tests move into projection.rs and the
  7 prefix-tree unit tests into prefix_tree.rs (so the private items they
  exercise need no extra exposure); the 4 simulator integration tests stay in
  mod.rs against the public API.

No behavior change: item inventory diff vs the original shows zero dropped
and zero added structural items, test count 16 = 16; clippy -D warnings
clean, 160 kiro tests pass, and the reverse-dep llm-access binary compiles
against the unchanged public surface.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Kiro prefix-cache simulation module by splitting a single monolithic file into focused submodules: anchor_index.rs, prefix_tree.rs, projection.rs, simulator.rs, and mod.rs. The feedback highlights an opportunity to optimize build_resume_anchor_hash in projection.rs by updating the Sha256 hasher directly, thereby avoiding unnecessary string clones and memory allocations.

Comment thread llm-access-kiro/src/cache_sim/projection.rs
acking-you and others added 2 commits May 31, 2026 04:23
build_resume_anchor_hash collected history_anchor_segments and
current_turn_history_segments into a temporary Vec<String> via .cloned()
only to feed them to Sha256. Hash the borrowed segments directly through the
existing update_hash_segments helper (chained iterator), matching the style
already used in into_runtime_projection.

Removes one Vec allocation plus a full clone of every history/current-turn
segment string on the record_success hot path. Byte-identical output: the
hashed segment order is unchanged, so runtime/non-runtime resume hashes still
agree (runtime_prompt_projection_preserves_matching_and_resume_hashes) and
anchor recovery still works.

Addresses a PR #7 review finding from gemini-code-assist.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The test job started failing at the link stage with "rust-lld: unable to
find library -lduckdb", while clippy stayed green. Root cause is a latent
interaction between libduckdb-sys and Swatinem/rust-cache, not the code under
test:

- llm-access-store -> libduckdb-sys, on the default (non-bundled) path with
  DUCKDB_DOWNLOAD_LIB=1, downloads libduckdb.so into target/duckdb-download/
  and links the test binary against it.
- rust-cache prunes that non-cargo artifact from the restored target/ while
  keeping the build-script fingerprint. A warm cache therefore replays the
  cached rustc-link-search pointing at a now-missing .so and the link fails.
- clippy never links the final binary, so it does not exercise -lduckdb and
  hid the breakage (cold cache passed once, the next warm run broke).

Fetch the matching prebuilt libduckdb into a dir outside target/ and point
libduckdb-sys at it via DUCKDB_LIB_DIR. That env branch is checked before the
download path and returns early; flipping the var from unset to set also fires
rerun-if-env-changed, forcing build.rs to re-run and self-healing an already
poisoned cache on the first run. The lib lives outside target/ so rust-cache
can no longer prune it. That branch emits no rpath and does not copy the .so
into target/deps, so LD_LIBRARY_PATH is exported for the test binaries to load
it at run time. The DuckDB version is decoded from Cargo.lock (same scheme as
build.rs) so it tracks future libduckdb-sys bumps. Only the test job needs
this; clippy does not link.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant