remem-ai 0.6.42

Local-first coding agent memory for Claude Code and OpenAI Codex
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
use super::*;

#[test]
fn git_rules_accept_path_qualified_executables() {
    let force_artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "/usr/bin/git push --force",
        "command /opt/homebrew/bin/git push --mirror origin",
        r"C:\\Program\ Files\\Git\\cmd\\git.exe push --force",
    ] {
        let outcome = evaluate_artifact(
            &force_artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert_eq!(outcome.matches.len(), 1, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }

    let trailer_artifact = CompiledRulesArtifact::new(99, vec![forbidden_trailer_rule()]);
    let outcome = evaluate_artifact(
        &trailer_artifact,
        &EvaluationInput {
            command: "/usr/local/bin/git commit --trailer 'AI-generated-by: Codex' -m safe"
                .to_string(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block);
    assert_eq!(outcome.matches.len(), 1);
    assert!(outcome.diagnostics.is_empty());
}

#[test]
fn force_push_rule_models_mirror_abbreviations_and_boolean_negation() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "git push --m origin",
        "git push --mi origin",
        "git push --mirror --no-mirror --force origin main",
        "git push --force --no-force --mirror origin",
        "git push --no-force -f origin main",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert_eq!(outcome.matches.len(), 1, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "git push --force --no-force origin main",
        "git push --mirror --no-mirror origin main",
        "git push --mi --no-m origin main",
        "git push -f --no-force origin main",
        "git push --{force,no-force} origin main",
        "git push --delete origin +main",
        "git push -d origin +main",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.matches.is_empty(), "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn force_push_rule_skips_only_statically_unreachable_boolean_branches() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "false && git push --force",
        "true || git push --force",
        "! true && git push --force",
        "! false || git push --force",
        "unknown && false && git push --force",
        "unknown || true || git push --force",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.matches.is_empty(), "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "true && git push --force",
        "false || git push --force",
        "unknown && git push --force",
        "unknown || git push --force",
        "true() { false; }; true || git push --force",
        "true >/definitely/missing/path || git push --force",
        "readonly FLAG; FLAG=x true || git push --force",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert_eq!(outcome.matches.len(), 1, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn function_state_obeys_unset_and_child_shell_scopes() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "f() { git push --force; }; unset -f f; f",
        "f() { git push --force; }; true && unset -f f; f",
        "f() { git push --force; }; false || unset -f f; f",
        "(f() { git push --force; }); f",
        "echo $(f() { git push --force; }); f",
        "git() { :; }; git push --force",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.matches.is_empty(), "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "f() { git push --force; }; (unset -f f); f",
        "f() { git push --force; }; echo $(unset -f f); f",
        "(f() { git push --force; }; f)",
        "f() { git push --force; }; unknown && unset -f f; f",
        "f() { git push --force; }; unknown && f() { :; }; f",
        "g() { git push --force; }; unknown && f() { unset -f g; }; f; g",
        "g() { git push --force; }; unknown && unset -f g; g",
        "f() { git push --force; }; unset -f f | cat; f",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert_eq!(outcome.matches.len(), 1, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn exported_functions_are_available_only_to_child_bash() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "f() { git push --force; }; export -f f; bash -c f",
        "f() { git push --force; }; export -f f; bash <<<'f'",
        "f() { git push --force; }; export -f f; unknown && export -nf f; bash -c f",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "f() { git push --force; }; bash -c f",
        "f() { git push --force; }; export -f f; export -nf f; bash -c f",
        "f() { git push --force; }; export -f f; sh -c f",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn nested_static_execution_receivers_preserve_shell_semantics() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "env -v git push --force",
        "env -C/tmp git push --force",
        "builtin eval 'git push --force'",
        "trap 'git push --force' EXIT",
        "trap 'git push --force' 0",
        "source /dev/stdin <<'EOF'\ngit push --force\nEOF",
        ". /dev/stdin <<'EOF'\ngit push --force\nEOF",
        "bash +n -c 'git push --force'",
        "bash -s -- arg <<'EOF'\ngit push --force\nEOF",
        "bash -c sh <<'EOF'\ngit push --force\nEOF",
        "env --default-signal=PIPE git push --force",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "bash -n -c 'git push --force'",
        "bash -nc 'git push --force'",
        "bash -o noexec -c 'git push --force'",
        "bash -n +n -n -c 'git push --force'",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn static_if_conditions_prune_only_unreachable_bodies() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "if false; then git push --force; fi",
        "if true; then :; else git push --force; fi",
        "if false; then git push --force; elif true; then :; fi",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "if true; then git push --force; fi",
        "if false; then :; else git push --force; fi",
        "if false; then :; elif true; then git push --force; fi",
        "if unknown; then git push --force; fi",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn shell_stdin_uses_the_effective_final_fd_zero_redirect() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "sh <<'EOF' </dev/null\ngit push --force\nEOF",
        "sh <<< 'git push --force' </dev/null",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.matches.is_empty(), "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "sh </dev/null <<'EOF'\ngit push --force\nEOF",
        "sh 3<<'EOF' <&3\ngit push --force\nEOF",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert_eq!(outcome.matches.len(), 1, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn env_split_string_preserves_argv_instead_of_shell_syntax() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "env -S 'echo safe; git push --force'",
        "env -S 'printf \"%s\" \"git push --force\"'",
        "env -S 'echo # git push --force'",
        "env FOO=1 -S 'git push --force'",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.matches.is_empty(), "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
    for command in [
        "env -S 'git push --force'",
        "env -S '-i git push --force'",
        "env -S 'sh -c \"git push --force\"'",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert_eq!(outcome.matches.len(), 1, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn single_word_brace_expansion_respects_the_materialization_bound() {
    let alternatives = (0..=300)
        .map(|value| format!("item{value}"))
        .collect::<Vec<_>>()
        .join(",");
    let segments = shell_command_segments(&format!("echo {{{alternatives}}}"))
        .expect("large static brace word should remain evaluable");
    assert!(
        segments.len() <= 2,
        "bounded word must not materialize every alternative: {}",
        segments.len()
    );
    let cartesian = shell_command_segments("echo {1..256} {left,right}")
        .expect("capped Cartesian word should remain evaluable");
    assert!(
        cartesian.len() <= 256,
        "bounded Cartesian summary must remain capped: {}",
        cartesian.len()
    );

    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: format!("git push --{{force,{alternatives}}}"),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block);
    assert_eq!(outcome.matches.len(), 1);
    assert!(outcome.diagnostics.is_empty());

    let shell_alternatives = ["bash.exe".to_string()]
        .into_iter()
        .chain((0..=300).map(|value| format!("item{value}.exe")))
        .collect::<Vec<_>>()
        .join(",");
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: format!("{{{shell_alternatives}}} -c 'git push --force'"),
        },
    );
    // Brace expansion materializes every member as argv. Here `item0.exe` is
    // the shell's script operand, so the later `-c` string is not executed.
    assert_eq!(outcome.verdict, EvaluationVerdict::Allow);
    assert!(outcome.matches.is_empty());
    assert!(outcome.diagnostics.is_empty());

    let semantic_alternatives = ["-vf".to_string(), "--mi".to_string()]
        .into_iter()
        .chain((0..=300).map(|value| format!("branch{value}")))
        .collect::<Vec<_>>()
        .join(",");
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: format!("git push {{{semantic_alternatives}}}"),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block);
    assert_eq!(outcome.matches.len(), 1);
    assert!(outcome.diagnostics.is_empty());

    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: format!("git push -{{vf,{alternatives}}}"),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block);
    assert_eq!(outcome.matches.len(), 1);
    assert!(outcome.diagnostics.is_empty());
}

#[test]
fn force_push_rule_tracks_static_shell_state_and_indirect_execution() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "f(){ git push --force;}; declare -fx f; bash -c f",
        "source /dev/fd/0 <<'EOF'\ngit push --force\nEOF",
        "source /proc/self/fd/0 <<'EOF'\ngit push --force\nEOF",
        "builtin builtin eval 'git push --force'",
        "GIT=git env -S '${GIT} push --force'",
        "shopt -s expand_aliases\nalias gp='git push --force'\ngp",
        "git -c alias.pf='push --force' pf",
        "git -c alias.pf='!git push' pf --force",
        "git push -od origin +HEAD:main",
        "shopt -s lastpipe; printf x | { f(){ git push --force;};}; f",
        "git(){ command git \"$@\";}; git push --force",
        "trap 'git push --force' EXIT; case ok in o*) :;; ok) trap - EXIT;; esac",
        "case ok in o*) : ;& nope) git push --force;; esac",
        "f(){ command git \"$@\";}; f push --force",
        "git(){ command git \"$1\" \"$2\";}; git push --force",
        "f(){ command git $@;}; f 'push --force'",
        "f(){ command git $1;}; f 'push --force'",
        "f(){ command git \"${1:-push}\" \"${2:---force}\";}; f",
        "f(){ command \"g$1\" push --force;}; f it",
        "f(){ command \"g${1:-it}\" push --force;}; f",
        "f(){ command git \"${1:-$2}\" \"${3:---force}\";}; f '' push",
        "f(){ command \"${1:-${2:-git}}\" push --force;}; f",
        "git -c $'alias.pf=push\n--force' pf",
        "git -c 'alias.pf=push # --force' pf",
        "bash -c 'sh 3<&0 <&3' <<'EOF'\ngit push --force\nEOF",
        "trap 'git push --force' EXIT; trap -p EXIT",
        "trap 'git push --force' EXIT; exit; trap - EXIT",
        "shopt -s nocasematch; case OK in ok) git push --force;; esac",
        "f(){ git push --force;}; typeset -fx f; bash -c f",
        "shopt -s lastpipe; unknown && set -m; printf x | { f(){ git push --force;};}; f",
        "shopt -s lastpipe; set -m; unknown && set +m; printf x | { f(){ git push --force;};}; f",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert_eq!(outcome.matches.len(), 1, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn force_push_rule_prunes_static_non_execution_and_honors_overrides() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "while false; do git push --force; done",
        "until true; do git push --force; done",
        "case ok in nope) git push --force;; esac",
        "trap 'git push --force' EXIT; trap - EXIT",
        "f(){ git push --force;}; declare -f f; bash -c f",
        "printf x | { f(){ git push --force;};}; f",
        "bash -c 'sh </dev/null' <<'EOF'\ngit push --force\nEOF",
        "f(){ printf '%s\\n' \"$@\";}; f git push --force",
        "f(){ printf '%s\\n' $@;}; f 'git push --force'",
        "f(){ command git $10 --force;}; f x a b c d e f g h push",
        "f(){ command \"g$1\" push --force;}; f 'it extra'",
        "f(){ command git \"${1:-$2}\" \"${3:---force}\";}; f '' 'push extra'",
        "f(){ command \"${1:-${2:-git}}\" push --force;}; f echo",
        "git -c alias.pf='echo safe; push --force' pf",
        "shopt -s expand_aliases\nalias gp='git push --force'; gp",
        "shopt -s lastpipe; set -m; printf x | { f(){ git push --force;};}; f",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.to_string(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.matches.is_empty(), "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }

    let alternatives = std::iter::once("--force".to_string())
        .chain((0..=300).map(|value| format!("branch{value}")))
        .chain(std::iter::once("--no-force".to_string()))
        .collect::<Vec<_>>()
        .join(",");
    let command = format!("git push {{{alternatives}}}");
    let outcome = evaluate_artifact(&artifact, &EvaluationInput { command });
    assert_eq!(outcome.verdict, EvaluationVerdict::Allow);
    assert!(outcome.matches.is_empty());
    assert!(outcome.diagnostics.is_empty());

    let reverse = std::iter::once("--no-force".to_string())
        .chain((0..=300).map(|value| format!("branch{value}")))
        .chain(std::iter::once("--force".to_string()))
        .collect::<Vec<_>>()
        .join(",");
    let command = format!("git push {{{reverse}}}");
    let outcome = evaluate_artifact(&artifact, &EvaluationInput { command });
    assert_eq!(outcome.verdict, EvaluationVerdict::Block);
    assert_eq!(outcome.matches.len(), 1);
    assert!(outcome.diagnostics.is_empty());

    let alternating = (0..=300)
        .map(|index| {
            if index % 2 == 0 {
                "--force"
            } else {
                "--no-force"
            }
        })
        .collect::<Vec<_>>()
        .join(",");
    let command = format!("git push {{{alternating}}}");
    let outcome = evaluate_artifact(&artifact, &EvaluationInput { command });
    assert_eq!(outcome.verdict, EvaluationVerdict::Block);
    assert_eq!(outcome.matches.len(), 1);
    assert!(outcome.diagnostics.is_empty());
}

#[test]
fn force_push_rule_recognizes_exe_shell_basenames() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "bash.exe -c 'git push --force'",
        "/usr/bin/bash.exe -c 'git push --force'",
        r#""C:\Program Files\Git\bin\bash.exe" -c 'git push --force'"#,
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }

    let command = "notbash.exe -c 'git push --force'";
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
    assert!(outcome.matches.is_empty(), "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}

#[test]
fn force_push_rule_binds_shell_command_positional_parameters() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        "bash -c 'git push \"$1\"' _ --force",
        "bash -c '\"$0\" push --force' git",
        "bash -c '$1' _ 'git push --force'",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }

    for command in [
        "bash -c 'git push \"$1\"' _ origin",
        "bash -c 'git push \"$1\"' _",
        "bash -c '\"$1\"' _ 'git push --force'",
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
        assert!(outcome.matches.is_empty(), "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }
}

#[test]
fn force_push_rule_preserves_missing_shell_zero() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    let command = "bash -c '${0:-git} push --force'";
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
    assert!(outcome.matches.is_empty(), "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");

    let command = "bash -c '$1' _ 'git push --force'";
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}

#[test]
fn force_push_rule_keeps_function_positional_scope_inside_shell_command() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    let command = "bash -c 'f(){ git push \"$1\"; }; f origin' _ --force";
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
    assert!(outcome.matches.is_empty(), "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");

    let command = "bash -c 'f(){ git push \"$1\"; }; f --force' _ origin";
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}

#[test]
fn force_push_rule_keeps_shell_positionals_for_exit_traps() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    let command = r#"bash -c 'trap '\''git push "$1"'\'' EXIT' _ --force"#;
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}

#[test]
fn force_push_rule_expands_shell_positionals_in_heredoc_handoff() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    for command in [
        r#"bash -c 'sh <<EOF
git push "$1"
EOF' _ --force"#,
        r#"bash -c 'sh <<EOF
git push '\''$1'\''
EOF' _ --force"#,
        r#"bash -c 'sh <<EOF
${1:-"git push --force"}
EOF' _"#,
    ] {
        let outcome = evaluate_artifact(
            &artifact,
            &EvaluationInput {
                command: command.into(),
            },
        );
        assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
        assert!(outcome.diagnostics.is_empty(), "{command}");
    }

    let command = r#"bash -c 'sh <<'\''EOF'\''
git push "$1"
EOF' _ --force"#;
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
    assert!(outcome.matches.is_empty(), "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");

    let command = "bash -c 'sh <<EOF\n$1\nEOF' _ 'git push --force; :'";
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}

#[test]
fn force_push_rule_keeps_nested_command_substitution_quotes_independent() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    let command = r#"bash -c 'echo "$( : '\''$1'\'' )"' _ "x' ; git push --force; : '""#;
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Allow, "{command}");
    assert!(outcome.matches.is_empty(), "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}

#[test]
fn force_push_rule_does_not_expand_function_definition_names() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    let command = r#"bash -c '$1(){ :; }; git push --force' _ git"#;
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}

#[test]
fn force_push_rule_expands_shell_positionals_as_arithmetic_source() {
    let artifact = CompiledRulesArtifact::new(99, vec![forbidden_force_push_rule()]);
    let command = r#"bash -c '(( $1 ))' _ 'x[$(git push --force)]'"#;
    let outcome = evaluate_artifact(
        &artifact,
        &EvaluationInput {
            command: command.into(),
        },
    );
    assert_eq!(outcome.verdict, EvaluationVerdict::Block, "{command}");
    assert!(outcome.diagnostics.is_empty(), "{command}");
}