refactor(llm-access-kiro): split cache_sim.rs into submodules#7
Merged
Conversation
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>
There was a problem hiding this comment.
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.
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>
This was referenced May 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Split the 2190-line
llm-access-kiro/src/cache_sim.rsinto acache_sim/mod.rsfacade 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
projection.rsPromptProjection,RuntimePromptProjection,CanonicalTokenPage,PREFIX_CACHE_PAGE_SIZE, allcanonicalize_*/tokenize/hash helpers + segment structsprefix_tree.rsPrefixTree(+ node/edge internals & free-fn helpers),PrefixCacheMatch,PrefixTreeRuntimeStatsanchor_index.rsConversationAnchorIndex(+ entry/estimate helpers),ConversationAnchorRuntimeStatssimulator.rsKiroCacheSimulator,KiroCacheSimulationMode,KiroCacheSimulationConfig,KiroCacheRuntimeStatsModule style (per the agreed standard)
mod.rsconvention (matches the rest of the workspace).use super::*in submodules — each imports deps explicitly from theirorigin. No
pub use X::*globs:mod.rsre-exports only the 10 itemsused outside the module, by name, so the
cache_sim::Xpublic surface(consumed by
llm-accessprovider.rs/admin.rsand kiroconfig.rs) isunchanged.
PrefixTree+ 4methods,
ConversationAnchorIndex+ 5 methods,PREFIX_CACHE_PAGE_SIZE) arepubwith a///doc; file-internal helpers/structs stay private. Nopub(crate).projection.rs, 7 prefix-tree unittests →
prefix_tree.rs(so the private items they exercise need no extraexposure); the 4 simulator integration tests stay in
mod.rsagainst thepublic API.
Verification
#[test]count 16 = 16.cargo clippy -p llm-access-kiro -- -D warnings: clean.cargo test -p llm-access-kiro: 160 passed.cargo build -p llm-access: compiles against the unchangedpublic surface.
rustfmton the 5 new files only;deps/lance&deps/lancedbstayed clean.🤖 Generated with Claude Code