tea_kernel/compaction.rs
1use std::future::Future;
2use std::pin::Pin;
3
4use tea_protocol::CanonicalMessage;
5
6use crate::KernelError;
7
8/// Boxed future returned by a compaction summarizer.
9pub type CompactionSummaryFuture<'a> =
10 Pin<Box<dyn Future<Output = Result<CanonicalMessage, KernelError>> + Send + 'a>>;
11
12/// Policy deciding whether a context overflow should trigger automatic
13/// compaction before the next model request.
14///
15/// The kernel never generates a summary itself; when this policy allows
16/// compaction, it requests one from a product-supplied [`CompactionSummarizer`].
17pub trait CompactionPolicy: std::fmt::Debug + Send + Sync {
18 /// Returns whether automatic compaction should run for an overflow.
19 fn should_compact(&self, estimated_input_tokens: usize, context_window: u64) -> bool;
20}
21
22/// Default policy that never compacts automatically.
23#[derive(Debug, Clone, Copy, Default)]
24pub struct NeverCompactPolicy;
25
26impl CompactionPolicy for NeverCompactPolicy {
27 fn should_compact(&self, _estimated_input_tokens: usize, _context_window: u64) -> bool {
28 false
29 }
30}
31
32/// Product-supplied source of compaction summaries.
33///
34/// The kernel never owns a model for compaction. A product wires a summarizer
35/// that may call a model, retrieve a cached summary, or produce a deterministic
36/// truncation; the testkit provides a deterministic implementation.
37pub trait CompactionSummarizer: std::fmt::Debug + Send + Sync {
38 /// Produces one assistant summary message for the supplied transcript.
39 fn summarize(&self, messages: Vec<CanonicalMessage>) -> CompactionSummaryFuture<'_>;
40}