lean-ctx 3.12.7

LeanCTX | OSS: the open-source runtime for the Context Gateway for AI agents. 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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
// SPDX-License-Identifier: Apache-2.0
//! Install/environment wiring: shell hooks, MCP registration, editor
//! surfaces, session state, container env, CWD sanity, LSP servers.

#[allow(clippy::wildcard_imports)]
use crate::doctor::common::*;
use crate::doctor::{BOLD, DIM, GREEN, Outcome, RED, RST, WHITE, YELLOW};
use std::net::TcpListener;

/// Cognition v2 activation: how many science-backed subsystems have actually
/// fired on this install. Proves the stack is wired (not dead code) without
/// needing external instrumentation — `lean-ctx introspect cognition` drills in.
pub(crate) fn cognition_activity_outcome() -> Outcome {
    let snap = lean_ctx::core::introspect::snapshot();
    let total = snap.len();
    let active = snap.iter().filter(|(_, a)| a.count > 0).count();
    // Before any tool calls have run nothing has fired yet — neutral, not a
    // failure. Always pass; the value is the visibility, not a gate.
    let line = if active == 0 {
        format!(
            "{BOLD}Cognition{RST}  {DIM}no activity recorded yet{RST}  {DIM}(inspect: lean-ctx introspect cognition){RST}"
        )
    } else {
        format!(
            "{BOLD}Cognition{RST}  {GREEN}{active}/{total} subsystems active{RST}  {DIM}(details: lean-ctx introspect cognition){RST}"
        )
    };
    Outcome { ok: true, line }
}
/// Reports the format-aware passthrough (#342): output already in a compact,
/// token-oriented format (TOON by default) is preserved verbatim instead of
/// recompressed, so an agent's proof-of-output-shape survives intact.
pub(crate) fn compact_format_passthrough_outcome() -> Outcome {
    let cfg = lean_ctx::core::config::Config::load();
    if cfg.preserve_compact_formats.is_empty() {
        return Outcome {
            ok: true,
            line: format!(
                "{BOLD}Compact-format passthrough{RST}  {YELLOW}off{RST}  {DIM}(set preserve_compact_formats to keep e.g. TOON verbatim){RST}"
            ),
        };
    }
    Outcome {
        ok: true,
        line: format!(
            "{BOLD}Compact-format passthrough{RST}  {GREEN}{}{RST}  {DIM}(preserved verbatim, not recompressed){RST}",
            cfg.preserve_compact_formats.join(", ")
        ),
    }
}

/// Surfaces the post-dispatch output-triage filter state (#1534): the 3.9.19
/// filter dropped MCP tool output while `doctor` reported every check green.
/// From 3.9.20 the filter is opt-in (`decision_loop.max_filter_level`,
/// default 0) and this line keeps its state on the board either way.
pub(crate) fn output_triage_outcome() -> Outcome {
    output_triage_outcome_for(
        lean_ctx::core::config::Config::load()
            .decision_loop
            .max_filter_level,
    )
}

pub(super) fn output_triage_outcome_for(max_filter_level: u8) -> Outcome {
    if max_filter_level == 0 {
        return Outcome {
            ok: true,
            line: format!(
                "{BOLD}Output triage{RST}  {GREEN}off{RST}  {DIM}(decision_loop.max_filter_level = 0 — tool output is never dropped){RST}"
            ),
        };
    }
    Outcome {
        ok: true,
        line: format!(
            "{BOLD}Output triage{RST}  {YELLOW}active (max level {max_filter_level}){RST}  {DIM}(lossy; disable: lean-ctx config set decision_loop.max_filter_level 0){RST}"
        ),
    }
}

pub(crate) fn shell_aliases_outcome() -> Outcome {
    let Some(home) = dirs::home_dir() else {
        return Outcome {
            ok: false,
            line: format!("{BOLD}Shell aliases{RST}  {RED}could not resolve home directory{RST}"),
        };
    };

    let mut parts = Vec::new();
    let mut needs_update = Vec::new();

    let zsh = home.join(".zshrc");
    if rc_contains_lean_ctx(&zsh) {
        parts.push(format!("{DIM}~/.zshrc{RST}"));
        if !rc_has_pipe_guard(&zsh) && is_active_shell("~/.zshrc") {
            needs_update.push("~/.zshrc");
        }
    }
    let bash = home.join(".bashrc");
    if rc_contains_lean_ctx(&bash) {
        parts.push(format!("{DIM}~/.bashrc{RST}"));
        if !rc_has_pipe_guard(&bash) && is_active_shell("~/.bashrc") {
            needs_update.push("~/.bashrc");
        }
    }

    let fish = home.join(".config").join("fish").join("config.fish");
    if rc_contains_lean_ctx(&fish) {
        parts.push(format!("{DIM}~/.config/fish/config.fish{RST}"));
        if !rc_has_pipe_guard(&fish) && is_active_shell("~/.config/fish/config.fish") {
            needs_update.push("~/.config/fish/config.fish");
        }
    }

    #[cfg(windows)]
    {
        let ps_profile = home
            .join("Documents")
            .join("PowerShell")
            .join("Microsoft.PowerShell_profile.ps1");
        let ps_profile_legacy = home
            .join("Documents")
            .join("WindowsPowerShell")
            .join("Microsoft.PowerShell_profile.ps1");
        if rc_contains_lean_ctx(&ps_profile) {
            parts.push(format!("{DIM}PowerShell profile{RST}"));
            if !rc_has_pipe_guard(&ps_profile) {
                needs_update.push("PowerShell profile");
            }
        } else if rc_contains_lean_ctx(&ps_profile_legacy) {
            parts.push(format!("{DIM}WindowsPowerShell profile{RST}"));
            if !rc_has_pipe_guard(&ps_profile_legacy) {
                needs_update.push("WindowsPowerShell profile");
            }
        }
    }

    if parts.is_empty() {
        let hint = if cfg!(windows) {
            "no \"lean-ctx\" in PowerShell profile, ~/.zshrc or ~/.bashrc"
        } else {
            "no \"lean-ctx\" in ~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish"
        };
        Outcome {
            ok: false,
            line: format!("{BOLD}Shell aliases{RST}  {RED}{hint}{RST}"),
        }
    } else if !needs_update.is_empty() {
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}Shell aliases{RST}  {YELLOW}outdated hook in {} — run {BOLD}lean-ctx init --global{RST}{YELLOW} to fix (pipe guard missing){RST}",
                needs_update.join(", ")
            ),
        }
    } else {
        Outcome {
            ok: true,
            line: format!(
                "{BOLD}Shell aliases{RST}  {GREEN}lean-ctx referenced in {}{RST}",
                parts.join(", ")
            ),
        }
    }
}
pub(crate) fn skip_agent_aliases_outcome() -> Outcome {
    let cfg = lean_ctx::core::config::Config::load();
    if !cfg.skip_agent_aliases {
        return Outcome {
            ok: true,
            line: format!("{BOLD}Agent aliases{RST}  {DIM}enabled (claude, codex, gemini){RST}"),
        };
    }
    let warn = if cfg.shell_activation == lean_ctx::core::config::ShellActivation::AgentsOnly {
        format!(
            "  {YELLOW}hint: shell_activation=agents-only — compression still active via _lc() hook{RST}"
        )
    } else {
        String::new()
    };
    Outcome {
        ok: true,
        line: format!(
            "{BOLD}Agent aliases{RST}  {GREEN}skipped (skip_agent_aliases = true){RST}{warn}"
        ),
    }
}

/// Claude Code can still search: a standard setup (a host pin `mcp`, `hybrid`
/// or `replace`, or the Grep/Glob deny it writes) needs the lean-ctx MCP
/// server. 3.12.1's automatic governor migration removed that entry while the
/// deny stayed, and sessions had no search tool at all. `None` where Claude
/// Code has no standard lean-ctx setup, runs the governor, or MCP registration
/// is turned off on purpose (`[setup] auto_update_mcp = false`, #281).
pub(crate) fn claude_tool_access_outcome(home: &std::path::Path) -> Option<Outcome> {
    use lean_ctx_hooks::hooks::agents::claude_governor;
    if claude_governor::is_installed(home) || !claude_governor::has_standard_setup(home) {
        return None;
    }
    let settings_path =
        lean_ctx::core::editor_registry::claude_state_dir(home).join("settings.json");
    let settings: serde_json::Value = std::fs::read_to_string(&settings_path)
        .ok()
        .and_then(|c| lean_ctx::core::jsonc::parse_jsonc(&c).ok())
        .unwrap_or_default();
    let denies: Vec<&str> = settings
        .pointer("/permissions/deny")
        .and_then(|d| d.as_array())
        .into_iter()
        .flatten()
        .filter_map(|v| v.as_str())
        .collect();
    let search_denied: Vec<&str> = denies
        .iter()
        .copied()
        .filter(|v| matches!(*v, "Grep" | "Glob"))
        .collect();
    // The user's own shell denies leave no fallback either; named for context.
    let shell_denied: Vec<&str> = denies
        .iter()
        .copied()
        .filter(|v| {
            ["cat", "grep", "rg", "find", "ls"]
                .iter()
                .any(|c| *v == format!("Bash({c}:*)"))
        })
        .collect();
    let cfg = lean_ctx::core::config::Config::load();
    let pinned = cfg
        .governor
        .pinned
        .get("claude")
        .filter(|m| lean_ctx_hooks::hooks::HookMode::from_str_loose(m).is_some())
        .cloned();
    if search_denied.is_empty() && pinned.is_none() {
        return None;
    }
    if lean_ctx::core::claude_host::has_lean_ctx_mcp_entry(home) {
        return Some(Outcome {
            ok: true,
            line: format!(
                "{BOLD}Claude Code tools{RST}  {GREEN}lean-ctx MCP server registered{RST}  {DIM}(the ctx_* tools of the setup's mode){RST}"
            ),
        });
    }
    if !cfg.setup.should_update_mcp() {
        return None;
    }
    let mode = pinned.as_deref().unwrap_or("hybrid");
    let fix = format!(
        "{DIM}(run: lean-ctx init --agent claude --mode {mode}, then restart Claude Code){RST}"
    );
    let line = if search_denied.is_empty() {
        format!(
            "{BOLD}Claude Code tools{RST}  {RED}mode {mode} is pinned, but ~/.claude.json has no lean-ctx MCP server: sessions have no ctx_* tools{RST}  {fix}"
        )
    } else {
        let mut all = search_denied.clone();
        all.extend(&shell_denied);
        format!(
            "{BOLD}Claude Code tools{RST}  {RED}settings deny {}, but ~/.claude.json has no lean-ctx MCP server: sessions have no search tool{RST}  {fix}",
            all.join(", ")
        )
    };
    Some(Outcome { ok: false, line })
}

#[cfg(test)]
mod claude_tool_access_tests {
    /// The 3.12.1 state (deny list kept, MCP entry gone, replace pinned)
    /// fails with the repair command; the entry back makes it pass. A global
    /// hook mode without a Claude Code setup, or a pin without a deny while MCP
    /// registration is off on purpose, is no finding.
    #[test]
    fn denied_tools_without_the_mcp_server_fail_with_the_fix() {
        let _iso = lean_ctx::core::data_dir::isolated_data_dir();
        lean_ctx::test_env::remove_var("CLAUDE_CONFIG_DIR");
        let home = tempfile::tempdir().unwrap();
        let h = home.path();
        lean_ctx::test_env::set_var("LEAN_CTX_HOOK_MODE", "hybrid");
        assert!(
            super::claude_tool_access_outcome(h).is_none(),
            "no Claude Code setup: a global hook mode alone is no finding"
        );
        lean_ctx::test_env::remove_var("LEAN_CTX_HOOK_MODE");

        std::fs::create_dir_all(h.join(".claude")).unwrap();
        let hooks = r#""hooks":{"PreToolUse":[{"matcher":"Bash","hooks":[{"type":"command","command":"lean-ctx hook rewrite"}]}]}"#;
        std::fs::write(
            h.join(".claude/settings.json"),
            format!(r#"{{{hooks},"permissions":{{"deny":["Bash(cat:*)","Grep","Glob"]}}}}"#),
        )
        .unwrap();
        std::fs::write(h.join(".claude.json"), r#"{"mcpServers":{}}"#).unwrap();
        lean_ctx::core::config::Config::update_global(|c| {
            c.governor.pinned.insert("claude".into(), "replace".into());
        })
        .unwrap();
        let bad = super::claude_tool_access_outcome(h).expect("checked");
        assert!(!bad.ok);
        assert!(
            bad.line.contains("deny Grep, Glob, Bash(cat:*)"),
            "{}",
            bad.line
        );
        assert!(bad.line.contains("no search tool"), "{}", bad.line);
        assert!(
            bad.line.contains("init --agent claude --mode replace"),
            "{}",
            bad.line
        );

        // Pinned without a deny: native search works, the ctx_* tools are
        // missing — unless MCP registration is off on purpose (#281).
        std::fs::write(h.join(".claude/settings.json"), format!("{{{hooks}}}")).unwrap();
        let pinned = super::claude_tool_access_outcome(h).expect("checked");
        assert!(
            !pinned.ok && pinned.line.contains("no ctx_* tools"),
            "{}",
            pinned.line
        );
        lean_ctx::core::config::Config::update_global(|c| c.setup.auto_update_mcp = false).unwrap();
        assert!(super::claude_tool_access_outcome(h).is_none());
        lean_ctx::core::config::Config::update_global(|c| c.setup.auto_update_mcp = true).unwrap();

        std::fs::write(
            h.join(".claude.json"),
            r#"{"mcpServers":{"lean-ctx":{"command":"lean-ctx"}}}"#,
        )
        .unwrap();
        assert!(super::claude_tool_access_outcome(h).expect("checked").ok);
    }
}

pub(crate) fn mcp_config_outcome() -> Outcome {
    let Some(home) = dirs::home_dir() else {
        return Outcome {
            ok: false,
            line: format!("{BOLD}MCP config{RST}  {RED}could not resolve home directory{RST}"),
        };
    };

    let locations = mcp_config_locations(&home);
    let location_names = lean_ctx_mcp_location_names(&home);
    let mut found: Vec<String> = Vec::new();
    let mut exists_no_ref: Vec<String> = Vec::new();

    // The Claude Code governor setup: no MCP server (governor), or one with
    // deferred tools (governor-mcp).
    let claude_governor = lean_ctx_hooks::hooks::agents::claude_governor::installed_arm(&home);
    for loc in &locations {
        if let Some(arm) = claude_governor
            && loc.name == "Claude Code"
        {
            let what = match arm {
                lean_ctx_hooks::hooks::agents::claude_governor::GovernorArm::NoMcp => {
                    "governor setup — hooks, no MCP server"
                }
                lean_ctx_hooks::hooks::agents::claude_governor::GovernorArm::McpDeferred => {
                    "governor-mcp setup — hooks, MCP server with deferred tools"
                }
            };
            found.push(format!("{} {DIM}({what}){RST}", loc.name));
            continue;
        }
        if std::fs::read_to_string(&loc.path).is_ok() {
            if location_names.contains(loc.name) {
                found.push(format!("{} {DIM}({}){RST}", loc.name, loc.display));
            } else {
                exists_no_ref.push(loc.name.to_string());
            }
        }
    }

    found.sort();
    found.dedup();
    exists_no_ref.sort();
    exists_no_ref.dedup();

    if !found.is_empty() {
        Outcome {
            ok: true,
            line: format!(
                "{BOLD}MCP config{RST}  {GREEN}lean-ctx found in: {}{RST}",
                found.join(", ")
            ),
        }
    } else if !exists_no_ref.is_empty() {
        let has_claude = exists_no_ref.iter().any(|n| n.starts_with("Claude Code"));
        let cause = if has_claude {
            format!(
                "{DIM}(Claude Code may overwrite ~/.claude.json on startup — lean-ctx entry missing from mcpServers){RST}"
            )
        } else {
            String::new()
        };
        let hint = if has_claude {
            format!("{DIM}(run: lean-ctx doctor --fix OR lean-ctx init --agent claude){RST}")
        } else {
            format!("{DIM}(run: lean-ctx doctor --fix OR lean-ctx setup){RST}")
        };
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}MCP config{RST}  {YELLOW}config exists for {} but mcpServers does not contain lean-ctx{RST}  {cause} {hint}",
                exists_no_ref.join(", "),
            ),
        }
    } else {
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}MCP config{RST}  {YELLOW}no MCP config found{RST}  {DIM}(run: lean-ctx setup){RST}"
            ),
        }
    }
}
/// WSL2 + VS Code (GH #669): VS Code's start-on-demand MCP lifecycle has a
/// known client-side race — the first tool call of a fresh conversation can
/// fire against cached tool metadata before the server's implementation is
/// bound, failing with `Cannot read properties of undefined (reading 'invoke')`
/// (microsoft/vscode#321150). Cold starts on WSL2 widen that window. Purely
/// informational (ok: true): the defect is upstream, a retry always succeeds.
pub(crate) fn wsl_vscode_mcp_outcome() -> Option<Outcome> {
    if !lean_ctx::core::io_health::is_wsl() {
        return None;
    }
    let home = dirs::home_dir()?;
    if !lean_ctx_mcp_location_names(&home).contains("VS Code") {
        return None;
    }
    Some(Outcome {
        ok: true,
        line: format!(
            "{BOLD}WSL2 + VS Code{RST}  {YELLOW}known first-call race in VS Code's MCP client{RST}  {DIM}(a fresh conversation's first ctx_* call may fail with \"reading 'invoke'\" — retry succeeds; upstream: microsoft/vscode#321150. Mitigation: run the MCP server once via \"MCP: List Servers\" → lean-ctx → Start){RST}"
        ),
    })
}

pub(crate) fn port_3333_outcome() -> Outcome {
    match TcpListener::bind("127.0.0.1:3333") {
        Ok(_listener) => Outcome {
            ok: true,
            line: format!("{BOLD}Cockpit port 3333{RST}  {GREEN}available on 127.0.0.1{RST}"),
        },
        // #644: a busy port is only a problem if it's *not* us. Reuse the dashboard's
        // own auth-aware /api/version probe (single source of truth) so our own
        // running dashboard reads as healthy rather than a false conflict.
        Err(_) if crate::dashboard::dashboard_responding("127.0.0.1", 3333) => Outcome {
            ok: true,
            line: format!(
                "{BOLD}Cockpit port 3333{RST}  {GREEN}already serving the LeanCTX Cockpit{RST}  {DIM}(http://localhost:3333){RST}"
            ),
        },
        Err(e) => Outcome {
            ok: false,
            line: format!("{BOLD}Cockpit port 3333{RST}  {RED}not available: {e}{RST}"),
        },
    }
}
pub(crate) fn pi_outcome() -> Option<Outcome> {
    let pi_result = std::process::Command::new("pi").arg("--version").output();

    match pi_result {
        Ok(output) if output.status.success() => {
            let version = String::from_utf8_lossy(&output.stdout).trim().to_string();
            let has_plugin = std::process::Command::new("pi")
                .args(["list"])
                .output()
                .is_ok_and(|o| {
                    o.status.success() && String::from_utf8_lossy(&o.stdout).contains("pi-lean-ctx")
                });

            let has_mcp = dirs::home_dir()
                .map(|h| h.join(".pi/agent/mcp.json"))
                .and_then(|p| std::fs::read_to_string(p).ok())
                .is_some_and(|c| c.contains("lean-ctx"));

            if has_plugin && has_mcp {
                Some(Outcome {
                    ok: true,
                    line: format!(
                        "{BOLD}Pi Coding Agent{RST}  {GREEN}{version}, pi-lean-ctx + MCP configured{RST}"
                    ),
                })
            } else if has_plugin {
                Some(Outcome {
                    ok: true,
                    line: format!(
                        "{BOLD}Pi Coding Agent{RST}  {GREEN}{version}, pi-lean-ctx installed{RST}  {DIM}(MCP not configured — embedded bridge active){RST}"
                    ),
                })
            } else {
                Some(Outcome {
                    ok: false,
                    line: format!(
                        "{BOLD}Pi Coding Agent{RST}  {YELLOW}{version}, but pi-lean-ctx not installed{RST}  {DIM}(run: pi install npm:pi-lean-ctx){RST}"
                    ),
                })
            }
        }
        _ => None,
    }
}
pub(crate) fn plan_mode_outcomes() -> Vec<Outcome> {
    let status = lean_ctx::core::editor_registry::plan_mode::check_plan_mode_status();
    let mut results = Vec::new();

    if let Some(configured) = status.vscode_configured {
        if configured {
            results.push(Outcome {
                ok: true,
                line: format!(
                    "{BOLD}Plan mode{RST}  VS Code  {GREEN}planAgent tools configured{RST}"
                ),
            });
        } else {
            results.push(Outcome {
                ok: false,
                line: format!(
                    "{BOLD}Plan mode{RST}  VS Code  {YELLOW}not configured{RST}  {DIM}(run: lean-ctx setup){RST}"
                ),
            });
        }
    }

    // A governor setup writes no plan-mode allow-list (setup skips it for
    // both arms): the no-MCP arm has no lean-ctx tools to allow, and
    // governor-mcp's deferred tools are auto-approved in its MCP entry.
    let claude_governor_arm = lean_ctx::core::home::resolve_home_dir()
        .and_then(|h| lean_ctx_hooks::hooks::agents::claude_governor::installed_arm(&h));
    if let Some(arm) = claude_governor_arm
        && status.claude_configured.is_some()
    {
        let why = match arm {
            lean_ctx_hooks::hooks::agents::claude_governor::GovernorArm::NoMcp => {
                "no MCP tools to allow"
            }
            lean_ctx_hooks::hooks::agents::claude_governor::GovernorArm::McpDeferred => {
                "deferred tools auto-approved in the MCP entry"
            }
        };
        results.push(Outcome {
            ok: true,
            line: format!(
                "{BOLD}Plan mode{RST}  Claude Code  {GREEN}{} setup{RST}  {DIM}({why}){RST}",
                arm.label()
            ),
        });
    } else if let Some(configured) = status.claude_configured {
        if configured {
            results.push(Outcome {
                ok: true,
                line: format!("{BOLD}Plan mode{RST}  Claude Code  {GREEN}permissions present{RST}"),
            });
        } else {
            results.push(Outcome {
                ok: false,
                line: format!(
                    "{BOLD}Plan mode{RST}  Claude Code  {YELLOW}not configured{RST}  {DIM}(run: lean-ctx setup){RST}"
                ),
            });
        }
    }

    results
}
pub(crate) fn session_state_outcome() -> Outcome {
    use lean_ctx::core::session::SessionState;

    match SessionState::load_latest() {
        Some(session) => {
            let root = session.project_root.as_deref().unwrap_or("(not set)");
            let cwd = session.shell_cwd.as_deref().unwrap_or("(not tracked)");
            Outcome {
                ok: true,
                line: format!(
                    "{BOLD}Session state{RST}  {GREEN}active{RST}  {DIM}root: {root}, cwd: {cwd}, v{}{RST}",
                    session.version
                ),
            }
        }
        // No session for THIS cwd — but sessions for other workspaces may be
        // live (GH #694: doctor run from a shell whose cwd is not one of the
        // open project roots claimed "no active session" and looked broken).
        // Surface the recent sessions with their roots instead of denying
        // their existence.
        None => match &recent_sessions_line(lean_ctx_time::UtcDateTime::now()) {
            Some(line) => Outcome {
                ok: true,
                line: format!(
                    "{BOLD}Session state{RST}  {YELLOW}none for this directory{RST}  {DIM}{line}{RST}"
                ),
            },
            None => Outcome {
                ok: true,
                line: format!(
                    "{BOLD}Session state{RST}  {WHITE}no active session{RST}  {DIM}(will be created on first tool call){RST}"
                ),
            },
        },
    }
}

/// Renders "recent: root1 (2h ago), root2 (5m ago)" for the newest sessions
/// across ALL workspaces (multi-window setups, GH #694). `None` when no
/// session exists at all.
fn recent_sessions_line(now: lean_ctx_time::UtcDateTime) -> Option<String> {
    format_recent_sessions(lean_ctx::core::session::SessionState::list_sessions(), now)
}

pub(crate) fn format_recent_sessions(
    sessions: Vec<lean_ctx::core::session::SessionSummary>,
    now: lean_ctx_time::UtcDateTime,
) -> Option<String> {
    let mut seen_roots = std::collections::HashSet::new();
    let mut parts = Vec::new();
    for s in sessions {
        let Some(root) = s.project_root.filter(|r| !r.is_empty()) else {
            continue;
        };
        if !seen_roots.insert(root.clone()) {
            continue;
        }
        let display_root = std::path::Path::new(&root)
            .file_name()
            .map_or_else(|| root.clone(), |n| n.to_string_lossy().into_owned());
        parts.push(format!(
            "{display_root} ({})",
            humanize_age(now.signed_duration_since(s.updated_at))
        ));
        if parts.len() == 3 {
            break;
        }
    }
    if parts.is_empty() {
        None
    } else {
        Some(format!("recent: {}", parts.join(", ")))
    }
}

pub(crate) fn humanize_age(age: lean_ctx_time::Duration) -> String {
    let mins = age.num_minutes();
    if mins < 1 {
        "just now".to_string()
    } else if mins < 60 {
        format!("{mins}m ago")
    } else if mins < 48 * 60 {
        format!("{}h ago", mins / 60)
    } else {
        format!("{}d ago", mins / (24 * 60))
    }
}
pub(crate) fn docker_env_outcomes() -> Vec<Outcome> {
    if !lean_ctx::shell::is_container() {
        return vec![];
    }
    let env_sh = lean_ctx::core::paths::config_dir().map_or_else(
        |_| "/root/.lean-ctx/env.sh".to_string(),
        |d| d.join("env.sh").to_string_lossy().to_string(),
    );

    let mut outcomes = vec![];

    let shell_name = std::env::var("SHELL").unwrap_or_default();
    let is_bash = shell_name.contains("bash") || shell_name.is_empty();

    if is_bash {
        let has_bash_env = std::env::var("BASH_ENV").is_ok();
        outcomes.push(if has_bash_env {
            Outcome {
                ok: true,
                line: format!(
                    "{BOLD}BASH_ENV{RST}  {GREEN}set{RST}  {DIM}({}){RST}",
                    std::env::var("BASH_ENV").unwrap_or_default()
                ),
            }
        } else {
            Outcome {
                ok: false,
                line: format!(
                    "{BOLD}BASH_ENV{RST}  {RED}not set{RST}  {YELLOW}(add to Dockerfile: ENV BASH_ENV=\"{env_sh}\"){RST}"
                ),
            }
        });
    }

    let has_claude_env = std::env::var("CLAUDE_ENV_FILE").is_ok();
    outcomes.push(if has_claude_env {
        Outcome {
            ok: true,
            line: format!(
                "{BOLD}CLAUDE_ENV_FILE{RST}  {GREEN}set{RST}  {DIM}({}){RST}",
                std::env::var("CLAUDE_ENV_FILE").unwrap_or_default()
            ),
        }
    } else {
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}CLAUDE_ENV_FILE{RST}  {RED}not set{RST}  {YELLOW}(for Claude Code: ENV CLAUDE_ENV_FILE=\"{env_sh}\"){RST}"
            ),
        }
    });

    outcomes
}

/// Cloud filter packs, shown only on an enrolled instance. A refused bundle
/// (a pack not as signed, malformed, over the rule limit) is a warning,
/// never a failure: local work goes on under the last verified bundle.
/// Local files only — no network.
pub(crate) fn cloud_policy_outcome() -> Option<Outcome> {
    use lean_ctx_cloud::{policy, state::CloudStore};
    let store = CloudStore::open().ok()?;
    store.instance().ok().flatten()?;
    let state = policy::load_state(store.data_dir());
    let line = match (&state.last_refusal, state.filter_packs.len()) {
        (Some(refusal), _) => format!(
            "{BOLD}LeanCTX Cloud policy{RST}     {YELLOW}bundle refused: {refusal}{RST}  {DIM}(the last verified bundle stays; see lean-ctx cloud status){RST}"
        ),
        (None, 0) => format!(
            "{BOLD}LeanCTX Cloud policy{RST}     {DIM}no filter packs from LeanCTX Cloud{RST}"
        ),
        (None, packs) => {
            format!(
                "{BOLD}LeanCTX Cloud policy{RST}     {GREEN}{packs} filter pack(s) verified{RST}"
            )
        }
    };
    Some(Outcome { ok: true, line })
}

/// The optional lean-ctx Claude Code mod: shown when Claude Code is present,
/// never a failure when absent (it is opt-in). File-only — no `claude` spawn.
pub(crate) fn claude_mod_outcome() -> Option<Outcome> {
    use lean_ctx_hooks::hooks::agents::claude_mod;
    let claude_dir = std::env::var_os("CLAUDE_CONFIG_DIR")
        .map(std::path::PathBuf::from)
        .or_else(|| dirs::home_dir().map(|h| h.join(".claude")))?;
    if !claude_dir.is_dir() {
        return None;
    }
    let versions = claude_mod::cached_versions(&claude_dir);
    let want = claude_mod::mod_version();
    let line = if versions.contains(&claude_mod::cache_dir_name(&want)) {
        let hint = if claude_mod_front_loads_under_governor(&claude_dir) {
            format!(
                "  {YELLOW}enabled under governor-mcp: front-loads lean-ctx tool schemas{RST}  {DIM}(run: claude plugin disable {} --scope user){RST}",
                claude_mod::PLUGIN_ID
            )
        } else {
            String::new()
        };
        format!("{BOLD}Claude Code mod{RST}  {GREEN}installed ({want}){RST}{hint}")
    } else if let Some(old) = versions.last() {
        format!(
            "{BOLD}Claude Code mod{RST}  {YELLOW}{old} — this instance ships {want}{RST}  {DIM}(run: lean-ctx claude-mod install){RST}"
        )
    } else {
        format!(
            "{BOLD}Claude Code mod{RST}  {DIM}not installed — optional: cockpit, /leanctx, wake-not-poll, Code Mode (run: lean-ctx claude-mod install){RST}"
        )
    };
    Some(Outcome { ok: true, line })
}

/// governor-mcp keeps lean-ctx tool schemas deferred; the mod, when enabled in
/// the user's Claude settings (user scope, so the hint names `--scope user`),
/// front-loads them again. Silent once the governor marker stands: a plugin
/// enabled after `init --mode governor-mcp` disabled it is the user's choice.
/// File-only.
fn claude_mod_front_loads_under_governor(claude_dir: &std::path::Path) -> bool {
    use lean_ctx_hooks::hooks::agents::{claude_governor, claude_mod};
    let governor_mcp = lean_ctx::core::home::resolve_home_dir()
        .and_then(|h| claude_governor::installed_arm(&h))
        == Some(claude_governor::GovernorArm::McpDeferred);
    let chosen = claude_mod::governor_disabled_marker().is_ok_and(|m| m.exists());
    governor_mcp
        && !chosen
        && std::fs::read_to_string(claude_dir.join("settings.json"))
            .is_ok_and(|text| core_plugin_enabled(&text))
}

/// Whether Claude settings text (JSONC, as Claude Code accepts it) enables
/// the core plugin.
pub(crate) fn core_plugin_enabled(settings: &str) -> bool {
    lean_ctx::core::jsonc::parse_jsonc(settings).is_ok_and(|doc| {
        doc.get("enabledPlugins")
            .and_then(|p| p.get(lean_ctx_hooks::hooks::agents::claude_mod::PLUGIN_ID))
            .and_then(serde_json::Value::as_bool)
            == Some(true)
    })
}

/// Claude Code's Code Mode next to lean-ctx: shown once the mod is installed,
/// informational only. Names the one thing `lean-ctx claude-mod code-mode`
/// cannot fix itself. File-only — no `claude` spawn.
pub(crate) fn claude_code_mode_outcome() -> Option<Outcome> {
    use lean_ctx_hooks::hooks::agents::claude_mod::{self, CodeModeReadiness};
    let claude_dir = std::env::var_os("CLAUDE_CONFIG_DIR")
        .map(std::path::PathBuf::from)
        .or_else(|| dirs::home_dir().map(|h| h.join(".claude")))?;
    if claude_mod::cached_versions(&claude_dir).is_empty() {
        return None;
    }
    let line = match claude_mod::code_mode_readiness(&claude_dir) {
        CodeModeReadiness::Ready => format!(
            "{BOLD}Code Mode{RST}        {GREEN}ready{RST}  {DIM}(run: lean-ctx claude-mod code-mode){RST}"
        ),
        CodeModeReadiness::Gateway(reason) => {
            format!("{BOLD}Code Mode{RST}        {YELLOW}unavailable: {reason}{RST}")
        }
        CodeModeReadiness::StaleHooks => format!(
            "{BOLD}Code Mode{RST}        {YELLOW}lean-ctx hooks predate Code Mode; a hook failure would refuse tool calls{RST}  {DIM}(run: lean-ctx init --agent claude){RST}"
        ),
    };
    Some(Outcome { ok: true, line })
}

pub(crate) fn skill_files_outcome() -> Outcome {
    let Some(home) = dirs::home_dir() else {
        return Outcome {
            ok: false,
            line: format!("{BOLD}SKILL.md{RST}  {RED}could not resolve home directory{RST}"),
        };
    };

    let candidates = [
        ("Claude Code", home.join(".claude/skills/lean-ctx/SKILL.md")),
        (
            "CodeBuddy",
            home.join(".codebuddy/skills/lean-ctx/SKILL.md"),
        ),
        ("Cursor", home.join(".cursor/skills/lean-ctx/SKILL.md")),
        (
            "Codex CLI",
            lean_ctx::core::home::resolve_codex_dir()
                .unwrap_or_else(|| home.join(".codex"))
                .join("skills/lean-ctx/SKILL.md"),
        ),
        (
            "GitHub Copilot",
            home.join(".copilot/skills/lean-ctx/SKILL.md"),
        ),
        (
            "OpenCode",
            home.join(".config/opencode/skills/lean-ctx/SKILL.md"),
        ),
    ];

    let mut found: Vec<&str> = Vec::new();
    for (name, path) in &candidates {
        if path.exists() {
            found.push(name);
        }
    }

    // The governor setup drops the skill listing (it rides every request).
    let governor = [
        "claude",
        "codebuddy",
        "cursor",
        "gemini",
        "codex",
        "copilot",
    ]
    .iter()
    .filter_map(|key| lean_ctx_hooks::hooks::governor_default::host(key))
    .any(|h| (h.is_installed)(&home));
    if found.is_empty() && governor {
        Outcome {
            ok: true,
            line: format!(
                "{BOLD}SKILL.md{RST}  {GREEN}not needed{RST}  {DIM}(governor setup carries no skill listing){RST}"
            ),
        }
    } else if found.is_empty() {
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}SKILL.md{RST}  {YELLOW}not installed{RST}  {DIM}(run: lean-ctx setup){RST}"
            ),
        }
    } else {
        Outcome {
            ok: true,
            line: format!(
                "{BOLD}SKILL.md{RST}  {GREEN}installed for {}{RST}",
                found.join(", ")
            ),
        }
    }
}
/// GH #594 config parity: surface whether the CLI and the editor-spawned MCP
/// server resolve the *same* `config.toml`.
///
/// The current MCP writers never emit an `env` block, so any editor entry that
/// still pins `LEAN_CTX_DATA_DIR` is stale and would force that editor's MCP
/// server into single-dir mode — reading a different config than this CLI. This
/// complements the stray-config heal check (which catches the *symptom*, a
/// config.toml already stranded in the data dir) by flagging the *cause* before
/// a divergent file is ever written, and it always prints the resolved path so
/// users can compare it against `lean-ctx config path` run inside their editor.
/// Extract the editor-baked `LEAN_CTX_DATA_DIR` value from raw config text.
///
/// Works across the JSON / TOML / YAML editor configs because all three write
/// the value as the first double-quoted token after the key on its line
/// (`LEAN_CTX_DATA_DIR = "…"`, `"LEAN_CTX_DATA_DIR": "…"`, `LEAN_CTX_DATA_DIR: "…"`).
/// `~/`, `$HOME` and `$XDG_DATA_HOME` are expanded so the result can be compared
/// against the CLI's resolved standard data dir; a trailing separator is trimmed.
pub(crate) fn pinned_data_dir(content: &str) -> Option<std::path::PathBuf> {
    const KEY: &str = "LEAN_CTX_DATA_DIR";
    let line = content.lines().find(|l| l.contains(KEY))?;
    let after_key = &line[line.find(KEY)? + KEY.len()..];
    // Skip the assignment separator (`=` for TOML, `:` for JSON/YAML). In JSON
    // the key's own closing quote precedes the separator, so anchoring on the
    // separator avoids mistaking that quote for the value's opening quote.
    let after_sep = &after_key[after_key.find(['=', ':'])? + 1..];
    let open = after_sep.find('"')? + 1;
    let rest = &after_sep[open..];
    let raw = rest[..rest.find('"')?].trim();
    if raw.is_empty() {
        return None;
    }
    let mut s = raw.to_string();
    if let Ok(home) = std::env::var("HOME") {
        s = s.replace("${HOME}", &home).replace("$HOME", &home);
    }
    if let Ok(xdg) = std::env::var("XDG_DATA_HOME") {
        s = s
            .replace("${XDG_DATA_HOME}", &xdg)
            .replace("$XDG_DATA_HOME", &xdg);
    }
    let path = match s.strip_prefix("~/") {
        Some(tail) => dirs::home_dir()?.join(tail),
        None => std::path::PathBuf::from(s.trim_end_matches('/')),
    };
    Some(std::path::PathBuf::from(
        path.to_string_lossy().trim_end_matches('/'),
    ))
}

pub(crate) fn config_parity_outcome() -> Outcome {
    let config_path = lean_ctx::core::config::Config::path()
        .map_or_else(|| "<unresolved>".to_string(), |p| p.display().to_string());

    // Only a *non-standard* pin actually moves the MCP server's config.toml away
    // from the CLI's (#594). A pin equal to the standard `$XDG_DATA_HOME/lean-ctx`
    // is data-only and keeps parity, so it must NOT be flagged (no false alarm
    // for the value editors used to bake by default).
    let mut divergent: Vec<String> = match dirs::home_dir() {
        Some(home) => lean_ctx::core::editor_registry::detect::build_targets(&home)
            .into_iter()
            .filter(|t| {
                std::fs::read_to_string(&t.config_path)
                    .ok()
                    .filter(|c| c.contains("lean-ctx"))
                    .and_then(|c| pinned_data_dir(&c))
                    .is_some_and(|pin| lean_ctx::core::paths::data_pin_diverges_config(&pin))
            })
            .map(|t| t.name.to_string())
            .collect(),
        None => Vec::new(),
    };
    divergent.sort_unstable();
    divergent.dedup();

    if divergent.is_empty() {
        Outcome {
            ok: true,
            line: format!(
                "{BOLD}config parity{RST}  {GREEN}CLI + MCP agree{RST}  {DIM}{config_path}  (verify in your editor: lean-ctx config path){RST}"
            ),
        }
    } else {
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}config parity{RST}  {YELLOW}{} pin a non-standard LEAN_CTX_DATA_DIR in their MCP config{RST}  {DIM}-> that editor's MCP server resolves a different config.toml than the CLI ({config_path}); run: lean-ctx setup (or doctor --fix) to unify{RST}",
                divergent.join(", ")
            ),
        }
    }
}
/// A user-global agent MCP entry that pins `LEAN_CTX_PROJECT_ROOT` /
/// `LEAN_CTX_EXTRA_ROOTS` binds every session of that agent — in any repo — to
/// one project. Builds before the pin heal wrote these themselves.
pub(crate) fn project_pin_outcome() -> Outcome {
    let pinned = dirs::home_dir()
        .map(|home| lean_ctx_hooks::hooks::project_pins::pinned_agent_configs(&home))
        .unwrap_or_default();
    if pinned.is_empty() {
        return Outcome {
            ok: true,
            line: format!(
                "{BOLD}Project binding{RST}  {GREEN}per session{RST}  {DIM}(no agent config pins a project){RST}"
            ),
        };
    }
    let list = pinned
        .iter()
        .map(|(agent, path)| format!("{agent} ({})", path.display()))
        .collect::<Vec<_>>()
        .join(", ");
    Outcome {
        ok: false,
        line: format!(
            "{BOLD}Project binding{RST}  {YELLOW}pinned globally in {list}{RST}  {DIM}-> every session of that agent opens the same project; run: lean-ctx doctor --fix, then restart the agent{RST}"
        ),
    }
}

/// How the code graph uses the language servers listed below.
pub(crate) fn semantic_mode_outcome() -> Outcome {
    use lean_ctx::core::config::{Config, SemanticMode};
    let explain = match SemanticMode::effective(&Config::load()) {
        SemanticMode::Off => "off  — structural graph only, no semantic backend is queried",
        SemanticMode::Auto => {
            "auto — verifies graph edges with servers that are already running or a live IDE; never starts one in the background"
        }
        SemanticMode::Eager => {
            "eager — background graph enrichment may start the language servers below (trusted workspaces only; others run as auto)"
        }
    };
    Outcome {
        ok: true,
        line: format!("{BOLD}semantic_mode{RST}  {explain}"),
    }
}

/// Per-language verified share of the current project's call edges, when the
/// project has a property graph. Opening a graph is read-only here: doctor
/// never builds one.
pub(crate) fn semantic_coverage_outcomes() -> Vec<Outcome> {
    let Some(root) = lean_ctx::core::config::Config::find_project_root() else {
        return Vec::new();
    };
    if !lean_ctx::core::property_graph::graph_dir(&root)
        .join("graph.db")
        .exists()
    {
        return Vec::new();
    }
    let Ok(graph) = lean_ctx::core::property_graph::CodeGraph::open(&root) else {
        return Vec::new();
    };
    use lean_ctx::core::semantic::coverage::{coverage_by_language, coverage_lines};
    coverage_lines(&coverage_by_language(&graph), &root)
        .into_iter()
        .map(|line| Outcome {
            ok: true,
            line: format!("{BOLD}coverage{RST} {}", line.trim_start()),
        })
        .collect()
}

pub(crate) fn lsp_server_outcomes() -> Vec<Outcome> {
    use lean_ctx::lsp::config::{KNOWN_SERVERS, resolve_server};

    KNOWN_SERVERS
        .iter()
        .map(|info| match resolve_server(info.language, None) {
            Some(server) => Outcome {
                ok: true,
                line: format!(
                    "{BOLD}{}{RST}  {GREEN}✓ {}{RST}  {DIM}{}{RST}",
                    info.language,
                    server.binary_name(),
                    server.binary_path().display()
                ),
            },
            None => Outcome {
                ok: false,
                line: format!(
                    "{BOLD}{}{RST}  {DIM}not installed{RST}  {YELLOW}{}{RST}",
                    info.language, info.install_hint
                ),
            },
        })
        .collect()
}
/// True when `cwd_str` points inside an IDE/agent config directory rather than a
/// real project (LM Studio, Claude, CodeBuddy, Codex). Matches both POSIX (`/`)
/// and Windows (`\`) separators so the warning fires on every platform.
pub(crate) fn cwd_looks_like_agent_dir(cwd_str: &str) -> bool {
    lean_ctx::core::pathutil::is_agent_config_dir(std::path::Path::new(cwd_str))
}

/// IDE-specific hint for doctor output when project root detection fails.
fn detect_ide_hint() -> String {
    if std::env::var("CURSOR_PROJECT_DIR").is_ok()
        || std::env::var("WORKSPACE_FOLDER_PATHS").is_ok()
    {
        return "IDE: Cursor (env vars present, root should auto-resolve).".into();
    }
    if std::env::var("VSCODE_WORKSPACE_FOLDER").is_ok() {
        return "IDE: VS Code (roots/list should resolve the project).".into();
    }
    if std::env::var("CLAUDE_PROJECT_DIR").is_ok() {
        return "IDE: Claude Code.".into();
    }
    if std::env::var("WINDSURF_PROJECT_DIR").is_ok() {
        return "IDE: Windsurf.".into();
    }
    if std::env::var("CODEX_SANDBOX_DIR").is_ok() {
        return "IDE: Codex.".into();
    }
    if std::env::var("TRAE_PROJECT_DIR").is_ok() {
        return "IDE: Trae.".into();
    }
    if std::env::var("KIRO_PROJECT_DIR").is_ok() {
        return "IDE: Kiro.".into();
    }
    "No IDE env var detected. If issues persist: lean-ctx init --agent <your-ide> from project dir."
        .into()
}

/// Warn if lean-ctx is running as an MCP server from a directory that lacks
/// a project marker and looks like an IDE/agent tool directory (e.g. .lmstudio,
/// .claude). This usually means the MCP client launched the process from the
/// wrong CWD, causing "path escapes project root" errors for every tool call.
pub(crate) fn mcp_server_cwd_outcome() -> Outcome {
    let is_mcp = lean_ctx::core::runtime_flags::mcp_server_enabled();
    if !is_mcp {
        return Outcome {
            ok: true,
            line: format!("{BOLD}MCP server CWD{RST}  {DIM}(not running as MCP server){RST}"),
        };
    }

    let Ok(cwd) = std::env::current_dir() else {
        return Outcome {
            ok: true,
            line: format!("{BOLD}MCP server CWD{RST}  {YELLOW}could not resolve{RST}"),
        };
    };

    let has_marker = lean_ctx::core::pathutil::has_project_marker(&cwd);
    let cwd_str = cwd.to_string_lossy();
    let suspicious = cwd_looks_like_agent_dir(&cwd_str);

    if has_marker {
        Outcome {
            ok: true,
            line: format!(
                "{BOLD}MCP server CWD{RST}  {GREEN}under project root{RST}  {DIM}{}{RST}",
                cwd.display()
            ),
        }
    } else if suspicious {
        let ide_hint = detect_ide_hint();
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}MCP server CWD{RST}  {YELLOW}launched from IDE config dir{RST}                   {DIM}cwd={} — lean-ctx auto-corrects to the real project on first                  file access (zero-config). {ide_hint}{RST}",
                cwd.display()
            ),
        }
    } else {
        let ide_hint = detect_ide_hint();
        Outcome {
            ok: false,
            line: format!(
                "{BOLD}MCP server CWD{RST}  {YELLOW}no project marker in CWD{RST}                   {DIM}cwd={} — lean-ctx auto-corrects to the real project on first                  file access. {ide_hint}{RST}",
                cwd.display()
            ),
        }
    }
}

/// GH #1470: Detect version skew between CLI binary and installed agent-bridge
/// packages (pi-lean-ctx, Cursor extension, CodeBuddy extension).
pub(crate) fn version_skew_outcomes() -> Vec<Outcome> {
    let cli_version = env!("CARGO_PKG_VERSION");
    let mut results = Vec::new();

    // 1) pi-lean-ctx npm package version
    if let Some(pi_ver) = detect_pi_lean_ctx_version() {
        let ok = versions_compatible(cli_version, &pi_ver);
        let status = if ok {
            format!("{GREEN}{pi_ver}{RST}")
        } else {
            format!(
                "{RED}{pi_ver} (CLI is {cli_version}){RST}  {DIM}update: pi install npm:pi-lean-ctx@latest{RST}"
            )
        };
        results.push(Outcome {
            ok,
            line: format!("{BOLD}Bridge version: pi-lean-ctx{RST}  {status}"),
        });
    }

    // 2) Cursor extension version (from package.json in known paths)
    if let Some(ext_ver) = detect_cursor_extension_version() {
        let ok = versions_compatible(cli_version, &ext_ver);
        let status = if ok {
            format!("{GREEN}{ext_ver}{RST}")
        } else {
            format!(
                "{RED}{ext_ver} (CLI is {cli_version}){RST}  {DIM}update the lean-ctx extension in Cursor{RST}"
            )
        };
        results.push(Outcome {
            ok,
            line: format!("{BOLD}Bridge version: Cursor extension{RST}  {status}"),
        });
    }

    results
}

/// Two versions are "compatible" if their major.minor match.
/// For pre-1.0 (0.x.y), major.minor.patch must match exactly.
fn versions_compatible(cli: &str, bridge: &str) -> bool {
    let cli_parts: Vec<&str> = cli.split('.').collect();
    let bridge_parts: Vec<&str> = bridge.split('.').collect();

    if cli_parts.len() < 2 || bridge_parts.len() < 2 {
        return cli == bridge;
    }

    let cli_major = cli_parts[0];
    let cli_minor = cli_parts[1];
    let bridge_major = bridge_parts[0];
    let bridge_minor = bridge_parts[1];

    if cli_major == "0" || bridge_major == "0" {
        cli == bridge
    } else {
        cli_major == bridge_major && cli_minor == bridge_minor
    }
}

fn detect_pi_lean_ctx_version() -> Option<String> {
    let output = std::process::Command::new("pi")
        .args(["list", "--json"])
        .output()
        .ok()?;

    if !output.status.success() {
        let text_output = std::process::Command::new("pi")
            .args(["list"])
            .output()
            .ok()?;
        if !text_output.status.success() {
            return None;
        }
        let text = String::from_utf8_lossy(&text_output.stdout);
        for line in text.lines() {
            if line.contains("pi-lean-ctx") {
                if let Some(at_pos) = line.rfind('@') {
                    let ver = line[at_pos + 1..].trim();
                    if !ver.is_empty() {
                        return Some(ver.to_string());
                    }
                }
            }
        }
        return None;
    }

    let json: serde_json::Value = serde_json::from_slice(&output.stdout).ok()?;
    if let Some(packages) = json.as_array() {
        for pkg in packages {
            let name = pkg.get("name").and_then(|v| v.as_str()).unwrap_or("");
            if name == "pi-lean-ctx" || name.contains("pi-lean-ctx") {
                if let Some(ver) = pkg.get("version").and_then(|v| v.as_str()) {
                    return Some(ver.to_string());
                }
            }
        }
    }

    None
}

fn detect_cursor_extension_version() -> Option<String> {
    let home = dirs::home_dir()?;
    let ext_dir = home.join(".cursor").join("extensions");
    if !ext_dir.is_dir() {
        return None;
    }

    let entries = std::fs::read_dir(&ext_dir).ok()?;
    for entry in entries.flatten() {
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        if name_str.contains("lean-ctx") || name_str.contains("leanctx") {
            let pkg_json = entry.path().join("package.json");
            if let Ok(content) = std::fs::read_to_string(&pkg_json) {
                if let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) {
                    if let Some(ver) = json.get("version").and_then(|v| v.as_str()) {
                        return Some(ver.to_string());
                    }
                }
            }
        }
    }

    None
}

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

    #[test]
    fn compatible_same_version() {
        assert!(versions_compatible("3.9.18", "3.9.18"));
    }

    #[test]
    fn compatible_same_major_minor() {
        assert!(versions_compatible("3.9.18", "3.9.20"));
    }

    #[test]
    fn incompatible_different_minor() {
        assert!(!versions_compatible("3.9.18", "3.8.18"));
    }

    #[test]
    fn incompatible_different_major() {
        assert!(!versions_compatible("3.9.18", "4.9.18"));
    }

    #[test]
    fn pre_1_0_requires_exact() {
        assert!(!versions_compatible("0.9.1", "0.9.2"));
        assert!(versions_compatible("0.9.1", "0.9.1"));
    }
}