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