Skip to main content

lean_ctx/hooks/
mod.rs

1use std::path::PathBuf;
2
3pub mod agents;
4mod support;
5use agents::*;
6use support::{
7    ensure_codex_hooks_enabled, install_codex_instruction_docs, install_named_json_server,
8    upsert_lean_ctx_codex_hook_entries,
9};
10
11fn mcp_server_quiet_mode() -> bool {
12    std::env::var_os("LEAN_CTX_MCP_SERVER").is_some()
13        || matches!(std::env::var("LEAN_CTX_QUIET"), Ok(value) if value.trim() == "1")
14}
15
16/// Silently refresh all hook scripts for agents that are already configured.
17/// Called after updates and on MCP server start to ensure hooks match the current binary version.
18pub fn refresh_installed_hooks() {
19    let home = match dirs::home_dir() {
20        Some(h) => h,
21        None => return,
22    };
23
24    let claude_dir = crate::setup::claude_config_dir(&home);
25    let claude_hooks = claude_dir.join("hooks/lean-ctx-rewrite.sh").exists()
26        || claude_dir.join("settings.json").exists()
27            && std::fs::read_to_string(claude_dir.join("settings.json"))
28                .unwrap_or_default()
29                .contains("lean-ctx");
30
31    if claude_hooks {
32        install_claude_hook_scripts(&home);
33        install_claude_hook_config(&home);
34    }
35
36    let cursor_hooks = home.join(".cursor/hooks/lean-ctx-rewrite.sh").exists()
37        || home.join(".cursor/hooks.json").exists()
38            && std::fs::read_to_string(home.join(".cursor/hooks.json"))
39                .unwrap_or_default()
40                .contains("lean-ctx");
41
42    if cursor_hooks {
43        install_cursor_hook_scripts(&home);
44        install_cursor_hook_config(&home);
45    }
46
47    let gemini_rewrite = home.join(".gemini/hooks/lean-ctx-rewrite-gemini.sh");
48    let gemini_legacy = home.join(".gemini/hooks/lean-ctx-hook-gemini.sh");
49    if gemini_rewrite.exists() || gemini_legacy.exists() {
50        install_gemini_hook_scripts(&home);
51        install_gemini_hook_config(&home);
52    }
53
54    let codex_hooks = home.join(".codex/hooks/lean-ctx-rewrite-codex.sh").exists()
55        || home.join(".codex/hooks.json").exists()
56            && std::fs::read_to_string(home.join(".codex/hooks.json"))
57                .unwrap_or_default()
58                .contains("lean-ctx");
59
60    if codex_hooks {
61        install_codex_hook();
62    }
63}
64
65fn resolve_binary_path() -> String {
66    if is_lean_ctx_in_path() {
67        return "lean-ctx".to_string();
68    }
69    crate::core::portable_binary::resolve_portable_binary()
70}
71
72fn is_lean_ctx_in_path() -> bool {
73    let which_cmd = if cfg!(windows) { "where" } else { "which" };
74    std::process::Command::new(which_cmd)
75        .arg("lean-ctx")
76        .stdout(std::process::Stdio::null())
77        .stderr(std::process::Stdio::null())
78        .status()
79        .map(|s| s.success())
80        .unwrap_or(false)
81}
82
83fn resolve_binary_path_for_bash() -> String {
84    let path = resolve_binary_path();
85    to_bash_compatible_path(&path)
86}
87
88pub fn to_bash_compatible_path(path: &str) -> String {
89    let path = match crate::core::pathutil::strip_verbatim_str(path) {
90        Some(stripped) => stripped,
91        None => path.replace('\\', "/"),
92    };
93    if path.len() >= 2 && path.as_bytes()[1] == b':' {
94        let drive = (path.as_bytes()[0] as char).to_ascii_lowercase();
95        format!("/{drive}{}", &path[2..])
96    } else {
97        path
98    }
99}
100
101/// Normalize paths from any client format to a consistent OS-native form.
102/// Handles MSYS2/Git Bash (`/c/Users/...` -> `C:/Users/...`), mixed separators,
103/// double slashes, and trailing slashes. Always uses forward slashes for consistency.
104pub fn normalize_tool_path(path: &str) -> String {
105    let mut p = match crate::core::pathutil::strip_verbatim_str(path) {
106        Some(stripped) => stripped,
107        None => path.to_string(),
108    };
109
110    // MSYS2/Git Bash: /c/Users/... -> C:/Users/...
111    if p.len() >= 3
112        && p.starts_with('/')
113        && p.as_bytes()[1].is_ascii_alphabetic()
114        && p.as_bytes()[2] == b'/'
115    {
116        let drive = p.as_bytes()[1].to_ascii_uppercase() as char;
117        p = format!("{drive}:{}", &p[2..]);
118    }
119
120    p = p.replace('\\', "/");
121
122    // Collapse double slashes (preserve UNC paths starting with //)
123    while p.contains("//") && !p.starts_with("//") {
124        p = p.replace("//", "/");
125    }
126
127    // Remove trailing slash (unless root like "/" or "C:/")
128    if p.len() > 1 && p.ends_with('/') && !p.ends_with(":/") {
129        p.pop();
130    }
131
132    p
133}
134
135pub fn generate_rewrite_script(binary: &str) -> String {
136    let case_pattern = crate::rewrite_registry::bash_case_pattern();
137    format!(
138        r#"#!/usr/bin/env bash
139# lean-ctx PreToolUse hook — rewrites bash commands to lean-ctx equivalents
140set -euo pipefail
141
142LEAN_CTX_BIN="{binary}"
143
144INPUT=$(cat)
145TOOL=$(echo "$INPUT" | grep -oE '"tool_name":"([^"\\]|\\.)*"' | head -1 | sed 's/^"tool_name":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
146
147if [ "$TOOL" != "Bash" ] && [ "$TOOL" != "bash" ]; then
148  exit 0
149fi
150
151CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
152
153if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then
154  exit 0
155fi
156
157case "$CMD" in
158  {case_pattern})
159    # Shell-escape then JSON-escape (two passes)
160    SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
161    REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
162    JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
163    printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD"
164    ;;
165  *) exit 0 ;;
166esac
167"#
168    )
169}
170
171pub fn generate_compact_rewrite_script(binary: &str) -> String {
172    let case_pattern = crate::rewrite_registry::bash_case_pattern();
173    format!(
174        r#"#!/usr/bin/env bash
175# lean-ctx hook — rewrites shell commands
176set -euo pipefail
177LEAN_CTX_BIN="{binary}"
178INPUT=$(cat)
179CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g' 2>/dev/null || echo "")
180if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then exit 0; fi
181case "$CMD" in
182  {case_pattern})
183    SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
184    REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
185    JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
186    printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD" ;;
187  *) exit 0 ;;
188esac
189"#
190    )
191}
192
193const REDIRECT_SCRIPT_CLAUDE: &str = r#"#!/usr/bin/env bash
194# lean-ctx PreToolUse hook — all native tools pass through
195# Read/Grep/ListFiles are allowed so Edit (which requires native Read) works.
196# The MCP instructions guide the AI to prefer ctx_read/ctx_search/ctx_tree.
197exit 0
198"#;
199
200const REDIRECT_SCRIPT_GENERIC: &str = r#"#!/usr/bin/env bash
201# lean-ctx hook — all native tools pass through
202exit 0
203"#;
204
205pub fn install_project_rules() {
206    if crate::core::config::Config::load().rules_scope_effective()
207        == crate::core::config::RulesScope::Global
208    {
209        return;
210    }
211
212    let cwd = std::env::current_dir().unwrap_or_default();
213
214    if !is_inside_git_repo(&cwd) {
215        eprintln!(
216            "  Skipping project files: not inside a git repository.\n  \
217             Run this command from your project root to create CLAUDE.md / AGENTS.md."
218        );
219        return;
220    }
221
222    let home = dirs::home_dir().unwrap_or_default();
223    if cwd == home {
224        eprintln!(
225            "  Skipping project files: current directory is your home folder.\n  \
226             Run this command from a project directory instead."
227        );
228        return;
229    }
230
231    ensure_project_agents_integration(&cwd);
232
233    let cursorrules = cwd.join(".cursorrules");
234    if !cursorrules.exists()
235        || !std::fs::read_to_string(&cursorrules)
236            .unwrap_or_default()
237            .contains("lean-ctx")
238    {
239        let content = CURSORRULES_TEMPLATE;
240        if cursorrules.exists() {
241            let mut existing = std::fs::read_to_string(&cursorrules).unwrap_or_default();
242            if !existing.ends_with('\n') {
243                existing.push('\n');
244            }
245            existing.push('\n');
246            existing.push_str(content);
247            write_file(&cursorrules, &existing);
248        } else {
249            write_file(&cursorrules, content);
250        }
251        println!("Created/updated .cursorrules in project root.");
252    }
253
254    let claude_rules_dir = cwd.join(".claude").join("rules");
255    let claude_rules_file = claude_rules_dir.join("lean-ctx.md");
256    if !claude_rules_file.exists()
257        || !std::fs::read_to_string(&claude_rules_file)
258            .unwrap_or_default()
259            .contains(crate::rules_inject::RULES_VERSION_STR)
260    {
261        let _ = std::fs::create_dir_all(&claude_rules_dir);
262        write_file(
263            &claude_rules_file,
264            crate::rules_inject::rules_dedicated_markdown(),
265        );
266        println!("Created .claude/rules/lean-ctx.md (Claude Code project rules).");
267    }
268
269    install_claude_project_hooks(&cwd);
270
271    let kiro_dir = cwd.join(".kiro");
272    if kiro_dir.exists() {
273        let steering_dir = kiro_dir.join("steering");
274        let steering_file = steering_dir.join("lean-ctx.md");
275        if !steering_file.exists()
276            || !std::fs::read_to_string(&steering_file)
277                .unwrap_or_default()
278                .contains("lean-ctx")
279        {
280            let _ = std::fs::create_dir_all(&steering_dir);
281            write_file(&steering_file, KIRO_STEERING_TEMPLATE);
282            println!("Created .kiro/steering/lean-ctx.md (Kiro steering).");
283        }
284    }
285}
286
287const PROJECT_LEAN_CTX_MD_MARKER: &str = "<!-- lean-ctx-owned: PROJECT-LEAN-CTX.md v1 -->";
288const PROJECT_LEAN_CTX_MD: &str = "LEAN-CTX.md";
289const PROJECT_AGENTS_MD: &str = "AGENTS.md";
290const AGENTS_BLOCK_START: &str = "<!-- lean-ctx -->";
291const AGENTS_BLOCK_END: &str = "<!-- /lean-ctx -->";
292
293fn ensure_project_agents_integration(cwd: &std::path::Path) {
294    let lean_ctx_md = cwd.join(PROJECT_LEAN_CTX_MD);
295    let desired = format!(
296        "{PROJECT_LEAN_CTX_MD_MARKER}\n{}\n",
297        crate::rules_inject::rules_dedicated_markdown()
298    );
299
300    if !lean_ctx_md.exists() {
301        write_file(&lean_ctx_md, &desired);
302    } else if std::fs::read_to_string(&lean_ctx_md)
303        .unwrap_or_default()
304        .contains(PROJECT_LEAN_CTX_MD_MARKER)
305    {
306        let current = std::fs::read_to_string(&lean_ctx_md).unwrap_or_default();
307        if !current.contains(crate::rules_inject::RULES_VERSION_STR) {
308            write_file(&lean_ctx_md, &desired);
309        }
310    }
311
312    let block = format!(
313        "{AGENTS_BLOCK_START}\n\
314## lean-ctx\n\n\
315Prefer lean-ctx MCP tools over native equivalents for token savings.\n\
316Full rules: @{PROJECT_LEAN_CTX_MD}\n\
317{AGENTS_BLOCK_END}\n"
318    );
319
320    let agents_md = cwd.join(PROJECT_AGENTS_MD);
321    if !agents_md.exists() {
322        let content = format!("# Agent Instructions\n\n{block}");
323        write_file(&agents_md, &content);
324        println!("Created AGENTS.md in project root (lean-ctx reference only).");
325        return;
326    }
327
328    let existing = std::fs::read_to_string(&agents_md).unwrap_or_default();
329    if existing.contains(AGENTS_BLOCK_START) {
330        let updated = replace_marked_block(&existing, AGENTS_BLOCK_START, AGENTS_BLOCK_END, &block);
331        if updated != existing {
332            write_file(&agents_md, &updated);
333        }
334        return;
335    }
336
337    if existing.contains("lean-ctx") && existing.contains(PROJECT_LEAN_CTX_MD) {
338        return;
339    }
340
341    let mut out = existing;
342    if !out.ends_with('\n') {
343        out.push('\n');
344    }
345    out.push('\n');
346    out.push_str(&block);
347    write_file(&agents_md, &out);
348    println!("Updated AGENTS.md (added lean-ctx reference block).");
349}
350
351fn replace_marked_block(content: &str, start: &str, end: &str, replacement: &str) -> String {
352    let s = content.find(start);
353    let e = content.find(end);
354    match (s, e) {
355        (Some(si), Some(ei)) if ei >= si => {
356            let after_end = ei + end.len();
357            let before = &content[..si];
358            let after = &content[after_end..];
359            let mut out = String::new();
360            out.push_str(before.trim_end_matches('\n'));
361            out.push('\n');
362            out.push('\n');
363            out.push_str(replacement.trim_end_matches('\n'));
364            out.push('\n');
365            out.push_str(after.trim_start_matches('\n'));
366            out
367        }
368        _ => content.to_string(),
369    }
370}
371
372const CURSORRULES_TEMPLATE: &str = "\
373# lean-ctx — Context Engineering Layer
374
375PREFER lean-ctx MCP tools over native equivalents for token savings:
376
377| PREFER | OVER | Why |
378|--------|------|-----|
379| `ctx_read(path)` | `Read` | Cached, 10 compression modes |
380| `ctx_shell(command)` | `Shell` | Pattern compression |
381| `ctx_search(pattern, path)` | `Grep` | Compact results |
382| `ctx_tree(path, depth)` | `ls` / `find` | Directory maps |
383| `ctx_edit(path, old_string, new_string)` | `Edit` (when Read unavailable) | Search-and-replace without native Read |
384
385Edit files: use native Edit/StrReplace if available. If Edit requires Read and Read is unavailable, use ctx_edit.
386Write, Delete, Glob — use normally. NEVER loop on Edit failures — switch to ctx_edit immediately.
387";
388
389pub const KIRO_STEERING_TEMPLATE: &str = "\
390---
391inclusion: always
392---
393
394# lean-ctx — Context Engineering Layer
395
396The workspace has the `lean-ctx` MCP server installed. You MUST prefer lean-ctx tools over native equivalents for token efficiency and caching.
397
398## Mandatory Tool Preferences
399
400| Use this | Instead of | Why |
401|----------|-----------|-----|
402| `mcp_lean_ctx_ctx_read` | `readFile`, `readCode` | Cached reads, 10 compression modes, re-reads cost ~13 tokens |
403| `mcp_lean_ctx_ctx_multi_read` | `readMultipleFiles` | Batch cached reads in one call |
404| `mcp_lean_ctx_ctx_shell` | `executeBash` | Pattern compression for git/npm/test output |
405| `mcp_lean_ctx_ctx_search` | `grepSearch` | Compact, .gitignore-aware results |
406| `mcp_lean_ctx_ctx_tree` | `listDirectory` | Compact directory maps with file counts |
407
408## When to use native Kiro tools instead
409
410- `fsWrite` / `fsAppend` — always use native (lean-ctx doesn't write files)
411- `strReplace` — always use native (precise string replacement)
412- `semanticRename` / `smartRelocate` — always use native (IDE integration)
413- `getDiagnostics` — always use native (language server diagnostics)
414- `deleteFile` — always use native
415
416## Session management
417
418- At the start of a long task, call `mcp_lean_ctx_ctx_preload` with a task description to warm the cache
419- Use `mcp_lean_ctx_ctx_compress` periodically in long conversations to checkpoint context
420- Use `mcp_lean_ctx_ctx_knowledge` to persist important discoveries across sessions
421
422## Rules
423
424- NEVER loop on edit failures — switch to `mcp_lean_ctx_ctx_edit` immediately
425- For large files, use `mcp_lean_ctx_ctx_read` with `mode: \"signatures\"` or `mode: \"map\"` first
426- For re-reading a file you already read, just call `mcp_lean_ctx_ctx_read` again (cache hit = ~13 tokens)
427- When running tests or build commands, use `mcp_lean_ctx_ctx_shell` for compressed output
428";
429
430pub fn install_agent_hook(agent: &str, global: bool) {
431    match agent {
432        "claude" | "claude-code" => install_claude_hook(global),
433        "cursor" => install_cursor_hook(global),
434        "gemini" | "antigravity" => install_gemini_hook(),
435        "codex" => install_codex_hook(),
436        "windsurf" => install_windsurf_rules(global),
437        "cline" | "roo" => install_cline_rules(global),
438        "copilot" => install_copilot_hook(global),
439        "pi" => install_pi_hook(global),
440        "qwen" => install_mcp_json_agent(
441            "Qwen Code",
442            "~/.qwen/mcp.json",
443            &dirs::home_dir().unwrap_or_default().join(".qwen/mcp.json"),
444        ),
445        "trae" => install_mcp_json_agent(
446            "Trae",
447            "~/.trae/mcp.json",
448            &dirs::home_dir().unwrap_or_default().join(".trae/mcp.json"),
449        ),
450        "amazonq" => install_mcp_json_agent(
451            "Amazon Q Developer",
452            "~/.aws/amazonq/mcp.json",
453            &dirs::home_dir()
454                .unwrap_or_default()
455                .join(".aws/amazonq/mcp.json"),
456        ),
457        "jetbrains" => install_jetbrains_hook(),
458        "kiro" => install_kiro_hook(),
459        "verdent" => install_mcp_json_agent(
460            "Verdent",
461            "~/.verdent/mcp.json",
462            &dirs::home_dir()
463                .unwrap_or_default()
464                .join(".verdent/mcp.json"),
465        ),
466        "opencode" => install_opencode_hook(),
467        "aider" => install_mcp_json_agent(
468            "Aider",
469            "~/.aider/mcp.json",
470            &dirs::home_dir().unwrap_or_default().join(".aider/mcp.json"),
471        ),
472        "amp" => install_amp_hook(),
473        "crush" => install_crush_hook(),
474        "hermes" => install_hermes_hook(global),
475        _ => {
476            eprintln!("Unknown agent: {agent}");
477            eprintln!("  Supported: claude, cursor, gemini, codex, windsurf, cline, roo, copilot, pi, qwen, trae, amazonq, jetbrains, kiro, verdent, opencode, aider, amp, crush, antigravity, hermes");
478            std::process::exit(1);
479        }
480    }
481}
482
483fn write_file(path: &std::path::Path, content: &str) {
484    if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
485        eprintln!("Error writing {}: {e}", path.display());
486    }
487}
488
489fn is_inside_git_repo(path: &std::path::Path) -> bool {
490    let mut p = path;
491    loop {
492        if p.join(".git").exists() {
493            return true;
494        }
495        match p.parent() {
496            Some(parent) => p = parent,
497            None => return false,
498        }
499    }
500}
501
502#[cfg(unix)]
503fn make_executable(path: &PathBuf) {
504    use std::os::unix::fs::PermissionsExt;
505    let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
506}
507
508#[cfg(not(unix))]
509fn make_executable(_path: &PathBuf) {}
510
511fn full_server_entry(binary: &str) -> serde_json::Value {
512    let data_dir = crate::core::data_dir::lean_ctx_data_dir()
513        .map(|d| d.to_string_lossy().to_string())
514        .unwrap_or_default();
515    let auto_approve = crate::core::editor_registry::auto_approve_tools();
516    serde_json::json!({
517        "command": binary,
518        "env": { "LEAN_CTX_DATA_DIR": data_dir },
519        "autoApprove": auto_approve
520    })
521}
522
523fn install_mcp_json_agent(name: &str, display_path: &str, config_path: &std::path::Path) {
524    let binary = resolve_binary_path();
525    let entry = full_server_entry(&binary);
526    install_named_json_server(name, display_path, config_path, "mcpServers", entry);
527}
528
529#[cfg(test)]
530mod tests {
531    use super::*;
532
533    #[test]
534    fn bash_path_unix_unchanged() {
535        assert_eq!(
536            to_bash_compatible_path("/usr/local/bin/lean-ctx"),
537            "/usr/local/bin/lean-ctx"
538        );
539    }
540
541    #[test]
542    fn bash_path_home_unchanged() {
543        assert_eq!(
544            to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
545            "/home/user/.cargo/bin/lean-ctx"
546        );
547    }
548
549    #[test]
550    fn bash_path_windows_drive_converted() {
551        assert_eq!(
552            to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
553            "/c/Users/Fraser/bin/lean-ctx.exe"
554        );
555    }
556
557    #[test]
558    fn bash_path_windows_lowercase_drive() {
559        assert_eq!(
560            to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
561            "/d/tools/lean-ctx.exe"
562        );
563    }
564
565    #[test]
566    fn bash_path_windows_forward_slashes() {
567        assert_eq!(
568            to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
569            "/c/Users/Fraser/bin/lean-ctx.exe"
570        );
571    }
572
573    #[test]
574    fn bash_path_bare_name_unchanged() {
575        assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
576    }
577
578    #[test]
579    fn normalize_msys2_path() {
580        assert_eq!(
581            normalize_tool_path("/c/Users/game/Downloads/project"),
582            "C:/Users/game/Downloads/project"
583        );
584    }
585
586    #[test]
587    fn normalize_msys2_drive_d() {
588        assert_eq!(
589            normalize_tool_path("/d/Projects/app/src"),
590            "D:/Projects/app/src"
591        );
592    }
593
594    #[test]
595    fn normalize_backslashes() {
596        assert_eq!(
597            normalize_tool_path("C:\\Users\\game\\project\\src"),
598            "C:/Users/game/project/src"
599        );
600    }
601
602    #[test]
603    fn normalize_mixed_separators() {
604        assert_eq!(
605            normalize_tool_path("C:\\Users/game\\project/src"),
606            "C:/Users/game/project/src"
607        );
608    }
609
610    #[test]
611    fn normalize_double_slashes() {
612        assert_eq!(
613            normalize_tool_path("/home/user//project///src"),
614            "/home/user/project/src"
615        );
616    }
617
618    #[test]
619    fn normalize_trailing_slash() {
620        assert_eq!(
621            normalize_tool_path("/home/user/project/"),
622            "/home/user/project"
623        );
624    }
625
626    #[test]
627    fn normalize_root_preserved() {
628        assert_eq!(normalize_tool_path("/"), "/");
629    }
630
631    #[test]
632    fn normalize_windows_root_preserved() {
633        assert_eq!(normalize_tool_path("C:/"), "C:/");
634    }
635
636    #[test]
637    fn normalize_unix_path_unchanged() {
638        assert_eq!(
639            normalize_tool_path("/home/user/project/src/main.rs"),
640            "/home/user/project/src/main.rs"
641        );
642    }
643
644    #[test]
645    fn normalize_relative_path_unchanged() {
646        assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
647    }
648
649    #[test]
650    fn normalize_dot_unchanged() {
651        assert_eq!(normalize_tool_path("."), ".");
652    }
653
654    #[test]
655    fn normalize_unc_path_preserved() {
656        assert_eq!(
657            normalize_tool_path("//server/share/file"),
658            "//server/share/file"
659        );
660    }
661
662    #[test]
663    fn cursor_hook_config_has_version_and_object_hooks() {
664        let config = serde_json::json!({
665            "version": 1,
666            "hooks": {
667                "preToolUse": [
668                    {
669                        "matcher": "terminal_command",
670                        "command": "lean-ctx hook rewrite"
671                    },
672                    {
673                        "matcher": "read_file|grep|search|list_files|list_directory",
674                        "command": "lean-ctx hook redirect"
675                    }
676                ]
677            }
678        });
679
680        let json_str = serde_json::to_string_pretty(&config).unwrap();
681        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
682
683        assert_eq!(parsed["version"], 1);
684        assert!(parsed["hooks"].is_object());
685        assert!(parsed["hooks"]["preToolUse"].is_array());
686        assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
687        assert_eq!(
688            parsed["hooks"]["preToolUse"][0]["matcher"],
689            "terminal_command"
690        );
691    }
692
693    #[test]
694    fn cursor_hook_detects_old_format_needs_migration() {
695        let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
696        let has_correct =
697            old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
698        assert!(
699            !has_correct,
700            "Old format should be detected as needing migration"
701        );
702    }
703
704    #[test]
705    fn gemini_hook_config_has_type_command() {
706        let binary = "lean-ctx";
707        let rewrite_cmd = format!("{binary} hook rewrite");
708        let redirect_cmd = format!("{binary} hook redirect");
709
710        let hook_config = serde_json::json!({
711            "hooks": {
712                "BeforeTool": [
713                    {
714                        "hooks": [{
715                            "type": "command",
716                            "command": rewrite_cmd
717                        }]
718                    },
719                    {
720                        "hooks": [{
721                            "type": "command",
722                            "command": redirect_cmd
723                        }]
724                    }
725                ]
726            }
727        });
728
729        let parsed = hook_config;
730        let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
731        assert_eq!(before_tool.len(), 2);
732
733        let first_hook = &before_tool[0]["hooks"][0];
734        assert_eq!(first_hook["type"], "command");
735        assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
736
737        let second_hook = &before_tool[1]["hooks"][0];
738        assert_eq!(second_hook["type"], "command");
739        assert_eq!(second_hook["command"], "lean-ctx hook redirect");
740    }
741
742    #[test]
743    fn gemini_hook_old_format_detected() {
744        let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
745        let has_new = old_format.contains("hook rewrite")
746            && old_format.contains("hook redirect")
747            && old_format.contains("\"type\"");
748        assert!(!has_new, "Missing 'type' field should trigger migration");
749    }
750
751    #[test]
752    fn rewrite_script_uses_registry_pattern() {
753        let script = generate_rewrite_script("/usr/bin/lean-ctx");
754        assert!(script.contains(r"git\ *"), "script missing git pattern");
755        assert!(script.contains(r"cargo\ *"), "script missing cargo pattern");
756        assert!(script.contains(r"npm\ *"), "script missing npm pattern");
757        assert!(
758            !script.contains(r"rg\ *"),
759            "script should not contain rg pattern"
760        );
761        assert!(
762            script.contains("LEAN_CTX_BIN=\"/usr/bin/lean-ctx\""),
763            "script missing binary path"
764        );
765    }
766
767    #[test]
768    fn compact_rewrite_script_uses_registry_pattern() {
769        let script = generate_compact_rewrite_script("/usr/bin/lean-ctx");
770        assert!(script.contains(r"git\ *"), "compact script missing git");
771        assert!(script.contains(r"cargo\ *"), "compact script missing cargo");
772        assert!(
773            !script.contains(r"rg\ *"),
774            "compact script should not contain rg"
775        );
776    }
777
778    #[test]
779    fn rewrite_scripts_contain_all_registry_commands() {
780        let script = generate_rewrite_script("lean-ctx");
781        let compact = generate_compact_rewrite_script("lean-ctx");
782        for entry in crate::rewrite_registry::REWRITE_COMMANDS {
783            if entry.category == crate::rewrite_registry::Category::Search {
784                continue;
785            }
786            let pattern = if entry.command.contains('-') {
787                format!("{}*", entry.command.replace('-', r"\-"))
788            } else {
789                format!(r"{}\ *", entry.command)
790            };
791            assert!(
792                script.contains(&pattern),
793                "rewrite_script missing '{}' (pattern: {})",
794                entry.command,
795                pattern
796            );
797            assert!(
798                compact.contains(&pattern),
799                "compact_rewrite_script missing '{}' (pattern: {})",
800                entry.command,
801                pattern
802            );
803        }
804    }
805}