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