Skip to main content

lean_ctx/
setup.rs

1use std::path::PathBuf;
2
3use crate::core::editor_registry::{ConfigType, EditorTarget, WriteAction, WriteOptions};
4use crate::core::portable_binary::resolve_portable_binary;
5use crate::core::setup_report::{PlatformInfo, SetupItem, SetupReport, SetupStepReport};
6use crate::hooks::{recommend_hook_mode, HookMode};
7use chrono::Utc;
8use std::ffi::OsString;
9
10pub fn claude_config_json_path(home: &std::path::Path) -> PathBuf {
11    crate::core::editor_registry::claude_mcp_json_path(home)
12}
13
14pub fn claude_config_dir(home: &std::path::Path) -> PathBuf {
15    crate::core::editor_registry::claude_state_dir(home)
16}
17
18pub(crate) struct EnvVarGuard {
19    key: &'static str,
20    previous: Option<OsString>,
21}
22
23impl EnvVarGuard {
24    pub(crate) fn set(key: &'static str, value: &str) -> Self {
25        let previous = std::env::var_os(key);
26        std::env::set_var(key, value);
27        Self { key, previous }
28    }
29}
30
31impl Drop for EnvVarGuard {
32    fn drop(&mut self) {
33        if let Some(previous) = &self.previous {
34            std::env::set_var(self.key, previous);
35        } else {
36            std::env::remove_var(self.key);
37        }
38    }
39}
40
41pub fn run_setup() {
42    use crate::terminal_ui;
43
44    if crate::shell::is_non_interactive() {
45        eprintln!("Non-interactive terminal detected (no TTY on stdin).");
46        eprintln!("Running in non-interactive mode (equivalent to: lean-ctx setup --non-interactive --yes)");
47        eprintln!();
48        let opts = SetupOptions {
49            non_interactive: true,
50            yes: true,
51            ..Default::default()
52        };
53        match run_setup_with_options(opts) {
54            Ok(report) => {
55                if !report.warnings.is_empty() {
56                    for w in &report.warnings {
57                        tracing::warn!("{w}");
58                    }
59                }
60            }
61            Err(e) => tracing::error!("Setup error: {e}"),
62        }
63        return;
64    }
65
66    let Some(home) = dirs::home_dir() else {
67        tracing::error!("Cannot determine home directory");
68        std::process::exit(1);
69    };
70
71    let binary = resolve_portable_binary();
72
73    let home_str = home.to_string_lossy().to_string();
74
75    terminal_ui::print_setup_header();
76
77    // Step 1: Shell hook (legacy aliases + universal shell hook)
78    terminal_ui::print_step_header(1, 10, "Shell Hook");
79    crate::cli::cmd_init(&["--global".to_string()]);
80    crate::shell_hook::install_all(false);
81
82    // Step 2: Daemon (optional acceleration for CLI routing)
83    terminal_ui::print_step_header(2, 10, "Daemon");
84    if crate::daemon::is_daemon_running() {
85        terminal_ui::print_status_ok("Daemon running — restarting with current binary…");
86        let _ = crate::daemon::stop_daemon();
87        std::thread::sleep(std::time::Duration::from_millis(500));
88        if let Err(e) = crate::daemon::start_daemon(&[]) {
89            terminal_ui::print_status_warn(&format!("Daemon restart failed: {e}"));
90        }
91    } else if let Err(e) = crate::daemon::start_daemon(&[]) {
92        terminal_ui::print_status_warn(&format!("Daemon start failed: {e}"));
93    }
94
95    // Step 3: Editor auto-detection + configuration
96    terminal_ui::print_step_header(3, 10, "AI Tool Detection");
97
98    let targets = crate::core::editor_registry::build_targets(&home);
99    let mut newly_configured: Vec<&str> = Vec::new();
100    let mut already_configured: Vec<&str> = Vec::new();
101    let mut not_installed: Vec<&str> = Vec::new();
102    let mut errors: Vec<&str> = Vec::new();
103
104    for target in &targets {
105        let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
106
107        if !target.detect_path.exists() {
108            not_installed.push(target.name);
109            continue;
110        }
111
112        let mode = if target.agent_key.is_empty() {
113            HookMode::Mcp
114        } else {
115            recommend_hook_mode(&target.agent_key)
116        };
117
118        match crate::core::editor_registry::write_config_with_options(
119            target,
120            &binary,
121            WriteOptions {
122                overwrite_invalid: false,
123            },
124        ) {
125            Ok(res) if res.action == WriteAction::Already => {
126                terminal_ui::print_status_ok(&format!(
127                    "{:<20} \x1b[36m{mode}\x1b[0m  \x1b[2m{short_path}\x1b[0m",
128                    target.name
129                ));
130                already_configured.push(target.name);
131            }
132            Ok(_) => {
133                terminal_ui::print_status_new(&format!(
134                    "{:<20} \x1b[36m{mode}\x1b[0m  \x1b[2m{short_path}\x1b[0m",
135                    target.name
136                ));
137                newly_configured.push(target.name);
138            }
139            Err(e) => {
140                terminal_ui::print_status_warn(&format!("{}: {e}", target.name));
141                errors.push(target.name);
142            }
143        }
144    }
145
146    let total_ok = newly_configured.len() + already_configured.len();
147    if total_ok == 0 && errors.is_empty() {
148        terminal_ui::print_status_warn(
149            "No AI tools detected. Install one and re-run: lean-ctx setup",
150        );
151    }
152
153    if !not_installed.is_empty() {
154        println!(
155            "  \x1b[2m○ {} not detected: {}\x1b[0m",
156            not_installed.len(),
157            not_installed.join(", ")
158        );
159    }
160
161    // Step 4: Agent rules injection
162    terminal_ui::print_step_header(4, 10, "Agent Rules");
163    let rules_result = crate::rules_inject::inject_all_rules(&home);
164    for name in &rules_result.injected {
165        terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mrules injected\x1b[0m"));
166    }
167    for name in &rules_result.updated {
168        terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mrules updated\x1b[0m"));
169    }
170    for name in &rules_result.already {
171        terminal_ui::print_status_ok(&format!("{name:<20} \x1b[2mrules up-to-date\x1b[0m"));
172    }
173    for err in &rules_result.errors {
174        terminal_ui::print_status_warn(err);
175    }
176    if rules_result.injected.is_empty()
177        && rules_result.updated.is_empty()
178        && rules_result.already.is_empty()
179        && rules_result.errors.is_empty()
180    {
181        terminal_ui::print_status_skip("No agent rules needed");
182    }
183
184    // Agent hooks (mode-aware)
185    for target in &targets {
186        if !target.detect_path.exists() || target.agent_key.is_empty() {
187            continue;
188        }
189        let mode = recommend_hook_mode(&target.agent_key);
190        crate::hooks::install_agent_hook_with_mode(&target.agent_key, true, mode);
191    }
192
193    // Step 5: API Proxy configuration
194    terminal_ui::print_step_header(5, 10, "API Proxy");
195    crate::proxy_setup::install_proxy_env(&home, crate::proxy_setup::default_port(), false);
196    println!();
197    println!("  \x1b[2mStart proxy for maximum token savings:\x1b[0m");
198    println!("    \x1b[1mlean-ctx proxy start\x1b[0m");
199    println!("  \x1b[2mEnable autostart:\x1b[0m");
200    println!("    \x1b[1mlean-ctx proxy start --autostart\x1b[0m");
201
202    // Step 6: SKILL.md installation
203    terminal_ui::print_step_header(6, 10, "Skill Files");
204    let skill_result = install_skill_files(&home);
205    for (name, installed) in &skill_result {
206        if *installed {
207            terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mSKILL.md installed\x1b[0m"));
208        } else {
209            terminal_ui::print_status_ok(&format!("{name:<20} \x1b[2mSKILL.md up-to-date\x1b[0m"));
210        }
211    }
212    if skill_result.is_empty() {
213        terminal_ui::print_status_skip("No skill directories to install");
214    }
215
216    // Step 7: Data directory + diagnostics
217    terminal_ui::print_step_header(7, 10, "Environment Check");
218    let lean_dir = home.join(".lean-ctx");
219    if lean_dir.exists() {
220        terminal_ui::print_status_ok("~/.lean-ctx/ ready");
221    } else {
222        let _ = std::fs::create_dir_all(&lean_dir);
223        terminal_ui::print_status_new("Created ~/.lean-ctx/");
224    }
225    crate::doctor::run_compact();
226
227    // Step 8: Data sharing
228    terminal_ui::print_step_header(8, 10, "Help Improve lean-ctx");
229    println!("  Share anonymous compression stats to make lean-ctx better.");
230    println!("  \x1b[1mNo code, no file names, no personal data — ever.\x1b[0m");
231    println!();
232    print!("  Enable anonymous data sharing? \x1b[1m[y/N]\x1b[0m ");
233    use std::io::Write;
234    std::io::stdout().flush().ok();
235
236    let mut input = String::new();
237    let contribute = if std::io::stdin().read_line(&mut input).is_ok() {
238        let answer = input.trim().to_lowercase();
239        answer == "y" || answer == "yes"
240    } else {
241        false
242    };
243
244    if contribute {
245        let config_dir = home.join(".lean-ctx");
246        let _ = std::fs::create_dir_all(&config_dir);
247        let config_path = config_dir.join("config.toml");
248        let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
249        if !config_content.contains("[cloud]") {
250            if !config_content.is_empty() && !config_content.ends_with('\n') {
251                config_content.push('\n');
252            }
253            config_content.push_str("\n[cloud]\ncontribute_enabled = true\n");
254            let _ = std::fs::write(&config_path, config_content);
255        }
256        terminal_ui::print_status_ok("Enabled — thank you!");
257    } else {
258        terminal_ui::print_status_skip("Skipped — enable later with: lean-ctx config");
259    }
260
261    // Step 9: Premium Features Configuration
262    terminal_ui::print_step_header(9, 10, "Premium Features");
263    configure_premium_features(&home);
264
265    // Step 10: Code Intelligence — build graph in background
266    terminal_ui::print_step_header(10, 10, "Code Intelligence");
267    let cwd = std::env::current_dir().ok();
268    let cwd_is_home = cwd
269        .as_ref()
270        .is_some_and(|d| dirs::home_dir().is_some_and(|h| d.as_path() == h.as_path()));
271    if cwd_is_home {
272        terminal_ui::print_status_warn(
273            "Running from $HOME — graph build skipped to avoid scanning your entire home directory.",
274        );
275        println!(
276            "  \x1b[2mRun `lean-ctx setup` from inside a project for code intelligence.\x1b[0m"
277        );
278    } else {
279        let is_project = cwd.as_ref().is_some_and(|d| {
280            d.join(".git").exists()
281                || d.join("Cargo.toml").exists()
282                || d.join("package.json").exists()
283                || d.join("go.mod").exists()
284        });
285        if is_project {
286            println!("  \x1b[2mBuilding code graph for graph-aware reads, impact analysis,\x1b[0m");
287            println!("  \x1b[2mand smart search fusion in the background...\x1b[0m");
288            if let Some(ref root) = cwd {
289                spawn_index_build_background(root);
290            }
291            terminal_ui::print_status_ok("Graph build started (background)");
292        } else {
293            println!(
294                "  \x1b[2mRun `lean-ctx impact build` inside any git project to enable\x1b[0m"
295            );
296            println!(
297                "  \x1b[2mgraph-aware reads, impact analysis, and smart search fusion.\x1b[0m"
298            );
299        }
300    }
301    println!();
302
303    // Auto-approve transparency banner
304    {
305        let tools = crate::core::editor_registry::writers::auto_approve_tools();
306        println!();
307        println!(
308            "  \x1b[33m⚡ Auto-approved tools ({} total):\x1b[0m",
309            tools.len()
310        );
311        for chunk in tools.chunks(6) {
312            let names: Vec<_> = chunk.iter().map(|t| format!("\x1b[2m{t}\x1b[0m")).collect();
313            println!("    {}", names.join(", "));
314        }
315        println!("  \x1b[2mDisable with: lean-ctx setup --no-auto-approve\x1b[0m");
316    }
317
318    // Summary
319    println!();
320    println!(
321        "  \x1b[1;32m✓ Setup complete!\x1b[0m  \x1b[1m{}\x1b[0m configured, \x1b[2m{} already set, {} skipped\x1b[0m",
322        newly_configured.len(),
323        already_configured.len(),
324        not_installed.len()
325    );
326
327    if !errors.is_empty() {
328        println!(
329            "  \x1b[33m⚠ {} error{}: {}\x1b[0m",
330            errors.len(),
331            if errors.len() == 1 { "" } else { "s" },
332            errors.join(", ")
333        );
334    }
335
336    // Next steps
337    let shell = std::env::var("SHELL").unwrap_or_default();
338    let source_cmd = if shell.contains("zsh") {
339        "source ~/.zshrc"
340    } else if shell.contains("fish") {
341        "source ~/.config/fish/config.fish"
342    } else if shell.contains("bash") {
343        "source ~/.bashrc"
344    } else {
345        "Restart your shell"
346    };
347
348    let dim = "\x1b[2m";
349    let bold = "\x1b[1m";
350    let cyan = "\x1b[36m";
351    let yellow = "\x1b[33m";
352    let rst = "\x1b[0m";
353
354    println!();
355    println!("  {bold}Next steps:{rst}");
356    println!();
357    println!("  {cyan}1.{rst} Reload your shell:");
358    println!("     {bold}{source_cmd}{rst}");
359    println!();
360
361    let mut tools_to_restart: Vec<String> = newly_configured
362        .iter()
363        .map(std::string::ToString::to_string)
364        .collect();
365    for name in rules_result
366        .injected
367        .iter()
368        .chain(rules_result.updated.iter())
369    {
370        if !tools_to_restart.iter().any(|t| t == name) {
371            tools_to_restart.push(name.clone());
372        }
373    }
374
375    if !tools_to_restart.is_empty() {
376        println!("  {cyan}2.{rst} {yellow}{bold}Restart your IDE / AI tool:{rst}");
377        println!("     {bold}{}{rst}", tools_to_restart.join(", "));
378        println!(
379            "     {dim}Changes take effect after a full restart (MCP may be enabled or disabled depending on mode).{rst}"
380        );
381        println!("     {dim}Close and re-open the application completely.{rst}");
382    } else if !already_configured.is_empty() {
383        println!(
384            "  {cyan}2.{rst} {dim}Your tools are already configured — no restart needed.{rst}"
385        );
386    }
387
388    println!();
389    println!(
390        "  {dim}After restart, lean-ctx will automatically optimize every AI interaction.{rst}"
391    );
392    println!("  {dim}Verify with:{rst} {bold}lean-ctx gain{rst}");
393
394    // Logo + commands
395    println!();
396    terminal_ui::print_logo_animated();
397    terminal_ui::print_command_box();
398}
399
400#[derive(Debug, Clone, Copy, Default)]
401pub struct SetupOptions {
402    pub non_interactive: bool,
403    pub yes: bool,
404    pub fix: bool,
405    pub json: bool,
406    pub no_auto_approve: bool,
407}
408
409pub fn run_setup_with_options(opts: SetupOptions) -> Result<SetupReport, String> {
410    let _quiet_guard = opts.json.then(|| EnvVarGuard::set("LEAN_CTX_QUIET", "1"));
411    let started_at = Utc::now();
412    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
413    let binary = resolve_portable_binary();
414    let home_str = home.to_string_lossy().to_string();
415
416    let mut steps: Vec<SetupStepReport> = Vec::new();
417
418    // Step: Shell Hook
419    let mut shell_step = SetupStepReport {
420        name: "shell_hook".to_string(),
421        ok: true,
422        items: Vec::new(),
423        warnings: Vec::new(),
424        errors: Vec::new(),
425    };
426    if !opts.non_interactive || opts.yes {
427        if opts.json {
428            crate::cli::cmd_init_quiet(&["--global".to_string()]);
429        } else {
430            crate::cli::cmd_init(&["--global".to_string()]);
431        }
432        crate::shell_hook::install_all(opts.json);
433        #[cfg(not(windows))]
434        {
435            let hook_content = crate::cli::generate_hook_posix(&binary);
436            if crate::shell::is_container() {
437                crate::cli::write_env_sh_for_containers(&hook_content);
438                shell_step.items.push(SetupItem {
439                    name: "env_sh".to_string(),
440                    status: "created".to_string(),
441                    path: Some("~/.lean-ctx/env.sh".to_string()),
442                    note: Some("Docker/CI helper (BASH_ENV / CLAUDE_ENV_FILE)".to_string()),
443                });
444            } else {
445                shell_step.items.push(SetupItem {
446                    name: "env_sh".to_string(),
447                    status: "skipped".to_string(),
448                    path: None,
449                    note: Some("not a container environment".to_string()),
450                });
451            }
452        }
453        shell_step.items.push(SetupItem {
454            name: "init --global".to_string(),
455            status: "ran".to_string(),
456            path: None,
457            note: None,
458        });
459        shell_step.items.push(SetupItem {
460            name: "universal_shell_hook".to_string(),
461            status: "installed".to_string(),
462            path: None,
463            note: Some("~/.zshenv, ~/.bashenv, agent aliases".to_string()),
464        });
465    } else {
466        shell_step
467            .warnings
468            .push("non_interactive_without_yes: shell hook not installed (use --yes)".to_string());
469        shell_step.ok = false;
470        shell_step.items.push(SetupItem {
471            name: "init --global".to_string(),
472            status: "skipped".to_string(),
473            path: None,
474            note: Some("requires --yes in --non-interactive mode".to_string()),
475        });
476    }
477    steps.push(shell_step);
478
479    // Step: Daemon (optional acceleration for CLI routing)
480    let mut daemon_step = SetupStepReport {
481        name: "daemon".to_string(),
482        ok: true,
483        items: Vec::new(),
484        warnings: Vec::new(),
485        errors: Vec::new(),
486    };
487    {
488        let was_running = crate::daemon::is_daemon_running();
489        if was_running {
490            let _ = crate::daemon::stop_daemon();
491            std::thread::sleep(std::time::Duration::from_millis(500));
492        }
493        match crate::daemon::start_daemon(&[]) {
494            Ok(()) => {
495                let action = if was_running { "restarted" } else { "started" };
496                daemon_step.items.push(SetupItem {
497                    name: "serve --daemon".to_string(),
498                    status: action.to_string(),
499                    path: Some(crate::daemon::daemon_addr().display()),
500                    note: Some("CLI commands can route via IPC when running".to_string()),
501                });
502            }
503            Err(e) => {
504                daemon_step
505                    .warnings
506                    .push(format!("daemon start failed (non-fatal): {e}"));
507                daemon_step.items.push(SetupItem {
508                    name: "serve --daemon".to_string(),
509                    status: "skipped".to_string(),
510                    path: None,
511                    note: Some(format!("optional — {e}")),
512                });
513            }
514        }
515    }
516    steps.push(daemon_step);
517
518    // Step: Editor MCP config
519    let mut editor_step = SetupStepReport {
520        name: "editors".to_string(),
521        ok: true,
522        items: Vec::new(),
523        warnings: Vec::new(),
524        errors: Vec::new(),
525    };
526
527    let targets = crate::core::editor_registry::build_targets(&home);
528    for target in &targets {
529        let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
530        if !target.detect_path.exists() {
531            editor_step.items.push(SetupItem {
532                name: target.name.to_string(),
533                status: "not_detected".to_string(),
534                path: Some(short_path),
535                note: None,
536            });
537            continue;
538        }
539
540        let mode = if target.agent_key.is_empty() {
541            HookMode::Mcp
542        } else {
543            recommend_hook_mode(&target.agent_key)
544        };
545
546        let res = crate::core::editor_registry::write_config_with_options(
547            target,
548            &binary,
549            WriteOptions {
550                overwrite_invalid: opts.fix,
551            },
552        );
553        match res {
554            Ok(w) => {
555                let note_parts: Vec<String> = [Some(format!("mode={mode}")), w.note]
556                    .into_iter()
557                    .flatten()
558                    .collect();
559                editor_step.items.push(SetupItem {
560                    name: target.name.to_string(),
561                    status: match w.action {
562                        WriteAction::Created => "created".to_string(),
563                        WriteAction::Updated => "updated".to_string(),
564                        WriteAction::Already => "already".to_string(),
565                    },
566                    path: Some(short_path),
567                    note: Some(note_parts.join("; ")),
568                });
569            }
570            Err(e) => {
571                editor_step.ok = false;
572                editor_step.items.push(SetupItem {
573                    name: target.name.to_string(),
574                    status: "error".to_string(),
575                    path: Some(short_path),
576                    note: Some(e),
577                });
578            }
579        }
580    }
581    steps.push(editor_step);
582
583    // Step: Agent rules
584    let mut rules_step = SetupStepReport {
585        name: "agent_rules".to_string(),
586        ok: true,
587        items: Vec::new(),
588        warnings: Vec::new(),
589        errors: Vec::new(),
590    };
591    let rules_result = crate::rules_inject::inject_all_rules(&home);
592    for n in rules_result.injected {
593        rules_step.items.push(SetupItem {
594            name: n,
595            status: "injected".to_string(),
596            path: None,
597            note: None,
598        });
599    }
600    for n in rules_result.updated {
601        rules_step.items.push(SetupItem {
602            name: n,
603            status: "updated".to_string(),
604            path: None,
605            note: None,
606        });
607    }
608    for n in rules_result.already {
609        rules_step.items.push(SetupItem {
610            name: n,
611            status: "already".to_string(),
612            path: None,
613            note: None,
614        });
615    }
616    for e in rules_result.errors {
617        rules_step.ok = false;
618        rules_step.errors.push(e);
619    }
620    steps.push(rules_step);
621
622    // Step: Skill files
623    let mut skill_step = SetupStepReport {
624        name: "skill_files".to_string(),
625        ok: true,
626        items: Vec::new(),
627        warnings: Vec::new(),
628        errors: Vec::new(),
629    };
630    let skill_results = crate::rules_inject::install_all_skills(&home);
631    for (name, is_new) in &skill_results {
632        skill_step.items.push(SetupItem {
633            name: name.clone(),
634            status: if *is_new { "installed" } else { "already" }.to_string(),
635            path: None,
636            note: Some("SKILL.md".to_string()),
637        });
638    }
639    if !skill_step.items.is_empty() {
640        steps.push(skill_step);
641    }
642
643    // Step: Agent-specific hooks (all detected agents)
644    let mut hooks_step = SetupStepReport {
645        name: "agent_hooks".to_string(),
646        ok: true,
647        items: Vec::new(),
648        warnings: Vec::new(),
649        errors: Vec::new(),
650    };
651    for target in &targets {
652        if !target.detect_path.exists() || target.agent_key.is_empty() {
653            continue;
654        }
655        let mode = recommend_hook_mode(&target.agent_key);
656        crate::hooks::install_agent_hook_with_mode(&target.agent_key, true, mode);
657        hooks_step.items.push(SetupItem {
658            name: format!("{} hooks", target.name),
659            status: "installed".to_string(),
660            path: Some(target.detect_path.to_string_lossy().to_string()),
661            note: Some(format!(
662                "mode={mode}; merge-based install/repair (preserves other hooks/plugins)"
663            )),
664        });
665    }
666    if !hooks_step.items.is_empty() {
667        steps.push(hooks_step);
668    }
669
670    // Step: Proxy env vars
671    let mut proxy_step = SetupStepReport {
672        name: "proxy_env".to_string(),
673        ok: true,
674        items: Vec::new(),
675        warnings: Vec::new(),
676        errors: Vec::new(),
677    };
678    crate::proxy_setup::install_proxy_env(&home, crate::proxy_setup::default_port(), opts.json);
679    proxy_step.items.push(SetupItem {
680        name: "proxy_env".to_string(),
681        status: "configured".to_string(),
682        path: None,
683        note: Some("ANTHROPIC_BASE_URL, OPENAI_BASE_URL, GEMINI_API_BASE_URL".to_string()),
684    });
685    steps.push(proxy_step);
686
687    // Step: Environment / doctor (compact)
688    let mut env_step = SetupStepReport {
689        name: "doctor_compact".to_string(),
690        ok: true,
691        items: Vec::new(),
692        warnings: Vec::new(),
693        errors: Vec::new(),
694    };
695    let (passed, total) = crate::doctor::compact_score();
696    env_step.items.push(SetupItem {
697        name: "doctor".to_string(),
698        status: format!("{passed}/{total}"),
699        path: None,
700        note: None,
701    });
702    if passed != total {
703        env_step.warnings.push(format!(
704            "doctor compact not fully passing: {passed}/{total}"
705        ));
706    }
707    steps.push(env_step);
708
709    // Auto-build property graph if inside any recognized project
710    if let Ok(cwd) = std::env::current_dir() {
711        let is_project = cwd.join(".git").exists()
712            || cwd.join("Cargo.toml").exists()
713            || cwd.join("package.json").exists()
714            || cwd.join("go.mod").exists();
715        if is_project {
716            spawn_index_build_background(&cwd);
717        }
718    }
719
720    let finished_at = Utc::now();
721    let success = steps.iter().all(|s| s.ok);
722    let report = SetupReport {
723        schema_version: 1,
724        started_at,
725        finished_at,
726        success,
727        platform: PlatformInfo {
728            os: std::env::consts::OS.to_string(),
729            arch: std::env::consts::ARCH.to_string(),
730        },
731        steps,
732        warnings: Vec::new(),
733        errors: Vec::new(),
734    };
735
736    let path = SetupReport::default_path()?;
737    let mut content =
738        serde_json::to_string_pretty(&report).map_err(|e| format!("serialize report: {e}"))?;
739    content.push('\n');
740    crate::config_io::write_atomic(&path, &content)?;
741
742    Ok(report)
743}
744
745fn spawn_index_build_background(root: &std::path::Path) {
746    let root_str = crate::core::graph_index::normalize_project_root(&root.to_string_lossy());
747    if !crate::core::graph_index::is_safe_scan_root_public(&root_str) {
748        tracing::info!("[setup: skipping background graph build for unsafe root {root_str}]");
749        return;
750    }
751
752    let binary = std::env::current_exe().map_or_else(
753        |_| resolve_portable_binary(),
754        |p| p.to_string_lossy().to_string(),
755    );
756
757    #[cfg(unix)]
758    {
759        let mut cmd = std::process::Command::new("nice");
760        cmd.args(["-n", "19"]);
761        if which_ionice_available() {
762            cmd.arg("ionice").args(["-c", "3"]);
763        }
764        cmd.arg(&binary)
765            .args(["index", "build-graph", "--root"])
766            .arg(root)
767            .stdout(std::process::Stdio::null())
768            .stderr(std::process::Stdio::null())
769            .stdin(std::process::Stdio::null());
770        let _ = cmd.spawn();
771    }
772
773    #[cfg(not(unix))]
774    {
775        let _ = std::process::Command::new(&binary)
776            .args(["index", "build-graph", "--root"])
777            .arg(root)
778            .stdout(std::process::Stdio::null())
779            .stderr(std::process::Stdio::null())
780            .stdin(std::process::Stdio::null())
781            .spawn();
782    }
783}
784
785#[cfg(unix)]
786fn which_ionice_available() -> bool {
787    std::process::Command::new("ionice")
788        .arg("--version")
789        .stdout(std::process::Stdio::null())
790        .stderr(std::process::Stdio::null())
791        .status()
792        .is_ok()
793}
794
795pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
796    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
797    let binary = resolve_portable_binary();
798
799    let targets = agent_mcp_targets(agent, &home)?;
800
801    for t in &targets {
802        crate::core::editor_registry::write_config_with_options(
803            t,
804            &binary,
805            WriteOptions {
806                overwrite_invalid: true,
807            },
808        )?;
809    }
810
811    if agent == "kiro" {
812        install_kiro_steering(&home);
813    }
814
815    Ok(())
816}
817
818fn agent_mcp_targets(agent: &str, home: &std::path::Path) -> Result<Vec<EditorTarget>, String> {
819    let mut targets = Vec::<EditorTarget>::new();
820
821    let push = |targets: &mut Vec<EditorTarget>,
822                name: &'static str,
823                config_path: PathBuf,
824                config_type: ConfigType| {
825        targets.push(EditorTarget {
826            name,
827            agent_key: agent.to_string(),
828            detect_path: PathBuf::from("/nonexistent"), // not used in direct agent config
829            config_path,
830            config_type,
831        });
832    };
833
834    let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
835
836    match agent {
837        "cursor" => push(
838            &mut targets,
839            "Cursor",
840            home.join(".cursor/mcp.json"),
841            ConfigType::McpJson,
842        ),
843        "claude" | "claude-code" => push(
844            &mut targets,
845            "Claude Code",
846            crate::core::editor_registry::claude_mcp_json_path(home),
847            ConfigType::McpJson,
848        ),
849        "windsurf" => push(
850            &mut targets,
851            "Windsurf",
852            home.join(".codeium/windsurf/mcp_config.json"),
853            ConfigType::McpJson,
854        ),
855        "codex" => {
856            let codex_dir =
857                crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
858            push(
859                &mut targets,
860                "Codex CLI",
861                codex_dir.join("config.toml"),
862                ConfigType::Codex,
863            );
864        }
865        "gemini" => {
866            push(
867                &mut targets,
868                "Gemini CLI",
869                home.join(".gemini/settings.json"),
870                ConfigType::GeminiSettings,
871            );
872            push(
873                &mut targets,
874                "Antigravity",
875                home.join(".gemini/antigravity/mcp_config.json"),
876                ConfigType::McpJson,
877            );
878        }
879        "antigravity" => push(
880            &mut targets,
881            "Antigravity",
882            home.join(".gemini/antigravity/mcp_config.json"),
883            ConfigType::McpJson,
884        ),
885        "copilot" => push(
886            &mut targets,
887            "VS Code / Copilot",
888            crate::core::editor_registry::vscode_mcp_path(),
889            ConfigType::VsCodeMcp,
890        ),
891        "crush" => push(
892            &mut targets,
893            "Crush",
894            home.join(".config/crush/crush.json"),
895            ConfigType::Crush,
896        ),
897        "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
898        "qoder" => {
899            for path in crate::core::editor_registry::qoder_all_mcp_paths(home) {
900                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
901            }
902        }
903        "qoderwork" => push(
904            &mut targets,
905            "QoderWork",
906            crate::core::editor_registry::qoderwork_mcp_path(home),
907            ConfigType::McpJson,
908        ),
909        "cline" => push(
910            &mut targets,
911            "Cline",
912            crate::core::editor_registry::cline_mcp_path(),
913            ConfigType::McpJson,
914        ),
915        "roo" => push(
916            &mut targets,
917            "Roo Code",
918            crate::core::editor_registry::roo_mcp_path(),
919            ConfigType::McpJson,
920        ),
921        "kiro" => push(
922            &mut targets,
923            "AWS Kiro",
924            home.join(".kiro/settings/mcp.json"),
925            ConfigType::McpJson,
926        ),
927        "verdent" => push(
928            &mut targets,
929            "Verdent",
930            home.join(".verdent/mcp.json"),
931            ConfigType::McpJson,
932        ),
933        "jetbrains" | "amp" => {
934            // Handled by dedicated install hooks (servers[] array / amp.mcpServers)
935        }
936        "qwen" => push(
937            &mut targets,
938            "Qwen Code",
939            home.join(".qwen/settings.json"),
940            ConfigType::McpJson,
941        ),
942        "trae" => push(
943            &mut targets,
944            "Trae",
945            home.join(".trae/mcp.json"),
946            ConfigType::McpJson,
947        ),
948        "amazonq" => push(
949            &mut targets,
950            "Amazon Q Developer",
951            home.join(".aws/amazonq/default.json"),
952            ConfigType::McpJson,
953        ),
954        "opencode" => {
955            #[cfg(windows)]
956            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
957                std::path::PathBuf::from(appdata)
958                    .join("opencode")
959                    .join("opencode.json")
960            } else {
961                home.join(".config/opencode/opencode.json")
962            };
963            #[cfg(not(windows))]
964            let opencode_path = home.join(".config/opencode/opencode.json");
965            push(
966                &mut targets,
967                "OpenCode",
968                opencode_path,
969                ConfigType::OpenCode,
970            );
971        }
972        "hermes" => push(
973            &mut targets,
974            "Hermes Agent",
975            home.join(".hermes/config.yaml"),
976            ConfigType::HermesYaml,
977        ),
978        "vscode" => push(
979            &mut targets,
980            "VS Code",
981            crate::core::editor_registry::vscode_mcp_path(),
982            ConfigType::VsCodeMcp,
983        ),
984        "zed" => push(
985            &mut targets,
986            "Zed",
987            crate::core::editor_registry::zed_settings_path(home),
988            ConfigType::Zed,
989        ),
990        "aider" => push(
991            &mut targets,
992            "Aider",
993            home.join(".aider/mcp.json"),
994            ConfigType::McpJson,
995        ),
996        "continue" => push(
997            &mut targets,
998            "Continue",
999            home.join(".continue/mcp.json"),
1000            ConfigType::McpJson,
1001        ),
1002        "neovim" => push(
1003            &mut targets,
1004            "Neovim (mcphub.nvim)",
1005            home.join(".config/mcphub/servers.json"),
1006            ConfigType::McpJson,
1007        ),
1008        "emacs" => push(
1009            &mut targets,
1010            "Emacs (mcp.el)",
1011            home.join(".emacs.d/mcp.json"),
1012            ConfigType::McpJson,
1013        ),
1014        "sublime" => push(
1015            &mut targets,
1016            "Sublime Text",
1017            home.join(".config/sublime-text/mcp.json"),
1018            ConfigType::McpJson,
1019        ),
1020        _ => {
1021            return Err(format!("Unknown agent '{agent}'"));
1022        }
1023    }
1024
1025    Ok(targets)
1026}
1027
1028pub fn disable_agent_mcp(agent: &str, overwrite_invalid: bool) -> Result<(), String> {
1029    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
1030
1031    let mut targets = Vec::<EditorTarget>::new();
1032
1033    let push = |targets: &mut Vec<EditorTarget>,
1034                name: &'static str,
1035                config_path: PathBuf,
1036                config_type: ConfigType| {
1037        targets.push(EditorTarget {
1038            name,
1039            agent_key: agent.to_string(),
1040            detect_path: PathBuf::from("/nonexistent"),
1041            config_path,
1042            config_type,
1043        });
1044    };
1045
1046    let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
1047
1048    match agent {
1049        "cursor" => push(
1050            &mut targets,
1051            "Cursor",
1052            home.join(".cursor/mcp.json"),
1053            ConfigType::McpJson,
1054        ),
1055        "claude" | "claude-code" => push(
1056            &mut targets,
1057            "Claude Code",
1058            crate::core::editor_registry::claude_mcp_json_path(&home),
1059            ConfigType::McpJson,
1060        ),
1061        "windsurf" => push(
1062            &mut targets,
1063            "Windsurf",
1064            home.join(".codeium/windsurf/mcp_config.json"),
1065            ConfigType::McpJson,
1066        ),
1067        "codex" => {
1068            let codex_dir =
1069                crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
1070            push(
1071                &mut targets,
1072                "Codex CLI",
1073                codex_dir.join("config.toml"),
1074                ConfigType::Codex,
1075            );
1076        }
1077        "gemini" => {
1078            push(
1079                &mut targets,
1080                "Gemini CLI",
1081                home.join(".gemini/settings.json"),
1082                ConfigType::GeminiSettings,
1083            );
1084            push(
1085                &mut targets,
1086                "Antigravity",
1087                home.join(".gemini/antigravity/mcp_config.json"),
1088                ConfigType::McpJson,
1089            );
1090        }
1091        "antigravity" => push(
1092            &mut targets,
1093            "Antigravity",
1094            home.join(".gemini/antigravity/mcp_config.json"),
1095            ConfigType::McpJson,
1096        ),
1097        "copilot" => push(
1098            &mut targets,
1099            "VS Code / Copilot",
1100            crate::core::editor_registry::vscode_mcp_path(),
1101            ConfigType::VsCodeMcp,
1102        ),
1103        "crush" => push(
1104            &mut targets,
1105            "Crush",
1106            home.join(".config/crush/crush.json"),
1107            ConfigType::Crush,
1108        ),
1109        "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
1110        "qoder" => {
1111            for path in crate::core::editor_registry::qoder_all_mcp_paths(&home) {
1112                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
1113            }
1114        }
1115        "qoderwork" => push(
1116            &mut targets,
1117            "QoderWork",
1118            crate::core::editor_registry::qoderwork_mcp_path(&home),
1119            ConfigType::McpJson,
1120        ),
1121        "cline" => push(
1122            &mut targets,
1123            "Cline",
1124            crate::core::editor_registry::cline_mcp_path(),
1125            ConfigType::McpJson,
1126        ),
1127        "roo" => push(
1128            &mut targets,
1129            "Roo Code",
1130            crate::core::editor_registry::roo_mcp_path(),
1131            ConfigType::McpJson,
1132        ),
1133        "kiro" => push(
1134            &mut targets,
1135            "AWS Kiro",
1136            home.join(".kiro/settings/mcp.json"),
1137            ConfigType::McpJson,
1138        ),
1139        "verdent" => push(
1140            &mut targets,
1141            "Verdent",
1142            home.join(".verdent/mcp.json"),
1143            ConfigType::McpJson,
1144        ),
1145        "jetbrains" | "amp" => {
1146            // Not supported for disable via this helper.
1147        }
1148        "qwen" => push(
1149            &mut targets,
1150            "Qwen Code",
1151            home.join(".qwen/settings.json"),
1152            ConfigType::McpJson,
1153        ),
1154        "trae" => push(
1155            &mut targets,
1156            "Trae",
1157            home.join(".trae/mcp.json"),
1158            ConfigType::McpJson,
1159        ),
1160        "amazonq" => push(
1161            &mut targets,
1162            "Amazon Q Developer",
1163            home.join(".aws/amazonq/default.json"),
1164            ConfigType::McpJson,
1165        ),
1166        "opencode" => {
1167            #[cfg(windows)]
1168            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
1169                std::path::PathBuf::from(appdata)
1170                    .join("opencode")
1171                    .join("opencode.json")
1172            } else {
1173                home.join(".config/opencode/opencode.json")
1174            };
1175            #[cfg(not(windows))]
1176            let opencode_path = home.join(".config/opencode/opencode.json");
1177            push(
1178                &mut targets,
1179                "OpenCode",
1180                opencode_path,
1181                ConfigType::OpenCode,
1182            );
1183        }
1184        "hermes" => push(
1185            &mut targets,
1186            "Hermes Agent",
1187            home.join(".hermes/config.yaml"),
1188            ConfigType::HermesYaml,
1189        ),
1190        "vscode" => push(
1191            &mut targets,
1192            "VS Code",
1193            crate::core::editor_registry::vscode_mcp_path(),
1194            ConfigType::VsCodeMcp,
1195        ),
1196        "zed" => push(
1197            &mut targets,
1198            "Zed",
1199            crate::core::editor_registry::zed_settings_path(&home),
1200            ConfigType::Zed,
1201        ),
1202        "aider" => push(
1203            &mut targets,
1204            "Aider",
1205            home.join(".aider/mcp.json"),
1206            ConfigType::McpJson,
1207        ),
1208        "continue" => push(
1209            &mut targets,
1210            "Continue",
1211            home.join(".continue/mcp.json"),
1212            ConfigType::McpJson,
1213        ),
1214        "neovim" => push(
1215            &mut targets,
1216            "Neovim (mcphub.nvim)",
1217            home.join(".config/mcphub/servers.json"),
1218            ConfigType::McpJson,
1219        ),
1220        "emacs" => push(
1221            &mut targets,
1222            "Emacs (mcp.el)",
1223            home.join(".emacs.d/mcp.json"),
1224            ConfigType::McpJson,
1225        ),
1226        "sublime" => push(
1227            &mut targets,
1228            "Sublime Text",
1229            home.join(".config/sublime-text/mcp.json"),
1230            ConfigType::McpJson,
1231        ),
1232        _ => {
1233            return Err(format!("Unknown agent '{agent}'"));
1234        }
1235    }
1236
1237    for t in &targets {
1238        crate::core::editor_registry::remove_lean_ctx_server(
1239            t,
1240            WriteOptions { overwrite_invalid },
1241        )?;
1242    }
1243
1244    Ok(())
1245}
1246
1247pub fn install_skill_files(home: &std::path::Path) -> Vec<(String, bool)> {
1248    crate::rules_inject::install_all_skills(home)
1249}
1250
1251fn install_kiro_steering(home: &std::path::Path) {
1252    let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
1253    let steering_dir = cwd.join(".kiro").join("steering");
1254    let steering_file = steering_dir.join("lean-ctx.md");
1255
1256    if steering_file.exists()
1257        && std::fs::read_to_string(&steering_file)
1258            .unwrap_or_default()
1259            .contains("lean-ctx")
1260    {
1261        println!("  Kiro steering file already exists at .kiro/steering/lean-ctx.md");
1262        return;
1263    }
1264
1265    let _ = std::fs::create_dir_all(&steering_dir);
1266    let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
1267    println!("  \x1b[32m✓\x1b[0m Created .kiro/steering/lean-ctx.md (Kiro will now prefer lean-ctx tools)");
1268}
1269
1270fn shorten_path(path: &str, home: &str) -> String {
1271    if let Some(stripped) = path.strip_prefix(home) {
1272        format!("~{stripped}")
1273    } else {
1274        path.to_string()
1275    }
1276}
1277
1278fn upsert_toml_key(content: &mut String, key: &str, value: &str) {
1279    let pattern = format!("{key} = ");
1280    if let Some(start) = content.find(&pattern) {
1281        let line_end = content[start..]
1282            .find('\n')
1283            .map_or(content.len(), |p| start + p);
1284        content.replace_range(start..line_end, &format!("{key} = \"{value}\""));
1285    } else {
1286        if !content.is_empty() && !content.ends_with('\n') {
1287            content.push('\n');
1288        }
1289        content.push_str(&format!("{key} = \"{value}\"\n"));
1290    }
1291}
1292
1293fn remove_toml_key(content: &mut String, key: &str) {
1294    let pattern = format!("{key} = ");
1295    if let Some(start) = content.find(&pattern) {
1296        let line_end = content[start..]
1297            .find('\n')
1298            .map_or(content.len(), |p| start + p + 1);
1299        content.replace_range(start..line_end, "");
1300    }
1301}
1302
1303fn configure_premium_features(home: &std::path::Path) {
1304    use crate::terminal_ui;
1305    use std::io::Write;
1306
1307    let config_dir = home.join(".lean-ctx");
1308    let _ = std::fs::create_dir_all(&config_dir);
1309    let config_path = config_dir.join("config.toml");
1310    let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
1311
1312    let dim = "\x1b[2m";
1313    let bold = "\x1b[1m";
1314    let cyan = "\x1b[36m";
1315    let rst = "\x1b[0m";
1316
1317    // Unified Compression Level (replaces terse_agent + output_density)
1318    println!("\n  {bold}Compression Level{rst} {dim}(controls all token optimization layers){rst}");
1319    println!("  {dim}Applies to tool output, agent prompts, and protocol mode.{rst}");
1320    println!();
1321    println!("  {cyan}off{rst}      — No compression (full verbose output)");
1322    println!("  {cyan}lite{rst}     — Light: concise output, basic terse filtering {dim}(~25% savings){rst}");
1323    println!("  {cyan}standard{rst} — Dense output + compact protocol + pattern-aware {dim}(~45% savings){rst}");
1324    println!("  {cyan}max{rst}      — Expert mode: TDD protocol, all layers active {dim}(~65% savings){rst}");
1325    println!();
1326    print!("  Compression level? {bold}[off/lite/standard/max]{rst} {dim}(default: off){rst} ");
1327    std::io::stdout().flush().ok();
1328
1329    let mut level_input = String::new();
1330    let level = if std::io::stdin().read_line(&mut level_input).is_ok() {
1331        match level_input.trim().to_lowercase().as_str() {
1332            "lite" => "lite",
1333            "standard" | "std" => "standard",
1334            "max" => "max",
1335            _ => "off",
1336        }
1337    } else {
1338        "off"
1339    };
1340
1341    let effective_level = if level != "off" {
1342        upsert_toml_key(&mut config_content, "compression_level", level);
1343        remove_toml_key(&mut config_content, "terse_agent");
1344        remove_toml_key(&mut config_content, "output_density");
1345        terminal_ui::print_status_ok(&format!("Compression: {level}"));
1346        crate::core::config::CompressionLevel::from_str_label(level)
1347    } else if config_content.contains("compression_level") {
1348        upsert_toml_key(&mut config_content, "compression_level", "off");
1349        terminal_ui::print_status_ok("Compression: off");
1350        Some(crate::core::config::CompressionLevel::Off)
1351    } else {
1352        terminal_ui::print_status_skip(
1353            "Compression: off (change later with: lean-ctx compression <level>)",
1354        );
1355        Some(crate::core::config::CompressionLevel::Off)
1356    };
1357
1358    if let Some(lvl) = effective_level {
1359        let n = crate::core::terse::rules_inject::inject(&lvl);
1360        if n > 0 {
1361            terminal_ui::print_status_ok(&format!(
1362                "Updated {n} rules file(s) with compression prompt"
1363            ));
1364        }
1365    }
1366
1367    // Tool Result Archive (unchanged)
1368    println!(
1369        "\n  {bold}Tool Result Archive{rst} {dim}(zero-loss: large outputs archived, retrievable via ctx_expand){rst}"
1370    );
1371    print!("  Enable auto-archive? {bold}[Y/n]{rst} ");
1372    std::io::stdout().flush().ok();
1373
1374    let mut archive_input = String::new();
1375    let archive_on = if std::io::stdin().read_line(&mut archive_input).is_ok() {
1376        let a = archive_input.trim().to_lowercase();
1377        a.is_empty() || a == "y" || a == "yes"
1378    } else {
1379        true
1380    };
1381
1382    if archive_on && !config_content.contains("[archive]") {
1383        if !config_content.is_empty() && !config_content.ends_with('\n') {
1384            config_content.push('\n');
1385        }
1386        config_content.push_str("\n[archive]\nenabled = true\n");
1387        terminal_ui::print_status_ok("Tool Result Archive: enabled");
1388    } else if !archive_on {
1389        terminal_ui::print_status_skip("Archive: off (enable later in config.toml)");
1390    }
1391
1392    let _ = std::fs::write(&config_path, config_content);
1393}
1394
1395#[cfg(all(test, target_os = "macos"))]
1396mod tests {
1397    use super::*;
1398
1399    #[test]
1400    #[cfg(target_os = "macos")]
1401    fn qoder_agent_targets_include_all_macos_mcp_locations() {
1402        let home = std::path::Path::new("/Users/tester");
1403        let targets = agent_mcp_targets("qoder", home).unwrap();
1404        let paths: Vec<_> = targets.iter().map(|t| t.config_path.as_path()).collect();
1405
1406        assert_eq!(
1407            paths,
1408            vec![
1409                home.join(".qoder/mcp.json").as_path(),
1410                home.join("Library/Application Support/Qoder/User/mcp.json")
1411                    .as_path(),
1412                home.join("Library/Application Support/Qoder/SharedClientCache/mcp.json")
1413                    .as_path(),
1414            ]
1415        );
1416        assert!(targets
1417            .iter()
1418            .all(|t| t.config_type == ConfigType::QoderSettings));
1419    }
1420}