lean-ctx 3.9.12

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! File/search/glob redirect decision logic for the `hook redirect` entry point.
//!
//! Extracted from `hook_handlers::mod` (#660/#966 LOC gate): redirects a
//! host's native Read/Grep/Glob tool call through the equivalent lean-ctx
//! subcommand for compression, caching, and (in shadow/harden mode) telemetry.

use super::{
    HOOK_STDIN_TIMEOUT, build_dual_allow_output, dedup, is_disabled, is_harden_active,
    is_shadow_mode_active, log_shadow_intercept, payload, read_stdin_with_timeout, resolve_binary,
};
use crate::core::debug_log::{self, Route};
use std::io::Read;
use std::time::Duration;

/// The lean-ctx redirect a host tool name maps to, if any.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum RedirectKind {
    Read,
    Grep,
    Glob,
    None,
}

/// Classify a host tool name into the lean-ctx redirect it should take.
///
/// Covers the documented read/search/glob tool names across hosts. Copilot CLI
/// fires the redirect hook for *every* tool call and dispatches purely on the tool
/// name, so its aliases must be listed here: `view` (its read tool) and `rg` (its
/// search alias) were previously unmatched and passed through uncompressed (#562).
pub(super) fn classify_redirect(tool_name: &str) -> RedirectKind {
    match tool_name {
        "Read" | "read" | "read_file" | "view" => RedirectKind::Read,
        "Grep" | "grep" | "search" | "ripgrep" | "rg" => RedirectKind::Grep,
        "Glob" | "glob" => RedirectKind::Glob,
        _ => RedirectKind::None,
    }
}

/// Decide the redirect hook's stdout (a redirect or an allow-passthrough) without
/// printing, so `handle_redirect` can run it under the fail-open timeout (#1035).
pub(super) fn compute_redirect() -> String {
    if is_disabled() {
        let _ = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT);
        return build_dual_allow_output();
    }

    let Some(input) = read_stdin_with_timeout(HOOK_STDIN_TIMEOUT) else {
        return build_dual_allow_output();
    };

    let Ok(v) = serde_json::from_str::<serde_json::Value>(&input) else {
        tracing::warn!("[hook redirect] invalid JSON payload, allowing passthrough");
        return build_dual_allow_output();
    };

    // Normalise host payload shapes (snake_case vs Copilot CLI camelCase, #551).
    let tool_name = payload::resolve_tool_name(&v).unwrap_or_default();
    let tool_args = payload::resolve_tool_args(&v);

    let kind = classify_redirect(&tool_name);
    if matches!(kind, RedirectKind::None) {
        return build_dual_allow_output();
    }

    // #1032: Cursor fires preToolUse twice (two processes, identical payload), so a
    // naive redirect runs the lean-ctx subprocess and logs twice. Dedup on a
    // PID-independent key (tool + args) so the second fire replays the first's
    // response — one subprocess, one log entry.
    let args_json = tool_args
        .as_ref()
        .map(ToString::to_string)
        .unwrap_or_default();
    let key_material = format!("{tool_name}\u{0}{args_json}");
    dedup::deduped("redirect", &key_material, || {
        produce_redirect_output(kind, tool_args.as_ref())
    })
}

/// Build the redirect stdout for a classified tool call. Returns the full hook
/// response (redirect or allow-passthrough) so `handle_redirect` can route it
/// through the double-fire dedup before printing exactly once.
fn produce_redirect_output(kind: RedirectKind, tool_args: Option<&serde_json::Value>) -> String {
    match kind {
        RedirectKind::Read => redirect_read(tool_args),
        RedirectKind::Grep => redirect_grep(tool_args),
        RedirectKind::Glob => redirect_glob(tool_args),
        RedirectKind::None => build_dual_allow_output(),
    }
}

/// Argv for the `lean-ctx read` subprocess a redirected native Read runs.
///
/// Smart mode selection: windowed reads (offset/limit) use `full-compact` to
/// preserve line structure for correct indexing. Full reads use `auto` which
/// selects the optimal compression mode (signatures, map, etc.) — achieving
/// 87-97% compression vs ~5% for full-compact. Safe on Cursor because
/// StrReplace does NOT fire a Read PreToolUse (validated by edit-probe PoC).
pub(super) fn redirect_read_args(path: &str, is_windowed: bool) -> Vec<String> {
    let mode = if is_windowed { "full-compact" } else { "auto" };
    vec![
        "read".to_string(),
        path.to_string(),
        "-m".to_string(),
        mode.to_string(),
    ]
}

/// Redirect Read through lean-ctx for compression + caching.
/// Safe because `mark_hook_environment()` sets LEAN_CTX_HOOK_CHILD=1 which
/// prevents daemon auto-start. The subprocess uses the fast local-only path.
pub(super) fn redirect_read(tool_input: Option<&serde_json::Value>) -> String {
    // Hosts disagree on the path field: Cursor/Claude send `file_path`, some MCP
    // schemas use `path`. Resolve across all of them and remember WHICH field
    // matched so the redirect rewrites the same field the host reads back.
    let Some((path_field, path)) =
        payload::resolve_path_field(tool_input, payload::READ_PATH_FIELDS)
    else {
        debug_log::log_hook_decision(
            "redirect",
            "Read",
            Route::Native,
            "<none>",
            "no path in tool input",
        );
        return build_dual_allow_output();
    };
    // #637: on hosts with a native read-before-write guard (Claude Code /
    // CodeBuddy), rewriting the Read to a temp `.lctx` copy makes the guard track
    // the temp path, so a later native Write/Edit to the real file fails with
    // "File has not been read yet". `read_redirect = auto` (default) disables the
    // Read redirect there so native Read reads the real file and the guard stays
    // intact; compression flows through the explicit ctx_read MCP tool instead.
    // Evaluated per hook fire (fresh Config + env), so it also covers headless
    // `claude -p` and never needs to fight the settings.json self-heal.
    if !crate::core::config::ReadRedirect::read_redirect_enabled(
        &crate::core::config::Config::load(),
    ) {
        debug_log::log_hook_decision(
            "redirect",
            "Read",
            Route::Native,
            &path,
            "read redirect disabled (host guard/config)",
        );
        return build_dual_allow_output();
    }
    if should_passthrough(&path) {
        debug_log::log_hook_decision(
            "redirect",
            "Read",
            Route::Native,
            &path,
            "passthrough path (sensitive/binary/excluded)",
        );
        return build_dual_allow_output();
    }

    let shadow = is_shadow_mode_active();
    if is_harden_active() || shadow {
        tracing::info!(
            "[hook redirect] {} active, redirecting Read through lean-ctx",
            if shadow { "shadow mode" } else { "harden mode" }
        );
    }

    let binary = resolve_binary();
    let temp_path = redirect_temp_path(&path);

    // Re-read handling (#938, #1048): when a marker exists, the model already
    // saw the compressed view. The strategy depends on the host:
    //
    // **Cursor** (no read-before-write guard): re-redirect through lean-ctx to
    // produce the *same* compressed output. The file is unchanged so the result
    // is byte-identical to the first read (prompt-cache optimal). The marker
    // stays alive so read 3, 4, ... also get compressed. This eliminates the
    // every-other-read 0% savings gap (#1048).
    //
    // **Guard hosts** (Claude Code, CodeBuddy): native passthrough. These hosts
    // fire an internal Read before Write that also triggers this hook; a
    // compressed response would break the edit. The PostToolUse `read_dedup`
    // handler owns dedup on these hosts instead.
    //
    // Marker format: "{mtime}\n{read_count}" -- mtime detects file changes.
    let marker = redirect_read_marker(&path);
    if marker.exists() {
        if let Ok(marker_data) = std::fs::read_to_string(&marker) {
            let parts: Vec<&str> = marker_data.splitn(2, '\n').collect();
            let stored_mtime = parts.first().unwrap_or(&"");
            let current_mtime = file_mtime_str(&path);

            if current_mtime.as_str() == *stored_mtime {
                if crate::core::config::read_redirect::hook_host_is_cursor() {
                    // Cursor: re-compress unchanged file (safe, no guard).
                    // Keep marker alive so subsequent reads also compress.
                    debug_log::log_hook_decision(
                        "redirect",
                        "Read",
                        Route::LeanCtx,
                        &path,
                        "re-read re-compress (Cursor, no guard)",
                    );
                    // Fall through to lean-ctx redirect below.
                } else {
                    // Guard host: native passthrough (read_dedup owns dedup).
                    debug_log::log_hook_decision(
                        "redirect",
                        "Read",
                        Route::Native,
                        &path,
                        "re-read passthrough (guard host, edit-safe)",
                    );
                    let _ = std::fs::remove_file(&marker);
                    return build_dual_allow_output();
                }
            } else {
                debug_log::log_hook_decision(
                    "redirect",
                    "Read",
                    Route::LeanCtx,
                    &path,
                    "file changed since first read, re-compress",
                );
                let _ = std::fs::remove_file(&marker);
            }
        } else {
            let _ = std::fs::remove_file(&marker);
        }
    }
    let is_windowed =
        tool_input.is_some_and(|v| v.get("offset").is_some() || v.get("limit").is_some());
    let args = redirect_read_args(&path, is_windowed);
    let args_refs: Vec<&str> = args.iter().map(String::as_str).collect();

    if let Some(output) = run_with_timeout(&binary, &args_refs, REDIRECT_SUBPROCESS_TIMEOUT) {
        // #1019: never prepend a banner to `output` — it is written to the temp
        // file the host reads *as the file's content*, so an edit would round-trip
        // the banner back into the real file (it corrupted config.toml). The
        // shadow nudge rides the model-visible `additionalContext` side channel
        // instead, and the intercept is still recorded in shadow.log.
        //
        // #778/#marker-contamination: NEVER append REDIRECT_SUFFIX to the temp
        // file content. The host reads this file as if it were the real source;
        // if the agent then copies it back (StrReplace/Edit), the marker leaks
        // into source code. The nudge now travels via additionalContext (gated
        // by inject_context) or not at all — the drifting detection still feeds
        // the radar log and ctx_knowledge for non-destructive recall.
        let final_output = output;
        let drifting = matches!(
            crate::core::data_dir::lean_ctx_data_dir(),
            Ok(ref d) if crate::server::bypass_hint::model_is_drifting(d)
        );
        if !final_output.is_empty() && std::fs::write(&temp_path, &final_output).is_ok() {
            let temp_str = temp_path.to_str().unwrap_or("");

            // Warm daemon cache: subsequent ctx_read(mode=full) hits warm
            // BM25/session cache → instant edit-safe content. The redirect
            // gives compressed exploration view; ctx_read gives full content.
            warm_daemon_cache(&path);
            debug_log::log_hook_decision(
                "redirect",
                "Read",
                Route::LeanCtx,
                &path,
                "redirected to ctx_read",
            );
            // #778: nudges only via additionalContext when inject_context is opted in
            let note = if !inject_context_allowed() {
                None
            } else if shadow {
                Some(format!(
                    "lean-ctx shadow mode: this Read was served by ctx_read(\"{path}\", \"full\"). Call ctx_read directly for better performance."
                ))
            } else if drifting {
                Some(
                    crate::server::bypass_hint::REDIRECT_SUFFIX
                        .trim()
                        .to_string(),
                )
            } else {
                None
            };
            log_shadow_intercept("Read", &path);
            let _ = std::fs::write(&marker, format!("{}\n1", file_mtime_str(&path)));
            return build_redirect_output(tool_input, path_field, temp_str, note.as_deref());
        }
    }

    debug_log::log_hook_decision(
        "redirect",
        "Read",
        Route::Native,
        &path,
        "lean-ctx read produced no output",
    );
    build_dual_allow_output()
}

/// Redirect Grep through lean-ctx for compressed results.
/// The Grep redirect rewrites `path` to a temp file the host re-greps, which is
/// only faithful for `output_mode=content` (see [`redirect_grep`]). For
/// `files_with_matches` the host would report the temp file itself as the match,
/// and for `count` it would count lines in the temp file — both wrong. The hook
/// Hosts disagree on the Grep default: Cursor defaults to `content`, Claude
/// Code to `files_with_matches`. An explicit non-content mode (`count`,
/// `files_with_matches`) must NOT be redirected — the path-swap would surface
/// the temp file itself. When `output_mode` is absent, Cursor's default is
/// `content`, so the redirect is safe there. (GH #398 hook follow-up)
pub(super) fn grep_content_mode(tool_input: Option<&serde_json::Value>) -> bool {
    let Some(ti) = tool_input else {
        return false;
    };
    match ti.get("output_mode").and_then(|m| m.as_str()) {
        Some("content") => true,
        Some(_) => false,
        None => crate::core::config::read_redirect::hook_host_is_cursor(),
    }
}

fn redirect_grep(tool_input: Option<&serde_json::Value>) -> String {
    let pattern = tool_input
        .and_then(|ti| ti.get("pattern"))
        .and_then(|p| p.as_str())
        .unwrap_or("");
    let search_path = tool_input
        .and_then(|ti| ti.get("path"))
        .and_then(|p| p.as_str())
        .unwrap_or(".");

    if pattern.is_empty() {
        debug_log::log_hook_decision(
            "redirect",
            "Grep",
            Route::Native,
            "<none>",
            "no pattern in tool input",
        );
        return build_dual_allow_output();
    }

    if !grep_content_mode(tool_input) {
        debug_log::log_hook_decision(
            "redirect",
            "Grep",
            Route::Native,
            &format!("{pattern} in {search_path}"),
            "non-content output_mode — native passthrough (path-swap only valid for content)",
        );
        if is_shadow_mode_active() {
            log_shadow_intercept("Grep", &format!("{pattern} in {search_path}"));
        }
        return build_dual_allow_output();
    }

    let shadow = is_shadow_mode_active();
    if is_harden_active() || shadow {
        tracing::info!(
            "[hook redirect] {} active, redirecting Grep through lean-ctx",
            if shadow { "shadow mode" } else { "harden mode" }
        );
    }

    let binary = resolve_binary();
    let key = format!("grep:{pattern}:{search_path}");
    let temp_path = redirect_temp_path(&key);

    if let Some(output) = run_with_timeout(
        &binary,
        &["grep", pattern, search_path],
        REDIRECT_SUBPROCESS_TIMEOUT,
    ) {
        // #1019: the temp file is re-grepped by the host, so a banner line would
        // be a spurious match (and skew counts). Keep `output` byte-faithful; the
        // shadow nudge rides `additionalContext`, and shadow.log records it.
        if !output.is_empty() && std::fs::write(&temp_path, &output).is_ok() {
            let temp_str = temp_path.to_str().unwrap_or("");
            debug_log::log_hook_decision(
                "redirect",
                "Grep",
                Route::LeanCtx,
                &format!("{pattern} in {search_path}"),
                "redirected to ctx_search",
            );
            // #778: shadow_note only when inject_context is opted in (cache-safe)
            let shadow_note = shadow
                .then(|| {
                    inject_context_allowed().then(|| {
                        format!(
                            "lean-ctx shadow mode: this Grep was served by ctx_search(\"{pattern}\", \"{search_path}\"). Call ctx_search directly for better performance."
                        )
                    })
                })
                .flatten();
            log_shadow_intercept("Grep", &format!("{pattern} in {search_path}"));
            return build_redirect_output(tool_input, "path", temp_str, shadow_note.as_deref());
        }
    }

    debug_log::log_hook_decision(
        "redirect",
        "Grep",
        Route::Native,
        &format!("{pattern} in {search_path}"),
        "lean-ctx grep produced no output",
    );
    build_dual_allow_output()
}

/// Redirect Glob through lean-ctx in shadow/harden mode (#556).
///
/// Glob differs from Read/Grep: its result is a list of paths matched against
/// the filesystem, not file content, so `build_redirect_output` (which swaps a
/// field to a temp file the host then *reads*) cannot carry it.
///
/// Won't-fix (#1033): a true Read/Grep-style redirect is impossible *by
/// construction*, not merely unimplemented. The host consumes the path list
/// directly and never re-reads a file we could substitute, so there is no
/// redirectable result to rewrite. We therefore only act when shadow or harden
/// mode is active — warm lean-ctx's own glob path (parity with `ctx_glob`) and
/// record the intercept in shadow.log — then allow the native call through
/// unchanged. Outside those modes there is nothing to gain, so we pass through
/// immediately without spawning a subprocess.
fn redirect_glob(tool_input: Option<&serde_json::Value>) -> String {
    let allow = build_dual_allow_output();
    let shadow = is_shadow_mode_active();
    if !shadow && !is_harden_active() {
        return allow;
    }

    let pattern = tool_input
        .and_then(|ti| ti.get("pattern"))
        .and_then(|p| p.as_str())
        .unwrap_or("");
    if pattern.is_empty() {
        debug_log::log_hook_decision(
            "redirect",
            "Glob",
            Route::Native,
            "<none>",
            "no pattern in tool input",
        );
        return allow;
    }

    let search_path = tool_input
        .and_then(|ti| ti.get("path"))
        .and_then(|p| p.as_str())
        .unwrap_or(".");

    tracing::info!(
        "[hook redirect] {} active, warming ctx_glob for {pattern}",
        if shadow { "shadow mode" } else { "harden mode" }
    );

    // Warm lean-ctx's glob path (populates caches, parity with the ctx_glob the
    // shadow header nudges toward); the native result is kept untouched.
    let binary = resolve_binary();
    let _ = run_with_timeout(
        &binary,
        &["glob", pattern, search_path],
        REDIRECT_SUBPROCESS_TIMEOUT,
    );

    debug_log::log_hook_decision(
        "redirect",
        "Glob",
        Route::Native,
        &format!("{pattern} in {search_path}"),
        "shadow/harden warm — native passthrough",
    );
    log_shadow_intercept("Glob", &format!("{pattern} in {search_path}"));
    allow
}

const REDIRECT_SUBPROCESS_TIMEOUT: Duration = Duration::from_secs(10);

/// Run a lean-ctx subprocess with a hard timeout. Returns stdout on success.
/// Kills the child if it exceeds the timeout to prevent orphan processes.
fn run_with_timeout(binary: &str, args: &[&str], timeout: Duration) -> Option<Vec<u8>> {
    let mut child = std::process::Command::new(binary)
        .args(args)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::null())
        .spawn()
        .ok()?;

    let deadline = std::time::Instant::now() + timeout;
    loop {
        match child.try_wait() {
            Ok(Some(status)) if status.success() => {
                let mut stdout = Vec::new();
                if let Some(mut out) = child.stdout.take() {
                    let _ = out.read_to_end(&mut stdout);
                }
                return if stdout.is_empty() {
                    None
                } else {
                    Some(stdout)
                };
            }
            Ok(Some(_)) | Err(_) => return None,
            Ok(None) => {
                if std::time::Instant::now() > deadline {
                    let _ = child.kill();
                    let _ = child.wait();
                    return None;
                }
                std::thread::sleep(Duration::from_millis(10));
            }
        }
    }
}

/// PID-independent marker for re-read detection (#938).
/// Unlike [`redirect_temp_path`] this omits `process::id()` so the marker
/// persists across hook subprocess invocations within the same session.
fn file_mtime_str(path: &str) -> String {
    std::fs::metadata(path)
        .and_then(|m| m.modified())
        .ok()
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| d.as_nanos().to_string())
        .unwrap_or_default()
}

fn redirect_read_marker(path: &str) -> std::path::PathBuf {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();
    path.hash(&mut hasher);
    let hash = hasher.finish();

    let temp_dir = std::env::temp_dir().join("lean-ctx-hook");
    let _ = std::fs::create_dir_all(&temp_dir);
    temp_dir.join(format!("{hash:016x}.read-marker"))
}
fn redirect_temp_path(key: &str) -> std::path::PathBuf {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();
    key.hash(&mut hasher);
    std::process::id().hash(&mut hasher);
    let hash = hasher.finish();

    let temp_dir = std::env::temp_dir().join("lean-ctx-hook");
    let _ = std::fs::create_dir_all(&temp_dir);
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = std::fs::set_permissions(&temp_dir, std::fs::Permissions::from_mode(0o700));
    }
    temp_dir.join(format!("{hash:016x}.lctx"))
}

/// #778: Whether `additionalContext` injection is allowed.
/// Default OFF — prevents prompt-cache invalidation on Anthropic models.
/// Opt-in via `[code_health] inject_context = true` or `LEAN_CTX_INJECT_CONTEXT=1`.
fn inject_context_allowed() -> bool {
    std::env::var("LEAN_CTX_INJECT_CONTEXT").is_ok()
        || crate::core::config::Config::load()
            .code_health
            .inject_context
}

pub(super) fn build_redirect_output(
    tool_input: Option<&serde_json::Value>,
    field: &str,
    temp_path: &str,
    shadow_note: Option<&str>,
) -> String {
    let updated_input = if let Some(obj) = tool_input.and_then(|v| v.as_object()) {
        let mut m = obj.clone();
        m.insert(
            field.to_string(),
            serde_json::Value::String(temp_path.to_string()),
        );
        serde_json::Value::Object(m)
    } else {
        serde_json::json!({ field: temp_path })
    };

    // Claude Code / CodeBuddy hook output format (other hosts ignore it).
    let mut hook_specific = serde_json::json!({
        "hookEventName": "PreToolUse",
        "permissionDecision": "allow",
        "updatedInput": updated_input.clone(),
    });
    // #1019: the shadow nudge travels here, not inside the file content. Hosts
    // that honor it (Claude Code / Codex) surface it as model-visible context;
    // others ignore it. Either way the temp file the host reads stays faithful.
    if let Some(note) = shadow_note {
        hook_specific["additionalContext"] = serde_json::Value::String(note.to_string());
    }

    serde_json::json!({
        // Cursor hook output format.
        "permission": "allow",
        "updated_input": updated_input.clone(),
        // GitHub Copilot CLI preToolUse format: top-level `permissionDecision`
        // + `modifiedArgs` (full substitute args) so the read/grep redirect to
        // the lean-ctx temp file actually takes effect on Copilot (#551).
        "permissionDecision": "allow",
        "modifiedArgs": updated_input.clone(),
        "hookSpecificOutput": hook_specific
    })
    .to_string()
}

const PASSTHROUGH_SUBSTRINGS: &[&str] = &[
    ".cursorrules",
    ".cursor/rules",
    ".cursor/hooks",
    "skill.md",
    "agents.md",
    ".env",
    "hooks.json",
    "node_modules",
];

const PASSTHROUGH_EXTENSIONS: &[&str] = &[
    "lock", "png", "jpg", "jpeg", "gif", "webp", "pdf", "ico", "svg", "woff", "woff2", "ttf", "eot",
];

pub(super) fn should_passthrough(path: &str) -> bool {
    let p = path.to_lowercase();

    if PASSTHROUGH_SUBSTRINGS.iter().any(|s| p.contains(s)) {
        return true;
    }

    std::path::Path::new(&p)
        .extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(|ext| {
            PASSTHROUGH_EXTENSIONS
                .iter()
                .any(|e| ext.eq_ignore_ascii_case(e))
        })
}

/// Fire-and-forget background cache warming via a detached subprocess.
///
/// Spawns `lean-ctx read <path> -m auto` in the background so the daemon's
/// BM25 index and SessionCache are warm when the agent subsequently calls
/// `ctx_read(mode=full)` before editing.  The redirect itself gives the
/// compressed exploration view; this warming ensures the follow-up full
/// read is instant instead of cold.
///
/// Uses the CLI subprocess (not direct UDS) because the daemon's HTTP
/// endpoint requires project context for PathJail.  The subprocess inherits
/// `LEAN_CTX_HOOK_CHILD=1` which prevents daemon auto-start and uses the
/// fast local-only path.  Completely fire-and-forget: stdout/stderr go to
/// /dev/null, the child is not awaited, and all failures are silent.
pub(super) fn warm_daemon_cache(path: &str) {
    use std::process::{Command, Stdio};

    let binary = resolve_binary();
    let _ = Command::new(&binary)
        .args(["read", path, "-m", "auto"])
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn();
}