Skip to main content

lean_ctx/core/patterns/
pattern_trait.rs

1/// Plugin-ready trait for shell output compression patterns.
2///
3/// Each pattern matches a specific CLI tool (or family of tools) and
4/// compresses its output into a token-efficient representation.
5///
6/// Existing patterns implement this via the blanket `compress(cmd, output)`
7/// functions. Future plugins will register implementations dynamically.
8pub trait CompressionPattern: Send + Sync {
9    /// Human-readable name for this pattern (e.g. "cargo", "git", "docker").
10    fn name(&self) -> &str;
11
12    /// Version of this pattern's output format. Bump when the compressed
13    /// output structure changes to maintain determinism guarantees.
14    fn version(&self) -> u32 {
15        1
16    }
17
18    /// Returns true if this pattern can handle the given command.
19    /// Called during dispatch to find the appropriate pattern.
20    fn matches(&self, command: &str) -> bool;
21
22    /// Compress the shell output for the matched command.
23    /// Returns `None` if the pattern cannot produce a shorter result.
24    fn compress(&self, command: &str, output: &str) -> Option<String>;
25
26    /// Command prefixes this pattern handles (for documentation/discovery).
27    fn prefixes(&self) -> &[&str];
28}
29
30/// Metadata about a compression result, for observability and IR recording.
31#[derive(Debug, Clone)]
32pub struct CompressionResult {
33    pub pattern_name: String,
34    pub pattern_version: u32,
35    pub input_tokens: usize,
36    pub output_tokens: usize,
37    pub compressed: String,
38}