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