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