lean-ctx 3.9.16

Context Runtime for AI Agents with CCP. 71 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%.
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Tests for rules injection. `super::*` resolves to the `rules_inject` module.

use super::content::rules_content;
use super::skills::{SKILL_TEMPLATE, build_skill_targets};
use std::sync::OnceLock;

use super::*;
use crate::core::config::CompressionLevel;
use crate::core::rules_canonical::{END_MARK, RULES_VERSION, RulesFile, START_MARK, Wrapper};

fn power() -> crate::core::tool_profiles::ToolProfile {
    crate::core::tool_profiles::ToolProfile::Power
}

fn shared_content() -> String {
    crate::core::rules_canonical::render(false, Wrapper::Shared, CompressionLevel::Off, &power())
}

fn dedicated_content_cached() -> &'static str {
    static RULES: OnceLock<String> = OnceLock::new();
    RULES.get_or_init(|| {
        crate::core::rules_canonical::render(
            false,
            Wrapper::Dedicated,
            CompressionLevel::Off,
            &power(),
        )
    })
}

// ── Canonical rules content ──────────────────────────────────

#[test]
fn shared_rules_have_markers() {
    let s = shared_content();
    assert!(s.contains(START_MARK));
    assert!(s.contains(END_MARK));
    assert!(s.contains(&format!("<!-- version: {RULES_VERSION} -->")));
    assert!(s.contains("MANDATORY MAPPING"));
    assert!(s.contains("NEVER"));
}

#[test]
fn dedicated_rules_have_markers() {
    let d = dedicated_content_cached();
    assert!(d.contains(START_MARK));
    assert!(d.contains(END_MARK));
    assert!(d.contains(&format!("<!-- version: {RULES_VERSION} -->")));
    assert!(d.contains("CRITICAL"));
    assert!(d.contains("intent"));
}

// ── Shadow mode ──────────────────────────────────────────────

#[test]
fn shadow_dedicated_omits_mapping() {
    let rules = crate::core::rules_canonical::render(
        true,
        Wrapper::Dedicated,
        CompressionLevel::Off,
        &power(),
    );
    assert!(
        !rules.contains("MUST USE"),
        "shadow must not include tool mapping"
    );
    assert!(
        !rules.contains("NEVER use native"),
        "shadow must not include native tool admonition"
    );
    assert!(rules.contains(START_MARK), "shadow keeps markers");
    assert!(rules.contains(END_MARK), "shadow keeps markers");
}

#[test]
fn shadow_shared_omits_mapping() {
    let rules = crate::core::rules_canonical::render(
        true,
        Wrapper::Shared,
        CompressionLevel::Off,
        &power(),
    );
    assert!(
        !rules.contains("MANDATORY MAPPING"),
        "shadow shared must not include mapping header"
    );
    assert!(rules.contains(START_MARK), "shadow shared keeps markers");
    assert!(rules.contains(END_MARK), "shadow shared keeps markers");
}

// ── Agent target catalog ─────────────────────────────────────

#[test]
fn zed_rules_path_is_os_aware_and_matches_config_dir() {
    let home = std::path::Path::new("/home/tester");
    let zed = build_rules_targets(home, crate::core::config::RulesInjection::Shared)
        .into_iter()
        .find(|t| t.name == "Zed")
        .expect("Zed rules target must exist");
    let expected = crate::core::editor_registry::zed_config_dir(home).join("rules/lean-ctx.md");
    assert_eq!(zed.path, expected);
}

#[test]
fn target_count() {
    let home = std::path::PathBuf::from("/tmp/fake_home");
    let targets = build_rules_targets(&home, crate::core::config::RulesInjection::Shared);
    // Includes Grok (`~/.grok/AGENTS.md`).
    assert_eq!(targets.len(), 26);
    assert!(
        targets.iter().any(|t| t.name == "Grok"),
        "Grok must have a rules target"
    );
    assert!(
        !targets.iter().any(|t| t.name == "Claude Code"),
        "Claude Code must not get a rules target"
    );
    assert!(
        !targets.iter().any(|t| t.name == "CodeBuddy"),
        "CodeBuddy must not get a rules target"
    );
    let dedicated = build_rules_targets(&home, crate::core::config::RulesInjection::Dedicated);
    assert_eq!(dedicated.len(), 26);
}

#[test]
fn dedicated_mode_swaps_shared_agents_to_dedicated_files() {
    use crate::core::config::RulesInjection;
    let home = std::path::Path::new("/home/tester");

    let shared = build_rules_targets(home, RulesInjection::Shared);
    let gemini_shared = shared.iter().find(|t| t.name == "Gemini CLI").unwrap();
    let opencode_shared = shared.iter().find(|t| t.name == "OpenCode").unwrap();
    assert!(matches!(gemini_shared.format, RulesFormat::SharedMarkdown));
    assert!(gemini_shared.path.ends_with("GEMINI.md"));
    assert!(matches!(
        opencode_shared.format,
        RulesFormat::SharedMarkdown
    ));
    assert!(opencode_shared.path.ends_with("AGENTS.md"));

    let dedicated = build_rules_targets(home, RulesInjection::Dedicated);
    let gemini = dedicated.iter().find(|t| t.name == "Gemini CLI").unwrap();
    let opencode = dedicated.iter().find(|t| t.name == "OpenCode").unwrap();
    assert!(matches!(gemini.format, RulesFormat::DedicatedMarkdown));
    assert_eq!(gemini.path, gemini_dedicated_rules_path(home));
    assert!(!gemini.path.ends_with("GEMINI.md"));
    assert!(matches!(opencode.format, RulesFormat::DedicatedMarkdown));
    assert_eq!(opencode.path, opencode_dedicated_rules_path(home));
    assert!(!opencode.path.ends_with("AGENTS.md"));
}

#[test]
fn rules_catalog_includes_previously_missing_agents_for_detection() {
    let home = std::path::Path::new("/home/tester");
    let names: std::collections::HashSet<&str> = [
        crate::core::config::RulesInjection::Shared,
        crate::core::config::RulesInjection::Dedicated,
    ]
    .iter()
    .flat_map(|inj| build_rules_targets(home, *inj))
    .map(|t| t.name)
    .collect();
    for agent in ["OpenCode", "Zed", "Cline", "Roo Code", "Continue", "Crush"] {
        assert!(
            names.contains(agent),
            "{agent} must be in the rules catalog used for presence detection (#442)"
        );
    }
}

// ── Cursor MDC ───────────────────────────────────────────────

#[test]
fn cursor_mdc_has_frontmatter_and_markers() {
    let mdc = rules_content(
        &RulesFormat::CursorMdc,
        CompressionLevel::Off,
        Wrapper::Dedicated,
        &power(),
    );
    assert!(mdc.contains("alwaysApply: true"));
    assert!(mdc.contains(START_MARK));
    assert!(mdc.contains(END_MARK));
    assert!(mdc.contains(&format!("<!-- version: {RULES_VERSION} -->")));
}

#[test]
fn cursor_wrapper_follows_hook_coverage() {
    // GL #1153: with lean-ctx rewrite+redirect hooks installed next to the
    // mdc, the injector selects the honest HookCovered profile; without them
    // (or with foreign hooks) it stays on the full Dedicated mapping.
    let home = std::env::temp_dir().join("lc_test_cursor_wrapper_coverage");
    let _ = std::fs::remove_dir_all(&home);
    std::fs::create_dir_all(home.join(".cursor/rules")).unwrap();
    let mdc_path = home.join(".cursor/rules/lean-ctx.mdc");

    assert!(matches!(
        super::content::cursor_wrapper_for_mdc(&mdc_path),
        Wrapper::Dedicated
    ));

    std::fs::write(
        home.join(".cursor/hooks.json"),
        r#"{"version":1,"hooks":{"preToolUse":[
            {"matcher":"Shell","command":"/usr/local/bin/lean-ctx hook rewrite"},
            {"matcher":"Read|Grep","command":"/usr/local/bin/lean-ctx hook redirect"}
        ]}}"#,
    )
    .unwrap();
    assert!(matches!(
        super::content::cursor_wrapper_for_mdc(&mdc_path),
        Wrapper::HookCovered
    ));

    let _ = std::fs::remove_dir_all(&home);
}

#[test]
fn inject_cursor_switches_profile_when_hooks_appear() {
    // End-to-end through the real injector: with shadow_mode=true (default),
    // both Dedicated and HookCovered collapse to the same shadow-minimal
    // output, so the test verifies that both states produce valid rules.
    // The non-shadow transition (Dedicated→HookCovered) is covered by the
    // existing shadow_omits_loop_and_paradox test in rules_canonical.
    let _guard = crate::core::data_dir::test_env_lock();
    let home = std::env::temp_dir().join("lc_test_inject_cursor_hookcovered");
    let _ = std::fs::remove_dir_all(&home);
    std::fs::create_dir_all(home.join(".cursor")).unwrap();

    let result = inject_rules_for_agent(&home, "cursor");
    assert!(result.errors.is_empty());
    let mdc_path = home.join(".cursor/rules/lean-ctx.mdc");
    let before = std::fs::read_to_string(&mdc_path).unwrap();
    assert!(
        before.contains("auto-route") || before.contains("MANDATORY MAPPING"),
        "initial rules must contain shadow nudge or full mapping"
    );

    std::fs::write(
        home.join(".cursor/hooks.json"),
        r#"{"version":1,"hooks":{"preToolUse":[
            {"matcher":"Shell","command":"/usr/local/bin/lean-ctx hook rewrite"},
            {"matcher":"Read|Grep","command":"/usr/local/bin/lean-ctx hook redirect"}
        ]}}"#,
    )
    .unwrap();
    let result = inject_rules_for_agent(&home, "cursor");
    assert!(result.errors.is_empty());
    let after = std::fs::read_to_string(&mdc_path).unwrap();
    assert!(
        after.contains("auto-route")
            || (after.contains("ALWAYS prefer lean-ctx")
                && after.contains("Hooks compress native")),
        "with hooks the mdc carries shadow block or HookCovered profile"
    );
    assert!(
        after.contains("alwaysApply: true"),
        "frontmatter survives the profile switch"
    );

    let _ = std::fs::remove_dir_all(&home);
}

// ── RulesFile operations ─────────────────────────────────────

#[test]
fn rules_file_merged_replaces_section_preserving_user_content() {
    let path = std::env::temp_dir().join("test_rules_merged.md");
    let old = format!(
        "user before\n{START_MARK}\n<!-- version: 0 -->\n\nold rules\n{END_MARK}\nuser after"
    );
    std::fs::write(&path, &old).unwrap();

    let content = std::fs::read_to_string(&path).unwrap();
    let file = RulesFile::parse(&content);
    assert!(file.has_content());
    assert_eq!(file.version(), 0);
    assert!(!file.is_current());

    let merged = file.merged(false, Wrapper::Shared, CompressionLevel::Off, &power());
    std::fs::write(&path, &merged).unwrap();

    let result = std::fs::read_to_string(&path).unwrap();
    assert!(result.contains("user before"), "prefix preserved");
    assert!(result.contains("user after"), "suffix preserved");
    assert!(!result.contains("old rules"), "old content replaced");
    assert!(
        result.contains(&format!("<!-- version: {RULES_VERSION} -->")),
        "version updated"
    );

    std::fs::remove_file(&path).ok();
}

#[test]
fn rules_file_merged_appends_when_no_section() {
    let content = "user content only";
    let file = RulesFile::parse(content);
    assert!(!file.has_content());

    let merged = file.merged(false, Wrapper::Shared, CompressionLevel::Off, &power());
    assert!(merged.contains("user content only"));
    assert!(merged.contains(START_MARK));
}

#[test]
fn rules_file_without_section_strips_lean_ctx_block() {
    let content = format!("header\n{START_MARK}\n<!-- version: 1 -->\n\nbody\n{END_MARK}\nfooter");
    let file = RulesFile::parse(&content);
    let stripped = file.without_section();
    assert!(stripped.contains("header"));
    assert!(stripped.contains("footer"));
    assert!(!stripped.contains("body"));
    assert!(!stripped.contains(START_MARK));
}

// ── Injection ────────────────────────────────────────────────

#[test]
fn inject_rules_for_agent_opencode() {
    let home = std::env::temp_dir().join("test_inject_rules_agent");
    let _ = std::fs::remove_dir_all(&home);
    let _ = std::fs::create_dir_all(&home);

    let opencode_dir = home.join(".config/opencode");
    let _ = std::fs::create_dir_all(&opencode_dir);

    let result = inject_rules_for_agent(&home, "opencode");
    assert!(
        !result.updated.is_empty() || !result.already.is_empty(),
        "should inject or find rules for OpenCode"
    );
    assert!(result.errors.is_empty(), "no errors expected");

    let agents_md = opencode_dir.join("AGENTS.md");
    if agents_md.exists() {
        let content = std::fs::read_to_string(&agents_md).unwrap();
        assert!(content.contains(START_MARK));
    }

    let _ = std::fs::remove_dir_all(&home);
}

#[test]
fn inject_rules_for_agent_cursor() {
    let home = std::env::temp_dir().join("test_inject_rules_cursor");
    let _ = std::fs::remove_dir_all(&home);
    let _ = std::fs::create_dir_all(&home);

    let cursor_dir = home.join(".cursor");
    let _ = std::fs::create_dir_all(&cursor_dir);

    let result = inject_rules_for_agent(&home, "cursor");
    assert!(result.errors.is_empty(), "no errors expected");

    let mdc_path = home.join(".cursor/rules/lean-ctx.mdc");
    if mdc_path.exists() {
        let content = std::fs::read_to_string(&mdc_path).unwrap();
        assert!(content.contains(START_MARK));
    }

    let _ = std::fs::remove_dir_all(&home);
}

#[test]
fn inject_rules_for_unknown_agent_is_empty() {
    let home = std::path::PathBuf::from("/tmp/fake_home_unknown");
    let result = inject_rules_for_agent(&home, "unknown_agent_xyz");
    assert!(result.updated.is_empty());
    assert!(result.already.is_empty());
    assert!(result.errors.is_empty());
}

#[test]
fn inject_rewrites_on_compression_change_without_version_bump() {
    // #548: a version-only freshness check skips the rewrite when the
    // compression level changes but RULES_VERSION stays the same. Drive the real
    // inject path through LEAN_CTX_COMPRESSION to prove the block is regenerated.
    let _guard = crate::core::data_dir::test_env_lock();

    let dir = std::env::temp_dir().join("lc_test_inject_compression_drift");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    let target = RulesTarget {
        name: "test",
        path: dir.join("lean-ctx.md"),
        format: RulesFormat::DedicatedMarkdown,
    };

    crate::test_env::set_var("LEAN_CTX_COMPRESSION", "off");
    assert!(matches!(
        super::write::inject_rules(&target).unwrap(),
        RulesResult::Updated
    ));
    let off = std::fs::read_to_string(&target.path).unwrap();

    // Same level → idempotent (block already matches a fresh render).
    assert!(matches!(
        super::write::inject_rules(&target).unwrap(),
        RulesResult::AlreadyPresent
    ));

    // Switch to max → must rewrite even though RULES_VERSION is unchanged.
    crate::test_env::set_var("LEAN_CTX_COMPRESSION", "max");
    assert!(matches!(
        super::write::inject_rules(&target).unwrap(),
        RulesResult::Updated
    ));
    let max = std::fs::read_to_string(&target.path).unwrap();

    crate::test_env::remove_var("LEAN_CTX_COMPRESSION");
    let _ = std::fs::remove_dir_all(&dir);

    assert_ne!(off, max, "compression-level change must alter the body");
    assert!(max.contains(&format!("<!-- version: {RULES_VERSION} -->")));
}

#[test]
fn any_rules_marker_present_detects_opencode() {
    let home = std::env::temp_dir().join("lc_test_marker_opencode");
    let _ = std::fs::remove_dir_all(&home);
    let opencode_dir = home.join(".config/opencode");
    std::fs::create_dir_all(&opencode_dir).unwrap();
    std::fs::write(
        opencode_dir.join("AGENTS.md"),
        format!("# preamble\n\n{}\n", shared_content()),
    )
    .unwrap();
    assert!(
        any_rules_marker_present(&home),
        "OpenCode AGENTS.md with the lean-ctx marker must be detected"
    );
    let _ = std::fs::remove_dir_all(&home);
}

// ── Skills ───────────────────────────────────────────────────

#[test]
fn skill_template_not_empty() {
    assert!(!SKILL_TEMPLATE.is_empty());
    assert!(SKILL_TEMPLATE.contains("lean-ctx"));
}

#[test]
fn skill_template_lean_profile_matches_core_tool_registry() {
    let lean_row = SKILL_TEMPLATE
        .lines()
        .find(|line| line.starts_with("| Lean (default, unpinned) |"))
        .expect("SKILL.md must document the default lean tool surface");

    for tool in crate::tool_defs::core_tool_names() {
        assert!(
            lean_row.contains(&format!("`{tool}`")),
            "default lean row is missing {tool}"
        );
    }
    assert!(
        lean_row.contains("`ctx_patch`*"),
        "client-dependent ctx_patch visibility must be marked"
    );
    for hidden in ["ctx_knowledge", "ctx_overview", "ctx_graph"] {
        assert!(
            !lean_row.contains(hidden),
            "{hidden} is not part of the default lean surface"
        );
    }
}

#[test]
fn skill_targets_count() {
    // Claude, CodeBuddy, Cursor, Codex, Copilot, Grok, OpenClaw + OpenCode (GH #686).
    let home = std::path::PathBuf::from("/tmp/fake_home");
    let targets = build_skill_targets(&home);
    assert_eq!(targets.len(), 8);
}

#[test]
fn install_skill_creates_file() {
    let home = std::env::temp_dir().join("test_skill_install");
    let _ = std::fs::create_dir_all(&home);
    let fake_cursor = home.join(".cursor");
    let _ = std::fs::create_dir_all(&fake_cursor);

    let result = install_skill_for_agent(&home, "cursor");
    assert!(result.is_ok());
    let path = result.unwrap();
    assert!(path.exists());
    let content = std::fs::read_to_string(&path).unwrap();
    assert_eq!(content, SKILL_TEMPLATE);
    let _ = std::fs::remove_dir_all(&home);
}

#[test]
fn install_skill_idempotent() {
    let home = std::env::temp_dir().join("test_skill_idempotent");
    let _ = std::fs::create_dir_all(&home);
    let fake_cursor = home.join(".cursor");
    let _ = std::fs::create_dir_all(&fake_cursor);
    let p1 = install_skill_for_agent(&home, "cursor").unwrap();
    let p2 = install_skill_for_agent(&home, "cursor").unwrap();
    assert_eq!(p1, p2);
    let _ = std::fs::remove_dir_all(&home);
}

#[test]
fn install_skill_unknown_agent() {
    let home = std::path::PathBuf::from("/tmp/fake_home");
    let result = install_skill_for_agent(&home, "unknown_agent");
    assert!(result.is_err());
}

// ── Agent name matching ──────────────────────────────────────

#[test]
fn match_agent_name_basic() {
    assert!(match_agent_name("cursor", "Cursor"));
    assert!(match_agent_name("opencode", "OpenCode"));
    assert!(match_agent_name("claude", "Claude Code"));
    assert!(match_agent_name("vscode", "VS Code"));
    assert!(match_agent_name("copilot", "Copilot CLI"));
    assert!(match_agent_name("kiro", "AWS Kiro"));
    assert!(match_agent_name("pi", "Pi Coding Agent"));
    assert!(match_agent_name("crush", "Crush"));
    assert!(match_agent_name("amp", "Amp"));
    assert!(match_agent_name("cline", "Cline"));
    assert!(match_agent_name("roo", "Roo Code"));
    assert!(match_agent_name("trae", "Trae"));
    assert!(match_agent_name("amazonq", "Amazon Q Developer"));
    assert!(match_agent_name("verdent", "Verdent"));
    assert!(match_agent_name("continue", "Continue"));
    assert!(match_agent_name("antigravity", "Antigravity"));
    assert!(match_agent_name("codebuddy", "CodeBuddy"));
    assert!(match_agent_name("gemini", "Gemini CLI"));
    assert!(match_agent_name("augment", "Augment"));
    assert!(match_agent_name("openclaw", "OpenClaw"));
    assert!(match_agent_name("qodercli", "Qoder"));
}

#[test]
fn match_agent_name_no_false_positives() {
    assert!(!match_agent_name("cursor", "Claude Code"));
    assert!(!match_agent_name("opencode", "Cursor"));
    assert!(!match_agent_name("unknown_agent", "Cursor"));
}

// ── InjectResult ─────────────────────────────────────────────

#[test]
fn inject_result_tracks_backed_up_files() {
    let result = InjectResult {
        backed_up: vec!["/tmp/test.md.bak".to_string()],
        ..Default::default()
    };
    assert_eq!(result.backed_up.len(), 1);
    assert!(
        std::path::Path::new(&result.backed_up[0])
            .extension()
            .is_some_and(|ext| ext.eq_ignore_ascii_case("bak"))
    );
}