lean-ctx-server 3.12.2

The LeanCTX MCP server and its tools on top of LeanCTX | OSS.
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// SPDX-License-Identifier: Apache-2.0

use super::*;
use lean_ctx::core::triage::profile::TaskProfileLocal;

/// The bounce tracker is process-global: the tests that arm it (and the one
/// asserting it is unarmed) run one at a time, or their `set_seq` calls and
/// reads interleave and flip each other's verdicts.
fn tracker_tests() -> std::sync::MutexGuard<'static, ()> {
    static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
    LOCK.lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// A tool call without task text yields the rules-fallback profile, which
/// must pass output through unfiltered.
#[test]
fn missing_task_text_profile_passes_output_through() {
    use lean_ctx::core::decision_loop_runtime::DecisionLoopRuntime;
    use lean_ctx::core::triage::TriageEngine;

    let runtime = DecisionLoopRuntime::with_triage(TriageEngine::default());
    runtime.on_tool_start("ctx_read", None, "gate-no-task-text", "agent");
    let profile = runtime.profile_for_session("gate-no-task-text").unwrap();
    assert_eq!(triage_filter_level(&profile), 0);
}

#[test]
fn eviction_target_display_emits_resolvable_targets() {
    // #715: root-relative when under the root, full path otherwise —
    // never a display-shortened form the resolver cannot find.
    assert_eq!(
        eviction_target_display("/w/proj/src/a.rs", Some("/w/proj")),
        "src/a.rs"
    );
    assert_eq!(
        eviction_target_display("/other/b.rs", Some("/w/proj")),
        "/other/b.rs"
    );
    assert_eq!(eviction_target_display("/x/c.rs", None), "/x/c.rs");
}

#[test]
fn pre_dispatch_passthrough_for_full() {
    let result = pre_dispatch_read("src/main.rs", "full", None, None);
    assert!(result.overridden_mode.is_none());
}

#[test]
fn pre_dispatch_passthrough_for_diff() {
    let result = pre_dispatch_read("src/main.rs", "diff", None, None);
    assert!(result.overridden_mode.is_none());
}

#[test]
fn pre_dispatch_passthrough_for_anchored_window() {
    let _tracker = tracker_tests();
    // #843: a windowed anchored:N-M read must keep its hash anchors —
    // bounce-prevention and the other heuristics must not clobber it to
    // "full" and silently drop the window.
    {
        let mut bt = lean_ctx::core::bounce_tracker::global()
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        bt.set_seq(101);
        bt.record_read("anchored-bouncy.yml", "map", 30, 400);
        bt.set_seq(102);
        bt.record_read("anchored-bouncy.yml", "full", 400, 400);
        bt.set_seq(103);
        bt.record_read("a2.yml", "map", 30, 400);
        bt.set_seq(104);
        bt.record_read("a2.yml", "full", 400, 400);
        bt.set_seq(105);
        bt.record_read("a3.yml", "map", 30, 400);
        bt.set_seq(106);
        bt.record_read("a3.yml", "full", 400, 400);
    }
    let result = pre_dispatch_read("anchored-new.yml", "anchored:10-40", None, None);
    assert!(
        result.overridden_mode.is_none(),
        "anchored:N-M must not be overridden by bounce-prevention"
    );
    let bare = pre_dispatch_read("anchored-new.yml", "anchored", None, None);
    assert!(
        bare.overridden_mode.is_none(),
        "bare anchored mode must not be overridden by bounce-prevention"
    );
}

#[test]
fn pre_dispatch_passthrough_for_lines_multi_select() {
    let _tracker = tracker_tests();
    // #971: `lines:A-B,C-D` is a precise pinned window exactly like
    // `lines:A-B`, but it parsed as Malformed, so `is_precise_pinned_mode`
    // reported "not pinned" and bounce-prevention rewrote an 8-line request
    // into a full-file read. Exercises the edit-forced branch, which is what
    // the field report actually hit (a ctx_patch anchored edit immediately
    // before the read).
    {
        let mut bt = lean_ctx::core::bounce_tracker::global()
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        bt.set_seq(201);
        bt.record_edit("multi-select-971.rs");
        bt.set_seq(202);
    }

    // Precondition: the tracker really is armed for this path, so the
    // assertions below cannot pass vacuously.
    let forced = pre_dispatch_read("multi-select-971.rs", "map", None, None);
    assert_eq!(
        forced.overridden_mode.as_deref(),
        Some("full"),
        "precondition: a recent edit must force a non-pinned mode to full"
    );

    let multi = pre_dispatch_read("multi-select-971.rs", "lines:620-622,1214-1218", None, None);
    assert!(
        multi.overridden_mode.is_none(),
        "lines:A-B,C-D must not be overridden by bounce-prevention (#971)"
    );

    // Control: the single-range form was already protected.
    let single = pre_dispatch_read("multi-select-971.rs", "lines:620-622", None, None);
    assert!(
        single.overridden_mode.is_none(),
        "lines:A-B must not be overridden by bounce-prevention"
    );
}

#[test]
fn fresh_escapes_bounce_prevention() {
    let _tracker = tracker_tests();
    // #1588: after an edit the gate pins the file to `full`. Without an
    // escape hatch, signatures/map/reference stay unreachable for the rest
    // of the session — `fresh=true` is that hatch.
    {
        let mut bt = lean_ctx::core::bounce_tracker::global()
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        bt.set_seq(301);
        bt.record_edit("fresh-escape-1586.rs");
        bt.set_seq(302);
    }

    let pinned = pre_dispatch_read("fresh-escape-1586.rs", "signatures", None, None);
    assert_eq!(
        pinned.overridden_mode.as_deref(),
        Some("full"),
        "precondition: the tracker must actually be armed for this path"
    );

    let escaped =
        pre_dispatch_read_for_agent("fresh-escape-1586.rs", "signatures", None, None, None, true);
    assert!(
        escaped.overridden_mode.is_none(),
        "fresh=true must keep the requested mode reachable"
    );
}

#[test]
fn intent_target_matches_whole_components_only() {
    // #1588: substring matching pinned unrelated files to `full`.
    assert!(path_matches_target(
        "/repo/src/server/context_gate.rs",
        "context_gate"
    ));
    assert!(path_matches_target(
        "/repo/src/server/context_gate.rs",
        "context_gate.rs"
    ));
    assert!(path_matches_target(
        "/repo/src/server/context_gate.rs",
        "src/server/context_gate.rs"
    ));

    assert!(
        !path_matches_target("/repo/src/printer/link_gate.rs", "print"),
        "a target must not match the middle of a longer component"
    );
    assert!(
        !path_matches_target("/repo/src/server/context_gate.rs", "src"),
        "targets below the minimum length are noise, not names"
    );
    assert!(!path_matches_target(
        "/repo/src/server/context_gate.rs",
        "gate"
    ));
}

#[test]
fn fresh_escapes_intent_target() {
    let result = pre_dispatch_read_for_agent(
        "/repo/src/server/context_gate.rs",
        "reference",
        Some("fix context_gate overrides"),
        None,
        None,
        true,
    );
    assert!(
        result.overridden_mode.is_none(),
        "fresh=true must also escape the intent-target override"
    );
}

#[test]
fn pinned_modes_preserve_budget_warning() {
    let agent_id = format!(
        "context_gate_pinned_advisory_{:?}",
        std::thread::current().id()
    );
    let path = format!(
        "context-gate-pinned-advisory-{:?}.rs",
        std::thread::current().id()
    );
    lean_ctx::core::agent_budget::remove(&agent_id);
    lean_ctx::core::agent_budget::set_limit(&agent_id, 10_000);
    lean_ctx::core::agent_budget::record_consumption(&agent_id, 8_000);

    for mode in ["full", "map", "aggressive"] {
        let result = pre_dispatch_pinned_read_for_agent(&path, mode, Some(&agent_id));
        assert!(result.overridden_mode.is_none(), "mode={mode}");
        assert!(result.reason.is_none(), "mode={mode}");
        assert!(!result.budget_blocked, "mode={mode}");
        assert_eq!(result.triage_filter_level, 0, "mode={mode}");
        assert!(
            result
                .budget_warning
                .as_deref()
                .is_some_and(|warning| warning.contains("BUDGET WARNING")),
            "mode={mode} must retain the budget warning"
        );
    }

    lean_ctx::core::agent_budget::remove(&agent_id);
}

#[test]
fn pinned_read_still_blocks_exceeded_budget() {
    let agent_id = format!(
        "context_gate_pinned_exceeded_{:?}",
        std::thread::current().id()
    );
    lean_ctx::core::agent_budget::remove(&agent_id);
    lean_ctx::core::agent_budget::set_limit(&agent_id, 1_000);
    lean_ctx::core::agent_budget::record_consumption(&agent_id, 900);

    let result = pre_dispatch_pinned_read_for_agent(
        "context-gate-pinned-exceeded-missing.rs",
        "map",
        Some(&agent_id),
    );
    assert!(result.overridden_mode.is_none());
    assert_eq!(result.reason, Some("agent-budget-exceeded"));
    assert!(result.budget_blocked);
    assert!(
        result
            .budget_warning
            .as_deref()
            .is_some_and(|warning| warning.contains("900/1000"))
    );
    assert_eq!(result.triage_filter_level, 0);

    lean_ctx::core::agent_budget::remove(&agent_id);
}

#[test]
fn pre_dispatch_no_override_without_signals() {
    let _tracker = tracker_tests();
    let result = pre_dispatch_read("src/unknown.rs", "auto", None, None);
    assert!(result.overridden_mode.is_none());
}

#[test]
fn pre_dispatch_bounce_prevention_forces_full() {
    let _tracker = tracker_tests();
    {
        let mut bt = lean_ctx::core::bounce_tracker::global()
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        bt.set_seq(1);
        bt.record_read("src/bouncy.yml", "map", 30, 400);
        bt.set_seq(2);
        bt.record_read("src/bouncy.yml", "full", 400, 400);
        bt.set_seq(3);
        bt.record_read("a2.yml", "map", 30, 400);
        bt.set_seq(4);
        bt.record_read("a2.yml", "full", 400, 400);
        bt.set_seq(5);
        bt.record_read("a3.yml", "map", 30, 400);
        bt.set_seq(6);
        bt.record_read("a3.yml", "full", 400, 400);
        // Bounces are provisional until their edit window passes.
        bt.flush_pending();
    }
    let result = pre_dispatch_read("new.yml", "auto", None, None);
    assert_eq!(result.overridden_mode, Some("full".to_string()));
    assert_eq!(result.reason, Some("bounce-prevention"));
}

#[test]
fn learned_full_applies_to_explicit_compressing_modes() {
    let path = "learned-full-gate-496.cfg";
    let norm = lean_ctx::core::pathutil::normalize_tool_path(path);
    lean_ctx::core::path_mode_memory::record_bounce(&norm);
    lean_ctx::core::path_mode_memory::record_bounce(&norm);

    for mode in [
        "map",
        "signatures",
        "aggressive",
        "entropy",
        "task",
        "reference",
    ] {
        let result = pre_dispatch_read(path, mode, None, None);
        assert_eq!(result.overridden_mode.as_deref(), Some("full"), "{mode}");
        assert_eq!(result.reason, Some("learned-full"), "{mode}");
    }
    for mode in ["lines:1-5", "diff", "raw", "anchored", "full"] {
        let result = pre_dispatch_read(path, mode, None, None);
        assert_ne!(
            result.reason,
            Some("learned-full"),
            "{mode} stays untouched"
        );
    }
    let escaped = pre_dispatch_read_for_agent(path, "map", None, None, None, true);
    assert_ne!(escaped.reason, Some("learned-full"), "fresh=true escapes");
}

#[test]
fn post_dispatch_reinjection_downgrades_entries() {
    let mut ledger = ContextLedger::with_window_size(1000);
    ledger.record("a.rs", "full", 400, 400);
    ledger.record("b.rs", "full", 400, 400);
    let overlay = OverlayStore::new();
    let result = post_dispatch_record("c.rs", "full", 300, 300, &mut ledger, &overlay);
    assert!(result.resource_changed);
    let a_entry = ledger.entries.iter().find(|e| e.path == "a.rs").unwrap();
    assert_eq!(a_entry.mode, "map");
}

#[test]
fn ignited_item_resists_reinjection_downgrade() {
    // #6: a high-salience outlier ignites (pins) and keeps its full view,
    // while the rest are downgraded to map by pressure reinjection.
    let mut ledger = ContextLedger::with_window_size(1000);
    for i in 0..5 {
        ledger.record(&format!("bg{i}.rs"), "full", 250, 250);
    }
    ledger.record("hot.rs", "full", 250, 250);
    // Set the salience distribution explicitly (record recomputes Phi, so we
    // overwrite afterwards) to make ignition deterministic in the test.
    for e in &mut ledger.entries {
        e.phi = Some(if e.path == "hot.rs" { 0.97 } else { 0.1 });
    }
    let ignited = ledger.ignite_high_salience();
    assert_eq!(ignited, vec!["hot.rs".to_string()], "outlier should ignite");

    apply_reinjection_plan(&mut ledger, &PressureAction::ForceCompression);
    let hot = ledger.entries.iter().find(|e| e.path == "hot.rs").unwrap();
    assert_eq!(hot.mode, "full", "ignited item keeps its full view");
    let bg = ledger.entries.iter().find(|e| e.path == "bg0.rs").unwrap();
    assert_eq!(bg.mode, "map", "non-ignited items are downgraded");
}

#[test]
fn overlay_pin_forces_full_mode() {
    let dir = tempfile::tempdir().expect("tmp dir");
    let root = dir.path();
    let mut store = OverlayStore::new();
    let target = ContextItemId::from_file("src/important.rs");
    store.add(lean_ctx::core::context_overlay::ContextOverlay::new(
        target,
        OverlayOp::Pin { verbatim: false },
        lean_ctx::core::context_overlay::OverlayScope::Project,
        String::new(),
        lean_ctx::core::context_overlay::OverlayAuthor::User,
    ));
    store.save_project(root).unwrap();

    let result = pre_dispatch_read(
        "src/important.rs",
        "auto",
        None,
        Some(root.to_str().unwrap()),
    );
    assert_eq!(result.overridden_mode, Some("full".to_string()));
    assert_eq!(result.reason, Some("pinned"));
}

#[test]
fn overlay_exclude_forces_signatures_mode() {
    let dir = tempfile::tempdir().expect("tmp dir");
    let root = dir.path();
    let mut store = OverlayStore::new();
    let target = ContextItemId::from_file("src/noisy.rs");
    store.add(lean_ctx::core::context_overlay::ContextOverlay::new(
        target,
        OverlayOp::Exclude {
            reason: "noise".to_string(),
        },
        lean_ctx::core::context_overlay::OverlayScope::Project,
        String::new(),
        lean_ctx::core::context_overlay::OverlayAuthor::User,
    ));
    store.save_project(root).unwrap();

    let result = pre_dispatch_read("src/noisy.rs", "auto", None, Some(root.to_str().unwrap()));
    assert_eq!(result.overridden_mode, Some("signatures".to_string()));
    assert_eq!(result.reason, Some("excluded"));
}

// --- estimate_read_tokens unit tests ---

#[test]
fn estimate_tokens_diff_mode_is_small() {
    let tokens = estimate_read_tokens("nonexistent.rs", "diff");
    assert!(tokens < 500, "diff mode should estimate low: got {tokens}");
}

#[test]
fn estimate_tokens_signatures_smaller_than_full() {
    let sig = estimate_read_tokens("nonexistent.rs", "signatures");
    let full = estimate_read_tokens("nonexistent.rs", "full");
    assert!(sig < full, "signatures={sig} should be < full={full}");
}

#[test]
fn estimate_tokens_lines_range() {
    let tokens = estimate_read_tokens("nonexistent.rs", "lines:1-10");
    assert!(tokens <= 200, "lines:1-10 should be small: got {tokens}");
}

#[test]
fn overlay_set_view_forces_specified_mode() {
    let dir = tempfile::tempdir().expect("tmp dir");
    let root = dir.path();
    let mut store = OverlayStore::new();
    let target = ContextItemId::from_file("src/big.rs");
    store.add(lean_ctx::core::context_overlay::ContextOverlay::new(
        target,
        OverlayOp::SetView(lean_ctx::core::context_field::ViewKind::Map),
        lean_ctx::core::context_overlay::OverlayScope::Project,
        String::new(),
        lean_ctx::core::context_overlay::OverlayAuthor::User,
    ));
    store.save_project(root).unwrap();

    let result = pre_dispatch_read("src/big.rs", "auto", None, Some(root.to_str().unwrap()));
    assert_eq!(result.overridden_mode, Some("map".to_string()));
    assert_eq!(result.reason, Some("overlay-set-view"));
}
use lean_ctx::core::triage::profile::TaskScopeLocal;

fn test_profile(confidence: u16, context_need: u16) -> TaskProfileLocal {
    TaskProfileLocal {
        task_class: "bug_fix".into(),
        intent: "fix context gate filtering".into(),
        complexity: "low".into(),
        scope: TaskScopeLocal::SingleFile,
        context_need_milli: context_need,
        reasoning_need_milli: 0,
        risk_signal_milli: 0,
        confidence_milli: confidence,
    }
}

#[test]
fn triage_filter_level_selects_expected_level() {
    for (confidence, context_need, expected) in [
        (200, 200, 0),
        // context_need == 0 means "unknown", never "needs no context" —
        // the fallback profile must pass through untouched.
        (300, 0, 0),
        (500, 0, 0),
        (500, 200, 1),
        (500, 450, 1),
        (500, 700, 0),
        // A rules-derived profile never selects the lossy level 2, no
        // matter how confident the intent classification is.
        (700, 200, 1),
        (700, 450, 1),
        (700, 700, 0),
        (1000, 100, 1),
    ] {
        assert_eq!(
            triage_filter_level(&test_profile(confidence, context_need)),
            expected,
            "confidence={confidence} context_need={context_need}"
        );
    }
}

#[test]
fn no_model_installed_means_passthrough() {
    use lean_ctx::core::triage::{TaskAnalysisInput, TaskAnalyzer, rules::RuleTriageBackend};

    let profile = RuleTriageBackend
        .analyze(&TaskAnalysisInput::default())
        .unwrap()
        .profile;
    assert_eq!(triage_filter_level(&profile), 0);
}

#[test]
fn apply_triage_filter_level_zero_is_passthrough() {
    let profile = test_profile(500, 200);
    let output = "// boilerplate\n".repeat(40);
    assert_eq!(apply_triage_filter(&output, &profile, 0), (output, 0));
}

#[test]
fn apply_triage_filter_short_output_is_passthrough() {
    let profile = test_profile(500, 200);
    let output = "fn main() {\n    println!(\"hi\");\n}";
    assert_eq!(apply_triage_filter(output, &profile, 2), (output.into(), 0));
}

// #1570 P4: a <protect> span is a user contract — the whole output is
// exempt from lossy filtering at every level.
#[test]
fn protect_span_bypasses_lossy_filtering_entirely() {
    let profile = test_profile(700, 200);
    let output = "// boilerplate noise\n".repeat(30)
        + "<protect>operational checklist — must stay verbatim</protect>\n";
    let (filtered, removed) = apply_triage_filter(&output, &profile, 2);
    assert_eq!(removed, 0, "protected output must lose nothing");
    assert_eq!(filtered, output);
}

#[test]
fn apply_triage_filter_level_one_preserves_actionable_comments() {
    let profile = test_profile(500, 450);
    let output = "// boilerplate comment\n".repeat(30)
        + "// TODO: keep this\n// FIXME: keep this\n// SAFETY: keep this\nfn keep_me() {}\n";
    let (filtered, removed) = apply_triage_filter(&output, &profile, 1);
    assert!(!filtered.contains("// boilerplate comment"));
    assert!(filtered.contains("// TODO: keep this"));
    assert!(filtered.contains("// FIXME: keep this"));
    assert!(filtered.contains("// SAFETY: keep this"));
    assert!(
        filtered.contains("[lean-ctx: 30 lines filtered by triage (level 1) — rerun with raw=true")
    );
    assert_eq!(removed, 30);
}

/// Community regression (3.9.19 "abridged into something that still looks
/// right"): level 2 on NON-markdown content must never delete a
/// non-comment line — plain `let` bindings used to vanish from source
/// files while the survivors still parsed.
#[test]
fn apply_triage_filter_level_two_never_drops_code_lines() {
    let profile = test_profile(700, 200);
    let output = "// boilerplate noise\n".repeat(20)
        + "pub fn read_identity() -> Identity {\n"
        + "    let serial = read_serial();\n"
        + "    let model = read_model();\n"
        + "    Identity { serial, model }\n"
        + "}\n";
    let (filtered, removed) = apply_triage_filter(&output, &profile, 2);
    // Every code line survives — especially the plain `let` bindings that
    // carry neither braces, keywords, nor task keywords.
    assert!(filtered.contains("let serial = read_serial();"));
    assert!(filtered.contains("let model = read_model();"));
    assert!(filtered.contains("Identity { serial, model }"));
    // Only the boilerplate comments were stripped, and the footer names
    // the escape hatch.
    assert_eq!(removed, 20);
    assert!(filtered.contains("rerun with raw=true"));
}

#[test]
fn apply_triage_filter_level_two_keeps_keywords_and_structure() {
    let profile = test_profile(500, 200);
    let output = "// unrelated noise about widgets\n".repeat(20)
        + "fix the context gate\n"
        + "pub fn unrelated() {}\n"
        + "#[derive(Debug)]\n"
        + "mod inner {}\n";
    let (filtered, removed) = apply_triage_filter(&output, &profile, 2);
    assert!(filtered.contains("fix the context gate"));
    assert!(filtered.contains("pub fn unrelated() {}"));
    assert!(filtered.contains("#[derive(Debug)]"));
    assert!(filtered.contains("mod inner {}"));
    assert!(filtered.contains("[lean-ctx:"));
    assert_eq!(removed, 20);
}

#[test]
fn benchmark_apply_triage_filter() {
    use std::time::Instant;

    let profile = test_profile(500, 200);
    // Use a mix of keyword-matching and non-matching lines so triage
    // actually filters (100% deletion is now a passthrough per #1493).
    let output = "// unrelated noise about widgets\n".repeat(1_999) + "fix the context gate\n";

    let started = Instant::now();
    for _ in 0..1_000 {
        let (_, removed) = apply_triage_filter(&output, &profile, 2);
        assert_eq!(removed, 1_999);
    }
    let average = started.elapsed() / 1_000;
    println!("benchmark_apply_triage_filter: {average:?} per call");
}

#[test]
fn gh1493_total_deletion_returns_original_output() {
    let profile = test_profile(500, 200);
    // 30 lines of plain file names — no keyword match, would be 100% deleted
    let output = (1..=30)
        .map(|i| format!("plugin/device_{i:03}.go"))
        .collect::<Vec<_>>()
        .join("\n");
    let (filtered, removed) = apply_triage_filter(&output, &profile, 2);
    assert_eq!(
        removed, 0,
        "100% deletion must be prevented — output should pass through"
    );
    assert_eq!(filtered, output);
}

#[test]
fn extract_task_keywords_normalizes_and_deduplicates() {
    assert_eq!(
        extract_task_keywords("bug_fix", "Fix context-gate"),
        ["bug", "context", "fix", "gate"]
    );
}

#[test]
fn markdown_level_two_keeps_section_leads_not_just_headings() {
    let profile = test_profile(500, 200);
    let filler = "Later filler that restates the same loop in racing language. ".repeat(8);
    let extra = "Ignore this extra dashboard discussion. ".repeat(8);
    let output = [
        "# LeanCTX: The Context SDK for AI Agents",
        "",
        "## The context-performance loop",
        "Connect, measure, tune, prove, deploy, repeat.",
        "",
        filler.as_str(),
        "",
        "## The Receipt is the trust product",
        "A Receipt is evidence, not a decorative log.",
        "",
        extra.as_str(),
        "",
        "## Guardrails",
        "Local runtime, SDK, CLI, and Receipt stay useful offline.",
        "",
        extra.as_str(),
    ]
    .join("\n");
    let (filtered, removed) = apply_triage_filter(&output, &profile, 2);
    assert!(
        filtered.contains("Connect, measure, tune, prove"),
        "section lead must survive: {filtered}"
    );
    assert!(
        filtered.contains("A Receipt is evidence"),
        "receipt lead must survive: {filtered}"
    );
    assert!(
        !filtered.contains("racing language"),
        "later filler should drop: {filtered}"
    );
    assert!(removed > 0);
}