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 = resolve_portable_binary();
949
950 #[cfg(unix)]
951 {
952 let mut cmd = std::process::Command::new("nice");
953 cmd.args(["-n", "19"]);
954 if which_ionice_available() {
955 cmd.arg("ionice").args(["-c", "3"]);
956 }
957 cmd.arg(&binary)
958 .args(["index", "build-graph", "--root"])
959 .arg(root)
960 .stdout(std::process::Stdio::null())
961 .stderr(std::process::Stdio::null())
962 .stdin(std::process::Stdio::null());
963 let _ = cmd.spawn();
964 }
965
966 #[cfg(windows)]
967 {
968 use std::os::windows::process::CommandExt;
969 const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
970 const CREATE_NO_WINDOW: u32 = 0x0800_0000;
971 let _ = std::process::Command::new(&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 .creation_flags(CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW)
978 .spawn();
979 }
980}
981
982#[cfg(unix)]
983fn which_ionice_available() -> bool {
984 std::process::Command::new("ionice")
985 .arg("--version")
986 .stdout(std::process::Stdio::null())
987 .stderr(std::process::Stdio::null())
988 .status()
989 .is_ok()
990}
991
992#[derive(Debug, Default)]
994pub struct AgentSetupResult {
995 pub mcp_ok: bool,
996 pub rules: crate::rules_inject::InjectResult,
997 pub skill_installed: bool,
998 pub errors: Vec<String>,
999}
1000
1001pub fn setup_single_agent(
1004 agent_name: &str,
1005 global: bool,
1006 mode: crate::hooks::HookMode,
1007) -> AgentSetupResult {
1008 let home = dirs::home_dir().unwrap_or_default();
1009 let mut result = AgentSetupResult::default();
1010
1011 crate::hooks::install_agent_hook_with_mode(agent_name, global, mode);
1012
1013 match configure_agent_mcp(agent_name) {
1014 Ok(()) => result.mcp_ok = true,
1015 Err(e) => result.errors.push(format!("MCP config: {e}")),
1016 }
1017
1018 result.rules = crate::rules_inject::inject_rules_for_agent(&home, agent_name);
1019
1020 if let Ok(path) = crate::rules_inject::install_skill_for_agent(&home, agent_name) {
1021 result.skill_installed = path.exists();
1022 }
1023
1024 result
1025}
1026
1027pub fn configure_agent_mcp(agent: &str) -> Result<(), String> {
1028 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
1029 let binary = resolve_portable_binary();
1030
1031 let targets = agent_mcp_targets(agent, &home)?;
1032
1033 let mut errors = Vec::new();
1034 for t in &targets {
1035 if let Err(e) = crate::core::editor_registry::write_config_with_options(
1036 t,
1037 &binary,
1038 WriteOptions {
1039 overwrite_invalid: true,
1040 },
1041 ) {
1042 eprintln!(
1043 "\x1b[33m⚠\x1b[0m Could not configure {}: {}",
1044 t.config_path.display(),
1045 e
1046 );
1047 errors.push(e);
1048 }
1049 }
1050
1051 if agent == "kiro" {
1052 install_kiro_steering(&home);
1053 }
1054
1055 if errors.is_empty() {
1056 Ok(())
1057 } else {
1058 Err(format!(
1059 "{} config(s) could not be written. See warnings above.",
1060 errors.len()
1061 ))
1062 }
1063}
1064
1065fn agent_mcp_targets(agent: &str, home: &std::path::Path) -> Result<Vec<EditorTarget>, String> {
1066 let mut targets = Vec::<EditorTarget>::new();
1067
1068 let push = |targets: &mut Vec<EditorTarget>,
1069 name: &'static str,
1070 config_path: PathBuf,
1071 config_type: ConfigType| {
1072 targets.push(EditorTarget {
1073 name,
1074 agent_key: agent.to_string(),
1075 detect_path: PathBuf::from("/nonexistent"), config_path,
1077 config_type,
1078 });
1079 };
1080
1081 let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
1082
1083 match agent {
1084 "cursor" => push(
1085 &mut targets,
1086 "Cursor",
1087 home.join(".cursor/mcp.json"),
1088 ConfigType::McpJson,
1089 ),
1090 "claude" | "claude-code" => push(
1091 &mut targets,
1092 "Claude Code",
1093 crate::core::editor_registry::claude_mcp_json_path(home),
1094 ConfigType::McpJson,
1095 ),
1096 "windsurf" => push(
1097 &mut targets,
1098 "Windsurf",
1099 home.join(".codeium/windsurf/mcp_config.json"),
1100 ConfigType::McpJson,
1101 ),
1102 "codex" => {
1103 let codex_dir =
1104 crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
1105 push(
1106 &mut targets,
1107 "Codex CLI",
1108 codex_dir.join("config.toml"),
1109 ConfigType::Codex,
1110 );
1111 }
1112 "gemini" => {
1113 push(
1114 &mut targets,
1115 "Gemini CLI",
1116 home.join(".gemini/settings.json"),
1117 ConfigType::GeminiSettings,
1118 );
1119 push(
1120 &mut targets,
1121 "Antigravity",
1122 home.join(".gemini/antigravity/mcp_config.json"),
1123 ConfigType::McpJson,
1124 );
1125 }
1126 "antigravity" => push(
1127 &mut targets,
1128 "Antigravity",
1129 home.join(".gemini/antigravity/mcp_config.json"),
1130 ConfigType::McpJson,
1131 ),
1132 "copilot" => push(
1133 &mut targets,
1134 "Copilot CLI",
1135 home.join(".copilot/mcp-config.json"),
1136 ConfigType::CopilotCli,
1137 ),
1138 "crush" => push(
1139 &mut targets,
1140 "Crush",
1141 home.join(".config/crush/crush.json"),
1142 ConfigType::Crush,
1143 ),
1144 "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
1145 "qoder" => {
1146 for path in crate::core::editor_registry::qoder_all_mcp_paths(home) {
1147 push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
1148 }
1149 }
1150 "qoderwork" => push(
1151 &mut targets,
1152 "QoderWork",
1153 crate::core::editor_registry::qoderwork_mcp_path(home),
1154 ConfigType::McpJson,
1155 ),
1156 "cline" => push(
1157 &mut targets,
1158 "Cline",
1159 crate::core::editor_registry::cline_mcp_path(),
1160 ConfigType::McpJson,
1161 ),
1162 "roo" => push(
1163 &mut targets,
1164 "Roo Code",
1165 crate::core::editor_registry::roo_mcp_path(),
1166 ConfigType::McpJson,
1167 ),
1168 "kiro" => push(
1169 &mut targets,
1170 "AWS Kiro",
1171 home.join(".kiro/settings/mcp.json"),
1172 ConfigType::McpJson,
1173 ),
1174 "verdent" => push(
1175 &mut targets,
1176 "Verdent",
1177 home.join(".verdent/mcp.json"),
1178 ConfigType::McpJson,
1179 ),
1180 "jetbrains" | "amp" => {
1181 }
1183 "qwen" => push(
1184 &mut targets,
1185 "Qwen Code",
1186 home.join(".qwen/settings.json"),
1187 ConfigType::McpJson,
1188 ),
1189 "trae" => push(
1190 &mut targets,
1191 "Trae",
1192 home.join(".trae/mcp.json"),
1193 ConfigType::McpJson,
1194 ),
1195 "amazonq" => push(
1196 &mut targets,
1197 "Amazon Q Developer",
1198 home.join(".aws/amazonq/default.json"),
1199 ConfigType::McpJson,
1200 ),
1201 "opencode" => {
1202 #[cfg(windows)]
1203 let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
1204 std::path::PathBuf::from(appdata)
1205 .join("opencode")
1206 .join("opencode.json")
1207 } else {
1208 home.join(".config/opencode/opencode.json")
1209 };
1210 #[cfg(not(windows))]
1211 let opencode_path = home.join(".config/opencode/opencode.json");
1212 push(
1213 &mut targets,
1214 "OpenCode",
1215 opencode_path,
1216 ConfigType::OpenCode,
1217 );
1218 }
1219 "hermes" => push(
1220 &mut targets,
1221 "Hermes Agent",
1222 home.join(".hermes/config.yaml"),
1223 ConfigType::HermesYaml,
1224 ),
1225 "vscode" => push(
1226 &mut targets,
1227 "VS Code",
1228 crate::core::editor_registry::vscode_mcp_path(),
1229 ConfigType::VsCodeMcp,
1230 ),
1231 "zed" => push(
1232 &mut targets,
1233 "Zed",
1234 crate::core::editor_registry::zed_settings_path(home),
1235 ConfigType::Zed,
1236 ),
1237 "aider" => push(
1238 &mut targets,
1239 "Aider",
1240 home.join(".aider/mcp.json"),
1241 ConfigType::McpJson,
1242 ),
1243 "continue" => push(
1244 &mut targets,
1245 "Continue",
1246 home.join(".continue/mcp.json"),
1247 ConfigType::McpJson,
1248 ),
1249 "neovim" => push(
1250 &mut targets,
1251 "Neovim (mcphub.nvim)",
1252 home.join(".config/mcphub/servers.json"),
1253 ConfigType::McpJson,
1254 ),
1255 "emacs" => push(
1256 &mut targets,
1257 "Emacs (mcp.el)",
1258 home.join(".emacs.d/mcp.json"),
1259 ConfigType::McpJson,
1260 ),
1261 "sublime" => push(
1262 &mut targets,
1263 "Sublime Text",
1264 home.join(".config/sublime-text/mcp.json"),
1265 ConfigType::McpJson,
1266 ),
1267 _ => {
1268 return Err(format!("Unknown agent '{agent}'"));
1269 }
1270 }
1271
1272 Ok(targets)
1273}
1274
1275pub fn disable_agent_mcp(agent: &str, overwrite_invalid: bool) -> Result<(), String> {
1276 let home = dirs::home_dir().ok_or_else(|| "Cannot determine home directory".to_string())?;
1277
1278 let mut targets = Vec::<EditorTarget>::new();
1279
1280 let push = |targets: &mut Vec<EditorTarget>,
1281 name: &'static str,
1282 config_path: PathBuf,
1283 config_type: ConfigType| {
1284 targets.push(EditorTarget {
1285 name,
1286 agent_key: agent.to_string(),
1287 detect_path: PathBuf::from("/nonexistent"),
1288 config_path,
1289 config_type,
1290 });
1291 };
1292
1293 let pi_cfg = home.join(".pi").join("agent").join("mcp.json");
1294
1295 match agent {
1296 "cursor" => push(
1297 &mut targets,
1298 "Cursor",
1299 home.join(".cursor/mcp.json"),
1300 ConfigType::McpJson,
1301 ),
1302 "claude" | "claude-code" => push(
1303 &mut targets,
1304 "Claude Code",
1305 crate::core::editor_registry::claude_mcp_json_path(&home),
1306 ConfigType::McpJson,
1307 ),
1308 "windsurf" => push(
1309 &mut targets,
1310 "Windsurf",
1311 home.join(".codeium/windsurf/mcp_config.json"),
1312 ConfigType::McpJson,
1313 ),
1314 "codex" => {
1315 let codex_dir =
1316 crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
1317 push(
1318 &mut targets,
1319 "Codex CLI",
1320 codex_dir.join("config.toml"),
1321 ConfigType::Codex,
1322 );
1323 }
1324 "gemini" => {
1325 push(
1326 &mut targets,
1327 "Gemini CLI",
1328 home.join(".gemini/settings.json"),
1329 ConfigType::GeminiSettings,
1330 );
1331 push(
1332 &mut targets,
1333 "Antigravity",
1334 home.join(".gemini/antigravity/mcp_config.json"),
1335 ConfigType::McpJson,
1336 );
1337 }
1338 "antigravity" => push(
1339 &mut targets,
1340 "Antigravity",
1341 home.join(".gemini/antigravity/mcp_config.json"),
1342 ConfigType::McpJson,
1343 ),
1344 "copilot" => push(
1345 &mut targets,
1346 "Copilot CLI",
1347 home.join(".copilot/mcp-config.json"),
1348 ConfigType::CopilotCli,
1349 ),
1350 "crush" => push(
1351 &mut targets,
1352 "Crush",
1353 home.join(".config/crush/crush.json"),
1354 ConfigType::Crush,
1355 ),
1356 "pi" => push(&mut targets, "Pi Coding Agent", pi_cfg, ConfigType::McpJson),
1357 "qoder" => {
1358 for path in crate::core::editor_registry::qoder_all_mcp_paths(&home) {
1359 push(&mut targets, "Qoder", path, ConfigType::QoderSettings);
1360 }
1361 }
1362 "qoderwork" => push(
1363 &mut targets,
1364 "QoderWork",
1365 crate::core::editor_registry::qoderwork_mcp_path(&home),
1366 ConfigType::McpJson,
1367 ),
1368 "cline" => push(
1369 &mut targets,
1370 "Cline",
1371 crate::core::editor_registry::cline_mcp_path(),
1372 ConfigType::McpJson,
1373 ),
1374 "roo" => push(
1375 &mut targets,
1376 "Roo Code",
1377 crate::core::editor_registry::roo_mcp_path(),
1378 ConfigType::McpJson,
1379 ),
1380 "kiro" => push(
1381 &mut targets,
1382 "AWS Kiro",
1383 home.join(".kiro/settings/mcp.json"),
1384 ConfigType::McpJson,
1385 ),
1386 "verdent" => push(
1387 &mut targets,
1388 "Verdent",
1389 home.join(".verdent/mcp.json"),
1390 ConfigType::McpJson,
1391 ),
1392 "jetbrains" | "amp" => {
1393 }
1395 "qwen" => push(
1396 &mut targets,
1397 "Qwen Code",
1398 home.join(".qwen/settings.json"),
1399 ConfigType::McpJson,
1400 ),
1401 "trae" => push(
1402 &mut targets,
1403 "Trae",
1404 home.join(".trae/mcp.json"),
1405 ConfigType::McpJson,
1406 ),
1407 "amazonq" => push(
1408 &mut targets,
1409 "Amazon Q Developer",
1410 home.join(".aws/amazonq/default.json"),
1411 ConfigType::McpJson,
1412 ),
1413 "opencode" => {
1414 #[cfg(windows)]
1415 let opencode_path = if let Ok(appdata) = std::env::var("APPDATA") {
1416 std::path::PathBuf::from(appdata)
1417 .join("opencode")
1418 .join("opencode.json")
1419 } else {
1420 home.join(".config/opencode/opencode.json")
1421 };
1422 #[cfg(not(windows))]
1423 let opencode_path = home.join(".config/opencode/opencode.json");
1424 push(
1425 &mut targets,
1426 "OpenCode",
1427 opencode_path,
1428 ConfigType::OpenCode,
1429 );
1430 }
1431 "hermes" => push(
1432 &mut targets,
1433 "Hermes Agent",
1434 home.join(".hermes/config.yaml"),
1435 ConfigType::HermesYaml,
1436 ),
1437 "vscode" => push(
1438 &mut targets,
1439 "VS Code",
1440 crate::core::editor_registry::vscode_mcp_path(),
1441 ConfigType::VsCodeMcp,
1442 ),
1443 "zed" => push(
1444 &mut targets,
1445 "Zed",
1446 crate::core::editor_registry::zed_settings_path(&home),
1447 ConfigType::Zed,
1448 ),
1449 "aider" => push(
1450 &mut targets,
1451 "Aider",
1452 home.join(".aider/mcp.json"),
1453 ConfigType::McpJson,
1454 ),
1455 "continue" => push(
1456 &mut targets,
1457 "Continue",
1458 home.join(".continue/mcp.json"),
1459 ConfigType::McpJson,
1460 ),
1461 "neovim" => push(
1462 &mut targets,
1463 "Neovim (mcphub.nvim)",
1464 home.join(".config/mcphub/servers.json"),
1465 ConfigType::McpJson,
1466 ),
1467 "emacs" => push(
1468 &mut targets,
1469 "Emacs (mcp.el)",
1470 home.join(".emacs.d/mcp.json"),
1471 ConfigType::McpJson,
1472 ),
1473 "sublime" => push(
1474 &mut targets,
1475 "Sublime Text",
1476 home.join(".config/sublime-text/mcp.json"),
1477 ConfigType::McpJson,
1478 ),
1479 _ => {
1480 return Err(format!("Unknown agent '{agent}'"));
1481 }
1482 }
1483
1484 for t in &targets {
1485 crate::core::editor_registry::remove_lean_ctx_server(
1486 t,
1487 WriteOptions { overwrite_invalid },
1488 )?;
1489 }
1490
1491 Ok(())
1492}
1493
1494pub fn install_skill_files(home: &std::path::Path) -> Vec<(String, bool)> {
1495 crate::rules_inject::install_all_skills(home)
1496}
1497
1498fn install_kiro_steering(home: &std::path::Path) {
1499 let cwd = std::env::current_dir().unwrap_or_else(|_| home.to_path_buf());
1500 let steering_dir = cwd.join(".kiro").join("steering");
1501 let steering_file = steering_dir.join("lean-ctx.md");
1502
1503 if steering_file.exists()
1504 && std::fs::read_to_string(&steering_file)
1505 .unwrap_or_default()
1506 .contains("lean-ctx")
1507 {
1508 println!(" Kiro steering file already exists at .kiro/steering/lean-ctx.md");
1509 return;
1510 }
1511
1512 let _ = std::fs::create_dir_all(&steering_dir);
1513 let _ = std::fs::write(&steering_file, crate::hooks::KIRO_STEERING_TEMPLATE);
1514 println!(" \x1b[32m✓\x1b[0m Created .kiro/steering/lean-ctx.md (Kiro will now prefer lean-ctx tools)");
1515}
1516
1517fn shorten_path(path: &str, home: &str) -> String {
1518 if let Some(stripped) = path.strip_prefix(home) {
1519 format!("~{stripped}")
1520 } else {
1521 path.to_string()
1522 }
1523}
1524
1525fn upsert_toml_key(content: &mut String, key: &str, value: &str) {
1526 let pattern = format!("{key} = ");
1527 if let Some(start) = content.find(&pattern) {
1528 let line_end = content[start..]
1529 .find('\n')
1530 .map_or(content.len(), |p| start + p);
1531 content.replace_range(start..line_end, &format!("{key} = \"{value}\""));
1532 } else {
1533 if !content.is_empty() && !content.ends_with('\n') {
1534 content.push('\n');
1535 }
1536 content.push_str(&format!("{key} = \"{value}\"\n"));
1537 }
1538}
1539
1540fn remove_toml_key(content: &mut String, key: &str) {
1541 let pattern = format!("{key} = ");
1542 if let Some(start) = content.find(&pattern) {
1543 let line_end = content[start..]
1544 .find('\n')
1545 .map_or(content.len(), |p| start + p + 1);
1546 content.replace_range(start..line_end, "");
1547 }
1548}
1549
1550fn configure_premium_features(home: &std::path::Path) {
1551 use crate::terminal_ui;
1552 use std::io::Write;
1553
1554 let config_dir = crate::core::data_dir::lean_ctx_data_dir()
1555 .unwrap_or_else(|_| home.join(".config/lean-ctx"));
1556 let _ = std::fs::create_dir_all(&config_dir);
1557 let config_path = config_dir.join("config.toml");
1558 let mut config_content = std::fs::read_to_string(&config_path).unwrap_or_default();
1559
1560 let dim = "\x1b[2m";
1561 let bold = "\x1b[1m";
1562 let cyan = "\x1b[36m";
1563 let rst = "\x1b[0m";
1564
1565 println!("\n {bold}Compression Level{rst} {dim}(controls all token optimization layers){rst}");
1567 println!(" {dim}Applies to tool output, agent prompts, and protocol mode.{rst}");
1568 println!();
1569 println!(" {cyan}off{rst} — No compression (full verbose output)");
1570 println!(" {cyan}lite{rst} — Light: concise output, basic terse filtering {dim}(~25% savings){rst}");
1571 println!(" {cyan}standard{rst} — Dense output + compact protocol + pattern-aware {dim}(~45% savings){rst}");
1572 println!(" {cyan}max{rst} — Expert mode: TDD protocol, all layers active {dim}(~65% savings){rst}");
1573 println!();
1574 print!(" Compression level? {bold}[off/lite/standard/max]{rst} {dim}(default: off){rst} ");
1575 std::io::stdout().flush().ok();
1576
1577 let mut level_input = String::new();
1578 let level = if std::io::stdin().read_line(&mut level_input).is_ok() {
1579 match level_input.trim().to_lowercase().as_str() {
1580 "lite" => "lite",
1581 "standard" | "std" => "standard",
1582 "max" => "max",
1583 _ => "off",
1584 }
1585 } else {
1586 "off"
1587 };
1588
1589 let effective_level = if level != "off" {
1590 upsert_toml_key(&mut config_content, "compression_level", level);
1591 remove_toml_key(&mut config_content, "terse_agent");
1592 remove_toml_key(&mut config_content, "output_density");
1593 terminal_ui::print_status_ok(&format!("Compression: {level}"));
1594 crate::core::config::CompressionLevel::from_str_label(level)
1595 } else if config_content.contains("compression_level") {
1596 upsert_toml_key(&mut config_content, "compression_level", "off");
1597 terminal_ui::print_status_ok("Compression: off");
1598 Some(crate::core::config::CompressionLevel::Off)
1599 } else {
1600 terminal_ui::print_status_skip(
1601 "Compression: off (change later with: lean-ctx compression <level>)",
1602 );
1603 Some(crate::core::config::CompressionLevel::Off)
1604 };
1605
1606 if let Some(lvl) = effective_level {
1607 let n = crate::core::terse::rules_inject::inject(&lvl);
1608 if n > 0 {
1609 terminal_ui::print_status_ok(&format!(
1610 "Updated {n} rules file(s) with compression prompt"
1611 ));
1612 }
1613 }
1614
1615 println!(
1617 "\n {bold}Tool Result Archive{rst} {dim}(zero-loss: large outputs archived, retrievable via ctx_expand){rst}"
1618 );
1619 print!(" Enable auto-archive? {bold}[Y/n]{rst} ");
1620 std::io::stdout().flush().ok();
1621
1622 let mut archive_input = String::new();
1623 let archive_on = if std::io::stdin().read_line(&mut archive_input).is_ok() {
1624 let a = archive_input.trim().to_lowercase();
1625 a.is_empty() || a == "y" || a == "yes"
1626 } else {
1627 true
1628 };
1629
1630 if archive_on && !config_content.contains("[archive]") {
1631 if !config_content.is_empty() && !config_content.ends_with('\n') {
1632 config_content.push('\n');
1633 }
1634 config_content.push_str("\n[archive]\nenabled = true\n");
1635 terminal_ui::print_status_ok("Tool Result Archive: enabled");
1636 } else if !archive_on {
1637 terminal_ui::print_status_skip("Archive: off (enable later in config.toml)");
1638 }
1639
1640 let _ = std::fs::write(&config_path, config_content);
1641}
1642
1643#[cfg(all(test, target_os = "macos"))]
1644mod tests {
1645 use super::*;
1646
1647 #[test]
1648 #[cfg(target_os = "macos")]
1649 fn qoder_agent_targets_include_all_macos_mcp_locations() {
1650 let home = std::path::Path::new("/Users/tester");
1651 let targets = agent_mcp_targets("qoder", home).unwrap();
1652 let paths: Vec<_> = targets.iter().map(|t| t.config_path.as_path()).collect();
1653
1654 assert_eq!(
1655 paths,
1656 vec![
1657 home.join(".qoder/mcp.json").as_path(),
1658 home.join("Library/Application Support/Qoder/User/mcp.json")
1659 .as_path(),
1660 home.join("Library/Application Support/Qoder/SharedClientCache/mcp.json")
1661 .as_path(),
1662 ]
1663 );
1664 assert!(targets
1665 .iter()
1666 .all(|t| t.config_type == ConfigType::QoderSettings));
1667 }
1668}