pub struct DedupCache { /* private fields */ }Expand description
Session-scoped deduplication cache.
Maintains a bounded LRU of recent (content_hash → tool_call_id) mappings.
A single instance is intended to live for one agent session (or one
context partition, whichever is shorter).
Implementations§
Source§impl DedupCache
impl DedupCache
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct a cache with the given LRU capacity.
Recommended default: 5. Empirically, 95% of deduplication savings in
Claude Code logs come from the three most-recent responses; capacities
above ~5 yield diminishing returns.
Sourcepub fn partition(&self) -> u64
pub fn partition(&self) -> u64
Current context partition counter. Increments on each
on_compaction_boundary() call.
Sourcepub fn check(&mut self, hash: &ContentHash) -> DedupDecision
pub fn check(&mut self, hash: &ContentHash) -> DedupDecision
LRU lookup that refreshes recency on hits.
A match promotes the entry to the most-recently-used position, so repeated references protect frequently-used content from eviction when new responses are inserted.
Sourcepub fn insert(
&mut self,
hash: ContentHash,
tool_call_id: impl Into<String>,
tool_kind: ToolKind,
file_path_hash: Option<String>,
tool_name: impl Into<String>,
)
pub fn insert( &mut self, hash: ContentHash, tool_call_id: impl Into<String>, tool_kind: ToolKind, file_path_hash: Option<String>, tool_name: impl Into<String>, )
Insert a fresh response. Evicts the oldest entry if the cache is at capacity.
tool_name is the anonymized tool that produced the response —
used by Self::invalidate_by_tool for Paper 3 cross-tool
invalidation. Pass an empty string when the tool is unknown
(cross-tool invalidation will simply never match it).
Sourcepub fn insert_with_body(
&mut self,
hash: ContentHash,
tool_call_id: impl Into<String>,
tool_kind: ToolKind,
file_path_hash: Option<String>,
body: Arc<String>,
tool_name: impl Into<String>,
)
pub fn insert_with_body( &mut self, hash: ContentHash, tool_call_id: impl Into<String>, tool_kind: ToolKind, file_path_hash: Option<String>, body: Arc<String>, tool_name: impl Into<String>, )
Insert a fresh response and retain its body for Type-2
(near-duplicate) hint extraction. Used when
dedup.near_ref_enabled is on; otherwise prefer Self::insert
to avoid the extra allocation.
Sourcepub fn find_near_ref(
&self,
new_body: &str,
config: &NearRefConfig,
) -> Option<(String, Vec<DeltaField>)>
pub fn find_near_ref( &self, new_body: &str, config: &NearRefConfig, ) -> Option<(String, Vec<DeltaField>)>
Look for a Type-2 near-duplicate match in the cache. Walks entries
from newest to oldest; the first one whose retained body produces
a valid delta against new_body (per extract_delta) wins.
Returns the matched tool_call_id plus the field-level deltas.
Returns None when:
- no entry has a body snapshot,
- no entry’s delta against
new_bodyclears the eligibility gate (size, scalar-only, key-set match), - or
new_bodyitself is too short to bother.
Sourcepub fn invalidate_file(&mut self, file_path_hash: &str) -> usize
pub fn invalidate_file(&mut self, file_path_hash: &str) -> usize
Invalidate all cached FileRead entries whose path hash matches.
Returns the number of entries dropped.
Callers typically invoke this from a FileMutate tool response
handler to prevent emitting stale hints for a subsequently re-read file.
Sourcepub fn invalidate_by_tool(&mut self, invalidates: &[String]) -> usize
pub fn invalidate_by_tool(&mut self, invalidates: &[String]) -> usize
Invalidate every entry whose tool_name appears in
invalidates. Drives Paper 3 cross-tool invalidation: a writer
(update_issue, Edit, Write, …) declares which read tools
its ToolValueModel.invalidates list, and the runtime calls this
method right after the writer’s response is processed.
Returns the number of entries dropped. Empty invalidates is a
no-op (returns 0) — used by tools that do not affect any cache.
Sourcepub fn on_compaction_boundary(&mut self)
pub fn on_compaction_boundary(&mut self)
Clear the cache and advance the partition counter.
Called when the LLM host emits a context-compaction boundary
(Claude Code: compact_boundary event). Older references are no
longer reachable from the agent’s context, so they must not be
referenced by new hints.