Skip to main content

CompactionStrategy

Trait CompactionStrategy 

Source
pub trait CompactionStrategy: Send + Sync {
    // Required methods
    fn should_compact(
        &self,
        messages: &[ChatMessage],
        context_window_tokens: u64,
    ) -> bool;
    fn compact<'life0, 'life1, 'async_trait>(
        &'life0 self,
        messages: Vec<ChatMessage>,
        ctx: &'life1 CompactionContext,
    ) -> Pin<Box<dyn Future<Output = Result<CompactionOutcome, CompactionError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}

Required Methods§

Source

fn should_compact( &self, messages: &[ChatMessage], context_window_tokens: u64, ) -> bool

Decide whether the agent loop should call Self::compact before the next model step.

This method should be cheap and side-effect-free because it is checked before every step. Returning false leaves history unchanged.

Source

fn compact<'life0, 'life1, 'async_trait>( &'life0 self, messages: Vec<ChatMessage>, ctx: &'life1 CompactionContext, ) -> Pin<Box<dyn Future<Output = Result<CompactionOutcome, CompactionError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Return the message history that should replace the current history.

Implementations must preserve provider invariants for any tool-call history they keep: assistant tool calls must still be followed by their matching tool results, and orphan tool results must not be introduced. If the strategy cannot safely compact, it should return the original messages with usage: None rather than fabricating an invalid history. Errors are treated by the agent loop as “skip this turn’s compaction” rather than a failed user turn.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§