pub struct ContextCompactor { /* private fields */ }Expand description
Core compressor implementing the hybrid retention strategy.
Holds an LlmProvider for LLM summarisation and a thread-safe cache to
avoid redundant calls. Designed to be shared via Arc across middleware
invocations.
Use clone() to create a second handle that shares the same
underlying cache — useful when the compactor is registered as middleware and
stored separately for manual /compact access.
Implementations§
Source§impl ContextCompactor
impl ContextCompactor
pub fn new(client: Arc<dyn LlmProvider>, config: CompressionConfig) -> Self
Sourcepub fn clone_handle(&self) -> Self
pub fn clone_handle(&self) -> Self
Create a second handle to the same compactor.
The clone shares the same LLM client and summary cache — clearing the cache through either handle affects both.
pub fn config(&self) -> &CompressionConfig
Sourcepub async fn compact(
&self,
session_id: u64,
messages: &[ChatMessage],
trigger: CompressionTrigger,
emit_fn: Option<&(dyn Fn(CompressionEvent) + Sync)>,
) -> AgentResult<Option<Vec<ChatMessage>>>
pub async fn compact( &self, session_id: u64, messages: &[ChatMessage], trigger: CompressionTrigger, emit_fn: Option<&(dyn Fn(CompressionEvent) + Sync)>, ) -> AgentResult<Option<Vec<ChatMessage>>>
Produce a compressed copy of messages for a single LLM call.
Returns None when compression is skipped (disabled, below threshold,
or too few messages). The caller should use the original messages
unchanged in that case.
session_id is used as part of the cache key so different sessions
never share summaries.
§Compression strategy
User messages from the old block are preserved verbatim (truncated to
MAX_PRESERVED_USER_TOKENS).
Only assistant and tool responses are summarised by the LLM. This
mirrors codex’s compaction strategy and avoids the common pitfall of
the summarizer reproducing assistant-generated content (poems, code,
articles) verbatim, which can make the compressed output larger than
the original.
Sourcepub fn clear_cache(&self)
pub fn clear_cache(&self)
Clear all cached summaries.
Sourcepub async fn compact_session(
&self,
runtime: &AgentRuntime,
session_id: &SessionId,
emit_fn: Option<Arc<dyn Fn(UserEvent) + Send + Sync>>,
) -> AgentResult<bool>
pub async fn compact_session( &self, runtime: &AgentRuntime, session_id: &SessionId, emit_fn: Option<Arc<dyn Fn(UserEvent) + Send + Sync>>, ) -> AgentResult<bool>
Read-compress-write-back: compress a session’s messages in place.
Returns Ok(true) if compression was applied (session was above the
threshold), Ok(false) if the session was already below the threshold.
Returns Err if the write-back validation fails (unexpected) or if
the session was modified concurrently between read and write-back
(TOCTOU guard).
Trait Implementations§
Source§impl ContextCompaction for ContextCompactor
Implement the agent-base ContextCompaction trait so the react loop
can trigger inline compaction without depending on agent-works directly.
impl ContextCompaction for ContextCompactor
Implement the agent-base ContextCompaction trait so the react loop
can trigger inline compaction without depending on agent-works directly.