lean-ctx 3.11.3

LeanCTX Engine: the open-source runtime for the Context Gateway for AI Systems. CLI, MCP server, and supported proxy integrations.
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
//! Command-rewrite decision logic for the `hook rewrite` entry point.
//!
//! Extracted from `hook_handlers::mod` (#660/#966 LOC gate). Search/dir-list
//! rewriting lives in the sibling `search_rewrite` module; this one owns the
//! file-read (cat/head/tail/Get-Content) rewrites, compound-command wrapping,
//! and the `rewrite_candidate` dispatch every rewrite entry point (Cursor,
//! Codex, Copilot, the inline CLI) funnels through.

use super::powershell_rewrite::{self, PowerShellDecision, is_powershell_tool};
use super::search_rewrite::{rewrite_dir_list_command, rewrite_search_command};
use super::{
    HOOK_STDIN_TIMEOUT, build_deny_output, build_dual_allow_output, build_dual_rewrite_output,
    dedup, is_claude_pretooluse_payload, is_disabled, is_shell_tool, payload,
    read_stdin_with_timeout, resolve_binary, shell_quote, shell_tokenize,
};
use crate::compound_lexer;
use crate::core::debug_log::{self, Route};
use crate::rewrite_registry;

/// Decide the rewrite hook's stdout (a rewrite or an allow-passthrough) without
/// printing, so `handle_rewrite` can run it under the fail-open timeout (#1035).
pub(super) fn compute_rewrite() -> String {
    if is_disabled() {
        return build_dual_allow_output();
    }
    // Shadow-only surface: native Shell passes through without rewrite
    if super::is_shadow_surface_active() {
        return build_dual_allow_output();
    }
    let binary = resolve_binary();
    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 rewrite] invalid JSON payload, allowing passthrough");
        return build_dual_allow_output();
    };

    // Resolve across host shapes: Claude/Cursor send snake_case `tool_name` +
    // `tool_input`; Copilot CLI sends camelCase `toolName` + `toolArgs` (a
    // JSON-encoded string). Before #551 only the snake_case path was read.
    let Some(tool_name) = payload::resolve_tool_name(&v) else {
        return build_dual_allow_output();
    };

    if !is_shell_tool(&tool_name) {
        return build_dual_allow_output();
    }

    let tool_args = payload::resolve_tool_args(&v);
    let Some(cmd) = payload::resolve_command(&v, tool_args.as_ref()) else {
        return build_dual_allow_output();
    };

    // #1032: Cursor fires preToolUse twice. Dedup on a PID-independent key (tool +
    // command) so the second fire replays the decision instead of re-logging.
    // The output dialect is part of the key: a replayed Cursor-shaped deny is
    // invalid for Claude Code.
    let claude = is_claude_pretooluse_payload(&v);
    let key_material = format!("{tool_name}\u{0}{cmd}\u{0}{claude}");
    dedup::deduped("rewrite", &key_material, || {
        if is_powershell_tool(&tool_name) {
            return powershell_output(&cmd, &binary, &tool_name, tool_args.as_ref(), &v);
        }
        if let Some(rewritten) = rewrite_candidate(&cmd, &binary) {
            debug_log::log_hook_decision(
                "rewrite",
                &tool_name,
                Route::LeanCtx,
                &cmd,
                "rewritable command",
            );
            build_dual_rewrite_output(tool_args.as_ref(), &rewritten)
        } else if needs_enforcement_wrap(&cmd) {
            // #1408: Commands that bypass compression-routing but violate the
            // shell allowlist must still be wrapped for enforcement. Without
            // this, compound commands (`true && docker --version`) and
            // unconditionally-blocked builtins (`eval ...`) skip the allowlist
            // when the hook passes them through to the native shell.
            debug_log::log_hook_decision(
                "rewrite",
                &tool_name,
                Route::LeanCtx,
                &cmd,
                "enforcement wrap (allowlist violation)",
            );
            build_dual_rewrite_output(tool_args.as_ref(), &wrap_single_command(&cmd, &binary))
        } else {
            debug_log::log_hook_decision(
                "rewrite",
                &tool_name,
                Route::Native,
                &cmd,
                rewrite_skip_reason(&cmd),
            );
            // #1285: native passthrough was structurally invisible — only
            // ctx_* calls reach metering.jsonl, so a session leaking reads
            // through raw Bash showed a clean savings rate. Count every
            // passthrough (token volumes are unknown pre-exec, so zeros) so
            // the dashboard's per-tool table shows the leak as a call count.
            record_native_passthrough();
            build_dual_allow_output()
        }
    })
}

/// The hook output for a host's PowerShell tool (#1848) — see
/// [`super::powershell_rewrite`] for why it never takes the Bash path.
fn powershell_output(
    cmd: &str,
    binary: &str,
    tool_name: &str,
    tool_args: Option<&serde_json::Value>,
    payload: &serde_json::Value,
) -> String {
    match powershell_rewrite::decide(cmd, binary) {
        PowerShellDecision::Rewrite(rewritten) => {
            debug_log::log_hook_decision(
                "rewrite",
                tool_name,
                Route::LeanCtx,
                cmd,
                "rewritable command (PowerShell)",
            );
            build_dual_rewrite_output(tool_args, &rewritten)
        }
        PowerShellDecision::Deny(msg) => {
            debug_log::log_hook_decision(
                "rewrite",
                tool_name,
                Route::Native,
                cmd,
                "denied by the shell allowlist (PowerShell)",
            );
            build_deny_output(&msg, payload)
        }
        PowerShellDecision::Passthrough => {
            debug_log::log_hook_decision(
                "rewrite",
                tool_name,
                Route::Native,
                cmd,
                "not a single read/search/list command (PowerShell)",
            );
            record_native_passthrough();
            build_dual_allow_output()
        }
    }
}

/// Count a native shell passthrough in metering (#1285). Synchronous append:
/// hooks are plain CLI processes without a Tokio reactor, so
/// `append_best_effort` (spawn_blocking) is unavailable here.
fn record_native_passthrough() {
    if let Ok(store) = crate::core::metering::MeterStore::from_data_dir() {
        let _ = store.append(&crate::core::metering::MeterEntry::new(
            crate::core::metering::NATIVE_PASSTHROUGH_TOOL,
            0,
            0,
            0,
        ));
    }
}

/// Human-readable reason a shell command was left to the native tool. Mirrors
/// the `None` branches of [`rewrite_candidate`] so #520's debug log can explain
/// *why* a call fell back to native instead of routing through lean-ctx.
pub(super) fn rewrite_skip_reason(cmd: &str) -> &'static str {
    if cmd.starts_with("lean-ctx ") {
        "already a lean-ctx command"
    } else if cmd.contains("<<") {
        "heredoc cannot be rewritten safely"
    } else if is_compound(cmd) && !crate::core::shell_allowlist::passes_enforced(cmd) {
        "compound pipes/chains into a non-allowlisted or interpreter sink — left raw for the agent shell"
    } else {
        "not a known read/search/list command"
    }
}

pub(super) fn is_rewritable(cmd: &str) -> bool {
    rewrite_registry::is_rewritable_command(cmd)
}

/// #1408: True when a command must be wrapped in `lean-ctx -c` purely for shell
/// allowlist enforcement, even though it was not selected for compression routing.
///
/// Conditions: shell security is active (not `Off`), the command would fail the
/// allowlist, and it can survive the quoting round-trip (no heredocs).
fn needs_enforcement_wrap(cmd: &str) -> bool {
    use crate::core::shell_allowlist::{ShellSecurity, passes_enforced};

    if ShellSecurity::resolve() == ShellSecurity::Off {
        return false;
    }
    if cmd.contains("<<") {
        return false;
    }
    if cmd.starts_with("lean-ctx ") {
        return false;
    }
    !passes_enforced(cmd)
}

/// True when `cmd` carries a top-level shell operator (`&&`, `||`, `;`, `|`),
/// i.e. it is a compound/pipeline rather than a single command. Compounds are
/// handled authoritatively by [`build_rewrite_compound`]; this guards the
/// single-command `is_rewritable` fallback in [`rewrite_candidate`] so a
/// compound the compound-handler declined is never re-wrapped whole.
pub(super) fn is_compound(cmd: &str) -> bool {
    compound_lexer::split_compound(cmd)
        .iter()
        .any(|s| matches!(s, compound_lexer::Segment::Operator(_)))
}

/// Wrap `cmd` in `lean-ctx -c '…'`, quoted for the host's Bash tool.
///
/// Always POSIX: the calling shell is the host's Bash (Git Bash on Windows),
/// not the shell lean-ctx detects for itself. cmd.exe-style double quotes let
/// Bash expand `$vars` and run `$(…)` before lean-ctx sees the command (#1862).
/// PowerShell tools never get here (#1848).
pub(super) fn wrap_single_command(cmd: &str, binary: &str) -> String {
    crate::shell::join_command_for(&[binary.to_owned(), "-c".to_owned(), cmd.to_owned()], "-c")
}

/// Quote-aware check for stdout file redirects (`>`, `>>`).
/// Returns `true` when the command contains an unquoted `>` that targets a
/// real file (not `/dev/null`, not `>&N` fd-duplication, not `2>`).
fn has_stdout_file_redirect(cmd: &str) -> bool {
    let bytes = cmd.as_bytes();
    let len = bytes.len();
    let mut i = 0;
    let mut in_single_quote = false;
    let mut in_double_quote = false;

    while i < len {
        let c = bytes[i];
        if c == b'\\' && !in_single_quote {
            i += 2;
            continue;
        }
        if c == b'\'' && !in_double_quote {
            in_single_quote = !in_single_quote;
        } else if c == b'"' && !in_single_quote {
            in_double_quote = !in_double_quote;
        } else if c == b'>' && !in_single_quote && !in_double_quote {
            // Skip stderr redirect `2>`
            if i > 0 && bytes[i - 1] == b'2' {
                i += 1;
                continue;
            }
            let target_start = if i + 1 < len && bytes[i + 1] == b'>' {
                i + 2 // >>
            } else {
                i + 1 // >
            };
            let target: String = cmd[target_start..]
                .trim_start()
                .chars()
                .take_while(|c| !c.is_whitespace())
                .collect();
            // /dev/null and fd-duplication are not file redirects.
            if target == "/dev/null" || target == "/dev/stdout" || target == "/dev/stderr" {
                i += 1;
                continue;
            }
            if let Some(fd) = target.strip_prefix('&')
                && !fd.is_empty()
                && (fd == "-" || fd.chars().all(|c| c.is_ascii_digit()))
            {
                i += 1;
                continue;
            }
            if !target.is_empty() {
                return true;
            }
        }
        i += 1;
    }
    false
}

pub(super) fn rewrite_candidate(cmd: &str, binary: &str) -> Option<String> {
    if !passes_rewrite_guards(cmd, binary) {
        return None;
    }

    // The direct rewrites splice the binary into a Bash command line as-is: a
    // path with a space (`C:/Users/First Last/…`) must arrive quoted (#1865).
    if let Some(rewritten) = direct_rewrite(cmd, &shell_quote(binary)) {
        return Some(rewritten);
    }

    if let Some(rewritten) = build_rewrite_compound(cmd, binary) {
        return Some(rewritten);
    }

    // Single-command fallback only. A compound that `build_rewrite_compound`
    // declined (tricky pipe/chain sink, or no rewritable segment) must NOT be
    // re-wrapped here: wrapping the whole string in `lean-ctx -c '…'` would newly
    // subject its sink to the allowlist gate and could block a command the
    // agent's shell ran fine before (#589). Compounds are authoritative above.
    if !is_compound(cmd) && is_rewritable(cmd) {
        return Some(wrap_single_command(cmd, binary));
    }

    None
}

/// Commands no rewrite may touch, whatever shell runs them. Shared with the
/// PowerShell path (#1848).
pub(super) fn passes_rewrite_guards(cmd: &str, binary: &str) -> bool {
    if is_leanctx_call(cmd, binary) {
        return false;
    }

    // GH #1420: package manager operations on lean-ctx itself must not be
    // rewritten — wrapping `npm install lean-ctx-bin` in `lean-ctx -c` locks
    // the binary (EBUSY on Windows) and can hang.
    if is_self_install_command(cmd) {
        return false;
    }

    // Package-manager install commands produce interactive progress output
    // and can hang when wrapped. Always pass through.
    if is_package_manager_install(cmd) {
        return false;
    }

    // Heredocs cannot survive the quoting round-trip through `lean-ctx -c '...'`.
    // Newlines get escaped, breaking the heredoc syntax entirely (GitHub #140).
    if cmd.contains("<<") {
        return false;
    }

    // If the command has a LEAN_CTX_DISABLED or LEAN_CTX_NO_HOOK env-prefix,
    // the agent explicitly wants raw execution. Wrapping it in `lean-ctx -c`
    // would bury the flag inside a string literal where is_disabled() can't
    // see it. Skip rewrite entirely. (#1320)
    {
        let stripped = crate::rewrite_registry::strip_env_prefix(cmd);
        if stripped.len() != cmd.len() {
            let prefix_part = &cmd[..cmd.len() - stripped.len()];
            if prefix_part.contains("LEAN_CTX_DISABLED") || prefix_part.contains("LEAN_CTX_NO_HOOK")
            {
                return false;
            }
        }
    }

    // File redirects (`cmd > out`, `cmd >> log`) mean the output is captured
    // as data, not read by the agent. Wrapping in lean-ctx -c would either:
    // (a) compress stdout before the redirect writes it to disk, or
    // (b) add quoting overhead that can break redirect target paths.
    // Let the native shell handle the redirect directly. (#1303)
    !has_stdout_file_redirect(cmd)
}

/// `cmd` already runs lean-ctx: bare, by path, or by a quoted path as our own
/// rewrites emit it (#1865).
pub(super) fn is_leanctx_call(cmd: &str, binary: &str) -> bool {
    cmd.starts_with("lean-ctx ")
        || cmd.starts_with(&format!("{binary} "))
        || cmd.starts_with(&format!("{} ", shell_quote(binary)))
}

/// A single read/search/list command mapped onto the matching `lean-ctx`
/// subcommand (`read`/`grep`/`ls`) — no `-c` wrap, so no shell in between.
pub(super) fn direct_rewrite(cmd: &str, binary: &str) -> Option<String> {
    if has_shell_expansion(cmd) {
        return None;
    }
    rewrite_file_read_command(cmd, binary)
        .or_else(|| rewrite_search_command(cmd, binary))
        .or_else(|| rewrite_dir_list_command(cmd, binary))
}

/// `$` or `` ` `` anywhere in the command, or an unquoted glob/brace
/// metacharacter (`*`, `?`, `[`, `{`). The tokenizer that feeds the direct
/// rewrites drops the quotes, so it cannot tell `"$HOME"` (expand) from
/// `'$HOME'` (literal), and the rewrites re-quote every path — which turns a
/// glob the calling shell would have expanded into a literal, missing path
/// (#1918). Such commands take the `-c` wrap, which hands the original text to
/// a shell unchanged (#1862). Quoted metacharacters (`grep -n 'a.*b' f`) are
/// literal to the shell too, so they keep the direct rewrite.
fn has_shell_expansion(cmd: &str) -> bool {
    cmd.contains(['$', '`']) || has_unquoted_glob(cmd)
}

/// True when `cmd` contains `*`, `?`, `[` or `{` outside single/double quotes
/// and not backslash-escaped — i.e. a word the shell would glob or
/// brace-expand.
fn has_unquoted_glob(cmd: &str) -> bool {
    let mut single = false;
    let mut double = false;
    let mut chars = cmd.chars();
    while let Some(c) = chars.next() {
        match c {
            '\\' if !single => {
                chars.next();
            }
            '\'' if !double => single = !single,
            '"' if !single => double = !double,
            '*' | '?' | '[' | '{' if !single && !double => return true,
            _ => {}
        }
    }
    false
}

/// Rewrites cat/head/tail to lean-ctx read with appropriate arguments.
/// Only rewrites simple single-file reads within the project scope.
pub(super) fn rewrite_file_read_command(cmd: &str, binary: &str) -> Option<String> {
    // Unix file-read commands come from the central registry; PowerShell-native
    // cmdlets (Get-Content/gc) are detected here so they are not added to the POSIX
    // shell-alias/registry surface (#561).
    if !rewrite_registry::is_file_read_command(cmd)
        && !is_powershell_file_read(cmd)
        && !is_sed_or_awk_line_read(cmd)
    {
        return None;
    }

    // Compound commands (pipes, chains) should not be rewritten as file reads.
    if cmd.contains('|') || cmd.contains("&&") || cmd.contains("||") || cmd.contains(';') {
        return None;
    }

    // Shell redirections indicate complex usage — don't rewrite.
    if cmd.contains(">&") || cmd.contains(">>") || cmd.contains(" >") {
        return None;
    }

    let parts = shell_tokenize(cmd);
    if parts.len() < 2 {
        return None;
    }

    match parts[0].as_str() {
        "cat" => {
            // #1279: only a single flag-free path maps faithfully onto `read`.
            // `cat a.md b.md` used to collapse into `read "a.md b.md"` (one
            // bogus path) and `cat -n f` into `read "-n f"`. Decline those —
            // the wrap/passthrough paths own them. A quoted path containing
            // spaces is one token after shell_tokenize, so it still matches.
            if parts.len() != 2 || parts[1].starts_with('-') {
                return None;
            }
            let path = parts[1].as_str();
            if is_outside_project_path(path) {
                return None;
            }
            Some(format!("{binary} read {}", shell_quote(path)))
        }
        "sed" => rewrite_sed_range_print(&parts, binary),
        "awk" => rewrite_awk_line_range(&parts, binary),
        "head" => {
            let refs: Vec<&str> = parts[1..].iter().map(String::as_str).collect();
            // #1537: `head -c N` / `--bytes=N` is a BYTE-precise read (often a
            // deliberate tiny probe of a sensitive file). A line-based rewrite
            // discards that precision and can print far more than asked — a
            // real secret-exposure incident. Byte-mode passes through untouched.
            if has_byte_count_flag(&refs) {
                return None;
            }
            let (n, path) = parse_head_tail_args(&refs);
            let path = path?;
            if is_outside_project_path(path) {
                return None;
            }
            let qp = shell_quote(path);
            match n {
                None => Some(format!("{binary} read {qp} -m lines:1-10")),
                Some(LineCount::Plain(lines)) => {
                    Some(format!("{binary} read {qp} -m lines:1-{lines}"))
                }
                // `head -n -N` (everything but the last N) and `+N` have no
                // `lines:` form; leave them to native head rather than rewrite
                // to a window that reads something else.
                Some(LineCount::Minus(_) | LineCount::Plus(_)) => None,
            }
        }
        "tail" => {
            let refs: Vec<&str> = parts[1..].iter().map(String::as_str).collect();
            // #1537: byte-precise `tail -c N` passes through untouched, same
            // as head — line-based rewrites must not widen a byte-count read.
            if has_byte_count_flag(&refs) {
                return None;
            }
            // `tail -f` / `-F` / `--follow` streams; a static window read can
            // never stand in for it.
            if has_follow_flag(&refs) {
                return None;
            }
            let (n, path) = parse_head_tail_args(&refs);
            let path = path?;
            if is_outside_project_path(path) {
                return None;
            }
            let qp = shell_quote(path);
            match n {
                // `tail -n +N` is "from line N to EOF" — a bare `lines:N`
                // (#1759). `"+N".parse::<usize>()` succeeds, so before the sign
                // was tracked this became `lines:-N`: the LAST N lines, a
                // silently wrong window once `lines:-N` started to parse.
                Some(LineCount::Plus(from)) => Some(format!("{binary} read {qp} -m lines:{from}")),
                Some(LineCount::Plain(lines) | LineCount::Minus(lines)) => {
                    Some(format!("{binary} read {qp} -m lines:-{lines}"))
                }
                None => Some(format!("{binary} read {qp} -m lines:-10")),
            }
        }
        "Get-Content" | "gc" => rewrite_get_content(&parts, binary),
        _ => None,
    }
}

/// True if the command is a PowerShell-native file-read cmdlet (`Get-Content`/`gc`).
fn is_powershell_file_read(cmd: &str) -> bool {
    matches!(cmd.split_whitespace().next(), Some("Get-Content" | "gc"))
}

/// True if the command starts with sed/awk — candidates for the line-range
/// read rewrites (#1279). Detected here rather than in the registry so plain
/// sed/awk stay off the generic rewrite surface.
fn is_sed_or_awk_line_read(cmd: &str) -> bool {
    matches!(cmd.split_whitespace().next(), Some("sed" | "awk"))
}

/// #1279: `sed -n 'N,Mp' file` / `sed -n 'Np' file` are line-range reads —
/// the exact form agent auto-modes recommend for reading files, and until now
/// a silent passthrough that bypassed lean-ctx entirely. Map them to
/// `read -m lines:N-M`; every other sed invocation passes through untouched.
fn rewrite_sed_range_print(parts: &[String], binary: &str) -> Option<String> {
    if parts.len() != 4 || parts[1] != "-n" {
        return None;
    }
    let (start, end) = parse_line_print_expr(&parts[2])?;
    let path = parts[3].as_str();
    if path.starts_with('-') || is_outside_project_path(path) {
        return None;
    }
    Some(format!(
        "{binary} read {} -m lines:{start}-{end}",
        shell_quote(path)
    ))
}

/// Parses a sed print expression `N,Mp` or `Np` (quotes already stripped by
/// shell_tokenize). Returns the inclusive 1-based line range.
fn parse_line_print_expr(expr: &str) -> Option<(u64, u64)> {
    let body = expr.strip_suffix('p')?;
    let (a, b) = match body.split_once(',') {
        Some((a, b)) => (a, b),
        None => (body, body),
    };
    let start: u64 = a.parse().ok()?;
    let end: u64 = b.parse().ok()?;
    (start >= 1 && end >= start).then_some((start, end))
}

/// #1279: `awk 'NR<=N' file` (plus `NR<N`, `NR==N`) are line-limited reads.
/// Anything beyond a bare NR comparison (actions, field refs, `&&`) declines.
fn rewrite_awk_line_range(parts: &[String], binary: &str) -> Option<String> {
    if parts.len() != 3 {
        return None;
    }
    let (start, end) = parse_awk_nr_expr(&parts[1])?;
    let path = parts[2].as_str();
    if path.starts_with('-') || is_outside_project_path(path) {
        return None;
    }
    Some(format!(
        "{binary} read {} -m lines:{start}-{end}",
        shell_quote(path)
    ))
}

/// Parses a bare awk NR comparison (`NR<=N` / `NR<N` / `NR==N`) into an
/// inclusive line range.
fn parse_awk_nr_expr(expr: &str) -> Option<(u64, u64)> {
    let rest = expr.strip_prefix("NR")?;
    if let Some(n) = rest.strip_prefix("<=") {
        let n: u64 = n.parse().ok()?;
        return (n >= 1).then_some((1, n));
    }
    if let Some(n) = rest.strip_prefix("==") {
        let n: u64 = n.parse().ok()?;
        return (n >= 1).then_some((n, n));
    }
    if let Some(n) = rest.strip_prefix('<') {
        let n: u64 = n.parse().ok()?;
        return (n >= 2).then_some((1, n - 1));
    }
    None
}

/// Maps `Get-Content`/`gc` to `lean-ctx read`, honoring `-Path`/`-LiteralPath`, the
/// positional path, `-TotalCount`/`-Head`/`-First` (first N lines) and `-Tail`/`-Last`
/// (last N lines). PowerShell parameter names are case-insensitive. Any other flag, a
/// missing path, multiple files, or both head+tail makes it pass through (conservative,
/// mirroring the Unix cat/head/tail handling).
fn rewrite_get_content(parts: &[String], binary: &str) -> Option<String> {
    let mut path: Option<String> = None;
    let mut head_n: Option<u64> = None;
    let mut tail_n: Option<u64> = None;
    let mut i = 1;
    while i < parts.len() {
        if let Some(flag) = parts[i].strip_prefix('-') {
            let value = parts.get(i + 1);
            match flag.to_ascii_lowercase().as_str() {
                "path" | "literalpath" => path = Some(value?.clone()),
                "totalcount" | "head" | "first" => head_n = Some(value?.parse().ok()?),
                "tail" | "last" => tail_n = Some(value?.parse().ok()?),
                _ => return None,
            }
            i += 2;
        } else if path.is_none() {
            path = Some(parts[i].clone());
            i += 1;
        } else {
            return None;
        }
    }
    let path = path?;
    if is_outside_project_path(&path) || (head_n.is_some() && tail_n.is_some()) {
        return None;
    }
    let qp = shell_quote(&path);
    match (head_n, tail_n) {
        (Some(n), None) => Some(format!("{binary} read {qp} -m lines:1-{n}")),
        (None, Some(n)) => Some(format!("{binary} read {qp} -m lines:-{n}")),
        _ => Some(format!("{binary} read {qp}")),
    }
}

/// Returns true if the path clearly points outside the current project.
/// Paths starting with `~`, `$`, well-known system/config locations, and any
/// path the jailed `lean-ctx read` would refuse should not be intercepted.
pub(super) fn is_outside_project_path(path: &str) -> bool {
    let trimmed = path.trim();

    // Home-relative paths are always outside the project
    if trimmed.starts_with('~') {
        return true;
    }

    // Environment variable expansion — too complex, pass through
    if trimmed.starts_with('$') {
        return true;
    }

    // /proc, /sys, /dev, /tmp, /var — system paths
    if trimmed.starts_with("/proc/")
        || trimmed.starts_with("/sys/")
        || trimmed.starts_with("/dev/")
        || trimmed.starts_with("/tmp/")
        || trimmed.starts_with("/var/")
    {
        return true;
    }

    // Absolute paths: only pass through if they clearly point outside.
    // We can't know the project root here (hooks are stateless), but we can
    // detect common external patterns.
    if trimmed.starts_with('/') {
        // Home directory paths (e.g. /Users/*/Library, /home/*/.config)
        if trimmed.contains("/Library/") || trimmed.contains("/.config/") {
            return true;
        }
        // lean-ctx's own data directories
        if trimmed.contains("/.lean-ctx/") || trimmed.contains("/lean-ctx/logs/") {
            return true;
        }
    }

    // #1903: `lean-ctx read` enforces the PathJail, so anything it would refuse
    // (`../../etc/hosts`, a sibling checkout, a broad-root CWD) stays native.
    crate::cli::cli_read_is_refused(trimmed)
}

/// #1537: whether a head/tail invocation uses byte-count semantics (`-c N`,
/// attached/clustered `-cN`, `--bytes N`, `--bytes=N`) — those must never be
/// rewritten to a line-based read. Conservatively preserve any short-option
/// cluster containing `c`; passing an invalid invocation through is safer than
/// widening a byte read into a line read.
pub(super) fn has_byte_count_flag(args: &[&str]) -> bool {
    args.iter().any(|a| {
        *a == "-c"
            || *a == "--bytes"
            || a.starts_with("--bytes=")
            || (a.starts_with('-') && !a.starts_with("--") && a[1..].contains('c'))
    })
}

/// The line count a `head`/`tail` invocation asked for, with its sign kept:
/// the same digits mean different windows depending on the sign and the
/// command (#1759).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum LineCount {
    /// `-N`, `-n N`, `-nN`: the first (head) / last (tail) N lines.
    Plain(usize),
    /// `-n -N`: for tail the last N lines (GNU spelling); for head
    /// "everything but the last N", which has no `lines:` equivalent.
    Minus(usize),
    /// `-n +N` / `+N` (tail): from line N to the end of the file.
    Plus(usize),
}

/// Parse one count token (`20`, `-20`, `+20`) into a signed [`LineCount`].
fn parse_line_count(raw: &str) -> Option<LineCount> {
    let raw = raw.trim();
    if let Some(from) = raw.strip_prefix('+') {
        return from.parse().ok().map(LineCount::Plus);
    }
    if let Some(last) = raw.strip_prefix('-') {
        return last.parse().ok().map(LineCount::Minus);
    }
    raw.parse().ok().map(LineCount::Plain)
}

/// True for `tail -f`, `-F`, `--follow[=…]` and a short-flag cluster carrying
/// `f`/`F` (`-nf`, `-fn 20`): the invocation streams, so it is never a
/// candidate for a static window read.
fn has_follow_flag(args: &[&str]) -> bool {
    args.iter().any(|arg| {
        if let Some(long) = arg.strip_prefix("--") {
            return long == "follow" || long.starts_with("follow=");
        }
        match arg.strip_prefix('-') {
            Some(cluster) if !cluster.is_empty() && !cluster.starts_with('-') => {
                // A numeric count (`-20`) is not a flag cluster.
                cluster.parse::<usize>().is_err() && cluster.contains(['f', 'F'])
            }
            _ => false,
        }
    })
}

pub(super) fn parse_head_tail_args<'a>(args: &[&'a str]) -> (Option<LineCount>, Option<&'a str>) {
    let mut n: Option<LineCount> = None;
    let mut path: Option<&str> = None;

    let mut i = 0;
    while i < args.len() {
        if args[i] == "-n" && i + 1 < args.len() {
            n = parse_line_count(args[i + 1]);
            i += 2;
        } else if let Some(num) = args[i].strip_prefix("-n") {
            n = parse_line_count(num);
            i += 1;
        } else if let Some(from) = args[i].strip_prefix('+') {
            // Legacy `tail +N file` (POSIX obsolescent, still accepted by GNU).
            if let Ok(from) = from.parse::<usize>() {
                n = Some(LineCount::Plus(from));
            }
            i += 1;
        } else if args[i].starts_with('-') && args[i].len() > 1 {
            if let Ok(num) = args[i][1..].parse::<usize>() {
                n = Some(LineCount::Plain(num));
            }
            i += 1;
        } else {
            path = Some(args[i]);
            i += 1;
        }
    }

    (n, path)
}

/// Rewrites a compound/pipeline (`a | b`, `a && b`, `a; b`, …) by wrapping the
/// WHOLE string in a single `lean-ctx -c "…"` — but only when it would pass the
/// allowlist gate. Otherwise it declines (`None`) and the command is left to the
/// agent's shell unchanged.
///
/// Why wrap-whole (not per-segment, the previous behavior): `lean-ctx -c` runs
/// the command in a profile-free POSIX shell and compresses only the FINAL
/// output, so `|`, `&&`, `||`, `;` all work natively inside it. The old
/// per-segment split left the operators in the OUTER (hooked) shell, which broke
/// two real cases (#589, idea by @getappz):
///   1. Aliased builtins (`head`, `tail`, …) resolve to an undefined `_lc`
///      helper in non-interactive git-bash → `_lc: command not found` on Windows.
///   2. The LEFT side of a pipe got compressed, so the downstream command read
///      the lean-ctx digest instead of the raw bytes it expected.
///
/// Why gate-clean only (compat-first, no new block, no bypass): wrapping subjects
/// every segment — including the pipe sink — to the allowlist. For gate-clean
/// compounds (`git log | head`, `cargo test && npm run lint`) that is exactly
/// right (compressed + fully gated). For a compound whose sink is an
/// interpreter-eval (`python3 -c …`) or a non-allowlisted tool, wrapping would
/// NEWLY block a command the agent's shell ran fine before. We decline instead
/// and leave it raw, so the user's own shell-security config keeps governing it
/// — the pre-existing behavior, with no agent-reachable raw/no-gate path opened.
pub(super) fn build_rewrite_compound(cmd: &str, binary: &str) -> Option<String> {
    let segments = compound_lexer::split_compound(cmd);
    let commands: Vec<&str> = segments
        .iter()
        .filter_map(|s| match s {
            compound_lexer::Segment::Command(c) => Some(c.trim()),
            compound_lexer::Segment::Operator(_) => None,
        })
        .collect();

    // No top-level operator → single command; the caller's wrap_single_command
    // fallback owns it.
    if segments.len() == commands.len() {
        return None;
    }

    // A segment is already a lean-ctx call → don't nest `-c "… lean-ctx -c …"`.
    if commands.iter().any(|c| is_leanctx_call(c, binary)) {
        return None;
    }

    // Wrap-whole when a registry-rewritable segment exists AND the compound
    // passes the allowlist gate (compression), OR when security is active and
    // the compound violates the allowlist (#1408: enforcement). The #589 "no
    // new block" concern only applies when the user has shell_security = off —
    // in that case lean-ctx -c won't enforce anyway.
    if commands.iter().any(|c| is_rewritable(c))
        && (crate::core::shell_allowlist::passes_enforced(cmd) || needs_enforcement_wrap(cmd))
    {
        return Some(wrap_single_command(cmd, binary));
    }

    // #1279: segment-wise fallback. `echo hi && cat f` used to escape whole —
    // wrap-whole declined (lead segment not allowlist-relevant) and the
    // rewritable tail stayed native. For `&&`/`||`/`;` chains without pipes,
    // rewrite each directly-mappable file-read segment in place and keep the
    // rest native.
    rewrite_segments_in_place(&segments, &shell_quote(binary))
}

/// Rewrites file-read segments of a `&&`/`||`/`;` chain in place (#1279).
/// Declines whole when the chain contains a pipe: a rewritten left side would
/// feed different bytes into the consumer on the right.
pub(super) fn rewrite_segments_in_place(
    segments: &[compound_lexer::Segment],
    binary: &str,
) -> Option<String> {
    use compound_lexer::Segment;
    if segments
        .iter()
        .any(|s| matches!(s, Segment::Operator(op) if op == "|"))
    {
        return None;
    }
    let mut out = String::new();
    let mut rewrote = false;
    for seg in segments {
        match seg {
            Segment::Operator(op) => {
                out.push(' ');
                out.push_str(op);
                out.push(' ');
            }
            Segment::Command(c) => {
                let c = c.trim();
                if let Some(r) = (!has_shell_expansion(c))
                    .then(|| rewrite_file_read_command(c, binary))
                    .flatten()
                {
                    out.push_str(&r);
                    rewrote = true;
                } else {
                    out.push_str(c);
                }
            }
        }
    }
    rewrote.then_some(out)
}

/// Package-manager install/add/remove commands produce interactive progress
/// output that is not useful to compress, and wrapping them in `lean-ctx -c`
/// can cause hangs when the daemon is unhealthy. Always pass through.
pub(super) fn is_package_manager_install(cmd: &str) -> bool {
    let tokens: Vec<&str> = cmd.split_whitespace().collect();
    let base = tokens
        .first()
        .map(|t| t.rsplit('/').next().unwrap_or(t))
        .unwrap_or("");
    let is_pm = matches!(
        base,
        "npm"
            | "npx"
            | "yarn"
            | "pnpm"
            | "bun"
            | "pip"
            | "pip3"
            | "cargo"
            | "brew"
            | "apt"
            | "apt-get"
            | "dnf"
            | "pacman"
            | "winget"
            | "choco"
            | "scoop"
    );
    if !is_pm {
        return false;
    }
    let sub = tokens.get(1).copied().unwrap_or("");
    matches!(
        sub,
        "install"
            | "i"
            | "add"
            | "remove"
            | "uninstall"
            | "rm"
            | "update"
            | "upgrade"
            | "ci"
            | "create"
    )
}

/// GH #1420: detect package-manager commands targeting the lean-ctx package.
/// Rewriting these through `lean-ctx -c` locks the binary and prevents
/// npm/cargo from replacing it (EBUSY on Windows, hang on all platforms).
fn is_self_install_command(cmd: &str) -> bool {
    let tokens: Vec<&str> = cmd.split_whitespace().collect();
    let has_lean_ctx_pkg = tokens.iter().any(|t| t.contains("lean-ctx"));
    if !has_lean_ctx_pkg {
        return false;
    }
    let base = tokens
        .first()
        .map(|t| t.rsplit('/').next().unwrap_or(t))
        .unwrap_or("");
    matches!(
        base,
        "npm"
            | "npx"
            | "yarn"
            | "pnpm"
            | "bun"
            | "pip"
            | "pip3"
            | "cargo"
            | "winget"
            | "choco"
            | "scoop"
            | "brew"
            | "apt"
            | "apt-get"
            | "dnf"
            | "pacman"
    )
}

#[cfg(test)]
mod tests {
    use super::rewrite_candidate;

    #[test]
    fn disabled_prefix_skips_rewrite() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("LEAN_CTX_DISABLED=1 cargo test --lib", binary).is_none(),
            "LEAN_CTX_DISABLED prefix must skip rewrite"
        );
    }

    #[test]
    fn no_hook_prefix_skips_rewrite() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("LEAN_CTX_NO_HOOK=1 cargo test --lib", binary).is_none(),
            "LEAN_CTX_NO_HOOK prefix must skip rewrite"
        );
    }

    #[test]
    fn disabled_with_multiple_env_vars_skips_rewrite() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("FOO=bar LEAN_CTX_DISABLED=1 cargo test --lib", binary).is_none(),
            "LEAN_CTX_DISABLED anywhere in env prefix must skip rewrite"
        );
    }

    #[test]
    fn normal_env_prefix_still_rewrites() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("FOO=bar cargo test --lib", binary).is_some(),
            "Non-disable env prefix must still rewrite"
        );
    }

    #[test]
    fn no_prefix_still_rewrites() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("cargo test --lib", binary).is_some(),
            "Command without env prefix must still rewrite"
        );
    }

    #[test]
    fn npm_install_lean_ctx_not_rewritten() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("npm install -g lean-ctx-bin", binary).is_none(),
            "npm install of lean-ctx-bin must skip rewrite (#1420)"
        );
    }

    #[test]
    fn npm_uninstall_lean_ctx_not_rewritten() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("npm uninstall -g lean-ctx-bin", binary).is_none(),
            "npm uninstall of lean-ctx-bin must skip rewrite (#1420)"
        );
    }

    #[test]
    fn cargo_install_lean_ctx_not_rewritten() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("cargo install lean-ctx", binary).is_none(),
            "cargo install lean-ctx must skip rewrite (#1420)"
        );
    }

    #[test]
    fn npm_install_other_package_not_rewritten() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("npm install express", binary).is_none(),
            "npm install must skip rewrite (package-manager passthrough)"
        );
    }

    #[test]
    fn package_manager_install_detected() {
        assert!(super::is_package_manager_install("npm install"));
        assert!(super::is_package_manager_install("npm i express"));
        assert!(super::is_package_manager_install("yarn add lodash"));
        assert!(super::is_package_manager_install("pnpm install"));
        assert!(super::is_package_manager_install("bun install"));
        assert!(super::is_package_manager_install("pip install requests"));
        assert!(super::is_package_manager_install("cargo install serde"));
        assert!(super::is_package_manager_install("npm ci"));
        assert!(super::is_package_manager_install("npm uninstall express"));
        assert!(super::is_package_manager_install("npm create vite@latest"));
    }

    #[test]
    fn package_manager_non_install_not_detected() {
        assert!(!super::is_package_manager_install("npm run build"));
        assert!(!super::is_package_manager_install("npm test"));
        assert!(!super::is_package_manager_install("npm start"));
        assert!(!super::is_package_manager_install("yarn dev"));
        assert!(!super::is_package_manager_install("cargo build"));
        assert!(!super::is_package_manager_install("cargo test"));
        assert!(!super::is_package_manager_install("git status"));
    }

    #[test]
    fn lean_ctx_command_not_rewritten() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("lean-ctx ls src/", binary).is_none(),
            "lean-ctx commands must not be rewritten"
        );
    }

    // --- #1303: File redirect detection ---

    #[test]
    fn redirect_to_file_skips_rewrite() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("git show HEAD:README.md > /tmp/out.md", binary).is_none(),
            "stdout redirect to file must skip rewrite"
        );
    }

    #[test]
    fn append_redirect_skips_rewrite() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("echo hello >> /tmp/log.txt", binary).is_none(),
            "append redirect must skip rewrite"
        );
    }

    #[test]
    fn dev_null_redirect_still_rewrites() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("cargo test 2>/dev/null", binary).is_some(),
            "/dev/null redirect must still rewrite"
        );
    }

    #[test]
    fn stderr_redirect_still_rewrites() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("cargo test 2> /tmp/err.log", binary).is_some(),
            "stderr-only redirect must still rewrite"
        );
    }

    #[test]
    fn fd_dup_still_rewrites() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("cargo test 2>&1", binary).is_some(),
            "fd duplication (2>&1) must still rewrite"
        );
    }

    #[test]
    fn quoted_redirect_not_detected() {
        let binary = "/Users/test/.local/bin/lean-ctx";
        assert!(
            rewrite_candidate("echo 'output > file.txt' | grep output", binary).is_some(),
            "redirect inside quotes must not trigger skip"
        );
    }

    // --- has_stdout_file_redirect unit tests ---

    #[test]
    fn redirect_detection_basic() {
        use super::has_stdout_file_redirect;
        assert!(has_stdout_file_redirect("git status > files.txt"));
        assert!(has_stdout_file_redirect("git diff >> changes.log"));
        assert!(!has_stdout_file_redirect("git status"));
        assert!(!has_stdout_file_redirect("git status 2>/dev/null"));
        assert!(!has_stdout_file_redirect("git status > /dev/null"));
        assert!(!has_stdout_file_redirect("git status 2>&1"));
        assert!(!has_stdout_file_redirect("echo 'a > b'"));
        assert!(!has_stdout_file_redirect("echo \"a > b\""));
    }
}