Skip to main content

ContextCompactor

Struct ContextCompactor 

Source
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

Source

pub fn new(client: Arc<dyn LlmProvider>, config: CompressionConfig) -> Self

Source

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.

Source

pub fn config(&self) -> &CompressionConfig

Source

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.

Source

pub fn clear_cache(&self)

Clear all cached summaries.

Source

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.

Source§

fn compact<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, session_id: &'life1 SessionId, messages: &'life2 [ChatMessage], ) -> Pin<Box<dyn Future<Output = Option<CompactionOutcome>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Compact a message history. Read more
Source§

fn token_count_hint(&self, _session_id: &SessionId) -> Option<usize>

Estimate current token count for the session. Read more

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: Sized + 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: Sized + 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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