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