lean-ctx 3.9.19

Context Runtime for AI Agents with CCP. 79 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Terminal-output compression for Cursor terminal poll files.
//!
//! Cursor polls `.cursor/projects/*/terminals/*.txt` every ~3s via `ctx_read`.
//! These files are typically 100-500KB of raw terminal output (ANSI escapes,
//! build logs, test output) — most of which is irrelevant to the agent.
//!
//! This module provides:
//! 1. **ANSI stripping** (~30-50% savings on colored output)
//! 2. **Repeat collapse** (identical consecutive lines → `[× N collapsed]`)
//! 3. **Tail priority** (only the last N lines where actionable output is)
//! 4. **Hash dedup** (unchanged content within 10s → ~15-token stub)

use std::collections::HashMap;
use std::sync::Mutex;
use std::time::Instant;

use super::{ReadOutput, SessionCache, count_tokens};

/// Maximum tail lines to keep in compressed output.
const TAIL_LINES: usize = 150;
/// Maximum header lines (metadata block at the top of terminal files).
const MAX_HEADER_LINES: usize = 15;
/// Minimum file lines before tail-truncation activates.
const TAIL_THRESHOLD: usize = TAIL_LINES + MAX_HEADER_LINES + 20;
/// Dedup window: re-reads within this duration return a stub if hash matches.
const DEDUP_WINDOW_SECS: u64 = 10;

/// Per-path state for hash-based dedup of rapid terminal polls.
struct PollState {
    hash: u64,
    last_seen: Instant,
    compressed_tokens: usize,
}

static POLL_CACHE: Mutex<Option<HashMap<String, PollState>>> = Mutex::new(None);

fn content_hash(content: &str) -> u64 {
    use std::hash::{Hash, Hasher};
    let mut h = std::collections::hash_map::DefaultHasher::new();
    content.hash(&mut h);
    h.finish()
}

/// Entry point called from `core_logic.rs` for terminal poll files.
///
/// Handles hash-dedup (unchanged → stub) and compression in one call.
pub(super) fn handle_terminal_read(
    _cache: &mut SessionCache,
    path: &str,
    content: &str,
    file_ref: &str,
    short: &str,
    original_tokens: usize,
) -> ReadOutput {
    let hash = content_hash(content);

    // Check dedup cache: if same hash within window, return stub.
    {
        let mut guard = POLL_CACHE
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let cache = guard.get_or_insert_with(HashMap::new);
        if let Some(state) = cache.get(path) {
            if state.hash == hash && state.last_seen.elapsed().as_secs() < DEDUP_WINDOW_SECS {
                let stub = format!(
                    "{file_ref}={short} [terminal unchanged · {tok}→stub]",
                    tok = state.compressed_tokens,
                );
                let stub_tokens = count_tokens(&stub);
                crate::core::stats::record("ctx_read", original_tokens, stub_tokens);
                return ReadOutput {
                    content: stub,
                    resolved_mode: "terminal".into(),
                    output_tokens: stub_tokens,
                    is_cache_hit: true,
                };
            }
        }
    }

    // Try compression; fall back to full content if no savings.
    let (output, sent) = match compress_terminal(content, file_ref, short, original_tokens) {
        Some(pair) => pair,
        None => (content.to_string(), original_tokens),
    };

    // Update dedup cache.
    {
        let mut guard = POLL_CACHE
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let cache = guard.get_or_insert_with(HashMap::new);
        cache.insert(
            path.to_string(),
            PollState {
                hash,
                last_seen: Instant::now(),
                compressed_tokens: sent,
            },
        );
        // Evict stale entries (>60s) to avoid memory growth.
        cache.retain(|_, v| v.last_seen.elapsed().as_secs() < 60);
    }

    crate::core::stats::record("ctx_read", original_tokens, sent);
    ReadOutput {
        content: output,
        resolved_mode: "terminal".into(),
        output_tokens: sent,
        is_cache_hit: false,
    }
}

/// Compress terminal output for token-efficient delivery.
///
/// Returns `(compressed_content, output_tokens)` or `None` if the content
/// is too small to benefit from compression.
fn compress_terminal(
    content: &str,
    file_ref: &str,
    short: &str,
    original_tokens: usize,
) -> Option<(String, usize)> {
    let lines: Vec<&str> = content.lines().collect();
    if lines.len() < TAIL_THRESHOLD {
        let cleaned = strip_ansi_and_collapse(content);
        let tokens = count_tokens(&cleaned);
        if tokens >= original_tokens {
            return None;
        }
        let header = format!("{file_ref}={short} [terminal {n}L]", n = lines.len());
        let body = format!("{header}\n{cleaned}");
        let sent = count_tokens(&body);
        return Some((body, sent));
    }

    let (meta_end, meta_block) = extract_metadata_header(&lines);

    let stripped_lines: Vec<String> = lines.iter().map(|l| strip_ansi(l)).collect();

    let tail_start = stripped_lines.len().saturating_sub(TAIL_LINES);
    let tail_start = tail_start.max(meta_end);

    let tail_slice = &stripped_lines[tail_start..];
    let tail_collapsed = collapse_repeats(tail_slice);

    let elided_count = tail_start - meta_end;
    let elided_bytes: usize = stripped_lines[meta_end..tail_start]
        .iter()
        .map(|l| l.len() + 1)
        .sum();

    let mut out = String::with_capacity(meta_block.len() + tail_collapsed.len() + 200);
    out.push_str(&format!(
        "{file_ref}={short} [terminal {total}L]\n",
        total = lines.len()
    ));
    out.push_str(&meta_block);

    if elided_count > 0 {
        out.push_str(&format!(
            "\n[lean-ctx: {elided_count} lines elided ({kb:.1}KB) — showing last {tail}]\n\n",
            kb = elided_bytes as f64 / 1024.0,
            tail = tail_collapsed.lines().count(),
        ));
    }

    out.push_str(&tail_collapsed);

    if let Some(exit_info) = extract_exit_info(&stripped_lines) {
        if !out.contains("exit_code") {
            out.push('\n');
            out.push_str(&exit_info);
        }
    }

    let sent = count_tokens(&out);
    if sent >= original_tokens {
        return None;
    }
    Some((out, sent))
}

/// Extracts the `---` delimited metadata block at the top of a terminal file.
fn extract_metadata_header(lines: &[&str]) -> (usize, String) {
    let mut meta = String::new();
    let mut in_meta = false;
    let mut end = 0;

    for (i, line) in lines.iter().enumerate().take(MAX_HEADER_LINES) {
        if line.trim() == "---" {
            meta.push_str(line);
            meta.push('\n');
            if in_meta {
                end = i + 1;
                break;
            }
            in_meta = true;
            continue;
        }
        if in_meta {
            meta.push_str(line);
            meta.push('\n');
        }
        if !in_meta && i > 2 {
            break;
        }
    }

    if end == 0 {
        end = if in_meta {
            lines.len().min(MAX_HEADER_LINES)
        } else {
            0
        };
    }

    (end, meta)
}

/// Strips ANSI escape sequences (CSI + OSC) from a single line.
fn strip_ansi(line: &str) -> String {
    let bytes = line.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;

    while i < bytes.len() {
        if bytes[i] == 0x1b && i + 1 < bytes.len() && bytes[i + 1] == b'[' {
            // CSI sequence: ESC [ ... <letter>
            i += 2;
            while i < bytes.len() && !bytes[i].is_ascii_alphabetic() && bytes[i] != b'm' {
                i += 1;
            }
            if i < bytes.len() {
                i += 1;
            }
        } else if bytes[i] == 0x1b && i + 1 < bytes.len() && bytes[i + 1] == b']' {
            // OSC sequence: ESC ] ... (BEL or ST)
            i += 2;
            while i < bytes.len() && bytes[i] != 0x07 {
                if bytes[i] == 0x1b && i + 1 < bytes.len() && bytes[i + 1] == b'\\' {
                    i += 2;
                    break;
                }
                i += 1;
            }
            if i < bytes.len() && bytes[i] == 0x07 {
                i += 1;
            }
        } else {
            out.push(bytes[i]);
            i += 1;
        }
    }

    String::from_utf8_lossy(&out).to_string()
}

fn strip_ansi_and_collapse(content: &str) -> String {
    let lines: Vec<String> = content.lines().map(strip_ansi).collect();
    collapse_repeats(&lines)
}

/// Collapses runs of 3+ identical consecutive lines into `[× N collapsed]`.
fn collapse_repeats(lines: &[String]) -> String {
    if lines.is_empty() {
        return String::new();
    }

    let mut out = String::with_capacity(lines.len() * 40);
    let mut prev = &lines[0];
    let mut count: usize = 1;

    for line in &lines[1..] {
        if line == prev {
            count += 1;
        } else {
            emit_line(&mut out, prev, count);
            prev = line;
            count = 1;
        }
    }
    emit_line(&mut out, prev, count);
    out
}

fn emit_line(out: &mut String, line: &str, count: usize) {
    if count > 2 {
        out.push_str(line);
        out.push('\n');
        out.push_str(&format!("{count} identical lines collapsed]\n"));
    } else {
        for _ in 0..count {
            out.push_str(line);
            out.push('\n');
        }
    }
}

/// Extracts exit_code/elapsed info from the footer metadata block.
fn extract_exit_info(lines: &[String]) -> Option<String> {
    let tail = if lines.len() > 10 {
        &lines[lines.len() - 10..]
    } else {
        lines
    };

    let mut in_footer = false;
    let mut info = String::new();
    for line in tail {
        if line.trim() == "---" {
            if in_footer {
                break;
            }
            in_footer = true;
            continue;
        }
        if in_footer
            && (line.starts_with("exit_code:")
                || line.starts_with("elapsed_ms:")
                || line.starts_with("ended_at:")
                || line.starts_with("status:"))
        {
            info.push_str(line.trim());
            info.push('\n');
        }
    }

    if info.is_empty() { None } else { Some(info) }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn strip_ansi_removes_color_codes() {
        assert_eq!(strip_ansi("\x1b[1;32mOK\x1b[0m"), "OK");
        assert_eq!(strip_ansi("no escapes"), "no escapes");
        assert_eq!(strip_ansi("\x1b[31merror\x1b[0m: bad"), "error: bad");
    }

    #[test]
    fn collapse_repeats_groups_identical_lines() {
        let lines: Vec<String> = vec![
            "line A".into(),
            "line B".into(),
            "line B".into(),
            "line B".into(),
            "line B".into(),
            "line C".into(),
        ];
        let result = collapse_repeats(&lines);
        assert!(result.contains("[× 4 identical lines collapsed]"));
        assert!(result.contains("line A"));
        assert!(result.contains("line C"));
    }

    #[test]
    fn extract_metadata_header_parses_terminal_format() {
        let content =
            "---\npid: 1234\ncwd: /foo\nstatus: running\n---\noutput line 1\noutput line 2";
        let lines: Vec<&str> = content.lines().collect();
        let (end, meta) = extract_metadata_header(&lines);
        assert_eq!(end, 5);
        assert!(meta.contains("pid: 1234"));
        assert!(meta.contains("cwd: /foo"));
    }

    #[test]
    fn compress_small_file_strips_ansi_only() {
        let content = "\x1b[32mOK\x1b[0m\ntest passed\n";
        let result = compress_terminal(content, "F1", "test.txt", 100);
        assert!(result.is_some());
        let (body, _) = result.unwrap();
        assert!(!body.contains("\x1b["));
        assert!(body.contains("OK"));
    }

    #[test]
    fn compress_large_file_tail_truncates() {
        let mut content = String::from("---\npid: 99\ncwd: /x\nstatus: done\n---\n");
        for i in 0..500 {
            content.push_str(&format!("build output line {i}\n"));
        }
        content.push_str("---\nexit_code: 0\nelapsed_ms: 5000\n---\n");
        let tokens = count_tokens(&content);
        let result = compress_terminal(&content, "F1", "build.txt", tokens);
        assert!(result.is_some());
        let (body, sent) = result.unwrap();
        assert!(sent < tokens, "must save tokens: {sent} < {tokens}");
        assert!(body.contains("lines elided"));
        assert!(body.contains("exit_code: 0"));
        assert!(body.contains("pid: 99"));
    }

    #[test]
    fn dedup_returns_stub_on_unchanged_reread() {
        let mut cache = SessionCache::default();
        let content = "---\npid: 1\ncwd: /x\n---\nline 1\nline 2\n";
        let tokens = count_tokens(content);

        let r1 = handle_terminal_read(
            &mut cache,
            "/tmp/test_term.txt",
            content,
            "F1",
            "test_term.txt",
            tokens,
        );
        assert_eq!(r1.resolved_mode, "terminal");
        assert!(!r1.is_cache_hit);

        let r2 = handle_terminal_read(
            &mut cache,
            "/tmp/test_term.txt",
            content,
            "F1",
            "test_term.txt",
            tokens,
        );
        assert!(
            r2.is_cache_hit,
            "second read of same content must be a stub"
        );
        assert!(r2.content.contains("unchanged"));
        assert!(r2.output_tokens < r1.output_tokens);
    }

    #[test]
    fn dedup_recompresses_on_changed_content() {
        let mut cache = SessionCache::default();
        let content_a = "---\npid: 1\ncwd: /x\n---\nline 1\nline 2\n";
        let content_b = "---\npid: 1\ncwd: /x\n---\nline 1\nline 2\nline 3\n";
        let tokens_a = count_tokens(content_a);
        let tokens_b = count_tokens(content_b);

        let _r1 = handle_terminal_read(
            &mut cache,
            "/tmp/test_term2.txt",
            content_a,
            "F1",
            "test_term2.txt",
            tokens_a,
        );
        let r2 = handle_terminal_read(
            &mut cache,
            "/tmp/test_term2.txt",
            content_b,
            "F1",
            "test_term2.txt",
            tokens_b,
        );
        assert!(!r2.is_cache_hit, "changed content must not be a stub");
    }

    #[test]
    fn extract_exit_info_from_footer() {
        let lines: Vec<String> = vec![
            "output".into(),
            "---".into(),
            "exit_code: 0".into(),
            "elapsed_ms: 1234".into(),
            "---".into(),
        ];
        let info = extract_exit_info(&lines);
        assert!(info.is_some());
        let info = info.unwrap();
        assert!(info.contains("exit_code: 0"));
        assert!(info.contains("elapsed_ms: 1234"));
    }
}