batpak 0.7.0

Event sourcing with causal graphs and policy gates. Sync API, zero async.
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
// justifies: INV-BUILD-FAIL-FAST; build.rs consolidates panic through the `fail` helper below, see build.rs and traceability/invariants.yaml
#![allow(clippy::panic)]

use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::fs;
use std::path::{Path, PathBuf};
use syn::visit::Visit;
mod shared_checks {
    include!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/build_support/shared_checks.rs"
    ));
}

/// Single documented failure path for build.rs. Every invariant violation
/// routes through here so the panic surface is consolidated and auditable.
/// Emits a cargo-parseable warning on stdout, then panics so Cargo stops the
/// build without relying on `process::exit`.
fn fail(msg: &str) -> ! {
    println!("cargo:warning=build.rs: {msg}");
    panic!("build.rs invariant failed: {msg}")
}

#[derive(Debug, Deserialize)]
struct PubItemAllowlistEntry {
    name: String,
    justification: String,
    #[serde(default)]
    witness: Vec<PubItemAllowlistWitness>,
}

#[derive(Debug, Deserialize)]
struct PubItemAllowlistWitness {
    path: String,
    #[serde(default)]
    lines: Vec<u32>,
}

// build.rs runs before every cargo build/check/test. Cannot be skipped.
// It enforces live runtime invariants at build time so agents get English
// errors instead of cryptic compiler failures. See README.md, GUIDE.md, and
// REFERENCE.md for the current truth hierarchy.
fn main() {
    let repo_invariants_available = repo_invariant_surface_available();

    println!("cargo:rerun-if-env-changed=BATPAK_PLATFORM_PROFILE");
    println!("cargo:rerun-if-changed=Cargo.toml");
    println!("cargo:rerun-if-changed=src/");
    println!("cargo:rerun-if-changed=examples/");
    println!("cargo:rerun-if-changed=build_support/shared_checks.rs");
    if repo_invariants_available {
        println!("cargo:rerun-if-changed=../../Cargo.toml");
        println!("cargo:rerun-if-changed=tests/");
        println!("cargo:rerun-if-changed=benches/");
        println!("cargo:rerun-if-changed=../macros/src/");
        println!("cargo:rerun-if-changed=../macros-support/src/");
        println!("cargo:rerun-if-changed=../../tools/xtask/src/");
        println!("cargo:rerun-if-changed=../../tools/integrity/src/");
        println!("cargo:rerun-if-changed=../../traceability/dead_code_silencer_allowlist.yaml");
        println!("cargo:rerun-if-changed=../../traceability/pub_item_allowlist.yaml");
        println!("cargo:rerun-if-changed=../../traceability/invariants.yaml");
        println!("cargo:rerun-if-changed=../../docs/");
    }

    check_no_tokio_in_deps();
    check_no_banned_patterns();
    check_store_config_field_usage();
    if repo_invariants_available {
        check_no_dead_code_silencers();
        check_allow_justifications();
    }
    check_no_stubs_in_src();
    check_store_surface_honesty();
    check_no_fixed_temp_patterns();
    if repo_invariants_available {
        check_pub_items_have_tests();
    }
    check_platform_profile_env();
}

fn repo_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..")
}

fn core_root() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}

fn repo_invariant_surface_available() -> bool {
    let repo_root = repo_root();
    [
        repo_root.join("Cargo.toml"),
        repo_root.join("traceability/dead_code_silencer_allowlist.yaml"),
        repo_root.join("traceability/pub_item_allowlist.yaml"),
        repo_root.join("traceability/invariants.yaml"),
        repo_root.join("tools/xtask/src"),
        repo_root.join("tools/integrity/src"),
    ]
    .iter()
    .all(|path| path.exists())
}

fn repo_relative_display(path: &Path) -> String {
    path.display().to_string().replace('\\', "/")
}

#[derive(Debug, Serialize, Deserialize)]
struct BuildPlatformProfile {
    schema_version: u16,
    host: BuildPlatformProfileHost,
    store_path: BuildStorePathProfile,
    admission: BuildPlatformAdmissionProfile,
    fingerprint_crc32: u32,
}

#[derive(Debug, Serialize, Deserialize)]
struct BuildPlatformProfileHost {
    monotonic_clock: BuildClockEvidence,
}

#[derive(Debug, Serialize, Deserialize)]
struct BuildStorePathProfile {
    path_status: BuildStorePathStatusEvidence,
    parent_dir_sync: BuildParentDirSyncEvidence,
    lock_leaf_symlink_protection: BuildLockLeafSymlinkProtection,
    mmap_index: BuildMmapEvidence,
    sealed_segment_mmap: BuildMmapEvidence,
    active_segment_read: BuildActiveSegmentReadEvidence,
}

#[derive(Debug, Serialize, Deserialize)]
struct BuildPlatformAdmissionProfile {
    store_lock: BuildStoreLockAdmissionSummary,
    parent_dir_sync: BuildParentDirSyncAdmissionSummary,
    mmap_index: BuildMmapAdmissionSummary,
    sealed_segment_mmap: BuildMmapAdmissionSummary,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildClockEvidence {
    ProcessLocalInstantAnchor,
    Unknown,
    ProbeFailed,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildStorePathStatusEvidence {
    ObservedDirectory,
    UnknownMissing,
    ObservedUnsupportedNotDirectory,
    ProbeFailed { reason: String },
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildParentDirSyncEvidence {
    UnixFsync,
    RenameOnly,
    Unknown,
    ProbeFailed,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildLockLeafSymlinkProtection {
    AtomicNoFollow,
    BestEffortCheckThenOpen,
    Unknown,
    ObservedUnsupported,
    ProbeFailed,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildMmapEvidence {
    FileBacked,
    Unknown,
    ObservedUnsupported,
    ProbeFailed,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildActiveSegmentReadEvidence {
    UnixReadAt,
    LockedSeekRead,
    Unknown,
    ProbeFailed,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildStoreLockAdmissionSummary {
    AtomicNoFollow,
    BestEffortCheckThenOpen,
    Rejected,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildParentDirSyncAdmissionSummary {
    UnixFsync,
    RenameOnly,
    Rejected,
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)]
enum BuildMmapAdmissionSummary {
    FileBacked,
    Rejected,
}

#[derive(Serialize)]
struct BuildPlatformProfileBody<'a> {
    schema_version: u16,
    host: &'a BuildPlatformProfileHost,
    store_path: &'a BuildStorePathProfile,
    admission: &'a BuildPlatformAdmissionProfile,
}

fn check_platform_profile_env() {
    let Ok(path) = std::env::var("BATPAK_PLATFORM_PROFILE") else {
        return;
    };
    println!("cargo:rerun-if-changed={path}");
    let bytes = fs::read(&path).unwrap_or_else(|error| {
        fail(&format!(
            "cannot read BATPAK_PLATFORM_PROFILE={path}: {error}"
        ))
    });
    let profile: BuildPlatformProfile = serde_json::from_slice(&bytes).unwrap_or_else(|error| {
        fail(&format!(
            "cannot decode BATPAK_PLATFORM_PROFILE={path}: {error}"
        ))
    });
    if profile.schema_version != 1 {
        fail(&format!(
            "BATPAK_PLATFORM_PROFILE={path} has schema_version {}; expected 1",
            profile.schema_version
        ));
    }
    validate_build_platform_profile_semantics(&profile, &path);
    let body = BuildPlatformProfileBody {
        schema_version: profile.schema_version,
        host: &profile.host,
        store_path: &profile.store_path,
        admission: &profile.admission,
    };
    let body_bytes = serde_json::to_vec(&body).unwrap_or_else(|error| {
        fail(&format!(
            "cannot canonicalize BATPAK_PLATFORM_PROFILE={path}: {error}"
        ))
    });
    let computed = crc32fast::hash(&body_bytes);
    if computed != profile.fingerprint_crc32 {
        fail(&format!(
            "BATPAK_PLATFORM_PROFILE={path} fingerprint_crc32 {} does not match computed {}",
            profile.fingerprint_crc32, computed
        ));
    }
    println!("cargo:rustc-env=BATPAK_PLATFORM_PROFILE_PATH={path}");
    println!("cargo:rustc-env=BATPAK_PLATFORM_PROFILE_FINGERPRINT_CRC32={computed}");
}

fn validate_build_platform_profile_semantics(profile: &BuildPlatformProfile, path: &str) {
    let expected_store_lock = match profile.store_path.lock_leaf_symlink_protection {
        BuildLockLeafSymlinkProtection::AtomicNoFollow => {
            BuildStoreLockAdmissionSummary::AtomicNoFollow
        }
        BuildLockLeafSymlinkProtection::BestEffortCheckThenOpen => {
            BuildStoreLockAdmissionSummary::BestEffortCheckThenOpen
        }
        BuildLockLeafSymlinkProtection::Unknown
        | BuildLockLeafSymlinkProtection::ObservedUnsupported
        | BuildLockLeafSymlinkProtection::ProbeFailed => BuildStoreLockAdmissionSummary::Rejected,
    };
    if profile.admission.store_lock != expected_store_lock {
        fail(&format!(
            "BATPAK_PLATFORM_PROFILE={path} has inconsistent store_lock admission {:?}; expected {:?} from lock evidence {:?}",
            profile.admission.store_lock,
            expected_store_lock,
            profile.store_path.lock_leaf_symlink_protection
        ));
    }

    let expected_parent_dir_sync = match profile.store_path.parent_dir_sync {
        BuildParentDirSyncEvidence::UnixFsync => BuildParentDirSyncAdmissionSummary::UnixFsync,
        BuildParentDirSyncEvidence::RenameOnly => BuildParentDirSyncAdmissionSummary::RenameOnly,
        BuildParentDirSyncEvidence::Unknown | BuildParentDirSyncEvidence::ProbeFailed => {
            BuildParentDirSyncAdmissionSummary::Rejected
        }
    };
    if profile.admission.parent_dir_sync != expected_parent_dir_sync {
        fail(&format!(
            "BATPAK_PLATFORM_PROFILE={path} has inconsistent parent_dir_sync admission {:?}; expected {:?} from parent-dir evidence {:?}",
            profile.admission.parent_dir_sync,
            expected_parent_dir_sync,
            profile.store_path.parent_dir_sync
        ));
    }

    validate_build_path_mmap_consistency(path, "mmap_index", profile);
    validate_build_path_mmap_consistency(path, "sealed_segment_mmap", profile);
    validate_build_mmap_admission(
        path,
        "mmap_index",
        &profile.store_path.mmap_index,
        &profile.admission.mmap_index,
    );
    validate_build_mmap_admission(
        path,
        "sealed_segment_mmap",
        &profile.store_path.sealed_segment_mmap,
        &profile.admission.sealed_segment_mmap,
    );
}

fn validate_build_path_mmap_consistency(path: &str, field: &str, profile: &BuildPlatformProfile) {
    let evidence = match field {
        "mmap_index" => &profile.store_path.mmap_index,
        "sealed_segment_mmap" => &profile.store_path.sealed_segment_mmap,
        _ => fail(&format!(
            "internal build profile validation bug: unknown mmap field {field}"
        )),
    };
    let required = match profile.store_path.path_status {
        BuildStorePathStatusEvidence::ObservedDirectory => return,
        BuildStorePathStatusEvidence::ObservedUnsupportedNotDirectory => {
            BuildMmapEvidence::ObservedUnsupported
        }
        BuildStorePathStatusEvidence::UnknownMissing => BuildMmapEvidence::Unknown,
        BuildStorePathStatusEvidence::ProbeFailed { .. } => BuildMmapEvidence::ProbeFailed,
    };
    if evidence != &required {
        fail(&format!(
            "BATPAK_PLATFORM_PROFILE={path} has inconsistent {field} evidence {evidence:?}; expected {required:?} from path_status {:?}",
            profile.store_path.path_status
        ));
    }
}

fn validate_build_mmap_admission(
    path: &str,
    field: &str,
    evidence: &BuildMmapEvidence,
    admission: &BuildMmapAdmissionSummary,
) {
    let expected = match evidence {
        BuildMmapEvidence::FileBacked => BuildMmapAdmissionSummary::FileBacked,
        BuildMmapEvidence::Unknown
        | BuildMmapEvidence::ObservedUnsupported
        | BuildMmapEvidence::ProbeFailed => BuildMmapAdmissionSummary::Rejected,
    };
    if admission != &expected {
        fail(&format!(
            "BATPAK_PLATFORM_PROFILE={path} has inconsistent {field} admission {admission:?}; expected {expected:?} from mmap evidence {evidence:?}"
        ));
    }
}

/// Audit Loop Layer 2 enforcement: no stub markers in production src/.
/// todo!() and unimplemented!() are already denied by clippy, but this
/// catches patterns clippy misses: hardcoded placeholder strings, empty
/// function bodies returning defaults, etc.
fn check_no_stubs_in_src() {
    let stub_patterns = [
        (
            "\"placeholder\"",
            "Placeholder string literal — replace with real implementation",
        ),
        (
            "\"not implemented\"",
            "Stub string — implement the real behavior or return a typed error",
        ),
        (
            "\"not yet implemented\"",
            "Stub string — implement the real behavior",
        ),
    ];

    walk_rs_files(Path::new("src"), &mut |path, contents| {
        let path_str = repo_relative_display(path);
        for (line_no, line) in contents.lines().enumerate() {
            let lower = line.to_lowercase();
            for (pattern, msg) in &stub_patterns {
                if lower.contains(pattern) {
                    panic!(
                        "STUB DETECTED in {path_str}:{}: {msg}\n\
                         Line: {line}\n\
                         LAW-001: No fake success responses. FM-009: No polite downgrades.",
                        line_no + 1
                    );
                }
            }
        }
    });
}

fn check_no_dead_code_silencers() {
    let repo_root = repo_root();
    let allowlisted = shared_checks::load_dead_code_silencer_allowlist(&repo_root)
        .unwrap_or_else(|err| fail(&err));
    walk_dead_code_checked_rs_files(&mut |path, contents| {
        let rel = path
            .strip_prefix(&repo_root)
            .unwrap_or(path)
            .to_string_lossy()
            .replace('\\', "/");
        let sites =
            shared_checks::collect_dead_code_silencer_sites(contents).unwrap_or_else(|err| {
                fail(&format!(
                    "cannot parse {} while checking dead_code silencers: {err}",
                    rel
                ))
            });
        for site in sites {
            let allowlist_site = format!("{rel}:{}", site.line);
            if allowlisted.contains(&allowlist_site) {
                continue;
            }
            fail(&format!(
                    "dead_code silencers are not tolerated in this repo: {rel}:{}:{}\n\
                     Found `{}`.\n\
                     If code is test-only, use #[cfg(test)]. If it is unused, delete it.\n\
                     If it is shared infrastructure, restructure it so the compiler sees the real ownership surface.\n\
                     If this is the rare legitimate exception, add `{allowlist_site}` to traceability/dead_code_silencer_allowlist.yaml with `reason` and `adr`.",
                    site.line,
                    site.column,
                    site.rendered,
                ));
        }
    });
}

/// INV-ALLOW-IS-DESIGN enforcement: every #[allow(...)] in runtime or
/// toolchain Rust code must carry a structured `// justifies:` comment with
/// >= 5 words AND >= 1 resolvable anchor (INV-id, ADR-NNNN, or repo path).
fn check_allow_justifications() {
    let repo_root = repo_root();
    let known_invariants =
        shared_checks::load_known_invariants(&repo_root).unwrap_or_else(|err| fail(&err));
    walk_allow_checked_rs_files(&mut |path, contents| {
        let path_str = repo_relative_display(path);
        let lines: Vec<&str> = contents.lines().collect();
        for (line_no, line) in lines.iter().enumerate() {
            let trimmed = line.trim();
            if trimmed.starts_with("#![allow(") || trimmed.starts_with("#[allow(") {
                let has_justification =
                    shared_checks::line_carries_justification(line, &repo_root, &known_invariants)
                        || (line_no > 0
                            && lines
                                .get(line_no - 1)
                                .map(|prev| {
                                    shared_checks::line_carries_justification(
                                        prev,
                                        &repo_root,
                                        &known_invariants,
                                    )
                                })
                                .unwrap_or(false));
                if !has_justification {
                    fail(&format!(
                        "ROGUE SILENCE in {path_str}:{}: `{trimmed}`\n\
                         Every #[allow(...)] must carry a `// justifies: <>=5 words + >=1 resolvable anchor>`\n\
                         comment on the same or preceding line. Anchors: INV-<NAME> from\n\
                         traceability/invariants.yaml, ADR-NNNN from docs/, or a concrete repo path\n\
                         (src/..., tests/..., examples/..., crates/macros/..., crates/macros-support/...,\n\
                         benches/..., tools/..., build.rs). See INV-ALLOW-IS-DESIGN.",
                        line_no + 1
                    ));
                }
            }
        }
    });
}

fn check_no_tokio_in_deps() {
    //Invariant 1: tokio must not appear in [dependencies].
    //Only [dev-dependencies] is allowed.
    let cargo = fs::read_to_string("Cargo.toml").expect("read Cargo.toml");

    //Strategy: find the [dependencies] section, take text until the next
    //section header (line starting with [), check for "tokio".
    //This is deliberately simple string matching — no toml parser dep.
    if let Some(deps_section) = cargo.split("[dependencies]").nth(1) {
        let deps_only = deps_section.split("\n[").next().unwrap_or("");
        if deps_only.contains("tokio") {
            panic!(
                "INVARIANT 1 VIOLATED: tokio found in [dependencies].\n\
                 tokio belongs in [dev-dependencies] only.\n\
                 The library is runtime-agnostic. Fan-out uses Vec<flume::Sender>.\n\
                 See: REFERENCE.md."
            );
        }
    }
}

fn check_no_banned_patterns() {
    //Walk src/**/*.rs, read each file, check for patterns that violate
    //invariants or red flags.
    walk_rs_files(Path::new("src"), &mut |path, contents| {
        let path_str = repo_relative_display(path);

        //Red flag: no transmute/mem::read/pointer_cast in any src file.
        //All serialization goes through MessagePack.
        for banned in ["transmute", "mem::read", "pointer_cast"] {
            if contents.contains(banned) {
                panic!(
                    "RED FLAG VIOLATED in {path_str}: found `{banned}`.\n\
                     repr(C) is for field ordering, not a wire format.\n\
                     All serialization goes through rmp-serde. Always.\n\
                     See: REFERENCE.md."
                );
            }
        }

        //Invariant 2: no async fn in store module.
        //Store API is sync. Async lives in flume channels.
        if path_str.contains("store") && contents.contains("async fn") {
            panic!(
                "INVARIANT 2 VIOLATED in {path_str}: found `async fn`.\n\
                 Store API is sync. Async callers use spawn_blocking()\n\
                 or flume's recv_async(). See: store/subscription.rs.\n\
                 See: REFERENCE.md."
            );
        }

        // Post-mortem Bug 7: std::thread::spawn() panics on failure.
        // All thread creation must use Builder::new().spawn() for fallible error handling.
        if contents.contains("std::thread::spawn(") {
            panic!(
                "BANNED PATTERN in {path_str}: `std::thread::spawn()` found.\n\
                 Use `std::thread::Builder::new().name(...).spawn()` instead.\n\
                 `thread::spawn` panics on failure; `Builder::spawn` returns Result.\n\
                 See: Bug 7 post-mortem (react_loop panic)."
            );
        }

        // Post-mortem Bug 9: bare .sync() bypasses sync_mode config.
        // In store/ files, require .sync_with_mode() — never bare .sync().
        // The only exception is segment.rs which defines the .sync() method itself.
        if path_str.contains("store") && !path_str.ends_with("segment.rs") {
            for (line_no, line) in contents.lines().enumerate() {
                let trimmed = line.trim();
                if trimmed.starts_with("//") || trimmed.starts_with("///") {
                    continue;
                }
                // Match .sync() but not .sync_with_mode() and not self.sync() (Store::sync)
                if trimmed.contains(".sync()")
                    && !trimmed.contains("sync_with_mode")
                    && !trimmed.contains("self.sync()")
                    && !trimmed.contains("force_sync()")
                {
                    panic!(
                        "BANNED PATTERN in {path_str}:{}: bare `.sync()` call.\n\
                         Use `.sync_with_mode(&config.sync.mode)` instead.\n\
                         Bare .sync() hardcodes SyncAll, ignoring the user's config.\n\
                         See: Bug 9 post-mortem (segment rotation bypassed sync.mode).\n\
                         Line: {trimmed}",
                        line_no + 1
                    );
                }
            }
        }

        // INV-3: ban product-shape nouns in public declarations everywhere except the
        // documented Lane-A substrate carve-out (`src/artifact.rs` owns the `artifact` noun).
        // `trajectory` and `tenant` remain banned crate-wide on declaration lines.
        const INV3_ARTIFACT_ALLOWED_PATH: &str = "src/artifact.rs";

        #[inline]
        fn inv3_declaration_has_word(lower: &str, noun: &str) -> bool {
            lower
                .split(|c: char| !c.is_alphanumeric() && c != '_')
                .any(|word| {
                    word == noun
                        || word.starts_with(&format!("{noun}_"))
                        || word.ends_with(&format!("_{noun}"))
                        || word.contains(&format!("_{noun}_"))
                })
        }

        //NOTE: "scope" and "agent" are common English words.
        //"turn" and "note" are substrings of "return" and "annotation" —
        //substring matching would false-positive on legitimate Rust code.
        //Strategy: check lines starting with pub/fn/struct/enum/type for word-boundary matches.
        for line in contents.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("//") || trimmed.starts_with("///") {
                continue;
            }
            let is_decl = trimmed.starts_with("pub ")
                || trimmed.starts_with("fn ")
                || trimmed.starts_with("struct ")
                || trimmed.starts_with("enum ")
                || trimmed.starts_with("type ");
            if !is_decl {
                continue;
            }
            let lower = trimmed.to_lowercase();

            for noun in ["trajectory", "tenant"] {
                if inv3_declaration_has_word(&lower, noun) {
                    panic!(
                        "INVARIANT 3 VIOLATED in {path_str}: \
                         product concept `{noun}` in declaration:\n  {trimmed}\n\
                         Library vocabulary: coordinate, entity, event, outcome, \
                         gate, region, transition.\n\
                         See: REFERENCE.md."
                    );
                }
            }

            if inv3_declaration_has_word(&lower, "artifact") {
                let artifact_decl_ok_in_lib =
                    path_str == "src/lib.rs" && trimmed == "pub mod artifact;";
                let artifact_decl_ok_in_prelude =
                    path_str == "src/prelude.rs" && trimmed.starts_with("pub use crate::artifact");
                if !(path_str == INV3_ARTIFACT_ALLOWED_PATH
                    || artifact_decl_ok_in_lib
                    || artifact_decl_ok_in_prelude)
                {
                    panic!(
                        "INVARIANT 3 VIOLATED in {path_str}: \
                         product concept `artifact` in declaration:\n  {trimmed}\n\
                         Lane-A exception: definitions only in `{INV3_ARTIFACT_ALLOWED_PATH}`; \
                         crate wiring may use `pub mod artifact;` in `src/lib.rs` and \
                         `pub use crate::artifact::{{...}}` in `src/prelude.rs`.\n\
                         See: REFERENCE.md."
                    );
                }
            }
        }
    });
}

fn check_store_surface_honesty() {
    let store_mod =
        fs::read_to_string("src/store/mod.rs").expect("read src/store/mod.rs for surface check");
    if store_mod.contains("pub fn subscribe(") {
        panic!(
            "PUBLIC API HONESTY VIOLATION: src/store/mod.rs still exports `pub fn subscribe(`.\n\
             The lossy broadcast API must be named `subscribe_lossy` so callers cannot\n\
             confuse it with guaranteed delivery."
        );
    }
    if store_mod.contains("pub fn cursor(") {
        panic!(
            "PUBLIC API HONESTY VIOLATION: src/store/mod.rs still exports `pub fn cursor(`.\n\
             The guaranteed replay API must be named `cursor_guaranteed`."
        );
    }
    if store_mod.contains("Freshness::BestEffort") || store_mod.contains("BestEffort") {
        panic!(
            "PUBLIC API HONESTY VIOLATION: stale `Freshness::BestEffort` reference in src/store/mod.rs.\n\
             Use `Freshness::MaybeStale {{ max_stale_ms }}`."
        );
    }

    walk_rs_files(Path::new("src/store"), &mut |path, contents| {
        let path_str = repo_relative_display(path);
        if contents.contains("test-support") {
            panic!(
                "FEATURE HONESTY VIOLATION in {path_str}: stale `test-support` reference.\n\
                 The explicit risk-bearing feature name is `dangerous-test-hooks`."
            );
        }
    });
}

fn check_no_fixed_temp_patterns() {
    walk_rs_files(Path::new("src/store"), &mut |path, contents| {
        let path_str = repo_relative_display(path);
        if contents.contains("index.ckpt.tmp") || contents.contains(".tmp_{pid}_{n}") {
            panic!(
                "TEMP FILE HARDENING VIOLATION in {path_str}: fixed temp-file pattern found.\n\
                 Use same-directory `tempfile::NamedTempFile` instead of predictable names."
            );
        }
        if contents.contains("create(true)") && contents.contains("truncate(true)") {
            panic!(
                "TEMP FILE HARDENING VIOLATION in {path_str}: `create(true)` + `truncate(true)` found.\n\
                 This is the symlink-clobber shape the release hardening pass bans in src/store."
            );
        }
    });
}

fn check_store_config_field_usage() {
    // Invariant: every pub field in StoreConfig must be read somewhere in src/.
    // This catches "config field defined but never wired up" bugs like the
    // historical writer.stack_size and sync.mode regressions.
    // This is part of the live configuration completeness contract.
    let config_src = fs::read_to_string("src/store/config.rs")
        .expect("read src/store/config.rs for config check");

    let config_ast = syn::parse_file(&config_src)
        .expect("parse src/store/config.rs for config field usage check");
    let fields = store_config_public_fields(&config_ast);
    if fields.is_empty() {
        return;
    }

    let mut used_fields = BTreeSet::new();
    walk_rs_files(Path::new("src"), &mut |path, contents| {
        if path
            .to_string_lossy()
            .replace('\\', "/")
            .ends_with("src/store/config.rs")
        {
            return;
        }
        let file = syn::parse_file(contents).unwrap_or_else(|err| {
            fail(&format!(
                "CONFIG FIELD USAGE CHECK PARSE FAILURE in {}: {err}",
                path.display()
            ))
        });
        let mut collector = StoreConfigFieldAccessCollector::new(&fields);
        collector.visit_file(&file);
        used_fields.extend(collector.found_fields);
    });

    for field in &fields {
        if !used_fields.contains(field) {
            panic!(
                "STORE CONFIG FIELD UNUSED: `{field}` is defined in StoreConfig but never \
                 accessed in any parsed src/ file outside src/store/config.rs.\n\
                 Every config field must be wired to actual behavior.\n\
                 Either use the field or remove it from StoreConfig.\n\
                 See: the historical writer.stack_size / sync.mode bugs that slipped through review."
            );
        }
    }
}

fn store_config_public_fields(file: &syn::File) -> BTreeSet<String> {
    for item in &file.items {
        if let syn::Item::Struct(item_struct) = item {
            if item_struct.ident == "StoreConfig" {
                let mut fields = BTreeSet::new();
                for field in &item_struct.fields {
                    if matches!(field.vis, syn::Visibility::Public(_)) {
                        if let Some(ident) = &field.ident {
                            fields.insert(ident.to_string());
                        }
                    }
                }
                return fields;
            }
        }
    }
    BTreeSet::new()
}

struct StoreConfigFieldAccessCollector<'a> {
    tracked_fields: &'a BTreeSet<String>,
    found_fields: BTreeSet<String>,
}

impl<'a> StoreConfigFieldAccessCollector<'a> {
    fn new(tracked_fields: &'a BTreeSet<String>) -> Self {
        Self {
            tracked_fields,
            found_fields: BTreeSet::new(),
        }
    }
}

impl Visit<'_> for StoreConfigFieldAccessCollector<'_> {
    fn visit_expr_field(&mut self, node: &syn::ExprField) {
        if let syn::Member::Named(ident) = &node.member {
            let field_name = ident.to_string();
            if self.tracked_fields.contains(&field_name) {
                self.found_fields.insert(field_name);
            }
        }
        syn::visit::visit_expr_field(self, node);
    }
}

/// Public-surface post-mortem defense: every public item in src/ must have at least
/// one real path-position reference in a test file (verified via AST walk, not
/// substring match). Allowlisted items must declare a `witness:` path that the
/// AST walker confirms contains a real reference — a bogus witness fails the
/// build. LAW-003 (No Orphan Infrastructure), FM-007 (Island Syndrome).
fn check_pub_items_have_tests() {
    let allowlist = load_pub_item_allowlist();
    let allowed_names: BTreeSet<&str> = allowlist.iter().map(|entry| entry.name.as_str()).collect();

    // Validate every allowlist entry's witness paths via the AST walker. A
    // single real reference in the referenced file is sufficient. Bogus
    // witnesses (path missing, file does not reference the item) fail here.
    for entry in &allowlist {
        if entry.witness.is_empty() {
            fail(&format!(
                "pub_item_allowlist entry `{}` must declare at least one `witness:` path pointing at a test that uses the item; narrative `justification:` is supplementary, not load-bearing",
                entry.name
            ));
        }
        for witness in &entry.witness {
            if !witness.path.starts_with("tests/")
                && !witness.path.starts_with("crates/core/tests/")
            {
                fail(&format!(
                    "pub_item_allowlist entry `{}` witness `{}` must point at a file under tests/, not production code",
                    entry.name, witness.path
                ));
            }
            if witness.lines.is_empty() {
                fail(&format!(
                    "pub_item_allowlist entry `{}` witness `{}` must include at least one concrete line hint",
                    entry.name, witness.path
                ));
            }
            let witness_path = resolve_repo_or_core_path(&witness.path);
            let content = match fs::read_to_string(&witness_path) {
                Ok(c) => c,
                Err(err) => fail(&format!(
                    "pub_item_allowlist entry `{}` witness `{}` cannot be read: {err}",
                    entry.name, witness.path
                )),
            };
            let file = match syn::parse_file(&content) {
                Ok(f) => f,
                Err(err) => fail(&format!(
                    "pub_item_allowlist entry `{}` witness `{}` does not parse: {err}",
                    entry.name, witness.path
                )),
            };
            if !shared_checks::ast_references_name(&file, &entry.name) {
                fail(&format!(
                    "pub_item_allowlist entry `{}` witness `{}` (line hints {:?}) has no real path-position reference to the item; update the witness path or hide the item via `#[doc(hidden)]`",
                    entry.name, witness.path, witness.lines,
                ));
            }
        }
    }

    // Parse every test file once.
    let test_files: Vec<(std::path::PathBuf, syn::File)> = collect_rs_file_asts(Path::new("tests"));

    // Walk src/ and collect public item names from the parsed AST, then
    // confirm each name has at least one real path-position reference in the
    // parsed tests.
    walk_rs_files(Path::new("src"), &mut |path, contents| {
        if path.ends_with("prelude.rs") {
            return;
        }
        let path_str = repo_relative_display(path);
        let file = syn::parse_file(contents).unwrap_or_else(|err| {
            fail(&format!(
                "PUB ITEM REFERENCE CHECK PARSE FAILURE in {path_str}: {err}\n\
                 This detector is syntax-aware by design; fix the source or the parser input."
            ))
        });
        for name in shared_checks::public_item_names(&file) {
            if allowed_names.contains(name.as_str()) {
                continue;
            }
            let witnessed = test_files
                .iter()
                .any(|(_, ast)| shared_checks::ast_references_name(ast, &name));
            if !witnessed {
                let (line, _col) = item_line_in_file(contents, &name);
                fail(&format!(
                    "pub item `{name}` declared at {path_str}:{line} has no test reference (checked {n} test files via AST); either add a real test use, add an allowlist entry with a `witness:` path that points to an actual use, or hide the item via `#[doc(hidden)]`.",
                    n = test_files.len(),
                ));
            }
        }
    });
}

fn collect_rs_file_asts(dir: &Path) -> Vec<(std::path::PathBuf, syn::File)> {
    let mut out = Vec::new();
    let entries = fs::read_dir(dir).unwrap_or_else(|err| {
        fail(&format!(
            "cannot read {} while collecting test witness ASTs: {err}",
            dir.display()
        ))
    });
    for entry in entries {
        let entry = entry.unwrap_or_else(|err| {
            fail(&format!(
                "cannot walk {} while collecting test witness ASTs: {err}",
                dir.display()
            ))
        });
        let path = entry.path();
        if path.is_dir() {
            out.extend(collect_rs_file_asts(&path));
        } else if path.extension().map(|e| e == "rs").unwrap_or(false) {
            let contents = fs::read_to_string(&path).unwrap_or_else(|err| {
                fail(&format!(
                    "cannot read {} while collecting test witness ASTs: {err}",
                    path.display()
                ))
            });
            let file = syn::parse_file(&contents).unwrap_or_else(|err| {
                fail(&format!(
                    "cannot parse {} while collecting test witness ASTs: {err}",
                    path.display()
                ))
            });
            out.push((path, file));
        }
    }
    out
}

fn item_line_in_file(contents: &str, name: &str) -> (usize, usize) {
    for (idx, line) in contents.lines().enumerate() {
        if line.contains(name) {
            return (idx + 1, line.find(name).unwrap_or(0) + 1);
        }
    }
    (0, 0)
}

fn load_pub_item_allowlist() -> Vec<PubItemAllowlistEntry> {
    let repo_root = repo_root();
    let path = repo_root.join("traceability/pub_item_allowlist.yaml");
    let contents = fs::read_to_string(&path)
        .unwrap_or_else(|err| fail(&format!("failed to read {}: {err}", path.display())));
    let entries: Vec<PubItemAllowlistEntry> = yaml_serde::from_str(&contents)
        .unwrap_or_else(|err| fail(&format!("failed to parse {}: {err}", path.display())));
    for entry in &entries {
        if entry.name.trim().is_empty() {
            fail(&format!(
                "invalid {} entry: `name` must not be empty (justification: {})",
                path.display(),
                entry.justification
            ));
        }
        if entry.justification.trim().is_empty() {
            fail(&format!(
                "invalid {} entry for `{}`: `justification` must not be empty",
                path.display(),
                entry.name
            ));
        }
    }
    entries
}

fn resolve_repo_or_core_path(path: &str) -> PathBuf {
    let path = Path::new(path);
    let repo_path = repo_root().join(path);
    if repo_path.exists() {
        return repo_path;
    }
    if path.starts_with("src")
        || path.starts_with("tests")
        || path.starts_with("examples")
        || path.starts_with("benches")
        || path.starts_with("fixtures")
        || path == Path::new("build.rs")
    {
        return core_root().join(path);
    }
    repo_path
}

fn walk_rs_files(dir: &Path, check: &mut dyn FnMut(&Path, &str)) {
    //Recursive directory walk. Only reads .rs files.
    //Uses std::fs only — no external deps allowed in build scripts
    //unless declared in [build-dependencies].
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                walk_rs_files(&path, check);
            } else if path.extension().map(|e| e == "rs").unwrap_or(false) {
                if let Ok(contents) = fs::read_to_string(&path) {
                    check(&path, &contents);
                }
            }
        }
    }
}

fn walk_allow_checked_rs_files(check: &mut dyn FnMut(&Path, &str)) {
    let core_root = core_root();
    let repo_root = repo_root();
    let roots = [
        core_root.join("build.rs"),
        core_root.join("src"),
        repo_root.join("tools/xtask/src"),
        repo_root.join("tools/integrity/src"),
    ];
    for root in &roots {
        if root.is_file() {
            if let Ok(contents) = fs::read_to_string(root) {
                check(root, &contents);
            }
        } else {
            walk_rs_files(root, check);
        }
    }
}

fn walk_dead_code_checked_rs_files(check: &mut dyn FnMut(&Path, &str)) {
    let core_root = core_root();
    let repo_root = repo_root();
    let roots = [
        core_root.join("build.rs"),
        core_root.join("src"),
        core_root.join("tests"),
        core_root.join("examples"),
        core_root.join("benches"),
        repo_root.join("tools/xtask/src"),
        repo_root.join("tools/integrity/src"),
        repo_root.join("crates/macros/src"),
        repo_root.join("crates/macros-support/src"),
    ];
    for root in &roots {
        if root.is_file() {
            if let Ok(contents) = fs::read_to_string(root) {
                check(root, &contents);
            }
        } else {
            walk_rs_files(root, check);
        }
    }
}