Skip to main content

lean_ctx/hooks/
mod.rs

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