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 "codex" => install_codex_hook(),
529 "windsurf" => install_windsurf_rules(global),
530 "cline" | "roo" => install_cline_rules(global),
531 "copilot" | "vscode" => install_copilot_hook(global),
532 "pi" => install_pi_hook_with_mode(global, mode),
533 "qoder" => install_qoder_hook_with_mode(mode),
534 "qoderwork" => install_mcp_json_agent(
535 "QoderWork",
536 "~/.qoderwork/mcp.json",
537 &home.join(".qoderwork/mcp.json"),
538 ),
539 "qwen" => install_mcp_json_agent(
540 "Qwen Code",
541 "~/.qwen/settings.json",
542 &home.join(".qwen/settings.json"),
543 ),
544 "trae" => install_mcp_json_agent("Trae", "~/.trae/mcp.json", &home.join(".trae/mcp.json")),
545 "amazonq" => install_mcp_json_agent(
546 "Amazon Q Developer",
547 "~/.aws/amazonq/default.json",
548 &home.join(".aws/amazonq/default.json"),
549 ),
550 "jetbrains" => install_jetbrains_hook(),
551 "kiro" => install_kiro_hook(),
552 "verdent" => install_mcp_json_agent(
553 "Verdent",
554 "~/.verdent/mcp.json",
555 &home.join(".verdent/mcp.json"),
556 ),
557 "opencode" => install_opencode_hook_with_mode(mode),
558 "amp" => install_amp_hook(),
559 "crush" => install_crush_hook_with_mode(mode),
560 "hermes" => install_hermes_hook_with_mode(global, mode),
561 "zed" => {
562 let zed_path = crate::core::editor_registry::zed_settings_path(&home);
563 let binary = resolve_binary_path();
564 let entry = full_server_entry(&binary);
565 install_named_json_server("Zed", "settings.json", &zed_path, "context_servers", entry);
566 }
567 "aider" => {
568 install_mcp_json_agent("Aider", "~/.aider/mcp.json", &home.join(".aider/mcp.json"));
569 }
570 "continue" => install_mcp_json_agent(
571 "Continue",
572 "~/.continue/mcp.json",
573 &home.join(".continue/mcp.json"),
574 ),
575 "neovim" => install_mcp_json_agent(
576 "Neovim (mcphub.nvim)",
577 "~/.config/mcphub/servers.json",
578 &home.join(".config/mcphub/servers.json"),
579 ),
580 "emacs" => install_mcp_json_agent(
581 "Emacs (mcp.el)",
582 "~/.emacs.d/mcp.json",
583 &home.join(".emacs.d/mcp.json"),
584 ),
585 "sublime" => install_mcp_json_agent(
586 "Sublime Text",
587 "~/.config/sublime-text/mcp.json",
588 &home.join(".config/sublime-text/mcp.json"),
589 ),
590 _ => {
591 eprintln!("Unknown agent: {agent}");
592 eprintln!(" Supported: aider, amazonq, amp, antigravity, claude, cline, codex,");
593 eprintln!(" continue, copilot, crush, cursor, emacs, gemini, hermes, jetbrains,");
594 eprintln!(" kiro, neovim, opencode, pi, qoder, qoderwork, qwen, roo, sublime,");
595 eprintln!(" trae, verdent, vscode, windsurf, zed");
596 std::process::exit(1);
597 }
598 }
599}
600
601pub fn install_agent_project_hooks(agent: &str, cwd: &std::path::Path) {
602 match agent {
603 "claude" | "claude-code" => agents::install_claude_project_hooks(cwd),
604 _ => {}
605 }
606}
607
608fn write_file(path: &std::path::Path, content: &str) {
609 if let Err(e) = crate::config_io::write_atomic_with_backup(path, content) {
610 tracing::error!("Error writing {}: {e}", path.display());
611 }
612}
613
614fn is_inside_git_repo(path: &std::path::Path) -> bool {
615 let mut p = path;
616 loop {
617 if p.join(".git").exists() {
618 return true;
619 }
620 match p.parent() {
621 Some(parent) => p = parent,
622 None => return false,
623 }
624 }
625}
626
627#[cfg(unix)]
628fn make_executable(path: &PathBuf) {
629 use std::os::unix::fs::PermissionsExt;
630 let _ = std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755));
631}
632
633#[cfg(not(unix))]
634fn make_executable(_path: &PathBuf) {}
635
636fn full_server_entry(binary: &str) -> serde_json::Value {
637 let data_dir = crate::core::data_dir::lean_ctx_data_dir()
638 .map(|d| d.to_string_lossy().to_string())
639 .unwrap_or_default();
640 serde_json::json!({
641 "command": binary,
642 "env": {
643 "LEAN_CTX_DATA_DIR": data_dir,
644 "LEAN_CTX_FULL_TOOLS": "1"
645 }
646 })
647}
648
649pub(crate) fn install_mcp_json_agent(
650 name: &str,
651 display_path: &str,
652 config_path: &std::path::Path,
653) {
654 let binary = resolve_binary_path();
655 let entry = full_server_entry(&binary);
656 install_named_json_server(name, display_path, config_path, "mcpServers", entry);
657}
658
659#[cfg(test)]
660mod tests {
661 use super::*;
662
663 #[test]
664 fn bash_path_unix_unchanged() {
665 assert_eq!(
666 to_bash_compatible_path("/usr/local/bin/lean-ctx"),
667 "/usr/local/bin/lean-ctx"
668 );
669 }
670
671 #[test]
672 fn bash_path_home_unchanged() {
673 assert_eq!(
674 to_bash_compatible_path("/home/user/.cargo/bin/lean-ctx"),
675 "/home/user/.cargo/bin/lean-ctx"
676 );
677 }
678
679 #[test]
680 fn bash_path_windows_drive_converted() {
681 assert_eq!(
682 to_bash_compatible_path("C:\\Users\\Fraser\\bin\\lean-ctx.exe"),
683 "/c/Users/Fraser/bin/lean-ctx.exe"
684 );
685 }
686
687 #[test]
688 fn bash_path_windows_lowercase_drive() {
689 assert_eq!(
690 to_bash_compatible_path("D:\\tools\\lean-ctx.exe"),
691 "/d/tools/lean-ctx.exe"
692 );
693 }
694
695 #[test]
696 fn bash_path_windows_forward_slashes() {
697 assert_eq!(
698 to_bash_compatible_path("C:/Users/Fraser/bin/lean-ctx.exe"),
699 "/c/Users/Fraser/bin/lean-ctx.exe"
700 );
701 }
702
703 #[test]
704 fn bash_path_bare_name_unchanged() {
705 assert_eq!(to_bash_compatible_path("lean-ctx"), "lean-ctx");
706 }
707
708 #[test]
709 fn normalize_msys2_path() {
710 assert_eq!(
711 normalize_tool_path("/c/Users/game/Downloads/project"),
712 "C:/Users/game/Downloads/project"
713 );
714 }
715
716 #[test]
717 fn normalize_msys2_drive_d() {
718 assert_eq!(
719 normalize_tool_path("/d/Projects/app/src"),
720 "D:/Projects/app/src"
721 );
722 }
723
724 #[test]
725 fn normalize_backslashes() {
726 assert_eq!(
727 normalize_tool_path("C:\\Users\\game\\project\\src"),
728 "C:/Users/game/project/src"
729 );
730 }
731
732 #[test]
733 fn normalize_mixed_separators() {
734 assert_eq!(
735 normalize_tool_path("C:\\Users/game\\project/src"),
736 "C:/Users/game/project/src"
737 );
738 }
739
740 #[test]
741 fn normalize_double_slashes() {
742 assert_eq!(
743 normalize_tool_path("/home/user//project///src"),
744 "/home/user/project/src"
745 );
746 }
747
748 #[test]
749 fn normalize_trailing_slash() {
750 assert_eq!(
751 normalize_tool_path("/home/user/project/"),
752 "/home/user/project"
753 );
754 }
755
756 #[test]
757 fn normalize_root_preserved() {
758 assert_eq!(normalize_tool_path("/"), "/");
759 }
760
761 #[test]
762 fn normalize_windows_root_preserved() {
763 assert_eq!(normalize_tool_path("C:/"), "C:/");
764 }
765
766 #[test]
767 fn normalize_unix_path_unchanged() {
768 assert_eq!(
769 normalize_tool_path("/home/user/project/src/main.rs"),
770 "/home/user/project/src/main.rs"
771 );
772 }
773
774 #[test]
775 fn normalize_relative_path_unchanged() {
776 assert_eq!(normalize_tool_path("src/main.rs"), "src/main.rs");
777 }
778
779 #[test]
780 fn normalize_dot_unchanged() {
781 assert_eq!(normalize_tool_path("."), ".");
782 }
783
784 #[test]
785 fn normalize_unc_path_preserved() {
786 assert_eq!(
787 normalize_tool_path("//server/share/file"),
788 "//server/share/file"
789 );
790 }
791
792 #[test]
793 fn cursor_hook_config_has_version_and_object_hooks() {
794 let config = serde_json::json!({
795 "version": 1,
796 "hooks": {
797 "preToolUse": [
798 {
799 "matcher": "terminal_command",
800 "command": "lean-ctx hook rewrite"
801 },
802 {
803 "matcher": "read_file|grep|search|list_files|list_directory",
804 "command": "lean-ctx hook redirect"
805 }
806 ]
807 }
808 });
809
810 let json_str = serde_json::to_string_pretty(&config).unwrap();
811 let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
812
813 assert_eq!(parsed["version"], 1);
814 assert!(parsed["hooks"].is_object());
815 assert!(parsed["hooks"]["preToolUse"].is_array());
816 assert_eq!(parsed["hooks"]["preToolUse"].as_array().unwrap().len(), 2);
817 assert_eq!(
818 parsed["hooks"]["preToolUse"][0]["matcher"],
819 "terminal_command"
820 );
821 }
822
823 #[test]
824 fn cursor_hook_detects_old_format_needs_migration() {
825 let old_format = r#"{"hooks":[{"event":"preToolUse","command":"lean-ctx hook rewrite"}]}"#;
826 let has_correct =
827 old_format.contains("\"version\"") && old_format.contains("\"preToolUse\"");
828 assert!(
829 !has_correct,
830 "Old format should be detected as needing migration"
831 );
832 }
833
834 #[test]
835 fn gemini_hook_config_has_type_command() {
836 let binary = "lean-ctx";
837 let rewrite_cmd = format!("{binary} hook rewrite");
838 let redirect_cmd = format!("{binary} hook redirect");
839
840 let hook_config = serde_json::json!({
841 "hooks": {
842 "BeforeTool": [
843 {
844 "hooks": [{
845 "type": "command",
846 "command": rewrite_cmd
847 }]
848 },
849 {
850 "hooks": [{
851 "type": "command",
852 "command": redirect_cmd
853 }]
854 }
855 ]
856 }
857 });
858
859 let parsed = hook_config;
860 let before_tool = parsed["hooks"]["BeforeTool"].as_array().unwrap();
861 assert_eq!(before_tool.len(), 2);
862
863 let first_hook = &before_tool[0]["hooks"][0];
864 assert_eq!(first_hook["type"], "command");
865 assert_eq!(first_hook["command"], "lean-ctx hook rewrite");
866
867 let second_hook = &before_tool[1]["hooks"][0];
868 assert_eq!(second_hook["type"], "command");
869 assert_eq!(second_hook["command"], "lean-ctx hook redirect");
870 }
871
872 #[test]
873 fn gemini_hook_old_format_detected() {
874 let old_format = r#"{"hooks":{"BeforeTool":[{"command":"lean-ctx hook rewrite"}]}}"#;
875 let has_new = old_format.contains("hook rewrite")
876 && old_format.contains("hook redirect")
877 && old_format.contains("\"type\"");
878 assert!(!has_new, "Missing 'type' field should trigger migration");
879 }
880
881 #[test]
882 fn rewrite_script_uses_registry_pattern() {
883 let script = generate_rewrite_script("/usr/bin/lean-ctx");
884 assert!(script.contains(r"git\ *"), "script missing git pattern");
885 assert!(script.contains(r"cargo\ *"), "script missing cargo pattern");
886 assert!(script.contains(r"npm\ *"), "script missing npm pattern");
887 assert!(
888 !script.contains(r"rg\ *"),
889 "script should not contain rg pattern"
890 );
891 assert!(
892 script.contains("LEAN_CTX_BIN=\"/usr/bin/lean-ctx\""),
893 "script missing binary path"
894 );
895 assert!(
896 script.contains("PowerShell|powershell"),
897 "rewrite script must accept PowerShell tool names for Windows compatibility"
898 );
899 }
900
901 #[test]
902 fn compact_rewrite_script_uses_registry_pattern() {
903 let script = generate_compact_rewrite_script("/usr/bin/lean-ctx");
904 assert!(script.contains(r"git\ *"), "compact script missing git");
905 assert!(script.contains(r"cargo\ *"), "compact script missing cargo");
906 assert!(
907 !script.contains(r"rg\ *"),
908 "compact script should not contain rg"
909 );
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!(
918 entry.category,
919 crate::rewrite_registry::Category::Search
920 | crate::rewrite_registry::Category::FileRead
921 | crate::rewrite_registry::Category::DirList
922 ) {
923 continue;
924 }
925 let pattern = if entry.command.contains('-') {
926 format!("{}*", entry.command.replace('-', r"\-"))
927 } else {
928 format!(r"{}\ *", entry.command)
929 };
930 assert!(
931 script.contains(&pattern),
932 "rewrite_script missing '{}' (pattern: {})",
933 entry.command,
934 pattern
935 );
936 assert!(
937 compact.contains(&pattern),
938 "compact_rewrite_script missing '{}' (pattern: {})",
939 entry.command,
940 pattern
941 );
942 }
943 }
944
945 #[test]
946 fn codex_is_hybrid() {
947 assert_eq!(recommend_hook_mode("codex"), HookMode::Hybrid);
948 }
949
950 #[test]
951 fn cursor_is_hybrid() {
952 assert_eq!(recommend_hook_mode("cursor"), HookMode::Hybrid);
953 }
954
955 #[test]
956 fn gemini_is_hybrid() {
957 assert_eq!(recommend_hook_mode("gemini"), HookMode::Hybrid);
958 }
959
960 #[test]
961 fn claude_is_hybrid() {
962 assert_eq!(recommend_hook_mode("claude"), HookMode::Hybrid);
963 }
964
965 #[test]
966 fn unknown_agent_falls_back_to_mcp() {
967 assert_eq!(recommend_hook_mode("unknown-agent"), HookMode::Mcp);
968 }
969
970 #[test]
971 fn from_bash_to_native_converts_msys_drive() {
972 assert_eq!(
973 from_bash_to_native_path("/c/Users/ABC/lean-ctx"),
974 "C:/Users/ABC/lean-ctx"
975 );
976 }
977
978 #[test]
979 fn from_bash_to_native_drive_d() {
980 assert_eq!(
981 from_bash_to_native_path("/d/Program Files/lean-ctx.exe"),
982 "D:/Program Files/lean-ctx.exe"
983 );
984 }
985
986 #[test]
987 fn from_bash_to_native_unix_path_unchanged() {
988 assert_eq!(
989 from_bash_to_native_path("/usr/local/bin/lean-ctx"),
990 "/usr/local/bin/lean-ctx"
991 );
992 }
993
994 #[test]
995 fn from_bash_to_native_bare_name() {
996 assert_eq!(from_bash_to_native_path("lean-ctx"), "lean-ctx");
997 }
998
999 #[test]
1000 fn roundtrip_windows_path() {
1001 let native = r"C:\Users\ABC\AppData\Local\lean-ctx\lean-ctx.exe";
1002 let bash = to_bash_compatible_path(native);
1003 assert_eq!(bash, "/c/Users/ABC/AppData/Local/lean-ctx/lean-ctx.exe");
1004 let back = from_bash_to_native_path(&bash);
1005 assert_eq!(back, "C:/Users/ABC/AppData/Local/lean-ctx/lean-ctx.exe");
1006 }
1007
1008 #[test]
1009 fn roundtrip_unix_path() {
1010 let native = "/usr/local/bin/lean-ctx";
1011 let bash = to_bash_compatible_path(native);
1012 assert_eq!(bash, native);
1013 let back = from_bash_to_native_path(&bash);
1014 assert_eq!(back, native);
1015 }
1016}