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
516fn replace_marked_block(content: &str, start: &str, end: &str, replacement: &str) -> String {
517    let s = content.find(start);
518    let e = content.find(end);
519    match (s, e) {
520        (Some(si), Some(ei)) if ei >= si => {
521            let after_end = ei + end.len();
522            let before = &content[..si];
523            let after = &content[after_end..];
524            let mut out = String::new();
525            out.push_str(before.trim_end_matches('\n'));
526            out.push('\n');
527            out.push('\n');
528            out.push_str(replacement.trim_end_matches('\n'));
529            out.push('\n');
530            out.push_str(after.trim_start_matches('\n'));
531            out
532        }
533        _ => content.to_string(),
534    }
535}
536
537const CURSORRULES_TEMPLATE: &str = "\
538# lean-ctx — Context Engineering Layer
539
540PREFER lean-ctx MCP tools over native equivalents for token savings:
541
542| PREFER | OVER | Why |
543|--------|------|-----|
544| `ctx_read(path)` | `Read` | Cached, 10 compression modes |
545| `ctx_shell(command)` | `Shell` | Pattern compression |
546| `ctx_search(pattern, path)` | `Grep` | Compact results |
547| `ctx_tree(path, depth)` | `ls` / `find` | Directory maps |
548| `ctx_edit(path, old_string, new_string)` | `Edit` (when Read unavailable) | Search-and-replace without native Read |
549
550Edit files: use native Edit/StrReplace if available. If Edit requires Read and Read is unavailable, use ctx_edit.
551Write, Delete, Glob — use normally. NEVER loop on Edit failures — switch to ctx_edit immediately.
552";
553
554pub const KIRO_STEERING_TEMPLATE: &str = "\
555---
556inclusion: always
557---
558
559# lean-ctx — Context Engineering Layer
560
561The workspace has the `lean-ctx` MCP server installed. You MUST prefer lean-ctx tools over native equivalents for token efficiency and caching.
562
563## Mandatory Tool Preferences
564
565| Use this | Instead of | Why |
566|----------|-----------|-----|
567| `mcp_lean_ctx_ctx_read` | `readFile`, `readCode` | Cached reads, 10 compression modes, re-reads cost ~13 tokens |
568| `mcp_lean_ctx_ctx_multi_read` | `readMultipleFiles` | Batch cached reads in one call |
569| `mcp_lean_ctx_ctx_shell` | `executeBash` | Pattern compression for git/npm/test output |
570| `mcp_lean_ctx_ctx_search` | `grepSearch` | Compact, .gitignore-aware results |
571| `mcp_lean_ctx_ctx_tree` | `listDirectory` | Compact directory maps with file counts |
572
573## When to use native Kiro tools instead
574
575- `fsWrite` / `fsAppend` — always use native (lean-ctx doesn't write files)
576- `strReplace` — always use native (precise string replacement)
577- `semanticRename` / `smartRelocate` — always use native (IDE integration)
578- `getDiagnostics` — always use native (language server diagnostics)
579- `deleteFile` — always use native
580
581## Session management
582
583- At the start of a long task, call `mcp_lean_ctx_ctx_preload` with a task description to warm the cache
584- Use `mcp_lean_ctx_ctx_compress` periodically in long conversations to checkpoint context
585- Use `mcp_lean_ctx_ctx_knowledge` to persist important discoveries across sessions
586
587## Rules
588
589- NEVER loop on edit failures — switch to `mcp_lean_ctx_ctx_edit` immediately
590- For large files, use `mcp_lean_ctx_ctx_read` with `mode: \"signatures\"` or `mode: \"map\"` first
591- For re-reading a file you already read, just call `mcp_lean_ctx_ctx_read` again (cache hit = ~13 tokens)
592- When running tests or build commands, use `mcp_lean_ctx_ctx_shell` for compressed output
593";
594
595pub fn install_agent_hook(agent: &str, global: bool) {
596    install_agent_hook_with_mode(agent, global, HookMode::Mcp);
597}
598
599pub fn install_agent_hook_with_mode(agent: &str, global: bool, mode: HookMode) {
600    let home = crate::core::home::resolve_home_dir().unwrap_or_default();
601    match agent {
602        "claude" | "claude-code" => install_claude_hook_with_mode(global, mode),
603        "cursor" => install_cursor_hook_with_mode(global, mode),
604        "gemini" => install_gemini_hook(),
605        "antigravity" => install_antigravity_hook(),
606        "codex" => install_codex_hook(),
607        "windsurf" => install_windsurf_rules(global),
608        "cline" | "roo" => install_cline_rules(global),
609        "copilot" | "vscode" => install_copilot_hook(global),
610        "pi" => install_pi_hook_with_mode(global, mode),
611        "qoder" => install_qoder_hook_with_mode(mode),
612        "qoderwork" => install_mcp_json_agent(
613            "QoderWork",
614            "~/.qoderwork/mcp.json",
615            &home.join(".qoderwork/mcp.json"),
616        ),
617        "qwen" => install_mcp_json_agent(
618            "Qwen Code",
619            "~/.qwen/settings.json",
620            &home.join(".qwen/settings.json"),
621        ),
622        "trae" => install_mcp_json_agent("Trae", "~/.trae/mcp.json", &home.join(".trae/mcp.json")),
623        "amazonq" => install_mcp_json_agent(
624            "Amazon Q Developer",
625            "~/.aws/amazonq/default.json",
626            &home.join(".aws/amazonq/default.json"),
627        ),
628        "jetbrains" => install_jetbrains_hook(),
629        "kiro" => install_kiro_hook(),
630        "verdent" => install_mcp_json_agent(
631            "Verdent",
632            "~/.verdent/mcp.json",
633            &home.join(".verdent/mcp.json"),
634        ),
635        "opencode" => install_opencode_hook_with_mode(mode),
636        "amp" => install_amp_hook(),
637        "crush" => install_crush_hook_with_mode(mode),
638        "hermes" => install_hermes_hook_with_mode(global, mode),
639        "zed" => {
640            let zed_path = crate::core::editor_registry::zed_settings_path(&home);
641            let binary = resolve_binary_path();
642            let entry = full_server_entry(&binary);
643            install_named_json_server("Zed", "settings.json", &zed_path, "context_servers", entry);
644        }
645        "aider" => {
646            install_mcp_json_agent("Aider", "~/.aider/mcp.json", &home.join(".aider/mcp.json"));
647        }
648        "continue" => install_mcp_json_agent(
649            "Continue",
650            "~/.continue/mcp.json",
651            &home.join(".continue/mcp.json"),
652        ),
653        "neovim" => install_mcp_json_agent(
654            "Neovim (mcphub.nvim)",
655            "~/.config/mcphub/servers.json",
656            &home.join(".config/mcphub/servers.json"),
657        ),
658        "emacs" => install_mcp_json_agent(
659            "Emacs (mcp.el)",
660            "~/.emacs.d/mcp.json",
661            &home.join(".emacs.d/mcp.json"),
662        ),
663        "sublime" => install_mcp_json_agent(
664            "Sublime Text",
665            "~/.config/sublime-text/mcp.json",
666            &home.join(".config/sublime-text/mcp.json"),
667        ),
668        _ => {
669            eprintln!("Unknown agent: {agent}");
670            eprintln!("  Supported: aider, amazonq, amp, antigravity, claude, cline, codex,");
671            eprintln!("    continue, copilot, crush, cursor, emacs, gemini, hermes, jetbrains,");
672            eprintln!("    kiro, neovim, opencode, pi, qoder, qoderwork, qwen, roo, sublime,");
673            eprintln!("    trae, verdent, vscode, windsurf, zed");
674            std::process::exit(1);
675        }
676    }
677}
678
679pub fn install_agent_project_hooks(agent: &str, cwd: &std::path::Path) {
680    match agent {
681        "claude" | "claude-code" => agents::install_claude_project_hooks(cwd),
682        _ => {}
683    }
684}
685
686fn write_file(path: &std::path::Path, content: &str) {
687    if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
688        tracing::error!("Error writing {}: {e}", path.display());
689    }
690}
691
692fn is_inside_git_repo(path: &std::path::Path) -> bool {
693    let mut p = path;
694    loop {
695        if p.join(".git").exists() {
696            return true;
697        }
698        match p.parent() {
699            Some(parent) => p = parent,
700            None => return false,
701        }
702    }
703}
704
705#[cfg(unix)]
706fn make_executable(path: &PathBuf) {
707    use std::os::unix::fs::PermissionsExt;
708    let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
709}
710
711#[cfg(not(unix))]
712fn make_executable(_path: &PathBuf) {}
713
714fn full_server_entry(binary: &str) -> serde_json::Value {
715    let data_dir = crate::core::data_dir::lean_ctx_data_dir()
716        .map(|d| d.to_string_lossy().to_string())
717        .unwrap_or_default();
718    serde_json::json!({
719        "command": binary,
720        "env": {
721            "LEAN_CTX_DATA_DIR": data_dir,
722            "LEAN_CTX_FULL_TOOLS": "1"
723        }
724    })
725}
726
727fn install_mcp_json_agent(name: &str, display_path: &str, config_path: &std::path::Path) {
728    let binary = resolve_binary_path();
729    let entry = full_server_entry(&binary);
730    install_named_json_server(name, display_path, config_path, "mcpServers", entry);
731}
732
733#[cfg(test)]
734mod tests {
735    use super::*;
736
737    #[test]
738    fn bash_path_unix_unchanged() {
739        assert_eq!(
740            to_bash_compatible_path("/usr/local/bin/lean-ctx"),
741            "/usr/local/bin/lean-ctx"
742        );
743    }
744
745    #[test]
746    fn bash_path_home_unchanged() {
747        assert_eq!(
748            to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
749            "/home/user/.cargo/bin/lean-ctx"
750        );
751    }
752
753    #[test]
754    fn bash_path_windows_drive_converted() {
755        assert_eq!(
756            to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
757            "/c/Users/Fraser/bin/lean-ctx.exe"
758        );
759    }
760
761    #[test]
762    fn bash_path_windows_lowercase_drive() {
763        assert_eq!(
764            to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
765            "/d/tools/lean-ctx.exe"
766        );
767    }
768
769    #[test]
770    fn bash_path_windows_forward_slashes() {
771        assert_eq!(
772            to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
773            "/c/Users/Fraser/bin/lean-ctx.exe"
774        );
775    }
776
777    #[test]
778    fn bash_path_bare_name_unchanged() {
779        assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
780    }
781
782    #[test]
783    fn normalize_msys2_path() {
784        assert_eq!(
785            normalize_tool_path("/c/Users/game/Downloads/project"),
786            "C:/Users/game/Downloads/project"
787        );
788    }
789
790    #[test]
791    fn normalize_msys2_drive_d() {
792        assert_eq!(
793            normalize_tool_path("/d/Projects/app/src"),
794            "D:/Projects/app/src"
795        );
796    }
797
798    #[test]
799    fn normalize_backslashes() {
800        assert_eq!(
801            normalize_tool_path("C:\\Users\\game\\project\\src"),
802            "C:/Users/game/project/src"
803        );
804    }
805
806    #[test]
807    fn normalize_mixed_separators() {
808        assert_eq!(
809            normalize_tool_path("C:\\Users/game\\project/src"),
810            "C:/Users/game/project/src"
811        );
812    }
813
814    #[test]
815    fn normalize_double_slashes() {
816        assert_eq!(
817            normalize_tool_path("/home/user//project///src"),
818            "/home/user/project/src"
819        );
820    }
821
822    #[test]
823    fn normalize_trailing_slash() {
824        assert_eq!(
825            normalize_tool_path("/home/user/project/"),
826            "/home/user/project"
827        );
828    }
829
830    #[test]
831    fn normalize_root_preserved() {
832        assert_eq!(normalize_tool_path("/"), "/");
833    }
834
835    #[test]
836    fn normalize_windows_root_preserved() {
837        assert_eq!(normalize_tool_path("C:/"), "C:/");
838    }
839
840    #[test]
841    fn normalize_unix_path_unchanged() {
842        assert_eq!(
843            normalize_tool_path("/home/user/project/src/main.rs"),
844            "/home/user/project/src/main.rs"
845        );
846    }
847
848    #[test]
849    fn normalize_relative_path_unchanged() {
850        assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
851    }
852
853    #[test]
854    fn normalize_dot_unchanged() {
855        assert_eq!(normalize_tool_path("."), ".");
856    }
857
858    #[test]
859    fn normalize_unc_path_preserved() {
860        assert_eq!(
861            normalize_tool_path("//server/share/file"),
862            "//server/share/file"
863        );
864    }
865
866    #[test]
867    fn cursor_hook_config_has_version_and_object_hooks() {
868        let config = serde_json::json!({
869            "version": 1,
870            "hooks": {
871                "preToolUse": [
872                    {
873                        "matcher": "terminal_command",
874                        "command": "lean-ctx hook rewrite"
875                    },
876                    {
877                        "matcher": "read_file|grep|search|list_files|list_directory",
878                        "command": "lean-ctx hook redirect"
879                    }
880                ]
881            }
882        });
883
884        let json_str = serde_json::to_string_pretty(&config).unwrap();
885        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
886
887        assert_eq!(parsed["version"], 1);
888        assert!(parsed["hooks"].is_object());
889        assert!(parsed["hooks"]["preToolUse"].is_array());
890        assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
891        assert_eq!(
892            parsed["hooks"]["preToolUse"][0]["matcher"],
893            "terminal_command"
894        );
895    }
896
897    #[test]
898    fn cursor_hook_detects_old_format_needs_migration() {
899        let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
900        let has_correct =
901            old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
902        assert!(
903            !has_correct,
904            "Old format should be detected as needing migration"
905        );
906    }
907
908    #[test]
909    fn gemini_hook_config_has_type_command() {
910        let binary = "lean-ctx";
911        let rewrite_cmd = format!("{binary} hook rewrite");
912        let redirect_cmd = format!("{binary} hook redirect");
913
914        let hook_config = serde_json::json!({
915            "hooks": {
916                "BeforeTool": [
917                    {
918                        "hooks": [{
919                            "type": "command",
920                            "command": rewrite_cmd
921                        }]
922                    },
923                    {
924                        "hooks": [{
925                            "type": "command",
926                            "command": redirect_cmd
927                        }]
928                    }
929                ]
930            }
931        });
932
933        let parsed = hook_config;
934        let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
935        assert_eq!(before_tool.len(), 2);
936
937        let first_hook = &before_tool[0]["hooks"][0];
938        assert_eq!(first_hook["type"], "command");
939        assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
940
941        let second_hook = &before_tool[1]["hooks"][0];
942        assert_eq!(second_hook["type"], "command");
943        assert_eq!(second_hook["command"], "lean-ctx hook redirect");
944    }
945
946    #[test]
947    fn gemini_hook_old_format_detected() {
948        let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
949        let has_new = old_format.contains("hook rewrite")
950            && old_format.contains("hook redirect")
951            && old_format.contains("\"type\"");
952        assert!(!has_new, "Missing 'type' field should trigger migration");
953    }
954
955    #[test]
956    fn rewrite_script_uses_registry_pattern() {
957        let script = generate_rewrite_script("/usr/bin/lean-ctx");
958        assert!(script.contains(r"git\ *"), "script missing git pattern");
959        assert!(script.contains(r"cargo\ *"), "script missing cargo pattern");
960        assert!(script.contains(r"npm\ *"), "script missing npm pattern");
961        assert!(
962            !script.contains(r"rg\ *"),
963            "script should not contain rg pattern"
964        );
965        assert!(
966            script.contains("LEAN_CTX_BIN=\"/usr/bin/lean-ctx\""),
967            "script missing binary path"
968        );
969    }
970
971    #[test]
972    fn compact_rewrite_script_uses_registry_pattern() {
973        let script = generate_compact_rewrite_script("/usr/bin/lean-ctx");
974        assert!(script.contains(r"git\ *"), "compact script missing git");
975        assert!(script.contains(r"cargo\ *"), "compact script missing cargo");
976        assert!(
977            !script.contains(r"rg\ *"),
978            "compact script should not contain rg"
979        );
980    }
981
982    #[test]
983    fn rewrite_scripts_contain_all_registry_commands() {
984        let script = generate_rewrite_script("lean-ctx");
985        let compact = generate_compact_rewrite_script("lean-ctx");
986        for entry in crate::rewrite_registry::REWRITE_COMMANDS {
987            if matches!(
988                entry.category,
989                crate::rewrite_registry::Category::Search
990                    | crate::rewrite_registry::Category::FileRead
991            ) {
992                continue;
993            }
994            let pattern = if entry.command.contains('-') {
995                format!("{}*", entry.command.replace('-', r"\-"))
996            } else {
997                format!(r"{}\ *", entry.command)
998            };
999            assert!(
1000                script.contains(&pattern),
1001                "rewrite_script missing '{}' (pattern: {})",
1002                entry.command,
1003                pattern
1004            );
1005            assert!(
1006                compact.contains(&pattern),
1007                "compact_rewrite_script missing '{}' (pattern: {})",
1008                entry.command,
1009                pattern
1010            );
1011        }
1012    }
1013}