1use std::path::PathBuf;
2
3pub mod agents;
4mod support;
5
6#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum HookMode {
13 #[default]
14 Mcp,
15 Hybrid,
16}
17
18impl std::fmt::Display for HookMode {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 match self {
21 Self::Mcp => write!(f, "MCP"),
22 Self::Hybrid => write!(f, "Hybrid"),
23 }
24 }
25}
26
27impl HookMode {
28 pub fn from_str_loose(s: &str) -> Option<Self> {
29 match s.to_lowercase().replace('-', "").as_str() {
30 "mcp" => Some(Self::Mcp),
31 "hybrid" => Some(Self::Hybrid),
32 _ => None,
33 }
34 }
35
36 pub fn description(&self) -> &'static str {
37 match self {
38 Self::Mcp => "MCP server only (extension/plugin-based agents without reliable shell)",
39 Self::Hybrid => "MCP server + shell hooks for command compression (best of both)",
40 }
41 }
42}
43
44pub fn recommend_hook_mode(agent_key: &str) -> HookMode {
51 match agent_key {
52 "cursor" | "gemini" | "codex" | "claude" | "claude-code" | "crush" | "hermes"
59 | "opencode" | "pi" | "qoder" | "windsurf" | "amp" | "cline" | "roo" | "copilot"
60 | "kiro" | "qwen" | "trae" | "antigravity" | "amazonq" | "verdent" => HookMode::Hybrid,
61
62 _ => HookMode::Mcp,
64 }
65}
66use agents::{
67 install_amp_hook, install_antigravity_hook, install_claude_hook_config,
68 install_claude_hook_scripts, install_claude_hook_with_mode, install_claude_project_hooks,
69 install_cline_rules, install_codex_hook, install_copilot_hook, install_crush_hook_with_mode,
70 install_cursor_hook_config, install_cursor_hook_scripts, install_cursor_hook_with_mode,
71 install_gemini_hook, install_gemini_hook_config, install_gemini_hook_scripts,
72 install_hermes_hook_with_mode, install_jetbrains_hook, install_kiro_hook,
73 install_opencode_hook_with_mode, install_pi_hook_with_mode, install_qoder_hook_with_mode,
74 install_windsurf_rules,
75};
76use support::{
77 ensure_codex_hooks_enabled, install_codex_instruction_docs, install_named_json_server,
78 upsert_lean_ctx_codex_hook_entries,
79};
80
81fn mcp_server_quiet_mode() -> bool {
82 std::env::var_os("LEAN_CTX_MCP_SERVER").is_some()
83 || matches!(std::env::var("LEAN_CTX_QUIET"), Ok(value) if value.trim() == "1")
84}
85
86pub fn refresh_installed_hooks() {
89 let Some(home) = crate::core::home::resolve_home_dir() else {
90 return;
91 };
92
93 let claude_dir = crate::setup::claude_config_dir(&home);
94 let claude_hooks = claude_dir.join("hooks/lean-ctx-rewrite.sh").exists()
95 || claude_dir.join("settings.json").exists()
96 && std::fs::read_to_string(claude_dir.join("settings.json"))
97 .unwrap_or_default()
98 .contains("lean-ctx");
99
100 if claude_hooks {
101 install_claude_hook_scripts(&home);
102 install_claude_hook_config(&home);
103 }
104
105 let cursor_hooks = home.join(".cursor/hooks/lean-ctx-rewrite.sh").exists()
106 || home.join(".cursor/hooks.json").exists()
107 && std::fs::read_to_string(home.join(".cursor/hooks.json"))
108 .unwrap_or_default()
109 .contains("lean-ctx");
110
111 if cursor_hooks {
112 install_cursor_hook_scripts(&home);
113 install_cursor_hook_config(&home);
114 }
115
116 let gemini_rewrite = home.join(".gemini/hooks/lean-ctx-rewrite-gemini.sh");
117 let gemini_legacy = home.join(".gemini/hooks/lean-ctx-hook-gemini.sh");
118 if gemini_rewrite.exists() || gemini_legacy.exists() {
119 install_gemini_hook_scripts(&home);
120 install_gemini_hook_config(&home);
121 }
122
123 let codex_dir = crate::core::home::resolve_codex_dir().unwrap_or_else(|| home.join(".codex"));
124 let codex_hooks = codex_dir.join("hooks/lean-ctx-rewrite-codex.sh").exists()
125 || codex_dir.join("hooks.json").exists()
126 && std::fs::read_to_string(codex_dir.join("hooks.json"))
127 .unwrap_or_default()
128 .contains("lean-ctx");
129
130 if codex_hooks {
131 install_codex_hook();
132 }
133}
134
135fn resolve_binary_path() -> String {
136 if is_lean_ctx_in_path() {
137 return "lean-ctx".to_string();
138 }
139 crate::core::portable_binary::resolve_portable_binary()
140}
141
142fn is_lean_ctx_in_path() -> bool {
143 let which_cmd = if cfg!(windows) { "where" } else { "which" };
144 std::process::Command::new(which_cmd)
145 .arg("lean-ctx")
146 .stdout(std::process::Stdio::null())
147 .stderr(std::process::Stdio::null())
148 .status()
149 .is_ok_and(|s| s.success())
150}
151
152fn resolve_binary_path_for_bash() -> String {
153 let path = resolve_binary_path();
154 to_bash_compatible_path(&path)
155}
156
157pub fn to_bash_compatible_path(path: &str) -> String {
158 let path = match crate::core::pathutil::strip_verbatim_str(path) {
159 Some(stripped) => stripped,
160 None => path.replace('\\', "/"),
161 };
162 if path.len() >= 2 && path.as_bytes()[1] == b':' {
163 let drive = (path.as_bytes()[0] as char).to_ascii_lowercase();
164 format!("/{drive}{}", &path[2..])
165 } else {
166 path
167 }
168}
169
170pub fn from_bash_to_native_path(path: &str) -> String {
173 crate::core::pathutil::normalize_tool_path(path)
174}
175
176pub fn normalize_tool_path(path: &str) -> String {
179 crate::core::pathutil::normalize_tool_path(path)
180}
181
182pub fn generate_rewrite_script(binary: &str) -> String {
183 let case_pattern = crate::rewrite_registry::bash_case_pattern();
184 format!(
185 r#"#!/usr/bin/env bash
186# lean-ctx PreToolUse hook — rewrites bash commands to lean-ctx equivalents
187set -euo pipefail
188
189LEAN_CTX_BIN="{binary}"
190
191INPUT=$(cat)
192TOOL=$(echo "$INPUT" | grep -oE '"tool_name":"([^"\\]|\\.)*"' | head -1 | sed 's/^"tool_name":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
193
194case "$TOOL" in
195 Bash|bash|PowerShell|powershell) ;;
196 *) exit 0 ;;
197esac
198
199CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g')
200
201if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then
202 exit 0
203fi
204
205case "$CMD" in
206 {case_pattern})
207 # Shell-escape then JSON-escape (two passes)
208 SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
209 REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
210 JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
211 printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD"
212 ;;
213 *) exit 0 ;;
214esac
215"#
216 )
217}
218
219pub fn generate_compact_rewrite_script(binary: &str) -> String {
220 let case_pattern = crate::rewrite_registry::bash_case_pattern();
221 format!(
222 r#"#!/usr/bin/env bash
223# lean-ctx hook — rewrites shell commands
224set -euo pipefail
225LEAN_CTX_BIN="{binary}"
226INPUT=$(cat)
227CMD=$(echo "$INPUT" | grep -oE '"command":"([^"\\]|\\.)*"' | head -1 | sed 's/^"command":"//;s/"$//' | sed 's/\\"/"/g;s/\\\\/\\/g' 2>/dev/null || echo "")
228if [ -z "$CMD" ] || echo "$CMD" | grep -qE "^(lean-ctx |$LEAN_CTX_BIN )"; then exit 0; fi
229case "$CMD" in
230 {case_pattern})
231 SHELL_ESC=$(printf '%s' "$CMD" | sed 's/\\/\\\\/g;s/"/\\"/g')
232 REWRITE="$LEAN_CTX_BIN -c \"$SHELL_ESC\""
233 JSON_CMD=$(printf '%s' "$REWRITE" | sed 's/\\/\\\\/g;s/"/\\"/g')
234 printf '{{"hookSpecificOutput":{{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{{"command":"%s"}}}}}}' "$JSON_CMD" ;;
235 *) exit 0 ;;
236esac
237"#
238 )
239}
240
241const REDIRECT_SCRIPT_CLAUDE: &str = r"#!/usr/bin/env bash
242# lean-ctx PreToolUse hook — all native tools pass through
243# Read/Grep/ListFiles are allowed so Edit (which requires native Read) works.
244# The MCP instructions guide the AI to prefer ctx_read/ctx_search/ctx_tree.
245exit 0
246";
247
248const REDIRECT_SCRIPT_GENERIC: &str = r"#!/usr/bin/env bash
249# lean-ctx hook — all native tools pass through
250exit 0
251";
252
253pub(crate) const HYBRID_RULES: &str = "\
254# lean-ctx — Hybrid Mode (MCP reads + CLI commands)
255
256Use MCP tools for reads (cache benefit), CLI commands for everything else (no schema overhead):
257
258## MCP tools (keep using):
259| Tool | Why MCP |
260|------|---------|
261| `ctx_read(path, mode)` | In-process cache, re-reads ~13 tokens |
262
263## CLI commands (via Shell/Bash):
264| USE (via Shell/Bash) | INSTEAD OF (MCP) | Why |
265|---------------------|-------------------|-----|
266| `lean-ctx -c \"<cmd>\"` | `ctx_shell` | No MCP schema overhead |
267| `lean-ctx grep <pattern> [path]` | `ctx_search` | No MCP schema overhead |
268| `lean-ctx ls [path]` | `ctx_tree` | No MCP schema overhead |
269
270## File editing:
271Use native Edit/StrReplace — lean-ctx only handles READ operations.
272Write, Delete, Glob → use normally.
273";
274
275pub fn install_project_rules() {
276 install_project_rules_for_agents(&[]);
277}
278
279pub fn install_project_rules_for_agents(agents: &[&str]) {
282 if crate::core::config::Config::load().rules_scope_effective()
283 == crate::core::config::RulesScope::Global
284 {
285 return;
286 }
287
288 let cwd = std::env::current_dir().unwrap_or_default();
289
290 if !is_inside_git_repo(&cwd) {
291 eprintln!(
292 " Skipping project files: not inside a git repository.\n \
293 Run this command from your project root to create CLAUDE.md / AGENTS.md."
294 );
295 return;
296 }
297
298 let home = crate::core::home::resolve_home_dir().unwrap_or_default();
299 if cwd == home {
300 eprintln!(
301 " Skipping project files: current directory is your home folder.\n \
302 Run this command from a project directory instead."
303 );
304 return;
305 }
306
307 let all = agents.is_empty();
308 let wants = |name: &str| all || agents.iter().any(|a| a.eq_ignore_ascii_case(name));
309
310 ensure_project_agents_integration(&cwd);
311
312 if wants("cursor") || wants("windsurf") {
313 let cursorrules = cwd.join(".cursorrules");
314 if !cursorrules.exists()
315 || !std::fs::read_to_string(&cursorrules)
316 .unwrap_or_default()
317 .contains("lean-ctx")
318 {
319 let content = CURSORRULES_TEMPLATE;
320 if cursorrules.exists() {
321 let mut existing = std::fs::read_to_string(&cursorrules).unwrap_or_default();
322 if !existing.ends_with('\n') {
323 existing.push('\n');
324 }
325 existing.push('\n');
326 existing.push_str(content);
327 write_file(&cursorrules, &existing);
328 } else {
329 write_file(&cursorrules, content);
330 }
331 if !mcp_server_quiet_mode() {
332 eprintln!("Created/updated .cursorrules in project root.");
333 }
334 }
335 }
336
337 if wants("claude") {
338 let claude_rules_dir = cwd.join(".claude").join("rules");
339 let claude_rules_file = claude_rules_dir.join("lean-ctx.md");
340 if !claude_rules_file.exists()
341 || !std::fs::read_to_string(&claude_rules_file)
342 .unwrap_or_default()
343 .contains(crate::rules_inject::RULES_VERSION_STR)
344 {
345 let _ = std::fs::create_dir_all(&claude_rules_dir);
346 write_file(
347 &claude_rules_file,
348 crate::rules_inject::rules_dedicated_markdown(),
349 );
350 if !mcp_server_quiet_mode() {
351 eprintln!("Created .claude/rules/lean-ctx.md (Claude Code project rules).");
352 }
353 }
354
355 install_claude_project_hooks(&cwd);
356 }
357
358 if wants("kiro") {
359 let kiro_dir = cwd.join(".kiro");
360 if kiro_dir.exists() {
361 let steering_dir = kiro_dir.join("steering");
362 let steering_file = steering_dir.join("lean-ctx.md");
363 if !steering_file.exists()
364 || !std::fs::read_to_string(&steering_file)
365 .unwrap_or_default()
366 .contains("lean-ctx")
367 {
368 let _ = std::fs::create_dir_all(&steering_dir);
369 write_file(&steering_file, KIRO_STEERING_TEMPLATE);
370 if !mcp_server_quiet_mode() {
371 eprintln!("Created .kiro/steering/lean-ctx.md (Kiro steering).");
372 }
373 }
374 }
375 }
376}
377
378const PROJECT_LEAN_CTX_MD_MARKER: &str = "<!-- lean-ctx-owned: PROJECT-LEAN-CTX.md v1 -->";
379const PROJECT_LEAN_CTX_MD: &str = "LEAN-CTX.md";
380const PROJECT_AGENTS_MD: &str = "AGENTS.md";
381const AGENTS_BLOCK_START: &str = "<!-- lean-ctx -->";
382const AGENTS_BLOCK_END: &str = "<!-- /lean-ctx -->";
383
384fn ensure_project_agents_integration(cwd: &std::path::Path) {
385 let lean_ctx_md = cwd.join(PROJECT_LEAN_CTX_MD);
386 let desired = format!(
387 "{PROJECT_LEAN_CTX_MD_MARKER}\n{}\n",
388 crate::rules_inject::rules_dedicated_markdown()
389 );
390
391 if !lean_ctx_md.exists() {
392 write_file(&lean_ctx_md, &desired);
393 } else if std::fs::read_to_string(&lean_ctx_md)
394 .unwrap_or_default()
395 .contains(PROJECT_LEAN_CTX_MD_MARKER)
396 {
397 let current = std::fs::read_to_string(&lean_ctx_md).unwrap_or_default();
398 if !current.contains(crate::rules_inject::RULES_VERSION_STR) {
399 write_file(&lean_ctx_md, &desired);
400 }
401 }
402
403 let block = format!(
404 "{AGENTS_BLOCK_START}\n\
405## lean-ctx\n\n\
406Prefer lean-ctx MCP tools over native equivalents for token savings.\n\
407Full rules: @{PROJECT_LEAN_CTX_MD}\n\
408{AGENTS_BLOCK_END}\n"
409 );
410
411 let agents_md = cwd.join(PROJECT_AGENTS_MD);
412 if !agents_md.exists() {
413 let content = format!("# Agent Instructions\n\n{block}");
414 write_file(&agents_md, &content);
415 if !mcp_server_quiet_mode() {
416 eprintln!("Created AGENTS.md in project root (lean-ctx reference only).");
417 }
418 return;
419 }
420
421 let existing = std::fs::read_to_string(&agents_md).unwrap_or_default();
422
423 if existing.contains("CLI-first Token Optimization for Pi")
424 && !existing.contains(AGENTS_BLOCK_START)
425 {
426 let content = format!("# Agent Instructions\n\n{block}");
427 write_file(&agents_md, &content);
428 return;
429 }
430
431 if existing.contains(AGENTS_BLOCK_START) {
432 let updated = crate::marked_block::replace_marked_block(
433 &existing,
434 AGENTS_BLOCK_START,
435 AGENTS_BLOCK_END,
436 &block,
437 );
438 if updated != existing {
439 write_file(&agents_md, &updated);
440 }
441 return;
442 }
443
444 if existing.contains("lean-ctx") && existing.contains(PROJECT_LEAN_CTX_MD) {
445 return;
446 }
447
448 let mut out = existing;
449 if !out.ends_with('\n') {
450 out.push('\n');
451 }
452 out.push('\n');
453 out.push_str(&block);
454 write_file(&agents_md, &out);
455 if !mcp_server_quiet_mode() {
456 eprintln!("Updated AGENTS.md (added lean-ctx reference block).");
457 }
458}
459
460const CURSORRULES_TEMPLATE: &str = "\
461# lean-ctx — Context Engineering Layer
462
463CRITICAL: ALWAYS use lean-ctx tools instead of native equivalents. This is NOT optional.
464
465| MUST USE | NEVER USE | Why |
466|----------|-----------|-----|
467| `ctx_read(path, mode)` | `Read` / `cat` / `head` / `tail` | Cached, 10 read modes, re-reads ~13 tokens |
468| `ctx_search(pattern, path)` | `Grep` / `rg` | Compact, token-efficient results |
469| `lean-ctx -c \"<cmd>\"` (via Shell) | `ctx_shell` / raw `Shell` | CLI compression, no MCP overhead |
470
471File editing: use native Edit/StrReplace. Write, Delete, Glob → use normally.
472NEVER loop on Edit failures — switch to ctx_edit immediately.
473REMINDER: You MUST use lean-ctx tools. NEVER use native Read, Grep, or Shell directly.
474";
475
476pub const KIRO_STEERING_TEMPLATE: &str = "\
477---
478inclusion: always
479---
480
481# lean-ctx — Context Engineering Layer
482
483The workspace has the `lean-ctx` MCP server installed. You MUST prefer lean-ctx tools over native equivalents for token efficiency and caching.
484
485## Mandatory Tool Preferences
486
487| Use this | Instead of | Why |
488|----------|-----------|-----|
489| `mcp_lean_ctx_ctx_read` | `readFile`, `readCode` | Cached reads, 10 compression modes, re-reads cost ~13 tokens |
490| `mcp_lean_ctx_ctx_multi_read` | `readMultipleFiles` | Batch cached reads in one call |
491| `mcp_lean_ctx_ctx_shell` | `executeBash` | Pattern compression for git/npm/test output |
492| `mcp_lean_ctx_ctx_search` | `grepSearch` | Compact, .gitignore-aware results |
493| `mcp_lean_ctx_ctx_tree` | `listDirectory` | Compact directory maps with file counts |
494
495## When to use native Kiro tools instead
496
497- `fsWrite` / `fsAppend` — always use native (lean-ctx doesn't write files)
498- `strReplace` — always use native (precise string replacement)
499- `semanticRename` / `smartRelocate` — always use native (IDE integration)
500- `getDiagnostics` — always use native (language server diagnostics)
501- `deleteFile` — always use native
502
503## Session management
504
505- At the start of a long task, call `mcp_lean_ctx_ctx_preload` with a task description to warm the cache
506- Use `mcp_lean_ctx_ctx_compress` periodically in long conversations to checkpoint context
507- Use `mcp_lean_ctx_ctx_knowledge` to persist important discoveries across sessions
508
509## Rules
510
511- NEVER loop on edit failures — switch to `mcp_lean_ctx_ctx_edit` immediately
512- For large files, use `mcp_lean_ctx_ctx_read` with `mode: \"signatures\"` or `mode: \"map\"` first
513- For re-reading a file you already read, just call `mcp_lean_ctx_ctx_read` again (cache hit = ~13 tokens)
514- When running tests or build commands, use `mcp_lean_ctx_ctx_shell` for compressed output
515";
516
517pub fn install_agent_hook(agent: &str, global: bool) {
518 install_agent_hook_with_mode(agent, global, HookMode::Mcp);
519}
520
521pub fn install_agent_hook_with_mode(agent: &str, global: bool, mode: HookMode) {
522 let home = crate::core::home::resolve_home_dir().unwrap_or_default();
523 match agent {
524 "claude" | "claude-code" => install_claude_hook_with_mode(global, mode),
525 "cursor" => install_cursor_hook_with_mode(global, mode),
526 "gemini" => install_gemini_hook(),
527 "antigravity" => install_antigravity_hook(),
528 "augment" => install_mcp_json_agent(
529 "Augment CLI",
530 "~/.augment/settings.json",
531 &crate::core::editor_registry::augment_cli_settings_path(&home),
532 ),
533 "codex" => install_codex_hook(),
534 "windsurf" => install_windsurf_rules(global),
535 "cline" | "roo" => install_cline_rules(global),
536 "copilot" | "vscode" => install_copilot_hook(global),
537 "pi" => install_pi_hook_with_mode(global, mode),
538 "qoder" => install_qoder_hook_with_mode(mode),
539 "qoderwork" => install_mcp_json_agent(
540 "QoderWork",
541 "~/.qoderwork/mcp.json",
542 &home.join(".qoderwork/mcp.json"),
543 ),
544 "qwen" => install_mcp_json_agent(
545 "Qwen Code",
546 "~/.qwen/settings.json",
547 &home.join(".qwen/settings.json"),
548 ),
549 "trae" => install_mcp_json_agent("Trae", "~/.trae/mcp.json", &home.join(".trae/mcp.json")),
550 "amazonq" => install_mcp_json_agent(
551 "Amazon Q Developer",
552 "~/.aws/amazonq/default.json",
553 &home.join(".aws/amazonq/default.json"),
554 ),
555 "jetbrains" => install_jetbrains_hook(),
556 "kiro" => install_kiro_hook(),
557 "verdent" => install_mcp_json_agent(
558 "Verdent",
559 "~/.verdent/mcp.json",
560 &home.join(".verdent/mcp.json"),
561 ),
562 "opencode" => install_opencode_hook_with_mode(mode),
563 "amp" => install_amp_hook(),
564 "crush" => install_crush_hook_with_mode(mode),
565 "hermes" => install_hermes_hook_with_mode(global, mode),
566 "zed" => {
567 let zed_path = crate::core::editor_registry::zed_settings_path(&home);
568 let binary = resolve_binary_path();
569 let entry = full_server_entry(&binary);
570 install_named_json_server("Zed", "settings.json", &zed_path, "context_servers", entry);
571 }
572 "aider" => {
573 install_mcp_json_agent("Aider", "~/.aider/mcp.json", &home.join(".aider/mcp.json"));
574 }
575 "continue" => install_mcp_json_agent(
576 "Continue",
577 "~/.continue/mcp.json",
578 &home.join(".continue/mcp.json"),
579 ),
580 "neovim" => install_mcp_json_agent(
581 "Neovim (mcphub.nvim)",
582 "~/.config/mcphub/servers.json",
583 &home.join(".config/mcphub/servers.json"),
584 ),
585 "emacs" => install_mcp_json_agent(
586 "Emacs (mcp.el)",
587 "~/.emacs.d/mcp.json",
588 &home.join(".emacs.d/mcp.json"),
589 ),
590 "sublime" => install_mcp_json_agent(
591 "Sublime Text",
592 "~/.config/sublime-text/mcp.json",
593 &home.join(".config/sublime-text/mcp.json"),
594 ),
595 _ => {
596 eprintln!("Unknown agent: {agent}");
597 eprintln!(" Supported: aider, amazonq, amp, antigravity, augment, claude, cline,");
598 eprintln!(" codex, continue, copilot, crush, cursor, emacs, gemini, hermes,");
599 eprintln!(" jetbrains, kiro, neovim, opencode, pi, qoder, qoderwork, qwen, roo,");
600 eprintln!(" sublime, trae, verdent, vscode, windsurf, zed");
601 std::process::exit(1);
602 }
603 }
604}
605
606pub fn install_agent_project_hooks(agent: &str, cwd: &std::path::Path) {
607 match agent {
608 "claude" | "claude-code" => agents::install_claude_project_hooks(cwd),
609 _ => {}
610 }
611}
612
613fn write_file(path: &std::path::Path, content: &str) {
614 if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
615 tracing::error!("Error writing {}: {e}", path.display());
616 }
617}
618
619fn is_inside_git_repo(path: &std::path::Path) -> bool {
620 let mut p = path;
621 loop {
622 if p.join(".git").exists() {
623 return true;
624 }
625 match p.parent() {
626 Some(parent) => p = parent,
627 None => return false,
628 }
629 }
630}
631
632#[cfg(unix)]
633fn make_executable(path: &PathBuf) {
634 use std::os::unix::fs::PermissionsExt;
635 let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
636}
637
638#[cfg(not(unix))]
639fn make_executable(_path: &PathBuf) {}
640
641fn full_server_entry(binary: &str) -> serde_json::Value {
642 let data_dir = crate::core::data_dir::lean_ctx_data_dir()
643 .map(|d| d.to_string_lossy().to_string())
644 .unwrap_or_default();
645 serde_json::json!({
646 "command": binary,
647 "env": {
648 "LEAN_CTX_DATA_DIR": data_dir,
649 "LEAN_CTX_FULL_TOOLS": "1"
650 }
651 })
652}
653
654pub(crate) fn install_mcp_json_agent(
655 name: &str,
656 display_path: &str,
657 config_path: &std::path::Path,
658) {
659 let binary = resolve_binary_path();
660 let entry = full_server_entry(&binary);
661 install_named_json_server(name, display_path, config_path, "mcpServers", entry);
662}
663
664#[cfg(test)]
665mod tests {
666 use super::*;
667
668 #[test]
669 fn bash_path_unix_unchanged() {
670 assert_eq!(
671 to_bash_compatible_path("/usr/local/bin/lean-ctx"),
672 "/usr/local/bin/lean-ctx"
673 );
674 }
675
676 #[test]
677 fn bash_path_home_unchanged() {
678 assert_eq!(
679 to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
680 "/home/user/.cargo/bin/lean-ctx"
681 );
682 }
683
684 #[test]
685 fn bash_path_windows_drive_converted() {
686 assert_eq!(
687 to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
688 "/c/Users/Fraser/bin/lean-ctx.exe"
689 );
690 }
691
692 #[test]
693 fn bash_path_windows_lowercase_drive() {
694 assert_eq!(
695 to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
696 "/d/tools/lean-ctx.exe"
697 );
698 }
699
700 #[test]
701 fn bash_path_windows_forward_slashes() {
702 assert_eq!(
703 to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
704 "/c/Users/Fraser/bin/lean-ctx.exe"
705 );
706 }
707
708 #[test]
709 fn bash_path_bare_name_unchanged() {
710 assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
711 }
712
713 #[test]
714 fn normalize_msys2_path() {
715 assert_eq!(
716 normalize_tool_path("/c/Users/game/Downloads/project"),
717 "C:/Users/game/Downloads/project"
718 );
719 }
720
721 #[test]
722 fn normalize_msys2_drive_d() {
723 assert_eq!(
724 normalize_tool_path("/d/Projects/app/src"),
725 "D:/Projects/app/src"
726 );
727 }
728
729 #[test]
730 fn normalize_backslashes() {
731 assert_eq!(
732 normalize_tool_path("C:\\Users\\game\\project\\src"),
733 "C:/Users/game/project/src"
734 );
735 }
736
737 #[test]
738 fn normalize_mixed_separators() {
739 assert_eq!(
740 normalize_tool_path("C:\\Users/game\\project/src"),
741 "C:/Users/game/project/src"
742 );
743 }
744
745 #[test]
746 fn normalize_double_slashes() {
747 assert_eq!(
748 normalize_tool_path("/home/user//project///src"),
749 "/home/user/project/src"
750 );
751 }
752
753 #[test]
754 fn normalize_trailing_slash() {
755 assert_eq!(
756 normalize_tool_path("/home/user/project/"),
757 "/home/user/project"
758 );
759 }
760
761 #[test]
762 fn normalize_root_preserved() {
763 assert_eq!(normalize_tool_path("/"), "/");
764 }
765
766 #[test]
767 fn normalize_windows_root_preserved() {
768 assert_eq!(normalize_tool_path("C:/"), "C:/");
769 }
770
771 #[test]
772 fn normalize_unix_path_unchanged() {
773 assert_eq!(
774 normalize_tool_path("/home/user/project/src/main.rs"),
775 "/home/user/project/src/main.rs"
776 );
777 }
778
779 #[test]
780 fn normalize_relative_path_unchanged() {
781 assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
782 }
783
784 #[test]
785 fn normalize_dot_unchanged() {
786 assert_eq!(normalize_tool_path("."), ".");
787 }
788
789 #[test]
790 fn normalize_unc_path_preserved() {
791 assert_eq!(
792 normalize_tool_path("//server/share/file"),
793 "//server/share/file"
794 );
795 }
796
797 #[test]
798 fn cursor_hook_config_has_version_and_object_hooks() {
799 let config = serde_json::json!({
800 "version": 1,
801 "hooks": {
802 "preToolUse": [
803 {
804 "matcher": "terminal_command",
805 "command": "lean-ctx hook rewrite"
806 },
807 {
808 "matcher": "read_file|grep|search|list_files|list_directory",
809 "command": "lean-ctx hook redirect"
810 }
811 ]
812 }
813 });
814
815 let json_str = serde_json::to_string_pretty(&config).unwrap();
816 let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
817
818 assert_eq!(parsed["version"], 1);
819 assert!(parsed["hooks"].is_object());
820 assert!(parsed["hooks"]["preToolUse"].is_array());
821 assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
822 assert_eq!(
823 parsed["hooks"]["preToolUse"][0]["matcher"],
824 "terminal_command"
825 );
826 }
827
828 #[test]
829 fn cursor_hook_detects_old_format_needs_migration() {
830 let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
831 let has_correct =
832 old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
833 assert!(
834 !has_correct,
835 "Old format should be detected as needing migration"
836 );
837 }
838
839 #[test]
840 fn gemini_hook_config_has_type_command() {
841 let binary = "lean-ctx";
842 let rewrite_cmd = format!("{binary} hook rewrite");
843 let redirect_cmd = format!("{binary} hook redirect");
844
845 let hook_config = serde_json::json!({
846 "hooks": {
847 "BeforeTool": [
848 {
849 "hooks": [{
850 "type": "command",
851 "command": rewrite_cmd
852 }]
853 },
854 {
855 "hooks": [{
856 "type": "command",
857 "command": redirect_cmd
858 }]
859 }
860 ]
861 }
862 });
863
864 let parsed = hook_config;
865 let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
866 assert_eq!(before_tool.len(), 2);
867
868 let first_hook = &before_tool[0]["hooks"][0];
869 assert_eq!(first_hook["type"], "command");
870 assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
871
872 let second_hook = &before_tool[1]["hooks"][0];
873 assert_eq!(second_hook["type"], "command");
874 assert_eq!(second_hook["command"], "lean-ctx hook redirect");
875 }
876
877 #[test]
878 fn gemini_hook_old_format_detected() {
879 let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
880 let has_new = old_format.contains("hook rewrite")
881 && old_format.contains("hook redirect")
882 && old_format.contains("\"type\"");
883 assert!(!has_new, "Missing 'type' field should trigger migration");
884 }
885
886 #[test]
887 fn rewrite_script_uses_registry_pattern() {
888 let script = generate_rewrite_script("/usr/bin/lean-ctx");
889 assert!(script.contains(r"git\ *"), "script missing git pattern");
890 assert!(script.contains(r"cargo\ *"), "script missing cargo pattern");
891 assert!(script.contains(r"npm\ *"), "script missing npm pattern");
892 assert!(script.contains(r"rg\ *"), "script missing rg pattern");
893 assert!(script.contains(r"ls\ *"), "script missing ls pattern");
894 assert!(
895 script.contains("LEAN_CTX_BIN=\"/usr/bin/lean-ctx\""),
896 "script missing binary path"
897 );
898 assert!(
899 script.contains("PowerShell|powershell"),
900 "rewrite script must accept PowerShell tool names for Windows compatibility"
901 );
902 }
903
904 #[test]
905 fn compact_rewrite_script_uses_registry_pattern() {
906 let script = generate_compact_rewrite_script("/usr/bin/lean-ctx");
907 assert!(script.contains(r"git\ *"), "compact script missing git");
908 assert!(script.contains(r"cargo\ *"), "compact script missing cargo");
909 assert!(script.contains(r"rg\ *"), "compact script missing rg");
910 }
911
912 #[test]
913 fn rewrite_scripts_contain_all_registry_commands() {
914 let script = generate_rewrite_script("lean-ctx");
915 let compact = generate_compact_rewrite_script("lean-ctx");
916 for entry in crate::rewrite_registry::REWRITE_COMMANDS {
917 if matches!(entry.category, crate::rewrite_registry::Category::FileRead) {
918 continue;
919 }
920 let pattern = if entry.command.contains('-') {
921 format!("{}*", entry.command.replace('-', r"\-"))
922 } else {
923 format!(r"{}\ *", entry.command)
924 };
925 assert!(
926 script.contains(&pattern),
927 "rewrite_script missing '{}' (pattern: {})",
928 entry.command,
929 pattern
930 );
931 assert!(
932 compact.contains(&pattern),
933 "compact_rewrite_script missing '{}' (pattern: {})",
934 entry.command,
935 pattern
936 );
937 }
938 }
939
940 #[test]
941 fn codex_is_hybrid() {
942 assert_eq!(recommend_hook_mode("codex"), HookMode::Hybrid);
943 }
944
945 #[test]
946 fn cursor_is_hybrid() {
947 assert_eq!(recommend_hook_mode("cursor"), HookMode::Hybrid);
948 }
949
950 #[test]
951 fn gemini_is_hybrid() {
952 assert_eq!(recommend_hook_mode("gemini"), HookMode::Hybrid);
953 }
954
955 #[test]
956 fn claude_is_hybrid() {
957 assert_eq!(recommend_hook_mode("claude"), HookMode::Hybrid);
958 }
959
960 #[test]
961 fn unknown_agent_falls_back_to_mcp() {
962 assert_eq!(recommend_hook_mode("unknown-agent"), HookMode::Mcp);
963 }
964
965 #[test]
966 fn from_bash_to_native_converts_msys_drive() {
967 assert_eq!(
968 from_bash_to_native_path("/c/Users/ABC/lean-ctx"),
969 "C:/Users/ABC/lean-ctx"
970 );
971 }
972
973 #[test]
974 fn from_bash_to_native_drive_d() {
975 assert_eq!(
976 from_bash_to_native_path("/d/Program Files/lean-ctx.exe"),
977 "D:/Program Files/lean-ctx.exe"
978 );
979 }
980
981 #[test]
982 fn from_bash_to_native_unix_path_unchanged() {
983 assert_eq!(
984 from_bash_to_native_path("/usr/local/bin/lean-ctx"),
985 "/usr/local/bin/lean-ctx"
986 );
987 }
988
989 #[test]
990 fn from_bash_to_native_bare_name() {
991 assert_eq!(from_bash_to_native_path("lean-ctx"), "lean-ctx");
992 }
993
994 #[test]
995 fn roundtrip_windows_path() {
996 let native = r"C:\Users\ABC\AppData\Local\lean-ctx\lean-ctx.exe";
997 let bash = to_bash_compatible_path(native);
998 assert_eq!(bash, "/c/Users/ABC/AppData/Local/lean-ctx/lean-ctx.exe");
999 let back = from_bash_to_native_path(&bash);
1000 assert_eq!(back, "C:/Users/ABC/AppData/Local/lean-ctx/lean-ctx.exe");
1001 }
1002
1003 #[test]
1004 fn roundtrip_unix_path() {
1005 let native = "/usr/local/bin/lean-ctx";
1006 let bash = to_bash_compatible_path(native);
1007 assert_eq!(bash, native);
1008 let back = from_bash_to_native_path(&bash);
1009 assert_eq!(back, native);
1010 }
1011}