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, 11, "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, 11, "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, 11, "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    configure_plan_mode_settings(&newly_configured, &already_configured);
162
163    // Step 4: Agent rules injection
164    terminal_ui::print_step_header(4, 11, "Agent Rules");
165    let rules_result = crate::rules_inject::inject_all_rules(&home);
166    for name in &rules_result.injected {
167        terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mrules injected\x1b[0m"));
168    }
169    for name in &rules_result.updated {
170        terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mrules updated\x1b[0m"));
171    }
172    for name in &rules_result.already {
173        terminal_ui::print_status_ok(&format!("{name:<20} \x1b[2mrules up-to-date\x1b[0m"));
174    }
175    for err in &rules_result.errors {
176        terminal_ui::print_status_warn(err);
177    }
178    if rules_result.injected.is_empty()
179        && rules_result.updated.is_empty()
180        && rules_result.already.is_empty()
181        && rules_result.errors.is_empty()
182    {
183        terminal_ui::print_status_skip("No agent rules needed");
184    }
185
186    // Agent hooks (mode-aware)
187    for target in &targets {
188        if !target.detect_path.exists() || target.agent_key.is_empty() {
189            continue;
190        }
191        let mode = recommend_hook_mode(&target.agent_key);
192        crate::hooks::install_agent_hook_with_mode(&target.agent_key, true, mode);
193    }
194
195    // Step 5: API Proxy (opt-in)
196    terminal_ui::print_step_header(5, 11, "API Proxy (optional)");
197    {
198        let mut cfg = crate::core::config::Config::load();
199        let proxy_port = crate::proxy_setup::default_port();
200
201        match cfg.proxy_enabled {
202            Some(true) => {
203                crate::proxy_autostart::install(proxy_port, false);
204                std::thread::sleep(std::time::Duration::from_millis(500));
205                crate::proxy_setup::install_proxy_env(&home, proxy_port, false);
206                terminal_ui::print_status_ok("Proxy active (opted in)");
207            }
208            Some(false) => {
209                terminal_ui::print_status_skip(
210                    "Proxy disabled (run `lean-ctx proxy enable` to change)",
211                );
212            }
213            None => {
214                println!(
215                    "  \x1b[2mThe API proxy routes LLM requests through lean-ctx for additional\x1b[0m"
216                );
217                println!(
218                    "  \x1b[2mtool-result compression and precise token analytics in the dashboard.\x1b[0m"
219                );
220                println!();
221                println!(
222                    "  \x1b[2mWithout it: MCP tools, shell hooks, gain tracking, and memory\x1b[0m"
223                );
224                println!(
225                    "  \x1b[2mall work normally. The proxy adds ~5-15% extra savings on top.\x1b[0m"
226                );
227                println!();
228                print!("  Enable the API proxy? [y/N] ");
229                let _ = std::io::Write::flush(&mut std::io::stdout());
230                let mut input = String::new();
231                let _ = std::io::stdin().read_line(&mut input);
232                let answer = matches!(input.trim().to_lowercase().as_str(), "y" | "yes");
233                cfg.proxy_enabled = Some(answer);
234                let _ = cfg.save();
235                if answer {
236                    crate::proxy_autostart::install(proxy_port, false);
237                    std::thread::sleep(std::time::Duration::from_millis(500));
238                    crate::proxy_setup::install_proxy_env(&home, proxy_port, false);
239                    terminal_ui::print_status_new("Proxy enabled");
240                } else {
241                    terminal_ui::print_status_skip(
242                        "Proxy skipped (run `lean-ctx proxy enable` anytime)",
243                    );
244                }
245            }
246        }
247    }
248
249    // Step 6: SKILL.md installation
250    terminal_ui::print_step_header(6, 11, "Skill Files");
251    let skill_result = install_skill_files(&home);
252    for (name, installed) in &skill_result {
253        if *installed {
254            terminal_ui::print_status_new(&format!("{name:<20} \x1b[2mSKILL.md installed\x1b[0m"));
255        } else {
256            terminal_ui::print_status_ok(&format!("{name:<20} \x1b[2mSKILL.md up-to-date\x1b[0m"));
257        }
258    }
259    if skill_result.is_empty() {
260        terminal_ui::print_status_skip("No skill directories to install");
261    }
262
263    // Step 7: Data directory + diagnostics
264    terminal_ui::print_step_header(7, 11, "Environment Check");
265    let lean_dir = crate::core::data_dir::lean_ctx_data_dir()
266        .unwrap_or_else(|_| home.join(".config/lean-ctx"));
267    if lean_dir.exists() {
268        terminal_ui::print_status_ok(&format!("{} ready", lean_dir.display()));
269    } else {
270        let _ = std::fs::create_dir_all(&lean_dir);
271        terminal_ui::print_status_new(&format!("Created {}", lean_dir.display()));
272    }
273    if let Some(tokens) = crate::core::data_dir::migrate_if_split() {
274        terminal_ui::print_status_new(&format!(
275            "Migrated stats from split data dir ({tokens} tokens recovered)"
276        ));
277    }
278    crate::doctor::run_compact();
279
280    // Step 8: Data sharing
281    terminal_ui::print_step_header(8, 11, "Help Improve lean-ctx");
282    println!("  Share anonymous compression stats to make lean-ctx better.");
283    println!("  \x1b[1mNo code, no file names, no personal data — ever.\x1b[0m");
284    println!();
285    print!("  Enable anonymous data sharing? \x1b[1m[y/N]\x1b[0m ");
286    use std::io::Write;
287    std::io::stdout().flush().ok();
288
289    let mut input = String::new();
290    let contribute = if std::io::stdin().read_line(&mut input).is_ok() {
291        let answer = input.trim().to_lowercase();
292        answer == "y" || answer == "yes"
293    } else {
294        false
295    };
296
297    if contribute {
298        let config_dir = crate::core::data_dir::lean_ctx_data_dir()
299            .unwrap_or_else(|_| home.join(".config/lean-ctx"));
300        let _ = std::fs::create_dir_all(&config_dir);
301        let config_path = config_dir.join("config.toml");
302        let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
303        if !config_content.contains("[cloud]") {
304            if !config_content.is_empty() && !config_content.ends_with('\n') {
305                config_content.push('\n');
306            }
307            config_content.push_str("\n[cloud]\ncontribute_enabled = true\n");
308            let _ = std::fs::write(&config_path, config_content);
309        }
310        terminal_ui::print_status_ok("Enabled — thank you!");
311    } else {
312        terminal_ui::print_status_skip("Skipped — enable later with: lean-ctx config");
313    }
314
315    // Step 9: Auto-Update opt-in
316    terminal_ui::print_step_header(9, 11, "Auto-Updates");
317    println!("  Keep lean-ctx up to date automatically.");
318    println!("  \x1b[1mChecks GitHub every 6h, installs only when a new release exists.\x1b[0m");
319    println!(
320        "  \x1b[2mNo restarts mid-session. Change anytime: lean-ctx update --schedule off\x1b[0m"
321    );
322    println!();
323    print!("  Enable automatic updates? \x1b[1m[y/N]\x1b[0m ");
324    std::io::stdout().flush().ok();
325
326    let mut auto_input = String::new();
327    let auto_update = if std::io::stdin().read_line(&mut auto_input).is_ok() {
328        let answer = auto_input.trim().to_lowercase();
329        answer == "y" || answer == "yes"
330    } else {
331        false
332    };
333
334    if auto_update {
335        let cfg = crate::core::config::Config::load();
336        let hours = cfg.updates.check_interval_hours;
337        match crate::core::update_scheduler::install_schedule(hours) {
338            Ok(info) => {
339                crate::core::update_scheduler::set_auto_update(true, false, hours);
340                terminal_ui::print_status_ok(&format!("Enabled — {info}"));
341            }
342            Err(e) => {
343                terminal_ui::print_status_warn(&format!("Scheduler setup failed: {e}"));
344                terminal_ui::print_status_skip("Enable later: lean-ctx update --schedule");
345            }
346        }
347    } else {
348        crate::core::update_scheduler::set_auto_update(false, false, 6);
349        terminal_ui::print_status_skip("Skipped — enable later: lean-ctx update --schedule");
350    }
351
352    // Step 10: Premium Features Configuration
353    terminal_ui::print_step_header(10, 11, "Premium Features");
354    configure_premium_features(&home);
355
356    // Step 11: Code Intelligence — build graph in background
357    terminal_ui::print_step_header(11, 11, "Code Intelligence");
358    let cwd = std::env::current_dir().ok();
359    let cwd_is_home = cwd
360        .as_ref()
361        .is_some_and(|d| dirs::home_dir().is_some_and(|h| d.as_path() == h.as_path()));
362    if cwd_is_home {
363        terminal_ui::print_status_warn(
364            "Running from $HOME — graph build skipped to avoid scanning your entire home directory.",
365        );
366        println!();
367        println!("  \x1b[1mSet a default project root to avoid this:\x1b[0m");
368        println!("  \x1b[2mEnter your main project path (or press Enter to skip):\x1b[0m");
369        print!("  \x1b[1m>\x1b[0m ");
370        use std::io::Write;
371        std::io::stdout().flush().ok();
372        let mut root_input = String::new();
373        if std::io::stdin().read_line(&mut root_input).is_ok() {
374            let root_trimmed = root_input.trim();
375            if root_trimmed.is_empty() {
376                terminal_ui::print_status_skip("No project root set. Set later: lean-ctx config set project_root /path/to/project");
377            } else {
378                let root_path = std::path::Path::new(root_trimmed);
379                if root_path.exists() && root_path.is_dir() {
380                    let config_path = crate::core::data_dir::lean_ctx_data_dir()
381                        .unwrap_or_else(|_| home.join(".config/lean-ctx"))
382                        .join("config.toml");
383                    let mut content = std::fs::read_to_string(&config_path).unwrap_or_default();
384                    if content.contains("project_root") {
385                        if let Ok(re) = regex::Regex::new(r#"(?m)^project_root\s*=\s*"[^"]*""#) {
386                            content = re
387                                .replace(&content, &format!("project_root = \"{root_trimmed}\""))
388                                .to_string();
389                        }
390                    } else {
391                        if !content.is_empty() && !content.ends_with('\n') {
392                            content.push('\n');
393                        }
394                        content.push_str(&format!("project_root = \"{root_trimmed}\"\n"));
395                    }
396                    let _ = std::fs::write(&config_path, &content);
397                    terminal_ui::print_status_ok(&format!("Project root set: {root_trimmed}"));
398                    if root_path.join(".git").exists()
399                        || root_path.join("Cargo.toml").exists()
400                        || root_path.join("package.json").exists()
401                    {
402                        spawn_index_build_background(root_path);
403                        terminal_ui::print_status_ok("Graph build started (background)");
404                    }
405                } else {
406                    terminal_ui::print_status_warn(&format!(
407                        "Path not found: {root_trimmed} — skipped"
408                    ));
409                }
410            }
411        }
412    } else {
413        let is_project = cwd.as_ref().is_some_and(|d| {
414            d.join(".git").exists()
415                || d.join("Cargo.toml").exists()
416                || d.join("package.json").exists()
417                || d.join("go.mod").exists()
418        });
419        if is_project {
420            println!("  \x1b[2mBuilding code graph for graph-aware reads, impact analysis,\x1b[0m");
421            println!("  \x1b[2mand smart search fusion in the background...\x1b[0m");
422            if let Some(ref root) = cwd {
423                spawn_index_build_background(root);
424            }
425            terminal_ui::print_status_ok("Graph build started (background)");
426        } else {
427            println!(
428                "  \x1b[2mRun `lean-ctx impact build` inside any git project to enable\x1b[0m"
429            );
430            println!(
431                "  \x1b[2mgraph-aware reads, impact analysis, and smart search fusion.\x1b[0m"
432            );
433        }
434    }
435    println!();
436
437    // Auto-approve transparency banner
438    {
439        let tools = crate::core::editor_registry::writers::auto_approve_tools();
440        println!();
441        println!(
442            "  \x1b[33m⚡ Auto-approved tools ({} total):\x1b[0m",
443            tools.len()
444        );
445        for chunk in tools.chunks(6) {
446            let names: Vec<_> = chunk.iter().map(|t| format!("\x1b[2m{t}\x1b[0m")).collect();
447            println!("    {}", names.join(", "));
448        }
449        println!("  \x1b[2mDisable with: lean-ctx setup --no-auto-approve\x1b[0m");
450    }
451
452    // Summary
453    println!();
454    println!(
455        "  \x1b[1;32m✓ Setup complete!\x1b[0m  \x1b[1m{}\x1b[0m configured, \x1b[2m{} already set, {} skipped\x1b[0m",
456        newly_configured.len(),
457        already_configured.len(),
458        not_installed.len()
459    );
460
461    if !errors.is_empty() {
462        println!(
463            "  \x1b[33m⚠ {} error{}: {}\x1b[0m",
464            errors.len(),
465            if errors.len() == 1 { "" } else { "s" },
466            errors.join(", ")
467        );
468    }
469
470    // Next steps
471    let source_cmd = crate::shell_hook::shell_source_command().unwrap_or("Restart your shell");
472
473    let dim = "\x1b[2m";
474    let bold = "\x1b[1m";
475    let cyan = "\x1b[36m";
476    let yellow = "\x1b[33m";
477    let rst = "\x1b[0m";
478
479    println!();
480    println!("  {bold}Next steps:{rst}");
481    println!();
482    println!("  {cyan}1.{rst} Reload your shell:");
483    println!("     {bold}{source_cmd}{rst}");
484    println!();
485
486    let mut tools_to_restart: Vec<String> = newly_configured
487        .iter()
488        .map(std::string::ToString::to_string)
489        .collect();
490    for name in rules_result
491        .injected
492        .iter()
493        .chain(rules_result.updated.iter())
494    {
495        if !tools_to_restart.iter().any(|t| t == name) {
496            tools_to_restart.push(name.clone());
497        }
498    }
499
500    if !tools_to_restart.is_empty() {
501        println!("  {cyan}2.{rst} {yellow}{bold}Restart your IDE / AI tool:{rst}");
502        println!("     {bold}{}{rst}", tools_to_restart.join(", "));
503        println!(
504            "     {dim}Changes take effect after a full restart (MCP may be enabled or disabled depending on mode).{rst}"
505        );
506        println!("     {dim}Close and re-open the application completely.{rst}");
507    } else if !already_configured.is_empty() {
508        println!(
509            "  {cyan}2.{rst} {dim}Your tools are already configured — no restart needed.{rst}"
510        );
511    }
512
513    println!();
514    println!(
515        "  {dim}After restart, lean-ctx will automatically optimize every AI interaction.{rst}"
516    );
517    println!("  {dim}Verify with:{rst} {bold}lean-ctx gain{rst}");
518
519    // Logo + commands
520    println!();
521    terminal_ui::print_logo_animated();
522    terminal_ui::print_command_box();
523}
524
525#[derive(Debug, Clone, Copy, Default)]
526pub struct SetupOptions {
527    pub non_interactive: bool,
528    pub yes: bool,
529    pub fix: bool,
530    pub json: bool,
531    pub no_auto_approve: bool,
532    pub skip_proxy: bool,
533}
534
535pub fn run_setup_with_options(opts: SetupOptions) -> Result<SetupReport, String> {
536    let _quiet_guard = opts.json.then(|| EnvVarGuard::set("LEAN_CTX_QUIET", "1"));
537    let started_at = Utc::now();
538    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
539    let binary = resolve_portable_binary();
540    let home_str = home.to_string_lossy().to_string();
541
542    let mut steps: Vec<SetupStepReport> = Vec::new();
543
544    // Step: Shell Hook
545    let mut shell_step = SetupStepReport {
546        name: "shell_hook".to_string(),
547        ok: true,
548        items: Vec::new(),
549        warnings: Vec::new(),
550        errors: Vec::new(),
551    };
552    if !opts.non_interactive || opts.yes {
553        if opts.json {
554            crate::cli::cmd_init_quiet(&["--global".to_string()]);
555        } else {
556            crate::cli::cmd_init(&["--global".to_string()]);
557        }
558        crate::shell_hook::install_all(opts.json);
559        #[cfg(not(windows))]
560        {
561            let hook_content = crate::cli::generate_hook_posix(&binary);
562            if crate::shell::is_container() {
563                crate::cli::write_env_sh_for_containers(&hook_content);
564                shell_step.items.push(SetupItem {
565                    name: "env_sh".to_string(),
566                    status: "created".to_string(),
567                    path: Some("~/.lean-ctx/env.sh".to_string()),
568                    note: Some("Docker/CI helper (BASH_ENV / CLAUDE_ENV_FILE)".to_string()),
569                });
570            } else {
571                shell_step.items.push(SetupItem {
572                    name: "env_sh".to_string(),
573                    status: "skipped".to_string(),
574                    path: None,
575                    note: Some("not a container environment".to_string()),
576                });
577            }
578        }
579        shell_step.items.push(SetupItem {
580            name: "init --global".to_string(),
581            status: "ran".to_string(),
582            path: None,
583            note: None,
584        });
585        shell_step.items.push(SetupItem {
586            name: "universal_shell_hook".to_string(),
587            status: "installed".to_string(),
588            path: None,
589            note: Some("~/.zshenv, ~/.bashenv, agent aliases".to_string()),
590        });
591    } else {
592        shell_step
593            .warnings
594            .push("non_interactive_without_yes: shell hook not installed (use --yes)".to_string());
595        shell_step.ok = false;
596        shell_step.items.push(SetupItem {
597            name: "init --global".to_string(),
598            status: "skipped".to_string(),
599            path: None,
600            note: Some("requires --yes in --non-interactive mode".to_string()),
601        });
602    }
603    steps.push(shell_step);
604
605    // Step: Daemon (optional acceleration for CLI routing)
606    let mut daemon_step = SetupStepReport {
607        name: "daemon".to_string(),
608        ok: true,
609        items: Vec::new(),
610        warnings: Vec::new(),
611        errors: Vec::new(),
612    };
613    {
614        let was_running = crate::daemon::is_daemon_running();
615        if was_running {
616            let _ = crate::daemon::stop_daemon();
617            std::thread::sleep(std::time::Duration::from_millis(500));
618        }
619        match crate::daemon::start_daemon(&[]) {
620            Ok(()) => {
621                let action = if was_running { "restarted" } else { "started" };
622                daemon_step.items.push(SetupItem {
623                    name: "serve --daemon".to_string(),
624                    status: action.to_string(),
625                    path: Some(crate::daemon::daemon_addr().display()),
626                    note: Some("CLI commands can route via IPC when running".to_string()),
627                });
628            }
629            Err(e) => {
630                daemon_step
631                    .warnings
632                    .push(format!("daemon start failed (non-fatal): {e}"));
633                daemon_step.items.push(SetupItem {
634                    name: "serve --daemon".to_string(),
635                    status: "skipped".to_string(),
636                    path: None,
637                    note: Some(format!("optional — {e}")),
638                });
639            }
640        }
641    }
642    steps.push(daemon_step);
643
644    // Step: Editor MCP config
645    let mut editor_step = SetupStepReport {
646        name: "editors".to_string(),
647        ok: true,
648        items: Vec::new(),
649        warnings: Vec::new(),
650        errors: Vec::new(),
651    };
652
653    let targets = crate::core::editor_registry::build_targets(&home);
654    for target in &targets {
655        let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
656        if !target.detect_path.exists() {
657            editor_step.items.push(SetupItem {
658                name: target.name.to_string(),
659                status: "not_detected".to_string(),
660                path: Some(short_path),
661                note: None,
662            });
663            continue;
664        }
665
666        let mode = if target.agent_key.is_empty() {
667            HookMode::Mcp
668        } else {
669            recommend_hook_mode(&target.agent_key)
670        };
671
672        let res = crate::core::editor_registry::write_config_with_options(
673            target,
674            &binary,
675            WriteOptions {
676                overwrite_invalid: opts.fix,
677            },
678        );
679        match res {
680            Ok(w) => {
681                let note_parts: Vec<String> = [Some(format!("mode={mode}")), w.note]
682                    .into_iter()
683                    .flatten()
684                    .collect();
685                editor_step.items.push(SetupItem {
686                    name: target.name.to_string(),
687                    status: match w.action {
688                        WriteAction::Created => "created".to_string(),
689                        WriteAction::Updated => "updated".to_string(),
690                        WriteAction::Already => "already".to_string(),
691                    },
692                    path: Some(short_path),
693                    note: Some(note_parts.join("; ")),
694                });
695            }
696            Err(e) => {
697                editor_step.ok = false;
698                editor_step.items.push(SetupItem {
699                    name: target.name.to_string(),
700                    status: "error".to_string(),
701                    path: Some(short_path),
702                    note: Some(e),
703                });
704            }
705        }
706    }
707    steps.push(editor_step);
708
709    // Step: Agent rules
710    let mut rules_step = SetupStepReport {
711        name: "agent_rules".to_string(),
712        ok: true,
713        items: Vec::new(),
714        warnings: Vec::new(),
715        errors: Vec::new(),
716    };
717    let rules_result = crate::rules_inject::inject_all_rules(&home);
718    for n in rules_result.injected {
719        rules_step.items.push(SetupItem {
720            name: n,
721            status: "injected".to_string(),
722            path: None,
723            note: None,
724        });
725    }
726    for n in rules_result.updated {
727        rules_step.items.push(SetupItem {
728            name: n,
729            status: "updated".to_string(),
730            path: None,
731            note: None,
732        });
733    }
734    for n in rules_result.already {
735        rules_step.items.push(SetupItem {
736            name: n,
737            status: "already".to_string(),
738            path: None,
739            note: None,
740        });
741    }
742    for e in rules_result.errors {
743        rules_step.ok = false;
744        rules_step.errors.push(e);
745    }
746    steps.push(rules_step);
747
748    // Step: Skill files
749    let mut skill_step = SetupStepReport {
750        name: "skill_files".to_string(),
751        ok: true,
752        items: Vec::new(),
753        warnings: Vec::new(),
754        errors: Vec::new(),
755    };
756    let skill_results = crate::rules_inject::install_all_skills(&home);
757    for (name, is_new) in &skill_results {
758        skill_step.items.push(SetupItem {
759            name: name.clone(),
760            status: if *is_new { "installed" } else { "already" }.to_string(),
761            path: None,
762            note: Some("SKILL.md".to_string()),
763        });
764    }
765    if !skill_step.items.is_empty() {
766        steps.push(skill_step);
767    }
768
769    // Step: Agent-specific hooks (all detected agents)
770    let mut hooks_step = SetupStepReport {
771        name: "agent_hooks".to_string(),
772        ok: true,
773        items: Vec::new(),
774        warnings: Vec::new(),
775        errors: Vec::new(),
776    };
777    for target in &targets {
778        if !target.detect_path.exists() || target.agent_key.is_empty() {
779            continue;
780        }
781        let mode = recommend_hook_mode(&target.agent_key);
782        crate::hooks::install_agent_hook_with_mode(&target.agent_key, true, mode);
783        let mcp_note = match configure_agent_mcp(&target.agent_key) {
784            Ok(()) => "; MCP config updated".to_string(),
785            Err(e) => format!("; MCP config skipped: {e}"),
786        };
787        hooks_step.items.push(SetupItem {
788            name: format!("{} hooks", target.name),
789            status: "installed".to_string(),
790            path: Some(target.detect_path.to_string_lossy().to_string()),
791            note: Some(format!(
792                "mode={mode}; merge-based install/repair (preserves other hooks/plugins){mcp_note}"
793            )),
794        });
795    }
796    if !hooks_step.items.is_empty() {
797        steps.push(hooks_step);
798    }
799
800    // Step: Proxy autostart + env vars (respects opt-in)
801    let mut proxy_step = SetupStepReport {
802        name: "proxy".to_string(),
803        ok: true,
804        items: Vec::new(),
805        warnings: Vec::new(),
806        errors: Vec::new(),
807    };
808    if opts.skip_proxy {
809        proxy_step.items.push(SetupItem {
810            name: "proxy".to_string(),
811            status: "skipped".to_string(),
812            path: None,
813            note: Some("Proxy not enabled (run `lean-ctx proxy enable`)".to_string()),
814        });
815    } else {
816        let proxy_cfg = crate::core::config::Config::load();
817        if proxy_cfg.proxy_enabled == Some(true) {
818            let proxy_port = crate::proxy_setup::default_port();
819            crate::proxy_autostart::install(proxy_port, true);
820            std::thread::sleep(std::time::Duration::from_millis(500));
821            crate::proxy_setup::install_proxy_env(&home, proxy_port, opts.json);
822            proxy_step.items.push(SetupItem {
823                name: "proxy_autostart".to_string(),
824                status: "installed".to_string(),
825                path: None,
826                note: Some("LaunchAgent/systemd auto-start on login".to_string()),
827            });
828            proxy_step.items.push(SetupItem {
829                name: "proxy_env".to_string(),
830                status: "configured".to_string(),
831                path: None,
832                note: Some("ANTHROPIC_BASE_URL, OPENAI_BASE_URL, GEMINI_API_BASE_URL".to_string()),
833            });
834        } else {
835            proxy_step.items.push(SetupItem {
836                name: "proxy".to_string(),
837                status: "skipped".to_string(),
838                path: None,
839                note: Some(
840                    "Proxy not opted-in (run `lean-ctx proxy enable` to activate)".to_string(),
841                ),
842            });
843        }
844    }
845    steps.push(proxy_step);
846
847    // Step: Environment / doctor (compact)
848    let mut env_step = SetupStepReport {
849        name: "doctor_compact".to_string(),
850        ok: true,
851        items: Vec::new(),
852        warnings: Vec::new(),
853        errors: Vec::new(),
854    };
855    let (passed, total) = crate::doctor::compact_score();
856    env_step.items.push(SetupItem {
857        name: "doctor".to_string(),
858        status: format!("{passed}/{total}"),
859        path: None,
860        note: None,
861    });
862    if passed != total {
863        env_step.warnings.push(format!(
864            "doctor compact not fully passing: {passed}/{total}"
865        ));
866    }
867    steps.push(env_step);
868
869    // Project root validation: warn if no root is configured and cwd is broad
870    {
871        let has_env_root = std::env::var("LEAN_CTX_PROJECT_ROOT")
872            .ok()
873            .is_some_and(|v| !v.is_empty());
874        let cfg = crate::core::config::Config::load();
875        let has_cfg_root = cfg.project_root.as_ref().is_some_and(|v| !v.is_empty());
876        if !has_env_root && !has_cfg_root {
877            if let Ok(cwd) = std::env::current_dir() {
878                let is_home = dirs::home_dir().is_some_and(|h| cwd == h);
879                if is_home {
880                    let mut root_step = SetupStepReport {
881                        name: "project_root".to_string(),
882                        ok: true,
883                        items: Vec::new(),
884                        warnings: vec![
885                            "No project_root configured. Running from $HOME can cause excessive scanning. \
886                             Set via: lean-ctx config set project_root /path/to/project".to_string()
887                        ],
888                        errors: Vec::new(),
889                    };
890                    root_step.items.push(SetupItem {
891                        name: "project_root".to_string(),
892                        status: "unconfigured".to_string(),
893                        path: None,
894                        note: Some(
895                            "Set LEAN_CTX_PROJECT_ROOT or add project_root to config.toml"
896                                .to_string(),
897                        ),
898                    });
899                    steps.push(root_step);
900                }
901            }
902        }
903    }
904
905    // Auto-build property graph if inside any recognized project
906    if let Ok(cwd) = std::env::current_dir() {
907        let is_project = cwd.join(".git").exists()
908            || cwd.join("Cargo.toml").exists()
909            || cwd.join("package.json").exists()
910            || cwd.join("go.mod").exists();
911        if is_project {
912            spawn_index_build_background(&cwd);
913        }
914    }
915
916    let finished_at = Utc::now();
917    let success = steps.iter().all(|s| s.ok);
918    let report = SetupReport {
919        schema_version: 1,
920        started_at,
921        finished_at,
922        success,
923        platform: PlatformInfo {
924            os: std::env::consts::OS.to_string(),
925            arch: std::env::consts::ARCH.to_string(),
926        },
927        steps,
928        warnings: Vec::new(),
929        errors: Vec::new(),
930    };
931
932    let path = SetupReport::default_path()?;
933    let mut content =
934        serde_json::to_string_pretty(&report).map_err(|e| format!("serialize report: {e}"))?;
935    content.push('\n');
936    crate::config_io::write_atomic(&path, &content)?;
937
938    Ok(report)
939}
940
941fn spawn_index_build_background(root: &std::path::Path) {
942    if std::env::var("LEAN_CTX_DISABLED").is_ok()
943        || matches!(std::env::var("LEAN_CTX_QUIET"), Ok(v) if v.trim() == "1")
944    {
945        return;
946    }
947    let root_str = crate::core::graph_index::normalize_project_root(&root.to_string_lossy());
948    if !crate::core::graph_index::is_safe_scan_root_public(&root_str) {
949        tracing::info!("[setup: skipping background graph build for unsafe root {root_str}]");
950        return;
951    }
952
953    let binary = resolve_portable_binary();
954
955    #[cfg(unix)]
956    {
957        let mut cmd = std::process::Command::new("nice");
958        cmd.args(["-n", "19"]);
959        if which_ionice_available() {
960            cmd.arg("ionice").args(["-c", "3"]);
961        }
962        cmd.arg(&binary)
963            .args(["index", "build", "--root"])
964            .arg(root)
965            .stdout(std::process::Stdio::null())
966            .stderr(std::process::Stdio::null())
967            .stdin(std::process::Stdio::null());
968        let _ = cmd.spawn();
969    }
970
971    #[cfg(windows)]
972    {
973        use std::os::windows::process::CommandExt;
974        const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
975        const CREATE_NO_WINDOW: u32 = 0x0800_0000;
976        let _ = std::process::Command::new(&binary)
977            .args(["index", "build", "--root"])
978            .arg(root)
979            .stdout(std::process::Stdio::null())
980            .stderr(std::process::Stdio::null())
981            .stdin(std::process::Stdio::null())
982            .creation_flags(CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW)
983            .spawn();
984    }
985}
986
987#[cfg(unix)]
988fn which_ionice_available() -> bool {
989    std::process::Command::new("ionice")
990        .arg("--version")
991        .stdout(std::process::Stdio::null())
992        .stderr(std::process::Stdio::null())
993        .status()
994        .is_ok()
995}
996
997/// Result of setting up a single agent with all steps.
998#[derive(Debug, Default)]
999pub struct AgentSetupResult {
1000    pub mcp_ok: bool,
1001    pub rules: crate::rules_inject::InjectResult,
1002    pub skill_installed: bool,
1003    pub errors: Vec<String>,
1004}
1005
1006/// Complete per-agent setup: MCP config + global rules + skill + hook.
1007/// Single source of truth — called by both `init --agent` and `setup`.
1008pub fn setup_single_agent(
1009    agent_name: &str,
1010    global: bool,
1011    mode: crate::hooks::HookMode,
1012) -> AgentSetupResult {
1013    let home = dirs::home_dir().unwrap_or_default();
1014    let mut result = AgentSetupResult::default();
1015
1016    crate::hooks::install_agent_hook_with_mode(agent_name, global, mode);
1017
1018    match configure_agent_mcp(agent_name) {
1019        Ok(()) => result.mcp_ok = true,
1020        Err(e) => result.errors.push(format!("MCP config: {e}")),
1021    }
1022
1023    result.rules = crate::rules_inject::inject_rules_for_agent(&home, agent_name);
1024
1025    if let Ok(path) = crate::rules_inject::install_skill_for_agent(&home, agent_name) {
1026        result.skill_installed = path.exists();
1027    }
1028
1029    result
1030}
1031
1032pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
1033    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
1034    let binary = resolve_portable_binary();
1035
1036    let targets = agent_mcp_targets(agent, &home)?;
1037
1038    let mut errors = Vec::new();
1039    for t in &targets {
1040        if let Err(e) = crate::core::editor_registry::write_config_with_options(
1041            t,
1042            &binary,
1043            WriteOptions {
1044                overwrite_invalid: true,
1045            },
1046        ) {
1047            eprintln!(
1048                "\x1b[33m⚠\x1b[0m  Could not configure {}: {}",
1049                t.config_path.display(),
1050                e
1051            );
1052            errors.push(e);
1053        }
1054    }
1055
1056    if agent == "kiro" {
1057        install_kiro_steering(&home);
1058    }
1059
1060    if agent == "vscode" || agent == "copilot" {
1061        if let Err(e) = crate::core::editor_registry::plan_mode::write_vscode_plan_settings() {
1062            eprintln!("\x1b[33m⚠\x1b[0m  VS Code plan mode: {e}");
1063        }
1064    }
1065    if agent == "claude" || agent == "claude-code" {
1066        if let Err(e) =
1067            crate::core::editor_registry::plan_mode::write_claude_code_plan_permissions()
1068        {
1069            eprintln!("\x1b[33m⚠\x1b[0m  Claude Code plan mode: {e}");
1070        }
1071    }
1072
1073    if errors.is_empty() {
1074        Ok(())
1075    } else {
1076        Err(format!(
1077            "{} config(s) could not be written. See warnings above.",
1078            errors.len()
1079        ))
1080    }
1081}
1082
1083fn agent_mcp_targets(agent: &str, home: &std::path::Path) -> Result<Vec<EditorTarget>, String> {
1084    let mut targets = Vec::<EditorTarget>::new();
1085
1086    let push = |targets: &mut Vec<EditorTarget>,
1087                name: &'static str,
1088                config_path: PathBuf,
1089                config_type: ConfigType| {
1090        targets.push(EditorTarget {
1091            name,
1092            agent_key: agent.to_string(),
1093            detect_path: PathBuf::from("/nonexistent"), // not used in direct agent config
1094            config_path,
1095            config_type,
1096        });
1097    };
1098
1099    let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
1100
1101    match agent {
1102        "cursor" => push(
1103            &mut targets,
1104            "Cursor",
1105            home.join(".cursor/mcp.json"),
1106            ConfigType::McpJson,
1107        ),
1108        "claude" | "claude-code" => push(
1109            &mut targets,
1110            "Claude Code",
1111            crate::core::editor_registry::claude_mcp_json_path(home),
1112            ConfigType::McpJson,
1113        ),
1114        "augment" => {
1115            push(
1116                &mut targets,
1117                "Augment CLI",
1118                crate::core::editor_registry::augment_cli_settings_path(home),
1119                ConfigType::McpJson,
1120            );
1121            push(
1122                &mut targets,
1123                "Augment (VS Code)",
1124                crate::core::editor_registry::augment_vscode_mcp_path(home),
1125                ConfigType::AugmentVsCode,
1126            );
1127        }
1128        "windsurf" => push(
1129            &mut targets,
1130            "Windsurf",
1131            home.join(".codeium/windsurf/mcp_config.json"),
1132            ConfigType::McpJson,
1133        ),
1134        "codex" => {
1135            let codex_dir =
1136                crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
1137            push(
1138                &mut targets,
1139                "Codex CLI",
1140                codex_dir.join("config.toml"),
1141                ConfigType::Codex,
1142            );
1143        }
1144        "gemini" => {
1145            push(
1146                &mut targets,
1147                "Gemini CLI",
1148                home.join(".gemini/settings.json"),
1149                ConfigType::GeminiSettings,
1150            );
1151            push(
1152                &mut targets,
1153                "Antigravity IDE",
1154                home.join(".gemini/antigravity/mcp_config.json"),
1155                ConfigType::McpJson,
1156            );
1157            push(
1158                &mut targets,
1159                "Antigravity CLI",
1160                home.join(".gemini/antigravity-cli/mcp_config.json"),
1161                ConfigType::McpJson,
1162            );
1163        }
1164        "antigravity" => push(
1165            &mut targets,
1166            "Antigravity IDE",
1167            home.join(".gemini/antigravity/mcp_config.json"),
1168            ConfigType::McpJson,
1169        ),
1170        "antigravity-cli" => push(
1171            &mut targets,
1172            "Antigravity CLI",
1173            home.join(".gemini/antigravity-cli/mcp_config.json"),
1174            ConfigType::McpJson,
1175        ),
1176        "copilot" => push(
1177            &mut targets,
1178            "Copilot CLI",
1179            home.join(".copilot/mcp-config.json"),
1180            ConfigType::CopilotCli,
1181        ),
1182        "crush" => push(
1183            &mut targets,
1184            "Crush",
1185            home.join(".config/crush/crush.json"),
1186            ConfigType::Crush,
1187        ),
1188        "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
1189        "qoder" => {
1190            for path in crate::core::editor_registry::qoder_all_mcp_paths(home) {
1191                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
1192            }
1193        }
1194        "qoderwork" => push(
1195            &mut targets,
1196            "QoderWork",
1197            crate::core::editor_registry::qoderwork_mcp_path(home),
1198            ConfigType::McpJson,
1199        ),
1200        "cline" => push(
1201            &mut targets,
1202            "Cline",
1203            crate::core::editor_registry::cline_mcp_path(),
1204            ConfigType::McpJson,
1205        ),
1206        "roo" => push(
1207            &mut targets,
1208            "Roo Code",
1209            crate::core::editor_registry::roo_mcp_path(),
1210            ConfigType::McpJson,
1211        ),
1212        "kiro" => push(
1213            &mut targets,
1214            "AWS Kiro",
1215            home.join(".kiro/settings/mcp.json"),
1216            ConfigType::McpJson,
1217        ),
1218        "verdent" => push(
1219            &mut targets,
1220            "Verdent",
1221            home.join(".verdent/mcp.json"),
1222            ConfigType::McpJson,
1223        ),
1224        "jetbrains" | "amp" | "openclaw" => {
1225            // Handled by dedicated install hooks (servers[] array / amp.mcpServers / mcp.servers)
1226        }
1227        "qwen" => push(
1228            &mut targets,
1229            "Qwen Code",
1230            home.join(".qwen/settings.json"),
1231            ConfigType::McpJson,
1232        ),
1233        "trae" => push(
1234            &mut targets,
1235            "Trae",
1236            home.join(".trae/mcp.json"),
1237            ConfigType::McpJson,
1238        ),
1239        "amazonq" => push(
1240            &mut targets,
1241            "Amazon Q Developer",
1242            home.join(".aws/amazonq/default.json"),
1243            ConfigType::McpJson,
1244        ),
1245        "opencode" => {
1246            #[cfg(windows)]
1247            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
1248                std::path::PathBuf::from(appdata)
1249                    .join("opencode")
1250                    .join("opencode.json")
1251            } else {
1252                home.join(".config/opencode/opencode.json")
1253            };
1254            #[cfg(not(windows))]
1255            let opencode_path = home.join(".config/opencode/opencode.json");
1256            push(
1257                &mut targets,
1258                "OpenCode",
1259                opencode_path,
1260                ConfigType::OpenCode,
1261            );
1262        }
1263        "hermes" => push(
1264            &mut targets,
1265            "Hermes Agent",
1266            home.join(".hermes/config.yaml"),
1267            ConfigType::HermesYaml,
1268        ),
1269        "vscode" => push(
1270            &mut targets,
1271            "VS Code",
1272            crate::core::editor_registry::vscode_mcp_path(),
1273            ConfigType::VsCodeMcp,
1274        ),
1275        "zed" => push(
1276            &mut targets,
1277            "Zed",
1278            crate::core::editor_registry::zed_settings_path(home),
1279            ConfigType::Zed,
1280        ),
1281        "aider" => push(
1282            &mut targets,
1283            "Aider",
1284            home.join(".aider/mcp.json"),
1285            ConfigType::McpJson,
1286        ),
1287        "continue" => push(
1288            &mut targets,
1289            "Continue",
1290            home.join(".continue/mcp.json"),
1291            ConfigType::McpJson,
1292        ),
1293        "neovim" => push(
1294            &mut targets,
1295            "Neovim (mcphub.nvim)",
1296            home.join(".config/mcphub/servers.json"),
1297            ConfigType::McpJson,
1298        ),
1299        "emacs" => push(
1300            &mut targets,
1301            "Emacs (mcp.el)",
1302            home.join(".emacs.d/mcp.json"),
1303            ConfigType::McpJson,
1304        ),
1305        "sublime" => push(
1306            &mut targets,
1307            "Sublime Text",
1308            home.join(".config/sublime-text/mcp.json"),
1309            ConfigType::McpJson,
1310        ),
1311        _ => {
1312            return Err(format!("Unknown agent '{agent}'"));
1313        }
1314    }
1315
1316    Ok(targets)
1317}
1318
1319pub fn disable_agent_mcp(agent: &str, overwrite_invalid: bool) -> Result<(), String> {
1320    let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
1321
1322    let mut targets = Vec::<EditorTarget>::new();
1323
1324    let push = |targets: &mut Vec<EditorTarget>,
1325                name: &'static str,
1326                config_path: PathBuf,
1327                config_type: ConfigType| {
1328        targets.push(EditorTarget {
1329            name,
1330            agent_key: agent.to_string(),
1331            detect_path: PathBuf::from("/nonexistent"),
1332            config_path,
1333            config_type,
1334        });
1335    };
1336
1337    let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
1338
1339    match agent {
1340        "cursor" => push(
1341            &mut targets,
1342            "Cursor",
1343            home.join(".cursor/mcp.json"),
1344            ConfigType::McpJson,
1345        ),
1346        "claude" | "claude-code" => push(
1347            &mut targets,
1348            "Claude Code",
1349            crate::core::editor_registry::claude_mcp_json_path(&home),
1350            ConfigType::McpJson,
1351        ),
1352        "augment" => {
1353            push(
1354                &mut targets,
1355                "Augment CLI",
1356                crate::core::editor_registry::augment_cli_settings_path(&home),
1357                ConfigType::McpJson,
1358            );
1359            push(
1360                &mut targets,
1361                "Augment (VS Code)",
1362                crate::core::editor_registry::augment_vscode_mcp_path(&home),
1363                ConfigType::AugmentVsCode,
1364            );
1365        }
1366        "windsurf" => push(
1367            &mut targets,
1368            "Windsurf",
1369            home.join(".codeium/windsurf/mcp_config.json"),
1370            ConfigType::McpJson,
1371        ),
1372        "codex" => {
1373            let codex_dir =
1374                crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
1375            push(
1376                &mut targets,
1377                "Codex CLI",
1378                codex_dir.join("config.toml"),
1379                ConfigType::Codex,
1380            );
1381        }
1382        "gemini" => {
1383            push(
1384                &mut targets,
1385                "Gemini CLI",
1386                home.join(".gemini/settings.json"),
1387                ConfigType::GeminiSettings,
1388            );
1389            push(
1390                &mut targets,
1391                "Antigravity IDE",
1392                home.join(".gemini/antigravity/mcp_config.json"),
1393                ConfigType::McpJson,
1394            );
1395            push(
1396                &mut targets,
1397                "Antigravity CLI",
1398                home.join(".gemini/antigravity-cli/mcp_config.json"),
1399                ConfigType::McpJson,
1400            );
1401        }
1402        "antigravity" => push(
1403            &mut targets,
1404            "Antigravity IDE",
1405            home.join(".gemini/antigravity/mcp_config.json"),
1406            ConfigType::McpJson,
1407        ),
1408        "antigravity-cli" => push(
1409            &mut targets,
1410            "Antigravity CLI",
1411            home.join(".gemini/antigravity-cli/mcp_config.json"),
1412            ConfigType::McpJson,
1413        ),
1414        "copilot" => push(
1415            &mut targets,
1416            "Copilot CLI",
1417            home.join(".copilot/mcp-config.json"),
1418            ConfigType::CopilotCli,
1419        ),
1420        "crush" => push(
1421            &mut targets,
1422            "Crush",
1423            home.join(".config/crush/crush.json"),
1424            ConfigType::Crush,
1425        ),
1426        "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
1427        "qoder" => {
1428            for path in crate::core::editor_registry::qoder_all_mcp_paths(&home) {
1429                push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
1430            }
1431        }
1432        "qoderwork" => push(
1433            &mut targets,
1434            "QoderWork",
1435            crate::core::editor_registry::qoderwork_mcp_path(&home),
1436            ConfigType::McpJson,
1437        ),
1438        "cline" => push(
1439            &mut targets,
1440            "Cline",
1441            crate::core::editor_registry::cline_mcp_path(),
1442            ConfigType::McpJson,
1443        ),
1444        "roo" => push(
1445            &mut targets,
1446            "Roo Code",
1447            crate::core::editor_registry::roo_mcp_path(),
1448            ConfigType::McpJson,
1449        ),
1450        "kiro" => push(
1451            &mut targets,
1452            "AWS Kiro",
1453            home.join(".kiro/settings/mcp.json"),
1454            ConfigType::McpJson,
1455        ),
1456        "verdent" => push(
1457            &mut targets,
1458            "Verdent",
1459            home.join(".verdent/mcp.json"),
1460            ConfigType::McpJson,
1461        ),
1462        "jetbrains" | "amp" | "openclaw" => {
1463            // Not supported for disable via this helper.
1464        }
1465        "qwen" => push(
1466            &mut targets,
1467            "Qwen Code",
1468            home.join(".qwen/settings.json"),
1469            ConfigType::McpJson,
1470        ),
1471        "trae" => push(
1472            &mut targets,
1473            "Trae",
1474            home.join(".trae/mcp.json"),
1475            ConfigType::McpJson,
1476        ),
1477        "amazonq" => push(
1478            &mut targets,
1479            "Amazon Q Developer",
1480            home.join(".aws/amazonq/default.json"),
1481            ConfigType::McpJson,
1482        ),
1483        "opencode" => {
1484            #[cfg(windows)]
1485            let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
1486                std::path::PathBuf::from(appdata)
1487                    .join("opencode")
1488                    .join("opencode.json")
1489            } else {
1490                home.join(".config/opencode/opencode.json")
1491            };
1492            #[cfg(not(windows))]
1493            let opencode_path = home.join(".config/opencode/opencode.json");
1494            push(
1495                &mut targets,
1496                "OpenCode",
1497                opencode_path,
1498                ConfigType::OpenCode,
1499            );
1500        }
1501        "hermes" => push(
1502            &mut targets,
1503            "Hermes Agent",
1504            home.join(".hermes/config.yaml"),
1505            ConfigType::HermesYaml,
1506        ),
1507        "vscode" => push(
1508            &mut targets,
1509            "VS Code",
1510            crate::core::editor_registry::vscode_mcp_path(),
1511            ConfigType::VsCodeMcp,
1512        ),
1513        "zed" => push(
1514            &mut targets,
1515            "Zed",
1516            crate::core::editor_registry::zed_settings_path(&home),
1517            ConfigType::Zed,
1518        ),
1519        "aider" => push(
1520            &mut targets,
1521            "Aider",
1522            home.join(".aider/mcp.json"),
1523            ConfigType::McpJson,
1524        ),
1525        "continue" => push(
1526            &mut targets,
1527            "Continue",
1528            home.join(".continue/mcp.json"),
1529            ConfigType::McpJson,
1530        ),
1531        "neovim" => push(
1532            &mut targets,
1533            "Neovim (mcphub.nvim)",
1534            home.join(".config/mcphub/servers.json"),
1535            ConfigType::McpJson,
1536        ),
1537        "emacs" => push(
1538            &mut targets,
1539            "Emacs (mcp.el)",
1540            home.join(".emacs.d/mcp.json"),
1541            ConfigType::McpJson,
1542        ),
1543        "sublime" => push(
1544            &mut targets,
1545            "Sublime Text",
1546            home.join(".config/sublime-text/mcp.json"),
1547            ConfigType::McpJson,
1548        ),
1549        _ => {
1550            return Err(format!("Unknown agent '{agent}'"));
1551        }
1552    }
1553
1554    for t in &targets {
1555        crate::core::editor_registry::remove_lean_ctx_server(
1556            t,
1557            WriteOptions { overwrite_invalid },
1558        )?;
1559    }
1560
1561    Ok(())
1562}
1563
1564pub fn install_skill_files(home: &std::path::Path) -> Vec<(String, bool)> {
1565    crate::rules_inject::install_all_skills(home)
1566}
1567
1568fn install_kiro_steering(home: &std::path::Path) {
1569    let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
1570    let steering_dir = cwd.join(".kiro").join("steering");
1571    let steering_file = steering_dir.join("lean-ctx.md");
1572
1573    if steering_file.exists()
1574        && std::fs::read_to_string(&steering_file)
1575            .unwrap_or_default()
1576            .contains("lean-ctx")
1577    {
1578        println!("  Kiro steering file already exists at .kiro/steering/lean-ctx.md");
1579        return;
1580    }
1581
1582    let _ = std::fs::create_dir_all(&steering_dir);
1583    let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
1584    println!("  \x1b[32m✓\x1b[0m Created .kiro/steering/lean-ctx.md (Kiro will now prefer lean-ctx tools)");
1585}
1586
1587fn configure_plan_mode_settings(newly_configured: &[&str], already_configured: &[&str]) {
1588    use crate::terminal_ui;
1589
1590    let all_configured: Vec<&str> = newly_configured
1591        .iter()
1592        .chain(already_configured.iter())
1593        .copied()
1594        .collect();
1595
1596    let has_vscode = all_configured.contains(&"VS Code");
1597    let has_claude = all_configured.contains(&"Claude Code");
1598
1599    if !has_vscode && !has_claude {
1600        return;
1601    }
1602
1603    if has_vscode {
1604        match crate::core::editor_registry::plan_mode::write_vscode_plan_settings() {
1605            Ok(r) if r.action == WriteAction::Already => {
1606                terminal_ui::print_status_ok(
1607                    "VS Code            \x1b[2mplan mode already configured\x1b[0m",
1608                );
1609            }
1610            Ok(_) => {
1611                terminal_ui::print_status_new(
1612                    "VS Code            \x1b[2mplan mode tools configured\x1b[0m",
1613                );
1614            }
1615            Err(e) => {
1616                terminal_ui::print_status_warn(&format!("VS Code plan mode: {e}"));
1617            }
1618        }
1619    }
1620
1621    if has_claude {
1622        match crate::core::editor_registry::plan_mode::write_claude_code_plan_permissions() {
1623            Ok(r) if r.action == WriteAction::Already => {
1624                terminal_ui::print_status_ok(
1625                    "Claude Code        \x1b[2mplan mode permissions present\x1b[0m",
1626                );
1627            }
1628            Ok(_) => {
1629                terminal_ui::print_status_new(
1630                    "Claude Code        \x1b[2mplan mode permissions added\x1b[0m",
1631                );
1632            }
1633            Err(e) => {
1634                terminal_ui::print_status_warn(&format!("Claude Code plan mode: {e}"));
1635            }
1636        }
1637    }
1638}
1639
1640fn shorten_path(path: &str, home: &str) -> String {
1641    if let Some(stripped) = path.strip_prefix(home) {
1642        format!("~{stripped}")
1643    } else {
1644        path.to_string()
1645    }
1646}
1647
1648fn upsert_toml_key(content: &mut String, key: &str, value: &str) {
1649    let pattern = format!("{key} = ");
1650    if let Some(start) = content.find(&pattern) {
1651        let line_end = content[start..]
1652            .find('\n')
1653            .map_or(content.len(), |p| start + p);
1654        content.replace_range(start..line_end, &format!("{key} = \"{value}\""));
1655    } else {
1656        if !content.is_empty() && !content.ends_with('\n') {
1657            content.push('\n');
1658        }
1659        content.push_str(&format!("{key} = \"{value}\"\n"));
1660    }
1661}
1662
1663fn remove_toml_key(content: &mut String, key: &str) {
1664    let pattern = format!("{key} = ");
1665    if let Some(start) = content.find(&pattern) {
1666        let line_end = content[start..]
1667            .find('\n')
1668            .map_or(content.len(), |p| start + p + 1);
1669        content.replace_range(start..line_end, "");
1670    }
1671}
1672
1673fn configure_premium_features(home: &std::path::Path) {
1674    use crate::terminal_ui;
1675    use std::io::Write;
1676
1677    let config_dir = crate::core::data_dir::lean_ctx_data_dir()
1678        .unwrap_or_else(|_| home.join(".config/lean-ctx"));
1679    let _ = std::fs::create_dir_all(&config_dir);
1680    let config_path = config_dir.join("config.toml");
1681    let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
1682
1683    let dim = "\x1b[2m";
1684    let bold = "\x1b[1m";
1685    let cyan = "\x1b[36m";
1686    let rst = "\x1b[0m";
1687
1688    // Unified Compression Level (replaces terse_agent + output_density)
1689    println!("\n  {bold}Compression Level{rst} {dim}(controls all token optimization layers){rst}");
1690    println!("  {dim}Applies to tool output, agent prompts, and protocol mode.{rst}");
1691    println!();
1692    println!("  {cyan}off{rst}      — No compression (full verbose output)");
1693    println!("  {cyan}lite{rst}     — Light: concise output, basic terse filtering {dim}(~25% savings){rst}");
1694    println!("  {cyan}standard{rst} — Dense output + compact protocol + pattern-aware {dim}(~45% savings){rst}");
1695    println!("  {cyan}max{rst}      — Expert mode: TDD protocol, all layers active {dim}(~65% savings){rst}");
1696    println!();
1697    print!("  Compression level? {bold}[off/lite/standard/max]{rst} {dim}(default: off){rst} ");
1698    std::io::stdout().flush().ok();
1699
1700    let mut level_input = String::new();
1701    let level = if std::io::stdin().read_line(&mut level_input).is_ok() {
1702        match level_input.trim().to_lowercase().as_str() {
1703            "lite" => "lite",
1704            "standard" | "std" => "standard",
1705            "max" => "max",
1706            _ => "off",
1707        }
1708    } else {
1709        "off"
1710    };
1711
1712    let effective_level = if level != "off" {
1713        upsert_toml_key(&mut config_content, "compression_level", level);
1714        remove_toml_key(&mut config_content, "terse_agent");
1715        remove_toml_key(&mut config_content, "output_density");
1716        terminal_ui::print_status_ok(&format!("Compression: {level}"));
1717        crate::core::config::CompressionLevel::from_str_label(level)
1718    } else if config_content.contains("compression_level") {
1719        upsert_toml_key(&mut config_content, "compression_level", "off");
1720        terminal_ui::print_status_ok("Compression: off");
1721        Some(crate::core::config::CompressionLevel::Off)
1722    } else {
1723        terminal_ui::print_status_skip(
1724            "Compression: off (change later with: lean-ctx compression <level>)",
1725        );
1726        Some(crate::core::config::CompressionLevel::Off)
1727    };
1728
1729    if let Some(lvl) = effective_level {
1730        let n = crate::core::terse::rules_inject::inject(&lvl);
1731        if n > 0 {
1732            terminal_ui::print_status_ok(&format!(
1733                "Updated {n} rules file(s) with compression prompt"
1734            ));
1735        }
1736    }
1737
1738    // Tool Result Archive (unchanged)
1739    println!(
1740        "\n  {bold}Tool Result Archive{rst} {dim}(zero-loss: large outputs archived, retrievable via ctx_expand){rst}"
1741    );
1742    print!("  Enable auto-archive? {bold}[Y/n]{rst} ");
1743    std::io::stdout().flush().ok();
1744
1745    let mut archive_input = String::new();
1746    let archive_on = if std::io::stdin().read_line(&mut archive_input).is_ok() {
1747        let a = archive_input.trim().to_lowercase();
1748        a.is_empty() || a == "y" || a == "yes"
1749    } else {
1750        true
1751    };
1752
1753    if archive_on && !config_content.contains("[archive]") {
1754        if !config_content.is_empty() && !config_content.ends_with('\n') {
1755            config_content.push('\n');
1756        }
1757        config_content.push_str("\n[archive]\nenabled = true\n");
1758        terminal_ui::print_status_ok("Tool Result Archive: enabled");
1759    } else if !archive_on {
1760        terminal_ui::print_status_skip("Archive: off (enable later in config.toml)");
1761    }
1762
1763    let _ = std::fs::write(&config_path, config_content);
1764}
1765
1766#[cfg(all(test, target_os = "macos"))]
1767mod tests {
1768    use super::*;
1769
1770    #[test]
1771    #[cfg(target_os = "macos")]
1772    fn qoder_agent_targets_include_all_macos_mcp_locations() {
1773        let home = std::path::Path::new("/Users/tester");
1774        let targets = agent_mcp_targets("qoder", home).unwrap();
1775        let paths: Vec<_> = targets.iter().map(|t| t.config_path.as_path()).collect();
1776
1777        assert_eq!(
1778            paths,
1779            vec![
1780                home.join(".qoder/mcp.json").as_path(),
1781                home.join("Library/Application Support/Qoder/User/mcp.json")
1782                    .as_path(),
1783                home.join("Library/Application Support/Qoder/SharedClientCache/mcp.json")
1784                    .as_path(),
1785            ]
1786        );
1787        assert!(targets
1788            .iter()
1789            .all(|t| t.config_type == ConfigType::QoderSettings));
1790    }
1791}