lean-ctx-server 3.12.3

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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// SPDX-License-Identifier: Apache-2.0
use lean_ctx::core::context_field::{ContextItemId, ContextState};
use lean_ctx::core::context_ledger::{ContextLedger, PressureAction};
use lean_ctx::core::context_overlay::{OverlayOp, OverlayStore};

/// #1570 P4: protected path arguments bypass every lossy output filter.
pub(super) fn protected_path_requested(
    args: Option<&serde_json::Map<String, serde_json::Value>>,
) -> bool {
    let Some(args) = args else {
        return false;
    };
    let config = lean_ctx::core::config::Config::load();
    if config.protection.file_patterns.is_empty() {
        return false;
    }
    ["path", "file_path", "filePath"].iter().any(|key| {
        args.get(*key)
            .and_then(serde_json::Value::as_str)
            .is_some_and(|path| config.protection.path_is_protected(path))
    })
}

/// #843: precise, pinned reads (`diff`, `lines:N-M`, `anchored`/`anchored:N-M`)
/// must pass through every mode-override path untouched — bounce-prevention
/// and the graph/knowledge heuristics below must never
/// silently reinterpret one of these to e.g. `full`, which would discard the
/// exact window/anchors/delta the caller asked for. Classifies off the typed
/// `ReadMode` (single source of truth, see `tools::ctx_read::mode`) rather than
/// a hand-maintained string-prefix list, so a future precise mode has to be
/// added to `ReadMode::is_precise_pinned_read` explicitly instead of silently
/// falling through an allowlist. An unparseable mode is conservatively treated
/// as *not* pinned, matching prior behaviour for anything outside this set.
fn is_precise_pinned_mode(requested_mode: &str) -> bool {
    requested_mode
        .parse::<crate::tools::ctx_read::ReadMode>()
        .is_ok_and(|m| m.is_precise_pinned_read())
}

#[derive(Debug, Clone)]
pub struct PreDispatchResult {
    pub overridden_mode: Option<String>,
    pub reason: Option<&'static str>,
    pub budget_blocked: bool,
    pub budget_warning: Option<String>,
    /// Triage-based output filter aggressiveness: 0=passthrough, 1=moderate, 2=aggressive.
    pub triage_filter_level: u8,
}

#[derive(Debug, Clone)]
pub struct PostDispatchResult {
    pub eviction_hint: Option<String>,
    pub elicitation_hint: Option<String>,
    pub resource_changed: bool,
}

/// Best-effort KnowledgeRouter advice for the completed Context Gate flow.
pub fn knowledge_advice(
    query: &str,
) -> lean_ctx::core::knowledge_router::gate_integration::KnowledgeAdvice {
    let current = lean_ctx::core::context_kernel::ContextState::new(
        Vec::new(),
        lean_ctx::core::knowledge::KnowledgeQuery::default(),
    );
    lean_ctx::core::knowledge_router::gate_integration::KnowledgeGateAdvisor::advise(
        query, &current,
    )
}

pub fn pre_dispatch_read(
    path: &str,
    requested_mode: &str,
    task: Option<&str>,
    project_root: Option<&str>,
) -> PreDispatchResult {
    pre_dispatch_read_for_agent(path, requested_mode, task, project_root, None, false)
}

enum AgentBudgetDecision {
    Allowed,
    Warning(String),
    Exceeded(PreDispatchResult),
}

fn no_change_result() -> PreDispatchResult {
    PreDispatchResult {
        overridden_mode: None,
        reason: None,
        budget_blocked: false,
        budget_warning: None,
        triage_filter_level: 0,
    }
}

fn check_agent_budget(
    path: &str,
    requested_mode: &str,
    agent_id: Option<&str>,
) -> AgentBudgetDecision {
    let Some(aid) = agent_id else {
        return AgentBudgetDecision::Allowed;
    };

    let estimated_tokens = estimate_read_tokens(path, requested_mode);
    match lean_ctx::core::agent_budget::check_budget(aid, estimated_tokens) {
        lean_ctx::core::agent_budget::BudgetCheckResult::Exceeded { limit, consumed } => {
            let mut result = no_change_result();
            result.reason = Some("agent-budget-exceeded");
            result.budget_blocked = true;
            result.budget_warning = Some(format!(
                "Agent budget exceeded: {consumed}/{limit} tokens consumed. Reset via ctx_session or set a higher limit."
            ));
            AgentBudgetDecision::Exceeded(result)
        }
        lean_ctx::core::agent_budget::BudgetCheckResult::Warning {
            remaining,
            percent_used,
        } => AgentBudgetDecision::Warning(format!(
            "[BUDGET WARNING] Agent '{aid}' at {:.0}% budget ({remaining} tokens remaining)",
            percent_used * 100.0
        )),
        lean_ctx::core::agent_budget::BudgetCheckResult::Allowed { .. } => {
            AgentBudgetDecision::Allowed
        }
    }
}

/// Pre-dispatch gate for an explicit caller-pinned mode.
///
/// Only the hard per-agent budget gate applies; advisory mode rewriting is
/// intentionally bypassed so the requested concrete mode remains intact.
pub fn pre_dispatch_pinned_read_for_agent(
    path: &str,
    requested_mode: &str,
    agent_id: Option<&str>,
) -> PreDispatchResult {
    let no_change = no_change_result();
    match check_agent_budget(path, requested_mode, agent_id) {
        AgentBudgetDecision::Exceeded(result) => result,
        AgentBudgetDecision::Warning(warning) => PreDispatchResult {
            budget_warning: Some(warning),
            ..no_change
        },
        AgentBudgetDecision::Allowed => no_change,
    }
}

/// `fresh` is the caller's explicit escape hatch (#1588). Bounce-prevention and
/// intent-target exist to stop an agent from *drifting* into a compressed view
/// it will immediately re-read in full; neither should be able to pin a file to
/// `full` forever. A caller who passes `fresh=true` has said, in the request
/// itself, that they want this view recomputed — honour it, or those modes are
/// simply unreachable for the rest of the session.
pub fn pre_dispatch_read_for_agent(
    path: &str,
    requested_mode: &str,
    task: Option<&str>,
    project_root: Option<&str>,
    agent_id: Option<&str>,
    fresh: bool,
) -> PreDispatchResult {
    let no_change = no_change_result();

    match check_agent_budget(path, requested_mode, agent_id) {
        AgentBudgetDecision::Exceeded(result) => return result,
        AgentBudgetDecision::Warning(warning) => {
            let mut result = no_change.clone();
            result.budget_warning = Some(warning);
            if is_precise_pinned_mode(requested_mode) {
                return result;
            }
            let rest = pre_dispatch_inner(path, requested_mode, task, project_root, fresh);
            return PreDispatchResult {
                budget_warning: result.budget_warning,
                ..rest
            };
        }
        AgentBudgetDecision::Allowed => {}
    }

    pre_dispatch_inner(path, requested_mode, task, project_root, fresh)
}

/// Does `norm` (a normalized path) actually name the intent target `target`?
///
/// The old rule was `norm.ends_with(t) || norm.contains(t)`, which matched on
/// any substring: a task mentioning `src` or `read` pinned every path
/// containing those letters to `full` (#1588), including matches that land in
/// the middle of a longer component (`print` inside `printer.rs`). A target now
/// has to line up with a whole path component or the file stem, and be long
/// enough to be a name rather than noise.
fn path_matches_target(norm: &str, target: &str) -> bool {
    const MIN_TARGET_LEN: usize = 4;
    let target = target.trim().trim_matches('/');
    if target.len() < MIN_TARGET_LEN {
        return false;
    }
    // A target that is itself a path suffix (`src/server/context_gate.rs`).
    if norm == target || norm.ends_with(&format!("/{target}")) {
        return true;
    }
    // Otherwise: a whole path component, with or without its extension.
    norm.split('/').any(|component| {
        component == target
            || component
                .rsplit_once('.')
                .is_some_and(|(stem, _)| stem == target)
    })
}

/// Explicitly requested views that drop content: the lossy summaries plus
/// `reference` and `density:`. Lossless, pinned and `auto` requests are not.
fn is_explicit_compressing_mode(mode: &str) -> bool {
    use crate::tools::ctx_read::ReadMode;
    mode.parse::<ReadMode>().is_ok_and(|parsed| {
        parsed.is_lossy_summary() || matches!(parsed, ReadMode::Reference | ReadMode::Density(_))
    })
}

fn pre_dispatch_inner(
    path: &str,
    requested_mode: &str,
    task: Option<&str>,
    project_root: Option<&str>,
    fresh: bool,
) -> PreDispatchResult {
    let no_change = PreDispatchResult {
        overridden_mode: None,
        reason: None,
        budget_blocked: false,
        budget_warning: None,
        triage_filter_level: 0,
    };

    if is_precise_pinned_mode(requested_mode) {
        return no_change;
    }

    if let Some(root) = project_root {
        let overlay = OverlayStore::load_project(&std::path::PathBuf::from(root));
        if let Some(result) = check_overlay_mode_override(path, requested_mode, &overlay) {
            return result;
        }
    }

    // Explicit mode=full must not be rewritten by heuristics.
    // Only overlays (user-explicit) above can override it.
    if requested_mode == "full" {
        return no_change;
    }

    // `fresh` is the documented way out (#1588): the caller re-requested this
    // view deliberately, so the "you keep bouncing back to full" heuristic has
    // nothing left to prevent.
    if !fresh
        && let Ok(bt) = lean_ctx::core::bounce_tracker::global().lock()
        && bt.should_force_full(path)
    {
        return PreDispatchResult {
            overridden_mode: Some("full".to_string()),
            reason: Some("bounce-prevention"),
            budget_blocked: false,
            budget_warning: None,
            triage_filter_level: 0,
        };
    }

    // #496 learned per-path memory: a file the agent keeps re-reading in full
    // after compressed views is delivered in full for explicit compressing
    // modes too (`auto` consults the same memory in its resolver). The caller
    // sees the stable `[mode overridden: … reason=learned-full …]` notice.
    if !fresh
        && is_explicit_compressing_mode(requested_mode)
        && lean_ctx::core::path_mode_memory::should_force_full(path)
    {
        return PreDispatchResult {
            overridden_mode: Some("full".to_string()),
            reason: Some("learned-full"),
            budget_blocked: false,
            budget_warning: None,
            triage_filter_level: 0,
        };
    }

    if let Some(task_str) = task
        && !fresh
    {
        let intent = lean_ctx::core::intent_engine::StructuredIntent::from_query(task_str);
        let norm = lean_ctx::core::pathutil::normalize_tool_path(path);
        let is_target = intent.targets.iter().any(|t| path_matches_target(&norm, t));
        if is_target {
            return PreDispatchResult {
                overridden_mode: Some("full".to_string()),
                reason: Some("intent-target"),
                budget_blocked: false,
                budget_warning: None,
                triage_filter_level: 0,
            };
        }
    }

    if let Some(root) = project_root
        && let Some(open) = try_load_graph(root)
    {
        let gp = &open.provider;
        let related = gp.related(path, 1);
        if let Some(task_str) = task {
            let intent = lean_ctx::core::intent_engine::StructuredIntent::from_query(task_str);
            for target in &intent.targets {
                let target_related = gp.related(target, 1);
                let norm = lean_ctx::core::pathutil::normalize_tool_path(path);
                if target_related
                    .iter()
                    .any(|r| r.contains(&norm) || norm.contains(r))
                {
                    return PreDispatchResult {
                        overridden_mode: Some("map".to_string()),
                        reason: Some("graph-direct-import"),
                        budget_blocked: false,
                        budget_warning: None,
                        triage_filter_level: 0,
                    };
                }
            }
        }
        if !related.is_empty() && requested_mode == "auto" {
            let reverse_deps = gp.dependents(path);
            if reverse_deps.len() > 3 {
                return PreDispatchResult {
                    overridden_mode: Some("map".to_string()),
                    reason: Some("graph-hub-file"),
                    budget_blocked: false,
                    budget_warning: None,
                    triage_filter_level: 0,
                };
            }
        }
    }

    if let Some(root) = project_root
        && let Some(knowledge) = lean_ctx::core::knowledge::ProjectKnowledge::load(root)
    {
        let norm = lean_ctx::core::pathutil::normalize_tool_path(path);
        let mentions = knowledge
            .facts
            .iter()
            .filter(|f| f.value.contains(&norm) || f.key.contains(&norm))
            .count();
        if mentions >= 3 {
            return PreDispatchResult {
                overridden_mode: Some("map".to_string()),
                reason: Some("knowledge-high-relevance"),
                budget_blocked: false,
                budget_warning: None,
                triage_filter_level: 0,
            };
        }
    }

    no_change
}

fn estimate_read_tokens(path: &str, mode: &str) -> usize {
    let file_size = std::fs::metadata(path).map_or(4000, |m| m.len() as usize);
    let char_estimate = file_size;
    let full_tokens = char_estimate / 4;
    match mode {
        "signatures" => full_tokens / 5,
        "map" => full_tokens / 3,
        "aggressive" | "entropy" => full_tokens / 4,
        "diff" => full_tokens / 10,
        _ if mode.starts_with("lines:") => {
            if let Some(range) = mode.strip_prefix("lines:") {
                // #971: comma is multi-select. Splitting the whole payload on
                // '-' yields 3 parts for `620-622,1214-1218` and fell through to
                // the whole-file tenth; sum the selected spans instead.
                let mut selected = 0usize;
                for part in range.split(',') {
                    let part = part.trim();
                    selected += if let Some((s, e)) = part.split_once('-') {
                        let start = s.trim().parse::<usize>().unwrap_or(1);
                        let end = e.trim().parse::<usize>().unwrap_or(start + 100);
                        end.saturating_sub(start) + 1
                    } else {
                        1
                    };
                }
                if selected == 0 {
                    full_tokens / 10
                } else {
                    selected * 10
                }
            } else {
                full_tokens / 10
            }
        }
        _ => full_tokens,
    }
}

fn check_overlay_mode_override(
    path: &str,
    requested_mode: &str,
    overlay: &OverlayStore,
) -> Option<PreDispatchResult> {
    let item_id = ContextItemId::from_file(path);
    let overlays = overlay.for_item(&item_id);

    for ov in overlays.iter().rev() {
        match &ov.operation {
            OverlayOp::SetView(view) => {
                let mode_str = view.as_str();
                if mode_str != requested_mode {
                    return Some(PreDispatchResult {
                        overridden_mode: Some(mode_str.to_string()),
                        reason: Some("overlay-set-view"),
                        budget_blocked: false,
                        budget_warning: None,
                        triage_filter_level: 0,
                    });
                }
            }
            OverlayOp::Pin { .. } if requested_mode != "full" => {
                return Some(PreDispatchResult {
                    overridden_mode: Some("full".to_string()),
                    reason: Some("pinned"),
                    budget_blocked: false,
                    budget_warning: None,
                    triage_filter_level: 0,
                });
            }
            OverlayOp::Exclude { .. } if requested_mode != "signatures" => {
                return Some(PreDispatchResult {
                    overridden_mode: Some("signatures".to_string()),
                    reason: Some("excluded"),
                    budget_blocked: false,
                    budget_warning: None,
                    triage_filter_level: 0,
                });
            }
            _ => {}
        }
    }
    None
}

pub fn post_dispatch_record(
    path: &str,
    mode: &str,
    original_tokens: usize,
    sent_tokens: usize,
    ledger: &mut ContextLedger,
    overlay: &OverlayStore,
) -> PostDispatchResult {
    post_dispatch_record_with_task(
        path,
        mode,
        original_tokens,
        sent_tokens,
        ledger,
        overlay,
        None,
        None,
    )
}

pub fn post_dispatch_record_with_task(
    path: &str,
    mode: &str,
    original_tokens: usize,
    sent_tokens: usize,
    ledger: &mut ContextLedger,
    overlay: &OverlayStore,
    task: Option<&str>,
    project_root: Option<&str>,
) -> PostDispatchResult {
    let prev_count = ledger.entries.len();
    let prev_pressure = ledger.pressure().recommendation;

    ledger.record_with_task(path, mode, original_tokens, sent_tokens, task);

    let item_id = ContextItemId::from_file(path);
    let state = overlay.apply_to_state(&item_id, ContextState::Included);

    if state == ContextState::Excluded {
        return PostDispatchResult {
            eviction_hint: Some(format!("File '{path}' is excluded by overlay.")),
            elicitation_hint: None,
            resource_changed: true,
        };
    }

    let elicitation =
        super::elicitation::check_elicitation_needed(ledger, Some(path), Some(sent_tokens))
            .map(|s| s.format_fallback_hint());

    let pressure = ledger.pressure();

    // #6 Global-Workspace ignition: salience outliers are broadcast (pinned) into
    // the working set BEFORE reinjection, so an ignited item keeps its view while
    // the rest are downgraded under pressure. Deterministic z-score threshold.
    let ignited = ledger.ignite_high_salience();

    apply_reinjection_plan(ledger, &pressure.recommendation);

    let new_entry = ledger.entries.len() != prev_count;
    let pressure_shifted = pressure.recommendation != prev_pressure;
    let resource_changed = new_entry || pressure_shifted || !ignited.is_empty();

    if pressure.utilization > 0.9 {
        let candidates = ledger.eviction_candidates_by_phi(3);
        if !candidates.is_empty() {
            // #715: emit targets the evict resolver can actually find —
            // root-relative paths (or the full path), never display-shortened
            // forms that used to produce "Evicted 0/N".
            let names: Vec<_> = candidates
                .iter()
                .take(3)
                .map(|p| eviction_target_display(p, project_root))
                .collect();
            return PostDispatchResult {
                eviction_hint: Some(format!(
                    "Context pressure {:.0}%. Evict: ctx_control(action=\"evict\", targets=\"{}\")",
                    pressure.utilization * 100.0,
                    names.join(", ")
                )),
                elicitation_hint: elicitation,
                resource_changed,
            };
        }
    }

    PostDispatchResult {
        eviction_hint: None,
        elicitation_hint: elicitation,
        resource_changed,
    }
}

/// #715: a resolvable evict target for hint output — project-root-relative
/// when the candidate lives under the root, otherwise the full canonical
/// path. Both forms round-trip through `ContextLedger::resolve_entry`.
fn eviction_target_display(path: &str, project_root: Option<&str>) -> String {
    if let Some(root) = project_root.filter(|r| !r.is_empty()) {
        let root_prefix = format!("{}/", root.trim_end_matches(['/', '\\']).replace('\\', "/"));
        if let Some(rel) = path.strip_prefix(&root_prefix)
            && !rel.is_empty()
        {
            return rel.to_string();
        }
    }
    path.to_string()
}

fn apply_reinjection_plan(ledger: &mut ContextLedger, action: &PressureAction) {
    if *action != PressureAction::ForceCompression && *action != PressureAction::EvictLeastRelevant
    {
        return;
    }
    for entry in &mut ledger.entries {
        // #6: ignited / user-pinned items stay broadcast — never downgraded.
        if entry.state == Some(ContextState::Pinned) {
            continue;
        }
        if entry.mode == "full" {
            entry.mode = "map".to_string();
        }
    }
}

/// Graph for the per-read mode decision. Only an already populated property
/// graph: this runs before every `ctx_read`, and the JSON-index fallback of
/// `open_best_effort` re-parsed the whole index on each read of a cold
/// session (focus analysis 2026-10-15).
fn try_load_graph(project_root: &str) -> Option<lean_ctx::core::graph_provider::OpenGraphProvider> {
    lean_ctx::core::graph_provider::open_ready(project_root)
}

/// Determines the output-filtering aggressiveness from a task profile.
///
/// Fail-safe by construction (community reports on 3.9.19, "triage always
/// level 2"): the original ladder inverted fail-safety — the empty-query
/// fallback profile landed on the MOST aggressive level, and per-tool-call
/// profiles almost always report a low context need (a single tool call
/// classifies as SingleFile, base 250). A rules-derived profile can now
/// select at most level 1; uncertainty degrades toward passthrough, never
/// toward harder filtering.
pub fn triage_filter_level(profile: &lean_ctx::core::triage::profile::TaskProfileLocal) -> u8 {
    use lean_ctx::core::triage::confidence::ACTIONABLE_FLOOR_MILLI;
    // An unset context need means "unknown", never "needs no context".
    if profile.confidence_milli < ACTIONABLE_FLOOR_MILLI || profile.context_need_milli == 0 {
        return 0;
    }
    // Level 2 removes declarations and leaves output that parses but no longer
    // means what it says (#1484). Nothing in a rules-derived profile is precise
    // enough to justify that: `confidence_milli` measures how sure the intent
    // classification is, not how much information loss the output tolerates.
    // The level stays reachable through `apply_triage_filter` for callers that
    // ask for it — where it is additionally markdown-only, so source code and
    // logs never lose non-comment lines — and should return here again only
    // once a model backend can supply that prediction.
    u8::from(profile.context_need_milli < 600)
}

/// Filters output according to the task profile and selected triage level.
pub fn apply_triage_filter(
    output: &str,
    profile: &lean_ctx::core::triage::profile::TaskProfileLocal,
    level: u8,
) -> (String, usize) {
    if level == 0 || output.chars().count() < 500 {
        return (output.to_string(), 0);
    }

    // Never filter structured JSON output — the line-based heuristics destroy
    // key-value pairs while keeping only braces, producing invalid JSON.
    let trimmed = output.trim_start();
    if (trimmed.starts_with('{') || trimmed.starts_with('['))
        && serde_json::from_str::<serde_json::Value>(trimmed).is_ok()
    {
        return (output.to_string(), 0);
    }

    let lines: Vec<&str> = output.lines().collect();

    // Also bypass when JSON is embedded after a short header (e.g. "ctx_handoff show\n path: …\n{…}")
    if let Some(json_start) = lines.iter().position(|l| {
        let t = l.trim_start();
        t.starts_with('{') || t.starts_with('[')
    }) {
        let json_portion = lines[json_start..].join("\n");
        if serde_json::from_str::<serde_json::Value>(&json_portion).is_ok() {
            return (output.to_string(), 0);
        }
    }
    // #1570 P4: an explicit <protect> span is a user contract — the whole
    // output bypasses lossy line filtering (gated on [protection].tags).
    // Lossless compression (archive digest + ctx_expand) is unaffected:
    // protection means "never lossy", not "never compressed".
    if output.contains("<protect>") && lean_ctx::core::config::Config::load().protection.tags {
        return (output.to_string(), 0);
    }
    // Build a set of kept lines for O(1) lookup.
    let keep: std::collections::HashSet<usize> = match level {
        1 => lines
            .iter()
            .enumerate()
            .filter(|(_, line)| !is_boilerplate_line(line.trim_start()))
            .map(|(i, _)| i)
            .collect(),
        2 => {
            let keywords = extract_task_keywords(&profile.task_class, &profile.intent);
            if lean_ctx::core::triage::markdown::looks_like_markdown(&lines) {
                lean_ctx::core::triage::markdown::keep_indices(&lines, &keywords)
            } else {
                // Community report (3.9.19, "abridged into something that
                // still looks right"): the structural+keyword keep-set is
                // provably unsafe for source code — plain `let` bindings
                // vanished from a Rust fn while the survivors still parsed
                // (21 of 36 lines dropped, output looked complete, bindings
                // became undeclared). Line-level lossy triage is therefore
                // markdown-only; every other content shape gets at most the
                // level-1 boilerplate strip.
                lines
                    .iter()
                    .enumerate()
                    .filter(|(_, line)| !is_boilerplate_line(line.trim_start()))
                    .map(|(i, _)| i)
                    .collect()
            }
        }
        _ => return (output.to_string(), 0),
    };

    // #1484: assemble output with inline elision markers so omissions are visible.
    let removed = lines.len().saturating_sub(keep.len());
    if removed == 0 {
        return (output.to_string(), 0);
    }
    // #1493: total deletion (100% of lines removed) is never a useful
    // compression outcome — the marker plus retry costs more than passing
    // through the original content. Return unfiltered when nothing survives.
    if keep.is_empty() {
        return (output.to_string(), 0);
    }
    // Heading-only markdown is the same failure mode: ATX titles survive the
    // markdown keep-set so `keep` is non-empty, but every body paragraph is
    // gone. Pass through instead of returning a TOC.
    if lean_ctx::core::triage::markdown::is_heading_only_collapse(&lines, &keep) {
        return (output.to_string(), 0);
    }
    let mut result = String::with_capacity(output.len());
    let mut consecutive_omitted: usize = 0;
    for (i, line) in lines.iter().enumerate() {
        if keep.contains(&i) {
            if consecutive_omitted > 0 {
                result.push_str(&format!(
                    "  [...{consecutive_omitted} lines omitted by triage...]\n"
                ));
                consecutive_omitted = 0;
            }
            result.push_str(line);
            result.push('\n');
        } else {
            consecutive_omitted += 1;
        }
    }
    if consecutive_omitted > 0 {
        result.push_str(&format!(
            "  [...{consecutive_omitted} lines omitted by triage...]\n"
        ));
    }
    result.push_str(&format!(
        "[lean-ctx: {removed} lines filtered by triage (level {level}) — rerun with raw=true for the unfiltered output]"
    ));
    (result, removed)
}

fn extract_task_keywords(task_class: &str, intent: &str) -> Vec<String> {
    let mut keywords = task_class
        .split(|character: char| !character.is_alphanumeric())
        .chain(intent.split(|character: char| !character.is_alphanumeric()))
        .filter(|keyword| !keyword.is_empty())
        .map(str::to_lowercase)
        .collect::<Vec<_>>();
    keywords.sort_unstable();
    keywords.dedup();
    keywords
}

fn is_boilerplate_line(line: &str) -> bool {
    line.starts_with("//")
        && !line.contains("TODO")
        && !line.contains("FIXME")
        && !line.contains("SAFETY")
}

#[cfg(test)]
#[path = "context_gate_tests.rs"]
mod tests;