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