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    install_project_rules_for_agents(&[]);
336}
337
338/// Install project rules, optionally scoped to specific agents.
339/// If `agents` is empty, installs for all agents (legacy behavior).
340pub fn install_project_rules_for_agents(agents: &[&str]) {
341    if crate::core::config::Config::load().rules_scope_effective()
342        == crate::core::config::RulesScope::Global
343    {
344        return;
345    }
346
347    let cwd = std::env::current_dir().unwrap_or_default();
348
349    if !is_inside_git_repo(&cwd) {
350        eprintln!(
351            "  Skipping project files: not inside a git repository.\n  \
352             Run this command from your project root to create CLAUDE.md / AGENTS.md."
353        );
354        return;
355    }
356
357    let home = crate::core::home::resolve_home_dir().unwrap_or_default();
358    if cwd == home {
359        eprintln!(
360            "  Skipping project files: current directory is your home folder.\n  \
361             Run this command from a project directory instead."
362        );
363        return;
364    }
365
366    let all = agents.is_empty();
367    let wants = |name: &str| all || agents.iter().any(|a| a.eq_ignore_ascii_case(name));
368
369    ensure_project_agents_integration(&cwd);
370
371    if wants("cursor") || wants("windsurf") {
372        let cursorrules = cwd.join(".cursorrules");
373        if !cursorrules.exists()
374            || !std::fs::read_to_string(&cursorrules)
375                .unwrap_or_default()
376                .contains("lean-ctx")
377        {
378            let content = CURSORRULES_TEMPLATE;
379            if cursorrules.exists() {
380                let mut existing = std::fs::read_to_string(&cursorrules).unwrap_or_default();
381                if !existing.ends_with('\n') {
382                    existing.push('\n');
383                }
384                existing.push('\n');
385                existing.push_str(content);
386                write_file(&cursorrules, &existing);
387            } else {
388                write_file(&cursorrules, content);
389            }
390            if !mcp_server_quiet_mode() {
391                eprintln!("Created/updated .cursorrules in project root.");
392            }
393        }
394    }
395
396    if wants("claude") {
397        let claude_rules_dir = cwd.join(".claude").join("rules");
398        let claude_rules_file = claude_rules_dir.join("lean-ctx.md");
399        if !claude_rules_file.exists()
400            || !std::fs::read_to_string(&claude_rules_file)
401                .unwrap_or_default()
402                .contains(crate::rules_inject::RULES_VERSION_STR)
403        {
404            let _ = std::fs::create_dir_all(&claude_rules_dir);
405            write_file(
406                &claude_rules_file,
407                crate::rules_inject::rules_dedicated_markdown(),
408            );
409            if !mcp_server_quiet_mode() {
410                eprintln!("Created .claude/rules/lean-ctx.md (Claude Code project rules).");
411            }
412        }
413
414        install_claude_project_hooks(&cwd);
415    }
416
417    if wants("kiro") {
418        let kiro_dir = cwd.join(".kiro");
419        if kiro_dir.exists() {
420            let steering_dir = kiro_dir.join("steering");
421            let steering_file = steering_dir.join("lean-ctx.md");
422            if !steering_file.exists()
423                || !std::fs::read_to_string(&steering_file)
424                    .unwrap_or_default()
425                    .contains("lean-ctx")
426            {
427                let _ = std::fs::create_dir_all(&steering_dir);
428                write_file(&steering_file, KIRO_STEERING_TEMPLATE);
429                if !mcp_server_quiet_mode() {
430                    eprintln!("Created .kiro/steering/lean-ctx.md (Kiro steering).");
431                }
432            }
433        }
434    }
435}
436
437const PROJECT_LEAN_CTX_MD_MARKER: &str = "<!-- lean-ctx-owned: PROJECT-LEAN-CTX.md v1 -->";
438const PROJECT_LEAN_CTX_MD: &str = "LEAN-CTX.md";
439const PROJECT_AGENTS_MD: &str = "AGENTS.md";
440const AGENTS_BLOCK_START: &str = "<!-- lean-ctx -->";
441const AGENTS_BLOCK_END: &str = "<!-- /lean-ctx -->";
442
443fn ensure_project_agents_integration(cwd: &std::path::Path) {
444    let lean_ctx_md = cwd.join(PROJECT_LEAN_CTX_MD);
445    let desired = format!(
446        "{PROJECT_LEAN_CTX_MD_MARKER}\n{}\n",
447        crate::rules_inject::rules_dedicated_markdown()
448    );
449
450    if !lean_ctx_md.exists() {
451        write_file(&lean_ctx_md, &desired);
452    } else if std::fs::read_to_string(&lean_ctx_md)
453        .unwrap_or_default()
454        .contains(PROJECT_LEAN_CTX_MD_MARKER)
455    {
456        let current = std::fs::read_to_string(&lean_ctx_md).unwrap_or_default();
457        if !current.contains(crate::rules_inject::RULES_VERSION_STR) {
458            write_file(&lean_ctx_md, &desired);
459        }
460    }
461
462    let block = format!(
463        "{AGENTS_BLOCK_START}\n\
464## lean-ctx\n\n\
465Prefer lean-ctx MCP tools over native equivalents for token savings.\n\
466Full rules: @{PROJECT_LEAN_CTX_MD}\n\
467{AGENTS_BLOCK_END}\n"
468    );
469
470    let agents_md = cwd.join(PROJECT_AGENTS_MD);
471    if !agents_md.exists() {
472        let content = format!("# Agent Instructions\n\n{block}");
473        write_file(&agents_md, &content);
474        if !mcp_server_quiet_mode() {
475            eprintln!("Created AGENTS.md in project root (lean-ctx reference only).");
476        }
477        return;
478    }
479
480    let existing = std::fs::read_to_string(&agents_md).unwrap_or_default();
481    if existing.contains(AGENTS_BLOCK_START) {
482        let updated = replace_marked_block(&existing, AGENTS_BLOCK_START, AGENTS_BLOCK_END, &block);
483        if updated != existing {
484            write_file(&agents_md, &updated);
485        }
486        return;
487    }
488
489    if existing.contains("lean-ctx") && existing.contains(PROJECT_LEAN_CTX_MD) {
490        return;
491    }
492
493    let mut out = existing;
494    if !out.ends_with('\n') {
495        out.push('\n');
496    }
497    out.push('\n');
498    out.push_str(&block);
499    write_file(&agents_md, &out);
500    if !mcp_server_quiet_mode() {
501        eprintln!("Updated AGENTS.md (added lean-ctx reference block).");
502    }
503}
504
505fn replace_marked_block(content: &str, start: &str, end: &str, replacement: &str) -> String {
506    let s = content.find(start);
507    let e = content.find(end);
508    match (s, e) {
509        (Some(si), Some(ei)) if ei >= si => {
510            let after_end = ei + end.len();
511            let before = &content[..si];
512            let after = &content[after_end..];
513            let mut out = String::new();
514            out.push_str(before.trim_end_matches('\n'));
515            out.push('\n');
516            out.push('\n');
517            out.push_str(replacement.trim_end_matches('\n'));
518            out.push('\n');
519            out.push_str(after.trim_start_matches('\n'));
520            out
521        }
522        _ => content.to_string(),
523    }
524}
525
526const CURSORRULES_TEMPLATE: &str = "\
527# lean-ctx — Context Engineering Layer
528
529PREFER lean-ctx MCP tools over native equivalents for token savings:
530
531| PREFER | OVER | Why |
532|--------|------|-----|
533| `ctx_read(path)` | `Read` | Cached, 10 compression modes |
534| `ctx_shell(command)` | `Shell` | Pattern compression |
535| `ctx_search(pattern, path)` | `Grep` | Compact results |
536| `ctx_tree(path, depth)` | `ls` / `find` | Directory maps |
537| `ctx_edit(path, old_string, new_string)` | `Edit` (when Read unavailable) | Search-and-replace without native Read |
538
539Edit files: use native Edit/StrReplace if available. If Edit requires Read and Read is unavailable, use ctx_edit.
540Write, Delete, Glob — use normally. NEVER loop on Edit failures — switch to ctx_edit immediately.
541";
542
543pub const KIRO_STEERING_TEMPLATE: &str = "\
544---
545inclusion: always
546---
547
548# lean-ctx — Context Engineering Layer
549
550The workspace has the `lean-ctx` MCP server installed. You MUST prefer lean-ctx tools over native equivalents for token efficiency and caching.
551
552## Mandatory Tool Preferences
553
554| Use this | Instead of | Why |
555|----------|-----------|-----|
556| `mcp_lean_ctx_ctx_read` | `readFile`, `readCode` | Cached reads, 10 compression modes, re-reads cost ~13 tokens |
557| `mcp_lean_ctx_ctx_multi_read` | `readMultipleFiles` | Batch cached reads in one call |
558| `mcp_lean_ctx_ctx_shell` | `executeBash` | Pattern compression for git/npm/test output |
559| `mcp_lean_ctx_ctx_search` | `grepSearch` | Compact, .gitignore-aware results |
560| `mcp_lean_ctx_ctx_tree` | `listDirectory` | Compact directory maps with file counts |
561
562## When to use native Kiro tools instead
563
564- `fsWrite` / `fsAppend` — always use native (lean-ctx doesn't write files)
565- `strReplace` — always use native (precise string replacement)
566- `semanticRename` / `smartRelocate` — always use native (IDE integration)
567- `getDiagnostics` — always use native (language server diagnostics)
568- `deleteFile` — always use native
569
570## Session management
571
572- At the start of a long task, call `mcp_lean_ctx_ctx_preload` with a task description to warm the cache
573- Use `mcp_lean_ctx_ctx_compress` periodically in long conversations to checkpoint context
574- Use `mcp_lean_ctx_ctx_knowledge` to persist important discoveries across sessions
575
576## Rules
577
578- NEVER loop on edit failures — switch to `mcp_lean_ctx_ctx_edit` immediately
579- For large files, use `mcp_lean_ctx_ctx_read` with `mode: \"signatures\"` or `mode: \"map\"` first
580- For re-reading a file you already read, just call `mcp_lean_ctx_ctx_read` again (cache hit = ~13 tokens)
581- When running tests or build commands, use `mcp_lean_ctx_ctx_shell` for compressed output
582";
583
584pub fn install_agent_hook(agent: &str, global: bool) {
585    install_agent_hook_with_mode(agent, global, HookMode::Mcp);
586}
587
588pub fn install_agent_hook_with_mode(agent: &str, global: bool, mode: HookMode) {
589    let home = crate::core::home::resolve_home_dir().unwrap_or_default();
590    match agent {
591        "claude" | "claude-code" => install_claude_hook_with_mode(global, mode),
592        "cursor" => install_cursor_hook_with_mode(global, mode),
593        "gemini" | "antigravity" => install_gemini_hook(),
594        "codex" => install_codex_hook(),
595        "windsurf" => install_windsurf_rules(global),
596        "cline" | "roo" => install_cline_rules(global),
597        "copilot" | "vscode" => install_copilot_hook(global),
598        "pi" => install_pi_hook_with_mode(global, mode),
599        "qoder" => install_qoder_hook_with_mode(mode),
600        "qoderwork" => install_mcp_json_agent(
601            "QoderWork",
602            "~/.qoderwork/mcp.json",
603            &home.join(".qoderwork/mcp.json"),
604        ),
605        "qwen" => install_mcp_json_agent(
606            "Qwen Code",
607            "~/.qwen/settings.json",
608            &home.join(".qwen/settings.json"),
609        ),
610        "trae" => install_mcp_json_agent("Trae", "~/.trae/mcp.json", &home.join(".trae/mcp.json")),
611        "amazonq" => install_mcp_json_agent(
612            "Amazon Q Developer",
613            "~/.aws/amazonq/default.json",
614            &home.join(".aws/amazonq/default.json"),
615        ),
616        "jetbrains" => install_jetbrains_hook(),
617        "kiro" => install_kiro_hook(),
618        "verdent" => install_mcp_json_agent(
619            "Verdent",
620            "~/.verdent/mcp.json",
621            &home.join(".verdent/mcp.json"),
622        ),
623        "opencode" => install_opencode_hook_with_mode(mode),
624        "amp" => install_amp_hook(),
625        "crush" => install_crush_hook_with_mode(mode),
626        "hermes" => install_hermes_hook_with_mode(global, mode),
627        "zed" => {
628            let zed_path = crate::core::editor_registry::zed_settings_path(&home);
629            let binary = resolve_binary_path();
630            let entry = full_server_entry(&binary);
631            install_named_json_server("Zed", "settings.json", &zed_path, "context_servers", entry);
632        }
633        "aider" => {
634            install_mcp_json_agent("Aider", "~/.aider/mcp.json", &home.join(".aider/mcp.json"));
635        }
636        "continue" => install_mcp_json_agent(
637            "Continue",
638            "~/.continue/mcp.json",
639            &home.join(".continue/mcp.json"),
640        ),
641        "neovim" => install_mcp_json_agent(
642            "Neovim (mcphub.nvim)",
643            "~/.config/mcphub/servers.json",
644            &home.join(".config/mcphub/servers.json"),
645        ),
646        "emacs" => install_mcp_json_agent(
647            "Emacs (mcp.el)",
648            "~/.emacs.d/mcp.json",
649            &home.join(".emacs.d/mcp.json"),
650        ),
651        "sublime" => install_mcp_json_agent(
652            "Sublime Text",
653            "~/.config/sublime-text/mcp.json",
654            &home.join(".config/sublime-text/mcp.json"),
655        ),
656        _ => {
657            eprintln!("Unknown agent: {agent}");
658            eprintln!("  Supported: aider, amazonq, amp, antigravity, claude, cline, codex,");
659            eprintln!("    continue, copilot, crush, cursor, emacs, gemini, hermes, jetbrains,");
660            eprintln!("    kiro, neovim, opencode, pi, qoder, qoderwork, qwen, roo, sublime,");
661            eprintln!("    trae, verdent, vscode, windsurf, zed");
662            std::process::exit(1);
663        }
664    }
665}
666
667pub fn install_agent_project_hooks(agent: &str, cwd: &std::path::Path) {
668    match agent {
669        "claude" | "claude-code" => agents::install_claude_project_hooks(cwd),
670        _ => {}
671    }
672}
673
674fn write_file(path: &std::path::Path, content: &str) {
675    if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
676        tracing::error!("Error writing {}: {e}", path.display());
677    }
678}
679
680fn is_inside_git_repo(path: &std::path::Path) -> bool {
681    let mut p = path;
682    loop {
683        if p.join(".git").exists() {
684            return true;
685        }
686        match p.parent() {
687            Some(parent) => p = parent,
688            None => return false,
689        }
690    }
691}
692
693#[cfg(unix)]
694fn make_executable(path: &PathBuf) {
695    use std::os::unix::fs::PermissionsExt;
696    let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
697}
698
699#[cfg(not(unix))]
700fn make_executable(_path: &PathBuf) {}
701
702fn full_server_entry(binary: &str) -> serde_json::Value {
703    let data_dir = crate::core::data_dir::lean_ctx_data_dir()
704        .map(|d| d.to_string_lossy().to_string())
705        .unwrap_or_default();
706    serde_json::json!({
707        "command": binary,
708        "env": {
709            "LEAN_CTX_DATA_DIR": data_dir,
710            "LEAN_CTX_FULL_TOOLS": "1"
711        }
712    })
713}
714
715fn install_mcp_json_agent(name: &str, display_path: &str, config_path: &std::path::Path) {
716    let binary = resolve_binary_path();
717    let entry = full_server_entry(&binary);
718    install_named_json_server(name, display_path, config_path, "mcpServers", entry);
719}
720
721#[cfg(test)]
722mod tests {
723    use super::*;
724
725    #[test]
726    fn bash_path_unix_unchanged() {
727        assert_eq!(
728            to_bash_compatible_path("/usr/local/bin/lean-ctx"),
729            "/usr/local/bin/lean-ctx"
730        );
731    }
732
733    #[test]
734    fn bash_path_home_unchanged() {
735        assert_eq!(
736            to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
737            "/home/user/.cargo/bin/lean-ctx"
738        );
739    }
740
741    #[test]
742    fn bash_path_windows_drive_converted() {
743        assert_eq!(
744            to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
745            "/c/Users/Fraser/bin/lean-ctx.exe"
746        );
747    }
748
749    #[test]
750    fn bash_path_windows_lowercase_drive() {
751        assert_eq!(
752            to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
753            "/d/tools/lean-ctx.exe"
754        );
755    }
756
757    #[test]
758    fn bash_path_windows_forward_slashes() {
759        assert_eq!(
760            to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
761            "/c/Users/Fraser/bin/lean-ctx.exe"
762        );
763    }
764
765    #[test]
766    fn bash_path_bare_name_unchanged() {
767        assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
768    }
769
770    #[test]
771    fn normalize_msys2_path() {
772        assert_eq!(
773            normalize_tool_path("/c/Users/game/Downloads/project"),
774            "C:/Users/game/Downloads/project"
775        );
776    }
777
778    #[test]
779    fn normalize_msys2_drive_d() {
780        assert_eq!(
781            normalize_tool_path("/d/Projects/app/src"),
782            "D:/Projects/app/src"
783        );
784    }
785
786    #[test]
787    fn normalize_backslashes() {
788        assert_eq!(
789            normalize_tool_path("C:\\Users\\game\\project\\src"),
790            "C:/Users/game/project/src"
791        );
792    }
793
794    #[test]
795    fn normalize_mixed_separators() {
796        assert_eq!(
797            normalize_tool_path("C:\\Users/game\\project/src"),
798            "C:/Users/game/project/src"
799        );
800    }
801
802    #[test]
803    fn normalize_double_slashes() {
804        assert_eq!(
805            normalize_tool_path("/home/user//project///src"),
806            "/home/user/project/src"
807        );
808    }
809
810    #[test]
811    fn normalize_trailing_slash() {
812        assert_eq!(
813            normalize_tool_path("/home/user/project/"),
814            "/home/user/project"
815        );
816    }
817
818    #[test]
819    fn normalize_root_preserved() {
820        assert_eq!(normalize_tool_path("/"), "/");
821    }
822
823    #[test]
824    fn normalize_windows_root_preserved() {
825        assert_eq!(normalize_tool_path("C:/"), "C:/");
826    }
827
828    #[test]
829    fn normalize_unix_path_unchanged() {
830        assert_eq!(
831            normalize_tool_path("/home/user/project/src/main.rs"),
832            "/home/user/project/src/main.rs"
833        );
834    }
835
836    #[test]
837    fn normalize_relative_path_unchanged() {
838        assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
839    }
840
841    #[test]
842    fn normalize_dot_unchanged() {
843        assert_eq!(normalize_tool_path("."), ".");
844    }
845
846    #[test]
847    fn normalize_unc_path_preserved() {
848        assert_eq!(
849            normalize_tool_path("//server/share/file"),
850            "//server/share/file"
851        );
852    }
853
854    #[test]
855    fn cursor_hook_config_has_version_and_object_hooks() {
856        let config = serde_json::json!({
857            "version": 1,
858            "hooks": {
859                "preToolUse": [
860                    {
861                        "matcher": "terminal_command",
862                        "command": "lean-ctx hook rewrite"
863                    },
864                    {
865                        "matcher": "read_file|grep|search|list_files|list_directory",
866                        "command": "lean-ctx hook redirect"
867                    }
868                ]
869            }
870        });
871
872        let json_str = serde_json::to_string_pretty(&config).unwrap();
873        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
874
875        assert_eq!(parsed["version"], 1);
876        assert!(parsed["hooks"].is_object());
877        assert!(parsed["hooks"]["preToolUse"].is_array());
878        assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
879        assert_eq!(
880            parsed["hooks"]["preToolUse"][0]["matcher"],
881            "terminal_command"
882        );
883    }
884
885    #[test]
886    fn cursor_hook_detects_old_format_needs_migration() {
887        let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
888        let has_correct =
889            old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
890        assert!(
891            !has_correct,
892            "Old format should be detected as needing migration"
893        );
894    }
895
896    #[test]
897    fn gemini_hook_config_has_type_command() {
898        let binary = "lean-ctx";
899        let rewrite_cmd = format!("{binary} hook rewrite");
900        let redirect_cmd = format!("{binary} hook redirect");
901
902        let hook_config = serde_json::json!({
903            "hooks": {
904                "BeforeTool": [
905                    {
906                        "hooks": [{
907                            "type": "command",
908                            "command": rewrite_cmd
909                        }]
910                    },
911                    {
912                        "hooks": [{
913                            "type": "command",
914                            "command": redirect_cmd
915                        }]
916                    }
917                ]
918            }
919        });
920
921        let parsed = hook_config;
922        let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
923        assert_eq!(before_tool.len(), 2);
924
925        let first_hook = &before_tool[0]["hooks"][0];
926        assert_eq!(first_hook["type"], "command");
927        assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
928
929        let second_hook = &before_tool[1]["hooks"][0];
930        assert_eq!(second_hook["type"], "command");
931        assert_eq!(second_hook["command"], "lean-ctx hook redirect");
932    }
933
934    #[test]
935    fn gemini_hook_old_format_detected() {
936        let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
937        let has_new = old_format.contains("hook rewrite")
938            && old_format.contains("hook redirect")
939            && old_format.contains("\"type\"");
940        assert!(!has_new, "Missing 'type' field should trigger migration");
941    }
942
943    #[test]
944    fn rewrite_script_uses_registry_pattern() {
945        let script = generate_rewrite_script("/usr/bin/lean-ctx");
946        assert!(script.contains(r"git\ *"), "script missing git pattern");
947        assert!(script.contains(r"cargo\ *"), "script missing cargo pattern");
948        assert!(script.contains(r"npm\ *"), "script missing npm pattern");
949        assert!(
950            !script.contains(r"rg\ *"),
951            "script should not contain rg pattern"
952        );
953        assert!(
954            script.contains("LEAN_CTX_BIN=\"/usr/bin/lean-ctx\""),
955            "script missing binary path"
956        );
957    }
958
959    #[test]
960    fn compact_rewrite_script_uses_registry_pattern() {
961        let script = generate_compact_rewrite_script("/usr/bin/lean-ctx");
962        assert!(script.contains(r"git\ *"), "compact script missing git");
963        assert!(script.contains(r"cargo\ *"), "compact script missing cargo");
964        assert!(
965            !script.contains(r"rg\ *"),
966            "compact script should not contain rg"
967        );
968    }
969
970    #[test]
971    fn rewrite_scripts_contain_all_registry_commands() {
972        let script = generate_rewrite_script("lean-ctx");
973        let compact = generate_compact_rewrite_script("lean-ctx");
974        for entry in crate::rewrite_registry::REWRITE_COMMANDS {
975            if matches!(
976                entry.category,
977                crate::rewrite_registry::Category::Search
978                    | crate::rewrite_registry::Category::FileRead
979            ) {
980                continue;
981            }
982            let pattern = if entry.command.contains('-') {
983                format!("{}*", entry.command.replace('-', r"\-"))
984            } else {
985                format!(r"{}\ *", entry.command)
986            };
987            assert!(
988                script.contains(&pattern),
989                "rewrite_script missing '{}' (pattern: {})",
990                entry.command,
991                pattern
992            );
993            assert!(
994                compact.contains(&pattern),
995                "compact_rewrite_script missing '{}' (pattern: {})",
996                entry.command,
997                pattern
998            );
999        }
1000    }
1001}