Skip to main content

lean_ctx/hooks/
mod.rs

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