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