lean-ctx 3.11.4

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. CLI, MCP server, and supported proxy integrations.
Documentation
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use super::*;

// UTF-8 BOM must not leak into the first line of ctx_read output
// (limitations doc #11).
#[test]
fn read_file_lossy_strips_utf8_bom() {
    let p = std::env::temp_dir().join("lean_ctx_bom_test.txt");
    std::fs::write(&p, b"\xEF\xBB\xBFhello\n").unwrap();
    let s = read_file_lossy(p.to_str().unwrap()).unwrap();
    let _ = std::fs::remove_file(&p);
    assert!(
        !s.starts_with('\u{feff}'),
        "BOM must be stripped from read content"
    );
    assert!(
        s.starts_with("hello"),
        "content after BOM must survive: {s}"
    );
}

#[test]
fn compressed_cache_key_distinguishes_task() {
    let no_task = compressed_cache_key("map", CrpMode::Off, None, None, &[]);
    let tdd_no_task = compressed_cache_key("map", CrpMode::Tdd, None, None, &[]);
    let with_task = compressed_cache_key("map", CrpMode::Off, Some("fix login"), None, &[]);
    let other_task = compressed_cache_key("map", CrpMode::Off, Some("refactor db"), None, &[]);
    assert_eq!(no_task, "map:v2");
    assert_eq!(tdd_no_task, "map:v2:tdd");
    // #E26: map/signatures are structure-preserving and task-independent.
    // Their cache key must NOT vary with task to improve provider cache hits.
    assert_eq!(with_task, no_task, "map key must be task-independent");
    assert_eq!(with_task, other_task, "map key must be task-independent");

    // Task-dependent modes MUST still distinguish tasks.
    let density_a = compressed_cache_key("density:0.3", CrpMode::Off, Some("fix login"), None, &[]);
    let density_b =
        compressed_cache_key("density:0.3", CrpMode::Off, Some("refactor db"), None, &[]);
    assert_ne!(density_a, density_b, "density key must vary with task");
}

/// #1910: an explicit request's raw fallback carries a banner, an `auto` one
/// does not — both resolve to the same mode, so the key must tell them apart.
/// Explicit keys stay byte-identical to their pre-#1910 form.
#[test]
fn request_scoped_key_separates_auto_from_explicit() {
    let base = compressed_cache_key("cognitive", CrpMode::Off, None, None, &[]);
    let explicit = request_scoped_key(base.clone(), false);
    let auto = request_scoped_key(base.clone(), true);
    assert_eq!(explicit, base);
    assert_ne!(auto, explicit);
    assert_eq!(auto, request_scoped_key(base, true), "deterministic (#498)");
}

#[test]
fn compressed_cache_key_distinguishes_aggressiveness() {
    // None → byte-identical to today's keys (#714 must not shift existing cache).
    let base = compressed_cache_key("map", CrpMode::Off, None, None, &[]);
    assert_eq!(base, "map:v2");
    // Same aggressiveness → same key (determinism, #498).
    let a = compressed_cache_key("map", CrpMode::Off, None, Some(0.7), &[]);
    assert_eq!(
        a,
        compressed_cache_key("map", CrpMode::Off, None, Some(0.7), &[])
    );
    // Distinct buckets → distinct keys; jitter inside a 0.05 bucket collapses.
    assert_ne!(a, base);
    assert_ne!(
        a,
        compressed_cache_key("map", CrpMode::Off, None, Some(0.2), &[])
    );
    assert_eq!(
        a,
        compressed_cache_key("map", CrpMode::Off, None, Some(0.701), &[])
    );
}

#[test]
fn compressed_cache_key_distinguishes_protect() {
    // Empty protect → byte-identical to today's keys (#720 must not shift cache).
    let base = compressed_cache_key("entropy", CrpMode::Off, None, None, &[]);
    assert_eq!(base, "entropy");
    // A non-empty protect list changes the key (lossy output differs, #498)…
    let p = compressed_cache_key("entropy", CrpMode::Off, None, None, &["TODO".to_string()]);
    assert_ne!(p, base);
    // …deterministically, and independent of token order / duplicates.
    assert_eq!(
        p,
        compressed_cache_key("entropy", CrpMode::Off, None, None, &["TODO".to_string()])
    );
    let multi_a = compressed_cache_key(
        "entropy",
        CrpMode::Off,
        None,
        None,
        &["a".to_string(), "b".to_string()],
    );
    let multi_b = compressed_cache_key(
        "entropy",
        CrpMode::Off,
        None,
        None,
        &["b".to_string(), "a".to_string(), "a".to_string()],
    );
    assert_eq!(multi_a, multi_b);
    assert_ne!(multi_a, p);
}

#[test]
fn aggressiveness_is_deterministic_and_monotonic() {
    let _lock = crate::core::data_dir::test_env_lock();
    // Suppress the savings footer: it carries session-cumulative counters by
    // design (state-triggered suffix), so we compare the pure compressed body.
    crate::test_env::set_var("LEAN_CTX_SHOW_SAVINGS", "0");

    // Prose-y fixture with redundant low-information lines the density pass can
    // shed; enough lines that compression is meaningful.
    let mut content = String::new();
    for i in 0..60 {
        content.push_str(&format!(
            "line {i}: the quick brown fox jumps over the lazy dog\n"
        ));
    }
    let render_at = |a: f64| -> String {
        // Bare `density:` exercises the aggressiveness-target fallback (#714).
        let (out, _) = process_mode_tuned(
            &content,
            "density:",
            "F1",
            "f.txt",
            "txt",
            count_tokens(&content),
            CrpMode::Off,
            "/tmp/f.txt",
            None,
            ReadTuning {
                aggressiveness: Some(a),
                protect: &[],
            },
        );
        out
    };
    // Determinism (#498): same aggressiveness → byte-identical output. Guards the
    // canonical-order entropy summation fix in `token_entropy_from_ids`.
    assert_eq!(render_at(0.7), render_at(0.7));
    // Monotonic: more aggressive keeps no more tokens than less aggressive.
    let low = count_tokens(&render_at(0.2));
    let high = count_tokens(&render_at(0.9));
    assert!(
        high <= low,
        "aggressiveness 0.9 ({high} tok) must not exceed 0.2 ({low} tok)"
    );

    crate::test_env::remove_var("LEAN_CTX_SHOW_SAVINGS");
}

/// #1911: `entropy` on real source used to save 0% (the absolute H/surprise
/// cut kept every line of a typical Rust file) and `aggressiveness` did not
/// move it. The file-relative surprise floor must shed real tokens at the
/// default and strictly more as aggressiveness rises — without ever falling
/// back to the "no compression applied" path.
#[test]
fn entropy_saves_on_real_source_and_scales_with_aggressiveness() {
    let _lock = crate::core::data_dir::test_env_lock();
    crate::test_env::set_var("LEAN_CTX_SHOW_SAVINGS", "0");
    let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/core/compressor.rs");
    let content = std::fs::read_to_string(&path).unwrap();
    let raw = count_tokens(&content);
    let render_at = |a: Option<f64>| -> String {
        process_mode_tuned(
            &content,
            "entropy",
            "F1",
            "compressor.rs",
            "rs",
            raw,
            CrpMode::Off,
            "src/core/compressor.rs",
            None,
            ReadTuning {
                aggressiveness: a,
                protect: &[],
            },
        )
        .0
    };
    let default = render_at(None);
    // The default threshold is file-adaptive (per-language base plus the
    // content's compressibility), not a point on the aggressiveness scale;
    // monotonicity is checked on explicit levels only.
    let low = count_tokens(&render_at(Some(0.1)));
    let mid = count_tokens(&render_at(Some(0.5)));
    let high = count_tokens(&render_at(Some(0.9)));
    let default_tokens = count_tokens(&default);
    let again = render_at(None);
    crate::test_env::remove_var("LEAN_CTX_SHOW_SAVINGS");

    assert_eq!(default, again, "entropy output must be deterministic");
    assert!(
        !default.starts_with("[lean-ctx] no compression applied"),
        "entropy fell back on compressor.rs"
    );
    assert!(
        default_tokens * 100 <= raw * 95,
        "entropy default saves <5%: {default_tokens} of {raw} tok"
    );
    assert!(low <= raw, "a=0.1 inflates: {low} > {raw}");
    assert!(
        high < mid && mid <= low,
        "not monotonic: a0.1={low} a0.5={mid} a0.9={high}"
    );
}

#[test]
fn aggressive_json_uses_lossless_crush_core() {
    let _lock = crate::core::data_dir::test_env_lock();
    crate::test_env::set_var("LEAN_CTX_SHOW_SAVINGS", "0");

    // A redundant array-of-objects JSON file: aggressive mode compacts it through
    // the shared json_crush core (#936) instead of generic text pruning, which
    // would mangle the structure. Constant columns + many rows so it halves.
    let items: Vec<String> = (0..40)
        .map(|i| {
            format!(r#"{{"status":"active","region":"eu-central-1","tier":"standard","id":{i}}}"#)
        })
        .collect();
    let content = format!("[{}]", items.join(","));
    let original = count_tokens(&content);

    let (out, sent) = process_mode_tuned(
        &content,
        "aggressive",
        "F1",
        "data.json",
        "json",
        original,
        CrpMode::Off,
        "/tmp/data.json",
        None,
        ReadTuning {
            aggressiveness: None,
            protect: &[],
        },
    );

    assert!(
        out.contains("_lc_crush"),
        "aggressive json must compact via the crush core: {out}"
    );
    assert!(
        sent < original,
        "crush must reduce tokens ({sent} >= {original})"
    );

    crate::test_env::remove_var("LEAN_CTX_SHOW_SAVINGS");
}

#[test]
fn map_mode_includes_signature_line_ranges() {
    // Map formatting is rendered by `process_mode`; assert it directly so the
    // structure check stays independent of the handle-level #361 cap, which
    // legitimately collapses this tiny fixture to raw.
    let content = "pub struct Config {}\n\npub fn build() -> Config { Config {} }\n";
    let (result, _) = process_mode(
        content,
        "map",
        "F1",
        "lib.rs",
        "rs",
        count_tokens(content),
        CrpMode::Off,
        "/tmp/lib.rs",
        None,
    );

    assert!(
        result.contains("API:"),
        "map output should include API: {result}"
    );
    assert!(
        result.contains("struct pub Config @L1"),
        "struct signature should include line suffix: {result}"
    );
    assert!(
        result.contains("fn pub build() → Config @L3"),
        "function signature should include line suffix: {result}"
    );
}

#[test]
fn map_mode_omits_exports_already_in_api() {
    // #361 follow-up: the `exports:` line duplicated symbols the API section
    // already lists with full signatures + line ranges. Map must not repeat
    // exports that the API already covers (pure redundant tokens). Rendered by
    // `process_mode`; assert it directly (handle would cap this tiny fixture).
    let content = "pub struct Config {}\n\npub fn build() -> Config { Config {} }\n";
    let (result, _) = process_mode(
        content,
        "map",
        "F1",
        "lib.rs",
        "rs",
        count_tokens(content),
        CrpMode::Off,
        "/tmp/lib.rs",
        None,
    );

    // Both exported symbols stay discoverable via the API section …
    assert!(
        result.contains("struct pub Config") && result.contains("fn pub build"),
        "API section must still list exported symbols: {result}"
    );
    // … and the redundant `exports:` line is gone (both are in the API).
    assert!(
        !result.contains("exports:"),
        "map must not repeat exports already shown in API: {result}"
    );
}

#[test]
fn tdd_map_output_carries_symbol_legend() {
    // GL #580: symbol notation must be self-describing for vanilla agents.
    // Rendered by `process_mode`; assert it directly (handle caps this fixture).
    let content = "pub struct Config {}\n\npub fn build() -> Config { Config {} }\n";
    let (result, _) = process_mode(
        content,
        "map",
        "F1",
        "lib.rs",
        "rs",
        count_tokens(content),
        CrpMode::Tdd,
        "/tmp/lib.rs",
        None,
    );
    assert!(
        result.contains("[λ=fn §=class +=pub]"),
        "TDD map output must carry the symbol legend: {result}"
    );

    let (sigs, _) = process_mode(
        content,
        "signatures",
        "F1",
        "lib.rs",
        "rs",
        count_tokens(content),
        CrpMode::Tdd,
        "/tmp/lib.rs",
        None,
    );
    assert!(
        sigs.contains("[λ=fn §=class +=pub]"),
        "TDD signatures output must carry the symbol legend: {sigs}"
    );
}

#[test]
fn instruction_file_detection() {
    assert!(is_instruction_file(
        "/home/user/.pi/agent/skills/committing-changes/SKILL.md"
    ));
    assert!(is_instruction_file("/workspace/.cursor/rules/lean-ctx.mdc"));
    assert!(is_instruction_file("/project/AGENTS.md"));
    assert!(is_instruction_file("/project/.cursorrules"));
    assert!(is_instruction_file("/home/user/.claude/rules/my-rule.md"));
    assert!(is_instruction_file("/skills/some-skill/README.md"));

    assert!(!is_instruction_file("/project/src/main.rs"));
    assert!(!is_instruction_file("/project/config.json"));
    assert!(!is_instruction_file("/project/data/report.csv"));
}

/// A plain `ctx_read` of a large CLAUDE.md returned headings only: the
/// standing instruction files of every supported agent are read in full.
#[test]
fn agent_instruction_files_are_read_in_full() {
    for path in [
        "/project/CLAUDE.md",
        "/project/sub/claude.md",
        "/project/CLAUDE.local.md",
        "/home/user/.claude/CLAUDE.md",
        "/project/GEMINI.md",
        "/project/.github/copilot-instructions.md",
        "/project/.windsurfrules",
        "/project/.claude/agents/reviewer.md",
        "/project/.claude/commands/release.md",
        r"C:\Users\nina\project\CLAUDE.md",
    ] {
        assert!(is_instruction_file(path), "{path} carries instructions");
    }
    assert!(!is_instruction_file("/project/docs/claude-notes.md"));
    assert!(!is_instruction_file("/project/.claude/commands/run.sh"));
}

/// #60: `redirect_exclude` was parsed but never applied, so listing CLAUDE.md
/// there changed nothing. Globs match trailing path components.
#[test]
fn redirect_exclude_globs_match_trailing_components() {
    use super::helpers::excluded_by;
    let configured = || {
        vec![
            "docs/**".to_string(),
            "*.json".to_string(),
            "./NOTES.md".to_string(),
            ".claude/**".to_string(),
        ]
    };
    for path in [
        "/project/docs/guide.md",
        "/project/docs/deep/nested/spec.md",
        "/project/package.json",
        "/project/sub/NOTES.md",
        "/home/user/.claude/settings.local.json",
        r"C:\work\project\docs\guide.md",
    ] {
        assert!(excluded_by(path, None, configured), "{path} is excluded");
    }
    for path in [
        "/project/src/docs.rs",
        "/project/src/main.rs",
        "/project/NOTES.txt",
    ] {
        assert!(
            !excluded_by(path, None, configured),
            "{path} is not excluded"
        );
    }
    // Nothing configured: nothing excluded.
    assert!(!excluded_by("/project/CLAUDE.md", None, Vec::new));
}

#[test]
fn hook_exclude_env_takes_precedence_over_config() {
    use super::helpers::excluded_by;
    let configured = || vec!["*.json".to_string()];
    let env = Some(" docs/** , NOTES.md ");
    assert!(excluded_by("/p/docs/a.md", env, configured));
    assert!(excluded_by("/p/NOTES.md", env, configured));
    assert!(
        !excluded_by("/p/package.json", env, configured),
        "env replaces config"
    );
    // A blank env value falls back to the configured globs.
    assert!(excluded_by("/p/package.json", Some("  "), configured));
}

/// #1794: classification is by file, not by ancestor directory. A skill ships
/// its instructions as documents and its implementation as source; forcing the
/// latter to `full` turned a bounded `map` request into a truncated dump.
#[test]
fn source_files_under_a_skill_are_not_instruction_files() {
    for path in [
        "/project/.agents/skills/example/scripts/runtime.ts",
        "/project/.agents/skills/example/scripts/state.ts",
        "/project/skills/demo/helper.py",
        "/project/skills/demo/lib.rs",
        "/workspace/.cursor/rules/generate.js",
        "/home/user/.claude/rules/build.sh",
    ] {
        assert!(
            !is_instruction_file(path),
            "{path} is implementation, not an instruction document"
        );
    }
}

/// The documents themselves must keep their full-read guarantee, including
/// extensionless rule files.
#[test]
fn documents_under_a_skill_remain_instruction_files() {
    for path in [
        "/project/.agents/skills/example/SKILL.md",
        "/project/skills/demo/notes.txt",
        "/project/skills/demo/guide.markdown",
        "/workspace/.cursor/rules/house-style.mdc",
        "/home/user/.claude/rules/PROMPT",
    ] {
        assert!(is_instruction_file(path), "{path} carries instructions");
    }
}

#[test]
fn resolve_auto_mode_returns_full_for_instruction_files() {
    let mode = resolve_auto_mode(
        None,
        "/home/user/.pi/agent/skills/committing-changes/SKILL.md",
        5000,
        None,
        Some("read"),
    );
    assert_eq!(mode, "full", "SKILL.md must always be read in full");

    let mode = resolve_auto_mode(None, "/workspace/AGENTS.md", 3000, None, Some("read"));
    assert_eq!(mode, "full", "AGENTS.md must always be read in full");

    let mode = resolve_auto_mode(None, "/workspace/.cursorrules", 2000, None, None);
    assert_eq!(mode, "full", ".cursorrules must always be read in full");

    // A large CLAUDE.md used to resolve to `map` (headings only).
    let mode = resolve_auto_mode(None, "/workspace/CLAUDE.md", 6000, None, None);
    assert_eq!(mode, "full", "CLAUDE.md must always be read in full");
}