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