lean-ctx 3.9.5

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
use std::path::{Path, PathBuf};

use super::paths::{
    augment_cli_settings_path, augment_vscode_mcp_path, claude_mcp_json_path, cline_mcp_path,
    codebuddy_mcp_json_path, qoder_all_mcp_paths, qoderwork_mcp_path, roo_mcp_path,
    vscode_insiders_mcp_path, vscode_mcp_path, zed_config_dir, zed_settings_path,
};
use super::types::{ConfigType, EditorTarget};

pub fn build_targets(home: &Path) -> Vec<EditorTarget> {
    #[cfg(windows)]
    let opencode_cfg = if let Ok(appdata) = std::env::var("APPDATA") {
        PathBuf::from(appdata)
            .join("opencode")
            .join("opencode.json")
    } else {
        home.join(".config/opencode/opencode.json")
    };
    #[cfg(not(windows))]
    let opencode_cfg = home.join(".config/opencode/opencode.json");

    #[cfg(windows)]
    let opencode_detect = opencode_cfg
        .parent()
        .map(|p| p.to_path_buf())
        .unwrap_or_else(|| home.join(".config/opencode"));
    #[cfg(not(windows))]
    let opencode_detect = home.join(".config/opencode");

    let mut targets = vec![
        EditorTarget {
            name: "Cursor",
            agent_key: "cursor".to_string(),
            config_path: home.join(".cursor/mcp.json"),
            detect_path: home.join(".cursor"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Claude Code",
            agent_key: "claude".to_string(),
            config_path: claude_mcp_json_path(home),
            detect_path: detect_claude_path(),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "CodeBuddy",
            agent_key: "codebuddy".to_string(),
            config_path: codebuddy_mcp_json_path(home),
            detect_path: detect_codebuddy_path(),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Augment CLI",
            agent_key: "augment".to_string(),
            config_path: augment_cli_settings_path(home),
            detect_path: detect_augment_path(home),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Augment (VS Code)",
            agent_key: "augment".to_string(),
            config_path: augment_vscode_mcp_path(home),
            detect_path: detect_augment_vscode_path(home),
            config_type: ConfigType::AugmentVsCode,
        },
        EditorTarget {
            name: "Windsurf",
            agent_key: "windsurf".to_string(),
            config_path: home.join(".codeium/windsurf/mcp_config.json"),
            detect_path: home.join(".codeium/windsurf"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Codex CLI",
            agent_key: "codex".to_string(),
            config_path: crate::core::home::resolve_codex_dir()
                .unwrap_or_else(|| home.join(".codex"))
                .join("config.toml"),
            detect_path: detect_codex_path(home),
            config_type: ConfigType::Codex,
        },
        EditorTarget {
            name: "Gemini CLI",
            agent_key: "gemini".to_string(),
            config_path: home.join(".gemini/settings.json"),
            detect_path: home.join(".gemini"),
            config_type: ConfigType::GeminiSettings,
        },
        EditorTarget {
            name: "Antigravity IDE",
            agent_key: "antigravity".to_string(),
            config_path: home.join(".gemini/antigravity/mcp_config.json"),
            detect_path: home.join(".gemini/antigravity"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Antigravity CLI",
            agent_key: "antigravity-cli".to_string(),
            config_path: home.join(".gemini/antigravity-cli/mcp_config.json"),
            detect_path: home.join(".gemini/antigravity-cli"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Zed",
            agent_key: "zed".to_string(),
            config_path: zed_settings_path(home),
            detect_path: zed_config_dir(home),
            config_type: ConfigType::Zed,
        },
        EditorTarget {
            name: "VS Code",
            agent_key: "vscode".to_string(),
            config_path: vscode_mcp_path(),
            detect_path: detect_vscode_path(),
            config_type: ConfigType::VsCodeMcp,
        },
        EditorTarget {
            name: "VS Code Insiders",
            agent_key: "vscode-insiders".to_string(),
            config_path: vscode_insiders_mcp_path(),
            detect_path: detect_vscode_insiders_path(),
            config_type: ConfigType::VsCodeMcp,
        },
        EditorTarget {
            name: "Copilot CLI",
            agent_key: "copilot".to_string(),
            config_path: home.join(".copilot/mcp-config.json"),
            detect_path: home.join(".copilot"),
            config_type: ConfigType::CopilotCli,
        },
        EditorTarget {
            name: "OpenCode",
            agent_key: "opencode".to_string(),
            config_path: opencode_cfg,
            detect_path: opencode_detect,
            config_type: ConfigType::OpenCode,
        },
        EditorTarget {
            name: "Qwen Code",
            agent_key: "qwen".to_string(),
            config_path: home.join(".qwen/settings.json"),
            detect_path: home.join(".qwen"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Trae",
            agent_key: "trae".to_string(),
            config_path: home.join(".trae/mcp.json"),
            detect_path: home.join(".trae"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Amazon Q Developer",
            agent_key: "amazonq".to_string(),
            config_path: home.join(".aws/amazonq/default.json"),
            detect_path: home.join(".aws/amazonq"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "JetBrains IDEs",
            agent_key: "jetbrains".to_string(),
            config_path: home.join(".jb-mcp.json"),
            detect_path: detect_jetbrains_path(home),
            config_type: ConfigType::JetBrains,
        },
        EditorTarget {
            name: "Cline",
            agent_key: "cline".to_string(),
            config_path: cline_mcp_path(),
            detect_path: detect_cline_path(),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Roo Code",
            agent_key: "roo".to_string(),
            config_path: roo_mcp_path(),
            detect_path: detect_roo_path(),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "AWS Kiro",
            agent_key: "kiro".to_string(),
            config_path: home.join(".kiro/settings/mcp.json"),
            detect_path: home.join(".kiro"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Verdent",
            agent_key: "verdent".to_string(),
            config_path: home.join(".verdent/mcp.json"),
            detect_path: home.join(".verdent"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Crush",
            agent_key: "crush".to_string(),
            config_path: home.join(".config/crush/crush.json"),
            detect_path: home.join(".config/crush"),
            config_type: ConfigType::Crush,
        },
        // NOTE: Pi Coding Agent is deliberately NOT an MCP-config target. Pi has
        // no native MCP adapter — a `lean-ctx` entry in ~/.pi/agent/mcp.json is
        // never served, and older pi-lean-ctx versions read it as "adapter
        // configured" and disabled their embedded bridge (GitHub #361). Pi is
        // integrated via the pi-lean-ctx npm package (embedded MCP bridge) and
        // AGENTS.md; `init --agent pi` removes stale mcp.json entries.
        EditorTarget {
            name: "Amp",
            agent_key: "amp".to_string(),
            config_path: home.join(".config/amp/settings.json"),
            detect_path: home.join(".config/amp"),
            config_type: ConfigType::Amp,
        },
        EditorTarget {
            name: "QoderWork",
            agent_key: "qoderwork".to_string(),
            config_path: qoderwork_mcp_path(home),
            detect_path: detect_qoderwork_path(home),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Hermes Agent",
            agent_key: "hermes".to_string(),
            config_path: home.join(".hermes/config.yaml"),
            detect_path: home.join(".hermes"),
            config_type: ConfigType::HermesYaml,
        },
        EditorTarget {
            name: "Aider",
            agent_key: "aider".to_string(),
            config_path: home.join(".aider/mcp.json"),
            detect_path: home.join(".aider"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Continue",
            agent_key: "continue".to_string(),
            config_path: home.join(".continue/mcp.json"),
            detect_path: home.join(".continue"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Neovim (mcphub.nvim)",
            agent_key: "neovim".to_string(),
            config_path: home.join(".config/mcphub/servers.json"),
            detect_path: home.join(".config/nvim"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Emacs (mcp.el)",
            agent_key: "emacs".to_string(),
            config_path: home.join(".emacs.d/mcp.json"),
            detect_path: home.join(".emacs.d"),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "Sublime Text",
            agent_key: "sublime".to_string(),
            config_path: detect_sublime_mcp_path(home),
            detect_path: detect_sublime_path(home),
            config_type: ConfigType::McpJson,
        },
        EditorTarget {
            name: "OpenClaw",
            agent_key: "openclaw".to_string(),
            config_path: home.join(".openclaw/openclaw.json"),
            detect_path: home.join(".openclaw"),
            // NOT McpJson: OpenClaw >= 2026.6.1 validates strictly and rejects
            // a top-level `mcpServers` key ("Unrecognized key") — it requires
            // the nested `mcp.servers` schema (GitHub #390).
            config_type: ConfigType::OpenClaw,
        },
    ];

    targets.extend(
        qoder_all_mcp_paths(home)
            .into_iter()
            .map(|config_path| EditorTarget {
                name: "Qoder",
                agent_key: "qoder".to_string(),
                config_path,
                detect_path: detect_qoder_path(home),
                config_type: ConfigType::QoderSettings,
            }),
    );

    targets
}

fn detect_qoder_path(home: &Path) -> PathBuf {
    let qoder_dir = home.join(".qoder");
    if qoder_dir.exists() {
        return qoder_dir;
    }
    #[cfg(target_os = "macos")]
    {
        let app_dir = home.join("Library/Application Support/Qoder");
        if app_dir.exists() {
            return app_dir;
        }
    }
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            let app_dir = PathBuf::from(appdata).join("Qoder");
            if app_dir.exists() {
                return app_dir;
            }
        }
    }
    PathBuf::from("/nonexistent")
}

fn detect_qoderwork_path(home: &Path) -> PathBuf {
    let dir = home.join(".qoderwork");
    if dir.exists() {
        return dir;
    }
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            let app_dir = PathBuf::from(appdata).join("QoderWork");
            if app_dir.exists() {
                return app_dir;
            }
        }
    }
    PathBuf::from("/nonexistent")
}

fn detect_sublime_path(home: &Path) -> PathBuf {
    #[cfg(target_os = "macos")]
    {
        let app_dir = home.join("Library/Application Support/Sublime Text");
        if app_dir.exists() {
            return app_dir;
        }
    }
    let xdg_dir = home.join(".config/sublime-text");
    if xdg_dir.exists() {
        return xdg_dir;
    }
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            let app_dir = PathBuf::from(appdata).join("Sublime Text");
            if app_dir.exists() {
                return app_dir;
            }
        }
    }
    PathBuf::from("/nonexistent")
}

fn detect_sublime_mcp_path(home: &Path) -> PathBuf {
    #[cfg(target_os = "macos")]
    {
        let app_dir = home.join("Library/Application Support/Sublime Text/Packages/User/mcp.json");
        if app_dir.parent().is_some_and(std::path::Path::exists) {
            return app_dir;
        }
    }
    home.join(".config/sublime-text/mcp.json")
}

pub fn detect_claude_path() -> PathBuf {
    let which_cmd = if cfg!(windows) { "where" } else { "which" };
    if let Ok(output) = std::process::Command::new(which_cmd).arg("claude").output()
        && output.status.success()
    {
        return PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
    }
    if let Ok(dir) = std::env::var("CLAUDE_CONFIG_DIR") {
        let dir = dir.trim();
        if !dir.is_empty() {
            let p = PathBuf::from(dir);
            if p.exists() {
                return p;
            }
        }
    }
    if let Some(home) = dirs::home_dir() {
        let claude_json = claude_mcp_json_path(&home);
        if claude_json.exists() {
            return claude_json;
        }
        // State dir counts as installed: doctor's Claude checks, rules_inject
        // and skills all key off `~/.claude/` existing. Without this fallback
        // a machine with only the state dir hits a dead loop — doctor says
        // "run: lean-ctx setup", but setup skips Claude (detect_path missing)
        // and the warning never clears.
        let state_dir = super::claude_state_dir(&home);
        if state_dir.exists() {
            return state_dir;
        }
    }
    PathBuf::from("/nonexistent")
}

pub fn detect_codebuddy_path() -> PathBuf {
    let which_cmd = if cfg!(windows) { "where" } else { "which" };
    if let Ok(output) = std::process::Command::new(which_cmd)
        .arg("codebuddy")
        .output()
        && output.status.success()
    {
        return PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
    }
    if let Ok(dir) = std::env::var("CODEBUDDY_CONFIG_DIR") {
        let dir = dir.trim();
        if !dir.is_empty() {
            let p = PathBuf::from(dir);
            if p.exists() {
                return p;
            }
        }
    }
    if let Some(home) = dirs::home_dir() {
        let codebuddy_json = codebuddy_mcp_json_path(&home);
        if codebuddy_json.exists() {
            return codebuddy_json;
        }
        // Same state-dir fallback as detect_claude_path (CodeBuddy mirrors
        // Claude Code's layout everywhere else — doctor, rules, skills).
        let state_dir = super::codebuddy_state_dir(&home);
        if state_dir.exists() {
            return state_dir;
        }
    }
    PathBuf::from("/nonexistent")
}

pub fn detect_augment_path(home: &Path) -> PathBuf {
    let which_cmd = if cfg!(windows) { "where" } else { "which" };
    if let Ok(output) = std::process::Command::new(which_cmd).arg("auggie").output()
        && output.status.success()
    {
        return PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
    }
    let augment_dir = home.join(".augment");
    if augment_dir.exists() {
        return augment_dir;
    }
    PathBuf::from("/nonexistent")
}

/// Locate the Augment VS Code extension on disk.
///
/// Returns a `PathBuf` that callers use as a "yes/no presence" signal via
/// `.exists()`. There are three positive-detection paths, in order of
/// specificity:
///
///   1. `mcpServers.json` itself exists → return that file path. Strongest
///      signal: the user has already configured at least one MCP server.
///   2. The `augment-global-state/` directory exists (parent-of-parent of
///      the mcp file) → return that directory. The extension has run at
///      least once but no MCP servers have been registered yet.
///   3. The extension's `globalStorage/augment.vscode-augment/` directory
///      exists → return the (still-nonexistent) `mcpServers.json` path.
///      The extension is installed but has never written persistent state;
///      callers will see `.exists() == false` here, which is intentional —
///      it signals "extension present, file not yet created" so `setup`
///      can create it.
///
/// On no match, returns `/nonexistent` so `.exists()` is unambiguously false.
pub fn detect_augment_vscode_path(home: &Path) -> PathBuf {
    let mcp_path = augment_vscode_mcp_path(home);
    if mcp_path.exists() {
        return mcp_path;
    }
    let extension_state = mcp_path
        .parent()
        .and_then(|p| p.parent())
        .map(Path::to_path_buf);
    if let Some(path) = extension_state
        && path.exists()
    {
        return path;
    }
    if detect_extension_installed(home, "augment.vscode-augment") {
        return mcp_path;
    }
    PathBuf::from("/nonexistent")
}

fn detect_extension_installed(home: &Path, extension_id: &str) -> bool {
    #[cfg(target_os = "macos")]
    {
        if home
            .join(format!(
                "Library/Application Support/Code/User/globalStorage/{extension_id}"
            ))
            .exists()
        {
            return true;
        }
    }
    #[cfg(target_os = "linux")]
    {
        if home
            .join(format!(".config/Code/User/globalStorage/{extension_id}"))
            .exists()
        {
            return true;
        }
        if home
            .join(format!(
                ".config/Code - Insiders/User/globalStorage/{extension_id}"
            ))
            .exists()
        {
            return true;
        }
        if home
            .join(format!(
                ".vscode-server/data/User/globalStorage/{extension_id}"
            ))
            .exists()
        {
            return true;
        }
    }
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            if PathBuf::from(appdata)
                .join(format!("Code/User/globalStorage/{extension_id}"))
                .exists()
            {
                return true;
            }
        }
        let _ = home;
    }
    false
}

pub fn detect_codex_path(home: &Path) -> PathBuf {
    let codex_dir = crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
    if codex_dir.exists() {
        return codex_dir;
    }
    let which_cmd = if cfg!(windows) { "where" } else { "which" };
    if let Ok(output) = std::process::Command::new(which_cmd).arg("codex").output()
        && output.status.success()
    {
        return codex_dir;
    }
    PathBuf::from("/nonexistent")
}

pub fn detect_vscode_path() -> PathBuf {
    #[cfg(target_os = "macos")]
    {
        if let Some(home) = dirs::home_dir() {
            let vscode = home.join("Library/Application Support/Code/User/settings.json");
            if vscode.exists() {
                return vscode;
            }
        }
    }
    #[cfg(target_os = "linux")]
    {
        if let Some(home) = dirs::home_dir() {
            let paths = [
                home.join(".config/Code/User/settings.json"),
                home.join(".config/Code - Insiders/User/settings.json"),
                home.join(".vscode-server/data/User/settings.json"),
            ];
            for vscode in paths {
                if vscode.exists() {
                    return vscode;
                }
            }
        }
    }
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            let vscode = PathBuf::from(appdata).join("Code/User/settings.json");
            if vscode.exists() {
                return vscode;
            }
        }
    }
    let which_cmd = if cfg!(windows) { "where" } else { "which" };
    if let Ok(output) = std::process::Command::new(which_cmd).arg("code").output()
        && output.status.success()
    {
        return PathBuf::from(String::from_utf8_lossy(&output.stdout).trim());
    }
    PathBuf::from("/nonexistent")
}

/// VS Code **Insiders** detection: its `settings.json` in the dedicated
/// `Code - Insiders` profile dir. Deliberately no `which code-insiders`
/// fallback — a PATH hit without the profile dir means the app never ran,
/// so there is no User dir to configure yet (GH #694).
pub fn detect_vscode_insiders_path() -> PathBuf {
    let Some(home) = dirs::home_dir() else {
        return PathBuf::from("/nonexistent");
    };
    #[cfg(target_os = "macos")]
    let candidate = home.join("Library/Application Support/Code - Insiders/User/settings.json");
    #[cfg(target_os = "windows")]
    let candidate = match std::env::var("APPDATA") {
        Ok(appdata) => PathBuf::from(appdata).join("Code - Insiders/User/settings.json"),
        Err(_) => home.join("AppData/Roaming/Code - Insiders/User/settings.json"),
    };
    #[cfg(not(any(target_os = "macos", target_os = "windows")))]
    let candidate = home.join(".config/Code - Insiders/User/settings.json");

    if candidate.exists() {
        return candidate;
    }
    PathBuf::from("/nonexistent")
}

pub fn detect_jetbrains_path(home: &Path) -> PathBuf {
    #[cfg(target_os = "macos")]
    {
        let lib = home.join("Library/Application Support/JetBrains");
        if lib.exists() {
            return lib;
        }
    }
    #[cfg(target_os = "linux")]
    {
        let cfg = home.join(".config/JetBrains");
        if cfg.exists() {
            return cfg;
        }
    }
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            let jb = std::path::PathBuf::from(appdata).join("JetBrains");
            if jb.exists() {
                return jb;
            }
        }
        if let Ok(local) = std::env::var("LOCALAPPDATA") {
            let jb = std::path::PathBuf::from(local).join("JetBrains");
            if jb.exists() {
                return jb;
            }
        }
    }
    if home.join(".jb-mcp.json").exists() {
        return home.join(".jb-mcp.json");
    }
    PathBuf::from("/nonexistent")
}

#[allow(unreachable_code)]
pub fn detect_cline_path() -> PathBuf {
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            let p = PathBuf::from(appdata).join("Code/User/globalStorage/saoudrizwan.claude-dev");
            if p.exists() {
                return p;
            }
        }
        return PathBuf::from("/nonexistent");
    }

    let Some(home) = dirs::home_dir() else {
        return PathBuf::from("/nonexistent");
    };
    #[cfg(target_os = "macos")]
    {
        let p =
            home.join("Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev");
        if p.exists() {
            return p;
        }
    }
    #[cfg(target_os = "linux")]
    {
        let suffix = "User/globalStorage/saoudrizwan.claude-dev";
        if let Some(p) = detect_vscode_global_storage(&home, suffix) {
            return p;
        }
    }
    PathBuf::from("/nonexistent")
}

#[allow(unreachable_code)]
pub fn detect_roo_path() -> PathBuf {
    #[cfg(target_os = "windows")]
    {
        if let Ok(appdata) = std::env::var("APPDATA") {
            let p =
                PathBuf::from(appdata).join("Code/User/globalStorage/rooveterinaryinc.roo-cline");
            if p.exists() {
                return p;
            }
        }
        return PathBuf::from("/nonexistent");
    }

    let Some(home) = dirs::home_dir() else {
        return PathBuf::from("/nonexistent");
    };
    #[cfg(target_os = "macos")]
    {
        let p = home
            .join("Library/Application Support/Code/User/globalStorage/rooveterinaryinc.roo-cline");
        if p.exists() {
            return p;
        }
    }
    #[cfg(target_os = "linux")]
    {
        let suffix = "User/globalStorage/rooveterinaryinc.roo-cline";
        if let Some(p) = detect_vscode_global_storage(&home, suffix) {
            return p;
        }
    }
    PathBuf::from("/nonexistent")
}

/// Checks VS Code-family base directories (VSCodium, Code - OSS, Code) on Linux
/// and returns the first one where the given suffix directory exists.
#[cfg(target_os = "linux")]
fn detect_vscode_global_storage(home: &Path, suffix: &str) -> Option<PathBuf> {
    const CANDIDATES: &[&str] = &["VSCodium", "Code - OSS", "Code"];
    for base in CANDIDATES {
        let p = home.join(".config").join(base).join(suffix);
        if p.exists() {
            return Some(p);
        }
    }
    None
}

#[cfg(test)]
mod augment_tests {
    use super::*;
    use crate::core::editor_registry::writers::{
        WriteAction, WriteOptions, remove_lean_ctx_server, write_config_with_options,
    };

    #[test]
    fn build_targets_includes_augment_cli_entry() {
        let home = Path::new("/home/tester");
        let target = build_targets(home)
            .into_iter()
            .find(|t| t.agent_key == "augment")
            .expect("augment target should be registered");
        assert_eq!(target.name, "Augment CLI");
        assert_eq!(target.config_path, home.join(".augment/settings.json"));
        assert!(matches!(target.config_type, ConfigType::McpJson));
    }

    #[test]
    fn build_targets_count_backs_supported_agents_claim() {
        // README / VISION advertise "30+ supported AI coding agents". This guards
        // that claim against the registry shrinking below it (i.e. an overclaim).
        let home = Path::new("/home/tester");
        let count = build_targets(home).len();
        assert!(
            count >= 30,
            "registry has only {count} agent targets; the documented '30+ supported agents' claim would be an overclaim"
        );
    }

    #[test]
    fn build_targets_includes_augment_vscode_entry() {
        let home = Path::new("/home/tester");
        let target = build_targets(home)
            .into_iter()
            .find(|t| t.name == "Augment (VS Code)")
            .expect("augment vscode target should be registered");
        assert_eq!(target.agent_key, "augment");
        assert_eq!(target.config_path, augment_vscode_mcp_path(home));
        assert!(matches!(target.config_type, ConfigType::AugmentVsCode));
    }

    // Writer-layer round-trip: verifies the McpJson writer preserves unrelated
    // entries when invoked against the Augment settings.json path. This does NOT
    // exercise the `--agent augment` CLI flow or the setup.rs match arms — those
    // are covered by the subprocess test in tests/setup_ci_smoke.rs.
    #[test]
    fn mcp_json_writer_round_trip_at_augment_settings_path_preserves_other_servers() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let cfg = tmp.path().join(".augment").join("settings.json");
        std::fs::create_dir_all(cfg.parent().unwrap()).unwrap();
        std::fs::write(
            &cfg,
            r#"{ "mcpServers": { "other": { "command": "other-bin", "args": [] } } }"#,
        )
        .unwrap();

        let target = EditorTarget {
            name: "Augment CLI",
            agent_key: "augment".to_string(),
            config_path: cfg.clone(),
            detect_path: PathBuf::from("/nonexistent"),
            config_type: ConfigType::McpJson,
        };

        let install =
            write_config_with_options(&target, "/usr/local/bin/lean-ctx", WriteOptions::default())
                .expect("install");
        assert!(matches!(
            install.action,
            WriteAction::Created | WriteAction::Updated
        ));
        let json: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&cfg).unwrap()).unwrap();
        assert_eq!(json["mcpServers"]["other"]["command"], "other-bin");
        assert_eq!(
            json["mcpServers"]["lean-ctx"]["command"],
            "/usr/local/bin/lean-ctx"
        );

        let uninstall =
            remove_lean_ctx_server(&target, WriteOptions::default()).expect("uninstall");
        assert!(matches!(uninstall.action, WriteAction::Updated));
        let json: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&cfg).unwrap()).unwrap();
        assert!(json["mcpServers"].get("lean-ctx").is_none());
        assert_eq!(json["mcpServers"]["other"]["command"], "other-bin");
    }
}

#[cfg(test)]
mod footgun_audit_tests {
    //! #361 Phase 4 — host-default footgun audit. The failure class is "shipped
    //! default misrepresents the product": a host where lean-ctx registers `ctx_*`
    //! as opt-in MCP tools *next to* the native ones, with nothing steering the
    //! agent to use them — so the default experience shows none of the savings
    //! (the R1 "102 native bash / 0 ctx_shell" finding). These tests pin the
    //! invariants that keep every host out of that trap.
    use super::*;

    #[test]
    fn opt_in_mcp_class_dominates_the_registry() {
        // The opt-in MCP shape (native tools stay; ctx_* merely offered) is the
        // footgun surface. Confirm it is the dominant host class so the default
        // steering below is load-bearing, not incidental.
        let home = Path::new("/home/tester");
        let mcp_like = build_targets(home)
            .iter()
            .filter(|t| matches!(t.config_type, ConfigType::McpJson))
            .count();
        assert!(
            mcp_like >= 10,
            "expected the opt-in MCP class to dominate the registry, got {mcp_like}"
        );
    }

    #[test]
    fn default_ships_the_prefer_ctx_nudge_not_a_naked_opt_in() {
        // THE anti-footgun invariant: a fresh install must NOT ship lean-ctx as a
        // naked opt-in MCP. The default rules-injection mode is Shared, so every
        // host that registers ctx_* also receives the "prefer ctx_* over native"
        // steering. A regression to Off would make the shipped default
        // misrepresent the product on every opt-in MCP host at once.
        let _lock = crate::core::data_dir::test_env_lock();
        crate::test_env::remove_var("LEAN_CTX_RULES_INJECTION");
        let mode = crate::core::config::Config::default().rules_injection_effective();
        crate::test_env::remove_var("LEAN_CTX_RULES_INJECTION");
        assert_eq!(
            mode,
            crate::core::config::RulesInjection::Shared,
            "default rules injection must ship the nudge (Shared), never Off"
        );
    }

    #[test]
    fn injected_nudge_steers_the_heavy_tools_to_ctx() {
        // The nudge must be substantive: it must remap the heavy native tools (the
        // ones that dominate a turn's tokens) and carry an explicit directive — an
        // "installed but toothless" block would be a footgun of its own.
        let block = crate::rules_inject::canonical_rules_block();
        for needle in ["ctx_read", "ctx_search", "ctx_shell"] {
            assert!(block.contains(needle), "nudge missing {needle}");
        }
        assert!(
            block.contains("NEVER use native"),
            "nudge must explicitly forbid native fallbacks"
        );
    }
}

#[cfg(all(test, target_os = "macos"))]
mod tests {
    use super::*;

    #[test]
    #[cfg(target_os = "macos")]
    fn build_targets_includes_all_qoder_macos_mcp_locations() {
        let home = Path::new("/Users/tester");
        let qoder_paths: Vec<_> = build_targets(home)
            .into_iter()
            .filter(|target| target.agent_key == "qoder")
            .map(|target| target.config_path)
            .collect();

        assert_eq!(
            qoder_paths,
            vec![
                home.join(".qoder/mcp.json"),
                home.join("Library/Application Support/Qoder/User/mcp.json"),
                home.join("Library/Application Support/Qoder/SharedClientCache/mcp.json"),
            ]
        );
    }
}