Skip to main content

lean_ctx/
hooks.rs

1use std::path::PathBuf;
2
3fn mcp_server_quiet_mode() -> bool {
4    std::env::var_os("LEAN_CTX_MCP_SERVER").is_some()
5}
6
7/// Silently refresh all hook scripts for agents that are already configured.
8/// Called after updates and on MCP server start to ensure hooks match the current binary version.
9pub fn refresh_installed_hooks() {
10    let home = match dirs::home_dir() {
11        Some(h) => h,
12        None => return,
13    };
14
15    let claude_hooks = home.join(".claude/hooks/lean-ctx-rewrite.sh").exists()
16        || home.join(".claude/settings.json").exists()
17            && std::fs::read_to_string(home.join(".claude/settings.json"))
18                .unwrap_or_default()
19                .contains("lean-ctx");
20
21    if claude_hooks {
22        install_claude_hook_scripts(&home);
23        install_claude_hook_config(&home);
24    }
25
26    let cursor_hooks = home.join(".cursor/hooks/lean-ctx-rewrite.sh").exists()
27        || home.join(".cursor/hooks.json").exists()
28            && std::fs::read_to_string(home.join(".cursor/hooks.json"))
29                .unwrap_or_default()
30                .contains("lean-ctx");
31
32    if cursor_hooks {
33        install_cursor_hook_scripts(&home);
34        install_cursor_hook_config(&home);
35    }
36
37    let gemini_rewrite = home.join(".gemini/hooks/lean-ctx-rewrite-gemini.sh");
38    let gemini_legacy = home.join(".gemini/hooks/lean-ctx-hook-gemini.sh");
39    if gemini_rewrite.exists() || gemini_legacy.exists() {
40        install_gemini_hook_scripts(&home);
41        install_gemini_hook_config(&home);
42    }
43
44    if home.join(".codex/hooks/lean-ctx-rewrite-codex.sh").exists() {
45        install_codex_hook_scripts(&home);
46    }
47}
48
49fn resolve_binary_path() -> String {
50    if is_lean_ctx_in_path() {
51        return "lean-ctx".to_string();
52    }
53    std::env::current_exe()
54        .map(|p| p.to_string_lossy().to_string())
55        .unwrap_or_else(|_| "lean-ctx".to_string())
56}
57
58fn is_lean_ctx_in_path() -> bool {
59    let which_cmd = if cfg!(windows) { "where" } else { "which" };
60    std::process::Command::new(which_cmd)
61        .arg("lean-ctx")
62        .stdout(std::process::Stdio::null())
63        .stderr(std::process::Stdio::null())
64        .status()
65        .map(|s| s.success())
66        .unwrap_or(false)
67}
68
69fn resolve_binary_path_for_bash() -> String {
70    let path = resolve_binary_path();
71    to_bash_compatible_path(&path)
72}
73
74pub fn to_bash_compatible_path(path: &str) -> String {
75    let path = path.replace('\\', "/");
76    if path.len() >= 2 && path.as_bytes()[1] == b':' {
77        let drive = (path.as_bytes()[0] as char).to_ascii_lowercase();
78        format!("/{drive}{}", &path[2..])
79    } else {
80        path
81    }
82}
83
84/// Normalize paths from any client format to a consistent OS-native form.
85/// Handles MSYS2/Git Bash (`/c/Users/...` -> `C:/Users/...`), mixed separators,
86/// double slashes, and trailing slashes. Always uses forward slashes for consistency.
87pub fn normalize_tool_path(path: &str) -> String {
88    let mut p = path.to_string();
89
90    // MSYS2/Git Bash: /c/Users/... -> C:/Users/...
91    if p.len() >= 3
92        && p.starts_with('/')
93        && p.as_bytes()[1].is_ascii_alphabetic()
94        && p.as_bytes()[2] == b'/'
95    {
96        let drive = p.as_bytes()[1].to_ascii_uppercase() as char;
97        p = format!("{drive}:{}", &p[2..]);
98    }
99
100    p = p.replace('\\', "/");
101
102    // Collapse double slashes (preserve UNC paths starting with //)
103    while p.contains("//") && !p.starts_with("//") {
104        p = p.replace("//", "/");
105    }
106
107    // Remove trailing slash (unless root like "/" or "C:/")
108    if p.len() > 1 && p.ends_with('/') && !p.ends_with(":/") {
109        p.pop();
110    }
111
112    p
113}
114
115pub fn generate_rewrite_script(binary: &str) -> String {
116    format!(
117        r#"#!/usr/bin/env bash
118# lean-ctx PreToolUse hook — rewrites bash commands to lean-ctx equivalents
119set -euo pipefail
120
121LEAN_CTX_BIN="{binary}"
122
123INPUT=$(cat)
124TOOL=$(echo "$INPUT" | grep -oE '"tool_name":"([^"\\]|\\.)*"' | head -1 | sed 's/^"tool_name":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
125
126if [ "$TOOL" != "Bash" ] && [ "$TOOL" != "bash" ]; then
127  exit 0
128fi
129
130CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
131
132if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then
133  exit 0
134fi
135
136case "$CMD" in
137  git\ *|gh\ *|cargo\ *|npm\ *|pnpm\ *|yarn\ *|docker\ *|kubectl\ *|pip\ *|pip3\ *|ruff\ *|go\ *|curl\ *|grep\ *|rg\ *|find\ *|cat\ *|head\ *|tail\ *|ls\ *|ls|eslint*|prettier*|tsc*|pytest*|mypy*|aws\ *|helm\ *)
138    # Shell-escape then JSON-escape (two passes)
139    SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
140    REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
141    JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
142    printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD"
143    ;;
144  *) exit 0 ;;
145esac
146"#
147    )
148}
149
150pub fn generate_compact_rewrite_script(binary: &str) -> String {
151    format!(
152        r#"#!/usr/bin/env bash
153# lean-ctx hook — rewrites shell commands
154set -euo pipefail
155LEAN_CTX_BIN="{binary}"
156INPUT=$(cat)
157CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g' 2>/dev/null || echo "")
158if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then exit 0; fi
159case "$CMD" in
160  git\ *|gh\ *|cargo\ *|npm\ *|pnpm\ *|docker\ *|kubectl\ *|pip\ *|ruff\ *|go\ *|curl\ *|grep\ *|rg\ *|find\ *|ls\ *|ls|cat\ *|aws\ *|helm\ *)
161    SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
162    REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
163    JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
164    printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD" ;;
165  *) exit 0 ;;
166esac
167"#
168    )
169}
170
171const REDIRECT_SCRIPT_CLAUDE: &str = r#"#!/usr/bin/env bash
172# lean-ctx PreToolUse hook — all native tools pass through
173# Read/Grep/ListFiles are allowed so Edit (which requires native Read) works.
174# The MCP instructions guide the AI to prefer ctx_read/ctx_search/ctx_tree.
175exit 0
176"#;
177
178const REDIRECT_SCRIPT_GENERIC: &str = r#"#!/usr/bin/env bash
179# lean-ctx hook — all native tools pass through
180exit 0
181"#;
182
183pub fn install_project_rules() {
184    let cwd = std::env::current_dir().unwrap_or_default();
185
186    let agents_md = cwd.join("AGENTS.md");
187    if !agents_md.exists()
188        || !std::fs::read_to_string(&agents_md)
189            .unwrap_or_default()
190            .contains("lean-ctx")
191    {
192        let content = AGENTS_MD_TEMPLATE;
193        write_file(&agents_md, content);
194        println!("Created AGENTS.md in project root.");
195    }
196
197    let cursorrules = cwd.join(".cursorrules");
198    if !cursorrules.exists()
199        || !std::fs::read_to_string(&cursorrules)
200            .unwrap_or_default()
201            .contains("lean-ctx")
202    {
203        let content = CURSORRULES_TEMPLATE;
204        if cursorrules.exists() {
205            let mut existing = std::fs::read_to_string(&cursorrules).unwrap_or_default();
206            if !existing.ends_with('\n') {
207                existing.push('\n');
208            }
209            existing.push('\n');
210            existing.push_str(content);
211            write_file(&cursorrules, &existing);
212        } else {
213            write_file(&cursorrules, content);
214        }
215        println!("Created/updated .cursorrules in project root.");
216    }
217
218    let kiro_dir = cwd.join(".kiro");
219    if kiro_dir.exists() {
220        let steering_dir = kiro_dir.join("steering");
221        let steering_file = steering_dir.join("lean-ctx.md");
222        if !steering_file.exists()
223            || !std::fs::read_to_string(&steering_file)
224                .unwrap_or_default()
225                .contains("lean-ctx")
226        {
227            let _ = std::fs::create_dir_all(&steering_dir);
228            write_file(&steering_file, KIRO_STEERING_TEMPLATE);
229            println!("Created .kiro/steering/lean-ctx.md (Kiro steering).");
230        }
231    }
232}
233
234const AGENTS_MD_TEMPLATE: &str = "\
235# lean-ctx — Context Engineering Layer
236
237PREFER lean-ctx MCP tools over native equivalents for token savings:
238
239| PREFER | OVER | Why |
240|--------|------|-----|
241| `ctx_read(path)` | Read / cat / head / tail | Cached, 8 compression modes, re-reads ~13 tokens |
242| `ctx_shell(command)` | Shell / bash / terminal | Pattern compression for git/npm/cargo output |
243| `ctx_search(pattern, path)` | Grep / rg / search | Compact, token-efficient results |
244| `ctx_tree(path, depth)` | ls / find / tree | Compact directory maps |
245| `ctx_edit(path, old_string, new_string)` | Edit (when Read unavailable) | Search-and-replace without native Read |
246
247Edit files: use native Edit/StrReplace if available. If Edit requires Read and Read is unavailable, use ctx_edit.
248Write, Delete, Glob — use normally. NEVER loop on Edit failures — switch to ctx_edit immediately.
249";
250
251const CURSORRULES_TEMPLATE: &str = "\
252# lean-ctx — Context Engineering Layer
253
254PREFER lean-ctx MCP tools over native equivalents for token savings:
255
256| PREFER | OVER | Why |
257|--------|------|-----|
258| `ctx_read(path)` | `Read` | Cached, 8 compression modes |
259| `ctx_shell(command)` | `Shell` | Pattern compression |
260| `ctx_search(pattern, path)` | `Grep` | Compact results |
261| `ctx_tree(path, depth)` | `ls` / `find` | Directory maps |
262| `ctx_edit(path, old_string, new_string)` | `Edit` (when Read unavailable) | Search-and-replace without native Read |
263
264Edit files: use native Edit/StrReplace if available. If Edit requires Read and Read is unavailable, use ctx_edit.
265Write, Delete, Glob — use normally. NEVER loop on Edit failures — switch to ctx_edit immediately.
266";
267
268pub const KIRO_STEERING_TEMPLATE: &str = "\
269---
270inclusion: always
271---
272
273# lean-ctx — Context Engineering Layer
274
275The workspace has the `lean-ctx` MCP server installed. You MUST prefer lean-ctx tools over native equivalents for token efficiency and caching.
276
277## Mandatory Tool Preferences
278
279| Use this | Instead of | Why |
280|----------|-----------|-----|
281| `mcp_lean_ctx_ctx_read` | `readFile`, `readCode` | Cached reads, 8 compression modes, re-reads cost ~13 tokens |
282| `mcp_lean_ctx_ctx_multi_read` | `readMultipleFiles` | Batch cached reads in one call |
283| `mcp_lean_ctx_ctx_shell` | `executeBash` | Pattern compression for git/npm/test output |
284| `mcp_lean_ctx_ctx_search` | `grepSearch` | Compact, .gitignore-aware results |
285| `mcp_lean_ctx_ctx_tree` | `listDirectory` | Compact directory maps with file counts |
286
287## When to use native Kiro tools instead
288
289- `fsWrite` / `fsAppend` — always use native (lean-ctx doesn't write files)
290- `strReplace` — always use native (precise string replacement)
291- `semanticRename` / `smartRelocate` — always use native (IDE integration)
292- `getDiagnostics` — always use native (language server diagnostics)
293- `deleteFile` — always use native
294
295## Session management
296
297- At the start of a long task, call `mcp_lean_ctx_ctx_preload` with a task description to warm the cache
298- Use `mcp_lean_ctx_ctx_compress` periodically in long conversations to checkpoint context
299- Use `mcp_lean_ctx_ctx_knowledge` to persist important discoveries across sessions
300
301## Rules
302
303- NEVER loop on edit failures — switch to `mcp_lean_ctx_ctx_edit` immediately
304- For large files, use `mcp_lean_ctx_ctx_read` with `mode: \"signatures\"` or `mode: \"map\"` first
305- For re-reading a file you already read, just call `mcp_lean_ctx_ctx_read` again (cache hit = ~13 tokens)
306- When running tests or build commands, use `mcp_lean_ctx_ctx_shell` for compressed output
307";
308
309pub fn install_agent_hook(agent: &str, global: bool) {
310    match agent {
311        "claude" | "claude-code" => install_claude_hook(global),
312        "cursor" => install_cursor_hook(global),
313        "gemini" | "antigravity" => install_gemini_hook(),
314        "codex" => install_codex_hook(),
315        "windsurf" => install_windsurf_rules(global),
316        "cline" | "roo" => install_cline_rules(global),
317        "copilot" => install_copilot_hook(global),
318        "pi" => install_pi_hook(global),
319        "qwen" => install_mcp_json_agent(
320            "Qwen Code",
321            "~/.qwen/mcp.json",
322            &dirs::home_dir().unwrap_or_default().join(".qwen/mcp.json"),
323        ),
324        "trae" => install_mcp_json_agent(
325            "Trae",
326            "~/.trae/mcp.json",
327            &dirs::home_dir().unwrap_or_default().join(".trae/mcp.json"),
328        ),
329        "amazonq" => install_mcp_json_agent(
330            "Amazon Q Developer",
331            "~/.aws/amazonq/mcp.json",
332            &dirs::home_dir()
333                .unwrap_or_default()
334                .join(".aws/amazonq/mcp.json"),
335        ),
336        "jetbrains" => install_mcp_json_agent(
337            "JetBrains IDEs",
338            "~/.jb-mcp.json",
339            &dirs::home_dir().unwrap_or_default().join(".jb-mcp.json"),
340        ),
341        "kiro" => install_kiro_hook(),
342        "verdent" => install_mcp_json_agent(
343            "Verdent",
344            "~/.verdent/mcp.json",
345            &dirs::home_dir()
346                .unwrap_or_default()
347                .join(".verdent/mcp.json"),
348        ),
349        "opencode" => install_mcp_json_agent(
350            "OpenCode",
351            "~/.opencode/mcp.json",
352            &dirs::home_dir()
353                .unwrap_or_default()
354                .join(".opencode/mcp.json"),
355        ),
356        "aider" => install_mcp_json_agent(
357            "Aider",
358            "~/.aider/mcp.json",
359            &dirs::home_dir().unwrap_or_default().join(".aider/mcp.json"),
360        ),
361        "amp" => install_mcp_json_agent(
362            "Amp",
363            "~/.amp/mcp.json",
364            &dirs::home_dir().unwrap_or_default().join(".amp/mcp.json"),
365        ),
366        "crush" => install_crush_hook(),
367        _ => {
368            eprintln!("Unknown agent: {agent}");
369            eprintln!("  Supported: claude, cursor, gemini, antigravity, codex, windsurf, cline, roo, copilot, pi, qwen, trae, amazonq, jetbrains, kiro, verdent, opencode, aider, amp, crush");
370            std::process::exit(1);
371        }
372    }
373}
374
375fn install_claude_hook(global: bool) {
376    let home = match dirs::home_dir() {
377        Some(h) => h,
378        None => {
379            eprintln!("Cannot resolve home directory");
380            return;
381        }
382    };
383
384    install_claude_hook_scripts(&home);
385    install_claude_hook_config(&home);
386
387    install_claude_global_md(&home);
388
389    if !global {
390        let claude_md = PathBuf::from("CLAUDE.md");
391        if !claude_md.exists()
392            || !std::fs::read_to_string(&claude_md)
393                .unwrap_or_default()
394                .contains("lean-ctx")
395        {
396            let content = include_str!("templates/CLAUDE.md");
397            write_file(&claude_md, content);
398            println!("Created CLAUDE.md in current project directory.");
399        } else {
400            println!("CLAUDE.md already configured.");
401        }
402    }
403}
404
405fn install_claude_global_md(home: &std::path::Path) {
406    let claude_dir = home.join(".claude");
407    let _ = std::fs::create_dir_all(&claude_dir);
408    let global_md = claude_dir.join("CLAUDE.md");
409
410    let existing = std::fs::read_to_string(&global_md).unwrap_or_default();
411    if existing.contains("lean-ctx") {
412        println!("  \x1b[32m✓\x1b[0m ~/.claude/CLAUDE.md already configured");
413        return;
414    }
415
416    let content = include_str!("templates/CLAUDE_GLOBAL.md");
417
418    if existing.is_empty() {
419        write_file(&global_md, content);
420    } else {
421        let mut merged = existing;
422        if !merged.ends_with('\n') {
423            merged.push('\n');
424        }
425        merged.push('\n');
426        merged.push_str(content);
427        write_file(&global_md, &merged);
428    }
429    println!("  \x1b[32m✓\x1b[0m Installed global ~/.claude/CLAUDE.md");
430}
431
432fn install_claude_hook_scripts(home: &std::path::Path) {
433    let hooks_dir = home.join(".claude").join("hooks");
434    let _ = std::fs::create_dir_all(&hooks_dir);
435
436    let binary = resolve_binary_path();
437
438    let rewrite_path = hooks_dir.join("lean-ctx-rewrite.sh");
439    let rewrite_script = generate_rewrite_script(&resolve_binary_path_for_bash());
440    write_file(&rewrite_path, &rewrite_script);
441    make_executable(&rewrite_path);
442
443    let redirect_path = hooks_dir.join("lean-ctx-redirect.sh");
444    write_file(&redirect_path, REDIRECT_SCRIPT_CLAUDE);
445    make_executable(&redirect_path);
446
447    let wrapper = |subcommand: &str| -> String {
448        if cfg!(windows) {
449            format!("{binary} hook {subcommand}")
450        } else {
451            format!("{} hook {subcommand}", resolve_binary_path_for_bash())
452        }
453    };
454
455    let rewrite_native = hooks_dir.join("lean-ctx-rewrite-native");
456    write_file(
457        &rewrite_native,
458        &format!(
459            "#!/bin/sh\nexec {} hook rewrite\n",
460            resolve_binary_path_for_bash()
461        ),
462    );
463    make_executable(&rewrite_native);
464
465    let redirect_native = hooks_dir.join("lean-ctx-redirect-native");
466    write_file(
467        &redirect_native,
468        &format!(
469            "#!/bin/sh\nexec {} hook redirect\n",
470            resolve_binary_path_for_bash()
471        ),
472    );
473    make_executable(&redirect_native);
474
475    let _ = wrapper; // suppress unused warning on unix
476}
477
478fn install_claude_hook_config(home: &std::path::Path) {
479    let hooks_dir = home.join(".claude").join("hooks");
480    let binary = resolve_binary_path();
481
482    let rewrite_cmd = format!("{binary} hook rewrite");
483    let redirect_cmd = format!("{binary} hook redirect");
484
485    let settings_path = home.join(".claude").join("settings.json");
486    let settings_content = if settings_path.exists() {
487        std::fs::read_to_string(&settings_path).unwrap_or_default()
488    } else {
489        String::new()
490    };
491
492    let needs_update =
493        !settings_content.contains("hook rewrite") || !settings_content.contains("hook redirect");
494    let has_old_hooks = settings_content.contains("lean-ctx-rewrite.sh")
495        || settings_content.contains("lean-ctx-redirect.sh");
496
497    if !needs_update && !has_old_hooks {
498        return;
499    }
500
501    let hook_entry = serde_json::json!({
502        "hooks": {
503            "PreToolUse": [
504                {
505                    "matcher": "Bash|bash",
506                    "hooks": [{
507                        "type": "command",
508                        "command": rewrite_cmd
509                    }]
510                },
511                {
512                    "matcher": "Read|read|ReadFile|read_file|View|view|Grep|grep|Search|search|ListFiles|list_files|ListDirectory|list_directory",
513                    "hooks": [{
514                        "type": "command",
515                        "command": redirect_cmd
516                    }]
517                }
518            ]
519        }
520    });
521
522    if settings_content.is_empty() {
523        write_file(
524            &settings_path,
525            &serde_json::to_string_pretty(&hook_entry).unwrap(),
526        );
527    } else if let Ok(mut existing) = serde_json::from_str::<serde_json::Value>(&settings_content) {
528        if let Some(obj) = existing.as_object_mut() {
529            obj.insert("hooks".to_string(), hook_entry["hooks"].clone());
530            write_file(
531                &settings_path,
532                &serde_json::to_string_pretty(&existing).unwrap(),
533            );
534        }
535    }
536    if !mcp_server_quiet_mode() {
537        println!("Installed Claude Code hooks at {}", hooks_dir.display());
538    }
539}
540
541fn install_cursor_hook(global: bool) {
542    let home = match dirs::home_dir() {
543        Some(h) => h,
544        None => {
545            eprintln!("Cannot resolve home directory");
546            return;
547        }
548    };
549
550    install_cursor_hook_scripts(&home);
551    install_cursor_hook_config(&home);
552
553    if !global {
554        let rules_dir = PathBuf::from(".cursor").join("rules");
555        let _ = std::fs::create_dir_all(&rules_dir);
556        let rule_path = rules_dir.join("lean-ctx.mdc");
557        if !rule_path.exists() {
558            let rule_content = include_str!("templates/lean-ctx.mdc");
559            write_file(&rule_path, rule_content);
560            println!("Created .cursor/rules/lean-ctx.mdc in current project.");
561        } else {
562            println!("Cursor rule already exists.");
563        }
564    } else {
565        println!("Global mode: skipping project-local .cursor/rules/ (use without --global in a project).");
566    }
567
568    println!("Restart Cursor to activate.");
569}
570
571fn install_cursor_hook_scripts(home: &std::path::Path) {
572    let hooks_dir = home.join(".cursor").join("hooks");
573    let _ = std::fs::create_dir_all(&hooks_dir);
574
575    let binary = resolve_binary_path_for_bash();
576
577    let rewrite_path = hooks_dir.join("lean-ctx-rewrite.sh");
578    let rewrite_script = generate_compact_rewrite_script(&binary);
579    write_file(&rewrite_path, &rewrite_script);
580    make_executable(&rewrite_path);
581
582    let redirect_path = hooks_dir.join("lean-ctx-redirect.sh");
583    write_file(&redirect_path, REDIRECT_SCRIPT_GENERIC);
584    make_executable(&redirect_path);
585
586    let native_binary = resolve_binary_path();
587    let rewrite_native = hooks_dir.join("lean-ctx-rewrite-native");
588    write_file(
589        &rewrite_native,
590        &format!("#!/bin/sh\nexec {} hook rewrite\n", native_binary),
591    );
592    make_executable(&rewrite_native);
593
594    let redirect_native = hooks_dir.join("lean-ctx-redirect-native");
595    write_file(
596        &redirect_native,
597        &format!("#!/bin/sh\nexec {} hook redirect\n", native_binary),
598    );
599    make_executable(&redirect_native);
600}
601
602fn install_cursor_hook_config(home: &std::path::Path) {
603    let binary = resolve_binary_path();
604    let rewrite_cmd = format!("{binary} hook rewrite");
605    let redirect_cmd = format!("{binary} hook redirect");
606
607    let hooks_json = home.join(".cursor").join("hooks.json");
608
609    let hook_config = serde_json::json!({
610        "version": 1,
611        "hooks": {
612            "preToolUse": [
613                {
614                    "matcher": "terminal_command",
615                    "command": rewrite_cmd
616                },
617                {
618                    "matcher": "read_file|grep|search|list_files|list_directory",
619                    "command": redirect_cmd
620                }
621            ]
622        }
623    });
624
625    let content = if hooks_json.exists() {
626        std::fs::read_to_string(&hooks_json).unwrap_or_default()
627    } else {
628        String::new()
629    };
630
631    let has_correct_format = content.contains("\"version\"") && content.contains("\"preToolUse\"");
632    if has_correct_format && content.contains("hook rewrite") && content.contains("hook redirect") {
633        return;
634    }
635
636    if content.is_empty() || !content.contains("\"version\"") {
637        write_file(
638            &hooks_json,
639            &serde_json::to_string_pretty(&hook_config).unwrap(),
640        );
641    } else if let Ok(mut existing) = serde_json::from_str::<serde_json::Value>(&content) {
642        if let Some(obj) = existing.as_object_mut() {
643            obj.insert("version".to_string(), serde_json::json!(1));
644            obj.insert("hooks".to_string(), hook_config["hooks"].clone());
645            write_file(
646                &hooks_json,
647                &serde_json::to_string_pretty(&existing).unwrap(),
648            );
649        }
650    } else {
651        write_file(
652            &hooks_json,
653            &serde_json::to_string_pretty(&hook_config).unwrap(),
654        );
655    }
656
657    if !mcp_server_quiet_mode() {
658        println!("Installed Cursor hooks at {}", hooks_json.display());
659    }
660}
661
662fn install_gemini_hook() {
663    let home = match dirs::home_dir() {
664        Some(h) => h,
665        None => {
666            eprintln!("Cannot resolve home directory");
667            return;
668        }
669    };
670
671    install_gemini_hook_scripts(&home);
672    install_gemini_hook_config(&home);
673}
674
675fn install_gemini_hook_scripts(home: &std::path::Path) {
676    let hooks_dir = home.join(".gemini").join("hooks");
677    let _ = std::fs::create_dir_all(&hooks_dir);
678
679    let binary = resolve_binary_path_for_bash();
680
681    let rewrite_path = hooks_dir.join("lean-ctx-rewrite-gemini.sh");
682    let rewrite_script = generate_compact_rewrite_script(&binary);
683    write_file(&rewrite_path, &rewrite_script);
684    make_executable(&rewrite_path);
685
686    let redirect_path = hooks_dir.join("lean-ctx-redirect-gemini.sh");
687    write_file(&redirect_path, REDIRECT_SCRIPT_GENERIC);
688    make_executable(&redirect_path);
689}
690
691fn install_gemini_hook_config(home: &std::path::Path) {
692    let binary = resolve_binary_path();
693    let rewrite_cmd = format!("{binary} hook rewrite");
694    let redirect_cmd = format!("{binary} hook redirect");
695
696    let settings_path = home.join(".gemini").join("settings.json");
697    let settings_content = if settings_path.exists() {
698        std::fs::read_to_string(&settings_path).unwrap_or_default()
699    } else {
700        String::new()
701    };
702
703    let has_new_format = settings_content.contains("hook rewrite")
704        && settings_content.contains("hook redirect")
705        && settings_content.contains("\"type\"");
706    let has_old_hooks = settings_content.contains("lean-ctx-rewrite")
707        || settings_content.contains("lean-ctx-redirect")
708        || (settings_content.contains("hook rewrite") && !settings_content.contains("\"type\""));
709
710    if has_new_format && !has_old_hooks {
711        return;
712    }
713
714    let hook_config = serde_json::json!({
715        "hooks": {
716            "BeforeTool": [
717                {
718                    "hooks": [{
719                        "type": "command",
720                        "command": rewrite_cmd
721                    }]
722                },
723                {
724                    "hooks": [{
725                        "type": "command",
726                        "command": redirect_cmd
727                    }]
728                }
729            ]
730        }
731    });
732
733    if settings_content.is_empty() {
734        write_file(
735            &settings_path,
736            &serde_json::to_string_pretty(&hook_config).unwrap(),
737        );
738    } else if let Ok(mut existing) = serde_json::from_str::<serde_json::Value>(&settings_content) {
739        if let Some(obj) = existing.as_object_mut() {
740            obj.insert("hooks".to_string(), hook_config["hooks"].clone());
741            write_file(
742                &settings_path,
743                &serde_json::to_string_pretty(&existing).unwrap(),
744            );
745        }
746    }
747    if !mcp_server_quiet_mode() {
748        println!(
749            "Installed Gemini CLI hooks at {}",
750            settings_path.parent().unwrap_or(&settings_path).display()
751        );
752    }
753}
754
755fn install_codex_hook() {
756    let home = match dirs::home_dir() {
757        Some(h) => h,
758        None => {
759            eprintln!("Cannot resolve home directory");
760            return;
761        }
762    };
763
764    let codex_dir = home.join(".codex");
765    let _ = std::fs::create_dir_all(&codex_dir);
766
767    install_codex_hook_scripts(&home);
768
769    let agents_path = codex_dir.join("AGENTS.md");
770    let agents_content = "# Global Agent Instructions\n\n@LEAN-CTX.md\n";
771
772    let lean_ctx_md = codex_dir.join("LEAN-CTX.md");
773    let binary = resolve_binary_path();
774    let lean_ctx_content = format!(
775        r#"# lean-ctx — Token Optimization
776
777Prefix all shell commands with `{binary} -c` for compressed output:
778
779```bash
780{binary} -c git status    # instead of: git status
781{binary} -c cargo test    # instead of: cargo test
782{binary} -c ls src/       # instead of: ls src/
783```
784
785This saves 60-90% tokens per command. Works with: git, cargo, npm, pnpm, docker, kubectl, pip, ruff, go, curl, grep, find, ls, aws, helm, and 90+ more commands.
786Use `{binary} -c --raw <cmd>` to skip compression and get full output.
787"#
788    );
789
790    if agents_path.exists() {
791        let content = std::fs::read_to_string(&agents_path).unwrap_or_default();
792        if content.contains("lean-ctx") || content.contains("LEAN-CTX") {
793            println!("Codex AGENTS.md already configured.");
794            return;
795        }
796    }
797
798    write_file(&agents_path, agents_content);
799    write_file(&lean_ctx_md, &lean_ctx_content);
800    println!("Installed Codex instructions at {}", codex_dir.display());
801}
802
803fn install_codex_hook_scripts(home: &std::path::Path) {
804    let hooks_dir = home.join(".codex").join("hooks");
805    let _ = std::fs::create_dir_all(&hooks_dir);
806
807    let binary = resolve_binary_path_for_bash();
808    let rewrite_path = hooks_dir.join("lean-ctx-rewrite-codex.sh");
809    let rewrite_script = generate_compact_rewrite_script(&binary);
810    write_file(&rewrite_path, &rewrite_script);
811    make_executable(&rewrite_path);
812    if !mcp_server_quiet_mode() {
813        println!(
814            "  \x1b[32m✓\x1b[0m Installed Codex hook scripts at {}",
815            hooks_dir.display()
816        );
817    }
818}
819
820fn install_windsurf_rules(global: bool) {
821    if global {
822        println!("Global mode: skipping project-local .windsurfrules (use without --global in a project).");
823        return;
824    }
825
826    let rules_path = PathBuf::from(".windsurfrules");
827    if rules_path.exists() {
828        let content = std::fs::read_to_string(&rules_path).unwrap_or_default();
829        if content.contains("lean-ctx") {
830            println!(".windsurfrules already configured.");
831            return;
832        }
833    }
834
835    let rules = include_str!("templates/windsurfrules.txt");
836    write_file(&rules_path, rules);
837    println!("Installed .windsurfrules in current project.");
838}
839
840fn install_cline_rules(global: bool) {
841    if global {
842        println!(
843            "Global mode: skipping project-local .clinerules (use without --global in a project)."
844        );
845        return;
846    }
847
848    let rules_path = PathBuf::from(".clinerules");
849    if rules_path.exists() {
850        let content = std::fs::read_to_string(&rules_path).unwrap_or_default();
851        if content.contains("lean-ctx") {
852            println!(".clinerules already configured.");
853            return;
854        }
855    }
856
857    let binary = resolve_binary_path();
858    let rules = format!(
859        r#"# lean-ctx Shell Optimization
860# Prefix all shell commands with `{binary} -c` for 60-90% token savings.
861
862When running shell commands, always prefix them with `{binary} -c` to compress output. For example:
863- `{binary} -c git status` instead of `git status`
864- `{binary} -c cargo test` instead of `cargo test`
865- `{binary} -c ls src/` instead of `ls src/`
866
867Supported commands: git, cargo, npm, pnpm, docker, kubectl, pip, ruff, go, curl, grep, find, ls, aws, helm, and 90+ more.
868"#
869    );
870
871    write_file(&rules_path, &rules);
872    println!("Installed .clinerules in current project.");
873}
874
875fn install_pi_hook(global: bool) {
876    let has_pi = std::process::Command::new("pi")
877        .arg("--version")
878        .output()
879        .is_ok();
880
881    if !has_pi {
882        println!("Pi Coding Agent not found in PATH.");
883        println!("Install Pi first: npm install -g @mariozechner/pi-coding-agent");
884        println!();
885    }
886
887    println!("Installing pi-lean-ctx Pi Package...");
888    println!();
889
890    let install_result = std::process::Command::new("pi")
891        .args(["install", "npm:pi-lean-ctx"])
892        .status();
893
894    match install_result {
895        Ok(status) if status.success() => {
896            println!("Installed pi-lean-ctx Pi Package.");
897        }
898        _ => {
899            println!("Could not auto-install pi-lean-ctx. Install manually:");
900            println!("  pi install npm:pi-lean-ctx");
901            println!();
902        }
903    }
904
905    write_pi_mcp_config();
906
907    if !global {
908        let agents_md = PathBuf::from("AGENTS.md");
909        if !agents_md.exists()
910            || !std::fs::read_to_string(&agents_md)
911                .unwrap_or_default()
912                .contains("lean-ctx")
913        {
914            let content = include_str!("templates/PI_AGENTS.md");
915            write_file(&agents_md, content);
916            println!("Created AGENTS.md in current project directory.");
917        } else {
918            println!("AGENTS.md already contains lean-ctx configuration.");
919        }
920    } else {
921        println!(
922            "Global mode: skipping project-local AGENTS.md (use without --global in a project)."
923        );
924    }
925
926    println!();
927    println!("Setup complete. All Pi tools (bash, read, grep, find, ls) route through lean-ctx.");
928    println!("MCP tools (ctx_session, ctx_knowledge, ctx_semantic_search, ...) also available.");
929    println!("Use /lean-ctx in Pi to verify the binary path and MCP status.");
930}
931
932fn write_pi_mcp_config() {
933    let home = match dirs::home_dir() {
934        Some(h) => h,
935        None => return,
936    };
937
938    let mcp_config_path = home.join(".pi/agent/mcp.json");
939
940    if !home.join(".pi/agent").exists() {
941        println!("  \x1b[2m○ ~/.pi/agent/ not found — skipping MCP config\x1b[0m");
942        return;
943    }
944
945    if mcp_config_path.exists() {
946        let content = match std::fs::read_to_string(&mcp_config_path) {
947            Ok(c) => c,
948            Err(_) => return,
949        };
950        if content.contains("lean-ctx") {
951            println!("  \x1b[32m✓\x1b[0m Pi MCP config already contains lean-ctx");
952            return;
953        }
954
955        if let Ok(mut json) = serde_json::from_str::<serde_json::Value>(&content) {
956            if let Some(obj) = json.as_object_mut() {
957                let servers = obj
958                    .entry("mcpServers")
959                    .or_insert_with(|| serde_json::json!({}));
960                if let Some(servers_obj) = servers.as_object_mut() {
961                    servers_obj.insert("lean-ctx".to_string(), pi_mcp_server_entry());
962                }
963                if let Ok(formatted) = serde_json::to_string_pretty(&json) {
964                    let _ = std::fs::write(&mcp_config_path, formatted);
965                    println!(
966                        "  \x1b[32m✓\x1b[0m Added lean-ctx to Pi MCP config (~/.pi/agent/mcp.json)"
967                    );
968                }
969            }
970        }
971        return;
972    }
973
974    let content = serde_json::json!({
975        "mcpServers": {
976            "lean-ctx": pi_mcp_server_entry()
977        }
978    });
979    if let Ok(formatted) = serde_json::to_string_pretty(&content) {
980        let _ = std::fs::write(&mcp_config_path, formatted);
981        println!("  \x1b[32m✓\x1b[0m Created Pi MCP config (~/.pi/agent/mcp.json)");
982    }
983}
984
985fn pi_mcp_server_entry() -> serde_json::Value {
986    let binary = resolve_binary_path();
987    serde_json::json!({
988        "command": binary,
989        "lifecycle": "lazy",
990        "directTools": true
991    })
992}
993
994fn install_copilot_hook(global: bool) {
995    let binary = resolve_binary_path();
996
997    if global {
998        let mcp_path = copilot_global_mcp_path();
999        if mcp_path.as_os_str() == "/nonexistent" {
1000            println!("  \x1b[2mVS Code not found — skipping global Copilot config\x1b[0m");
1001            return;
1002        }
1003        write_vscode_mcp_file(&mcp_path, &binary, "global VS Code User MCP");
1004    } else {
1005        let vscode_dir = PathBuf::from(".vscode");
1006        let _ = std::fs::create_dir_all(&vscode_dir);
1007        let mcp_path = vscode_dir.join("mcp.json");
1008        write_vscode_mcp_file(&mcp_path, &binary, ".vscode/mcp.json");
1009    }
1010}
1011
1012fn copilot_global_mcp_path() -> PathBuf {
1013    if let Some(home) = dirs::home_dir() {
1014        #[cfg(target_os = "macos")]
1015        {
1016            return home.join("Library/Application Support/Code/User/mcp.json");
1017        }
1018        #[cfg(target_os = "linux")]
1019        {
1020            return home.join(".config/Code/User/mcp.json");
1021        }
1022        #[cfg(target_os = "windows")]
1023        {
1024            if let Ok(appdata) = std::env::var("APPDATA") {
1025                return PathBuf::from(appdata).join("Code/User/mcp.json");
1026            }
1027        }
1028        #[allow(unreachable_code)]
1029        home.join(".config/Code/User/mcp.json")
1030    } else {
1031        PathBuf::from("/nonexistent")
1032    }
1033}
1034
1035fn write_vscode_mcp_file(mcp_path: &PathBuf, binary: &str, label: &str) {
1036    let desired = serde_json::json!({ "command": binary, "args": [] });
1037    if mcp_path.exists() {
1038        let content = std::fs::read_to_string(mcp_path).unwrap_or_default();
1039        match serde_json::from_str::<serde_json::Value>(&content) {
1040            Ok(mut json) => {
1041                if let Some(obj) = json.as_object_mut() {
1042                    let servers = obj
1043                        .entry("servers")
1044                        .or_insert_with(|| serde_json::json!({}));
1045                    if let Some(servers_obj) = servers.as_object_mut() {
1046                        if servers_obj.get("lean-ctx") == Some(&desired) {
1047                            println!("  \x1b[32m✓\x1b[0m Copilot already configured in {label}");
1048                            return;
1049                        }
1050                        servers_obj.insert("lean-ctx".to_string(), desired);
1051                    }
1052                    write_file(
1053                        mcp_path,
1054                        &serde_json::to_string_pretty(&json).unwrap_or_default(),
1055                    );
1056                    println!("  \x1b[32m✓\x1b[0m Added lean-ctx to {label}");
1057                    return;
1058                }
1059            }
1060            Err(e) => {
1061                eprintln!(
1062                    "Could not parse VS Code MCP config at {}: {e}\nAdd to \"servers\": \"lean-ctx\": {{ \"command\": \"{}\", \"args\": [] }}",
1063                    mcp_path.display(),
1064                    binary
1065                );
1066                return;
1067            }
1068        };
1069    }
1070
1071    if let Some(parent) = mcp_path.parent() {
1072        let _ = std::fs::create_dir_all(parent);
1073    }
1074
1075    let config = serde_json::json!({
1076        "servers": {
1077            "lean-ctx": {
1078                "command": binary,
1079                "args": []
1080            }
1081        }
1082    });
1083
1084    write_file(
1085        mcp_path,
1086        &serde_json::to_string_pretty(&config).unwrap_or_default(),
1087    );
1088    println!("  \x1b[32m✓\x1b[0m Created {label} with lean-ctx MCP server");
1089}
1090
1091fn write_file(path: &std::path::Path, content: &str) {
1092    if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
1093        eprintln!("Error writing {}: {e}", path.display());
1094    }
1095}
1096
1097#[cfg(unix)]
1098fn make_executable(path: &PathBuf) {
1099    use std::os::unix::fs::PermissionsExt;
1100    let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
1101}
1102
1103#[cfg(not(unix))]
1104fn make_executable(_path: &PathBuf) {}
1105
1106fn install_crush_hook() {
1107    let binary = resolve_binary_path();
1108    let home = dirs::home_dir().unwrap_or_default();
1109    let config_path = home.join(".config/crush/crush.json");
1110    let display_path = "~/.config/crush/crush.json";
1111
1112    if let Some(parent) = config_path.parent() {
1113        let _ = std::fs::create_dir_all(parent);
1114    }
1115
1116    if config_path.exists() {
1117        let content = std::fs::read_to_string(&config_path).unwrap_or_default();
1118        if content.contains("lean-ctx") {
1119            println!("Crush MCP already configured at {display_path}");
1120            return;
1121        }
1122
1123        if let Ok(mut json) = serde_json::from_str::<serde_json::Value>(&content) {
1124            if let Some(obj) = json.as_object_mut() {
1125                let servers = obj.entry("mcp").or_insert_with(|| serde_json::json!({}));
1126                if let Some(servers_obj) = servers.as_object_mut() {
1127                    servers_obj.insert(
1128                        "lean-ctx".to_string(),
1129                        serde_json::json!({ "type": "stdio", "command": binary }),
1130                    );
1131                }
1132                if let Ok(formatted) = serde_json::to_string_pretty(&json) {
1133                    let _ = std::fs::write(&config_path, formatted);
1134                    println!("  \x1b[32m✓\x1b[0m Crush MCP configured at {display_path}");
1135                    return;
1136                }
1137            }
1138        }
1139    }
1140
1141    let content = serde_json::to_string_pretty(&serde_json::json!({
1142        "mcp": {
1143            "lean-ctx": {
1144                "type": "stdio",
1145                "command": binary
1146            }
1147        }
1148    }));
1149
1150    if let Ok(json_str) = content {
1151        let _ = std::fs::write(&config_path, json_str);
1152        println!("  \x1b[32m✓\x1b[0m Crush MCP configured at {display_path}");
1153    } else {
1154        eprintln!("  \x1b[31m✗\x1b[0m Failed to configure Crush");
1155    }
1156}
1157
1158fn install_kiro_hook() {
1159    let home = dirs::home_dir().unwrap_or_default();
1160
1161    install_mcp_json_agent(
1162        "AWS Kiro",
1163        "~/.kiro/settings/mcp.json",
1164        &home.join(".kiro/settings/mcp.json"),
1165    );
1166
1167    let cwd = std::env::current_dir().unwrap_or_default();
1168    let steering_dir = cwd.join(".kiro").join("steering");
1169    let steering_file = steering_dir.join("lean-ctx.md");
1170
1171    if steering_file.exists()
1172        && std::fs::read_to_string(&steering_file)
1173            .unwrap_or_default()
1174            .contains("lean-ctx")
1175    {
1176        println!("  Kiro steering file already exists at .kiro/steering/lean-ctx.md");
1177    } else {
1178        let _ = std::fs::create_dir_all(&steering_dir);
1179        write_file(&steering_file, KIRO_STEERING_TEMPLATE);
1180        println!("  \x1b[32m✓\x1b[0m Created .kiro/steering/lean-ctx.md (Kiro will now prefer lean-ctx tools)");
1181    }
1182}
1183
1184fn install_mcp_json_agent(name: &str, display_path: &str, config_path: &std::path::Path) {
1185    let binary = resolve_binary_path();
1186
1187    if let Some(parent) = config_path.parent() {
1188        let _ = std::fs::create_dir_all(parent);
1189    }
1190
1191    if config_path.exists() {
1192        let content = std::fs::read_to_string(config_path).unwrap_or_default();
1193        if content.contains("lean-ctx") {
1194            println!("{name} MCP already configured at {display_path}");
1195            return;
1196        }
1197
1198        if let Ok(mut json) = serde_json::from_str::<serde_json::Value>(&content) {
1199            if let Some(obj) = json.as_object_mut() {
1200                let servers = obj
1201                    .entry("mcpServers")
1202                    .or_insert_with(|| serde_json::json!({}));
1203                if let Some(servers_obj) = servers.as_object_mut() {
1204                    servers_obj.insert(
1205                        "lean-ctx".to_string(),
1206                        serde_json::json!({ "command": binary }),
1207                    );
1208                }
1209                if let Ok(formatted) = serde_json::to_string_pretty(&json) {
1210                    let _ = std::fs::write(config_path, formatted);
1211                    println!("  \x1b[32m✓\x1b[0m {name} MCP configured at {display_path}");
1212                    return;
1213                }
1214            }
1215        }
1216    }
1217
1218    let content = serde_json::to_string_pretty(&serde_json::json!({
1219        "mcpServers": {
1220            "lean-ctx": {
1221                "command": binary
1222            }
1223        }
1224    }));
1225
1226    if let Ok(json_str) = content {
1227        let _ = std::fs::write(config_path, json_str);
1228        println!("  \x1b[32m✓\x1b[0m {name} MCP configured at {display_path}");
1229    } else {
1230        eprintln!("  \x1b[31m✗\x1b[0m Failed to configure {name}");
1231    }
1232}
1233
1234#[cfg(test)]
1235mod tests {
1236    use super::*;
1237
1238    #[test]
1239    fn bash_path_unix_unchanged() {
1240        assert_eq!(
1241            to_bash_compatible_path("/usr/local/bin/lean-ctx"),
1242            "/usr/local/bin/lean-ctx"
1243        );
1244    }
1245
1246    #[test]
1247    fn bash_path_home_unchanged() {
1248        assert_eq!(
1249            to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
1250            "/home/user/.cargo/bin/lean-ctx"
1251        );
1252    }
1253
1254    #[test]
1255    fn bash_path_windows_drive_converted() {
1256        assert_eq!(
1257            to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
1258            "/c/Users/Fraser/bin/lean-ctx.exe"
1259        );
1260    }
1261
1262    #[test]
1263    fn bash_path_windows_lowercase_drive() {
1264        assert_eq!(
1265            to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
1266            "/d/tools/lean-ctx.exe"
1267        );
1268    }
1269
1270    #[test]
1271    fn bash_path_windows_forward_slashes() {
1272        assert_eq!(
1273            to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
1274            "/c/Users/Fraser/bin/lean-ctx.exe"
1275        );
1276    }
1277
1278    #[test]
1279    fn bash_path_bare_name_unchanged() {
1280        assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
1281    }
1282
1283    #[test]
1284    fn normalize_msys2_path() {
1285        assert_eq!(
1286            normalize_tool_path("/c/Users/game/Downloads/project"),
1287            "C:/Users/game/Downloads/project"
1288        );
1289    }
1290
1291    #[test]
1292    fn normalize_msys2_drive_d() {
1293        assert_eq!(
1294            normalize_tool_path("/d/Projects/app/src"),
1295            "D:/Projects/app/src"
1296        );
1297    }
1298
1299    #[test]
1300    fn normalize_backslashes() {
1301        assert_eq!(
1302            normalize_tool_path("C:\\Users\\game\\project\\src"),
1303            "C:/Users/game/project/src"
1304        );
1305    }
1306
1307    #[test]
1308    fn normalize_mixed_separators() {
1309        assert_eq!(
1310            normalize_tool_path("C:\\Users/game\\project/src"),
1311            "C:/Users/game/project/src"
1312        );
1313    }
1314
1315    #[test]
1316    fn normalize_double_slashes() {
1317        assert_eq!(
1318            normalize_tool_path("/home/user//project///src"),
1319            "/home/user/project/src"
1320        );
1321    }
1322
1323    #[test]
1324    fn normalize_trailing_slash() {
1325        assert_eq!(
1326            normalize_tool_path("/home/user/project/"),
1327            "/home/user/project"
1328        );
1329    }
1330
1331    #[test]
1332    fn normalize_root_preserved() {
1333        assert_eq!(normalize_tool_path("/"), "/");
1334    }
1335
1336    #[test]
1337    fn normalize_windows_root_preserved() {
1338        assert_eq!(normalize_tool_path("C:/"), "C:/");
1339    }
1340
1341    #[test]
1342    fn normalize_unix_path_unchanged() {
1343        assert_eq!(
1344            normalize_tool_path("/home/user/project/src/main.rs"),
1345            "/home/user/project/src/main.rs"
1346        );
1347    }
1348
1349    #[test]
1350    fn normalize_relative_path_unchanged() {
1351        assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
1352    }
1353
1354    #[test]
1355    fn normalize_dot_unchanged() {
1356        assert_eq!(normalize_tool_path("."), ".");
1357    }
1358
1359    #[test]
1360    fn normalize_unc_path_preserved() {
1361        assert_eq!(
1362            normalize_tool_path("//server/share/file"),
1363            "//server/share/file"
1364        );
1365    }
1366
1367    #[test]
1368    fn cursor_hook_config_has_version_and_object_hooks() {
1369        let config = serde_json::json!({
1370            "version": 1,
1371            "hooks": {
1372                "preToolUse": [
1373                    {
1374                        "matcher": "terminal_command",
1375                        "command": "lean-ctx hook rewrite"
1376                    },
1377                    {
1378                        "matcher": "read_file|grep|search|list_files|list_directory",
1379                        "command": "lean-ctx hook redirect"
1380                    }
1381                ]
1382            }
1383        });
1384
1385        let json_str = serde_json::to_string_pretty(&config).unwrap();
1386        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
1387
1388        assert_eq!(parsed["version"], 1);
1389        assert!(parsed["hooks"].is_object());
1390        assert!(parsed["hooks"]["preToolUse"].is_array());
1391        assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
1392        assert_eq!(
1393            parsed["hooks"]["preToolUse"][0]["matcher"],
1394            "terminal_command"
1395        );
1396    }
1397
1398    #[test]
1399    fn cursor_hook_detects_old_format_needs_migration() {
1400        let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
1401        let has_correct =
1402            old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
1403        assert!(
1404            !has_correct,
1405            "Old format should be detected as needing migration"
1406        );
1407    }
1408
1409    #[test]
1410    fn gemini_hook_config_has_type_command() {
1411        let binary = "lean-ctx";
1412        let rewrite_cmd = format!("{binary} hook rewrite");
1413        let redirect_cmd = format!("{binary} hook redirect");
1414
1415        let hook_config = serde_json::json!({
1416            "hooks": {
1417                "BeforeTool": [
1418                    {
1419                        "hooks": [{
1420                            "type": "command",
1421                            "command": rewrite_cmd
1422                        }]
1423                    },
1424                    {
1425                        "hooks": [{
1426                            "type": "command",
1427                            "command": redirect_cmd
1428                        }]
1429                    }
1430                ]
1431            }
1432        });
1433
1434        let parsed = hook_config;
1435        let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
1436        assert_eq!(before_tool.len(), 2);
1437
1438        let first_hook = &before_tool[0]["hooks"][0];
1439        assert_eq!(first_hook["type"], "command");
1440        assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
1441
1442        let second_hook = &before_tool[1]["hooks"][0];
1443        assert_eq!(second_hook["type"], "command");
1444        assert_eq!(second_hook["command"], "lean-ctx hook redirect");
1445    }
1446
1447    #[test]
1448    fn gemini_hook_old_format_detected() {
1449        let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
1450        let has_new = old_format.contains("hook rewrite")
1451            && old_format.contains("hook redirect")
1452            && old_format.contains("\"type\"");
1453        assert!(!has_new, "Missing 'type' field should trigger migration");
1454    }
1455}