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