Skip to main content

lean_ctx/
hook_handlers.rs

1use crate::compound_lexer;
2use crate::rewrite_registry;
3use std::io::Read;
4use std::sync::mpsc;
5use std::time::Duration;
6
7const HOOK_STDIN_TIMEOUT: Duration = Duration::from_secs(3);
8
9// ---------------------------------------------------------------------------
10// Observe handler — records ALL hook events for context awareness
11// ---------------------------------------------------------------------------
12
13/// Unified observe handler for all IDE hook events.
14/// Reads JSON from stdin, normalizes to `ObserveEvent`, counts tokens,
15/// appends to `context_radar.jsonl`, and exits immediately.
16pub fn handle_observe() {
17    if is_disabled() {
18        return;
19    }
20    let Some(input) = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT) else {
21        return;
22    };
23    let Some(event) = parse_observe_event(&input) else {
24        return;
25    };
26    append_radar_event(&event);
27}
28
29#[derive(serde::Serialize)]
30struct ObserveEvent {
31    ts: u64,
32    event_type: &'static str,
33    tokens: usize,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    tool_name: Option<String>,
36    #[serde(skip_serializing_if = "Option::is_none")]
37    detail: Option<String>,
38    #[serde(skip_serializing_if = "Option::is_none")]
39    content: Option<String>,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    model: Option<String>,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    conversation_id: Option<String>,
44}
45
46const MAX_CONTENT_CHARS: usize = 50_000;
47
48fn parse_observe_event(input: &str) -> Option<ObserveEvent> {
49    let v: serde_json::Value = serde_json::from_str(input).ok()?;
50
51    let ts = std::time::SystemTime::now()
52        .duration_since(std::time::UNIX_EPOCH)
53        .unwrap_or_default()
54        .as_secs();
55
56    let model = v
57        .get("model")
58        .and_then(|m| m.as_str())
59        .filter(|m| !m.is_empty())
60        .map(String::from);
61    let conversation_id = v
62        .get("conversation_id")
63        .and_then(|c| c.as_str())
64        .filter(|c| !c.is_empty())
65        .map(String::from);
66
67    let transcript_path = v
68        .get("transcript_path")
69        .and_then(|t| t.as_str())
70        .filter(|t| !t.is_empty())
71        .map(String::from);
72
73    if let Some(ref m) = model {
74        persist_detected_model(m);
75    }
76    if let Some(ref tp) = transcript_path {
77        persist_transcript_path(tp, conversation_id.as_deref());
78    }
79
80    let mut event = detect_event_type(&v, ts)?;
81    event.model = model;
82    event.conversation_id = conversation_id;
83    Some(event)
84}
85
86fn detect_event_type(v: &serde_json::Value, ts: u64) -> Option<ObserveEvent> {
87    if let Some(result) = v
88        .get("result_json")
89        .or_else(|| v.get("result"))
90        .or_else(|| v.get("tool_response"))
91        .or_else(|| v.get("tool_output"))
92    {
93        let tool = v
94            .get("tool_name")
95            .and_then(|t| t.as_str())
96            .unwrap_or("unknown");
97        let tokens = estimate_tokens_json(result);
98        let content_str = match result {
99            serde_json::Value::String(s) => s.clone(),
100            other => other.to_string(),
101        };
102        return Some(ObserveEvent {
103            ts,
104            event_type: "mcp_call",
105            tokens,
106            tool_name: Some(tool.to_string()),
107            detail: v
108                .get("server_name")
109                .and_then(|s| s.as_str())
110                .map(String::from),
111            content: Some(cap_content(&content_str)),
112            model: None,
113            conversation_id: None,
114        });
115    }
116
117    if let Some(output) = v.get("output") {
118        let cmd = v
119            .get("command")
120            .and_then(|c| c.as_str())
121            .unwrap_or("")
122            .to_string();
123        let tokens = estimate_tokens_value(output);
124        let out_str = match output {
125            serde_json::Value::String(s) => s.clone(),
126            other => other.to_string(),
127        };
128        return Some(ObserveEvent {
129            ts,
130            event_type: "shell",
131            tokens,
132            tool_name: None,
133            detail: Some(truncate_str(&cmd, 80)),
134            content: Some(cap_content(&format!("$ {cmd}\n{out_str}"))),
135            model: None,
136            conversation_id: None,
137        });
138    }
139
140    if v.get("content").is_some() && v.get("file_path").is_some() {
141        let path = v
142            .get("file_path")
143            .and_then(|p| p.as_str())
144            .unwrap_or("")
145            .to_string();
146        let file_content = v.get("content").and_then(|c| c.as_str()).unwrap_or("");
147        let tokens = file_content.len() / 4;
148        return Some(ObserveEvent {
149            ts,
150            event_type: "file_read",
151            tokens,
152            tool_name: None,
153            detail: Some(truncate_str(&path, 120)),
154            content: Some(cap_content(file_content)),
155            model: None,
156            conversation_id: None,
157        });
158    }
159
160    if let Some(text) = v.get("text").and_then(|t| t.as_str()) {
161        let has_duration = v.get("duration_ms").is_some();
162        let event_type = if has_duration {
163            "thinking"
164        } else {
165            "agent_response"
166        };
167        let tokens = text.len() / 4;
168        return Some(ObserveEvent {
169            ts,
170            event_type,
171            tokens,
172            tool_name: None,
173            detail: None,
174            content: Some(cap_content(text)),
175            model: None,
176            conversation_id: None,
177        });
178    }
179
180    if let Some(prompt) = v.get("prompt").and_then(|p| p.as_str()) {
181        let tokens = prompt.len() / 4;
182        let mut full = prompt.to_string();
183        if let Some(attachments) = v.get("attachments").and_then(|a| a.as_array()) {
184            if !attachments.is_empty() {
185                full.push_str(&format!("\n\n[{} attachments]", attachments.len()));
186                for att in attachments {
187                    if let Some(name) = att.get("name").and_then(|n| n.as_str()) {
188                        full.push_str(&format!("\n  - {name}"));
189                    }
190                }
191            }
192        }
193        return Some(ObserveEvent {
194            ts,
195            event_type: "user_message",
196            tokens,
197            tool_name: None,
198            detail: v
199                .get("attachments")
200                .and_then(|a| a.as_array())
201                .map(|a| format!("{} attachments", a.len())),
202            content: Some(cap_content(&full)),
203            model: None,
204            conversation_id: None,
205        });
206    }
207
208    if v.get("tool_name").is_some() || v.get("tool_input").is_some() {
209        let tool = v
210            .get("tool_name")
211            .and_then(|t| t.as_str())
212            .unwrap_or("unknown")
213            .to_string();
214        let is_lctx = tool.starts_with("ctx_") || tool.starts_with("mcp__lean-ctx__");
215        let tokens = v.get("tool_input").map_or(0, estimate_tokens_json);
216        let input_str = v
217            .get("tool_input")
218            .map(std::string::ToString::to_string)
219            .unwrap_or_default();
220        return Some(ObserveEvent {
221            ts,
222            event_type: if is_lctx { "mcp_call" } else { "native_tool" },
223            tokens,
224            tool_name: Some(tool),
225            detail: None,
226            content: if input_str.is_empty() {
227                None
228            } else {
229                Some(cap_content(&input_str))
230            },
231            model: None,
232            conversation_id: None,
233        });
234    }
235
236    if v.get("session_id").is_some() {
237        return Some(ObserveEvent {
238            ts,
239            event_type: "session",
240            tokens: 0,
241            tool_name: None,
242            detail: v
243                .get("session_id")
244                .and_then(|s| s.as_str())
245                .map(String::from),
246            content: None,
247            model: None,
248            conversation_id: None,
249        });
250    }
251
252    let is_compaction = v.get("compaction").is_some()
253        || v.get("messages_count").is_some()
254        || v.get("event")
255            .and_then(|e| e.as_str())
256            .is_some_and(|e| e == "compaction" || e == "compact");
257    if is_compaction {
258        return Some(ObserveEvent {
259            ts,
260            event_type: "compaction",
261            tokens: 0,
262            tool_name: None,
263            detail: None,
264            content: None,
265            model: None,
266            conversation_id: None,
267        });
268    }
269
270    None
271}
272
273fn estimate_tokens_json(v: &serde_json::Value) -> usize {
274    match v {
275        serde_json::Value::String(s) => s.len() / 4,
276        _ => v.to_string().len() / 4,
277    }
278}
279
280fn estimate_tokens_value(v: &serde_json::Value) -> usize {
281    match v {
282        serde_json::Value::String(s) => s.len() / 4,
283        _ => v.to_string().len() / 4,
284    }
285}
286
287fn persist_detected_model(model: &str) {
288    let m = model.to_lowercase();
289    let is_bg_model = m.contains("flash")
290        || m.contains("mini")
291        || m.contains("haiku")
292        || m.contains("fast")
293        || m.contains("nano")
294        || m.contains("small");
295    if is_bg_model {
296        return;
297    }
298
299    let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() else {
300        return;
301    };
302    let path = data_dir.join("detected_model.json");
303    let ts = std::time::SystemTime::now()
304        .duration_since(std::time::UNIX_EPOCH)
305        .unwrap_or_default()
306        .as_secs();
307    let window = model_context_window(model);
308    let payload = serde_json::json!({
309        "model": model,
310        "window_size": window,
311        "detected_at": ts,
312    });
313    if let Ok(json) = serde_json::to_string_pretty(&payload) {
314        let tmp = path.with_extension("tmp");
315        if std::fs::write(&tmp, &json).is_ok() {
316            let _ = std::fs::rename(&tmp, &path);
317        }
318    }
319}
320
321pub fn model_context_window(model: &str) -> usize {
322    crate::core::model_registry::context_window_for_model(model)
323}
324
325pub fn load_detected_model() -> Option<(String, usize)> {
326    let data_dir = crate::core::data_dir::lean_ctx_data_dir().ok()?;
327    let path = data_dir.join("detected_model.json");
328    let content = std::fs::read_to_string(&path).ok()?;
329    let v: serde_json::Value = serde_json::from_str(&content).ok()?;
330    let model = v.get("model")?.as_str()?.to_string();
331    let window = v.get("window_size")?.as_u64()? as usize;
332    let detected_at = v.get("detected_at")?.as_u64()?;
333    let now = std::time::SystemTime::now()
334        .duration_since(std::time::UNIX_EPOCH)
335        .unwrap_or_default()
336        .as_secs();
337    if now.saturating_sub(detected_at) > 7200 {
338        return None;
339    }
340    Some((model, window))
341}
342
343fn persist_transcript_path(path: &str, conversation_id: Option<&str>) {
344    let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() else {
345        return;
346    };
347    let meta_path = data_dir.join("active_transcript.json");
348    let ts = std::time::SystemTime::now()
349        .duration_since(std::time::UNIX_EPOCH)
350        .unwrap_or_default()
351        .as_secs();
352    let payload = serde_json::json!({
353        "transcript_path": path,
354        "conversation_id": conversation_id,
355        "updated_at": ts,
356    });
357    if let Ok(json) = serde_json::to_string_pretty(&payload) {
358        let tmp = meta_path.with_extension("tmp");
359        if std::fs::write(&tmp, &json).is_ok() {
360            let _ = std::fs::rename(&tmp, &meta_path);
361        }
362    }
363}
364
365pub fn load_active_transcript() -> Option<(String, Option<String>)> {
366    let data_dir = crate::core::data_dir::lean_ctx_data_dir().ok()?;
367    let path = data_dir.join("active_transcript.json");
368    let content = std::fs::read_to_string(&path).ok()?;
369    let v: serde_json::Value = serde_json::from_str(&content).ok()?;
370    let tp = v.get("transcript_path")?.as_str()?.to_string();
371    let conv = v
372        .get("conversation_id")
373        .and_then(|c| c.as_str())
374        .map(String::from);
375    let updated = v.get("updated_at")?.as_u64()?;
376    let now = std::time::SystemTime::now()
377        .duration_since(std::time::UNIX_EPOCH)
378        .unwrap_or_default()
379        .as_secs();
380    if now.saturating_sub(updated) > 7200 {
381        return None;
382    }
383    Some((tp, conv))
384}
385
386fn cap_content(s: &str) -> String {
387    if s.len() <= MAX_CONTENT_CHARS {
388        s.to_string()
389    } else {
390        let truncated = safe_truncate(s, MAX_CONTENT_CHARS);
391        format!("{}…\n\n[truncated: {} total chars]", truncated, s.len())
392    }
393}
394
395fn truncate_str(s: &str, max: usize) -> String {
396    if s.len() <= max {
397        s.to_string()
398    } else {
399        format!("{}...", safe_truncate(s, max))
400    }
401}
402
403/// Truncate a string at a char boundary <= max bytes. Never panics on multi-byte UTF-8.
404fn safe_truncate(s: &str, max: usize) -> &str {
405    if max >= s.len() {
406        return s;
407    }
408    let mut end = max;
409    while end > 0 && !s.is_char_boundary(end) {
410        end -= 1;
411    }
412    &s[..end]
413}
414
415fn append_radar_event(event: &ObserveEvent) {
416    let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() else {
417        return;
418    };
419    let radar_path = data_dir.join("context_radar.jsonl");
420
421    if event.event_type == "session" {
422        if let Ok(meta) = std::fs::metadata(&radar_path) {
423            const MAX_RADAR_SIZE: u64 = 10 * 1024 * 1024; // 10 MB
424            if meta.len() > MAX_RADAR_SIZE {
425                let prev = data_dir.join("context_radar.prev.jsonl");
426                let _ = std::fs::rename(&radar_path, &prev);
427            }
428        }
429    }
430
431    let Ok(line) = serde_json::to_string(event) else {
432        return;
433    };
434
435    use std::fs::OpenOptions;
436    use std::io::Write;
437    if let Ok(mut f) = OpenOptions::new()
438        .create(true)
439        .append(true)
440        .open(&radar_path)
441    {
442        let _ = writeln!(f, "{line}");
443    }
444}
445
446fn is_disabled() -> bool {
447    std::env::var("LEAN_CTX_DISABLED").is_ok()
448}
449
450fn is_harden_active() -> bool {
451    matches!(std::env::var("LEAN_CTX_HARDEN"), Ok(v) if v.trim() == "1")
452}
453
454fn is_quiet() -> bool {
455    matches!(std::env::var("LEAN_CTX_QUIET"), Ok(v) if v.trim() == "1")
456}
457
458/// Mark this process as a hook child so the daemon-client never auto-starts
459/// the daemon from inside a hook (which would create zombie processes).
460pub fn mark_hook_environment() {
461    std::env::set_var("LEAN_CTX_HOOK_CHILD", "1");
462}
463
464/// Arms a watchdog that force-exits the process after the given duration.
465/// Prevents hook processes from becoming zombies when stdin pipes break or
466/// the IDE cancels the call. Since hooks MUST NOT spawn child processes
467/// (to avoid orphan zombies), a simple exit(1) suffices.
468pub fn arm_watchdog(timeout: Duration) {
469    std::thread::spawn(move || {
470        std::thread::sleep(timeout);
471        eprintln!(
472            "[lean-ctx hook] watchdog timeout after {}s — force exit",
473            timeout.as_secs()
474        );
475        std::process::exit(1);
476    });
477}
478
479/// Reads all of stdin with a timeout. Returns None if stdin is empty, broken, or times out.
480fn read_stdin_with_timeout(timeout: Duration) -> Option<String> {
481    let (tx, rx) = mpsc::channel();
482    std::thread::spawn(move || {
483        let mut buf = String::new();
484        let result = std::io::stdin().read_to_string(&mut buf);
485        let _ = tx.send(result.ok().map(|_| buf));
486    });
487    match rx.recv_timeout(timeout) {
488        Ok(Some(s)) if !s.is_empty() => Some(s),
489        _ => None,
490    }
491}
492
493fn build_dual_allow_output() -> String {
494    serde_json::json!({
495        "permission": "allow",
496        "hookSpecificOutput": {
497            "hookEventName": "PreToolUse",
498            "permissionDecision": "allow"
499        }
500    })
501    .to_string()
502}
503
504fn build_dual_rewrite_output(tool_input: Option<&serde_json::Value>, rewritten: &str) -> String {
505    let updated_input = if let Some(obj) = tool_input.and_then(|v| v.as_object()) {
506        let mut m = obj.clone();
507        m.insert(
508            "command".to_string(),
509            serde_json::Value::String(rewritten.to_string()),
510        );
511        serde_json::Value::Object(m)
512    } else {
513        serde_json::json!({ "command": rewritten })
514    };
515
516    serde_json::json!({
517        // Cursor hook output format
518        "permission": "allow",
519        "updated_input": updated_input,
520        // Claude Code hook output format (extra fields are ignored by other hosts)
521        "hookSpecificOutput": {
522            "hookEventName": "PreToolUse",
523            "permissionDecision": "allow",
524            "updatedInput": {
525                "command": rewritten
526            }
527        }
528    })
529    .to_string()
530}
531
532pub fn handle_rewrite() {
533    let allow = build_dual_allow_output();
534    if is_disabled() {
535        print!("{allow}");
536        return;
537    }
538    let binary = resolve_binary();
539    let Some(input) = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT) else {
540        print!("{allow}");
541        return;
542    };
543
544    let Ok(v) = serde_json::from_str::<serde_json::Value>(&input) else {
545        tracing::warn!("[hook rewrite] invalid JSON payload, allowing passthrough");
546        print!("{allow}");
547        return;
548    };
549
550    let tool = v.get("tool_name").and_then(|t| t.as_str());
551    let Some(tool_name) = tool else {
552        print!("{allow}");
553        return;
554    };
555
556    let is_shell_tool = matches!(
557        tool_name,
558        "Bash" | "bash" | "Shell" | "shell" | "runInTerminal" | "run_in_terminal" | "terminal"
559    );
560    if !is_shell_tool {
561        print!("{allow}");
562        return;
563    }
564
565    let tool_input = v.get("tool_input");
566    let Some(cmd) = tool_input
567        .and_then(|ti| ti.get("command"))
568        .and_then(|c| c.as_str())
569        .or_else(|| v.get("command").and_then(|c| c.as_str()))
570    else {
571        print!("{allow}");
572        return;
573    };
574
575    if let Some(rewritten) = rewrite_candidate(cmd, &binary) {
576        print!("{}", build_dual_rewrite_output(tool_input, &rewritten));
577    } else {
578        print!("{allow}");
579    }
580}
581
582fn is_rewritable(cmd: &str) -> bool {
583    rewrite_registry::is_rewritable_command(cmd)
584}
585
586fn wrap_single_command(cmd: &str, binary: &str) -> String {
587    if cfg!(windows) {
588        let escaped = cmd.replace('"', "\\\"");
589        format!("{binary} -c \"{escaped}\"")
590    } else {
591        let shell_escaped = cmd.replace('\'', "'\\''");
592        format!("{binary} -c '{shell_escaped}'")
593    }
594}
595
596fn rewrite_candidate(cmd: &str, binary: &str) -> Option<String> {
597    if cmd.starts_with("lean-ctx ") || cmd.starts_with(&format!("{binary} ")) {
598        return None;
599    }
600
601    // Heredocs cannot survive the quoting round-trip through `lean-ctx -c '...'`.
602    // Newlines get escaped, breaking the heredoc syntax entirely (GitHub #140).
603    if cmd.contains("<<") {
604        return None;
605    }
606
607    if let Some(rewritten) = rewrite_file_read_command(cmd, binary) {
608        return Some(rewritten);
609    }
610
611    if let Some(rewritten) = rewrite_search_command(cmd, binary) {
612        return Some(rewritten);
613    }
614
615    if let Some(rewritten) = rewrite_dir_list_command(cmd, binary) {
616        return Some(rewritten);
617    }
618
619    if let Some(rewritten) = build_rewrite_compound(cmd, binary) {
620        return Some(rewritten);
621    }
622
623    if is_rewritable(cmd) {
624        return Some(wrap_single_command(cmd, binary));
625    }
626
627    None
628}
629
630/// Rewrites cat/head/tail to lean-ctx read with appropriate arguments.
631/// Only rewrites simple single-file reads within the project scope.
632fn rewrite_file_read_command(cmd: &str, binary: &str) -> Option<String> {
633    if !rewrite_registry::is_file_read_command(cmd) {
634        return None;
635    }
636
637    // Compound commands (pipes, chains) should not be rewritten as file reads.
638    if cmd.contains('|') || cmd.contains("&&") || cmd.contains("||") || cmd.contains(';') {
639        return None;
640    }
641
642    // Shell redirections indicate complex usage — don't rewrite.
643    if cmd.contains(">&") || cmd.contains(">>") || cmd.contains(" >") {
644        return None;
645    }
646
647    let parts = shell_tokenize(cmd);
648    if parts.len() < 2 {
649        return None;
650    }
651
652    match parts[0].as_str() {
653        "cat" => {
654            let path = parts[1..].join(" ");
655            if is_outside_project_path(&path) {
656                return None;
657            }
658            Some(format!("{binary} read {}", shell_quote(&path)))
659        }
660        "head" => {
661            let refs: Vec<&str> = parts[1..].iter().map(String::as_str).collect();
662            let (n, path) = parse_head_tail_args(&refs);
663            let path = path?;
664            if is_outside_project_path(path) {
665                return None;
666            }
667            let qp = shell_quote(path);
668            match n {
669                Some(lines) => Some(format!("{binary} read {qp} -m lines:1-{lines}")),
670                None => Some(format!("{binary} read {qp} -m lines:1-10")),
671            }
672        }
673        "tail" => {
674            let refs: Vec<&str> = parts[1..].iter().map(String::as_str).collect();
675            let (n, path) = parse_head_tail_args(&refs);
676            let path = path?;
677            if is_outside_project_path(path) {
678                return None;
679            }
680            let qp = shell_quote(path);
681            let lines = n.unwrap_or(10);
682            Some(format!("{binary} read {qp} -m lines:-{lines}"))
683        }
684        _ => None,
685    }
686}
687
688/// Returns true if the path clearly points outside the current project.
689/// Paths starting with `~`, `$`, or absolute paths that don't resolve
690/// within the working directory should not be intercepted.
691fn is_outside_project_path(path: &str) -> bool {
692    let trimmed = path.trim();
693
694    // Home-relative paths are always outside the project
695    if trimmed.starts_with('~') {
696        return true;
697    }
698
699    // Environment variable expansion — too complex, pass through
700    if trimmed.starts_with('$') {
701        return true;
702    }
703
704    // /proc, /sys, /dev, /tmp, /var — system paths
705    if trimmed.starts_with("/proc/")
706        || trimmed.starts_with("/sys/")
707        || trimmed.starts_with("/dev/")
708        || trimmed.starts_with("/tmp/")
709        || trimmed.starts_with("/var/")
710    {
711        return true;
712    }
713
714    // Absolute paths: only pass through if they clearly point outside.
715    // We can't know the project root here (hooks are stateless), but we can
716    // detect common external patterns.
717    if trimmed.starts_with('/') {
718        // Home directory paths (e.g. /Users/*/Library, /home/*/.config)
719        if trimmed.contains("/Library/") || trimmed.contains("/.config/") {
720            return true;
721        }
722        // lean-ctx's own data directories
723        if trimmed.contains("/.lean-ctx/") || trimmed.contains("/lean-ctx/logs/") {
724            return true;
725        }
726    }
727
728    false
729}
730
731/// Rewrites `rg <pattern> [path]` to `lean-ctx grep <pattern> [path]` for simple forms.
732fn rewrite_search_command(cmd: &str, binary: &str) -> Option<String> {
733    let parts = shell_tokenize(cmd);
734    if parts.first().map(String::as_str) != Some("rg") {
735        return None;
736    }
737    if parts.len() < 2 || parts.len() > 3 {
738        return None;
739    }
740    if parts[1].starts_with('-') {
741        return None;
742    }
743    let pattern = &parts[1];
744    match parts.get(2) {
745        Some(p) if p.starts_with('-') => None,
746        Some(p) => Some(format!("{binary} grep {pattern} {}", shell_quote(p))),
747        None => Some(format!("{binary} grep {pattern}")),
748    }
749}
750
751/// Rewrites simple `ls [path]` to `lean-ctx ls [path]`.
752fn rewrite_dir_list_command(cmd: &str, binary: &str) -> Option<String> {
753    let parts = shell_tokenize(cmd);
754    if parts.first().map(String::as_str) != Some("ls") {
755        return None;
756    }
757    match parts.len() {
758        1 => Some(format!("{binary} ls")),
759        2 if !parts[1].starts_with('-') => Some(format!("{binary} ls {}", shell_quote(&parts[1]))),
760        _ => None,
761    }
762}
763
764/// Tokenize a shell command respecting single/double quotes and backslash escapes.
765pub fn shell_tokenize(input: &str) -> Vec<String> {
766    let mut tokens = Vec::new();
767    let mut current = String::new();
768    let mut chars = input.chars().peekable();
769    let mut in_single = false;
770    let mut in_double = false;
771
772    while let Some(c) = chars.next() {
773        match c {
774            '\'' if !in_double => in_single = !in_single,
775            '"' if !in_single => in_double = !in_double,
776            '\\' if !in_single => {
777                if let Some(next) = chars.next() {
778                    current.push(next);
779                }
780            }
781            c if c.is_whitespace() && !in_single && !in_double => {
782                if !current.is_empty() {
783                    tokens.push(std::mem::take(&mut current));
784                }
785            }
786            _ => current.push(c),
787        }
788    }
789    if !current.is_empty() {
790        tokens.push(current);
791    }
792    tokens
793}
794
795/// Quote a path/arg for shell if it contains spaces or special chars.
796pub fn shell_quote(s: &str) -> String {
797    if s.contains(|c: char| c.is_whitespace() || c == '\'' || c == '"' || c == '\\') {
798        format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
799    } else {
800        s.to_string()
801    }
802}
803
804fn parse_head_tail_args<'a>(args: &[&'a str]) -> (Option<usize>, Option<&'a str>) {
805    let mut n: Option<usize> = None;
806    let mut path: Option<&str> = None;
807
808    let mut i = 0;
809    while i < args.len() {
810        if args[i] == "-n" && i + 1 < args.len() {
811            n = args[i + 1].parse().ok();
812            i += 2;
813        } else if let Some(num) = args[i].strip_prefix("-n") {
814            n = num.parse().ok();
815            i += 1;
816        } else if args[i].starts_with('-') && args[i].len() > 1 {
817            if let Ok(num) = args[i][1..].parse::<usize>() {
818                n = Some(num);
819            }
820            i += 1;
821        } else {
822            path = Some(args[i]);
823            i += 1;
824        }
825    }
826
827    (n, path)
828}
829
830fn build_rewrite_compound(cmd: &str, binary: &str) -> Option<String> {
831    compound_lexer::rewrite_compound(cmd, |segment| {
832        if segment.starts_with("lean-ctx ") || segment.starts_with(&format!("{binary} ")) {
833            return None;
834        }
835        if is_rewritable(segment) {
836            Some(wrap_single_command(segment, binary))
837        } else {
838            None
839        }
840    })
841}
842
843fn emit_rewrite(rewritten: &str) {
844    let json_escaped = rewritten.replace('\\', "\\\\").replace('"', "\\\"");
845    print!(
846        "{{\"hookSpecificOutput\":{{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"allow\",\"updatedInput\":{{\"command\":\"{json_escaped}\"}}}}}}"
847    );
848}
849
850pub fn handle_redirect() {
851    let allow = build_dual_allow_output();
852    if is_disabled() {
853        let _ = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT);
854        print!("{allow}");
855        return;
856    }
857
858    let Some(input) = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT) else {
859        print!("{allow}");
860        return;
861    };
862
863    let Ok(v) = serde_json::from_str::<serde_json::Value>(&input) else {
864        tracing::warn!("[hook redirect] invalid JSON payload, allowing passthrough");
865        print!("{allow}");
866        return;
867    };
868
869    let tool_name = v.get("tool_name").and_then(|t| t.as_str()).unwrap_or("");
870    let tool_input = v.get("tool_input");
871
872    match tool_name {
873        "Read" | "read" | "read_file" => redirect_read(tool_input),
874        "Grep" | "grep" | "search" | "ripgrep" => redirect_grep(tool_input),
875        _ => print!("{allow}"),
876    }
877}
878
879/// Redirect Read through lean-ctx for compression + caching.
880/// Safe because `mark_hook_environment()` sets LEAN_CTX_HOOK_CHILD=1 which
881/// prevents daemon auto-start. The subprocess uses the fast local-only path.
882fn redirect_read(tool_input: Option<&serde_json::Value>) {
883    let path = tool_input
884        .and_then(|ti| ti.get("path"))
885        .and_then(|p| p.as_str())
886        .unwrap_or("");
887
888    if path.is_empty() || should_passthrough(path) {
889        print!("{}", build_dual_allow_output());
890        return;
891    }
892
893    if is_harden_active() {
894        tracing::info!("[hook redirect] harden mode active, redirecting Read through lean-ctx");
895    }
896
897    let binary = resolve_binary();
898    let temp_path = redirect_temp_path(path);
899
900    if let Some(output) = run_with_timeout(&binary, &["read", path], REDIRECT_SUBPROCESS_TIMEOUT) {
901        if !output.is_empty() && std::fs::write(&temp_path, &output).is_ok() {
902            let temp_str = temp_path.to_str().unwrap_or("");
903            print!("{}", build_redirect_output(tool_input, "path", temp_str));
904            return;
905        }
906    }
907
908    print!("{}", build_dual_allow_output());
909}
910
911/// Redirect Grep through lean-ctx for compressed results.
912fn redirect_grep(tool_input: Option<&serde_json::Value>) {
913    let pattern = tool_input
914        .and_then(|ti| ti.get("pattern"))
915        .and_then(|p| p.as_str())
916        .unwrap_or("");
917    let search_path = tool_input
918        .and_then(|ti| ti.get("path"))
919        .and_then(|p| p.as_str())
920        .unwrap_or(".");
921
922    if pattern.is_empty() {
923        print!("{}", build_dual_allow_output());
924        return;
925    }
926
927    if is_harden_active() {
928        tracing::info!("[hook redirect] harden mode active, redirecting Grep through lean-ctx");
929    }
930
931    let binary = resolve_binary();
932    let key = format!("grep:{pattern}:{search_path}");
933    let temp_path = redirect_temp_path(&key);
934
935    if let Some(output) = run_with_timeout(
936        &binary,
937        &["grep", pattern, search_path],
938        REDIRECT_SUBPROCESS_TIMEOUT,
939    ) {
940        if !output.is_empty() && std::fs::write(&temp_path, &output).is_ok() {
941            let temp_str = temp_path.to_str().unwrap_or("");
942            print!("{}", build_redirect_output(tool_input, "path", temp_str));
943            return;
944        }
945    }
946
947    print!("{}", build_dual_allow_output());
948}
949
950const REDIRECT_SUBPROCESS_TIMEOUT: Duration = Duration::from_secs(10);
951
952/// Run a lean-ctx subprocess with a hard timeout. Returns stdout on success.
953/// Kills the child if it exceeds the timeout to prevent orphan processes.
954fn run_with_timeout(binary: &str, args: &[&str], timeout: Duration) -> Option<Vec<u8>> {
955    let mut child = std::process::Command::new(binary)
956        .args(args)
957        .stdout(std::process::Stdio::piped())
958        .stderr(std::process::Stdio::null())
959        .spawn()
960        .ok()?;
961
962    let deadline = std::time::Instant::now() + timeout;
963    loop {
964        match child.try_wait() {
965            Ok(Some(status)) if status.success() => {
966                let mut stdout = Vec::new();
967                if let Some(mut out) = child.stdout.take() {
968                    let _ = out.read_to_end(&mut stdout);
969                }
970                return if stdout.is_empty() {
971                    None
972                } else {
973                    Some(stdout)
974                };
975            }
976            Ok(Some(_)) | Err(_) => return None,
977            Ok(None) => {
978                if std::time::Instant::now() > deadline {
979                    let _ = child.kill();
980                    let _ = child.wait();
981                    return None;
982                }
983                std::thread::sleep(Duration::from_millis(10));
984            }
985        }
986    }
987}
988
989fn redirect_temp_path(key: &str) -> std::path::PathBuf {
990    use std::collections::hash_map::DefaultHasher;
991    use std::hash::{Hash, Hasher};
992
993    let mut hasher = DefaultHasher::new();
994    key.hash(&mut hasher);
995    std::process::id().hash(&mut hasher);
996    let hash = hasher.finish();
997
998    let temp_dir = std::env::temp_dir().join("lean-ctx-hook");
999    let _ = std::fs::create_dir_all(&temp_dir);
1000    #[cfg(unix)]
1001    {
1002        use std::os::unix::fs::PermissionsExt;
1003        let _ = std::fs::set_permissions(&temp_dir, std::fs::Permissions::from_mode(0o700));
1004    }
1005    temp_dir.join(format!("{hash:016x}.lctx"))
1006}
1007
1008fn build_redirect_output(
1009    tool_input: Option<&serde_json::Value>,
1010    field: &str,
1011    temp_path: &str,
1012) -> String {
1013    let updated_input = if let Some(obj) = tool_input.and_then(|v| v.as_object()) {
1014        let mut m = obj.clone();
1015        m.insert(
1016            field.to_string(),
1017            serde_json::Value::String(temp_path.to_string()),
1018        );
1019        serde_json::Value::Object(m)
1020    } else {
1021        serde_json::json!({ field: temp_path })
1022    };
1023
1024    serde_json::json!({
1025        "permission": "allow",
1026        "updated_input": updated_input,
1027        "hookSpecificOutput": {
1028            "hookEventName": "PreToolUse",
1029            "permissionDecision": "allow",
1030            "updatedInput": { field: temp_path }
1031        }
1032    })
1033    .to_string()
1034}
1035
1036const PASSTHROUGH_SUBSTRINGS: &[&str] = &[
1037    ".cursorrules",
1038    ".cursor/rules",
1039    ".cursor/hooks",
1040    "skill.md",
1041    "agents.md",
1042    ".env",
1043    "hooks.json",
1044    "node_modules",
1045];
1046
1047const PASSTHROUGH_EXTENSIONS: &[&str] = &[
1048    "lock", "png", "jpg", "jpeg", "gif", "webp", "pdf", "ico", "svg", "woff", "woff2", "ttf", "eot",
1049];
1050
1051fn should_passthrough(path: &str) -> bool {
1052    let p = path.to_lowercase();
1053
1054    if PASSTHROUGH_SUBSTRINGS.iter().any(|s| p.contains(s)) {
1055        return true;
1056    }
1057
1058    std::path::Path::new(&p)
1059        .extension()
1060        .and_then(|ext| ext.to_str())
1061        .is_some_and(|ext| {
1062            PASSTHROUGH_EXTENSIONS
1063                .iter()
1064                .any(|e| ext.eq_ignore_ascii_case(e))
1065        })
1066}
1067
1068fn codex_reroute_message(rewritten: &str) -> String {
1069    format!(
1070        "Command should run via lean-ctx for compact output. Do not retry the original command. Re-run with: {rewritten}"
1071    )
1072}
1073
1074pub fn handle_codex_pretooluse() {
1075    if is_disabled() {
1076        return;
1077    }
1078    let binary = resolve_binary();
1079    let Some(input) = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT) else {
1080        return;
1081    };
1082
1083    let tool = extract_json_field(&input, "tool_name");
1084    if !matches!(tool.as_deref(), Some("Bash" | "bash")) {
1085        return;
1086    }
1087
1088    let Some(cmd) = extract_json_field(&input, "command") else {
1089        return;
1090    };
1091
1092    if let Some(rewritten) = rewrite_candidate(&cmd, &binary) {
1093        if is_quiet() {
1094            eprintln!("Re-run: {rewritten}");
1095        } else {
1096            eprintln!("{}", codex_reroute_message(&rewritten));
1097        }
1098        std::process::exit(2);
1099    }
1100}
1101
1102pub fn handle_codex_session_start() {
1103    if is_quiet() {
1104        return;
1105    }
1106    println!(
1107        "For shell commands matched by lean-ctx compression rules, prefer `lean-ctx -c \"<command>\"`. If a Bash call is blocked, rerun it with the exact command suggested by the hook."
1108    );
1109}
1110
1111/// Copilot-specific PreToolUse handler.
1112/// VS Code Copilot Chat uses the same hook format as Claude Code.
1113/// Tool names differ: "runInTerminal" / "editFile" instead of "Bash" / "Read".
1114pub fn handle_copilot() {
1115    if is_disabled() {
1116        return;
1117    }
1118    let binary = resolve_binary();
1119    let Some(input) = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT) else {
1120        return;
1121    };
1122
1123    let tool = extract_json_field(&input, "tool_name");
1124    let Some(tool_name) = tool.as_deref() else {
1125        return;
1126    };
1127
1128    let is_shell_tool = matches!(
1129        tool_name,
1130        "Bash" | "bash" | "runInTerminal" | "run_in_terminal" | "terminal" | "shell"
1131    );
1132    if !is_shell_tool {
1133        return;
1134    }
1135
1136    let Some(cmd) = extract_json_field(&input, "command") else {
1137        return;
1138    };
1139
1140    if let Some(rewritten) = rewrite_candidate(&cmd, &binary) {
1141        emit_rewrite(&rewritten);
1142    }
1143}
1144
1145/// Inline rewrite: takes a command as CLI args, prints the rewritten command to stdout.
1146/// Used by the OpenCode TS plugin where the command is passed as an argument,
1147/// not via stdin JSON. Uses native OS paths (not MSYS) because the calling
1148/// shell may be PowerShell or cmd on Windows.
1149pub fn handle_rewrite_inline() {
1150    if is_disabled() {
1151        return;
1152    }
1153    let binary = resolve_binary_native();
1154    let args: Vec<String> = std::env::args().collect();
1155    // args: [binary, "hook", "rewrite-inline", ...command parts]
1156    if args.len() < 4 {
1157        return;
1158    }
1159    let cmd = args[3..].join(" ");
1160
1161    if let Some(rewritten) = rewrite_candidate(&cmd, &binary) {
1162        print!("{rewritten}");
1163        return;
1164    }
1165
1166    if cmd.starts_with("lean-ctx ") || cmd.starts_with(&format!("{binary} ")) {
1167        print!("{cmd}");
1168        return;
1169    }
1170
1171    print!("{cmd}");
1172}
1173
1174fn resolve_binary() -> String {
1175    let path = crate::core::portable_binary::resolve_portable_binary();
1176    crate::hooks::to_bash_compatible_path(&path)
1177}
1178
1179fn resolve_binary_native() -> String {
1180    crate::core::portable_binary::resolve_portable_binary()
1181}
1182
1183fn extract_json_field(input: &str, field: &str) -> Option<String> {
1184    let key = format!("\"{field}\":");
1185    let key_pos = input.find(&key)?;
1186    let after_colon = &input[key_pos + key.len()..];
1187    let trimmed = after_colon.trim_start();
1188    if !trimmed.starts_with('"') {
1189        return None;
1190    }
1191    let rest = &trimmed[1..];
1192    let bytes = rest.as_bytes();
1193    let mut end = 0;
1194    while end < bytes.len() {
1195        if bytes[end] == b'\\' && end + 1 < bytes.len() {
1196            end += 2;
1197            continue;
1198        }
1199        if bytes[end] == b'"' {
1200            break;
1201        }
1202        end += 1;
1203    }
1204    if end >= bytes.len() {
1205        return None;
1206    }
1207    let raw = &rest[..end];
1208    Some(raw.replace("\\\"", "\"").replace("\\\\", "\\"))
1209}
1210
1211#[cfg(test)]
1212mod tests {
1213    use super::*;
1214
1215    fn expect_wrapped(cmd: &str, binary: &str) -> String {
1216        if cfg!(windows) {
1217            let escaped = cmd.replace('"', "\\\"");
1218            format!("{binary} -c \"{escaped}\"")
1219        } else {
1220            let shell_escaped = cmd.replace('\'', "'\\''");
1221            format!("{binary} -c '{shell_escaped}'")
1222        }
1223    }
1224
1225    #[test]
1226    fn is_rewritable_basic() {
1227        assert!(is_rewritable("git status"));
1228        assert!(is_rewritable("cargo test --lib"));
1229        assert!(is_rewritable("npm run build"));
1230        assert!(!is_rewritable("echo hello"));
1231        assert!(!is_rewritable("cd src"));
1232        assert!(!is_rewritable("cat file.rs"));
1233    }
1234
1235    #[test]
1236    fn file_read_rewrite_cat() {
1237        let r = rewrite_file_read_command("cat src/main.rs", "lean-ctx");
1238        assert_eq!(r, Some("lean-ctx read src/main.rs".to_string()));
1239    }
1240
1241    #[test]
1242    fn file_read_rewrite_head_with_n() {
1243        let r = rewrite_file_read_command("head -n 20 src/main.rs", "lean-ctx");
1244        assert_eq!(
1245            r,
1246            Some("lean-ctx read src/main.rs -m lines:1-20".to_string())
1247        );
1248    }
1249
1250    #[test]
1251    fn file_read_rewrite_head_short() {
1252        let r = rewrite_file_read_command("head -50 src/main.rs", "lean-ctx");
1253        assert_eq!(
1254            r,
1255            Some("lean-ctx read src/main.rs -m lines:1-50".to_string())
1256        );
1257    }
1258
1259    #[test]
1260    fn file_read_rewrite_tail() {
1261        let r = rewrite_file_read_command("tail -n 10 src/main.rs", "lean-ctx");
1262        assert_eq!(
1263            r,
1264            Some("lean-ctx read src/main.rs -m lines:-10".to_string())
1265        );
1266    }
1267
1268    #[test]
1269    fn file_read_rewrite_not_git() {
1270        assert_eq!(rewrite_file_read_command("git status", "lean-ctx"), None);
1271    }
1272
1273    #[test]
1274    fn file_read_skips_home_relative_paths() {
1275        assert_eq!(
1276            rewrite_file_read_command("cat ~/Library/Logs/proxy.log", "lean-ctx"),
1277            None
1278        );
1279        assert_eq!(
1280            rewrite_file_read_command("head -20 ~/.lean-ctx/logs/proxy.stderr.log", "lean-ctx"),
1281            None
1282        );
1283        assert_eq!(
1284            rewrite_file_read_command("tail -50 ~/some/file.txt", "lean-ctx"),
1285            None
1286        );
1287    }
1288
1289    #[test]
1290    fn file_read_skips_system_paths() {
1291        assert_eq!(
1292            rewrite_file_read_command("cat /tmp/test.log", "lean-ctx"),
1293            None
1294        );
1295        assert_eq!(
1296            rewrite_file_read_command("cat /var/log/syslog", "lean-ctx"),
1297            None
1298        );
1299        assert_eq!(
1300            rewrite_file_read_command("cat /proc/cpuinfo", "lean-ctx"),
1301            None
1302        );
1303    }
1304
1305    #[test]
1306    fn file_read_skips_env_var_paths() {
1307        assert_eq!(
1308            rewrite_file_read_command("cat $HOME/.bashrc", "lean-ctx"),
1309            None
1310        );
1311    }
1312
1313    #[test]
1314    fn file_read_skips_library_and_config_paths() {
1315        assert_eq!(
1316            rewrite_file_read_command(
1317                "cat /Users/user/Library/LaunchAgents/com.leanctx.proxy.plist",
1318                "lean-ctx"
1319            ),
1320            None
1321        );
1322        assert_eq!(
1323            rewrite_file_read_command("cat /home/user/.config/lean-ctx/config.toml", "lean-ctx"),
1324            None
1325        );
1326    }
1327
1328    #[test]
1329    fn file_read_skips_pipes_and_redirects() {
1330        assert_eq!(
1331            rewrite_file_read_command("cat file.rs | grep fn", "lean-ctx"),
1332            None
1333        );
1334        assert_eq!(
1335            rewrite_file_read_command("cat file.rs 2>&1", "lean-ctx"),
1336            None
1337        );
1338        assert_eq!(
1339            rewrite_file_read_command("cat file.rs >> output.log", "lean-ctx"),
1340            None
1341        );
1342        assert_eq!(
1343            rewrite_file_read_command("cat a.rs && cat b.rs", "lean-ctx"),
1344            None
1345        );
1346        assert_eq!(
1347            rewrite_file_read_command("cat a.rs; echo done", "lean-ctx"),
1348            None
1349        );
1350    }
1351
1352    #[test]
1353    fn file_read_still_rewrites_project_relative_paths() {
1354        assert_eq!(
1355            rewrite_file_read_command("cat src/main.rs", "lean-ctx"),
1356            Some("lean-ctx read src/main.rs".to_string())
1357        );
1358        assert_eq!(
1359            rewrite_file_read_command("cat ./Cargo.toml", "lean-ctx"),
1360            Some("lean-ctx read ./Cargo.toml".to_string())
1361        );
1362        assert_eq!(
1363            rewrite_file_read_command("head -20 src/lib.rs", "lean-ctx"),
1364            Some("lean-ctx read src/lib.rs -m lines:1-20".to_string())
1365        );
1366    }
1367
1368    #[test]
1369    fn is_outside_project_path_tests() {
1370        assert!(is_outside_project_path("~/foo"));
1371        assert!(is_outside_project_path("~/.lean-ctx/config.toml"));
1372        assert!(is_outside_project_path("$HOME/.bashrc"));
1373        assert!(is_outside_project_path("/tmp/test"));
1374        assert!(is_outside_project_path("/var/log/syslog"));
1375        assert!(is_outside_project_path("/proc/cpuinfo"));
1376        assert!(is_outside_project_path("/Users/x/Library/Logs/foo.log"));
1377        assert!(is_outside_project_path("/home/x/.config/app/conf"));
1378        assert!(is_outside_project_path("/root/.lean-ctx/logs/proxy.log"));
1379
1380        assert!(!is_outside_project_path("src/main.rs"));
1381        assert!(!is_outside_project_path("./Cargo.toml"));
1382        assert!(!is_outside_project_path("../sibling/file.rs"));
1383        assert!(!is_outside_project_path("file.txt"));
1384    }
1385
1386    #[test]
1387    fn parse_head_tail_args_basic() {
1388        let (n, path) = parse_head_tail_args(&["-n", "20", "file.rs"]);
1389        assert_eq!(n, Some(20));
1390        assert_eq!(path, Some("file.rs"));
1391    }
1392
1393    #[test]
1394    fn parse_head_tail_args_combined() {
1395        let (n, path) = parse_head_tail_args(&["-n20", "file.rs"]);
1396        assert_eq!(n, Some(20));
1397        assert_eq!(path, Some("file.rs"));
1398    }
1399
1400    #[test]
1401    fn parse_head_tail_args_short_flag() {
1402        let (n, path) = parse_head_tail_args(&["-50", "file.rs"]);
1403        assert_eq!(n, Some(50));
1404        assert_eq!(path, Some("file.rs"));
1405    }
1406
1407    #[test]
1408    fn should_passthrough_rules_files() {
1409        assert!(should_passthrough("/home/user/.cursorrules"));
1410        assert!(should_passthrough("/project/.cursor/rules/test.mdc"));
1411        assert!(should_passthrough("/home/.cursor/hooks/hooks.json"));
1412        assert!(should_passthrough("/project/SKILL.md"));
1413        assert!(should_passthrough("/project/AGENTS.md"));
1414        assert!(should_passthrough("/project/icon.png"));
1415        assert!(!should_passthrough("/project/src/main.rs"));
1416        assert!(!should_passthrough("/project/src/lib.ts"));
1417    }
1418
1419    #[test]
1420    fn wrap_single() {
1421        let r = wrap_single_command("git status", "lean-ctx");
1422        assert_eq!(r, expect_wrapped("git status", "lean-ctx"));
1423    }
1424
1425    #[test]
1426    fn wrap_with_quotes() {
1427        let r = wrap_single_command(r#"curl -H "Auth" https://api.com"#, "lean-ctx");
1428        assert_eq!(
1429            r,
1430            expect_wrapped(r#"curl -H "Auth" https://api.com"#, "lean-ctx")
1431        );
1432    }
1433
1434    #[test]
1435    fn rewrite_candidate_returns_none_for_existing_lean_ctx_command() {
1436        assert_eq!(
1437            rewrite_candidate("lean-ctx -c git status", "lean-ctx"),
1438            None
1439        );
1440    }
1441
1442    #[test]
1443    fn rewrite_candidate_wraps_single_command() {
1444        assert_eq!(
1445            rewrite_candidate("git status", "lean-ctx"),
1446            Some(expect_wrapped("git status", "lean-ctx"))
1447        );
1448    }
1449
1450    #[test]
1451    fn rewrite_candidate_passes_through_heredoc() {
1452        assert_eq!(
1453            rewrite_candidate(
1454                "git commit -m \"$(cat <<'EOF'\nfix: something\nEOF\n)\"",
1455                "lean-ctx"
1456            ),
1457            None
1458        );
1459    }
1460
1461    #[test]
1462    fn rewrite_candidate_passes_through_heredoc_compound() {
1463        assert_eq!(
1464            rewrite_candidate(
1465                "git add . && git commit -m \"$(cat <<EOF\nfeat: add\nEOF\n)\"",
1466                "lean-ctx"
1467            ),
1468            None
1469        );
1470    }
1471
1472    #[test]
1473    fn codex_reroute_message_includes_exact_rewritten_command() {
1474        let message = codex_reroute_message("lean-ctx -c 'git status'");
1475        assert_eq!(
1476            message,
1477            "Command should run via lean-ctx for compact output. Do not retry the original command. Re-run with: lean-ctx -c 'git status'"
1478        );
1479    }
1480
1481    #[test]
1482    fn compound_rewrite_and_chain() {
1483        let result = build_rewrite_compound("cd src && git status && echo done", "lean-ctx");
1484        let w = expect_wrapped("git status", "lean-ctx");
1485        assert_eq!(result, Some(format!("cd src && {w} && echo done")));
1486    }
1487
1488    #[test]
1489    fn compound_rewrite_pipe() {
1490        let result = build_rewrite_compound("git log --oneline | head -5", "lean-ctx");
1491        let w = expect_wrapped("git log --oneline", "lean-ctx");
1492        assert_eq!(result, Some(format!("{w} | head -5")));
1493    }
1494
1495    #[test]
1496    fn compound_rewrite_no_match() {
1497        let result = build_rewrite_compound("cd src && echo done", "lean-ctx");
1498        assert_eq!(result, None);
1499    }
1500
1501    #[test]
1502    fn compound_rewrite_multiple_rewritable() {
1503        let result = build_rewrite_compound("git add . && cargo test && npm run lint", "lean-ctx");
1504        let w1 = expect_wrapped("git add .", "lean-ctx");
1505        let w2 = expect_wrapped("cargo test", "lean-ctx");
1506        let w3 = expect_wrapped("npm run lint", "lean-ctx");
1507        assert_eq!(result, Some(format!("{w1} && {w2} && {w3}")));
1508    }
1509
1510    #[test]
1511    fn compound_rewrite_semicolons() {
1512        let result = build_rewrite_compound("git add .; git commit -m 'fix'", "lean-ctx");
1513        let w1 = expect_wrapped("git add .", "lean-ctx");
1514        let w2 = expect_wrapped("git commit -m 'fix'", "lean-ctx");
1515        assert_eq!(result, Some(format!("{w1} ; {w2}")));
1516    }
1517
1518    #[test]
1519    fn compound_rewrite_or_chain() {
1520        let result = build_rewrite_compound("git pull || echo failed", "lean-ctx");
1521        let w = expect_wrapped("git pull", "lean-ctx");
1522        assert_eq!(result, Some(format!("{w} || echo failed")));
1523    }
1524
1525    #[test]
1526    fn compound_skips_already_rewritten() {
1527        let result = build_rewrite_compound("lean-ctx -c git status && git diff", "lean-ctx");
1528        let w = expect_wrapped("git diff", "lean-ctx");
1529        assert_eq!(result, Some(format!("lean-ctx -c git status && {w}")));
1530    }
1531
1532    #[test]
1533    fn single_command_not_compound() {
1534        let result = build_rewrite_compound("git status", "lean-ctx");
1535        assert_eq!(result, None);
1536    }
1537
1538    #[test]
1539    fn extract_field_works() {
1540        let input = r#"{"tool_name":"Bash","command":"git status"}"#;
1541        assert_eq!(
1542            extract_json_field(input, "tool_name"),
1543            Some("Bash".to_string())
1544        );
1545        assert_eq!(
1546            extract_json_field(input, "command"),
1547            Some("git status".to_string())
1548        );
1549    }
1550
1551    #[test]
1552    fn extract_field_with_spaces_after_colon() {
1553        let input = r#"{"tool_name": "Bash", "tool_input": {"command": "git status"}}"#;
1554        assert_eq!(
1555            extract_json_field(input, "tool_name"),
1556            Some("Bash".to_string())
1557        );
1558        assert_eq!(
1559            extract_json_field(input, "command"),
1560            Some("git status".to_string())
1561        );
1562    }
1563
1564    #[test]
1565    fn extract_field_pretty_printed() {
1566        let input = "{\n  \"tool_name\": \"Bash\",\n  \"tool_input\": {\n    \"command\": \"npm test\"\n  }\n}";
1567        assert_eq!(
1568            extract_json_field(input, "tool_name"),
1569            Some("Bash".to_string())
1570        );
1571        assert_eq!(
1572            extract_json_field(input, "command"),
1573            Some("npm test".to_string())
1574        );
1575    }
1576
1577    #[test]
1578    fn extract_field_handles_escaped_quotes() {
1579        let input = r#"{"tool_name":"Bash","command":"grep -r \"TODO\" src/"}"#;
1580        assert_eq!(
1581            extract_json_field(input, "command"),
1582            Some(r#"grep -r "TODO" src/"#.to_string())
1583        );
1584    }
1585
1586    #[test]
1587    fn extract_field_handles_escaped_backslash() {
1588        let input = r#"{"tool_name":"Bash","command":"echo \\\"hello\\\""}"#;
1589        assert_eq!(
1590            extract_json_field(input, "command"),
1591            Some(r#"echo \"hello\""#.to_string())
1592        );
1593    }
1594
1595    #[test]
1596    fn extract_field_handles_complex_curl() {
1597        let input = r#"{"tool_name":"Bash","command":"curl -H \"Authorization: Bearer token\" https://api.com"}"#;
1598        assert_eq!(
1599            extract_json_field(input, "command"),
1600            Some(r#"curl -H "Authorization: Bearer token" https://api.com"#.to_string())
1601        );
1602    }
1603
1604    #[test]
1605    fn to_bash_compatible_path_windows_drive() {
1606        let p = crate::hooks::to_bash_compatible_path(r"E:\packages\lean-ctx.exe");
1607        assert_eq!(p, "/e/packages/lean-ctx.exe");
1608    }
1609
1610    #[test]
1611    fn to_bash_compatible_path_backslashes() {
1612        let p = crate::hooks::to_bash_compatible_path(r"C:\Users\test\bin\lean-ctx.exe");
1613        assert_eq!(p, "/c/Users/test/bin/lean-ctx.exe");
1614    }
1615
1616    #[test]
1617    fn to_bash_compatible_path_unix_unchanged() {
1618        let p = crate::hooks::to_bash_compatible_path("/usr/local/bin/lean-ctx");
1619        assert_eq!(p, "/usr/local/bin/lean-ctx");
1620    }
1621
1622    #[test]
1623    fn to_bash_compatible_path_msys2_unchanged() {
1624        let p = crate::hooks::to_bash_compatible_path("/e/packages/lean-ctx.exe");
1625        assert_eq!(p, "/e/packages/lean-ctx.exe");
1626    }
1627
1628    #[test]
1629    fn wrap_command_with_bash_path() {
1630        let binary = crate::hooks::to_bash_compatible_path(r"E:\packages\lean-ctx.exe");
1631        let result = wrap_single_command("git status", &binary);
1632        assert!(
1633            !result.contains('\\'),
1634            "wrapped command must not contain backslashes, got: {result}"
1635        );
1636        assert!(
1637            result.starts_with("/e/packages/lean-ctx.exe"),
1638            "must use bash-compatible path, got: {result}"
1639        );
1640    }
1641
1642    #[test]
1643    fn wrap_single_command_em_dash() {
1644        let r = wrap_single_command("gh --comment \"closing — see #407\"", "lean-ctx");
1645        assert_eq!(
1646            r,
1647            expect_wrapped("gh --comment \"closing — see #407\"", "lean-ctx")
1648        );
1649    }
1650
1651    #[test]
1652    fn wrap_single_command_dollar_sign() {
1653        let r = wrap_single_command("echo $HOME", "lean-ctx");
1654        assert_eq!(r, expect_wrapped("echo $HOME", "lean-ctx"));
1655    }
1656
1657    #[test]
1658    fn wrap_single_command_backticks() {
1659        let r = wrap_single_command("echo `date`", "lean-ctx");
1660        assert_eq!(r, expect_wrapped("echo `date`", "lean-ctx"));
1661    }
1662
1663    #[test]
1664    fn wrap_single_command_nested_single_quotes() {
1665        let r = wrap_single_command("echo 'hello world'", "lean-ctx");
1666        assert_eq!(r, expect_wrapped("echo 'hello world'", "lean-ctx"));
1667    }
1668
1669    #[test]
1670    fn wrap_single_command_exclamation_mark() {
1671        let r = wrap_single_command("echo hello!", "lean-ctx");
1672        assert_eq!(r, expect_wrapped("echo hello!", "lean-ctx"));
1673    }
1674
1675    #[test]
1676    fn wrap_single_command_find_with_many_excludes() {
1677        let cmd = "find . -not -path ./node_modules -not -path ./.git -not -path ./dist";
1678        let r = wrap_single_command(cmd, "lean-ctx");
1679        assert_eq!(r, expect_wrapped(cmd, "lean-ctx"));
1680    }
1681
1682    #[test]
1683    fn detect_event_type_tool_response_is_mcp_call() {
1684        let v = serde_json::json!({
1685            "tool_name": "ctx_read",
1686            "tool_response": "file contents here"
1687        });
1688        let event = detect_event_type(&v, 1000).unwrap();
1689        assert_eq!(event.event_type, "mcp_call");
1690    }
1691
1692    #[test]
1693    fn detect_event_type_tool_output_is_mcp_call() {
1694        let v = serde_json::json!({
1695            "tool_name": "ctx_search",
1696            "tool_output": "search results"
1697        });
1698        let event = detect_event_type(&v, 1000).unwrap();
1699        assert_eq!(event.event_type, "mcp_call");
1700    }
1701
1702    #[test]
1703    fn detect_event_type_ctx_prefix_is_mcp_call() {
1704        let v = serde_json::json!({
1705            "tool_name": "ctx_read",
1706            "tool_input": {"path": "src/main.rs"}
1707        });
1708        let event = detect_event_type(&v, 1000).unwrap();
1709        assert_eq!(event.event_type, "mcp_call");
1710    }
1711
1712    #[test]
1713    fn detect_event_type_mcp_prefix_is_mcp_call() {
1714        let v = serde_json::json!({
1715            "tool_name": "mcp__lean-ctx__ctx_read",
1716            "tool_input": {"path": "src/main.rs"}
1717        });
1718        let event = detect_event_type(&v, 1000).unwrap();
1719        assert_eq!(event.event_type, "mcp_call");
1720    }
1721
1722    #[test]
1723    fn detect_event_type_native_read_is_native_tool() {
1724        let v = serde_json::json!({
1725            "tool_name": "Read",
1726            "tool_input": {"path": "src/main.rs"}
1727        });
1728        let event = detect_event_type(&v, 1000).unwrap();
1729        assert_eq!(event.event_type, "native_tool");
1730    }
1731
1732    #[test]
1733    fn detect_event_type_result_json_is_mcp_call() {
1734        let v = serde_json::json!({
1735            "tool_name": "ctx_read",
1736            "result_json": {"content": "..."}
1737        });
1738        let event = detect_event_type(&v, 1000).unwrap();
1739        assert_eq!(event.event_type, "mcp_call");
1740    }
1741}