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