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