mati 0.1.2

An enforcement layer for codebase knowledge: confirmed gotchas gate what AI agents read and edit at the hook level. Not a passive memory store.
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
/// M-15 Bash Hook Compliance Test Suite — wrapper execution + lifecycle contracts.
///
/// This module tests two things:
///
/// 1. **Wrapper execution** (Category 1) — the thin shell wrappers (`pre_read`,
///    `pre_bash`, `codex_pre_bash`, `codex_post_bash`) correctly delegate to
///    `mati hook-decide <variant>` with stdin passthrough and exit-code propagation.
///
/// 2. **Lifecycle contracts** (Category 3) — post-compliance, post-edit,
///    pre-compact, session-end, codex-session-start, codex-user-prompt, and
///    codex-stop hooks invoke the correct daemon commands in the right order.
///
/// Enforcement logic (the allow/deny decision matrix) is tested by:
/// - `hooks::decide::tests` — pure decision functions (51 unit tests)
/// - `cli::hook_decide::tests` — adapter pipeline: eval response → decision →
///   platform events → formatted output (22 tests, including 10 e2e fixtures)
///
/// The test harness writes bash scripts to temp files, creates a mock `mati`
/// binary, and exercises hooks with controlled stdin JSON + mock responses.
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

use tempfile::TempDir;

use super::codex_post_bash;
use super::codex_pre_bash;
use super::codex_session_start;
use super::codex_stop;
use super::codex_user_prompt;
use super::post_compliance;
use super::post_edit;
use super::pre_bash;
use super::pre_compact;
use super::pre_edit;
use super::pre_read;
use super::session_end;

// ─── Test Harness ────────────────────────────────────────────────────────────

struct HookTestHarness {
    script_content: String,
    mock_dir: TempDir,
    mock_responses: HashMap<String, String>,
    exclude_binaries: Vec<String>,
    mock_ping_exit_code: i32,
    mock_recent_consulted: bool,
    /// Optional: inject extra logic into the mock mati script (e.g. sleep).
    mock_extra_cases: String,
}

// Used by Category 3 lifecycle tests.
#[allow(dead_code)]
struct HookOutput {
    stdout: String,
    stderr: String,
    exit_code: i32,
    json: Option<serde_json::Value>,
}

#[allow(dead_code)]
impl HookOutput {
    fn decision(&self) -> &str {
        self.json
            .as_ref()
            .and_then(|j| j.pointer("/hookSpecificOutput/permissionDecision"))
            .and_then(|v| v.as_str())
            .unwrap_or("")
    }

    fn reason(&self) -> &str {
        self.json
            .as_ref()
            .and_then(|j| j.pointer("/hookSpecificOutput/permissionDecisionReason"))
            .and_then(|v| v.as_str())
            .unwrap_or("")
    }

    fn additional_context(&self) -> &str {
        self.json
            .as_ref()
            .and_then(|j| j.pointer("/hookSpecificOutput/additionalContext"))
            .and_then(|v| v.as_str())
            .unwrap_or("")
    }
}

// Some constructors (for_pre_read, for_pre_bash, etc.) are used by specific
// Category 1 and Category 3 tests only.
#[allow(dead_code)]
impl HookTestHarness {
    fn for_pre_read() -> Self {
        Self {
            script_content: pre_read::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_pre_bash() -> Self {
        Self {
            script_content: pre_bash::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_post_compliance() -> Self {
        Self {
            script_content: post_compliance::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_codex_session_start() -> Self {
        Self {
            script_content: codex_session_start::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_codex_user_prompt() -> Self {
        Self {
            script_content: codex_user_prompt::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_codex_pre_bash() -> Self {
        Self {
            script_content: codex_pre_bash::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_codex_post_bash() -> Self {
        Self {
            script_content: codex_post_bash::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_codex_stop() -> Self {
        Self {
            script_content: codex_stop::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_post_edit() -> Self {
        Self {
            script_content: post_edit::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_pre_compact() -> Self {
        Self {
            script_content: pre_compact::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn for_session_end() -> Self {
        Self {
            script_content: session_end::SCRIPT.to_string(),
            mock_dir: TempDir::new().expect("failed to create temp dir for harness"),
            mock_responses: HashMap::new(),
            exclude_binaries: Vec::new(),
            mock_ping_exit_code: 0,
            mock_recent_consulted: false,
            mock_extra_cases: String::new(),
        }
    }

    fn with_mock_record(mut self, key: &str, json: &str) -> Self {
        self.mock_responses
            .insert(key.to_string(), json.to_string());
        self
    }

    fn with_ping_failure(mut self) -> Self {
        self.mock_ping_exit_code = 1;
        self
    }

    fn exclude_binary(mut self, name: &str) -> Self {
        self.exclude_binaries.push(name.to_string());
        self
    }

    fn with_extra_mock_case(mut self, case: &str) -> Self {
        self.mock_extra_cases = case.to_string();
        self
    }

    fn with_recent_consulted(mut self, recent: bool) -> Self {
        self.mock_recent_consulted = recent;
        self
    }

    /// Build the mock `mati` script and write it to mock_dir.
    fn write_mock_mati(&self) -> PathBuf {
        let log_file = self.mock_dir.path().join("mati_log.txt");
        let mut get_cases = String::new();
        for (key, response) in &self.mock_responses {
            // Escape single quotes in the response for bash safety
            let escaped = response.replace('\'', "'\\''");
            get_cases.push_str(&format!("            \"{key}\") echo '{escaped}' ;;\n"));
        }
        // Default case for unknown keys
        get_cases.push_str("            *) echo 'null' ;;\n");

        let script = format!(
            r#"#!/usr/bin/env bash
case "$1" in
    ping) exit {ping_exit} ;;
    get)
        KEY="$2"
        case "$KEY" in
{get_cases}        esac ;;
    session-check-consulted)
        echo "false" ;;
    session-check-consulted-recent)
        if [ "${{3:-}}" = "--ttl-secs" ] && [ "${{4:-}}" = "900" ]; then
            echo "{recent_consulted}"
        else
            echo "false"
        fi ;;
    doc-capture)
        cat >/dev/null
        echo "$@" >> "{log_file}" ;;
    log-miss|log-hit|log-compliance-miss|log-compliance-hit|log-codex-shell-miss|log-bootstrap|log-prompt-nudge|edit-hook|session-flush|session-harvest)
        echo "$@" >> "{log_file}" ;;
    reparse)
        exit 0 ;;
    {extra}
    *) exit 0 ;;
esac
"#,
            ping_exit = self.mock_ping_exit_code,
            recent_consulted = if self.mock_recent_consulted {
                "true"
            } else {
                "false"
            },
            get_cases = get_cases,
            log_file = log_file.display(),
            extra = self.mock_extra_cases,
        );

        let mock_path = self.mock_dir.path().join("mati");
        std::fs::write(&mock_path, script).expect("failed to write mock mati script");

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&mock_path, std::fs::Permissions::from_mode(0o755))
                .expect("failed to chmod mock mati");
        }

        mock_path
    }

    /// Write the hook bash script to a temp file.
    fn write_hook_script(&self) -> PathBuf {
        let script_path = self.mock_dir.path().join("hook.sh");
        std::fs::write(&script_path, &self.script_content).expect("failed to write hook script");

        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755))
                .expect("failed to chmod hook script");
        }

        script_path
    }

    /// Build a controlled PATH that includes the mock dir + essential system dirs,
    /// but excludes specified binaries by creating a filtered bin directory.
    fn build_path(&self) -> String {
        if self.exclude_binaries.is_empty() {
            // No exclusions: prepend mock_dir to system PATH
            let system_path = std::env::var("PATH").unwrap_or_else(|_| "/usr/bin:/bin".to_string());
            return format!("{}:{}", self.mock_dir.path().display(), system_path);
        }

        // Create a filtered bin dir with symlinks to system binaries,
        // excluding the ones we want to hide.
        let filtered_dir = self.mock_dir.path().join("filtered_bin");
        std::fs::create_dir_all(&filtered_dir).expect("failed to create filtered_bin dir");

        // Find system binaries we need: bash, cat, echo, printf, sed, grep, awk, test, [
        // and optionally jq, bc
        let essential_bins = [
            "bash", "cat", "echo", "printf", "sed", "grep", "awk", "test", "env", "command", "jq",
            "bc", "which", "dirname", "basename", "rm", "mkdir", "touch", "true", "false", "expr",
            "tr", "sort", "cut", "wc",
        ];

        // Search standard system directories for each binary
        let system_dirs = ["/usr/bin", "/bin", "/usr/local/bin"];

        for bin_name in &essential_bins {
            if self.exclude_binaries.contains(&bin_name.to_string()) {
                continue;
            }

            for dir in &system_dirs {
                let src = PathBuf::from(dir).join(bin_name);
                if src.exists() {
                    let dst = filtered_dir.join(bin_name);
                    if !dst.exists() {
                        #[cfg(unix)]
                        std::os::unix::fs::symlink(&src, &dst).ok();
                    }
                    break;
                }
            }
        }

        // PATH = mock_dir (for our mock mati) : filtered_bin (for system tools)
        format!(
            "{}:{}",
            self.mock_dir.path().display(),
            filtered_dir.display()
        )
    }

    /// Run the hook script with the given stdin JSON, capturing output.
    fn run(&self, stdin_json: &str) -> HookOutput {
        self.write_mock_mati();
        let script_path = self.write_hook_script();
        let path = self.build_path();

        let output = Command::new("bash")
            .arg(script_path.to_str().expect("script path not valid UTF-8"))
            .env("PATH", path)
            .env("HOME", self.mock_dir.path())
            .current_dir(self.mock_dir.path())
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .and_then(|mut child| {
                if let Some(ref mut stdin) = child.stdin {
                    // BrokenPipe is expected when the script exits before
                    // reading stdin (e.g. the no-op user-prompt hook).
                    let _ = stdin.write_all(stdin_json.as_bytes());
                }
                // Drop stdin to send EOF
                child.stdin.take();
                child.wait_with_output()
            })
            .expect("failed to execute hook script");

        let stdout = String::from_utf8_lossy(&output.stdout).to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).to_string();
        let exit_code = output.status.code().unwrap_or(-1);

        let json = serde_json::from_str::<serde_json::Value>(stdout.trim()).ok();

        HookOutput {
            stdout,
            stderr,
            exit_code,
            json,
        }
    }

    /// Read the log file written by mock mati's log-miss/log-hit commands.
    fn read_log(&self) -> String {
        let log_file = self.mock_dir.path().join("mati_log.txt");
        std::fs::read_to_string(&log_file).unwrap_or_default()
    }

    fn wait_for_log_contains(&self, needle: &str) -> bool {
        self.wait_for_log_contains_timeout(needle, std::time::Duration::from_secs(2))
    }

    fn wait_for_log_contains_timeout(&self, needle: &str, timeout: std::time::Duration) -> bool {
        let deadline = std::time::Instant::now() + timeout;
        while std::time::Instant::now() < deadline {
            if self.read_log().contains(needle) {
                return true;
            }
            std::thread::sleep(std::time::Duration::from_millis(25));
        }
        false
    }
}

// ═════════════════════════════════════════════════════════════════════════════
// Category 1: Wrapper Execution Tests
//
// Each thin shell wrapper must:
//   (a) pass stdin through to `mati hook-decide <variant>`,
//   (b) propagate the exit code,
//   (c) relay stdout/stderr.
//
// Enforcement logic is tested by hooks::decide::tests (51 unit tests) and
// cli::hook_decide::tests (22 tests including 10 e2e adapter fixtures).
// ═════════════════════════════════════════════════════════════════════════════

/// Build a mock `mati` that handles `hook-decide <variant>`:
///   - Logs the variant and stdin to a file for assertion.
///   - Outputs a canned JSON response on stdout.
///   - Exits with a configurable code.
struct WrapperHarness {
    mock_dir: TempDir,
}

struct WrapperOutput {
    stdout: String,
    stderr: String,
    exit_code: i32,
}

impl WrapperHarness {
    fn new() -> Self {
        Self {
            mock_dir: TempDir::new().expect("failed to create temp dir"),
        }
    }

    /// Write a mock `mati` binary that records invocation details and replays
    /// a canned response. `exit_code` controls what the mock returns.
    fn write_mock(&self, canned_stdout: &str, canned_stderr: &str, exit_code: i32) {
        let log_path = self.mock_dir.path().join("invocation.log");
        let escaped_stdout = canned_stdout.replace('\'', "'\\''");
        let escaped_stderr = canned_stderr.replace('\'', "'\\''");
        let script = format!(
            r#"#!/usr/bin/env bash
# Log the full argument vector.
echo "ARGS: $*" >> "{log}"
# Log stdin so tests can verify passthrough.
STDIN="$(cat)"
echo "STDIN: $STDIN" >> "{log}"
# Replay canned response.
echo -n '{stdout}' >&1
echo -n '{stderr}' >&2
exit {code}
"#,
            log = log_path.display(),
            stdout = escaped_stdout,
            stderr = escaped_stderr,
            code = exit_code,
        );
        let mock_path = self.mock_dir.path().join("mati");
        std::fs::write(&mock_path, script).expect("write mock");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&mock_path, std::fs::Permissions::from_mode(0o755))
                .expect("chmod mock");
        }
    }

    /// Execute a wrapper script against the mock.
    fn run(&self, script_content: &str, stdin_json: &str) -> WrapperOutput {
        let script_path = self.mock_dir.path().join("hook.sh");
        std::fs::write(&script_path, script_content).expect("write hook script");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755))
                .expect("chmod hook script");
        }

        // Do NOT include mock_dir in PATH — the wrapper's own HOOKS_DIR
        // setup (`export PATH="$HOOKS_DIR:$PATH"`) must be the only way
        // the sibling `mati` binary is found.
        let path = "/usr/bin:/bin:/usr/local/bin";

        let output = Command::new("bash")
            .arg(script_path.to_str().unwrap())
            .env("PATH", path)
            .env("HOME", self.mock_dir.path())
            .current_dir(self.mock_dir.path())
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .and_then(|mut child| {
                if let Some(ref mut stdin) = child.stdin {
                    stdin.write_all(stdin_json.as_bytes()).expect("write stdin");
                }
                child.stdin.take();
                child.wait_with_output()
            })
            .expect("execute hook");

        WrapperOutput {
            stdout: String::from_utf8_lossy(&output.stdout).to_string(),
            stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            exit_code: output.status.code().unwrap_or(-1),
        }
    }

    /// Read the invocation log written by the mock.
    fn invocation_log(&self) -> String {
        let log_path = self.mock_dir.path().join("invocation.log");
        std::fs::read_to_string(&log_path).unwrap_or_default()
    }
}

// ── 1.01–1.04: Variant handoff — each wrapper passes the correct variant ────

#[test]
fn preread_wrapper_executes_hook_decide_with_correct_variant() {
    let h = WrapperHarness::new();
    let response = r#"{"hookSpecificOutput":{"permissionDecision":"allow"}}"#;
    h.write_mock(response, "", 0);
    let out = h.run(
        pre_read::SCRIPT,
        r#"{"tool_input":{"file_path":"src/main.rs"}}"#,
    );

    assert_eq!(out.exit_code, 0, "wrapper must propagate exit 0");
    assert_eq!(out.stdout.trim(), response, "wrapper must relay stdout");

    let log = h.invocation_log();
    assert!(
        log.contains("ARGS: hook-decide claude-pre-read"),
        "wrapper must pass variant claude-pre-read, log: {log}"
    );
}

#[test]
fn preedit_wrapper_executes_hook_decide_with_correct_variant() {
    let h = WrapperHarness::new();
    // The edit gate denies via a Claude PreToolUse deny JSON on stdout, exit 0.
    let response = r#"{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"[mati] before editing this file."}}"#;
    h.write_mock(response, "", 0);
    let out = h.run(
        pre_edit::SCRIPT,
        r#"{"tool_input":{"file_path":"src/main.rs"}}"#,
    );

    assert_eq!(out.exit_code, 0, "wrapper must propagate exit 0");
    assert_eq!(
        out.stdout.trim(),
        response,
        "wrapper must relay the deny JSON"
    );

    let log = h.invocation_log();
    assert!(
        log.contains("ARGS: hook-decide claude-pre-edit"),
        "wrapper must pass variant claude-pre-edit, log: {log}"
    );
    assert!(
        log.contains(r#"STDIN: {"tool_input":{"file_path":"src/main.rs"}}"#),
        "wrapper must pass stdin through, log: {log}"
    );
}

#[test]
fn prebash_wrapper_executes_hook_decide_with_correct_variant() {
    let h = WrapperHarness::new();
    h.write_mock(
        r#"{"hookSpecificOutput":{"permissionDecision":"allow"}}"#,
        "",
        0,
    );
    h.run(
        pre_bash::SCRIPT,
        r#"{"tool_input":{"command":"cat src/main.rs"}}"#,
    );

    let log = h.invocation_log();
    assert!(
        log.contains("ARGS: hook-decide claude-pre-bash"),
        "wrapper must pass variant claude-pre-bash, log: {log}"
    );
}

#[test]
fn codex_prebash_wrapper_executes_hook_decide_with_correct_variant() {
    let h = WrapperHarness::new();
    h.write_mock("", "", 0);
    h.run(
        codex_pre_bash::SCRIPT,
        r#"{"tool_input":{"command":"cat src/main.rs"}}"#,
    );

    let log = h.invocation_log();
    assert!(
        log.contains("ARGS: hook-decide codex-pre-bash"),
        "wrapper must pass variant codex-pre-bash, log: {log}"
    );
}

#[test]
fn codex_postbash_wrapper_executes_hook_decide_with_correct_variant() {
    let h = WrapperHarness::new();
    h.write_mock("", "", 0);
    h.run(
        codex_post_bash::SCRIPT,
        r#"{"tool_input":{"command":"cat src/main.rs"}}"#,
    );

    let log = h.invocation_log();
    assert!(
        log.contains("ARGS: hook-decide codex-post-bash"),
        "wrapper must pass variant codex-post-bash, log: {log}"
    );
}

// ── 1.05–1.06: Stdin passthrough — tool_input JSON reaches mock mati ────────

#[test]
fn preread_wrapper_passes_stdin_through() {
    let h = WrapperHarness::new();
    h.write_mock(
        r#"{"hookSpecificOutput":{"permissionDecision":"allow"}}"#,
        "",
        0,
    );
    let input = r#"{"tool_input":{"file_path":"src/store/db.rs"}}"#;
    h.run(pre_read::SCRIPT, input);

    let log = h.invocation_log();
    assert!(
        log.contains(input),
        "stdin must reach mock mati intact, log: {log}"
    );
}

#[test]
fn codex_prebash_wrapper_passes_stdin_through() {
    let h = WrapperHarness::new();
    h.write_mock("", "", 0);
    let input = r#"{"tool_input":{"command":"head -20 src/main.rs"}}"#;
    h.run(codex_pre_bash::SCRIPT, input);

    let log = h.invocation_log();
    assert!(
        log.contains(input),
        "stdin must reach mock mati intact, log: {log}"
    );
}

// ── 1.07–1.08: Exit code propagation — non-zero codes survive exec ──────────

#[test]
fn preread_wrapper_propagates_nonzero_exit() {
    let h = WrapperHarness::new();
    h.write_mock("", "blocked", 1);
    let out = h.run(
        pre_read::SCRIPT,
        r#"{"tool_input":{"file_path":"src/main.rs"}}"#,
    );

    assert_eq!(
        out.exit_code, 1,
        "wrapper must propagate exit 1 from hook-decide"
    );
}

#[test]
fn codex_prebash_wrapper_propagates_exit2_deny() {
    let h = WrapperHarness::new();
    h.write_mock("", "Run mem_get first", 2);
    let out = h.run(
        codex_pre_bash::SCRIPT,
        r#"{"tool_input":{"command":"cat src/main.rs"}}"#,
    );

    assert_eq!(out.exit_code, 2, "Codex deny exit 2 must survive exec");
    assert!(
        out.stderr.contains("mem_get"),
        "stderr must relay deny message, got: {}",
        out.stderr
    );
}

// ── 1.09: Stderr relay — wrapper does not swallow stderr ─────────────────────

#[test]
fn preread_wrapper_relays_stderr() {
    let h = WrapperHarness::new();
    h.write_mock(
        r#"{"hookSpecificOutput":{"permissionDecision":"deny"}}"#,
        "[mati] WARNING: test stderr relay",
        0,
    );
    let out = h.run(
        pre_read::SCRIPT,
        r#"{"tool_input":{"file_path":"src/main.rs"}}"#,
    );

    assert!(
        out.stderr.contains("[mati] WARNING: test stderr relay"),
        "wrapper must relay stderr, got: {}",
        out.stderr
    );
}

// ── 1.10: No mati in PATH — set -e should cause a clean failure ─────────────

#[test]
fn preread_wrapper_no_mati_in_path_fails_cleanly() {
    // Don't write any mock — mati is not in PATH.
    let h = WrapperHarness::new();
    let script_path = h.mock_dir.path().join("hook.sh");
    std::fs::write(&script_path, pre_read::SCRIPT).expect("write hook");
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&script_path, std::fs::Permissions::from_mode(0o755))
            .expect("chmod");
    }

    // PATH with only system dirs — no mock mati.
    let output = Command::new("bash")
        .arg(script_path.to_str().unwrap())
        .env("PATH", "/usr/bin:/bin")
        .env("HOME", h.mock_dir.path())
        .current_dir(h.mock_dir.path())
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .and_then(|mut child| {
            if let Some(ref mut stdin) = child.stdin {
                stdin.write_all(b"{}").expect("write stdin");
            }
            child.stdin.take();
            child.wait_with_output()
        })
        .expect("execute hook");

    // `command -v mati` guard in the wrapper exits 0 (graceful fail-open)
    // when mati is not in PATH — the agent must not be blocked by a missing binary.
    assert_eq!(
        output.status.code().unwrap_or(-1),
        0,
        "missing mati must exit 0 (graceful fail-open)"
    );
}

// ═════════════════════════════════════════════════════════════════════════════
// Category 3: Post/Lifecycle Hook Contracts (10 tests)
// ═════════════════════════════════════════════════════════════════════════════

/// 3.01 — post-read compliance logs extensionless root-level files.
#[test]
fn post_compliance_logs_extensionless_root_file_miss() {
    let harness = HookTestHarness::for_post_compliance();
    let root_file = harness.mock_dir.path().join("Dockerfile");
    std::fs::write(&root_file, "FROM rust:1.80").expect("failed to write Dockerfile");

    let input = serde_json::json!({
        "tool_input": { "path": "Dockerfile" }
    })
    .to_string();
    let output = harness.run(&input);
    assert_eq!(output.exit_code, 0);
    assert!(
        harness.wait_for_log_contains("log-compliance-miss file:Dockerfile"),
        "extensionless file should still be tracked as a compliance miss, log: {}",
        harness.read_log()
    );
}

/// 3.02 — post-edit invokes both doc-capture and edit-hook.
#[test]
fn post_edit_invokes_doc_capture_and_edit_hook() {
    let harness = HookTestHarness::for_post_edit();
    let input = serde_json::json!({
        "tool_input": {
            "file_path": "src/main.rs",
            "content": "/// Main entrypoint\nfn main() {}\n"
        }
    })
    .to_string();
    let output = harness.run(&input);
    assert_eq!(output.exit_code, 0);
    // post-edit fires background processes (`&`) — allow extra time under parallel load.
    let bg_timeout = std::time::Duration::from_secs(5);
    assert!(
        harness.wait_for_log_contains_timeout("doc-capture src/main.rs", bg_timeout),
        "post-edit should invoke doc-capture, log: {}",
        harness.read_log()
    );
    assert!(
        harness.wait_for_log_contains_timeout("edit-hook src/main.rs", bg_timeout),
        "post-edit should invoke edit-hook, log: {}",
        harness.read_log()
    );
}

/// 3.03 — pre-compact flushes session state synchronously.
#[test]
fn pre_compact_invokes_session_flush() {
    let harness = HookTestHarness::for_pre_compact();
    let output = harness.run(r#"{"event":"PreCompact"}"#);
    assert_eq!(output.exit_code, 0);
    assert!(
        harness.wait_for_log_contains("session-flush"),
        "pre-compact should invoke session-flush, log: {}",
        harness.read_log()
    );
}

/// 3.04 — session-end triggers session harvest.
#[test]
fn session_end_invokes_session_harvest() {
    let harness = HookTestHarness::for_session_end();
    let output = harness.run("");
    assert_eq!(output.exit_code, 0);
    assert!(
        harness.wait_for_log_contains("session-harvest"),
        "session-end should invoke session-harvest, log: {}",
        harness.read_log()
    );
}

/// 3.05 — Codex session-start emits compact active sentinel (~5 tokens).
#[test]
fn codex_session_start_emits_active_sentinel() {
    let harness = HookTestHarness::for_codex_session_start();
    let output = harness.run("{}");
    assert_eq!(output.exit_code, 0);
    assert!(
        output.stdout.contains("[mati] active"),
        "session-start should emit compact sentinel, got: {}",
        output.stdout
    );
}

/// 3.06 — Codex user-prompt hook exits cleanly with zero injection.
#[test]
fn codex_user_prompt_exits_clean_no_injection() {
    let harness = HookTestHarness::for_codex_user_prompt();
    let output = harness.run(r#"{"prompt":"Please inspect src/main.rs and fix the bug"}"#);
    assert_eq!(output.exit_code, 0);
    assert!(
        output.stdout.trim().is_empty(),
        "user-prompt hook must inject zero tokens, got: {}",
        output.stdout
    );
}

// 3.07–3.09 — Codex pre-bash and post-bash enforcement is in Rust.
// Wrapper execution is in Category 1 above. Enforcement matrix is covered
// by hooks::decide::tests (51 unit tests) + cli::hook_decide::tests (22 tests).

/// 3.10 — Codex stop flushes then harvests the session.
#[test]
fn codex_stop_flushes_then_harvests() {
    let harness = HookTestHarness::for_codex_stop();
    let output = harness.run("{}");
    assert_eq!(output.exit_code, 0);
    let log = harness.read_log();
    let flush_pos = log
        .find("session-flush")
        .expect("missing session-flush in log");
    let harvest_pos = log
        .find("session-harvest")
        .expect("missing session-harvest in log");
    assert!(
        flush_pos < harvest_pos,
        "flush must precede harvest, log: {log}"
    );
}