1use std::path::PathBuf;
2
3pub mod agents;
4mod support;
5
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum HookMode {
13 #[default]
14 Mcp,
15 Hybrid,
16}
17
18impl std::fmt::Display for HookMode {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 match self {
21 Self::Mcp => write!(f, "MCP"),
22 Self::Hybrid => write!(f, "Hybrid"),
23 }
24 }
25}
26
27impl HookMode {
28 pub fn from_str_loose(s: &str) -> Option<Self> {
29 match s.to_lowercase().replace('-', "").as_str() {
30 "mcp" => Some(Self::Mcp),
31 "hybrid" => Some(Self::Hybrid),
32 _ => None,
33 }
34 }
35
36 pub fn description(&self) -> &'static str {
37 match self {
38 Self::Mcp => "MCP server only (extension/plugin-based agents without reliable shell)",
39 Self::Hybrid => "MCP server + shell hooks for command compression (best of both)",
40 }
41 }
42}
43
44pub fn recommend_hook_mode(agent_key: &str) -> HookMode {
51 match agent_key {
52 "cursor" | "gemini" | "codex" | "claude" | "claude-code" | "crush" | "hermes"
59 | "opencode" | "openclaw" | "pi" | "qoder" | "windsurf" | "amp" | "cline" | "roo"
60 | "copilot" | "kiro" | "qwen" | "trae" | "antigravity" | "antigravity-cli" | "amazonq"
61 | "verdent" => HookMode::Hybrid,
62
63 _ => HookMode::Mcp,
65 }
66}
67use agents::{
68 install_amp_hook, install_antigravity_cli_hook, install_antigravity_hook,
69 install_claude_hook_config, install_claude_hook_scripts, install_claude_hook_with_mode,
70 install_claude_project_hooks, install_cline_rules, install_codex_hook, install_copilot_hook,
71 install_crush_hook_with_mode, install_cursor_hook_config, install_cursor_hook_scripts,
72 install_cursor_hook_with_mode, install_gemini_hook, install_gemini_hook_config,
73 install_gemini_hook_scripts, install_hermes_hook_with_mode, install_jetbrains_hook,
74 install_kiro_hook, install_openclaw_hook, install_opencode_hook_with_mode,
75 install_pi_hook_with_mode, install_qoder_hook_with_mode, install_windsurf_rules,
76};
77use support::{
78 ensure_codex_hooks_enabled, install_codex_instruction_docs, install_named_json_server,
79 upsert_lean_ctx_codex_hook_entries,
80};
81
82fn mcp_server_quiet_mode() -> bool {
83 std::env::var_os("LEAN_CTX_MCP_SERVER").is_some()
84 || matches!(std::env::var("LEAN_CTX_QUIET"), Ok(value) if value.trim() == "1")
85}
86
87pub fn refresh_installed_hooks() {
90 let Some(home) = crate::core::home::resolve_home_dir() else {
91 return;
92 };
93
94 let claude_dir = crate::setup::claude_config_dir(&home);
95 let claude_hooks = claude_dir.join("hooks/lean-ctx-rewrite.sh").exists()
96 || claude_dir.join("settings.json").exists()
97 && std::fs::read_to_string(claude_dir.join("settings.json"))
98 .unwrap_or_default()
99 .contains("lean-ctx");
100
101 if claude_hooks {
102 install_claude_hook_scripts(&home);
103 install_claude_hook_config(&home);
104 }
105
106 let cursor_hooks = home.join(".cursor/hooks/lean-ctx-rewrite.sh").exists()
107 || home.join(".cursor/hooks.json").exists()
108 && std::fs::read_to_string(home.join(".cursor/hooks.json"))
109 .unwrap_or_default()
110 .contains("lean-ctx");
111
112 if cursor_hooks {
113 install_cursor_hook_scripts(&home);
114 install_cursor_hook_config(&home);
115 }
116
117 let gemini_rewrite = home.join(".gemini/hooks/lean-ctx-rewrite-gemini.sh");
118 let gemini_legacy = home.join(".gemini/hooks/lean-ctx-hook-gemini.sh");
119 if gemini_rewrite.exists() || gemini_legacy.exists() {
120 install_gemini_hook_scripts(&home);
121 install_gemini_hook_config(&home);
122 }
123
124 let codex_dir = crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
125 let codex_hooks = codex_dir.join("hooks/lean-ctx-rewrite-codex.sh").exists()
126 || codex_dir.join("hooks.json").exists()
127 && std::fs::read_to_string(codex_dir.join("hooks.json"))
128 .unwrap_or_default()
129 .contains("lean-ctx");
130
131 if codex_hooks {
132 install_codex_hook();
133 }
134}
135
136fn resolve_binary_path() -> String {
137 if is_lean_ctx_in_path() {
138 return "lean-ctx".to_string();
139 }
140 crate::core::portable_binary::resolve_portable_binary()
141}
142
143fn is_lean_ctx_in_path() -> bool {
144 let which_cmd = if cfg!(windows) { "where" } else { "which" };
145 std::process::Command::new(which_cmd)
146 .arg("lean-ctx")
147 .stdout(std::process::Stdio::null())
148 .stderr(std::process::Stdio::null())
149 .status()
150 .is_ok_and(|s| s.success())
151}
152
153fn resolve_binary_path_for_bash() -> String {
154 let path = resolve_binary_path();
155 to_bash_compatible_path(&path)
156}
157
158pub fn to_bash_compatible_path(path: &str) -> String {
159 let path = match crate::core::pathutil::strip_verbatim_str(path) {
160 Some(stripped) => stripped,
161 None => path.replace('\\', "/"),
162 };
163 if path.len() >= 2 && path.as_bytes()[1] == b':' {
164 let drive = (path.as_bytes()[0] as char).to_ascii_lowercase();
165 format!("/{drive}{}", &path[2..])
166 } else {
167 path
168 }
169}
170
171pub fn from_bash_to_native_path(path: &str) -> String {
174 crate::core::pathutil::normalize_tool_path(path)
175}
176
177pub fn normalize_tool_path(path: &str) -> String {
180 crate::core::pathutil::normalize_tool_path(path)
181}
182
183pub fn generate_rewrite_script(binary: &str) -> String {
184 let case_pattern = crate::rewrite_registry::bash_case_pattern();
185 format!(
186 r#"#!/usr/bin/env bash
187# lean-ctx PreToolUse hook — rewrites bash commands to lean-ctx equivalents
188set -euo pipefail
189
190LEAN_CTX_BIN="{binary}"
191
192INPUT=$(cat)
193TOOL=$(echo "$INPUT" | grep -oE '"tool_name":"([^"\\]|\\.)*"' | head -1 | sed 's/^"tool_name":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
194
195case "$TOOL" in
196 Bash|bash|PowerShell|powershell) ;;
197 *) exit 0 ;;
198esac
199
200CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
201
202if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then
203 exit 0
204fi
205
206case "$CMD" in
207 {case_pattern})
208 # Shell-escape then JSON-escape (two passes)
209 SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
210 REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
211 JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
212 printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD"
213 ;;
214 *) exit 0 ;;
215esac
216"#
217 )
218}
219
220pub fn generate_compact_rewrite_script(binary: &str) -> String {
221 let case_pattern = crate::rewrite_registry::bash_case_pattern();
222 format!(
223 r#"#!/usr/bin/env bash
224# lean-ctx hook — rewrites shell commands
225set -euo pipefail
226LEAN_CTX_BIN="{binary}"
227INPUT=$(cat)
228CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g' 2>/dev/null || echo "")
229if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then exit 0; fi
230case "$CMD" in
231 {case_pattern})
232 SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
233 REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
234 JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
235 printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD" ;;
236 *) exit 0 ;;
237esac
238"#
239 )
240}
241
242const REDIRECT_SCRIPT_CLAUDE: &str = r"#!/usr/bin/env bash
243# lean-ctx PreToolUse hook — all native tools pass through
244# Read/Grep/ListFiles are allowed so Edit (which requires native Read) works.
245# The MCP instructions guide the AI to prefer ctx_read/ctx_search/ctx_tree.
246exit 0
247";
248
249const REDIRECT_SCRIPT_GENERIC: &str = r"#!/usr/bin/env bash
250# lean-ctx hook — all native tools pass through
251exit 0
252";
253
254pub(crate) const HYBRID_RULES: &str = "\
255# lean-ctx — Hybrid Mode (MCP reads + CLI commands)
256
257Use MCP tools for reads (cache benefit), CLI commands for everything else (no schema overhead):
258
259## MCP tools (keep using):
260| Tool | Why MCP |
261|------|---------|
262| `ctx_read(path, mode)` | In-process cache, re-reads ~13 tokens |
263
264## CLI commands (via Shell/Bash):
265| USE (via Shell/Bash) | INSTEAD OF (MCP) | Why |
266|---------------------|-------------------|-----|
267| `lean-ctx -c \"<cmd>\"` | `ctx_shell` | No MCP schema overhead |
268| `lean-ctx grep <pattern> [path]` | `ctx_search` | No MCP schema overhead |
269| `lean-ctx ls [path]` | `ctx_tree` | No MCP schema overhead |
270
271## File editing:
272Use native Edit/StrReplace — lean-ctx only handles READ operations.
273Write, Delete, Glob → use normally.
274";
275
276pub fn install_project_rules() {
277 install_project_rules_for_agents(&[]);
278}
279
280pub fn install_project_rules_for_agents(agents: &[&str]) {
283 if crate::core::config::Config::load().rules_scope_effective()
284 == crate::core::config::RulesScope::Global
285 {
286 return;
287 }
288
289 let cwd = std::env::current_dir().unwrap_or_default();
290
291 if !is_inside_git_repo(&cwd) {
292 eprintln!(
293 " Skipping project files: not inside a git repository.\n \
294 Run this command from your project root to create CLAUDE.md / AGENTS.md."
295 );
296 return;
297 }
298
299 let home = crate::core::home::resolve_home_dir().unwrap_or_default();
300 if cwd == home {
301 eprintln!(
302 " Skipping project files: current directory is your home folder.\n \
303 Run this command from a project directory instead."
304 );
305 return;
306 }
307
308 let all = agents.is_empty();
309 let wants = |name: &str| all || agents.iter().any(|a| a.eq_ignore_ascii_case(name));
310
311 ensure_project_agents_integration(&cwd);
312
313 if wants("cursor") || wants("windsurf") {
314 let cursorrules = cwd.join(".cursorrules");
315 if !cursorrules.exists()
316 || !std::fs::read_to_string(&cursorrules)
317 .unwrap_or_default()
318 .contains("lean-ctx")
319 {
320 let content = CURSORRULES_TEMPLATE;
321 if cursorrules.exists() {
322 let mut existing = std::fs::read_to_string(&cursorrules).unwrap_or_default();
323 if !existing.ends_with('\n') {
324 existing.push('\n');
325 }
326 existing.push('\n');
327 existing.push_str(content);
328 write_file(&cursorrules, &existing);
329 } else {
330 write_file(&cursorrules, content);
331 }
332 if !mcp_server_quiet_mode() {
333 eprintln!("Created/updated .cursorrules in project root.");
334 }
335 }
336 }
337
338 if wants("claude") {
339 let claude_rules_dir = cwd.join(".claude").join("rules");
340 let claude_rules_file = claude_rules_dir.join("lean-ctx.md");
341 if !claude_rules_file.exists()
342 || !std::fs::read_to_string(&claude_rules_file)
343 .unwrap_or_default()
344 .contains(crate::rules_inject::RULES_VERSION_STR)
345 {
346 let _ = std::fs::create_dir_all(&claude_rules_dir);
347 write_file(
348 &claude_rules_file,
349 crate::rules_inject::rules_dedicated_markdown(),
350 );
351 if !mcp_server_quiet_mode() {
352 eprintln!("Created .claude/rules/lean-ctx.md (Claude Code project rules).");
353 }
354 }
355
356 install_claude_project_hooks(&cwd);
357 }
358
359 if wants("kiro") {
360 let kiro_dir = cwd.join(".kiro");
361 if kiro_dir.exists() {
362 let steering_dir = kiro_dir.join("steering");
363 let steering_file = steering_dir.join("lean-ctx.md");
364 if !steering_file.exists()
365 || !std::fs::read_to_string(&steering_file)
366 .unwrap_or_default()
367 .contains("lean-ctx")
368 {
369 let _ = std::fs::create_dir_all(&steering_dir);
370 write_file(&steering_file, KIRO_STEERING_TEMPLATE);
371 if !mcp_server_quiet_mode() {
372 eprintln!("Created .kiro/steering/lean-ctx.md (Kiro steering).");
373 }
374 }
375 }
376 }
377}
378
379const PROJECT_LEAN_CTX_MD_MARKER: &str = "<!-- lean-ctx-owned: PROJECT-LEAN-CTX.md v1 -->";
380const PROJECT_LEAN_CTX_MD: &str = "LEAN-CTX.md";
381const PROJECT_AGENTS_MD: &str = "AGENTS.md";
382const AGENTS_BLOCK_START: &str = "<!-- lean-ctx -->";
383const AGENTS_BLOCK_END: &str = "<!-- /lean-ctx -->";
384
385fn ensure_project_agents_integration(cwd: &std::path::Path) {
386 let lean_ctx_md = cwd.join(PROJECT_LEAN_CTX_MD);
387 let desired = format!(
388 "{PROJECT_LEAN_CTX_MD_MARKER}\n{}\n",
389 crate::rules_inject::rules_dedicated_markdown()
390 );
391
392 if !lean_ctx_md.exists() {
393 write_file(&lean_ctx_md, &desired);
394 } else if std::fs::read_to_string(&lean_ctx_md)
395 .unwrap_or_default()
396 .contains(PROJECT_LEAN_CTX_MD_MARKER)
397 {
398 let current = std::fs::read_to_string(&lean_ctx_md).unwrap_or_default();
399 if !current.contains(crate::rules_inject::RULES_VERSION_STR) {
400 write_file(&lean_ctx_md, &desired);
401 }
402 }
403
404 let block = format!(
405 "{AGENTS_BLOCK_START}\n\
406## lean-ctx\n\n\
407Prefer lean-ctx MCP tools over native equivalents for token savings.\n\
408Full rules: @{PROJECT_LEAN_CTX_MD}\n\
409{AGENTS_BLOCK_END}\n"
410 );
411
412 let agents_md = cwd.join(PROJECT_AGENTS_MD);
413 if !agents_md.exists() {
414 let content = format!("# Agent Instructions\n\n{block}");
415 write_file(&agents_md, &content);
416 if !mcp_server_quiet_mode() {
417 eprintln!("Created AGENTS.md in project root (lean-ctx reference only).");
418 }
419 return;
420 }
421
422 let existing = std::fs::read_to_string(&agents_md).unwrap_or_default();
423
424 if existing.contains("CLI-first Token Optimization for Pi")
425 && !existing.contains(AGENTS_BLOCK_START)
426 {
427 let content = format!("# Agent Instructions\n\n{block}");
428 write_file(&agents_md, &content);
429 return;
430 }
431
432 if existing.contains(AGENTS_BLOCK_START) {
433 let updated = crate::marked_block::replace_marked_block(
434 &existing,
435 AGENTS_BLOCK_START,
436 AGENTS_BLOCK_END,
437 &block,
438 );
439 if updated != existing {
440 write_file(&agents_md, &updated);
441 }
442 return;
443 }
444
445 if existing.contains("lean-ctx") && existing.contains(PROJECT_LEAN_CTX_MD) {
446 return;
447 }
448
449 let mut out = existing;
450 if !out.ends_with('\n') {
451 out.push('\n');
452 }
453 out.push('\n');
454 out.push_str(&block);
455 write_file(&agents_md, &out);
456 if !mcp_server_quiet_mode() {
457 eprintln!("Updated AGENTS.md (added lean-ctx reference block).");
458 }
459}
460
461const CURSORRULES_TEMPLATE: &str = "\
462# lean-ctx — Context Engineering Layer
463
464CRITICAL: ALWAYS use lean-ctx tools instead of native equivalents. This is NOT optional.
465
466| MUST USE | NEVER USE | Why |
467|----------|-----------|-----|
468| `ctx_read(path, mode)` | `Read` / `cat` / `head` / `tail` | Cached, 10 read modes, re-reads ~13 tokens |
469| `ctx_search(pattern, path)` | `Grep` / `rg` | Compact, token-efficient results |
470| `lean-ctx -c \"<cmd>\"` (via Shell) | `ctx_shell` / raw `Shell` | CLI compression, no MCP overhead |
471
472File editing: use native Edit/StrReplace. Write, Delete, Glob → use normally.
473NEVER loop on Edit failures — switch to ctx_edit immediately.
474REMINDER: You MUST use lean-ctx tools. NEVER use native Read, Grep, or Shell directly.
475";
476
477pub const KIRO_STEERING_TEMPLATE: &str = "\
478---
479inclusion: always
480---
481
482# lean-ctx — Context Engineering Layer
483
484The workspace has the `lean-ctx` MCP server installed. You MUST prefer lean-ctx tools over native equivalents for token efficiency and caching.
485
486## Mandatory Tool Preferences
487
488| Use this | Instead of | Why |
489|----------|-----------|-----|
490| `mcp_lean_ctx_ctx_read` | `readFile`, `readCode` | Cached reads, 10 compression modes, re-reads cost ~13 tokens |
491| `mcp_lean_ctx_ctx_multi_read` | `readMultipleFiles` | Batch cached reads in one call |
492| `mcp_lean_ctx_ctx_shell` | `executeBash` | Pattern compression for git/npm/test output |
493| `mcp_lean_ctx_ctx_search` | `grepSearch` | Compact, .gitignore-aware results |
494| `mcp_lean_ctx_ctx_tree` | `listDirectory` | Compact directory maps with file counts |
495
496## When to use native Kiro tools instead
497
498- `fsWrite` / `fsAppend` — always use native (lean-ctx doesn't write files)
499- `strReplace` — always use native (precise string replacement)
500- `semanticRename` / `smartRelocate` — always use native (IDE integration)
501- `getDiagnostics` — always use native (language server diagnostics)
502- `deleteFile` — always use native
503
504## Session management
505
506- At the start of a long task, call `mcp_lean_ctx_ctx_preload` with a task description to warm the cache
507- Use `mcp_lean_ctx_ctx_compress` periodically in long conversations to checkpoint context
508- Use `mcp_lean_ctx_ctx_knowledge` to persist important discoveries across sessions
509
510## Rules
511
512- NEVER loop on edit failures — switch to `mcp_lean_ctx_ctx_edit` immediately
513- For large files, use `mcp_lean_ctx_ctx_read` with `mode: \"signatures\"` or `mode: \"map\"` first
514- For re-reading a file you already read, just call `mcp_lean_ctx_ctx_read` again (cache hit = ~13 tokens)
515- When running tests or build commands, use `mcp_lean_ctx_ctx_shell` for compressed output
516";
517
518pub fn install_agent_hook(agent: &str, global: bool) {
519 install_agent_hook_with_mode(agent, global, HookMode::Mcp);
520}
521
522pub fn install_agent_hook_with_mode(agent: &str, global: bool, mode: HookMode) {
523 let home = crate::core::home::resolve_home_dir().unwrap_or_default();
524 match agent {
525 "claude" | "claude-code" => install_claude_hook_with_mode(global, mode),
526 "cursor" => install_cursor_hook_with_mode(global, mode),
527 "gemini" => install_gemini_hook(),
528 "antigravity" => install_antigravity_hook(),
529 "antigravity-cli" => install_antigravity_cli_hook(),
530 "augment" => install_mcp_json_agent(
531 "Augment CLI",
532 "~/.augment/settings.json",
533 &crate::core::editor_registry::augment_cli_settings_path(&home),
534 ),
535 "codex" => install_codex_hook(),
536 "windsurf" => install_windsurf_rules(global),
537 "cline" | "roo" => install_cline_rules(global),
538 "copilot" | "vscode" => install_copilot_hook(global),
539 "pi" => install_pi_hook_with_mode(global, mode),
540 "qoder" => install_qoder_hook_with_mode(mode),
541 "qoderwork" => install_mcp_json_agent(
542 "QoderWork",
543 "~/.qoderwork/mcp.json",
544 &home.join(".qoderwork/mcp.json"),
545 ),
546 "qwen" => install_mcp_json_agent(
547 "Qwen Code",
548 "~/.qwen/settings.json",
549 &home.join(".qwen/settings.json"),
550 ),
551 "trae" => install_mcp_json_agent("Trae", "~/.trae/mcp.json", &home.join(".trae/mcp.json")),
552 "amazonq" => install_mcp_json_agent(
553 "Amazon Q Developer",
554 "~/.aws/amazonq/default.json",
555 &home.join(".aws/amazonq/default.json"),
556 ),
557 "jetbrains" => install_jetbrains_hook(),
558 "kiro" => install_kiro_hook(),
559 "verdent" => install_mcp_json_agent(
560 "Verdent",
561 "~/.verdent/mcp.json",
562 &home.join(".verdent/mcp.json"),
563 ),
564 "opencode" => install_opencode_hook_with_mode(mode),
565 "amp" => install_amp_hook(),
566 "crush" => install_crush_hook_with_mode(mode),
567 "openclaw" => install_openclaw_hook(),
568 "hermes" => install_hermes_hook_with_mode(global, mode),
569 "zed" => {
570 let zed_path = crate::core::editor_registry::zed_settings_path(&home);
571 let binary = resolve_binary_path();
572 let entry = full_server_entry(&binary);
573 install_named_json_server("Zed", "settings.json", &zed_path, "context_servers", entry);
574 }
575 "aider" => {
576 install_mcp_json_agent("Aider", "~/.aider/mcp.json", &home.join(".aider/mcp.json"));
577 }
578 "continue" => install_mcp_json_agent(
579 "Continue",
580 "~/.continue/mcp.json",
581 &home.join(".continue/mcp.json"),
582 ),
583 "neovim" => install_mcp_json_agent(
584 "Neovim (mcphub.nvim)",
585 "~/.config/mcphub/servers.json",
586 &home.join(".config/mcphub/servers.json"),
587 ),
588 "emacs" => install_mcp_json_agent(
589 "Emacs (mcp.el)",
590 "~/.emacs.d/mcp.json",
591 &home.join(".emacs.d/mcp.json"),
592 ),
593 "sublime" => install_mcp_json_agent(
594 "Sublime Text",
595 "~/.config/sublime-text/mcp.json",
596 &home.join(".config/sublime-text/mcp.json"),
597 ),
598 _ => {
599 eprintln!("Unknown agent: {agent}");
600 eprintln!(" Supported: aider, amazonq, amp, antigravity, antigravity-cli, augment,");
601 eprintln!(" claude, cline, codex, continue, copilot, crush, cursor, emacs, gemini,");
602 eprintln!(" hermes, jetbrains, kiro, neovim, openclaw, opencode, pi, qoder,");
603 eprintln!(" qoderwork, qwen, roo, sublime, trae, verdent, vscode, windsurf, zed");
604 std::process::exit(1);
605 }
606 }
607}
608
609pub fn install_agent_project_hooks(agent: &str, cwd: &std::path::Path) {
610 match agent {
611 "claude" | "claude-code" => agents::install_claude_project_hooks(cwd),
612 _ => {}
613 }
614}
615
616fn write_file(path: &std::path::Path, content: &str) {
617 if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
618 tracing::error!("Error writing {}: {e}", path.display());
619 }
620}
621
622fn is_inside_git_repo(path: &std::path::Path) -> bool {
623 let mut p = path;
624 loop {
625 if p.join(".git").exists() {
626 return true;
627 }
628 match p.parent() {
629 Some(parent) => p = parent,
630 None => return false,
631 }
632 }
633}
634
635#[cfg(unix)]
636fn make_executable(path: &PathBuf) {
637 use std::os::unix::fs::PermissionsExt;
638 let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
639}
640
641#[cfg(not(unix))]
642fn make_executable(_path: &PathBuf) {}
643
644fn full_server_entry(binary: &str) -> serde_json::Value {
645 let data_dir = crate::core::data_dir::lean_ctx_data_dir()
646 .map(|d| d.to_string_lossy().to_string())
647 .unwrap_or_default();
648 serde_json::json!({
649 "command": binary,
650 "env": {
651 "LEAN_CTX_DATA_DIR": data_dir,
652 "LEAN_CTX_FULL_TOOLS": "1"
653 }
654 })
655}
656
657pub(crate) fn install_mcp_json_agent(
658 name: &str,
659 display_path: &str,
660 config_path: &std::path::Path,
661) {
662 let binary = resolve_binary_path();
663 let entry = full_server_entry(&binary);
664 install_named_json_server(name, display_path, config_path, "mcpServers", entry);
665}
666
667#[cfg(test)]
668mod tests {
669 use super::*;
670
671 #[test]
672 fn bash_path_unix_unchanged() {
673 assert_eq!(
674 to_bash_compatible_path("/usr/local/bin/lean-ctx"),
675 "/usr/local/bin/lean-ctx"
676 );
677 }
678
679 #[test]
680 fn bash_path_home_unchanged() {
681 assert_eq!(
682 to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
683 "/home/user/.cargo/bin/lean-ctx"
684 );
685 }
686
687 #[test]
688 fn bash_path_windows_drive_converted() {
689 assert_eq!(
690 to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
691 "/c/Users/Fraser/bin/lean-ctx.exe"
692 );
693 }
694
695 #[test]
696 fn bash_path_windows_lowercase_drive() {
697 assert_eq!(
698 to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
699 "/d/tools/lean-ctx.exe"
700 );
701 }
702
703 #[test]
704 fn bash_path_windows_forward_slashes() {
705 assert_eq!(
706 to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
707 "/c/Users/Fraser/bin/lean-ctx.exe"
708 );
709 }
710
711 #[test]
712 fn bash_path_bare_name_unchanged() {
713 assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
714 }
715
716 #[test]
717 fn normalize_msys2_path() {
718 assert_eq!(
719 normalize_tool_path("/c/Users/game/Downloads/project"),
720 "C:/Users/game/Downloads/project"
721 );
722 }
723
724 #[test]
725 fn normalize_msys2_drive_d() {
726 assert_eq!(
727 normalize_tool_path("/d/Projects/app/src"),
728 "D:/Projects/app/src"
729 );
730 }
731
732 #[test]
733 fn normalize_backslashes() {
734 assert_eq!(
735 normalize_tool_path("C:\\Users\\game\\project\\src"),
736 "C:/Users/game/project/src"
737 );
738 }
739
740 #[test]
741 fn normalize_mixed_separators() {
742 assert_eq!(
743 normalize_tool_path("C:\\Users/game\\project/src"),
744 "C:/Users/game/project/src"
745 );
746 }
747
748 #[test]
749 fn normalize_double_slashes() {
750 assert_eq!(
751 normalize_tool_path("/home/user//project///src"),
752 "/home/user/project/src"
753 );
754 }
755
756 #[test]
757 fn normalize_trailing_slash() {
758 assert_eq!(
759 normalize_tool_path("/home/user/project/"),
760 "/home/user/project"
761 );
762 }
763
764 #[test]
765 fn normalize_root_preserved() {
766 assert_eq!(normalize_tool_path("/"), "/");
767 }
768
769 #[test]
770 fn normalize_windows_root_preserved() {
771 assert_eq!(normalize_tool_path("C:/"), "C:/");
772 }
773
774 #[test]
775 fn normalize_unix_path_unchanged() {
776 assert_eq!(
777 normalize_tool_path("/home/user/project/src/main.rs"),
778 "/home/user/project/src/main.rs"
779 );
780 }
781
782 #[test]
783 fn normalize_relative_path_unchanged() {
784 assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
785 }
786
787 #[test]
788 fn normalize_dot_unchanged() {
789 assert_eq!(normalize_tool_path("."), ".");
790 }
791
792 #[test]
793 fn normalize_unc_path_preserved() {
794 assert_eq!(
795 normalize_tool_path("//server/share/file"),
796 "//server/share/file"
797 );
798 }
799
800 #[test]
801 fn cursor_hook_config_has_version_and_object_hooks() {
802 let config = serde_json::json!({
803 "version": 1,
804 "hooks": {
805 "preToolUse": [
806 {
807 "matcher": "terminal_command",
808 "command": "lean-ctx hook rewrite"
809 },
810 {
811 "matcher": "read_file|grep|search|list_files|list_directory",
812 "command": "lean-ctx hook redirect"
813 }
814 ]
815 }
816 });
817
818 let json_str = serde_json::to_string_pretty(&config).unwrap();
819 let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
820
821 assert_eq!(parsed["version"], 1);
822 assert!(parsed["hooks"].is_object());
823 assert!(parsed["hooks"]["preToolUse"].is_array());
824 assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
825 assert_eq!(
826 parsed["hooks"]["preToolUse"][0]["matcher"],
827 "terminal_command"
828 );
829 }
830
831 #[test]
832 fn cursor_hook_detects_old_format_needs_migration() {
833 let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
834 let has_correct =
835 old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
836 assert!(
837 !has_correct,
838 "Old format should be detected as needing migration"
839 );
840 }
841
842 #[test]
843 fn gemini_hook_config_has_type_command() {
844 let binary = "lean-ctx";
845 let rewrite_cmd = format!("{binary} hook rewrite");
846 let redirect_cmd = format!("{binary} hook redirect");
847
848 let hook_config = serde_json::json!({
849 "hooks": {
850 "BeforeTool": [
851 {
852 "hooks": [{
853 "type": "command",
854 "command": rewrite_cmd
855 }]
856 },
857 {
858 "hooks": [{
859 "type": "command",
860 "command": redirect_cmd
861 }]
862 }
863 ]
864 }
865 });
866
867 let parsed = hook_config;
868 let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
869 assert_eq!(before_tool.len(), 2);
870
871 let first_hook = &before_tool[0]["hooks"][0];
872 assert_eq!(first_hook["type"], "command");
873 assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
874
875 let second_hook = &before_tool[1]["hooks"][0];
876 assert_eq!(second_hook["type"], "command");
877 assert_eq!(second_hook["command"], "lean-ctx hook redirect");
878 }
879
880 #[test]
881 fn gemini_hook_old_format_detected() {
882 let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
883 let has_new = old_format.contains("hook rewrite")
884 && old_format.contains("hook redirect")
885 && old_format.contains("\"type\"");
886 assert!(!has_new, "Missing 'type' field should trigger migration");
887 }
888
889 #[test]
890 fn rewrite_script_uses_registry_pattern() {
891 let script = generate_rewrite_script("/usr/bin/lean-ctx");
892 assert!(script.contains(r"git\ *"), "script missing git pattern");
893 assert!(script.contains(r"cargo\ *"), "script missing cargo pattern");
894 assert!(script.contains(r"npm\ *"), "script missing npm pattern");
895 assert!(script.contains(r"rg\ *"), "script missing rg pattern");
896 assert!(script.contains(r"ls\ *"), "script missing ls pattern");
897 assert!(
898 script.contains("LEAN_CTX_BIN=\"/usr/bin/lean-ctx\""),
899 "script missing binary path"
900 );
901 assert!(
902 script.contains("PowerShell|powershell"),
903 "rewrite script must accept PowerShell tool names for Windows compatibility"
904 );
905 }
906
907 #[test]
908 fn compact_rewrite_script_uses_registry_pattern() {
909 let script = generate_compact_rewrite_script("/usr/bin/lean-ctx");
910 assert!(script.contains(r"git\ *"), "compact script missing git");
911 assert!(script.contains(r"cargo\ *"), "compact script missing cargo");
912 assert!(script.contains(r"rg\ *"), "compact script missing rg");
913 }
914
915 #[test]
916 fn rewrite_scripts_contain_all_registry_commands() {
917 let script = generate_rewrite_script("lean-ctx");
918 let compact = generate_compact_rewrite_script("lean-ctx");
919 for entry in crate::rewrite_registry::REWRITE_COMMANDS {
920 if matches!(entry.category, crate::rewrite_registry::Category::FileRead) {
921 continue;
922 }
923 let pattern = if entry.command.contains('-') {
924 format!("{}*", entry.command.replace('-', r"\-"))
925 } else {
926 format!(r"{}\ *", entry.command)
927 };
928 assert!(
929 script.contains(&pattern),
930 "rewrite_script missing '{}' (pattern: {})",
931 entry.command,
932 pattern
933 );
934 assert!(
935 compact.contains(&pattern),
936 "compact_rewrite_script missing '{}' (pattern: {})",
937 entry.command,
938 pattern
939 );
940 }
941 }
942
943 #[test]
944 fn codex_is_hybrid() {
945 assert_eq!(recommend_hook_mode("codex"), HookMode::Hybrid);
946 }
947
948 #[test]
949 fn cursor_is_hybrid() {
950 assert_eq!(recommend_hook_mode("cursor"), HookMode::Hybrid);
951 }
952
953 #[test]
954 fn gemini_is_hybrid() {
955 assert_eq!(recommend_hook_mode("gemini"), HookMode::Hybrid);
956 }
957
958 #[test]
959 fn claude_is_hybrid() {
960 assert_eq!(recommend_hook_mode("claude"), HookMode::Hybrid);
961 }
962
963 #[test]
964 fn unknown_agent_falls_back_to_mcp() {
965 assert_eq!(recommend_hook_mode("unknown-agent"), HookMode::Mcp);
966 }
967
968 #[test]
969 fn from_bash_to_native_converts_msys_drive() {
970 assert_eq!(
971 from_bash_to_native_path("/c/Users/ABC/lean-ctx"),
972 "C:/Users/ABC/lean-ctx"
973 );
974 }
975
976 #[test]
977 fn from_bash_to_native_drive_d() {
978 assert_eq!(
979 from_bash_to_native_path("/d/Program Files/lean-ctx.exe"),
980 "D:/Program Files/lean-ctx.exe"
981 );
982 }
983
984 #[test]
985 fn from_bash_to_native_unix_path_unchanged() {
986 assert_eq!(
987 from_bash_to_native_path("/usr/local/bin/lean-ctx"),
988 "/usr/local/bin/lean-ctx"
989 );
990 }
991
992 #[test]
993 fn from_bash_to_native_bare_name() {
994 assert_eq!(from_bash_to_native_path("lean-ctx"), "lean-ctx");
995 }
996
997 #[test]
998 fn roundtrip_windows_path() {
999 let native = r"C:\Users\ABC\AppData\Local\lean-ctx\lean-ctx.exe";
1000 let bash = to_bash_compatible_path(native);
1001 assert_eq!(bash, "/c/Users/ABC/AppData/Local/lean-ctx/lean-ctx.exe");
1002 let back = from_bash_to_native_path(&bash);
1003 assert_eq!(back, "C:/Users/ABC/AppData/Local/lean-ctx/lean-ctx.exe");
1004 }
1005
1006 #[test]
1007 fn roundtrip_unix_path() {
1008 let native = "/usr/local/bin/lean-ctx";
1009 let bash = to_bash_compatible_path(native);
1010 assert_eq!(bash, native);
1011 let back = from_bash_to_native_path(&bash);
1012 assert_eq!(back, native);
1013 }
1014}