batpak 0.2.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
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
# SPEC_REGISTRY — Portable Context for Parallel Agent Execution

Status note (2026-04-04): **This registry is historical.** 24 commits since
2026-04-03 added extensive new features (group commit, checkpoint v2, mmap
reader, 6 index layouts, SIDX footers, incremental projection, schema
versioning, watch_projection, etc.). The StoreConfig, StoreError, IndexEntry,
Store method list, and WriterCommand shapes below are from the pre-Keller-Cut
era. Consult `src/store/mod.rs` for the current API surface and
`docs/reference/ARCHITECTURE.md` + `docs/reference/TUNING.md` for current docs.

```
WHAT THIS IS:
  Self-contained file-level build contexts. Each H2 section is ONE file.
  An agent loads this section + the DEPENDENCY SURFACE below, writes the file, done.
  No compiler access. No discovery. No guessing. Execute from context.

HOW TO USE:
  1. Read the DEPENDENCY SURFACE section (always load this — it's the shared type atlas)
  2. Grep for the file you're assigned: ## src/wire.rs
  3. Read from that H2 to the next H2 (or EOF)
  4. Everything you need is in that section:
     - CONSTRAINTS: what NOT to do (read these FIRST, before any code)
     - IMPORTS: exact use statements (copy verbatim, do NOT substitute from memory)
     - SHAPES: type signatures for every cross-file type this file touches
     - TYPES: exact struct/enum/trait definitions for this file
     - IMPL: pseudo-code with ///why comments for every method
     - TESTS: what the test suite asserts about this file
  5. Write the file. Do not run cargo. Do not compile. Do not test.
  6. The /// comments are prompts. They explain WHY. Code blocks explain WHAT.

FORMAT:
  ## path/to/file.rs            ← H2 = file path (grepable)
  >[filename.rs]                ← end marker (grep from ## to >[] = full context)

CROSS-REFERENCES:
  [SPEC:section_name]          ← refers to SPEC.md ## section_name
  [DEP:crate::path::fn]        ← refers to DEPENDENCY SURFACE section below (NOT the web)
  [FILE:path/to/other.rs]      ← refers to another file section in this registry

ANTI-DRIFT PRINCIPLES (read these if you feel yourself improvising):
  - If a type isn't in SHAPES or DEPENDENCY SURFACE, you don't know its shape. Stop.
  - If an API isn't in DEPENDENCY SURFACE, you don't know if it exists. Stop.
  - If you're about to write a struct/enum that isn't in TYPES, you're inventing. Stop.
  - If your code compiles in your head but contradicts a CONSTRAINT, the constraint wins.
  - "Seems right" is not "is right." Check SHAPES. Check DEPENDENCY SURFACE.
```

---

## DEPENDENCY SURFACE

Exact API signatures for every external crate function/type we call.
This section is GROUND TRUTH. If it contradicts your training data, this wins.
Verified against cargo doc output for the pinned versions in Cargo.toml.

```
=== flume 0.12 ===

flume::bounded<T>(cap: usize) -> (Sender<T>, Receiver<T>)
flume::unbounded<T>() -> (Sender<T>, Receiver<T>)

Sender<T>: Clone + Send + Sync (where T: Send)
  fn send(&self, msg: T) -> Result<(), SendError<T>>       // blocks if bounded+full
  fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> // never blocks
  fn is_disconnected(&self) -> bool

  TrySendError<T>:
    Full(T)           — channel at capacity, message returned
    Disconnected(T)   — all receivers dropped, message returned

  SendError<T>(pub T) — all receivers dropped

Receiver<T>: Clone + Send + Sync (where T: Send)
  fn recv(&self) -> Result<T, RecvError>                    // blocks until message
  fn recv_async(&self) -> RecvFut<'_, T>                 // async, runtime-agnostic
  fn try_recv(&self) -> Result<T, TryRecvError>
  fn iter(&self) -> Iter<'_, T>                             // blocking iterator

  RecvError — unit struct, all senders dropped + channel empty
  TryRecvError: Empty | Disconnected

  RecvFut<'a, T>: Future<Output = Result<T, RecvError>>
    // works with any async runtime (tokio, async-std, smol)
    // no tokio dependency — uses std::task::Waker

=== rmp_serde 1 ===

rmp_serde::to_vec_named<T: Serialize>(val: &T) -> Result<Vec<u8>, rmp_serde::encode::Error>
  // ALWAYS use to_vec_named, NEVER to_vec. [SPEC:WIRE FORMAT DECISIONS]
  // Named mode preserves field names as msgpack map keys.
  // to_vec serializes as positional arrays — breaks on field reorder.

rmp_serde::from_slice<T: Deserialize>(buf: &[u8]) -> Result<T, rmp_serde::decode::Error>
  // Handles both named and positional input.

=== crc32fast 1 ===

crc32fast::hash(data: &[u8]) -> u32                // one-shot CRC32
crc32fast::Hasher::new() -> Hasher                  // incremental
  fn update(&mut self, data: &[u8])
  fn finalize(self) -> u32

  // Uses CRC-32/ISO-HDLC (standard zlib/gzip/PNG CRC32)
  // Hardware-accelerated on x86_64 (SSE4.2 PCLMULQDQ)

=== blake3 1 (behind feature = "blake3") ===

blake3::hash(input: &[u8]) -> blake3::Hash
  Hash::into() -> [u8; 32]                          // via From<Hash> for [u8; 32]
  // Usage: let bytes: [u8; 32] = blake3::hash(data).into();

=== uuid 1 (features = ["v7"]) ===

uuid::Uuid::now_v7() -> Uuid                        // timestamp + random
  fn as_u128(&self) -> u128                          // big-endian u128
  // Used ONLY inside define_entity_id! macro. Never in public API.

=== parking_lot 0.12 ===

parking_lot::Mutex<T>: Send + Sync (where T: Send)
  fn lock(&self) -> MutexGuard<'_, T>                // NO Result, NO poisoning
  fn try_lock(&self) -> Option<MutexGuard<'_, T>>
  // 1 byte overhead (vs ~40 bytes for std::sync::Mutex)

=== dashmap 5 ===

DashMap<K, V>: Send + Sync (where K: Eq + Hash + Send + Sync, V: Send + Sync)
  fn insert(&self, key: K, value: V) -> Option<V>
  fn get(&self, key: &Q) -> Option<Ref<'_, K, V>>   // where K: Borrow<Q>
  fn get_mut(&self, key: &Q) -> Option<RefMut<'_, K, V>>
  fn entry(&self, key: K) -> Entry<'_, K, V>         // holds WRITE lock on shard
  fn contains_key(&self, key: &Q) -> bool
  fn remove(&self, key: &Q) -> Option<(K, V)>
  fn iter(&self) -> Iter<'_, K, V>                   // NOT a consistent snapshot
  fn len(&self) -> usize

  Ref<'a, K, V>: Deref<Target = V>
    fn key(&self) -> &K
    fn value(&self) -> &V
    // DROPS the shard read-lock when Ref is dropped.
    // DEADLOCK RISK: do not hold Ref while calling insert/remove/entry on same map.

  Entry<'a, K, V>:
    fn or_insert(self, default: V) -> RefMut<'_, K, V>
    fn or_insert_with(self, f: impl FnOnce() -> V) -> RefMut<'_, K, V>
    // Holds WRITE lock for its entire lifetime. Release fast.

  Arc<str> as key: works. Eq + Hash delegate to inner str.
  Lookup with &str: works via K: Borrow<Q> where Arc<str>: Borrow<str>.

=== tracing 0.1 ===

tracing::trace!(field = value, "message")            // most verbose
tracing::debug!(field = value, "message")
tracing::info!(field = value, "message")
tracing::warn!(field = value, "message")
tracing::error!(field = value, "message")            // least verbose
  // %value = Display, ?value = Debug, value = native
  // No-op when no subscriber installed. Near-zero overhead.

=== serde 1 ===

#[derive(Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]             // adjacent tagging for enums
#[serde(with = "module_path")]                       // custom ser/de
#[serde(skip_serializing_if = "Option::is_none")]    // omit None fields
#[serde(default)]                                    // fill missing fields with Default

  Serializer trait — we use: serialize_bytes, serialize_none, serialize_seq
  Deserializer trait — we use: deserialize_bytes, deserialize_option, deserialize_seq
  Visitor trait — we implement: visit_bytes, visit_seq, visit_none, visit_some

=== std (Rust 1.92 MSRV) ===

Arc<str>: Clone + Send + Sync + Eq + Hash + Deref<Target = str>
  Arc::from("string_literal") -> Arc<str>
  // Borrow<str> implemented — can use &str for HashMap/DashMap lookups

AtomicU64: Send + Sync
  fn fetch_add(&self, val: u64, order: Ordering) -> u64
  fn load(&self, order: Ordering) -> u64
  fn store(&self, val: u64, order: Ordering)

std::os::unix::fs::FileExt (stable since 1.15):
  fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>  // pread
  fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>     // pwrite
  // File::create_new() available since 1.77 (within MSRV 1.92).

std::sync::OnceLock<T> (stable since 1.70):
  fn get_or_init(&self, f: impl FnOnce() -> T) -> &T
  // LazyLock available since 1.80 (within MSRV 1.92).
  // get_or_try_init available since 1.82 (within MSRV 1.92).
```

---

## INTERNAL TYPE ATLAS

Shapes of every cross-file type. When your file imports a type from another
module, its shape is here. If you need a field or method, check this section.
Do NOT invent fields. Do NOT guess method signatures.

```
EventKind(u16)                     — PRIVATE inner. No .0 access from outside event/.
  ::custom(category: u8, type_id: u16) -> Self
  .category() -> u8
  .type_id() -> u16
  .is_system() -> bool
  .is_effect() -> bool
  Constants: DATA, SYSTEM_INIT, SYSTEM_SHUTDOWN, SYSTEM_HEARTBEAT,
    SYSTEM_CONFIG_CHANGE, SYSTEM_CHECKPOINT, EFFECT_ERROR, EFFECT_RETRY,
    EFFECT_ACK, EFFECT_BACKPRESSURE, EFFECT_CANCEL, EFFECT_CONFLICT

Coordinate { entity: Arc<str>, scope: Arc<str> }   — fields PRIVATE
  ::new(entity: impl AsRef<str>, scope: impl AsRef<str>) -> Result<Self, CoordinateError>
  .entity() -> &str
  .scope() -> &str
  pub(crate) .entity_arc() -> Arc<str>
  pub(crate) .scope_arc() -> Arc<str>
  Display: "entity@scope"

CoordinateError: EmptyEntity | EmptyScope          — impl Display, Error

DagPosition { pub wall_ms: u64, pub counter: u16, pub depth: u32, pub lane: u32, pub sequence: u32 }  — repr(C)
  ::new(depth, lane, sequence) -> Self              // wall_ms=0, counter=0
  ::with_hlc(wall_ms, counter, depth, lane, sequence) -> Self
  ::root() -> Self                    // all zeros
  ::child(sequence: u32) -> Self      // depth=0, lane=0, wall_ms=0
  ::child_at(sequence, wall_ms, counter) -> Self    // v1 with HLC
  ::fork(parent_depth, new_lane) -> Self
  .is_root() -> bool
  .is_ancestor_of(&DagPosition) -> bool
  Display: "depth:lane:sequence@wall_ms.counter"

Region { entity_prefix, scope, fact, clock_range } — all Option, all pub
  ::all() -> Self (Default)
  ::entity(prefix) -> Self
  ::scope(scope) -> Self
  ::coordinate(&Coordinate) -> Self
  .with_scope(s) -> Self
  .with_fact(KindFilter) -> Self
  .with_fact_category(u8) -> Self
  .with_clock_range((u32, u32)) -> Self
  .matches_event(entity: &str, scope: &str, kind: EventKind) -> bool

KindFilter: Exact(EventKind) | Category(u8) | Any

HashChain { pub prev_hash: [u8; 32], pub event_hash: [u8; 32] }
  Default: all zeros (genesis convention)

EventHeader { event_id, correlation_id, causation_id, timestamp_us: i64,
              position, payload_size, event_kind, flags: u8, content_hash: [u8; 32] }
  All fields pub. Serde annotations on u128 fields. content_hash has #[serde(default)].
  ::new(event_id, correlation_id, causation_id, timestamp_us, position, payload_size, event_kind) -> Self
    // flags defaults to 0, content_hash defaults to [0u8; 32]
  .with_flags(u8) -> Self
  .requires_ack() -> bool    // flag bit 0
  .is_transactional() -> bool // flag bit 1
  .is_replay() -> bool       // flag bit 3
  .age_us(now_us: i64) -> u64

Event<P> { pub header: EventHeader, pub payload: P, pub hash_chain: Option<HashChain> }
  ::new(header, payload) -> Self
  .with_hash_chain(HashChain) -> Self
  .event_id() -> u128
  .event_kind() -> EventKind
  .position() -> &DagPosition
  .is_genesis() -> bool
  .map_payload(f) -> Event<U>

StoredEvent<P> { pub coordinate: Coordinate, pub event: Event<P> }

Outcome<T>: Ok(T) | Err(OutcomeError) | Retry{..} | Pending{..} | Cancelled{..} | Batch(Vec<Outcome<T>>)
  .map(f) .and_then(f) .map_err(f) .or_else(f) .flatten()
  .is_ok() .is_err() .is_retry() .is_pending() .is_cancelled() .is_batch() .is_terminal()
  .into_result() -> Result<T, OutcomeError>

OutcomeError { pub kind: ErrorKind, pub message: String, pub compensation: Option<CompensationAction>, pub retryable: bool }
ErrorKind: NotFound | Conflict | Validation | PolicyRejection | StorageError | Timeout | Serialization | Internal | Custom(u16)

Gate<Ctx>: trait, Send + Sync
  .name() -> &'static str
  .evaluate(&Ctx) -> Result<(), Denial>

GateSet<Ctx> { gates: Vec<Box<dyn Gate<Ctx>>> }
  .push(gate)
  .evaluate(ctx, proposal) -> Result<Receipt<T>, Denial>  // fail-fast
  .evaluate_all(ctx) -> Vec<Denial>                        // no fail-fast

Denial { pub gate: &'static str, pub code: String, pub message: String, pub context: Vec<(String, String)> }
  Serialize only — NOT Deserialize (&'static str can't deser from owned data)
  ::new(gate, message) .with_code(code) .with_context(key, value)

Receipt<T> { _seal: seal::Token, gates_passed: Vec<&'static str>, payload: T }
  NOT Clone. NOT Copy. NOT Serialize. Consumed once.
  .payload() -> &T
  .gates_passed() -> &[&'static str]
  .into_parts() -> (T, Vec<&'static str>)           // consuming
  Only constructible via GateSet::evaluate(). seal::Token is pub(crate).

Proposal<T>(pub T)
  ::new(payload) .payload() .map(f)

Committed<T> { pub payload: T, pub event_id: u128, pub sequence: u64, pub hash: [u8; 32] }

Pipeline<Ctx> { gates: GateSet<Ctx> }
  ::new(gates)
  .evaluate(ctx, proposal) -> Result<Receipt<T>, Denial>
  .commit<E>(receipt, f: impl FnOnce(T) -> Result<Committed<T>, E>) -> Result<Committed<T>, E>

Transition<From, To, P> { kind: EventKind, payload: P, _from: PhantomData, _to: PhantomData }
  ::new(kind, payload) .kind() .payload() .into_payload()

EntityIdType: trait (Copy + Clone + Eq + Hash + Debug + Display + FromStr + Send + Sync)
  ::new(u128) .as_u128() .now_v7() .nil()

=== STORE TYPES ===

Store { index: Arc<StoreIndex>, reader: Arc<Reader>, cache: Box<dyn ProjectionCache>,
        writer: WriterHandle, config: Arc<StoreConfig> }
  Store: Send + Sync (all fields are Send + Sync)
  ::open(config: StoreConfig) -> Result<Self, StoreError>
  ::open_default() -> Result<Self, StoreError>     // ./batpak-data/
  .append(&self, coord, kind, payload: &impl Serialize) -> Result<AppendReceipt, StoreError>
  .append_reaction(&self, coord, kind, payload, correlation_id: u128, causation_id: u128) -> Result<AppendReceipt, StoreError>
  .get(event_id: u128) -> Result<StoredEvent<serde_json::Value>, StoreError>
  .query(region: &Region) -> Vec<IndexEntry>
  .walk_ancestors(event_id: u128, limit: usize) -> Vec<StoredEvent<Value>>
  .project<T: EventSourced<Value>>(entity: &str, freshness: Freshness) -> Result<Option<T>, StoreError>
  .subscribe(region: &Region) -> Subscription
  .cursor(region: &Region) -> Cursor
  .stream(entity) .by_scope(scope) .by_fact(kind)  // convenience sugar
  .sync() .close(self) .stats() -> StoreStats

StoreConfig { data_dir: PathBuf, segment_max_bytes: u64, sync_every_n_events: u32,
              fd_budget: usize, writer_channel_capacity: usize, broadcast_capacity: usize,
              cache_map_size_bytes: usize, restart_policy: RestartPolicy, shutdown_drain_limit: usize,
              writer_stack_size: Option<usize>, clock: Option<Arc<dyn Fn() -> i64 + Send + Sync>>,
              sync_mode: SyncMode }
  All pub. No Default — use StoreConfig::new(data_dir) with sane defaults, then override fields.
  Manual Clone and Debug impls because `clock` is `Arc<dyn Fn>`.
  SyncMode: SyncAll (default) | SyncData.

StoreError: Io(io::Error) | Coordinate(CoordinateError) | Serialization(String)
  | CrcMismatch{segment_id,offset} | CorruptSegment{segment_id,detail}
  | NotFound(u128) | SequenceMismatch{entity,expected,actual}
  | DuplicateEvent(u128) | WriterCrashed | ShuttingDown | CacheFailed(String)
  impl Display, Error, From<CoordinateError>, From<io::Error>

AppendReceipt { pub event_id: u128, pub sequence: u64, pub disk_pos: DiskPos }

AppendOptions { pub expected_sequence: Option<u32>, pub idempotency_key: Option<u128>,
                pub correlation_id: Option<u128>, pub causation_id: Option<u128> }
  impl Default (all None)

RestartPolicy: Once | Bounded { max_restarts: u32, within_ms: u64 }
  impl Default → Once. EXACTLY two variants. [SPEC:RED FLAGS]

StoreIndex — pub(crate). Fields: streams, scope_entities, by_fact, by_id, latest,
  global_sequence: AtomicU64, len: AtomicUsize, entity_locks: DashMap
  .insert(entry) .get_by_id(u128) .get_latest(&str) .stream(&str) .query(&Region)
  .global_sequence() -> u64 .len() -> usize

IndexEntry { pub event_id: u128, pub correlation_id: u128, pub causation_id: Option<u128>,
             pub coord: Coordinate, pub kind: EventKind, pub wall_ms: u64, pub clock: u32,
             pub hash_chain: HashChain, pub disk_pos: DiskPos, pub global_sequence: u64 }
  .is_correlated() -> bool    (event_id != correlation_id)
  .is_caused_by(u128) -> bool (causation_id == Some(id))
  .is_root_cause() -> bool    (causation_id.is_none())

ClockKey { pub wall_ms: u64, pub clock: u32, pub uuid: u128 }
  impl Ord: wall_ms first, then clock, then uuid tiebreak. [SPEC:IMPLEMENTATION NOTES item 1]

DiskPos { pub segment_id: u64, pub offset: u64, pub length: u32 }

WriterHandle — pub(crate) { pub tx: Sender<WriterCommand>, pub subscribers: Arc<SubscriberList>, thread }
  ::spawn(config, index, subscribers) -> Result<Self, StoreError>
  .tx is pub(crate) — Store sends commands directly, no wrapper method.

WriterCommand: Append{entity,scope,event,kind,correlation_id,causation_id,respond}
  | Sync{respond} | Shutdown{respond}

SubscriberList { senders: Mutex<Vec<Sender<Notification>>> }
  .subscribe(capacity) -> Receiver<Notification>
  .broadcast(Notification)  // try_send, retain on Ok|Full, prune on Disconnected

Notification: Clone + Debug
  { pub event_id: u128, pub correlation_id: u128, pub causation_id: Option<u128>,
    pub coord: Coordinate, pub kind: EventKind, pub sequence: u64 }

Cursor { region: Region, position: u64, index: Arc<StoreIndex> }
  .poll() -> Option<IndexEntry>
  .poll_batch(max: usize) -> Vec<IndexEntry>

Subscription { rx: Receiver<Notification>, region: Region }
  .recv() -> Option<Notification>               // sync, blocks, filters by region
  .receiver() -> &Receiver<Notification>         // for async: rx.recv_async().await

ProjectionCache: trait (Send + Sync + 'static)
  .get(key: &[u8]) -> Result<Option<(Vec<u8>, CacheMeta)>, StoreError>
  .put(key: &[u8], value: &[u8], meta: CacheMeta) -> Result<(), StoreError>
  .delete_prefix(prefix: &[u8]) -> Result<u64, StoreError>
  .sync() -> Result<(), StoreError>

CacheMeta { pub watermark: u64, pub cached_at_us: i64 }
Freshness: Consistent | BestEffort { max_stale_ms: u64 }
NoCache — default, always miss, forces replay from segments

SegmentHeader { pub version: u16, pub flags: u16, pub created_ns: i64, pub segment_id: u64 }
FramePayload<P> { pub event: Event<P>, pub entity: String, pub scope: String }
Segment<Active> — writable. Segment<Sealed> — immutable. Typestate transition via .seal()

StoreStats { pub event_count: usize, pub global_sequence: u64 }
```

---

## build.rs

IMPORTS:
```rust
use std::fs;
use std::path::Path;
```

TYPES: none (build script, not a library module)

IMPL:
```rust
/// build.rs runs before every cargo build/check/test. Cannot be skipped.
/// It enforces SPEC invariants at build time so agents get English errors
/// instead of cryptic compiler failures. [SPEC:INVARIANTS]

fn main() {
    println!("cargo:rerun-if-changed=Cargo.toml");
    println!("cargo:rerun-if-changed=src/");

    check_no_tokio_in_deps();
    check_no_banned_patterns();
}

fn check_no_tokio_in_deps() {
    /// Invariant 1: tokio must not appear in [dependencies].
    /// Only [dev-dependencies] is allowed. [SPEC:INVARIANTS item 1]
    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: SPEC.md ## INVARIANTS, item 1."
            );
        }
    }
}

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

        /// Red flag: no transmute/mem::read/pointer_cast in any src file.
        /// All serialization goes through MessagePack. [SPEC:RED FLAGS item 1]
        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: SPEC.md ## RED FLAGS, item 1."
                );
            }
        }

        /// Invariant 2: no async fn in store module.
        /// Store API is sync. Async lives in flume channels. [SPEC:INVARIANTS item 2]
        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: SPEC.md ## INVARIANTS, item 2."
            );
        }

        /// Invariant 3: no product concepts in library code.
        /// Check struct/enum/fn/type declarations for banned nouns.
        /// Skip string literals and comments. [SPEC:INVARIANTS item 3]
        let banned_nouns = [
            "trajectory", "artifact", "tenant",
        ];
        /// 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.
        /// Only check nouns that are unambiguous product concepts.
        /// Strategy: check lines starting with pub/fn/struct/enum/type
        /// for WORD-BOUNDARY matches of banned nouns.
        for line in contents.lines() {
            let trimmed = line.trim();
            if trimmed.starts_with("//") || trimmed.starts_with("///") {
                continue; // skip comments
            }
            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 {
                let lower = trimmed.to_lowercase();
                for noun in &banned_nouns {
                    /// Word boundary check: noun must be preceded by start/underscore/space
                    /// and followed by end/underscore/space/(/>. Prevents "return" matching "turn".
                    let has_match = 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}_"))
                        });
                    if has_match {
                        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: SPEC.md ## INVARIANTS, item 3."
                        );
                    }
                }
            }
        }
    });
}

fn walk_rs_files(dir: &Path, check: &dyn Fn(&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);
                }
            }
        }
    }
}
```

TESTS: build.rs is tested implicitly — if invariants are violated, cargo build fails.

>[build.rs]

---

## src/wire.rs

IMPORTS:
```rust
use serde::de::{self, Deserializer, SeqAccess, Visitor};
use serde::ser::Serializer;
use std::fmt;
```

TYPES: three submodules, no structs.

IMPL:
```rust
/// Serde helpers for u128 serialization as [u8; 16] big-endian.
/// MessagePack has no native u128 type. Bare u128 causes rmp-serde errors.
/// Big-endian preserves sort order and is standard network byte order.
/// [SPEC:WIRE FORMAT DECISIONS item 2]
///
/// ZERO internal dependencies. This module is declared FIRST in lib.rs.
/// Every serializable type with a u128 field uses these helpers.
/// [SPEC:BUILD ORDER STEP 4 — wire.rs is FIRST]

pub mod u128_bytes {
    /// Usage: #[serde(with = "crate::wire::u128_bytes")]
    /// Annotated on: EventHeader.event_id, EventHeader.correlation_id,
    ///   Notification.event_id, Notification.correlation_id,
    ///   Committed.event_id, WaitCondition::Event.event_id,
    ///   CompensationAction::Notify.target_id, Outcome::Pending.resume_token

    pub fn serialize<S: Serializer>(val: &u128, ser: S) -> Result<S::Ok, S::Error> {
        /// Convert to 16-byte big-endian array, serialize as bytes.
        /// [DEP:serde::Serializer::serialize_bytes]
        ser.serialize_bytes(&val.to_be_bytes())
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<u128, D::Error> {
        /// Accept bytes, convert from big-endian to u128.
        /// Use a Visitor that handles both byte arrays and sequences.
        /// [DEP:serde::de::Visitor]
        struct U128Visitor;
        impl<'de> Visitor<'de> for U128Visitor {
            type Value = u128;
            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str("16 bytes for u128")
            }
            fn visit_bytes<E: de::Error>(self, v: &[u8]) -> Result<u128, E> {
                /// v must be exactly 16 bytes. Convert via from_be_bytes.
                let arr: [u8; 16] = v.try_into().map_err(|_| {
                    E::invalid_length(v.len(), &"16 bytes")
                })?;
                Ok(u128::from_be_bytes(arr))
            }
            /// Also handle seq format (some deserializers emit sequences not bytes)
            fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<u128, A::Error> {
                let mut bytes = [0u8; 16];
                for (i, byte) in bytes.iter_mut().enumerate() {
                    *byte = seq.next_element()?
                        .ok_or_else(|| de::Error::invalid_length(i, &"16 bytes"))?;
                }
                Ok(u128::from_be_bytes(bytes))
            }
        }
        de.deserialize_bytes(U128Visitor)
    }
}

pub mod option_u128_bytes {
    /// Usage: #[serde(with = "crate::wire::option_u128_bytes")]
    /// Annotated on: EventHeader.causation_id, Notification.causation_id

    pub fn serialize<S: Serializer>(val: &Option<u128>, ser: S) -> Result<S::Ok, S::Error> {
        match val {
            Some(v) => ser.serialize_bytes(&v.to_be_bytes()),
            None => ser.serialize_none(),
        }
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<Option<u128>, D::Error> {
        /// Visitor that handles None (nil) and Some(bytes).
        struct OptU128Visitor;
        impl<'de> Visitor<'de> for OptU128Visitor {
            type Value = Option<u128>;
            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str("null or 16 bytes for u128")
            }
            fn visit_none<E: de::Error>(self) -> Result<Option<u128>, E> {
                Ok(None)
            }
            fn visit_some<D2: Deserializer<'de>>(self, de: D2) -> Result<Option<u128>, D2::Error> {
                super::u128_bytes::deserialize(de).map(Some)
            }
            fn visit_bytes<E: de::Error>(self, v: &[u8]) -> Result<Option<u128>, E> {
                let arr: [u8; 16] = v.try_into().map_err(|_| {
                    E::invalid_length(v.len(), &"16 bytes")
                })?;
                Ok(Some(u128::from_be_bytes(arr)))
            }
        }
        de.deserialize_option(OptU128Visitor)
    }
}

pub mod vec_u128_bytes {
    /// Usage: #[serde(with = "crate::wire::vec_u128_bytes")]
    /// Annotated on: CompensationAction::Rollback.event_ids,
    ///   CompensationAction::Release.resource_ids

    pub fn serialize<S: Serializer>(val: &[u128], ser: S) -> Result<S::Ok, S::Error> {
        /// Serialize as a sequence of [u8; 16] fixed-size arrays (NOT bytes).
        /// Using arrays ensures serialize and deserialize use the same msgpack
        /// format (array of arrays, not array of bin). Avoids format mismatch.
        use serde::ser::SerializeSeq;
        let mut seq = ser.serialize_seq(Some(val.len()))?;
        for v in val {
            seq.serialize_element(&v.to_be_bytes())?; // [u8; 16], serialized as array
        }
        seq.end()
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(de: D) -> Result<Vec<u128>, D::Error> {
        /// Deserialize a sequence of [u8; 16] arrays back to Vec<u128>.
        struct VecU128Visitor;
        impl<'de> Visitor<'de> for VecU128Visitor {
            type Value = Vec<u128>;
            fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str("sequence of 16-byte u128 values")
            }
            fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Vec<u128>, A::Error> {
                let mut out = Vec::with_capacity(seq.size_hint().unwrap_or(0));
                while let Some(arr) = seq.next_element::<[u8; 16]>()? {
                    out.push(u128::from_be_bytes(arr));
                }
                Ok(out)
            }
        }
        de.deserialize_seq(VecU128Visitor)
    }
}
```

TESTS: [FILE:tests/wire_format.rs] golden file comparison verifies round-trip.

>[wire.rs]

---

## src/event/kind.rs

IMPORTS:
```rust
use serde::{Deserialize, Serialize};
use std::fmt;
```

TYPES:
```rust
/// EventKind wraps a private u16. Products cannot construct arbitrary system kinds.
/// Products use EventKind::custom(category, type_id) which validates the range.
/// [SPEC:src/event/kind.rs]

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct EventKind(u16); // PRIVATE inner field — not pub
```

IMPL:
```rust
impl EventKind {
    /// category:type encoding. Upper 4 bits = category, lower 12 = type.
    /// Products use categories 0x1-0xF. System uses 0x0 and 0xD.
    pub const fn custom(category: u8, type_id: u16) -> Self {
        /// Combine: (category as u16) << 12 | (type_id & 0x0FFF)
        Self(((category as u16) << 12) | (type_id & 0x0FFF))
    }

    pub const fn category(self) -> u8 {
        (self.0 >> 12) as u8
    }

    pub const fn type_id(self) -> u16 {
        self.0 & 0x0FFF
    }

    pub const fn is_system(self) -> bool {
        self.category() == 0x0
    }

    pub const fn is_effect(self) -> bool {
        self.category() == 0xD
    }

    /// Library constants. Products NEVER define these — they use custom().
    pub const DATA: Self = Self(0x0000);
    pub const SYSTEM_INIT: Self = Self(0x0001);
    pub const SYSTEM_SHUTDOWN: Self = Self(0x0002);
    pub const SYSTEM_HEARTBEAT: Self = Self(0x0003);
    pub const SYSTEM_CONFIG_CHANGE: Self = Self(0x0004);
    pub const SYSTEM_CHECKPOINT: Self = Self(0x0005);
    pub const EFFECT_ERROR: Self = Self(0xD001);
    pub const EFFECT_RETRY: Self = Self(0xD002);
    pub const EFFECT_ACK: Self = Self(0xD004);
    pub const EFFECT_BACKPRESSURE: Self = Self(0xD005);
    pub const EFFECT_CANCEL: Self = Self(0xD006);
    pub const EFFECT_CONFLICT: Self = Self(0xD007);
}

impl fmt::Display for EventKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:04X}", self.0)
    }
}
```

TESTS: [FILE:tests/gate_pipeline.rs] verifies custom() range. [FILE:tests/wire_format.rs] verifies serde round-trip.

>[kind.rs]

---

## src/coordinate/mod.rs

IMPORTS:
```rust
pub mod position;
pub use position::DagPosition;

use crate::event::EventKind;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::Arc;
```

TYPES:
```rust
/// Coordinate: WHO (entity) + WHERE (scope). The address of an event stream.
/// [SPEC:src/coordinate/mod.rs]

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Coordinate {
    entity: Arc<str>,   // WHO — stream key, hash chain anchor
    scope: Arc<str>,    // WHERE — isolation boundary
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CoordinateError {
    EmptyEntity,
    EmptyScope,
}

/// Region: the ONE predicate type for query, subscription, cursor, traversal.
/// [SPEC:src/coordinate/mod.rs — Region replaces SubscriptionPattern]
#[derive(Clone, Debug, Default)]
pub struct Region {
    pub entity_prefix: Option<Arc<str>>,
    pub scope: Option<Arc<str>>,
    pub fact: Option<KindFilter>,
    pub clock_range: Option<(u32, u32)>, // per-entity clock, NOT global_sequence [SPEC:IMPLEMENTATION NOTES item 12]
}

#[derive(Clone, Debug)]
pub enum KindFilter {
    Exact(EventKind),
    Category(u8),    // matches any EventKind in this 4-bit category
    Any,
}
```

IMPL:
```rust
impl Coordinate {
    pub fn new(
        entity: impl AsRef<str>,
        scope: impl AsRef<str>,
    ) -> Result<Self, CoordinateError> {
        let entity = entity.as_ref();
        let scope = scope.as_ref();
        if entity.is_empty() { return Err(CoordinateError::EmptyEntity); }
        if scope.is_empty() { return Err(CoordinateError::EmptyScope); }
        Ok(Self {
            entity: Arc::from(entity),
            scope: Arc::from(scope),
        })
    }

    pub fn entity(&self) -> &str { &self.entity }
    pub fn scope(&self) -> &str { &self.scope }
    pub(crate) fn entity_arc(&self) -> Arc<str> { Arc::clone(&self.entity) }
    pub(crate) fn scope_arc(&self) -> Arc<str> { Arc::clone(&self.scope) }
}

impl fmt::Display for Coordinate {
    /// "entity@scope"
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}@{}", self.entity, self.scope)
    }
}

impl fmt::Display for CoordinateError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyEntity => write!(f, "entity cannot be empty"),
            Self::EmptyScope => write!(f, "scope cannot be empty"),
        }
    }
}
impl std::error::Error for CoordinateError {}

/// Region builder — method chaining. [SPEC:src/coordinate/mod.rs — Region builder]
impl Region {
    pub fn all() -> Self { Self::default() }

    pub fn entity(prefix: impl AsRef<str>) -> Self {
        Self { entity_prefix: Some(Arc::from(prefix.as_ref())), ..Self::default() }
    }

    pub fn scope(scope: impl AsRef<str>) -> Self {
        Self { scope: Some(Arc::from(scope.as_ref())), ..Self::default() }
    }

    pub fn coordinate(coord: &Coordinate) -> Self {
        Self {
            entity_prefix: Some(coord.entity_arc()),
            scope: Some(coord.scope_arc()),
            ..Self::default()
        }
    }

    /// Chainable setters
    pub fn with_scope(mut self, scope: impl AsRef<str>) -> Self {
        self.scope = Some(Arc::from(scope.as_ref()));
        self
    }

    pub fn with_fact(mut self, filter: KindFilter) -> Self {
        self.fact = Some(filter);
        self
    }

    pub fn with_fact_category(mut self, cat: u8) -> Self {
        self.fact = Some(KindFilter::Category(cat));
        self
    }

    pub fn with_clock_range(mut self, range: (u32, u32)) -> Self {
        self.clock_range = Some(range);
        self
    }

    /// Match against individual fields — avoids circular dep on store::Notification.
    /// Called by Subscription::recv() to filter events. [FILE:src/store/subscription.rs]
    pub fn matches_event(&self, entity: &str, scope: &str, kind: EventKind) -> bool {
        if let Some(ref prefix) = self.entity_prefix {
            if !entity.starts_with(prefix.as_ref()) {
                return false;
            }
        }
        if let Some(ref s) = self.scope {
            if scope != s.as_ref() {
                return false;
            }
        }
        if let Some(ref fact) = self.fact {
            match fact {
                KindFilter::Exact(k) => if kind != *k { return false; },
                KindFilter::Category(c) => if kind.category() != *c { return false; },
                KindFilter::Any => {},
            }
        }
        /// clock_range is not checked here — it's for index queries, not live filtering.
        true
    }
}
```

TESTS: [FILE:tests/store_integration.rs] tests Region query matching. [FILE:tests/gate_pipeline.rs] tests Coordinate construction.

>[mod.rs]

---

## src/coordinate/position.rs

IMPORTS:
```rust
use serde::{Deserialize, Serialize};
use std::fmt;
```

TYPES:
```rust
/// DagPosition: graph position with hybrid logical clock + depth + lane + sequence.
/// wall_ms + counter provide global causal ordering (HLC-style) across entities.
/// depth/lane/sequence provide per-entity chain ordering.
/// v1: depth=0, lane=0 always. Sequence is per-entity monotonic counter.
/// Lane/depth vocabulary is scaffolding for future distributed fan-out.
/// [SPEC:src/coordinate/position.rs]
/// [CROSS-POLLINATION:czap/hlc.ts — HLC adds wall-clock causality to event ordering]

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DagPosition {
    /// Wall-clock milliseconds at event creation. HLC layer 1.
    pub wall_ms: u64,
    /// HLC counter for same-millisecond tiebreaking.
    pub counter: u16,
    pub depth: u32,
    pub lane: u32,
    pub sequence: u32,
}
```

IMPL:
```rust
impl DagPosition {
    pub const fn new(depth: u32, lane: u32, sequence: u32) -> Self {
        Self { wall_ms: 0, counter: 0, depth, lane, sequence }
    }

    /// Full constructor with HLC fields.
    pub const fn with_hlc(wall_ms: u64, counter: u16, depth: u32, lane: u32, sequence: u32) -> Self {
        Self { wall_ms, counter, depth, lane, sequence }
    }

    pub const fn root() -> Self {
        Self { wall_ms: 0, counter: 0, depth: 0, lane: 0, sequence: 0 }
    }

    /// v1: always depth=0, lane=0, sequence=N. wall_ms set by writer.
    pub const fn child(sequence: u32) -> Self {
        Self { wall_ms: 0, counter: 0, depth: 0, lane: 0, sequence }
    }

    /// v1 with HLC: same as child but with wall clock context.
    pub const fn child_at(sequence: u32, wall_ms: u64, counter: u16) -> Self {
        Self { wall_ms, counter, depth: 0, lane: 0, sequence }
    }

    /// Future: fork creates a new lane at depth+1
    pub const fn fork(parent_depth: u32, new_lane: u32) -> Self {
        Self { wall_ms: 0, counter: 0, depth: parent_depth + 1, lane: new_lane, sequence: 0 }
    }

    pub const fn is_root(&self) -> bool {
        self.depth == 0 && self.lane == 0 && self.sequence == 0
    }

    /// Causal ordering: ancestor if same lane, same depth, and lower sequence.
    /// v1: depth is always 0, lane always 0, so just compare sequence.
    /// DAG-ready: different depths means different branches — not ancestor.
    pub const fn is_ancestor_of(&self, other: &DagPosition) -> bool {
        self.lane == other.lane
            && self.depth == other.depth
            && self.sequence < other.sequence
    }
}

impl fmt::Display for DagPosition {
    /// "depth:lane:sequence@wall_ms.counter"
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}:{}@{}.{}", self.depth, self.lane, self.sequence, self.wall_ms, self.counter)
    }
}

/// PartialOrd for causal ordering — not total because different lanes
/// are incomparable. [SPEC:src/coordinate/position.rs — PartialOrd]
impl PartialOrd for DagPosition {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        if self.lane != other.lane {
            return None; // different lanes are incomparable
        }
        Some(self.sequence.cmp(&other.sequence))
    }
}
```

TESTS: unit tests inline. [FILE:tests/store_integration.rs] verifies position assignment.

>[position.rs]

---

## src/outcome/error.rs

IMPORTS:
```rust
use serde::{Deserialize, Serialize};
use std::fmt;
```

TYPES:
```rust
/// OutcomeError: structured error with kind, message, optional compensation.
/// [SPEC:src/outcome/error.rs]

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct OutcomeError {
    pub kind: ErrorKind,
    pub message: String,
    pub compensation: Option<super::wait::CompensationAction>,
    pub retryable: bool,
}

/// ErrorKind: 8 domain kinds + Custom(u16) for product extension.
/// Products extend via Custom(u16) — same category:type encoding as EventKind.
/// [SPEC:src/outcome/error.rs — ErrorKind]

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ErrorKind {
    NotFound,
    Conflict,
    Validation,
    PolicyRejection,
    StorageError,
    Timeout,
    Serialization,
    Internal,
    Custom(u16),
}
```

IMPL:
```rust
impl ErrorKind {
    pub fn is_retryable(&self) -> bool {
        matches!(self, Self::StorageError | Self::Timeout)
    }

    pub fn is_domain(&self) -> bool {
        matches!(self, Self::NotFound | Self::Conflict | Self::Validation | Self::PolicyRejection)
    }

    pub fn is_operational(&self) -> bool {
        matches!(self, Self::StorageError | Self::Timeout | Self::Serialization | Self::Internal)
    }
}

impl fmt::Display for OutcomeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{:?}] {}", self.kind, self.message)
    }
}
impl std::error::Error for OutcomeError {}
```

TESTS: [FILE:tests/monad_laws.rs] constructs OutcomeErrors in Outcome::Err variants.

>[error.rs]

---

## src/outcome/wait.rs

IMPORTS:
```rust
use serde::{Deserialize, Serialize};
// NOTE: No `use crate::wire::*` needed here. The #[serde(with = "crate::wire::...")]
// annotations are string literal paths — serde resolves them at compile time, not
// through Rust's `use` mechanism. The wire module just needs to exist in the crate.
```

TYPES:
```rust
/// WaitCondition: what an Outcome::Pending is waiting for.
/// [SPEC:src/outcome/wait.rs]

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum WaitCondition {
    Timeout { resume_at_ms: u64 },
    Event {
        #[serde(with = "crate::wire::u128_bytes")]
        event_id: u128,
    },
    All(Vec<WaitCondition>),
    Any(Vec<WaitCondition>),
    Custom { tag: u16, data: Vec<u8> },
}

/// CompensationAction: what to do when an error needs compensation.
/// The writer persists this as data. Products implement the handler.
/// [SPEC:src/outcome/wait.rs — CompensationAction]

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum CompensationAction {
    Rollback {
        #[serde(with = "crate::wire::vec_u128_bytes")]
        event_ids: Vec<u128>,
    },
    Notify {
        #[serde(with = "crate::wire::u128_bytes")]
        target_id: u128,
        message: String,
    },
    Release {
        #[serde(with = "crate::wire::vec_u128_bytes")]
        resource_ids: Vec<u128>,
    },
    Custom { action_type: String, data: Vec<u8> },
}
```

IMPL: no methods — pure data types.

TESTS: [FILE:tests/wire_format.rs] verifies u128 round-trip through msgpack.

>[wait.rs]

---

## src/outcome/combine.rs

IMPORTS:
```rust
use super::{Outcome, OutcomeError};
use crate::outcome::error::ErrorKind;
```

TYPES: no types — free functions only.

IMPL:
```rust
/// zip: combine two outcomes into a tuple outcome.
/// If either is Err, the first Err wins.
/// [SPEC:src/outcome/combine.rs]

pub fn zip<A: Clone, B: Clone>(a: Outcome<A>, b: Outcome<B>) -> Outcome<(A, B)> {
    /// Priority order for non-Ok variants (highest wins):
    ///   Err > Cancelled > Retry > Pending > Batch > Ok
    /// When both are non-Ok, the FIRST (a) argument's variant wins at equal priority.
    match (a, b) {
        // Both Ok → combine
        (Outcome::Ok(a), Outcome::Ok(b)) => Outcome::Ok((a, b)),

        // Either Err → first Err wins
        (Outcome::Err(e), _) | (_, Outcome::Err(e)) => Outcome::Err(e),

        // Either Cancelled → first Cancelled wins
        (Outcome::Cancelled { reason }, _) | (_, Outcome::Cancelled { reason }) => {
            Outcome::Cancelled { reason }
        }

        // Either Retry → first Retry wins
        (Outcome::Retry { after_ms, attempt, max_attempts, reason }, _)
        | (_, Outcome::Retry { after_ms, attempt, max_attempts, reason }) => {
            Outcome::Retry { after_ms, attempt, max_attempts, reason }
        }

        // Either Pending → first Pending wins
        (Outcome::Pending { condition, resume_token }, _)
        | (_, Outcome::Pending { condition, resume_token }) => {
            Outcome::Pending { condition, resume_token }
        }

        // Both Batch → zip elements pairwise (truncate to shorter)
        (Outcome::Batch(a_items), Outcome::Batch(b_items)) => {
            Outcome::Batch(
                a_items.into_iter().zip(b_items).map(|(a, b)| zip(a, b)).collect()
            )
        }

        // One Batch, one Ok → map the Ok into each Batch element
        (Outcome::Batch(items), Outcome::Ok(b)) => {
            Outcome::Batch(items.into_iter().map(|a| zip(a, Outcome::Ok(b.clone()))).collect())
        }
        (Outcome::Ok(a), Outcome::Batch(items)) => {
            Outcome::Batch(items.into_iter().map(|b| zip(Outcome::Ok(a.clone()), b)).collect())
        }
    }
}
/// A: Clone and B: Clone required for the Batch+Ok distribution cases above.

/// join_all: collect a Vec of outcomes into an outcome of Vec.
/// All must be Ok for the result to be Ok. First Err short-circuits.
/// [SPEC:src/outcome/combine.rs]

pub fn join_all<T>(outcomes: Vec<Outcome<T>>) -> Outcome<Vec<T>> {
    let mut results = Vec::with_capacity(outcomes.len());
    for outcome in outcomes {
        match outcome {
            Outcome::Ok(v) => results.push(v),
            Outcome::Err(e) => return Outcome::Err(e),
            Outcome::Cancelled { reason } => return Outcome::Cancelled { reason },
            Outcome::Retry { after_ms, attempt, max_attempts, reason } => {
                return Outcome::Retry { after_ms, attempt, max_attempts, reason };
            }
            Outcome::Pending { condition, resume_token } => {
                return Outcome::Pending { condition, resume_token };
            }
            Outcome::Batch(inner) => {
                /// Flatten: join_all on the inner batch, then continue collecting.
                match join_all(inner) {
                    Outcome::Ok(vs) => results.extend(vs),
                    other => return other.map(|mut vs| { let mut r = results; r.append(&mut vs); r }),
                }
            }
        }
    }
    Outcome::Ok(results)
}

/// join_any: first Ok wins. If all fail, last Err wins.
/// [SPEC:src/outcome/combine.rs]

pub fn join_any<T>(outcomes: Vec<Outcome<T>>) -> Outcome<T> {
    let mut last_err = None;
    for outcome in outcomes {
        match outcome {
            Outcome::Ok(v) => return Outcome::Ok(v),
            Outcome::Err(e) => last_err = Some(e),
            other => return other, // Retry/Pending/Cancelled propagate immediately
        }
    }
    match last_err {
        Some(e) => Outcome::Err(e),
        None => Outcome::Err(OutcomeError {
            kind: ErrorKind::Internal,
            message: "join_any called with empty vec".into(),
            compensation: None,
            retryable: false,
        }),
    }
}
```

TESTS: [FILE:tests/monad_laws.rs] verifies zip/join_all/join_any properties.

>[combine.rs]

---

## src/outcome/mod.rs

IMPORTS:
```rust
use serde::{Deserialize, Serialize};
// NOTE: No `use crate::wire::*` needed. serde(with = "crate::wire::...") resolves via string path.

pub mod error;
pub mod combine;
pub mod wait;

pub use error::{OutcomeError, ErrorKind};
pub use wait::{WaitCondition, CompensationAction};
pub use combine::{zip, join_all, join_any};
```

TYPES:
```rust
/// Outcome<T>: the core algebraic type. 6 variants.
/// Named "Outcome" not "Effect" to eliminate Effect/Event confusion.
/// [SPEC:src/outcome/mod.rs]

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
pub enum Outcome<T> {
    Ok(T),
    Err(OutcomeError),
    Retry {
        after_ms: u64,
        attempt: u32,
        max_attempts: u32,
        reason: String,
    },
    Pending {
        condition: WaitCondition,
        #[serde(with = "crate::wire::u128_bytes")]
        resume_token: u128,
    },
    Cancelled { reason: String },
    Batch(Vec<Outcome<T>>),
}
```

IMPL:
```rust
impl<T> Outcome<T> {
    // --- Construction ---
    pub fn ok(val: T) -> Self { Self::Ok(val) }
    pub fn err(e: OutcomeError) -> Self { Self::Err(e) }
    pub fn cancelled(reason: impl Into<String>) -> Self {
        Self::Cancelled { reason: reason.into() }
    }
    pub fn retry(after_ms: u64, attempt: u32, max_attempts: u32, reason: impl Into<String>) -> Self {
        Self::Retry { after_ms, attempt, max_attempts, reason: reason.into() }
    }
    pub fn pending(condition: WaitCondition, resume_token: u128) -> Self {
        Self::Pending { condition, resume_token }
    }

    // --- Predicates ---
    pub fn is_ok(&self) -> bool { matches!(self, Self::Ok(_)) }
    pub fn is_err(&self) -> bool { matches!(self, Self::Err(_)) }
    pub fn is_retry(&self) -> bool { matches!(self, Self::Retry { .. }) }
    pub fn is_pending(&self) -> bool { matches!(self, Self::Pending { .. }) }
    pub fn is_cancelled(&self) -> bool { matches!(self, Self::Cancelled { .. }) }
    pub fn is_batch(&self) -> bool { matches!(self, Self::Batch(_)) }
    pub fn is_terminal(&self) -> bool {
        matches!(self, Self::Ok(_) | Self::Err(_) | Self::Cancelled { .. })
    }

    // --- Combinators ---

    /// map: transform the Ok value. Distributes over Batch.
    /// [SPEC:src/outcome/mod.rs — combinators distribute over Batch via F: Clone]
    pub fn map<U, F: FnOnce(T) -> U + Clone>(self, f: F) -> Outcome<U> {
        match self {
            Self::Ok(v) => Outcome::Ok(f(v)),
            Self::Err(e) => Outcome::Err(e),
            Self::Retry { after_ms, attempt, max_attempts, reason } =>
                Outcome::Retry { after_ms, attempt, max_attempts, reason },
            Self::Pending { condition, resume_token } =>
                Outcome::Pending { condition, resume_token },
            Self::Cancelled { reason } => Outcome::Cancelled { reason },
            Self::Batch(items) => Outcome::Batch(
                items.into_iter().map(|o| o.map(f.clone())).collect()
            ),
        }
    }

    /// and_then: the monad bind. Distributes over Batch.
    /// F: Clone is required for Batch distribution (called once per element).
    /// [SPEC:src/outcome/mod.rs — The and_then monad fix]
    /// This is THE critical method. Monad laws are verified by proptest.
    /// [FILE:tests/monad_laws.rs]
    pub fn and_then<U, F: FnOnce(T) -> Outcome<U> + Clone>(self, f: F) -> Outcome<U> {
        match self {
            Self::Ok(v) => f(v),
            Self::Err(e) => Outcome::Err(e),
            Self::Retry { after_ms, attempt, max_attempts, reason } =>
                Outcome::Retry { after_ms, attempt, max_attempts, reason },
            Self::Pending { condition, resume_token } =>
                Outcome::Pending { condition, resume_token },
            Self::Cancelled { reason } => Outcome::Cancelled { reason },
            Self::Batch(items) => Outcome::Batch(
                items.into_iter().map(|o| o.and_then(f.clone())).collect()
            ),
        }
    }

    pub fn map_err<F: FnOnce(OutcomeError) -> OutcomeError + Clone>(self, f: F) -> Self {
        match self {
            Self::Err(e) => Self::Err(f(e)),
            Self::Batch(items) => Self::Batch(
                items.into_iter().map(|o| o.map_err(f.clone())).collect()
            ),
            other => other,
        }
    }

    pub fn or_else<F: FnOnce(OutcomeError) -> Outcome<T> + Clone>(self, f: F) -> Outcome<T> {
        match self {
            Self::Err(e) => f(e),
            Self::Batch(items) => Self::Batch(
                items.into_iter().map(|o| o.or_else(f.clone())).collect()
            ),
            other => other,
        }
    }

    pub fn flatten(self) -> Outcome<T>
    where T: Into<Outcome<T>> {
        /// Unwrap one layer: Outcome<Outcome<T>> → Outcome<T>
        self.and_then(|v| v.into())
    }

    pub fn inspect<F: FnOnce(&T) + Clone>(self, f: F) -> Self {
        match &self {
            Self::Ok(v) => { f(v); self }
            Self::Batch(_) => self.map(|v| { f(&v); v }),
            _ => self,
        }
    }

    pub fn inspect_err<F: FnOnce(&OutcomeError) + Clone>(self, f: F) -> Self {
        match &self {
            Self::Err(e) => { f(e); self }
            Self::Batch(_) => {
                /// Walk batch, inspect errors, return unchanged
                match self {
                    Self::Batch(items) => Self::Batch(
                        items.into_iter().map(|o| o.inspect_err(f.clone())).collect()
                    ),
                    _ => unreachable!(),
                }
            }
            _ => self,
        }
    }

    pub fn and_then_if<F: FnOnce(&T) -> bool, G: FnOnce(T) -> Outcome<T>>(
        self, pred: F, f: G,
    ) -> Outcome<T> {
        match self {
            Self::Ok(v) => if pred(&v) { f(v) } else { Self::Ok(v) },
            other => other,
        }
    }

    pub fn into_result(self) -> Result<T, OutcomeError> {
        match self {
            Self::Ok(v) => Ok(v),
            Self::Err(e) => Err(e),
            Self::Cancelled { reason } => Err(OutcomeError {
                kind: ErrorKind::Internal,
                message: format!("cancelled: {reason}"),
                compensation: None,
                retryable: false,
            }),
            _ => Err(OutcomeError {
                kind: ErrorKind::Internal,
                message: "outcome is not terminal".into(),
                compensation: None,
                retryable: false,
            }),
        }
    }

    pub fn unwrap_or(self, default: T) -> T {
        match self {
            Self::Ok(v) => v,
            _ => default,
        }
    }

    pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T {
        match self {
            Self::Ok(v) => v,
            _ => f(),
        }
    }
}
```

TESTS:
  [FILE:tests/monad_laws.rs] — proptest: left/right identity, associativity, Batch distribution.
  Failure message includes: "LEFT IDENTITY VIOLATED: Outcome::ok(x).and_then(f) != f(x). Check: outcome/mod.rs and_then implementation. The F: Clone bound must recurse into Batch elements."

>[mod.rs]

---

## src/event/hash.rs

IMPORTS:
```rust
use serde::{Deserialize, Serialize};
```

TYPES:
```rust
/// HashChain: prev_hash + event_hash. Per-entity linear chain.
/// Default (all zeros) = genesis convention.
/// [SPEC:src/event/hash.rs — NO TRAIT. NO ENUM.]

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HashChain {
    pub prev_hash: [u8; 32],
    pub event_hash: [u8; 32],
}
```

IMPL:
```rust
impl Default for HashChain {
    fn default() -> Self {
        Self { prev_hash: [0u8; 32], event_hash: [0u8; 32] }
    }
}

/// compute_hash: blake3 hash of content bytes.
/// Behind feature = "blake3". When off, Committed.hash is [0u8; 32].
/// [SPEC:INVARIANTS item 5 — blake3 is the only hash]
/// [DEP:blake3::hash] → returns blake3::Hash, .into() gives [u8; 32]

#[cfg(feature = "blake3")]
pub fn compute_hash(content_bytes: &[u8]) -> [u8; 32] {
    blake3::hash(content_bytes).into()
}

/// verify_chain: check that event_hash matches content AND prev_hash matches expected.
/// [SPEC:src/event/hash.rs — verify_chain]

#[cfg(feature = "blake3")]
pub fn verify_chain(
    content_bytes: &[u8],
    chain: &HashChain,
    expected_prev: &[u8; 32],
) -> bool {
    chain.prev_hash == *expected_prev
        && chain.event_hash == compute_hash(content_bytes)
}
```

TESTS: [FILE:tests/hash_chain.rs] — proptest: chain verification, tamper detection, genesis.

>[hash.rs]

---

## src/guard/denial.rs

IMPORTS:
```rust
use serde::Serialize;
use std::fmt;
```

TYPES:
```rust
/// Denial: returned by a Gate when it rejects a proposal.
/// Separate from OutcomeError. Library does NOT auto-store denials.
/// Products decide whether to persist denials as events.
/// [SPEC:src/guard/denial.rs]

#[derive(Clone, Debug, PartialEq, Serialize)]
// NOTE: Denial does NOT derive Deserialize. The gate field is &'static str which
// cannot be deserialized from owned data (no 'static lifetime at deser time).
// The library never persists Denials — it returns them to callers.
// Products that want to persist denials serialize them into event payloads.
pub struct Denial {
    pub gate: &'static str,
    pub code: String,
    pub message: String,
    pub context: Vec<(String, String)>,
}
```

IMPL:
```rust
impl Denial {
    pub fn new(gate: &'static str, message: impl Into<String>) -> Self {
        Self { gate, code: String::new(), message: message.into(), context: vec![] }
    }

    pub fn with_code(mut self, code: impl Into<String>) -> Self {
        self.code = code.into();
        self
    }

    pub fn with_context(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.context.push((key.into(), value.into()));
        self
    }
}

impl fmt::Display for Denial {
    /// "[gate] message"
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}] {}", self.gate, self.message)
    }
}
impl std::error::Error for Denial {}
```

TESTS: [FILE:tests/gate_pipeline.rs] tests Denial construction and Display.

>[denial.rs]

---

## src/guard/receipt.rs

IMPORTS:
```rust
// NO serde imports. Receipt is NOT Serialize, NOT Clone, NOT Copy.
```

TYPES:
```rust
/// Receipt<T>: proof that all gates passed. Consumed exactly once.
/// The seal module prevents external construction. Only GateSet::evaluate() creates these.
/// [SPEC:src/guard/receipt.rs — TOCTOU fix]

pub struct Receipt<T> {
    _seal: seal::Token,
    gates_passed: Vec<&'static str>,
    payload: T,
}

mod seal {
    /// Private module. Token cannot be constructed outside guard/.
    pub(crate) struct Token;
}
```

IMPL:
```rust
/// Receipt is NOT Clone, NOT Copy, NOT Serialize.
/// It wraps the payload INSIDE so it can't be mutated after gate evaluation.
/// Consumed via into_parts().

impl<T> Receipt<T> {
    /// Only callable from within the crate (seal::Token is pub(crate)).
    /// [FILE:src/guard/mod.rs — GateSet::evaluate() is the only caller]
    pub(crate) fn new(payload: T, gates_passed: Vec<&'static str>) -> Self {
        Self { _seal: seal::Token, gates_passed, payload }
    }

    pub fn payload(&self) -> &T { &self.payload }
    pub fn gates_passed(&self) -> &[&'static str] { &self.gates_passed }

    /// Consuming extraction. After this, the receipt is gone.
    pub fn into_parts(self) -> (T, Vec<&'static str>) {
        (self.payload, self.gates_passed)
    }
}
```

TESTS:
  [FILE:tests/gate_pipeline.rs] — receipt TOCTOU, consumed once.
  [FILE:tests/typestate_safety.rs] — trybuild: forge_receipt.rs must NOT compile.
  [FILE:tests/ui/forge_receipt.rs] — attempts to construct Receipt directly, expects E0603.

>[receipt.rs]

---

## src/guard/mod.rs

IMPORTS:
```rust
pub mod denial;
pub mod receipt;

pub use denial::Denial;
pub use receipt::Receipt;
```

TYPES:
```rust
/// Gate<Ctx>: a predicate that evaluates a context and either permits or denies.
/// Gates are PREDICATES, not transformers. No I/O, no mutation, pure.
/// Ctx is product-defined. Library is generic over it.
/// [SPEC:src/guard/mod.rs]

pub trait Gate<Ctx>: Send + Sync {
    fn name(&self) -> &'static str;
    fn evaluate(&self, ctx: &Ctx) -> Result<(), Denial>;
    fn description(&self) -> &'static str { "" }
}

/// GateSet<Ctx>: ordered collection of gates. Fail-fast by default.
pub struct GateSet<Ctx> {
    gates: Vec<Box<dyn Gate<Ctx>>>,
}
```

IMPL:
```rust
impl<Ctx> GateSet<Ctx> {
    pub fn new() -> Self { Self { gates: vec![] } }

    pub fn push(&mut self, gate: impl Gate<Ctx> + 'static) {
        self.gates.push(Box::new(gate));
    }

    /// Fail-fast evaluation. First denial stops.
    /// Returns Receipt<T> wrapping the proposal payload on success.
    pub fn evaluate<T>(&self, ctx: &Ctx, proposal: crate::pipeline::Proposal<T>)
        -> Result<Receipt<T>, Denial>
    {
        for gate in &self.gates {
            gate.evaluate(ctx)?;
        }
        let names: Vec<&'static str> = self.gates.iter().map(|g| g.name()).collect();
        Ok(Receipt::new(proposal.0, names))
    }

    /// Evaluate ALL gates (no fail-fast). For observability — collect all denials.
    pub fn evaluate_all(&self, ctx: &Ctx) -> Vec<Denial> {
        self.gates.iter()
            .filter_map(|g| g.evaluate(ctx).err())
            .collect()
    }

    pub fn len(&self) -> usize { self.gates.len() }
    pub fn is_empty(&self) -> bool { self.gates.is_empty() }
    pub fn names(&self) -> Vec<&'static str> {
        self.gates.iter().map(|g| g.name()).collect()
    }
}

impl<Ctx> Default for GateSet<Ctx> {
    fn default() -> Self { Self::new() }
}
```

TESTS: [FILE:tests/gate_pipeline.rs] — registration order, fail-fast, receipt TOCTOU, consumed once.

>[mod.rs]

---

## src/id/mod.rs

IMPORTS:
```rust
use std::fmt;
use std::hash::Hash;
use std::str::FromStr;
```

TYPES:
```rust
/// EntityIdType: Layer 0 trait. No uuid dep.
/// All IDs are u128 internally. No Uuid in public API. [SPEC:src/id/mod.rs]
/// [SPEC:RED FLAGS — DO NOT put uuid::Uuid in the public API]

pub trait EntityIdType:
    Copy + Clone + Eq + Hash + fmt::Debug + fmt::Display + FromStr + Send + Sync + 'static
{
    const ENTITY_NAME: &'static str;
    fn new(id: u128) -> Self;
    fn as_u128(&self) -> u128;
    fn now_v7() -> Self;
    fn nil() -> Self;
}
```

IMPL:
```rust
/// Helper function: generates a UUIDv7 as u128. Used by the macro below.
/// This keeps `uuid` as a private dependency — downstream crates calling
/// define_entity_id! don't need uuid in their own Cargo.toml.
/// [DEP:uuid::Uuid::now_v7] → generates UUIDv7, .as_u128() → u128
pub fn generate_v7_id() -> u128 {
    uuid::Uuid::now_v7().as_u128()
}

/// define_entity_id!: Layer 1+ macro. Uses generate_v7_id() helper.
/// Downstream crates do NOT need uuid as a direct dependency.

#[macro_export]
macro_rules! define_entity_id {
    ($name:ident, $entity:literal) => {
        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
        pub struct $name(u128);

        impl $crate::id::EntityIdType for $name {
            const ENTITY_NAME: &'static str = $entity;

            fn new(id: u128) -> Self { Self(id) }

            fn as_u128(&self) -> u128 { self.0 }

            fn now_v7() -> Self {
                Self($crate::id::generate_v7_id())
            }

            fn nil() -> Self { Self(0) }
        }

        impl ::std::fmt::Display for $name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                /// Display as "entity_name:hex" e.g. "event:0a1b2c..."
                write!(f, "{}:{:032x}", $entity, self.0)
            }
        }

        impl ::std::str::FromStr for $name {
            type Err = String;
            fn from_str(s: &str) -> Result<Self, Self::Err> {
                /// Parse "entity_name:hex" or bare hex
                let hex = s.strip_prefix(concat!($entity, ":")).unwrap_or(s);
                u128::from_str_radix(hex, 16)
                    .map(Self)
                    .map_err(|e| format!("invalid {}: {e}", $entity))
            }
        }
    };
}

/// Library defines ONE id type.
define_entity_id!(EventId, "event");
```

TESTS: [FILE:tests/gate_pipeline.rs] tests EventId round-trip Display/FromStr.

>[mod.rs]

---

## src/typestate/mod.rs

IMPORTS:
```rust
// No imports needed — pure macro_rules!, zero deps, zero runtime code.
```

TYPES: generated by macros.

IMPL:
```rust
/// define_state_machine!: generates a sealed marker trait + zero-sized state structs.
/// [SPEC:src/typestate/mod.rs — 99 LOC of macros]
///
/// Usage:
///   define_state_machine!(LockState { Acquired, Released });
///   // Generates:
///   //   pub trait LockState: private::Sealed {}
///   //   pub struct Acquired;
///   //   pub struct Released;
///   //   impl LockState for Acquired {}
///   //   impl LockState for Released {}

#[macro_export]
macro_rules! define_state_machine {
    ($trait_name:ident { $($state:ident),+ $(,)? }) => {
        mod private {
            pub trait Sealed {}
        }

        pub trait $trait_name: private::Sealed {}

        $(
            #[derive(Debug, Clone, Copy, PartialEq, Eq)]
            pub struct $state;

            impl private::Sealed for $state {}
            impl $trait_name for $state {}
        )+
    };
}

/// define_typestate!: generates a PhantomData wrapper for typed state machines.
/// [SPEC:src/typestate/mod.rs]
///
/// Usage:
///   define_typestate!(Lock<S: LockState> { holder: String });
///   // Generates Lock<S> with PhantomData<S>, data(), into_data(), new()

#[macro_export]
macro_rules! define_typestate {
    ($name:ident<$param:ident: $bound:ident> { $($field:ident: $ftype:ty),* $(,)? }) => {
        pub struct $name<$param: $bound> {
            $( pub $field: $ftype, )*
            _state: ::std::marker::PhantomData<$param>,
        }

        impl<$param: $bound> $name<$param> {
            pub fn new($($field: $ftype),*) -> Self {
                Self { $($field,)* _state: ::std::marker::PhantomData }
            }

            pub fn data(&self) -> ($(&$ftype,)*) {
                ($(&self.$field,)*)
            }

            pub fn into_data(self) -> ($($ftype,)*) {
                ($(self.$field,)*)
            }
        }
    };
}
```

TESTS: [FILE:tests/typestate_safety.rs] — trybuild compile-fail tests for invalid transitions.

>[mod.rs]

---

## src/typestate/transition.rs

IMPORTS:
```rust
use crate::event::EventKind;
use std::marker::PhantomData;
```

TYPES:
```rust
/// Transition<From, To, P>: a typed state change with an EventKind and payload.
/// The compiler ensures you can only create transitions from valid source states.
/// [SPEC:src/typestate/transition.rs]

pub struct Transition<From, To, P> {
    kind: EventKind,
    payload: P,
    _from: PhantomData<From>,
    _to: PhantomData<To>,
}
```

IMPL:
```rust
impl<From, To, P> Transition<From, To, P> {
    pub fn new(kind: EventKind, payload: P) -> Self {
        Self { kind, payload, _from: PhantomData, _to: PhantomData }
    }

    pub fn kind(&self) -> EventKind { self.kind }
    pub fn payload(&self) -> &P { &self.payload }
    pub fn into_payload(self) -> P { self.payload }
}
```

TESTS: [FILE:tests/typestate_safety.rs] — verifies transition type safety.

>[transition.rs]

---

## src/event/header.rs

IMPORTS:
```rust
use crate::coordinate::DagPosition;
use crate::event::EventKind;
use serde::{Deserialize, Serialize};
use std::fmt;
// NOTE: No `use crate::wire::*` needed. serde(with = "crate::wire::...") resolves via string path.
```

TYPES:
```rust
/// EventHeader: metadata for every event. Store generates this — users don't call new directly.
/// repr(C) for deterministic field ordering (NOT a wire format — msgpack handles serialization).
/// [SPEC:src/event/header.rs]

#[repr(C)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EventHeader {
    #[serde(with = "crate::wire::u128_bytes")]
    pub event_id: u128,
    #[serde(with = "crate::wire::u128_bytes")]
    pub correlation_id: u128,
    #[serde(with = "crate::wire::option_u128_bytes")]
    pub causation_id: Option<u128>,
    pub timestamp_us: i64,
    pub position: DagPosition,
    pub payload_size: u32,
    pub event_kind: EventKind,
    pub flags: u8,
    /// Content hash of the serialized payload. Enables automatic projection cache
    /// invalidation when event schemas evolve. [0u8; 32] when blake3 is off.
    #[serde(default)]
    pub content_hash: [u8; 32],
}
```

Flag bit constants:
```rust
pub const FLAG_REQUIRES_ACK: u8 = 0x01;
pub const FLAG_TRANSACTIONAL: u8 = 0x02;
pub const FLAG_REPLAY: u8 = 0x08;
```

IMPL:
```rust
impl EventHeader {
    pub fn new(
        event_id: u128,
        correlation_id: u128,
        causation_id: Option<u128>,
        timestamp_us: i64,
        position: DagPosition,
        payload_size: u32,
        event_kind: EventKind,
    ) -> Self {
        Self {
            event_id, correlation_id, causation_id, timestamp_us,
            position, payload_size, event_kind, flags: 0,
            content_hash: [0u8; 32],
        }
    }

    pub fn with_flags(mut self, flags: u8) -> Self {
        self.flags = flags;
        self
    }

    pub fn requires_ack(&self) -> bool { self.flags & FLAG_REQUIRES_ACK != 0 }
    pub fn is_transactional(&self) -> bool { self.flags & FLAG_TRANSACTIONAL != 0 }
    pub fn is_replay(&self) -> bool { self.flags & FLAG_REPLAY != 0 }
    pub fn age_us(&self, now_us: i64) -> u64 { now_us.saturating_sub(self.timestamp_us).max(0) as u64 }
}
```

TESTS: [FILE:tests/wire_format.rs] — golden file round-trip for EventHeader msgpack encoding.

>[header.rs]

---

## src/event/sourcing.rs

IMPORTS:
```rust
use crate::coordinate::Coordinate;
use crate::event::{Event, EventKind};
```

TYPES:
```rust
/// EventSourced<P>: backward-looking fold. Replay events to reconstruct state.
/// P is generic — NO serde_json dependency in the trait.
/// Store uses EventSourced<serde_json::Value>. [SPEC:src/event/sourcing.rs]

pub trait EventSourced<P>: Sized {
    fn from_events(events: &[Event<P>]) -> Option<Self>;
    fn apply_event(&mut self, event: &Event<P>);
    fn relevant_event_kinds() -> &'static [EventKind];
}

/// Reactive<P>: forward-looking counterpart. See event → maybe emit derived events.
/// Products compose: subscribe + react + append (7 lines of glue).
/// [SPEC:src/event/sourcing.rs]

pub trait Reactive<P> {
    fn react(&self, event: &Event<P>) -> Vec<(Coordinate, EventKind, P)>;
}
```

IMPL: traits only — no default implementations. ~15 LOC total.

>[sourcing.rs]

---

## src/event/mod.rs

IMPORTS:
```rust
pub mod kind;
pub mod header;
pub mod hash;
pub mod sourcing;

pub use kind::EventKind;
pub use header::EventHeader;
pub use hash::HashChain;
pub use sourcing::{EventSourced, Reactive};

use crate::coordinate::Coordinate;
use serde::{Deserialize, Serialize};
```

TYPES:
```rust
/// Event<P>: header + payload + optional hash chain.
/// [SPEC:src/event/mod.rs]

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Event<P> {
    pub header: EventHeader,
    pub payload: P,
    pub hash_chain: Option<HashChain>,
}

/// StoredEvent<P>: what store.get() returns. Coordinate + Event.
/// store.get() returns StoredEvent<serde_json::Value> because segments are
/// schema-free MessagePack. [SPEC:src/event/mod.rs]

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StoredEvent<P> {
    pub coordinate: Coordinate,
    pub event: Event<P>,
}
```

IMPL:
```rust
impl<P> Event<P> {
    pub fn new(header: EventHeader, payload: P) -> Self {
        Self { header, payload, hash_chain: None }
    }

    pub fn with_hash_chain(mut self, chain: HashChain) -> Self {
        self.hash_chain = Some(chain);
        self
    }

    pub fn event_id(&self) -> u128 { self.header.event_id }
    pub fn event_kind(&self) -> EventKind { self.header.event_kind }
    pub fn position(&self) -> &crate::coordinate::DagPosition { &self.header.position }

    pub fn is_genesis(&self) -> bool {
        self.hash_chain.as_ref()
            .map(|c| c.prev_hash == [0u8; 32])
            .unwrap_or(true)
    }

    pub fn map_payload<U, F: FnOnce(P) -> U>(self, f: F) -> Event<U> {
        Event {
            header: self.header,
            payload: f(self.payload),
            hash_chain: self.hash_chain,
        }
    }
}
```

TESTS: [FILE:tests/store_integration.rs] [FILE:tests/wire_format.rs]

>[mod.rs]

---

## src/pipeline/mod.rs

IMPORTS:
```rust
use crate::guard::{Denial, GateSet, Receipt};
use serde::{Deserialize, Serialize};
// NOTE: No `use crate::wire::*` needed. serde(with = "crate::wire::...") resolves via string path.

pub mod bypass;
pub use bypass::{BypassReason, BypassReceipt};
```

TYPES:
```rust
/// Proposal<T>: wraps a value for gate evaluation.
/// [SPEC:src/pipeline/mod.rs]
pub struct Proposal<T>(pub T);

/// Committed<T>: proof that an event was persisted.
/// [SPEC:src/pipeline/mod.rs]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Committed<T> {
    pub payload: T,
    #[serde(with = "crate::wire::u128_bytes")]
    pub event_id: u128,
    pub sequence: u64,
    pub hash: [u8; 32], // blake3, or [0u8;32] if feature off
}

/// Pipeline<Ctx>: evaluate gates then commit.
/// [SPEC:src/pipeline/mod.rs]
pub struct Pipeline<Ctx> {
    gates: GateSet<Ctx>,
}
```

IMPL:
```rust
impl<T> Proposal<T> {
    pub fn new(payload: T) -> Self { Self(payload) }
    pub fn payload(&self) -> &T { &self.0 }
    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Proposal<U> {
        Proposal(f(self.0))
    }
}

impl<Ctx> Pipeline<Ctx> {
    pub fn new(gates: GateSet<Ctx>) -> Self { Self { gates } }

    pub fn evaluate<T>(&self, ctx: &Ctx, proposal: Proposal<T>)
        -> Result<Receipt<T>, Denial>
    {
        self.gates.evaluate(ctx, proposal)
    }

    /// commit: generic over error type E. Pipeline doesn't know about StoreError.
    /// Products pass a closure that calls store.append() and wraps the result.
    /// [SPEC:IMPLEMENTATION NOTES item 9 — Pipeline::commit() E is generic]
    pub fn commit<T, E>(
        &self,
        receipt: Receipt<T>,
        commit_fn: impl FnOnce(T) -> Result<Committed<T>, E>,
    ) -> Result<Committed<T>, E> {
        let (payload, _gate_names) = receipt.into_parts();
        commit_fn(payload)
    }

    /// bypass: skip gates with an auditable reason.
    /// [FILE:src/pipeline/bypass.rs]
    pub fn bypass<T>(
        proposal: Proposal<T>,
        reason: &'static dyn BypassReason,
    ) -> BypassReceipt<T> {
        BypassReceipt {
            payload: proposal.0,
            reason: reason.name(),
            justification: reason.justification(),
        }
    }
}
```

TESTS: [FILE:tests/gate_pipeline.rs]

>[mod.rs]

---

## src/pipeline/bypass.rs

IMPORTS:
```rust
// No imports needed.
```

TYPES:
```rust
/// BypassReason: products implement this to justify skipping gates.
/// [SPEC:src/pipeline/bypass.rs]

pub trait BypassReason: Send + Sync {
    fn name(&self) -> &'static str;
    fn justification(&self) -> &'static str;
}

/// BypassReceipt<T>: audit trail shows "bypassed: {reason}".
pub struct BypassReceipt<T> {
    pub payload: T,
    pub reason: &'static str,
    pub justification: &'static str,
}
```

IMPL: none — pure types.

>[bypass.rs]

---

## src/prelude.rs

IMPORTS:
```rust
pub use crate::coordinate::{Coordinate, Region, KindFilter, CoordinateError};
pub use crate::coordinate::DagPosition;
pub use crate::event::{Event, EventHeader, EventKind, HashChain, StoredEvent, EventSourced};
pub use crate::guard::{Gate, GateSet, Denial, Receipt};
pub use crate::outcome::{Outcome, OutcomeError, ErrorKind};
pub use crate::pipeline::{Proposal, Committed};
pub use crate::store::Store;
```

TYPES: none — re-exports only.

IMPL: none.

>[prelude.rs]

---

## src/lib.rs

IMPORTS:
```rust
pub mod wire;
pub mod coordinate;
pub mod outcome;
pub mod event;
pub mod guard;
pub mod pipeline;
pub mod store;
pub mod typestate;
pub mod id;
pub mod prelude;
```

IMPL:
```rust
/// Module declarations in DEPENDENCY ORDER. wire first (zero deps).
/// [SPEC:src/lib.rs — Module declarations in DEPENDENCY ORDER]

/// compile_error guards for impossible configurations:
#[cfg(feature = "async-store")]
compile_error!(
    "INVARIANT 2: batpak does not have an async Store API. \
     Async callers use spawn_blocking() or flume recv_async(). \
     See: src/store/subscription.rs for the async pattern."
);

#[cfg(feature = "sha256")]
compile_error!(
    "INVARIANT 5: blake3 is the only hash. No HashAlgorithm enum. \
     One function: compute_hash(bytes) -> [u8; 32], behind feature = blake3."
);

/// Crate-level doc comment: see [SPEC:src/lib.rs] for structure.
/// P1: what it is. P2: four concepts. P3: hello world. P4: reading order.
```

>[lib.rs]

---

## src/store/index.rs

IMPORTS:
```rust
use crate::coordinate::Coordinate;
use crate::event::{EventKind, HashChain};
use dashmap::DashMap;
use std::collections::{BTreeMap, HashSet};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
```

TYPES:
```rust
/// StoreIndex: in-memory 2D index + auxiliaries. NOT persisted — rebuilt from segments on cold start.
/// [SPEC:src/store/index.rs]
/// [DEP:dashmap::DashMap] — see DEPENDENCY SURFACE for deadlock warnings

pub(crate) struct StoreIndex {
    /// Primary: entity -> ordered events. [DEP:dashmap::DashMap::get_mut] for insert.
    streams: DashMap<Arc<str>, BTreeMap<ClockKey, IndexEntry>>,
    /// Scope dimension: scope -> set of entities in that scope.
    scope_entities: DashMap<Arc<str>, HashSet<Arc<str>>>,
    /// Fact dimension: event kind -> ordered events of that kind.
    by_fact: DashMap<EventKind, BTreeMap<ClockKey, IndexEntry>>,
    /// Point lookup: event_id -> entry. O(1) get by ID.
    by_id: DashMap<u128, IndexEntry>,
    /// Chain head: entity -> latest IndexEntry. For prev_hash in writer step 2.
    latest: DashMap<Arc<str>, IndexEntry>,
    /// Monotonic counter. Foundation for cursors, checkpoints, exactly-once.
    global_sequence: AtomicU64,
    /// Total event count.
    len: AtomicUsize,
    /// Per-entity write locks. Writer step 1 acquires these.
    /// [SPEC:IMPLEMENTATION NOTES item 5 — DashMap guard lifetimes]
    pub(crate) entity_locks: DashMap<Arc<str>, Arc<parking_lot::Mutex<()>>>,
}

/// ClockKey: BTreeMap key. Ord: wall_ms-first, then clock, then uuid tiebreak.
/// [SPEC:IMPLEMENTATION NOTES item 1]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClockKey {
    /// HLC wall clock milliseconds — global ordering across entities.
    pub wall_ms: u64,
    pub clock: u32,
    pub uuid: u128,
}

/// IndexEntry: everything needed for index queries without disk reads.
#[derive(Clone, Debug)]
pub struct IndexEntry {
    pub event_id: u128,
    pub correlation_id: u128,
    pub causation_id: Option<u128>,
    pub coord: Coordinate,
    pub kind: EventKind,
    /// HLC wall clock milliseconds — for global causal ordering.
    pub wall_ms: u64,
    pub clock: u32,
    pub hash_chain: HashChain,
    pub disk_pos: DiskPos,
    pub global_sequence: u64,
}

/// DiskPos: where to find this event on disk.
#[derive(Clone, Debug)]
pub struct DiskPos {
    pub segment_id: u64,
    pub offset: u64,
    pub length: u32,
}
```

IMPL:
```rust
impl Ord for ClockKey {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.wall_ms.cmp(&other.wall_ms)
            .then(self.clock.cmp(&other.clock))
            .then(self.uuid.cmp(&other.uuid))
    }
}
impl PartialOrd for ClockKey {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl IndexEntry {
    pub fn is_correlated(&self) -> bool { self.event_id != self.correlation_id }
    pub fn is_caused_by(&self, event_id: u128) -> bool { self.causation_id == Some(event_id) }
    pub fn is_root_cause(&self) -> bool { self.causation_id.is_none() }
}

impl StoreIndex {
    pub(crate) fn new() -> Self {
        Self {
            streams: DashMap::new(),
            scope_entities: DashMap::new(),
            by_fact: DashMap::new(),
            by_id: DashMap::new(),
            latest: DashMap::new(),
            global_sequence: AtomicU64::new(0),
            len: AtomicUsize::new(0),
            entity_locks: DashMap::new(),
        }
    }

    /// Called by writer step 9. Inserts into ALL indexes atomically.
    /// CONSTRAINT: caller MUST hold the entity lock before calling this.
    /// [SPEC:IMPLEMENTATION NOTES item 5]
    pub(crate) fn insert(&self, entry: IndexEntry) {
        let entity = entry.coord.entity_arc();
        let scope = entry.coord.scope_arc();
        let key = ClockKey { wall_ms: entry.wall_ms, clock: entry.clock, uuid: entry.event_id };

        /// Primary index: entity -> BTreeMap
        /// [DEP:dashmap::DashMap::entry] — holds write lock, release fast
        self.streams.entry(entity.clone())
            .or_insert_with(BTreeMap::new)
            .insert(key.clone(), entry.clone());

        /// Scope index
        self.scope_entities.entry(scope)
            .or_insert_with(HashSet::new)
            .insert(entity.clone());

        /// Fact index
        self.by_fact.entry(entry.kind)
            .or_insert_with(BTreeMap::new)
            .insert(key, entry.clone());

        /// Point lookup
        self.by_id.insert(entry.event_id, entry.clone());

        /// Chain head
        self.latest.insert(entity, entry);

        /// Counters
        self.global_sequence.fetch_add(1, Ordering::SeqCst);
        self.len.fetch_add(1, Ordering::Relaxed);
    }

    pub(crate) fn get_by_id(&self, event_id: u128) -> Option<IndexEntry> {
        self.by_id.get(&event_id).map(|r| r.value().clone())
    }

    pub(crate) fn get_latest(&self, entity: &str) -> Option<IndexEntry> {
        self.latest.get(entity).map(|r| r.value().clone())
    }

    pub(crate) fn stream(&self, entity: &str) -> Vec<IndexEntry> {
        self.streams.get(entity)
            .map(|r| r.value().values().cloned().collect())
            .unwrap_or_default()
    }

    pub(crate) fn query(&self, region: &crate::coordinate::Region) -> Vec<IndexEntry> {
        /// Region query strategy:
        /// 1. Determine candidate set based on most selective filter
        /// 2. Apply remaining filters to narrow results
        /// 3. Apply clock_range last (it's per-entity, cheap)
        use crate::coordinate::KindFilter;

        let mut candidates: Vec<IndexEntry> = if let Some(ref prefix) = region.entity_prefix {
            /// Entity prefix → scan streams map for matching keys
            /// [DEP:dashmap::DashMap::iter] — NOT a consistent snapshot, fine for queries
            self.streams.iter()
                .filter(|r| r.key().as_ref().starts_with(prefix.as_ref()))
                .flat_map(|r| r.value().values().cloned())
                .collect()
        } else if let Some(ref scope) = region.scope {
            /// Scope → look up entities in scope, collect their streams
            if let Some(entities) = self.scope_entities.get(scope.as_ref()) {
                entities.value().iter()
                    .flat_map(|entity| {
                        self.streams.get(entity.as_ref())
                            .map(|r| r.value().values().cloned().collect::<Vec<_>>())
                            .unwrap_or_default()
                    })
                    .collect()
            } else {
                Vec::new()
            }
        } else if let Some(ref fact) = region.fact {
            /// Fact filter without entity/scope → scan by_fact index
            match fact {
                KindFilter::Exact(k) => {
                    self.by_fact.get(k)
                        .map(|r| r.value().values().cloned().collect())
                        .unwrap_or_default()
                }
                KindFilter::Category(c) => {
                    let cat = *c;
                    self.by_fact.iter()
                        .filter(|r| r.key().category() == cat)
                        .flat_map(|r| r.value().values().cloned())
                        .collect()
                }
                KindFilter::Any => {
                    /// No filter at all — return everything (expensive, use sparingly)
                    self.streams.iter()
                        .flat_map(|r| r.value().values().cloned())
                        .collect()
                }
            }
        } else {
            /// Region::all() with no filters — return everything
            self.streams.iter()
                .flat_map(|r| r.value().values().cloned())
                .collect()
        };

        /// Apply remaining filters that weren't used for the initial candidate set.

        /// Scope filter (if entity_prefix was the primary selector)
        if region.entity_prefix.is_some() {
            if let Some(ref scope) = region.scope {
                candidates.retain(|e| e.coord.scope() == scope.as_ref());
            }
        }

        /// Entity prefix filter (if scope was the primary selector)
        if region.scope.is_some() && region.entity_prefix.is_none() {
            if let Some(ref prefix) = region.entity_prefix {
                candidates.retain(|e| e.coord.entity().starts_with(prefix.as_ref()));
            }
        }

        /// Fact filter (if not already applied)
        if region.entity_prefix.is_some() || region.scope.is_some() {
            if let Some(ref fact) = region.fact {
                candidates.retain(|e| match fact {
                    KindFilter::Exact(k) => e.kind == *k,
                    KindFilter::Category(c) => e.kind.category() == *c,
                    KindFilter::Any => true,
                });
            }
        }

        /// Clock range filter (always per-entity clock, not global_sequence)
        if let Some((min, max)) = region.clock_range {
            candidates.retain(|e| e.clock >= min && e.clock <= max);
        }

        /// Sort by global_sequence for consistent ordering
        candidates.sort_by_key(|e| e.global_sequence);
        candidates
    }

    pub(crate) fn global_sequence(&self) -> u64 {
        self.global_sequence.load(Ordering::SeqCst)
    }

    pub(crate) fn len(&self) -> usize {
        self.len.load(Ordering::Relaxed)
    }
}
```

>[index.rs]

---

## src/store/segment.rs

IMPORTS:
```rust
use crate::event::Event;
use crate::store::StoreError;
use serde::{Deserialize, Serialize};
use std::io::{self, Read, Write};
// NOTE: No `use crate::wire::*` needed. serde(with) resolves via string path.
```

TYPES:
```rust
/// Segment file format: magic + header + frames.
/// Magic: b"FBAT" (4 bytes). Header: 32 bytes. Frame: [len:u32 BE][crc32:u32 BE][msgpack]
/// Files named: {segment_id:06}.fbat (e.g., 000001.fbat). Sequential u64.
/// [SPEC:src/store/segment.rs]

pub const SEGMENT_MAGIC: &[u8; 4] = b"FBAT";
pub const SEGMENT_HEADER_SIZE: usize = 32;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SegmentHeader {
    pub version: u16,
    pub flags: u16,
    pub created_ns: i64,
    pub segment_id: u64,
}

/// FramePayload: what gets serialized into each frame.
/// entity and scope are stored as strings (not Coordinate) because segments
/// are the persistence layer — they don't depend on the Coordinate type.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FramePayload<P> {
    pub event: Event<P>,
    pub entity: String,
    pub scope: String,
}

/// Typestate for segment lifecycle.
pub struct Active;
pub struct Sealed;
pub struct Segment<State> {
    pub header: SegmentHeader,
    pub path: std::path::PathBuf,
    file: Option<std::fs::File>,
    written_bytes: u64,
    _state: std::marker::PhantomData<State>,
}

#[derive(Debug)]
pub struct CompactionResult {
    pub segments_removed: usize,
    pub bytes_reclaimed: u64,
}
```

IMPL:
```rust
/// frame_encode: serialize data to msgpack, wrap in [len:u32 BE][crc32:u32 BE][msgpack]
/// [SPEC:WIRE FORMAT DECISIONS — ALWAYS rmp_serde::to_vec_named()]
/// [DEP:rmp_serde::to_vec_named] → Result<Vec<u8>, encode::Error>
/// [DEP:crc32fast::hash] → u32

pub fn frame_encode<T: serde::Serialize>(data: &T) -> Result<Vec<u8>, StoreError> {
    let msgpack = rmp_serde::to_vec_named(data)
        .map_err(|e| StoreError::Serialization(e.to_string()))?;
    let crc = crc32fast::hash(&msgpack);
    let len = msgpack.len() as u32;

    let mut frame = Vec::with_capacity(8 + msgpack.len());
    frame.extend_from_slice(&len.to_be_bytes());
    frame.extend_from_slice(&crc.to_be_bytes());
    frame.extend_from_slice(&msgpack);
    Ok(frame)
}

/// frame_decode: read [len][crc][msgpack], verify CRC, return msgpack bytes.
/// Returns (msgpack_bytes, total_frame_size_consumed).
pub fn frame_decode(buf: &[u8]) -> Result<(&[u8], usize), StoreError> {
    if buf.len() < 8 {
        return Err(StoreError::CorruptSegment {
            segment_id: 0, detail: "frame too short for header".into()
        });
    }
    let len = u32::from_be_bytes([buf[0], buf[1], buf[2], buf[3]]) as usize;
    let expected_crc = u32::from_be_bytes([buf[4], buf[5], buf[6], buf[7]]);
    if buf.len() < 8 + len {
        return Err(StoreError::CorruptSegment {
            segment_id: 0, detail: "frame truncated".into()
        });
    }
    let msgpack = &buf[8..8 + len];
    let actual_crc = crc32fast::hash(msgpack);
    if actual_crc != expected_crc {
        return Err(StoreError::CrcMismatch { segment_id: 0, offset: 0 });
    }
    Ok((msgpack, 8 + len))
}

/// Segment naming helper.
pub fn segment_filename(segment_id: u64) -> String {
    format!("{:06}.fbat", segment_id)
}

impl Segment<Active> {
    /// Create new active segment.
    pub fn create(dir: &std::path::Path, segment_id: u64) -> Result<Self, StoreError> {
        let path = dir.join(segment_filename(segment_id));
        /// Create segment file exclusively (fail if exists).
        /// [SPEC:IMPLEMENTATION NOTES item 7 — MSRV standard library]
        let mut file = std::fs::OpenOptions::new()
            .write(true).create_new(true).open(&path)
            .map_err(StoreError::Io)?;

        let header = SegmentHeader {
            version: 1, flags: 0,
            created_ns: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default().as_nanos() as i64,
            segment_id,
        };

        /// Write magic + header
        file.write_all(SEGMENT_MAGIC).map_err(StoreError::Io)?;
        let header_bytes = rmp_serde::to_vec_named(&header)
            .map_err(|e| StoreError::Serialization(e.to_string()))?;
        file.write_all(&header_bytes).map_err(StoreError::Io)?;

        Ok(Self {
            header, path, file: Some(file),
            written_bytes: (4 + header_bytes.len()) as u64,
            _state: std::marker::PhantomData,
        })
    }

    /// Write a frame. Returns offset where frame starts.
    pub fn write_frame(&mut self, frame: &[u8]) -> Result<u64, StoreError> {
        let offset = self.written_bytes;
        if let Some(ref mut f) = self.file {
            f.write_all(frame).map_err(StoreError::Io)?;
        }
        self.written_bytes += frame.len() as u64;
        Ok(offset)
    }

    pub fn needs_rotation(&self, max_bytes: u64) -> bool {
        self.written_bytes >= max_bytes
    }

    pub fn sync(&mut self) -> Result<(), StoreError> {
        if let Some(ref f) = self.file {
            f.sync_all().map_err(StoreError::Io)?;
        }
        Ok(())
    }

    /// Seal: close file handle, transition to Sealed.
    pub fn seal(mut self) -> Segment<Sealed> {
        drop(self.file.take());
        Segment {
            header: self.header, path: self.path, file: None,
            written_bytes: self.written_bytes, _state: std::marker::PhantomData,
        }
    }
}
```

>[segment.rs]

---

## src/store/reader.rs

IMPORTS:
```rust
use crate::coordinate::Coordinate;
use crate::event::{Event, StoredEvent};
use crate::store::segment::{self, FramePayload, SEGMENT_MAGIC};
use crate::store::{StoreError, DiskPos};
use parking_lot::Mutex;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
```

TYPES:
```rust
/// Reader: reads events from segment files. LRU file descriptor cache.
/// Behind parking_lot::Mutex for Send + Sync. [SPEC:src/store/reader.rs]
/// [SPEC:IMPLEMENTATION NOTES item 6 — Store is Send + Sync]

pub(crate) struct Reader {
    data_dir: PathBuf,
    /// LRU FD cache: segment_id -> open File handle. Evicts oldest when full.
    /// [DEP:parking_lot::Mutex] — lock() returns guard directly, no poisoning
    fd_cache: Mutex<FdCache>,
}

struct FdCache {
    fds: HashMap<u64, File>,
    order: Vec<u64>,  // LRU order: most recent at end
    budget: usize,
}

/// ScannedEntry: what cold start produces per event in a segment.
pub(crate) struct ScannedEntry {
    pub event: Event<serde_json::Value>,
    pub entity: String,
    pub scope: String,
    pub segment_id: u64,
    pub offset: u64,
    pub length: u32,
}
```

IMPL:
```rust
impl Reader {
    pub(crate) fn new(data_dir: PathBuf, fd_budget: usize) -> Self {
        Self {
            data_dir,
            fd_cache: Mutex::new(FdCache {
                fds: HashMap::new(),
                order: Vec::new(),
                budget: fd_budget,
            }),
        }
    }

    /// Read a single event by disk position. CRC32 verified.
    /// [DEP:crc32fast::hash] verifies frame integrity on every read.
    pub(crate) fn read_entry(&self, pos: &DiskPos)
        -> Result<StoredEvent<serde_json::Value>, StoreError>
    {
        let file = self.get_fd(pos.segment_id)?;
        let mut buf = vec![0u8; pos.length as usize];

        /// Use pread (read_at) — doesn't modify file cursor. [SPEC:IMPLEMENTATION NOTES item 7]
        #[cfg(unix)]
        {
            use std::os::unix::fs::FileExt;
            file.read_at(&mut buf, pos.offset).map_err(StoreError::Io)?;
        }
        #[cfg(not(unix))]
        {
            /// Fallback: seek + read (holds the mutex so this is safe)
            use std::io::{Seek, SeekFrom};
            let mut file = file; // need mut for seek
            file.seek(SeekFrom::Start(pos.offset)).map_err(StoreError::Io)?;
            file.read_exact(&mut buf).map_err(StoreError::Io)?;
        }

        let (msgpack, _) = segment::frame_decode(&buf)?;
        let payload: FramePayload<serde_json::Value> = rmp_serde::from_slice(msgpack)
            .map_err(|e| StoreError::Serialization(e.to_string()))?;

        let coord = Coordinate::new(&payload.entity, &payload.scope)
            .map_err(StoreError::Coordinate)?;
        Ok(StoredEvent { coordinate: coord, event: payload.event })
    }

    /// Scan an entire segment for cold start. Returns all events in order.
    pub(crate) fn scan_segment(&self, path: &Path)
        -> Result<Vec<ScannedEntry>, StoreError>
    {
        let mut file = File::open(path).map_err(StoreError::Io)?;
        let mut all_bytes = Vec::new();
        file.read_to_end(&mut all_bytes).map_err(StoreError::Io)?;

        /// Verify magic
        if all_bytes.len() < 4 || &all_bytes[..4] != SEGMENT_MAGIC {
            return Err(StoreError::CorruptSegment {
                segment_id: 0, detail: "bad magic".into()
            });
        }

        /// Extract segment_id from filename: "000042.fbat" → 42
        let segment_id = path.file_stem()
            .and_then(|s| s.to_str())
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(0);

        /// Skip magic (4 bytes). Parse segment header from msgpack.
        /// [DEP:rmp_serde::from_slice] — deserialize SegmentHeader
        let after_magic = &all_bytes[4..];
        let _header: segment::SegmentHeader = rmp_serde::from_slice(after_magic)
            .map_err(|e| StoreError::Serialization(e.to_string()))?;

        /// Find where header ends and frames begin.
        /// Re-encode header to measure its serialized size (simplest approach).
        let header_bytes = rmp_serde::to_vec_named(&_header)
            .map_err(|e| StoreError::Serialization(e.to_string()))?;
        let mut cursor = 4 + header_bytes.len();

        /// Read frames until EOF. Each frame: [len:u32 BE][crc32:u32 BE][msgpack]
        let mut entries = Vec::new();
        while cursor < all_bytes.len() {
            let remaining = &all_bytes[cursor..];
            if remaining.len() < 8 { break; } // not enough for a frame header

            let frame_offset = cursor as u64;
            match segment::frame_decode(remaining) {
                Ok((msgpack, frame_size)) => {
                    /// Deserialize frame payload
                    match rmp_serde::from_slice::<FramePayload<serde_json::Value>>(msgpack) {
                        Ok(payload) => {
                            entries.push(ScannedEntry {
                                event: payload.event,
                                entity: payload.entity,
                                scope: payload.scope,
                                segment_id,
                                offset: frame_offset,
                                length: frame_size as u32,
                            });
                        }
                        Err(e) => {
                            tracing::warn!(
                                segment_id, offset = frame_offset,
                                "skipping unreadable frame: {e}"
                            );
                        }
                    }
                    cursor += frame_size;
                }
                Err(StoreError::CrcMismatch { .. }) => {
                    tracing::warn!(segment_id, offset = frame_offset, "CRC mismatch, skipping frame");
                    break; // CRC mismatch = stop scanning this segment
                }
                Err(_) => break, // truncated or corrupt — stop
            }
        }
        Ok(entries)
    }

    fn get_fd(&self, segment_id: u64) -> Result<File, StoreError> {
        let mut cache = self.fd_cache.lock();
        /// LRU logic: if in cache, move to end of order vec. If not, open file,
        /// evict oldest if over budget, insert.
        if let Some(pos) = cache.order.iter().position(|&id| id == segment_id) {
            cache.order.remove(pos);
            cache.order.push(segment_id);
            return Ok(cache.fds[&segment_id].try_clone().map_err(StoreError::Io)?);
        }

        let path = self.data_dir.join(segment::segment_filename(segment_id));
        let file = File::open(&path).map_err(StoreError::Io)?;

        if cache.fds.len() >= cache.budget {
            if let Some(oldest) = cache.order.first().copied() {
                cache.fds.remove(&oldest);
                cache.order.remove(0);
            }
        }

        cache.fds.insert(segment_id, file.try_clone().map_err(StoreError::Io)?);
        cache.order.push(segment_id);
        Ok(file)
    }
}
```

>[reader.rs]

---

## src/store/writer.rs

IMPORTS:
```rust
use crate::coordinate::{Coordinate, DagPosition};
use crate::event::{Event, EventHeader, EventKind, HashChain};
use crate::store::index::{StoreIndex, IndexEntry, ClockKey, DiskPos};
use crate::store::segment::{self, Segment, Active, FramePayload};
use crate::store::{StoreConfig, StoreError, AppendReceipt};
use flume::{Sender, Receiver, TrySendError};
use parking_lot::Mutex;
use std::sync::Arc;
use tracing::{debug, info, warn, trace};
```

TYPES:
```rust
/// WriterCommand: messages sent to the background writer thread via flume.
/// All respond channels: flume::Sender — sync send from writer, async recv from caller.
/// [SPEC:src/store/writer.rs]

pub(crate) enum WriterCommand {
    Append {
        entity: Arc<str>,
        scope: Arc<str>,
        event: Event<Vec<u8>>,  // pre-serialized payload as msgpack bytes
        kind: EventKind,
        correlation_id: u128,
        causation_id: Option<u128>,
        respond: Sender<Result<AppendReceipt, StoreError>>,
    },
    Sync {
        respond: Sender<Result<(), StoreError>>,
    },
    Shutdown {
        respond: Sender<Result<(), StoreError>>,
    },
}

/// WriterHandle: owned by Store. Communicates with the background thread.
pub(crate) struct WriterHandle {
    pub tx: Sender<WriterCommand>,
    pub subscribers: Arc<SubscriberList>,
    thread: Option<std::thread::JoinHandle<()>>,
}

/// SubscriberList: push-based notification fanout via flume channels.
/// [SPEC:src/store/writer.rs — try_send pattern]
pub(crate) struct SubscriberList {
    senders: Mutex<Vec<Sender<Notification>>>,
}

/// Notification: lightweight event summary pushed to subscribers.
/// Must derive Clone (used in try_send broadcast loop).
/// [SPEC:src/store/writer.rs — Notification struct]
#[derive(Clone, Debug)]
pub struct Notification {
    pub event_id: u128,
    pub correlation_id: u128,
    pub causation_id: Option<u128>,
    pub coord: Coordinate,
    pub kind: EventKind,
    pub sequence: u64,
}

/// RestartPolicy: how the writer recovers from panics.
/// [SPEC:src/store/writer.rs — RestartPolicy]
/// EXACTLY two variants. Adding a third violates the RED FLAGS.
#[derive(Clone, Debug)]
pub enum RestartPolicy {
    Once,
    Bounded { max_restarts: u32, within_ms: u64 },
}

impl Default for RestartPolicy {
    fn default() -> Self { Self::Once }
}
```

IMPL:
```rust
impl SubscriberList {
    pub(crate) fn new() -> Self {
        Self { senders: Mutex::new(Vec::new()) }
    }

    /// Subscribe: create a new bounded channel, store the sender, return the receiver.
    pub(crate) fn subscribe(&self, capacity: usize) -> Receiver<Notification> {
        let (tx, rx) = flume::bounded(capacity);
        self.senders.lock().push(tx);
        rx
    }

    /// Broadcast: try_send to all, retain on Ok or Full, prune on Disconnected.
    /// NEVER use blocking send() — one slow subscriber must not block the writer.
    /// [DEP:flume::Sender::try_send] → Result<(), TrySendError<T>>
    /// [DEP:flume::TrySendError::Full] / [DEP:flume::TrySendError::Disconnected]
    pub(crate) fn broadcast(&self, notif: Notification) {
        let mut guard = self.senders.lock();
        guard.retain(|tx| match tx.try_send(notif.clone()) {
            Ok(()) => true,
            Err(TrySendError::Full(_)) => true,
            Err(TrySendError::Disconnected(_)) => false,
        });
    }
}

impl WriterHandle {
    /// Spawn the background writer thread.
    /// [SPEC:src/store/writer.rs — "batpak-writer-{hash}" thread]
    pub(crate) fn spawn(
        config: &Arc<StoreConfig>,
        index: &Arc<StoreIndex>,
        subscribers: &Arc<SubscriberList>,
    ) -> Result<Self, StoreError> {
        std::fs::create_dir_all(&config.data_dir).map_err(StoreError::Io)?;
        let initial_segment_id = find_latest_segment_id(&config.data_dir).unwrap_or(0) + 1;
        let initial_segment = Segment::<Active>::create(&config.data_dir, initial_segment_id)?;

        let (tx, rx) = flume::bounded::<WriterCommand>(config.writer_channel_capacity);
        let subs = Arc::clone(subscribers);
        let cfg = Arc::clone(config);
        let idx = Arc::clone(index);

        let thread_name = format!("batpak-writer-{:08x}", {
            let mut h: u64 = 0xcbf29ce484222325; // FNV-1a basis
            for b in config.data_dir.to_string_lossy().bytes() {
                h ^= b as u64;
                h = h.wrapping_mul(0x100000001b3); // FNV-1a prime
            }
            h
        });

        let thread = std::thread::Builder::new()
            .name(thread_name)
            .spawn(move || {
                writer_loop(&rx, &cfg, &idx, &subs, initial_segment, initial_segment_id);
            })
            .map_err(StoreError::Io)?;

        Ok(Self { tx, subscribers: Arc::clone(subscribers), _thread: Some(thread) })
    }

    /// NOTE: No send_append() method here. Store::append() and Store::append_reaction()
    /// in store/mod.rs create one-shot flume channels and send WriterCommands directly
    /// via self.writer.tx.send(). This avoids an unnecessary abstraction layer.
    /// WriterHandle.tx is pub(crate) for direct access. [SPEC:INVARIANTS item 4]
}

/// The writer's main loop. Runs on the background thread.
fn writer_loop(
    rx: Receiver<WriterCommand>,
    config: Arc<StoreConfig>,
    index: Arc<StoreIndex>,
    subscribers: Arc<SubscriberList>,
) {
    let data_dir = &config.data_dir;
    /// Initialize: create data_dir if not exists, find latest segment or create first.
    std::fs::create_dir_all(data_dir).expect("create data dir");
    let mut segment_id: u64 = find_latest_segment_id(data_dir).unwrap_or(0) + 1;
    let mut active_segment = Segment::<Active>::create(data_dir, segment_id)
        .expect("create initial segment");
    let mut events_since_sync: u32 = 0;

    /// Main loop: recv commands, dispatch.
    for cmd in rx.iter() {
        match cmd {
            WriterCommand::Append { entity, scope, event, kind,
                                    correlation_id, causation_id, respond } => {
                let result = handle_append(
                    &entity, &scope, event, kind, correlation_id, causation_id,
                    &index, &mut active_segment, &mut segment_id,
                    &config, &subscribers,
                );
                /// Respond to caller. Ignore send error (caller may have dropped).
                let _ = respond.send(result);

                events_since_sync += 1;
                if events_since_sync >= config.sync_every_n_events {
                    let _ = active_segment.sync_with_mode(&config.sync_mode);
                    events_since_sync = 0;
                }
            }
            WriterCommand::Sync { respond } => {
                let result = active_segment.sync_with_mode(&config.sync_mode);
                let _ = respond.send(result);
                events_since_sync = 0;
            }
            WriterCommand::Shutdown { respond } => {
                /// Drain up to shutdown_drain_limit queued commands.
                /// [SPEC:src/store/writer.rs — Shutdown drain semantics]
                let mut drained = 0;
                while drained < config.shutdown_drain_limit {
                    match rx.try_recv() {
                        Ok(WriterCommand::Append { entity, scope, event, kind,
                                                   correlation_id, causation_id, respond: r }) => {
                            let result = handle_append(
                                &entity, &scope, event, kind, correlation_id, causation_id,
                                &index, &mut active_segment, &mut segment_id,
                                &config, &subscribers,
                            );
                            let _ = r.send(result);
                            drained += 1;
                        }
                        Ok(WriterCommand::Shutdown { respond: r }) => {
                            let _ = r.send(Ok(()));
                        }
                        Ok(WriterCommand::Sync { respond: r }) => {
                            let _ = r.send(active_segment.sync_with_mode(&config.sync_mode));
                        }
                        Err(_) => break, // channel empty
                    }
                }
                let _ = active_segment.sync_with_mode(&config.sync_mode);
                let _ = respond.send(Ok(()));
                return; // exit writer loop
            }
        }
    }
}

/// The 10-step commit protocol.
/// [SPEC:src/store/writer.rs — handle_append]
fn handle_append(
    entity: &Arc<str>,
    scope: &Arc<str>,
    mut event: Event<Vec<u8>>,
    kind: EventKind,
    correlation_id: u128,
    causation_id: Option<u128>,
    index: &StoreIndex,
    active_segment: &mut Segment<Active>,
    segment_id: &mut u64,
    config: &StoreConfig,
    subscribers: &SubscriberList,
) -> Result<AppendReceipt, StoreError> {

    /// STEP 1: Acquire per-entity lock.
    /// [SPEC:IMPLEMENTATION NOTES item 5 — DashMap guard lifetimes]
    /// Clone the Arc<Mutex> OUT of DashMap, drop the DashMap entry guard,
    /// THEN lock the Mutex. Never hold DashMap Ref across the commit.
    let lock = index.entity_locks.entry(entity.clone())
        .or_insert_with(|| Arc::new(parking_lot::Mutex::new(())))
        .clone();
    let _entity_guard = lock.lock();
    debug!(entity = %entity, "entity lock acquired");

    /// STEP 2: Get prev_hash from index (or [0u8;32] for genesis).
    /// Clone the value out of the DashMap Ref immediately.
    let prev_hash = index.get_latest(entity)
        .map(|e| e.hash_chain.event_hash)
        .unwrap_or([0u8; 32]);

    /// STEP 3: Compute sequence (latest.clock + 1, or 0).
    let clock = index.get_latest(entity)
        .map(|e| e.clock + 1)
        .unwrap_or(0);

    /// STEP 4: Set event header position with HLC wall clock.
    /// Ensure wall_ms is monotonically non-decreasing per entity.
    let raw_ms = (event.header.timestamp_us / 1000) as u64;
    let last_ms = index.get_latest(entity).map(|e| e.wall_ms).unwrap_or(0);
    let now_ms = raw_ms.max(last_ms);
    let position = DagPosition::child_at(clock, now_ms, 0);
    event.header.position = position;
    event.header.event_kind = kind;
    event.header.correlation_id = correlation_id;
    event.header.causation_id = causation_id;

    /// STEP 5: Compute blake3 hash, set hash chain (or skip if feature off).
    /// [SPEC:INVARIANTS item 5 — blake3 only]
    let payload_for_hash = &event.payload; // pre-serialized bytes
    #[cfg(feature = "blake3")]
    let event_hash = crate::event::hash::compute_hash(payload_for_hash);
    #[cfg(not(feature = "blake3"))]
    let event_hash = [0u8; 32];

    event.hash_chain = Some(HashChain { prev_hash, event_hash });

    /// STEP 6: Serialize to MessagePack + CRC32 frame.
    /// [SPEC:WIRE FORMAT DECISIONS — rmp_serde::to_vec_named() ALWAYS]
    let frame_payload = FramePayload {
        event: event.clone(),
        entity: entity.to_string(),
        scope: scope.to_string(),
    };
    let frame = segment::frame_encode(&frame_payload)?;

    /// STEP 7: Check segment rotation.
    if active_segment.needs_rotation(config.segment_max_bytes) {
        active_segment.sync_with_mode(&config.sync_mode)?;
        let old = std::mem::replace(
            active_segment,
            Segment::<Active>::create(&config.data_dir, *segment_id + 1)?,
        );
        let _sealed = old.seal();
        *segment_id += 1;
        info!(segment_id = *segment_id, "segment rotated");
    }

    /// STEP 8: Write frame to segment file.
    let offset = active_segment.write_frame(&frame)?;
    trace!(offset = offset, len = frame.len(), "frame written");

    /// STEP 9: Update index.
    let global_seq = index.global_sequence();
    let disk_pos = DiskPos {
        segment_id: *segment_id,
        offset,
        length: frame.len() as u32,
    };
    let entry = IndexEntry {
        event_id: event.header.event_id,
        correlation_id,
        causation_id,
        coord: Coordinate::new(entity.as_ref(), scope.as_ref())
            .map_err(StoreError::Coordinate)?,
        kind,
        wall_ms: now_ms,
        clock,
        hash_chain: event.hash_chain.clone().unwrap_or_default(),
        disk_pos: disk_pos.clone(),
        global_sequence: global_seq,
    };
    index.insert(entry);
    debug!(event_id = %event.header.event_id, clock = clock, "append committed");

    /// STEP 10: Broadcast notification to subscribers.
    subscribers.broadcast(Notification {
        event_id: event.header.event_id,
        correlation_id,
        causation_id,
        coord: Coordinate::new(entity.as_ref(), scope.as_ref())
            .map_err(StoreError::Coordinate)?,
        kind,
        sequence: global_seq,
    });

    Ok(AppendReceipt {
        event_id: event.header.event_id,
        sequence: global_seq,
        disk_pos,
    })
}

/// Find the latest segment ID by scanning data_dir for .fbat files.
fn find_latest_segment_id(dir: &std::path::Path) -> Option<u64> {
    std::fs::read_dir(dir).ok()?
        .filter_map(|e| e.ok())
        .filter_map(|e| {
            let name = e.file_name();
            let name = name.to_str()?;
            if name.ends_with(".fbat") {
                name.trim_end_matches(".fbat").parse::<u64>().ok()
            } else { None }
        })
        .max()
}
```

>[writer.rs]

---

## src/store/projection.rs

IMPORTS:
```rust
use crate::store::StoreError;
use serde::{Deserialize, Serialize};
```

TYPES:
```rust
/// ProjectionCache: trait for caching projected state.
/// Three impls: NoCache (default), RedbCache (optional), LmdbCache (optional).
/// [SPEC:src/store/projection.rs]

pub trait ProjectionCache: Send + Sync + 'static {
    fn get(&self, key: &[u8]) -> Result<Option<(Vec<u8>, CacheMeta)>, StoreError>;
    fn put(&self, key: &[u8], value: &[u8], meta: CacheMeta) -> Result<(), StoreError>;
    fn delete_prefix(&self, prefix: &[u8]) -> Result<u64, StoreError>;
    fn sync(&self) -> Result<(), StoreError>;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CacheMeta {
    pub watermark: u64,
    pub cached_at_us: i64,
}

#[derive(Clone, Debug)]
pub enum Freshness {
    Consistent,
    BestEffort { max_stale_ms: u64 },
}

/// NoCache: default. Every read replays from segments. No state.
pub struct NoCache;
```

IMPL:
```rust
impl ProjectionCache for NoCache {
    fn get(&self, _key: &[u8]) -> Result<Option<(Vec<u8>, CacheMeta)>, StoreError> {
        Ok(None) // always miss — forces replay
    }
    fn put(&self, _key: &[u8], _value: &[u8], _meta: CacheMeta) -> Result<(), StoreError> {
        Ok(()) // no-op
    }
    fn delete_prefix(&self, _prefix: &[u8]) -> Result<u64, StoreError> {
        Ok(0) // nothing to delete
    }
    fn sync(&self) -> Result<(), StoreError> {
        Ok(()) // nothing to sync
    }
}

/// RedbCache: backed by redb embedded database.
/// [DEP:redb::Database::create] [DEP:redb::TableDefinition]
#[cfg(feature = "redb")]
pub struct RedbCache {
    db: redb::Database,
}

#[cfg(feature = "redb")]
const CACHE_TABLE: redb::TableDefinition<&[u8], &[u8]> = redb::TableDefinition::new("projection_cache");

#[cfg(feature = "redb")]
impl RedbCache {
    pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self, StoreError> {
        let db = redb::Database::create(path.as_ref())
            .map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        Ok(Self { db })
    }
}

#[cfg(feature = "redb")]
impl ProjectionCache for RedbCache {
    fn get(&self, key: &[u8]) -> Result<Option<(Vec<u8>, CacheMeta)>, StoreError> {
        let txn = self.db.begin_read().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        let table = txn.open_table(CACHE_TABLE).map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        match table.get(key) {
            Ok(Some(guard)) => {
                let bytes = guard.value().to_vec();
                /// Last 16 bytes = CacheMeta (watermark u64 LE + cached_at_us i64 LE)
                if bytes.len() < 16 {
                    return Ok(None);
                }
                let (value, meta_bytes) = bytes.split_at(bytes.len() - 16);
                let watermark = u64::from_le_bytes(meta_bytes[..8].try_into().unwrap());
                let cached_at_us = i64::from_le_bytes(meta_bytes[8..16].try_into().unwrap());
                Ok(Some((value.to_vec(), CacheMeta { watermark, cached_at_us })))
            }
            Ok(None) => Ok(None),
            Err(e) => Err(StoreError::CacheFailed(e.to_string())),
        }
    }

    fn put(&self, key: &[u8], value: &[u8], meta: CacheMeta) -> Result<(), StoreError> {
        /// Append CacheMeta as last 16 bytes of value
        let mut buf = Vec::with_capacity(value.len() + 16);
        buf.extend_from_slice(value);
        buf.extend_from_slice(&meta.watermark.to_le_bytes());
        buf.extend_from_slice(&meta.cached_at_us.to_le_bytes());

        let txn = self.db.begin_write().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        {
            let mut table = txn.open_table(CACHE_TABLE).map_err(|e| StoreError::CacheFailed(e.to_string()))?;
            table.insert(key, buf.as_slice()).map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        }
        txn.commit().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        Ok(())
    }

    fn delete_prefix(&self, prefix: &[u8]) -> Result<u64, StoreError> {
        /// redb has no built-in delete_prefix. Iterate range + collect keys + delete.
        let txn = self.db.begin_write().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        let mut count = 0u64;
        {
            let mut table = txn.open_table(CACHE_TABLE).map_err(|e| StoreError::CacheFailed(e.to_string()))?;
            /// Range: prefix..prefix_with_ff_appended
            let mut end = prefix.to_vec();
            end.push(0xFF);
            let keys: Vec<Vec<u8>> = table.range(prefix..end.as_slice())
                .map_err(|e| StoreError::CacheFailed(e.to_string()))?
                .filter_map(|r| r.ok())
                .map(|(k, _)| k.value().to_vec())
                .collect();
            for key in &keys {
                table.remove(key.as_slice()).map_err(|e| StoreError::CacheFailed(e.to_string()))?;
                count += 1;
            }
        }
        txn.commit().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        Ok(count)
    }

    fn sync(&self) -> Result<(), StoreError> {
        Ok(()) // redb commits are durable by default
    }
}

/// LmdbCache: backed by LMDB via heed.
/// [DEP:heed::EnvOpenOptions] — open() is unsafe (must not double-open same dir)
#[cfg(feature = "lmdb")]
pub struct LmdbCache {
    env: heed::Env,
    db: heed::Database<heed::types::Bytes, heed::types::Bytes>,
}

#[cfg(feature = "lmdb")]
impl LmdbCache {
    pub fn open(path: impl AsRef<std::path::Path>, map_size: usize) -> Result<Self, StoreError> {
        std::fs::create_dir_all(path.as_ref()).map_err(StoreError::Io)?;
        /// SAFETY: We guarantee this path is opened at most once per process.
        /// The Store owns the LmdbCache exclusively.
        let env = unsafe {
            heed::EnvOpenOptions::new()
                .map_size(map_size)
                .max_dbs(1)
                .open(path.as_ref())
                .map_err(|e| StoreError::CacheFailed(e.to_string()))?
        };
        let mut wtxn = env.write_txn().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        let db = env.create_database(&mut wtxn, Some("projection_cache"))
            .map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        wtxn.commit().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        Ok(Self { env, db })
    }
}

#[cfg(feature = "lmdb")]
impl ProjectionCache for LmdbCache {
    fn get(&self, key: &[u8]) -> Result<Option<(Vec<u8>, CacheMeta)>, StoreError> {
        let txn = self.env.read_txn().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        match self.db.get(&txn, key).map_err(|e| StoreError::CacheFailed(e.to_string()))? {
            Some(bytes) if bytes.len() >= 16 => {
                let (value, meta_bytes) = bytes.split_at(bytes.len() - 16);
                let watermark = u64::from_le_bytes(meta_bytes[..8].try_into().unwrap());
                let cached_at_us = i64::from_le_bytes(meta_bytes[8..16].try_into().unwrap());
                Ok(Some((value.to_vec(), CacheMeta { watermark, cached_at_us })))
            }
            _ => Ok(None),
        }
    }

    fn put(&self, key: &[u8], value: &[u8], meta: CacheMeta) -> Result<(), StoreError> {
        let mut buf = Vec::with_capacity(value.len() + 16);
        buf.extend_from_slice(value);
        buf.extend_from_slice(&meta.watermark.to_le_bytes());
        buf.extend_from_slice(&meta.cached_at_us.to_le_bytes());

        let mut txn = self.env.write_txn().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        self.db.put(&mut txn, key, &buf).map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        txn.commit().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        Ok(())
    }

    fn delete_prefix(&self, prefix: &[u8]) -> Result<u64, StoreError> {
        /// heed has built-in delete_prefix! One line.
        let mut txn = self.env.write_txn().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        let count = self.db.delete_prefix(&mut txn, prefix)
            .map_err(|e| StoreError::CacheFailed(e.to_string()))? as u64;
        txn.commit().map_err(|e| StoreError::CacheFailed(e.to_string()))?;
        Ok(count)
    }

    fn sync(&self) -> Result<(), StoreError> {
        self.env.force_sync().map_err(|e| StoreError::CacheFailed(e.to_string()))
    }
}
```

>[projection.rs]

---

## src/store/cursor.rs

IMPORTS:
```rust
use crate::coordinate::Region;
use crate::store::index::{StoreIndex, IndexEntry};
use std::sync::Arc;
```

TYPES:
```rust
/// Cursor: pull-based event consumption with guaranteed delivery.
/// Reads from index, not channels. Cannot lose events.
/// [SPEC:src/store/cursor.rs]

pub struct Cursor {
    region: Region,
    position: u64,      // tracks global_sequence — next poll starts after this
    index: Arc<StoreIndex>,
}
```

IMPL:
```rust
impl Cursor {
    pub(crate) fn new(region: Region, index: Arc<StoreIndex>) -> Self {
        Self { region, position: 0, index }
    }

    /// Poll for the next matching event after our current position.
    pub fn poll(&mut self) -> Option<IndexEntry> {
        /// Query the index for events matching our region with global_sequence > self.position.
        /// Return the first match, advance position.
        let results = self.index.query(&self.region);
        for entry in results {
            if entry.global_sequence > self.position {
                self.position = entry.global_sequence;
                return Some(entry);
            }
        }
        None
    }

    /// Poll for up to max matching events.
    pub fn poll_batch(&mut self, max: usize) -> Vec<IndexEntry> {
        let mut batch = Vec::with_capacity(max);
        let results = self.index.query(&self.region);
        for entry in results {
            if entry.global_sequence > self.position {
                self.position = entry.global_sequence;
                batch.push(entry);
                if batch.len() >= max { break; }
            }
        }
        batch
    }
}
```

>[cursor.rs]

---

## src/store/subscription.rs

IMPORTS:
```rust
use crate::coordinate::Region;
use crate::store::writer::Notification;
use flume::Receiver;
```

TYPES:
```rust
/// Subscription: push-based per-subscriber flume channel. Lossy.
/// If subscriber is slow, bounded channel fills. Writer's retain() prunes.
/// For guaranteed delivery, use Cursor instead.
/// [SPEC:src/store/subscription.rs]

pub struct Subscription {
    rx: Receiver<Notification>,
    region: Region,
}
```

IMPL:
```rust
impl Subscription {
    pub(crate) fn new(rx: Receiver<Notification>, region: Region) -> Self {
        Self { rx, region }
    }

    /// Blocking receive. Filters by region. Returns None if channel closed.
    pub fn recv(&self) -> Option<Notification> {
        loop {
            match self.rx.recv() {
                Ok(notif) => {
                    /// Filter: only return events matching our region.
                    /// [FILE:src/coordinate/mod.rs — Region::matches_event]
                    if self.region.matches_event(
                        notif.coord.entity(), notif.coord.scope(), notif.kind
                    ) {
                        return Some(notif);
                    }
                    /// Didn't match — keep receiving
                }
                Err(_) => return None, // channel closed
            }
        }
    }

    /// Expose the raw receiver for async usage.
    /// Caller uses: sub.receiver().recv_async().await
    /// [DEP:flume::Receiver::recv_async] → RecvFut<'_, T>: Future
    /// ASYNC NOTE: This is for async event consumption. For Store methods
    /// (append, get, query), use spawn_blocking instead. Two different patterns.
    /// [SPEC:src/store/subscription.rs — ASYNC NOTE]
    pub fn receiver(&self) -> &Receiver<Notification> {
        &self.rx
    }
}
```

>[subscription.rs]

---

## src/store/mod.rs

Current-state note (2026-03-30): the live repo no longer keeps every store
type in this file. `StoreConfig` lives in `src/store/config.rs`, `StoreError`
lives in `src/store/error.rs`, append/compaction contracts live in
`src/store/contracts.rs`, test-only runtime checks live in
`src/store/runtime_contracts.rs`, ancestor traversal is split into
`src/store/ancestors.rs` plus cfg-specific helper files, lifecycle helpers live
in `src/store/maintenance.rs`, projection orchestration lives in
`src/store/projection_flow.rs`, and test-only hooks live behind the
`test-support` feature in `src/store/test_support.rs`. Read the section below
as public API intent, not as the literal final file layout.

IMPORTS:
```rust
pub mod index;
pub mod segment;
pub mod writer;
pub mod reader;
pub mod projection;
pub mod cursor;
pub mod subscription;

pub use index::{IndexEntry, ClockKey, DiskPos};
pub use projection::{ProjectionCache, NoCache, CacheMeta, Freshness};
pub use cursor::Cursor;
pub use subscription::Subscription;
pub use writer::{Notification, RestartPolicy};

use crate::coordinate::{Coordinate, CoordinateError, Region, KindFilter};
use crate::event::{Event, EventHeader, EventKind, StoredEvent, EventSourced};
use crate::wire::u128_bytes;
use index::StoreIndex;
use reader::Reader;
use writer::{WriterHandle, WriterCommand, SubscriberList};
use projection::ProjectionCache;
use serde::Serialize;
use std::path::PathBuf;
use std::sync::Arc;
// NOTE: the `use crate::wire::u128_bytes` IS needed here — Store uses it
// for AppendOptions.idempotency_key serde annotation (if AppendOptions is serialized).
// If AppendOptions is never serialized, this can be removed.
```

TYPES:
```rust
/// Store: the runtime. Sync API. Send + Sync.
/// [SPEC:src/store/mod.rs]
/// Invariant 2: ALL METHODS ARE SYNC. No .await anywhere.
#[cfg(feature = "async-store")]
compile_error!("INVARIANT 2: Store API is sync. Use spawn_blocking or flume recv_async.");

pub struct Store {
    index: Arc<StoreIndex>,
    reader: Arc<Reader>,
    cache: Box<dyn ProjectionCache>,
    writer: WriterHandle,
    config: Arc<StoreConfig>,
}

/// StoreConfig: all settings for a Store instance.
/// No Default — callers must provide data_dir via `StoreConfig::new(path)`.
/// Manual Clone and Debug impls because `clock` field is `Arc<dyn Fn>`.
pub struct StoreConfig {
    pub data_dir: PathBuf,
    pub segment_max_bytes: u64,
    pub sync_every_n_events: u32,
    pub fd_budget: usize,
    pub writer_channel_capacity: usize,
    pub broadcast_capacity: usize,
    pub cache_map_size_bytes: usize,
    pub restart_policy: RestartPolicy,
    pub shutdown_drain_limit: usize,
    /// Optional writer thread stack size. None = OS default (~8MB on Linux).
    pub writer_stack_size: Option<usize>,
    /// Injectable clock for deterministic testing. Returns microseconds since epoch.
    /// None = std::time::SystemTime::now() (production default).
    pub clock: Option<Arc<dyn Fn() -> i64 + Send + Sync>>,
    /// Sync mode: SyncAll (data+metadata, default) or SyncData (data only, faster).
    pub sync_mode: SyncMode,
}

/// Sync strategy for segment fsync.
#[derive(Clone, Debug, Default)]
pub enum SyncMode {
    #[default]
    SyncAll,
    SyncData,
}

impl StoreConfig {
    /// Create a StoreConfig with required data_dir and sensible defaults.
    pub fn new(data_dir: impl Into<PathBuf>) -> Self {
        Self {
            data_dir: data_dir.into(),
            segment_max_bytes: 256 * 1024 * 1024,  // 256MB
            sync_every_n_events: 1000,
            fd_budget: 64,
            writer_channel_capacity: 4096,
            broadcast_capacity: 8192,
            cache_map_size_bytes: 64 * 1024 * 1024, // 64MB
            restart_policy: RestartPolicy::default(),
            shutdown_drain_limit: 1024,
            writer_stack_size: None,
            clock: None,
            sync_mode: SyncMode::default(),
        }
    }
}

/// StoreError: every error the store can produce.
/// [SPEC:src/store/mod.rs — StoreError variants]
#[derive(Debug)]
pub enum StoreError {
    Io(std::io::Error),
    Coordinate(CoordinateError),
    Serialization(String),
    CrcMismatch { segment_id: u64, offset: u64 },
    CorruptSegment { segment_id: u64, detail: String },
    NotFound(u128),
    SequenceMismatch { entity: String, expected: u32, actual: u32 },
    DuplicateEvent(u128),
    WriterCrashed,
    ShuttingDown,
    CacheFailed(String),
}

impl std::fmt::Display for StoreError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(e) => write!(f, "IO error: {e}"),
            Self::Coordinate(e) => write!(f, "coordinate error: {e}"),
            Self::Serialization(s) => write!(f, "serialization error: {s}"),
            Self::CrcMismatch { segment_id, offset } =>
                write!(f, "CRC mismatch in segment {segment_id} at offset {offset}"),
            Self::CorruptSegment { segment_id, detail } =>
                write!(f, "corrupt segment {segment_id}: {detail}"),
            Self::NotFound(id) => write!(f, "event {id:032x} not found"),
            Self::SequenceMismatch { entity, expected, actual } =>
                write!(f, "CAS failed for {entity}: expected seq {expected}, got {actual}"),
            Self::DuplicateEvent(key) => write!(f, "duplicate idempotency key {key:032x}"),
            Self::WriterCrashed => write!(f, "writer thread crashed"),
            Self::ShuttingDown => write!(f, "store is shutting down"),
            Self::CacheFailed(s) => write!(f, "cache error: {s}"),
        }
    }
}
impl std::error::Error for StoreError {}
impl From<CoordinateError> for StoreError {
    fn from(e: CoordinateError) -> Self { Self::Coordinate(e) }
}
impl From<std::io::Error> for StoreError {
    fn from(e: std::io::Error) -> Self { Self::Io(e) }
}

/// AppendReceipt: proof an event was persisted.
#[derive(Clone, Debug)]
pub struct AppendReceipt {
    pub event_id: u128,
    pub sequence: u64,
    pub disk_pos: DiskPos,
}

/// AppendOptions: CAS, idempotency, custom correlation/causation.
/// [SPEC:src/store/mod.rs — AppendOptions]
#[derive(Clone, Debug, Default)]
pub struct AppendOptions {
    pub expected_sequence: Option<u32>,
    pub idempotency_key: Option<u128>,
    pub correlation_id: Option<u128>,
    pub causation_id: Option<u128>,
}
```

IMPL:
```rust
impl Store {
    pub fn open(config: StoreConfig) -> Result<Self, StoreError> {
        std::fs::create_dir_all(&config.data_dir)?;
        let config = Arc::new(config);
        let index = Arc::new(StoreIndex::new());
        let reader = Arc::new(Reader::new(config.data_dir.clone(), config.fd_budget));

        /// Cold start: scan all segments, rebuild index.
        /// [SPEC:IMPLEMENTATION NOTES item 2 — segment naming, alphabetical scan]
        let mut entries: Vec<std::fs::DirEntry> = std::fs::read_dir(&config.data_dir)?
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().map(|ext| ext == "fbat").unwrap_or(false))
            .collect();
        entries.sort_by_key(|e| e.file_name());

        for dir_entry in &entries {
            let scanned = reader.scan_segment(&dir_entry.path())?;
            for se in scanned {
                let coord = Coordinate::new(&se.entity, &se.scope)?;
                let clock = se.event.header.position.sequence;
                let entry = IndexEntry {
                    event_id: se.event.header.event_id,
                    correlation_id: se.event.header.correlation_id,
                    causation_id: se.event.header.causation_id,
                    coord,
                    kind: se.event.header.event_kind,
                    wall_ms: se.event.header.position.wall_ms,
                    clock,
                    hash_chain: se.event.hash_chain.clone().unwrap_or_default(),
                    disk_pos: DiskPos {
                        segment_id: se.segment_id,
                        offset: se.offset,
                        length: se.length,
                    },
                    global_sequence: index.global_sequence(),
                };
                index.insert(entry);
            }
        }

        let subscribers = Arc::new(SubscriberList::new());
        let writer = WriterHandle::spawn(
            Arc::clone(&config), Arc::clone(&index), Arc::clone(&subscribers),
        )?;

        Ok(Self {
            index, reader, cache: Box::new(NoCache), writer, config,
        })
    }

    pub fn open_default() -> Result<Self, StoreError> {
        Self::open(StoreConfig::new("./batpak-data"))
    }

    /// WRITE: append a new root-cause event.
    /// correlation_id defaults to event_id (self-correlated). causation_id = None.
    pub fn append(
        &self, coord: &Coordinate, kind: EventKind, payload: &impl Serialize,
    ) -> Result<AppendReceipt, StoreError> {
        let payload_bytes = rmp_serde::to_vec_named(payload)
            .map_err(|e| StoreError::Serialization(e.to_string()))?;
        let event_id = crate::id::generate_v7_id();
        let header = EventHeader::new(
            event_id, event_id, None, // correlation = self, causation = root
            now_us(), crate::coordinate::DagPosition::root(),
            payload_bytes.len() as u32, kind,
        );
        let event = Event::new(header, payload_bytes);

        let (tx, rx) = flume::bounded(1);
        self.writer.tx.send(WriterCommand::Append {
            entity: coord.entity_arc(),
            scope: coord.scope_arc(),
            event, kind,
            correlation_id: event_id,
            causation_id: None,
            respond: tx,
        }).map_err(|_| StoreError::WriterCrashed)?;

        rx.recv().map_err(|_| StoreError::WriterCrashed)?
    }

    /// WRITE: append a reaction (caused by another event).
    pub fn append_reaction(
        &self, coord: &Coordinate, kind: EventKind, payload: &impl Serialize,
        correlation_id: u128, causation_id: u128,
    ) -> Result<AppendReceipt, StoreError> {
        let payload_bytes = rmp_serde::to_vec_named(payload)
            .map_err(|e| StoreError::Serialization(e.to_string()))?;
        let event_id = crate::id::generate_v7_id();
        let header = EventHeader::new(
            event_id, correlation_id, Some(causation_id),
            now_us(), crate::coordinate::DagPosition::root(),
            payload_bytes.len() as u32, kind,
        );
        let event = Event::new(header, payload_bytes);

        let (tx, rx) = flume::bounded(1);
        self.writer.tx.send(WriterCommand::Append {
            entity: coord.entity_arc(), scope: coord.scope_arc(),
            event, kind, correlation_id, causation_id: Some(causation_id),
            respond: tx,
        }).map_err(|_| StoreError::WriterCrashed)?;

        rx.recv().map_err(|_| StoreError::WriterCrashed)?
    }

    /// READ: get a single event by ID.
    pub fn get(&self, event_id: u128) -> Result<StoredEvent<serde_json::Value>, StoreError> {
        let entry = self.index.get_by_id(event_id)
            .ok_or(StoreError::NotFound(event_id))?;
        self.reader.read_entry(&entry.disk_pos)
    }

    /// READ: query by Region.
    pub fn query(&self, region: &Region) -> Vec<IndexEntry> {
        self.index.query(region)
    }

    /// READ: walk hash chain ancestors. [SPEC:IMPLEMENTATION NOTES item 3]
    pub fn walk_ancestors(&self, event_id: u128, limit: usize)
        -> Vec<StoredEvent<serde_json::Value>>
    {
        let mut results = Vec::new();
        let mut current_id = Some(event_id);
        while let Some(id) = current_id {
            if results.len() >= limit { break; }
            if let Some(entry) = self.index.get_by_id(id) {
                if let Ok(stored) = self.reader.read_entry(&entry.disk_pos) {
                    results.push(stored);
                }
                /// Follow prev_hash: find the entry whose event_hash matches prev_hash
                let prev = entry.hash_chain.prev_hash;
                if prev == [0u8; 32] { break; } // genesis
                /// Linear scan is acceptable for ancestor walks (bounded by limit).
                current_id = self.index.stream(entry.coord.entity())
                    .iter()
                    .find(|e| e.hash_chain.event_hash == prev)
                    .map(|e| e.event_id);
            } else {
                break;
            }
        }
        results
    }

    /// PROJECT: reconstruct typed state from events.
    pub fn project<T: EventSourced<serde_json::Value>>(
        &self, entity: &str, _freshness: Freshness,
    ) -> Result<Option<T>, StoreError> {
        let entries = self.index.stream(entity);
        if entries.is_empty() { return Ok(None); }

        let mut events = Vec::with_capacity(entries.len());
        for entry in &entries {
            let stored = self.reader.read_entry(&entry.disk_pos)?;
            events.push(stored.event);
        }
        Ok(T::from_events(&events))
    }

    /// SUBSCRIBE: push-based, lossy.
    pub fn subscribe(&self, region: &Region) -> Subscription {
        let rx = self.writer.subscribers.subscribe(self.config.broadcast_capacity);
        Subscription::new(rx, region.clone())
    }

    /// CURSOR: pull-based, guaranteed delivery.
    pub fn cursor(&self, region: &Region) -> Cursor {
        Cursor::new(region.clone(), Arc::clone(&self.index))
    }

    /// CONVENIENCE: sugar over Region.
    pub fn stream(&self, entity: &str) -> Vec<IndexEntry> {
        self.query(&Region::entity(entity))
    }
    pub fn by_scope(&self, scope: &str) -> Vec<IndexEntry> {
        self.query(&Region::scope(scope))
    }
    pub fn by_fact(&self, kind: EventKind) -> Vec<IndexEntry> {
        self.query(&Region::all().with_fact(KindFilter::Exact(kind)))
    }

    /// LIFECYCLE
    pub fn sync(&self) -> Result<(), StoreError> {
        let (tx, rx) = flume::bounded(1);
        self.writer.tx.send(WriterCommand::Sync { respond: tx })
            .map_err(|_| StoreError::WriterCrashed)?;
        rx.recv().map_err(|_| StoreError::WriterCrashed)?
    }

    pub fn close(self) -> Result<(), StoreError> {
        let (tx, rx) = flume::bounded(1);
        self.writer.tx.send(WriterCommand::Shutdown { respond: tx })
            .map_err(|_| StoreError::WriterCrashed)?;
        rx.recv().map_err(|_| StoreError::WriterCrashed)?
    }

    /// DIAGNOSTICS
    pub fn stats(&self) -> StoreStats {
        StoreStats {
            event_count: self.index.len(),
            global_sequence: self.index.global_sequence(),
        }
    }
}

fn now_us() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_micros() as i64
}

#[derive(Clone, Debug)]
pub struct StoreStats {
    pub event_count: usize,
    pub global_sequence: u64,
}
```

>[mod.rs]

---

```
STORE MODULE REGISTRATION COMPLETE — 7 files registered.

Tests and benches pending registration:
  tests/monad_laws.rs, hash_chain.rs, store_integration.rs, gate_pipeline.rs,
  typestate_safety.rs, wire_format.rs, perf_gates.rs
  benches/write_throughput.rs, cold_start.rs, projection_latency.rs

Test/bench registration will follow the same pattern:
  IMPORTS → TYPES → IMPL → cross-references → end marker.
```