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