Skip to main content

ContextCompaction

Trait ContextCompaction 

Source
pub trait ContextCompaction: Send + Sync {
    // Required methods
    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;
    fn token_count_hint(&self, session_id: &SessionId) -> Option<usize>;
}
Expand description

Trait for inline context compaction within the react loop.

Implemented by agent-works’s ContextCompactor. agent-base defines the trait to avoid circular dependencies (agent-works depends on agent-base).

The react loop calls compact after tool execution when the estimated token count exceeds a configurable threshold. This prevents context window overflow without requiring the LLM call to fail first.

Required Methods§

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.

Takes the current messages and returns Some(outcome) if an action was performed — the outcome carries the action kind (for logging) and the messages to install. Returns None if compaction was skipped (below threshold, too few messages, or disabled).

The react loop handles reading/writing the session — the compactor only transforms the message list.

Source

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

Estimate current token count for the session.

Returns None if the implementation cannot estimate (falls back to ContextWindowManager::estimate_tokens on the react loop side).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§