machi_compaction/strategy.rs
1//! Compaction strategy trait.
2
3use machi_types::{MachiError, Message};
4
5/// Result of a compaction pass.
6#[derive(Debug, Clone)]
7pub struct CompactionOutcome {
8 /// Messages after compaction.
9 pub messages: Vec<Message>,
10 /// True when the list changed.
11 pub changed: bool,
12 /// Strategy name for metrics.
13 pub strategy: &'static str,
14}
15
16/// Pure compaction strategy.
17pub trait CompactionStrategy: Send + Sync {
18 /// Stable strategy id (`max_messages`, `token_threshold`, …).
19 fn name(&self) -> &'static str;
20
21 /// Whether compaction should run given current size estimates.
22 fn should_compact(&self, messages: &[Message], token_estimate: u64) -> bool;
23
24 /// Produce a compacted message list.
25 ///
26 /// # Errors
27 ///
28 /// Returns typed compaction failures (should not panic on normal input).
29 fn compact(&self, messages: Vec<Message>) -> Result<CompactionOutcome, MachiError>;
30}