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 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 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 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 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 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 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 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 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 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 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 terminal_ui::print_step_header(10, 11, "Premium Features");
354 configure_premium_features(&home);
355
356 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 {
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 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 let shell = std::env::var("SHELL").unwrap_or_default();
472 let source_cmd = if shell.contains("zsh") {
473 "source ~/.zshrc"
474 } else if shell.contains("fish") {
475 "source ~/.config/fish/config.fish"
476 } else if shell.contains("bash") {
477 "source ~/.bashrc"
478 } else {
479 "Restart your shell"
480 };
481
482 let dim = "\x1b[2m";
483 let bold = "\x1b[1m";
484 let cyan = "\x1b[36m";
485 let yellow = "\x1b[33m";
486 let rst = "\x1b[0m";
487
488 println!();
489 println!(" {bold}Next steps:{rst}");
490 println!();
491 println!(" {cyan}1.{rst} Reload your shell:");
492 println!(" {bold}{source_cmd}{rst}");
493 println!();
494
495 let mut tools_to_restart: Vec<String> = newly_configured
496 .iter()
497 .map(std::string::ToString::to_string)
498 .collect();
499 for name in rules_result
500 .injected
501 .iter()
502 .chain(rules_result.updated.iter())
503 {
504 if !tools_to_restart.iter().any(|t| t == name) {
505 tools_to_restart.push(name.clone());
506 }
507 }
508
509 if !tools_to_restart.is_empty() {
510 println!(" {cyan}2.{rst} {yellow}{bold}Restart your IDE / AI tool:{rst}");
511 println!(" {bold}{}{rst}", tools_to_restart.join(", "));
512 println!(
513 " {dim}Changes take effect after a full restart (MCP may be enabled or disabled depending on mode).{rst}"
514 );
515 println!(" {dim}Close and re-open the application completely.{rst}");
516 } else if !already_configured.is_empty() {
517 println!(
518 " {cyan}2.{rst} {dim}Your tools are already configured — no restart needed.{rst}"
519 );
520 }
521
522 println!();
523 println!(
524 " {dim}After restart, lean-ctx will automatically optimize every AI interaction.{rst}"
525 );
526 println!(" {dim}Verify with:{rst} {bold}lean-ctx gain{rst}");
527
528 println!();
530 terminal_ui::print_logo_animated();
531 terminal_ui::print_command_box();
532}
533
534#[derive(Debug, Clone, Copy, Default)]
535pub struct SetupOptions {
536 pub non_interactive: bool,
537 pub yes: bool,
538 pub fix: bool,
539 pub json: bool,
540 pub no_auto_approve: bool,
541 pub skip_proxy: bool,
542}
543
544pub fn run_setup_with_options(opts: SetupOptions) -> Result<SetupReport, String> {
545 let _quiet_guard = opts.json.then(|| EnvVarGuard::set("LEAN_CTX_QUIET", "1"));
546 let started_at = Utc::now();
547 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
548 let binary = resolve_portable_binary();
549 let home_str = home.to_string_lossy().to_string();
550
551 let mut steps: Vec<SetupStepReport> = Vec::new();
552
553 let mut shell_step = SetupStepReport {
555 name: "shell_hook".to_string(),
556 ok: true,
557 items: Vec::new(),
558 warnings: Vec::new(),
559 errors: Vec::new(),
560 };
561 if !opts.non_interactive || opts.yes {
562 if opts.json {
563 crate::cli::cmd_init_quiet(&["--global".to_string()]);
564 } else {
565 crate::cli::cmd_init(&["--global".to_string()]);
566 }
567 crate::shell_hook::install_all(opts.json);
568 #[cfg(not(windows))]
569 {
570 let hook_content = crate::cli::generate_hook_posix(&binary);
571 if crate::shell::is_container() {
572 crate::cli::write_env_sh_for_containers(&hook_content);
573 shell_step.items.push(SetupItem {
574 name: "env_sh".to_string(),
575 status: "created".to_string(),
576 path: Some("~/.lean-ctx/env.sh".to_string()),
577 note: Some("Docker/CI helper (BASH_ENV / CLAUDE_ENV_FILE)".to_string()),
578 });
579 } else {
580 shell_step.items.push(SetupItem {
581 name: "env_sh".to_string(),
582 status: "skipped".to_string(),
583 path: None,
584 note: Some("not a container environment".to_string()),
585 });
586 }
587 }
588 shell_step.items.push(SetupItem {
589 name: "init --global".to_string(),
590 status: "ran".to_string(),
591 path: None,
592 note: None,
593 });
594 shell_step.items.push(SetupItem {
595 name: "universal_shell_hook".to_string(),
596 status: "installed".to_string(),
597 path: None,
598 note: Some("~/.zshenv, ~/.bashenv, agent aliases".to_string()),
599 });
600 } else {
601 shell_step
602 .warnings
603 .push("non_interactive_without_yes: shell hook not installed (use --yes)".to_string());
604 shell_step.ok = false;
605 shell_step.items.push(SetupItem {
606 name: "init --global".to_string(),
607 status: "skipped".to_string(),
608 path: None,
609 note: Some("requires --yes in --non-interactive mode".to_string()),
610 });
611 }
612 steps.push(shell_step);
613
614 let mut daemon_step = SetupStepReport {
616 name: "daemon".to_string(),
617 ok: true,
618 items: Vec::new(),
619 warnings: Vec::new(),
620 errors: Vec::new(),
621 };
622 {
623 let was_running = crate::daemon::is_daemon_running();
624 if was_running {
625 let _ = crate::daemon::stop_daemon();
626 std::thread::sleep(std::time::Duration::from_millis(500));
627 }
628 match crate::daemon::start_daemon(&[]) {
629 Ok(()) => {
630 let action = if was_running { "restarted" } else { "started" };
631 daemon_step.items.push(SetupItem {
632 name: "serve --daemon".to_string(),
633 status: action.to_string(),
634 path: Some(crate::daemon::daemon_addr().display()),
635 note: Some("CLI commands can route via IPC when running".to_string()),
636 });
637 }
638 Err(e) => {
639 daemon_step
640 .warnings
641 .push(format!("daemon start failed (non-fatal): {e}"));
642 daemon_step.items.push(SetupItem {
643 name: "serve --daemon".to_string(),
644 status: "skipped".to_string(),
645 path: None,
646 note: Some(format!("optional — {e}")),
647 });
648 }
649 }
650 }
651 steps.push(daemon_step);
652
653 let mut editor_step = SetupStepReport {
655 name: "editors".to_string(),
656 ok: true,
657 items: Vec::new(),
658 warnings: Vec::new(),
659 errors: Vec::new(),
660 };
661
662 let targets = crate::core::editor_registry::build_targets(&home);
663 for target in &targets {
664 let short_path = shorten_path(&target.config_path.to_string_lossy(), &home_str);
665 if !target.detect_path.exists() {
666 editor_step.items.push(SetupItem {
667 name: target.name.to_string(),
668 status: "not_detected".to_string(),
669 path: Some(short_path),
670 note: None,
671 });
672 continue;
673 }
674
675 let mode = if target.agent_key.is_empty() {
676 HookMode::Mcp
677 } else {
678 recommend_hook_mode(&target.agent_key)
679 };
680
681 let res = crate::core::editor_registry::write_config_with_options(
682 target,
683 &binary,
684 WriteOptions {
685 overwrite_invalid: opts.fix,
686 },
687 );
688 match res {
689 Ok(w) => {
690 let note_parts: Vec<String> = [Some(format!("mode={mode}")), w.note]
691 .into_iter()
692 .flatten()
693 .collect();
694 editor_step.items.push(SetupItem {
695 name: target.name.to_string(),
696 status: match w.action {
697 WriteAction::Created => "created".to_string(),
698 WriteAction::Updated => "updated".to_string(),
699 WriteAction::Already => "already".to_string(),
700 },
701 path: Some(short_path),
702 note: Some(note_parts.join("; ")),
703 });
704 }
705 Err(e) => {
706 editor_step.ok = false;
707 editor_step.items.push(SetupItem {
708 name: target.name.to_string(),
709 status: "error".to_string(),
710 path: Some(short_path),
711 note: Some(e),
712 });
713 }
714 }
715 }
716 steps.push(editor_step);
717
718 let mut rules_step = SetupStepReport {
720 name: "agent_rules".to_string(),
721 ok: true,
722 items: Vec::new(),
723 warnings: Vec::new(),
724 errors: Vec::new(),
725 };
726 let rules_result = crate::rules_inject::inject_all_rules(&home);
727 for n in rules_result.injected {
728 rules_step.items.push(SetupItem {
729 name: n,
730 status: "injected".to_string(),
731 path: None,
732 note: None,
733 });
734 }
735 for n in rules_result.updated {
736 rules_step.items.push(SetupItem {
737 name: n,
738 status: "updated".to_string(),
739 path: None,
740 note: None,
741 });
742 }
743 for n in rules_result.already {
744 rules_step.items.push(SetupItem {
745 name: n,
746 status: "already".to_string(),
747 path: None,
748 note: None,
749 });
750 }
751 for e in rules_result.errors {
752 rules_step.ok = false;
753 rules_step.errors.push(e);
754 }
755 steps.push(rules_step);
756
757 let mut skill_step = SetupStepReport {
759 name: "skill_files".to_string(),
760 ok: true,
761 items: Vec::new(),
762 warnings: Vec::new(),
763 errors: Vec::new(),
764 };
765 let skill_results = crate::rules_inject::install_all_skills(&home);
766 for (name, is_new) in &skill_results {
767 skill_step.items.push(SetupItem {
768 name: name.clone(),
769 status: if *is_new { "installed" } else { "already" }.to_string(),
770 path: None,
771 note: Some("SKILL.md".to_string()),
772 });
773 }
774 if !skill_step.items.is_empty() {
775 steps.push(skill_step);
776 }
777
778 let mut hooks_step = SetupStepReport {
780 name: "agent_hooks".to_string(),
781 ok: true,
782 items: Vec::new(),
783 warnings: Vec::new(),
784 errors: Vec::new(),
785 };
786 for target in &targets {
787 if !target.detect_path.exists() || target.agent_key.is_empty() {
788 continue;
789 }
790 let mode = recommend_hook_mode(&target.agent_key);
791 crate::hooks::install_agent_hook_with_mode(&target.agent_key, true, mode);
792 let mcp_note = match configure_agent_mcp(&target.agent_key) {
793 Ok(()) => "; MCP config updated".to_string(),
794 Err(e) => format!("; MCP config skipped: {e}"),
795 };
796 hooks_step.items.push(SetupItem {
797 name: format!("{} hooks", target.name),
798 status: "installed".to_string(),
799 path: Some(target.detect_path.to_string_lossy().to_string()),
800 note: Some(format!(
801 "mode={mode}; merge-based install/repair (preserves other hooks/plugins){mcp_note}"
802 )),
803 });
804 }
805 if !hooks_step.items.is_empty() {
806 steps.push(hooks_step);
807 }
808
809 let mut proxy_step = SetupStepReport {
811 name: "proxy".to_string(),
812 ok: true,
813 items: Vec::new(),
814 warnings: Vec::new(),
815 errors: Vec::new(),
816 };
817 if opts.skip_proxy {
818 proxy_step.items.push(SetupItem {
819 name: "proxy".to_string(),
820 status: "skipped".to_string(),
821 path: None,
822 note: Some("Proxy not enabled (run `lean-ctx proxy enable`)".to_string()),
823 });
824 } else {
825 let proxy_cfg = crate::core::config::Config::load();
826 if proxy_cfg.proxy_enabled == Some(true) {
827 let proxy_port = crate::proxy_setup::default_port();
828 crate::proxy_autostart::install(proxy_port, true);
829 std::thread::sleep(std::time::Duration::from_millis(500));
830 crate::proxy_setup::install_proxy_env(&home, proxy_port, opts.json);
831 proxy_step.items.push(SetupItem {
832 name: "proxy_autostart".to_string(),
833 status: "installed".to_string(),
834 path: None,
835 note: Some("LaunchAgent/systemd auto-start on login".to_string()),
836 });
837 proxy_step.items.push(SetupItem {
838 name: "proxy_env".to_string(),
839 status: "configured".to_string(),
840 path: None,
841 note: Some("ANTHROPIC_BASE_URL, OPENAI_BASE_URL, GEMINI_API_BASE_URL".to_string()),
842 });
843 } else {
844 proxy_step.items.push(SetupItem {
845 name: "proxy".to_string(),
846 status: "skipped".to_string(),
847 path: None,
848 note: Some(
849 "Proxy not opted-in (run `lean-ctx proxy enable` to activate)".to_string(),
850 ),
851 });
852 }
853 }
854 steps.push(proxy_step);
855
856 let mut env_step = SetupStepReport {
858 name: "doctor_compact".to_string(),
859 ok: true,
860 items: Vec::new(),
861 warnings: Vec::new(),
862 errors: Vec::new(),
863 };
864 let (passed, total) = crate::doctor::compact_score();
865 env_step.items.push(SetupItem {
866 name: "doctor".to_string(),
867 status: format!("{passed}/{total}"),
868 path: None,
869 note: None,
870 });
871 if passed != total {
872 env_step.warnings.push(format!(
873 "doctor compact not fully passing: {passed}/{total}"
874 ));
875 }
876 steps.push(env_step);
877
878 {
880 let has_env_root = std::env::var("LEAN_CTX_PROJECT_ROOT")
881 .ok()
882 .is_some_and(|v| !v.is_empty());
883 let cfg = crate::core::config::Config::load();
884 let has_cfg_root = cfg.project_root.as_ref().is_some_and(|v| !v.is_empty());
885 if !has_env_root && !has_cfg_root {
886 if let Ok(cwd) = std::env::current_dir() {
887 let is_home = dirs::home_dir().is_some_and(|h| cwd == h);
888 if is_home {
889 let mut root_step = SetupStepReport {
890 name: "project_root".to_string(),
891 ok: true,
892 items: Vec::new(),
893 warnings: vec![
894 "No project_root configured. Running from $HOME can cause excessive scanning. \
895 Set via: lean-ctx config set project_root /path/to/project".to_string()
896 ],
897 errors: Vec::new(),
898 };
899 root_step.items.push(SetupItem {
900 name: "project_root".to_string(),
901 status: "unconfigured".to_string(),
902 path: None,
903 note: Some(
904 "Set LEAN_CTX_PROJECT_ROOT or add project_root to config.toml"
905 .to_string(),
906 ),
907 });
908 steps.push(root_step);
909 }
910 }
911 }
912 }
913
914 if let Ok(cwd) = std::env::current_dir() {
916 let is_project = cwd.join(".git").exists()
917 || cwd.join("Cargo.toml").exists()
918 || cwd.join("package.json").exists()
919 || cwd.join("go.mod").exists();
920 if is_project {
921 spawn_index_build_background(&cwd);
922 }
923 }
924
925 let finished_at = Utc::now();
926 let success = steps.iter().all(|s| s.ok);
927 let report = SetupReport {
928 schema_version: 1,
929 started_at,
930 finished_at,
931 success,
932 platform: PlatformInfo {
933 os: std::env::consts::OS.to_string(),
934 arch: std::env::consts::ARCH.to_string(),
935 },
936 steps,
937 warnings: Vec::new(),
938 errors: Vec::new(),
939 };
940
941 let path = SetupReport::default_path()?;
942 let mut content =
943 serde_json::to_string_pretty(&report).map_err(|e| format!("serialize report: {e}"))?;
944 content.push('\n');
945 crate::config_io::write_atomic(&path, &content)?;
946
947 Ok(report)
948}
949
950fn spawn_index_build_background(root: &std::path::Path) {
951 if std::env::var("LEAN_CTX_DISABLED").is_ok()
952 || matches!(std::env::var("LEAN_CTX_QUIET"), Ok(v) if v.trim() == "1")
953 {
954 return;
955 }
956 let root_str = crate::core::graph_index::normalize_project_root(&root.to_string_lossy());
957 if !crate::core::graph_index::is_safe_scan_root_public(&root_str) {
958 tracing::info!("[setup: skipping background graph build for unsafe root {root_str}]");
959 return;
960 }
961
962 let binary = resolve_portable_binary();
963
964 #[cfg(unix)]
965 {
966 let mut cmd = std::process::Command::new("nice");
967 cmd.args(["-n", "19"]);
968 if which_ionice_available() {
969 cmd.arg("ionice").args(["-c", "3"]);
970 }
971 cmd.arg(&binary)
972 .args(["index", "build-graph", "--root"])
973 .arg(root)
974 .stdout(std::process::Stdio::null())
975 .stderr(std::process::Stdio::null())
976 .stdin(std::process::Stdio::null());
977 let _ = cmd.spawn();
978 }
979
980 #[cfg(windows)]
981 {
982 use std::os::windows::process::CommandExt;
983 const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
984 const CREATE_NO_WINDOW: u32 = 0x0800_0000;
985 let _ = std::process::Command::new(&binary)
986 .args(["index", "build-graph", "--root"])
987 .arg(root)
988 .stdout(std::process::Stdio::null())
989 .stderr(std::process::Stdio::null())
990 .stdin(std::process::Stdio::null())
991 .creation_flags(CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW)
992 .spawn();
993 }
994}
995
996#[cfg(unix)]
997fn which_ionice_available() -> bool {
998 std::process::Command::new("ionice")
999 .arg("--version")
1000 .stdout(std::process::Stdio::null())
1001 .stderr(std::process::Stdio::null())
1002 .status()
1003 .is_ok()
1004}
1005
1006#[derive(Debug, Default)]
1008pub struct AgentSetupResult {
1009 pub mcp_ok: bool,
1010 pub rules: crate::rules_inject::InjectResult,
1011 pub skill_installed: bool,
1012 pub errors: Vec<String>,
1013}
1014
1015pub fn setup_single_agent(
1018 agent_name: &str,
1019 global: bool,
1020 mode: crate::hooks::HookMode,
1021) -> AgentSetupResult {
1022 let home = dirs::home_dir().unwrap_or_default();
1023 let mut result = AgentSetupResult::default();
1024
1025 crate::hooks::install_agent_hook_with_mode(agent_name, global, mode);
1026
1027 match configure_agent_mcp(agent_name) {
1028 Ok(()) => result.mcp_ok = true,
1029 Err(e) => result.errors.push(format!("MCP config: {e}")),
1030 }
1031
1032 result.rules = crate::rules_inject::inject_rules_for_agent(&home, agent_name);
1033
1034 if let Ok(path) = crate::rules_inject::install_skill_for_agent(&home, agent_name) {
1035 result.skill_installed = path.exists();
1036 }
1037
1038 result
1039}
1040
1041pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
1042 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
1043 let binary = resolve_portable_binary();
1044
1045 let targets = agent_mcp_targets(agent, &home)?;
1046
1047 let mut errors = Vec::new();
1048 for t in &targets {
1049 if let Err(e) = crate::core::editor_registry::write_config_with_options(
1050 t,
1051 &binary,
1052 WriteOptions {
1053 overwrite_invalid: true,
1054 },
1055 ) {
1056 eprintln!(
1057 "\x1b[33m⚠\x1b[0m Could not configure {}: {}",
1058 t.config_path.display(),
1059 e
1060 );
1061 errors.push(e);
1062 }
1063 }
1064
1065 if agent == "kiro" {
1066 install_kiro_steering(&home);
1067 }
1068
1069 if agent == "vscode" || agent == "copilot" {
1070 if let Err(e) = crate::core::editor_registry::plan_mode::write_vscode_plan_settings() {
1071 eprintln!("\x1b[33m⚠\x1b[0m VS Code plan mode: {e}");
1072 }
1073 }
1074 if agent == "claude" || agent == "claude-code" {
1075 if let Err(e) =
1076 crate::core::editor_registry::plan_mode::write_claude_code_plan_permissions()
1077 {
1078 eprintln!("\x1b[33m⚠\x1b[0m Claude Code plan mode: {e}");
1079 }
1080 }
1081
1082 if errors.is_empty() {
1083 Ok(())
1084 } else {
1085 Err(format!(
1086 "{} config(s) could not be written. See warnings above.",
1087 errors.len()
1088 ))
1089 }
1090}
1091
1092fn agent_mcp_targets(agent: &str, home: &std::path::Path) -> Result<Vec<EditorTarget>, String> {
1093 let mut targets = Vec::<EditorTarget>::new();
1094
1095 let push = |targets: &mut Vec<EditorTarget>,
1096 name: &'static str,
1097 config_path: PathBuf,
1098 config_type: ConfigType| {
1099 targets.push(EditorTarget {
1100 name,
1101 agent_key: agent.to_string(),
1102 detect_path: PathBuf::from("/nonexistent"), config_path,
1104 config_type,
1105 });
1106 };
1107
1108 let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
1109
1110 match agent {
1111 "cursor" => push(
1112 &mut targets,
1113 "Cursor",
1114 home.join(".cursor/mcp.json"),
1115 ConfigType::McpJson,
1116 ),
1117 "claude" | "claude-code" => push(
1118 &mut targets,
1119 "Claude Code",
1120 crate::core::editor_registry::claude_mcp_json_path(home),
1121 ConfigType::McpJson,
1122 ),
1123 "augment" => {
1124 push(
1125 &mut targets,
1126 "Augment CLI",
1127 crate::core::editor_registry::augment_cli_settings_path(home),
1128 ConfigType::McpJson,
1129 );
1130 push(
1131 &mut targets,
1132 "Augment (VS Code)",
1133 crate::core::editor_registry::augment_vscode_mcp_path(home),
1134 ConfigType::AugmentVsCode,
1135 );
1136 }
1137 "windsurf" => push(
1138 &mut targets,
1139 "Windsurf",
1140 home.join(".codeium/windsurf/mcp_config.json"),
1141 ConfigType::McpJson,
1142 ),
1143 "codex" => {
1144 let codex_dir =
1145 crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
1146 push(
1147 &mut targets,
1148 "Codex CLI",
1149 codex_dir.join("config.toml"),
1150 ConfigType::Codex,
1151 );
1152 }
1153 "gemini" => {
1154 push(
1155 &mut targets,
1156 "Gemini CLI",
1157 home.join(".gemini/settings.json"),
1158 ConfigType::GeminiSettings,
1159 );
1160 push(
1161 &mut targets,
1162 "Antigravity IDE",
1163 home.join(".gemini/antigravity/mcp_config.json"),
1164 ConfigType::McpJson,
1165 );
1166 push(
1167 &mut targets,
1168 "Antigravity CLI",
1169 home.join(".gemini/antigravity-cli/mcp_config.json"),
1170 ConfigType::McpJson,
1171 );
1172 }
1173 "antigravity" => push(
1174 &mut targets,
1175 "Antigravity IDE",
1176 home.join(".gemini/antigravity/mcp_config.json"),
1177 ConfigType::McpJson,
1178 ),
1179 "antigravity-cli" => push(
1180 &mut targets,
1181 "Antigravity CLI",
1182 home.join(".gemini/antigravity-cli/mcp_config.json"),
1183 ConfigType::McpJson,
1184 ),
1185 "copilot" => push(
1186 &mut targets,
1187 "Copilot CLI",
1188 home.join(".copilot/mcp-config.json"),
1189 ConfigType::CopilotCli,
1190 ),
1191 "crush" => push(
1192 &mut targets,
1193 "Crush",
1194 home.join(".config/crush/crush.json"),
1195 ConfigType::Crush,
1196 ),
1197 "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
1198 "qoder" => {
1199 for path in crate::core::editor_registry::qoder_all_mcp_paths(home) {
1200 push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
1201 }
1202 }
1203 "qoderwork" => push(
1204 &mut targets,
1205 "QoderWork",
1206 crate::core::editor_registry::qoderwork_mcp_path(home),
1207 ConfigType::McpJson,
1208 ),
1209 "cline" => push(
1210 &mut targets,
1211 "Cline",
1212 crate::core::editor_registry::cline_mcp_path(),
1213 ConfigType::McpJson,
1214 ),
1215 "roo" => push(
1216 &mut targets,
1217 "Roo Code",
1218 crate::core::editor_registry::roo_mcp_path(),
1219 ConfigType::McpJson,
1220 ),
1221 "kiro" => push(
1222 &mut targets,
1223 "AWS Kiro",
1224 home.join(".kiro/settings/mcp.json"),
1225 ConfigType::McpJson,
1226 ),
1227 "verdent" => push(
1228 &mut targets,
1229 "Verdent",
1230 home.join(".verdent/mcp.json"),
1231 ConfigType::McpJson,
1232 ),
1233 "jetbrains" | "amp" | "openclaw" => {
1234 }
1236 "qwen" => push(
1237 &mut targets,
1238 "Qwen Code",
1239 home.join(".qwen/settings.json"),
1240 ConfigType::McpJson,
1241 ),
1242 "trae" => push(
1243 &mut targets,
1244 "Trae",
1245 home.join(".trae/mcp.json"),
1246 ConfigType::McpJson,
1247 ),
1248 "amazonq" => push(
1249 &mut targets,
1250 "Amazon Q Developer",
1251 home.join(".aws/amazonq/default.json"),
1252 ConfigType::McpJson,
1253 ),
1254 "opencode" => {
1255 #[cfg(windows)]
1256 let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
1257 std::path::PathBuf::from(appdata)
1258 .join("opencode")
1259 .join("opencode.json")
1260 } else {
1261 home.join(".config/opencode/opencode.json")
1262 };
1263 #[cfg(not(windows))]
1264 let opencode_path = home.join(".config/opencode/opencode.json");
1265 push(
1266 &mut targets,
1267 "OpenCode",
1268 opencode_path,
1269 ConfigType::OpenCode,
1270 );
1271 }
1272 "hermes" => push(
1273 &mut targets,
1274 "Hermes Agent",
1275 home.join(".hermes/config.yaml"),
1276 ConfigType::HermesYaml,
1277 ),
1278 "vscode" => push(
1279 &mut targets,
1280 "VS Code",
1281 crate::core::editor_registry::vscode_mcp_path(),
1282 ConfigType::VsCodeMcp,
1283 ),
1284 "zed" => push(
1285 &mut targets,
1286 "Zed",
1287 crate::core::editor_registry::zed_settings_path(home),
1288 ConfigType::Zed,
1289 ),
1290 "aider" => push(
1291 &mut targets,
1292 "Aider",
1293 home.join(".aider/mcp.json"),
1294 ConfigType::McpJson,
1295 ),
1296 "continue" => push(
1297 &mut targets,
1298 "Continue",
1299 home.join(".continue/mcp.json"),
1300 ConfigType::McpJson,
1301 ),
1302 "neovim" => push(
1303 &mut targets,
1304 "Neovim (mcphub.nvim)",
1305 home.join(".config/mcphub/servers.json"),
1306 ConfigType::McpJson,
1307 ),
1308 "emacs" => push(
1309 &mut targets,
1310 "Emacs (mcp.el)",
1311 home.join(".emacs.d/mcp.json"),
1312 ConfigType::McpJson,
1313 ),
1314 "sublime" => push(
1315 &mut targets,
1316 "Sublime Text",
1317 home.join(".config/sublime-text/mcp.json"),
1318 ConfigType::McpJson,
1319 ),
1320 _ => {
1321 return Err(format!("Unknown agent '{agent}'"));
1322 }
1323 }
1324
1325 Ok(targets)
1326}
1327
1328pub fn disable_agent_mcp(agent: &str, overwrite_invalid: bool) -> Result<(), String> {
1329 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
1330
1331 let mut targets = Vec::<EditorTarget>::new();
1332
1333 let push = |targets: &mut Vec<EditorTarget>,
1334 name: &'static str,
1335 config_path: PathBuf,
1336 config_type: ConfigType| {
1337 targets.push(EditorTarget {
1338 name,
1339 agent_key: agent.to_string(),
1340 detect_path: PathBuf::from("/nonexistent"),
1341 config_path,
1342 config_type,
1343 });
1344 };
1345
1346 let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
1347
1348 match agent {
1349 "cursor" => push(
1350 &mut targets,
1351 "Cursor",
1352 home.join(".cursor/mcp.json"),
1353 ConfigType::McpJson,
1354 ),
1355 "claude" | "claude-code" => push(
1356 &mut targets,
1357 "Claude Code",
1358 crate::core::editor_registry::claude_mcp_json_path(&home),
1359 ConfigType::McpJson,
1360 ),
1361 "augment" => {
1362 push(
1363 &mut targets,
1364 "Augment CLI",
1365 crate::core::editor_registry::augment_cli_settings_path(&home),
1366 ConfigType::McpJson,
1367 );
1368 push(
1369 &mut targets,
1370 "Augment (VS Code)",
1371 crate::core::editor_registry::augment_vscode_mcp_path(&home),
1372 ConfigType::AugmentVsCode,
1373 );
1374 }
1375 "windsurf" => push(
1376 &mut targets,
1377 "Windsurf",
1378 home.join(".codeium/windsurf/mcp_config.json"),
1379 ConfigType::McpJson,
1380 ),
1381 "codex" => {
1382 let codex_dir =
1383 crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
1384 push(
1385 &mut targets,
1386 "Codex CLI",
1387 codex_dir.join("config.toml"),
1388 ConfigType::Codex,
1389 );
1390 }
1391 "gemini" => {
1392 push(
1393 &mut targets,
1394 "Gemini CLI",
1395 home.join(".gemini/settings.json"),
1396 ConfigType::GeminiSettings,
1397 );
1398 push(
1399 &mut targets,
1400 "Antigravity IDE",
1401 home.join(".gemini/antigravity/mcp_config.json"),
1402 ConfigType::McpJson,
1403 );
1404 push(
1405 &mut targets,
1406 "Antigravity CLI",
1407 home.join(".gemini/antigravity-cli/mcp_config.json"),
1408 ConfigType::McpJson,
1409 );
1410 }
1411 "antigravity" => push(
1412 &mut targets,
1413 "Antigravity IDE",
1414 home.join(".gemini/antigravity/mcp_config.json"),
1415 ConfigType::McpJson,
1416 ),
1417 "antigravity-cli" => push(
1418 &mut targets,
1419 "Antigravity CLI",
1420 home.join(".gemini/antigravity-cli/mcp_config.json"),
1421 ConfigType::McpJson,
1422 ),
1423 "copilot" => push(
1424 &mut targets,
1425 "Copilot CLI",
1426 home.join(".copilot/mcp-config.json"),
1427 ConfigType::CopilotCli,
1428 ),
1429 "crush" => push(
1430 &mut targets,
1431 "Crush",
1432 home.join(".config/crush/crush.json"),
1433 ConfigType::Crush,
1434 ),
1435 "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
1436 "qoder" => {
1437 for path in crate::core::editor_registry::qoder_all_mcp_paths(&home) {
1438 push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
1439 }
1440 }
1441 "qoderwork" => push(
1442 &mut targets,
1443 "QoderWork",
1444 crate::core::editor_registry::qoderwork_mcp_path(&home),
1445 ConfigType::McpJson,
1446 ),
1447 "cline" => push(
1448 &mut targets,
1449 "Cline",
1450 crate::core::editor_registry::cline_mcp_path(),
1451 ConfigType::McpJson,
1452 ),
1453 "roo" => push(
1454 &mut targets,
1455 "Roo Code",
1456 crate::core::editor_registry::roo_mcp_path(),
1457 ConfigType::McpJson,
1458 ),
1459 "kiro" => push(
1460 &mut targets,
1461 "AWS Kiro",
1462 home.join(".kiro/settings/mcp.json"),
1463 ConfigType::McpJson,
1464 ),
1465 "verdent" => push(
1466 &mut targets,
1467 "Verdent",
1468 home.join(".verdent/mcp.json"),
1469 ConfigType::McpJson,
1470 ),
1471 "jetbrains" | "amp" | "openclaw" => {
1472 }
1474 "qwen" => push(
1475 &mut targets,
1476 "Qwen Code",
1477 home.join(".qwen/settings.json"),
1478 ConfigType::McpJson,
1479 ),
1480 "trae" => push(
1481 &mut targets,
1482 "Trae",
1483 home.join(".trae/mcp.json"),
1484 ConfigType::McpJson,
1485 ),
1486 "amazonq" => push(
1487 &mut targets,
1488 "Amazon Q Developer",
1489 home.join(".aws/amazonq/default.json"),
1490 ConfigType::McpJson,
1491 ),
1492 "opencode" => {
1493 #[cfg(windows)]
1494 let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
1495 std::path::PathBuf::from(appdata)
1496 .join("opencode")
1497 .join("opencode.json")
1498 } else {
1499 home.join(".config/opencode/opencode.json")
1500 };
1501 #[cfg(not(windows))]
1502 let opencode_path = home.join(".config/opencode/opencode.json");
1503 push(
1504 &mut targets,
1505 "OpenCode",
1506 opencode_path,
1507 ConfigType::OpenCode,
1508 );
1509 }
1510 "hermes" => push(
1511 &mut targets,
1512 "Hermes Agent",
1513 home.join(".hermes/config.yaml"),
1514 ConfigType::HermesYaml,
1515 ),
1516 "vscode" => push(
1517 &mut targets,
1518 "VS Code",
1519 crate::core::editor_registry::vscode_mcp_path(),
1520 ConfigType::VsCodeMcp,
1521 ),
1522 "zed" => push(
1523 &mut targets,
1524 "Zed",
1525 crate::core::editor_registry::zed_settings_path(&home),
1526 ConfigType::Zed,
1527 ),
1528 "aider" => push(
1529 &mut targets,
1530 "Aider",
1531 home.join(".aider/mcp.json"),
1532 ConfigType::McpJson,
1533 ),
1534 "continue" => push(
1535 &mut targets,
1536 "Continue",
1537 home.join(".continue/mcp.json"),
1538 ConfigType::McpJson,
1539 ),
1540 "neovim" => push(
1541 &mut targets,
1542 "Neovim (mcphub.nvim)",
1543 home.join(".config/mcphub/servers.json"),
1544 ConfigType::McpJson,
1545 ),
1546 "emacs" => push(
1547 &mut targets,
1548 "Emacs (mcp.el)",
1549 home.join(".emacs.d/mcp.json"),
1550 ConfigType::McpJson,
1551 ),
1552 "sublime" => push(
1553 &mut targets,
1554 "Sublime Text",
1555 home.join(".config/sublime-text/mcp.json"),
1556 ConfigType::McpJson,
1557 ),
1558 _ => {
1559 return Err(format!("Unknown agent '{agent}'"));
1560 }
1561 }
1562
1563 for t in &targets {
1564 crate::core::editor_registry::remove_lean_ctx_server(
1565 t,
1566 WriteOptions { overwrite_invalid },
1567 )?;
1568 }
1569
1570 Ok(())
1571}
1572
1573pub fn install_skill_files(home: &std::path::Path) -> Vec<(String, bool)> {
1574 crate::rules_inject::install_all_skills(home)
1575}
1576
1577fn install_kiro_steering(home: &std::path::Path) {
1578 let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
1579 let steering_dir = cwd.join(".kiro").join("steering");
1580 let steering_file = steering_dir.join("lean-ctx.md");
1581
1582 if steering_file.exists()
1583 && std::fs::read_to_string(&steering_file)
1584 .unwrap_or_default()
1585 .contains("lean-ctx")
1586 {
1587 println!(" Kiro steering file already exists at .kiro/steering/lean-ctx.md");
1588 return;
1589 }
1590
1591 let _ = std::fs::create_dir_all(&steering_dir);
1592 let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
1593 println!(" \x1b[32m✓\x1b[0m Created .kiro/steering/lean-ctx.md (Kiro will now prefer lean-ctx tools)");
1594}
1595
1596fn configure_plan_mode_settings(newly_configured: &[&str], already_configured: &[&str]) {
1597 use crate::terminal_ui;
1598
1599 let all_configured: Vec<&str> = newly_configured
1600 .iter()
1601 .chain(already_configured.iter())
1602 .copied()
1603 .collect();
1604
1605 let has_vscode = all_configured.contains(&"VS Code");
1606 let has_claude = all_configured.contains(&"Claude Code");
1607
1608 if !has_vscode && !has_claude {
1609 return;
1610 }
1611
1612 if has_vscode {
1613 match crate::core::editor_registry::plan_mode::write_vscode_plan_settings() {
1614 Ok(r) if r.action == WriteAction::Already => {
1615 terminal_ui::print_status_ok(
1616 "VS Code \x1b[2mplan mode already configured\x1b[0m",
1617 );
1618 }
1619 Ok(_) => {
1620 terminal_ui::print_status_new(
1621 "VS Code \x1b[2mplan mode tools configured\x1b[0m",
1622 );
1623 }
1624 Err(e) => {
1625 terminal_ui::print_status_warn(&format!("VS Code plan mode: {e}"));
1626 }
1627 }
1628 }
1629
1630 if has_claude {
1631 match crate::core::editor_registry::plan_mode::write_claude_code_plan_permissions() {
1632 Ok(r) if r.action == WriteAction::Already => {
1633 terminal_ui::print_status_ok(
1634 "Claude Code \x1b[2mplan mode permissions present\x1b[0m",
1635 );
1636 }
1637 Ok(_) => {
1638 terminal_ui::print_status_new(
1639 "Claude Code \x1b[2mplan mode permissions added\x1b[0m",
1640 );
1641 }
1642 Err(e) => {
1643 terminal_ui::print_status_warn(&format!("Claude Code plan mode: {e}"));
1644 }
1645 }
1646 }
1647}
1648
1649fn shorten_path(path: &str, home: &str) -> String {
1650 if let Some(stripped) = path.strip_prefix(home) {
1651 format!("~{stripped}")
1652 } else {
1653 path.to_string()
1654 }
1655}
1656
1657fn upsert_toml_key(content: &mut String, key: &str, value: &str) {
1658 let pattern = format!("{key} = ");
1659 if let Some(start) = content.find(&pattern) {
1660 let line_end = content[start..]
1661 .find('\n')
1662 .map_or(content.len(), |p| start + p);
1663 content.replace_range(start..line_end, &format!("{key} = \"{value}\""));
1664 } else {
1665 if !content.is_empty() && !content.ends_with('\n') {
1666 content.push('\n');
1667 }
1668 content.push_str(&format!("{key} = \"{value}\"\n"));
1669 }
1670}
1671
1672fn remove_toml_key(content: &mut String, key: &str) {
1673 let pattern = format!("{key} = ");
1674 if let Some(start) = content.find(&pattern) {
1675 let line_end = content[start..]
1676 .find('\n')
1677 .map_or(content.len(), |p| start + p + 1);
1678 content.replace_range(start..line_end, "");
1679 }
1680}
1681
1682fn configure_premium_features(home: &std::path::Path) {
1683 use crate::terminal_ui;
1684 use std::io::Write;
1685
1686 let config_dir = crate::core::data_dir::lean_ctx_data_dir()
1687 .unwrap_or_else(|_| home.join(".config/lean-ctx"));
1688 let _ = std::fs::create_dir_all(&config_dir);
1689 let config_path = config_dir.join("config.toml");
1690 let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
1691
1692 let dim = "\x1b[2m";
1693 let bold = "\x1b[1m";
1694 let cyan = "\x1b[36m";
1695 let rst = "\x1b[0m";
1696
1697 println!("\n {bold}Compression Level{rst} {dim}(controls all token optimization layers){rst}");
1699 println!(" {dim}Applies to tool output, agent prompts, and protocol mode.{rst}");
1700 println!();
1701 println!(" {cyan}off{rst} — No compression (full verbose output)");
1702 println!(" {cyan}lite{rst} — Light: concise output, basic terse filtering {dim}(~25% savings){rst}");
1703 println!(" {cyan}standard{rst} — Dense output + compact protocol + pattern-aware {dim}(~45% savings){rst}");
1704 println!(" {cyan}max{rst} — Expert mode: TDD protocol, all layers active {dim}(~65% savings){rst}");
1705 println!();
1706 print!(" Compression level? {bold}[off/lite/standard/max]{rst} {dim}(default: off){rst} ");
1707 std::io::stdout().flush().ok();
1708
1709 let mut level_input = String::new();
1710 let level = if std::io::stdin().read_line(&mut level_input).is_ok() {
1711 match level_input.trim().to_lowercase().as_str() {
1712 "lite" => "lite",
1713 "standard" | "std" => "standard",
1714 "max" => "max",
1715 _ => "off",
1716 }
1717 } else {
1718 "off"
1719 };
1720
1721 let effective_level = if level != "off" {
1722 upsert_toml_key(&mut config_content, "compression_level", level);
1723 remove_toml_key(&mut config_content, "terse_agent");
1724 remove_toml_key(&mut config_content, "output_density");
1725 terminal_ui::print_status_ok(&format!("Compression: {level}"));
1726 crate::core::config::CompressionLevel::from_str_label(level)
1727 } else if config_content.contains("compression_level") {
1728 upsert_toml_key(&mut config_content, "compression_level", "off");
1729 terminal_ui::print_status_ok("Compression: off");
1730 Some(crate::core::config::CompressionLevel::Off)
1731 } else {
1732 terminal_ui::print_status_skip(
1733 "Compression: off (change later with: lean-ctx compression <level>)",
1734 );
1735 Some(crate::core::config::CompressionLevel::Off)
1736 };
1737
1738 if let Some(lvl) = effective_level {
1739 let n = crate::core::terse::rules_inject::inject(&lvl);
1740 if n > 0 {
1741 terminal_ui::print_status_ok(&format!(
1742 "Updated {n} rules file(s) with compression prompt"
1743 ));
1744 }
1745 }
1746
1747 println!(
1749 "\n {bold}Tool Result Archive{rst} {dim}(zero-loss: large outputs archived, retrievable via ctx_expand){rst}"
1750 );
1751 print!(" Enable auto-archive? {bold}[Y/n]{rst} ");
1752 std::io::stdout().flush().ok();
1753
1754 let mut archive_input = String::new();
1755 let archive_on = if std::io::stdin().read_line(&mut archive_input).is_ok() {
1756 let a = archive_input.trim().to_lowercase();
1757 a.is_empty() || a == "y" || a == "yes"
1758 } else {
1759 true
1760 };
1761
1762 if archive_on && !config_content.contains("[archive]") {
1763 if !config_content.is_empty() && !config_content.ends_with('\n') {
1764 config_content.push('\n');
1765 }
1766 config_content.push_str("\n[archive]\nenabled = true\n");
1767 terminal_ui::print_status_ok("Tool Result Archive: enabled");
1768 } else if !archive_on {
1769 terminal_ui::print_status_skip("Archive: off (enable later in config.toml)");
1770 }
1771
1772 let _ = std::fs::write(&config_path, config_content);
1773}
1774
1775#[cfg(all(test, target_os = "macos"))]
1776mod tests {
1777 use super::*;
1778
1779 #[test]
1780 #[cfg(target_os = "macos")]
1781 fn qoder_agent_targets_include_all_macos_mcp_locations() {
1782 let home = std::path::Path::new("/Users/tester");
1783 let targets = agent_mcp_targets("qoder", home).unwrap();
1784 let paths: Vec<_> = targets.iter().map(|t| t.config_path.as_path()).collect();
1785
1786 assert_eq!(
1787 paths,
1788 vec![
1789 home.join(".qoder/mcp.json").as_path(),
1790 home.join("Library/Application Support/Qoder/User/mcp.json")
1791 .as_path(),
1792 home.join("Library/Application Support/Qoder/SharedClientCache/mcp.json")
1793 .as_path(),
1794 ]
1795 );
1796 assert!(targets
1797 .iter()
1798 .all(|t| t.config_type == ConfigType::QoderSettings));
1799 }
1800}