pub trait CompactionStrategy: Send + Sync {
// Required methods
fn name(&self) -> &'static str;
fn compact<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
messages: &'life1 [Message],
pinned: &'life2 [bool],
preserve_n_last: usize,
) -> Pin<Box<dyn Future<Output = Result<CompactionOutput, CompactionError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait;
}Expand description
User-implementable strategy for shrinking a History’s
history when History::compact_if_needed runs.
Ships with two built-in implementations: TruncateStrategy drops
the oldest unpinned messages until the budget fits;
SummarizeStrategy replaces the dropped prefix with a model-
generated summary so context is compressed rather than lost. Use
Box<dyn CompactionStrategy> to swap algorithms at runtime, or
implement the trait yourself for domain-specific reductions
(sliding window, importance scoring, RAG-style summarization, …).
Required Methods§
Sourcefn name(&self) -> &'static str
fn name(&self) -> &'static str
Stable, machine-readable name of the strategy. Used by
HistoryCompacted events so callers can attribute compaction
to a specific algorithm in logs/metrics.
Sourcefn compact<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
messages: &'life1 [Message],
pinned: &'life2 [bool],
preserve_n_last: usize,
) -> Pin<Box<dyn Future<Output = Result<CompactionOutput, CompactionError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn compact<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
messages: &'life1 [Message],
pinned: &'life2 [bool],
preserve_n_last: usize,
) -> Pin<Box<dyn Future<Output = Result<CompactionOutput, CompactionError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
Compact messages into a smaller history.
pinned is a parallel slice of the same length as messages:
pinned[i] == true marks messages[i] as “must survive”. A
strategy must include every pinned message in its output (in
the original relative order) and forward its true pin state
in the returned CompactionOutput::pinned.
preserve_n_last is a hint: at minimum the last N messages
(after walking back to a safe boundary that doesn’t strand a
ToolResult from its ToolCall) should be kept verbatim.