lean-ctx 3.1.3

Context Runtime for AI Agents with CCP. 42 MCP tools, 10 read modes, 90+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing + diaries, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24 AI tools. Reduces LLM token consumption by up to 99%.
Documentation
use regex::Regex;
use std::sync::OnceLock;

static PROGRESS_RE: OnceLock<Regex> = OnceLock::new();

fn progress_re() -> &'static Regex {
    PROGRESS_RE.get_or_init(|| Regex::new(r"^\s*\d+K\s+.*\d+%").unwrap())
}

pub fn compress(output: &str) -> Option<String> {
    let trimmed = output.trim();
    if trimmed.is_empty() {
        return Some("ok".to_string());
    }

    let useful: Vec<&str> = trimmed
        .lines()
        .filter(|l| {
            let t = l.trim();
            !t.is_empty()
                && !progress_re().is_match(t)
                && !t.starts_with("Length:")
                && !t.starts_with("Connecting to")
                && !t.starts_with("Resolving")
                && !t.starts_with("HTTP request sent")
                && !t.starts_with("Reusing existing")
        })
        .collect();

    let saved = trimmed.lines().find(|l| l.contains("saved"));

    if let Some(saved_line) = saved {
        let mut result = Vec::new();
        for line in &useful {
            if line.contains("Saving to") || line.contains("saved") || line.contains("--") {
                result.push(line.to_string());
            }
        }
        if result.is_empty() {
            result.push(saved_line.trim().to_string());
        }
        return Some(result.join("\n"));
    }

    if useful.len() <= 5 {
        return Some(useful.join("\n"));
    }
    Some(format!(
        "{}\n... ({} more lines)",
        useful[..3].join("\n"),
        useful.len() - 3
    ))
}