Skip to main content

DedupCache

Struct DedupCache 

Source
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

Source

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.

Source

pub fn partition(&self) -> u64

Current context partition counter. Increments on each on_compaction_boundary() call.

Source

pub fn len(&self) -> usize

Number of entries currently cached.

Source

pub fn is_empty(&self) -> bool

true if no entries are cached.

Source

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.

Source

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).

Source

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.

Source

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_body clears the eligibility gate (size, scalar-only, key-set match),
  • or new_body itself is too short to bother.
Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for DedupCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DedupCache

Source§

fn default() -> Self

Equivalent to DedupCache::with_capacity(5).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more