fsqlite-mvcc 0.3.4

MVCC page-level versioning for concurrent writers
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
//! Shared-memory header layout for cross-process MVCC coordination (§5.6.1).
//!
//! The `foo.db.fsqlite-shm` region is a 216-byte header that carries:
//!
//! - Immutable fields: magic, version, page_size, max_txn_slots, region offsets.
//! - Atomic counters: next_txn_id, next_commit_seq, snapshot_seq (seqlock), commit_seq,
//!   schema_epoch, ecs_epoch, gc_horizon.
//! - Serialized writer indicator: writer_txn_id, pid, pid_birth, lease_expiry.
//! - Snapshot publisher identity: owner token and PID birth marker.
//! - An xxh3_64 checksum over the immutable fields.
//!
//! The in-process fast path uses native Rust atomics. Serialization to/from
//! the on-disk byte-level wire format uses explicit `to_le_bytes`/`from_le_bytes`
//! at computed offsets. No `unsafe`, no `repr(C)` reinterpret casts.

use std::sync::{
    OnceLock,
    atomic::{AtomicU64, Ordering},
};

use fsqlite_types::{CommitSeq, PageSize, SchemaEpoch, TxnId, sync_primitives::SystemTime};
use fsqlite_vfs::ShmRegion;
use xxhash_rust::xxh3::xxh3_64;

use crate::lifecycle::MvccError;

// ---------------------------------------------------------------------------
// Wire-format offsets
// ---------------------------------------------------------------------------

/// Byte offsets and sizes for the SHM header wire format.
mod offsets {
    /// `[u8;8]` — `"FSQLSHM\0"`.
    pub const MAGIC: usize = 0;
    pub const MAGIC_LEN: usize = 8;

    /// `u32` — layout version.
    pub const VERSION: usize = 8;

    /// `u32` — database page size.
    pub const PAGE_SIZE: usize = 12;

    /// `u32` — maximum transaction slots.
    pub const MAX_TXN_SLOTS: usize = 16;

    /// `u32` — alignment padding (always 0).
    pub const ALIGN0: usize = 20;

    /// `u64` — next transaction id (atomic counter).
    pub const NEXT_TXN_ID: usize = 24;

    /// `u64` — next commit sequence to reserve.
    pub const NEXT_COMMIT_SEQ: usize = 152;

    /// `u64` — snapshot sequence (seqlock counter).
    pub const SNAPSHOT_SEQ: usize = 32;

    /// `u64` — commit sequence.
    pub const COMMIT_SEQ: usize = 40;

    /// `u64` — schema epoch.
    pub const SCHEMA_EPOCH: usize = 48;

    /// `u64` — ECS epoch.
    pub const ECS_EPOCH: usize = 56;

    /// `u64` — GC horizon.
    pub const GC_HORIZON: usize = 64;

    /// `u64` — serialized writer txn id (0 = none).
    pub const SERIALIZED_WRITER_TXN_ID: usize = 72;

    /// `u64` — serialized writer PID (lower 32) and generation (upper 32).
    pub const SERIALIZED_WRITER_PID_AND_GEN: usize = 80;

    /// `u64` — serialized writer PID birth timestamp.
    pub const SERIALIZED_WRITER_PID_BIRTH: usize = 88;

    /// `u64` — serialized writer lease expiry.
    pub const SERIALIZED_WRITER_LEASE_EXPIRY: usize = 96;

    /// `u64` — lock table region offset.
    pub const LOCK_TABLE_OFFSET: usize = 104;

    /// `u64` — witness region offset.
    pub const WITNESS_OFFSET: usize = 112;

    /// `u64` — transaction slot region offset.
    pub const TXN_SLOT_OFFSET: usize = 120;

    /// `u64` — committed readers region offset.
    pub const COMMITTED_READERS_OFFSET: usize = 128;

    /// `u64` — committed readers region size in bytes.
    pub const COMMITTED_READERS_BYTES: usize = 136;

    /// `u64` — xxh3_64 checksum over immutable fields.
    pub const LAYOUT_CHECKSUM: usize = 144;

    /// `u64` — snapshot publisher owner token.
    ///
    /// Bit 63 marks owner initialization; bits 32..62 hold a process-local
    /// generation and bits 0..31 hold the publisher PID.
    pub const SNAPSHOT_PUBLISHER_OWNER: usize = 160;

    /// `u64` — snapshot publisher PID birth marker.
    pub const SNAPSHOT_PUBLISHER_PID_BIRTH: usize = 168;

    /// `[u8;40]` — reserved padding to 216 bytes.
    pub const _PADDING: usize = 176;
    pub const _PADDING_LEN: usize = 40;

    /// Total header size in bytes.
    pub const HEADER_SIZE: usize = 216;
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Magic bytes identifying a valid FrankenSQLite SHM header.
const MAGIC: [u8; 8] = *b"FSQLSHM\0";

/// Current layout version.
const LAYOUT_VERSION: u32 = 1;

/// Default max transaction slots when not specified.
const DEFAULT_MAX_TXN_SLOTS: u32 = 128;

/// High bit identifying a `/proc/<pid>/stat` start-time birth marker.
#[cfg(unix)]
const PID_BIRTH_PROCFS_TAG: u64 = 1_u64 << 63;

/// High bit identifying an owner token whose birth marker is not published yet.
const SNAPSHOT_PUBLISHER_INITIALIZING: u64 = 1_u64 << 63;

/// Process-local generation used to make consecutive ownership tokens distinct.
///
/// Seeded from per-process entropy rather than a fixed constant: with a fixed
/// seed, a new process that reuses a dead publisher's PID would mint the exact
/// token the dead owner left stamped, and a concurrent waiter that already
/// proved the old owner dead could CAS the *live* new owner out (token ABA).
/// Entropy makes cross-process token collision negligible, so the claim CAS
/// remains a sound guard.
static NEXT_SNAPSHOT_PUBLISHER_GENERATION: OnceLock<AtomicU64> = OnceLock::new();

fn next_snapshot_publisher_generation_counter() -> &'static AtomicU64 {
    NEXT_SNAPSHOT_PUBLISHER_GENERATION.get_or_init(|| {
        use std::hash::{BuildHasher as _, Hasher as _};
        // RandomState carries per-process random SipHash keys; hashing the
        // pid and birth marker through it yields a process-unique seed
        // without pulling in an RNG dependency.
        let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
        hasher.write_u32(std::process::id());
        hasher.write_u64(current_process_birth_marker());
        AtomicU64::new(hasher.finish())
    })
}

#[cfg(unix)]
fn read_proc_start_time_ticks(pid: u32) -> Option<u64> {
    let stat_path = std::path::Path::new("/proc")
        .join(pid.to_string())
        .join("stat");
    let stat = std::fs::read_to_string(stat_path).ok()?;
    let comm_end = stat.rfind(')')?;
    let tail = stat.get(comm_end + 1..)?.trim_start();
    tail.split_whitespace().nth(19)?.parse::<u64>().ok()
}

fn current_process_birth_marker() -> u64 {
    static FALLBACK_BIRTH: OnceLock<u64> = OnceLock::new();

    #[cfg(unix)]
    if std::path::Path::new("/proc").exists()
        && let Some(start_ticks) = read_proc_start_time_ticks(std::process::id())
    {
        return PID_BIRTH_PROCFS_TAG | (start_ticks & !PID_BIRTH_PROCFS_TAG);
    }

    *FALLBACK_BIRTH.get_or_init(|| {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .map_or(1, |duration| duration.as_nanos() as u64);
        now.max(1)
    })
}

fn snapshot_publisher_alive_os(pid: u32, pid_birth: u64) -> bool {
    #[cfg(unix)]
    {
        if pid == 0 {
            return false;
        }
        if !std::path::Path::new("/proc").exists() {
            return true;
        }
        let proc_dir = std::path::Path::new("/proc").join(pid.to_string());
        if !proc_dir.exists() {
            return false;
        }
        if pid_birth == 0 || pid_birth & PID_BIRTH_PROCFS_TAG == 0 {
            return true;
        }
        let expected_ticks = pid_birth & !PID_BIRTH_PROCFS_TAG;
        read_proc_start_time_ticks(pid).is_none_or(|start_ticks| start_ticks == expected_ticks)
    }
    #[cfg(not(unix))]
    {
        let _ = (pid, pid_birth);
        true
    }
}

// ---------------------------------------------------------------------------
// ShmSnapshot
// ---------------------------------------------------------------------------

/// A consistent snapshot read from the SHM header via the seqlock protocol.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ShmSnapshot {
    /// The latest committed sequence number.
    pub commit_seq: CommitSeq,
    /// The current schema epoch.
    pub schema_epoch: SchemaEpoch,
    /// The current ECS epoch.
    pub ecs_epoch: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SnapshotPublisherIdentity {
    active_token: u64,
    initializing_token: u64,
    pid: u32,
    pid_birth: u64,
}

impl SnapshotPublisherIdentity {
    fn current() -> Self {
        let pid = std::process::id();
        let generation = loop {
            let raw = next_snapshot_publisher_generation_counter().fetch_add(1, Ordering::Relaxed);
            let generation = (raw as u32) & 0x7fff_ffff;
            if generation != 0 {
                break generation;
            }
        };
        let active_token = (u64::from(generation) << 32) | u64::from(pid);
        Self {
            active_token,
            initializing_token: active_token | SNAPSHOT_PUBLISHER_INITIALIZING,
            pid,
            pid_birth: current_process_birth_marker(),
        }
    }

    #[cfg(test)]
    const fn for_test(pid: u32, generation: u32, pid_birth: u64) -> Self {
        let active_token = ((generation as u64 & 0x7fff_ffff) << 32) | pid as u64;
        Self {
            active_token,
            initializing_token: active_token | SNAPSHOT_PUBLISHER_INITIALIZING,
            pid,
            pid_birth,
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct SnapshotPublishPermit {
    identity: SnapshotPublisherIdentity,
    odd_sequence: u64,
}

// ---------------------------------------------------------------------------
// SharedMemoryLayout
// ---------------------------------------------------------------------------

/// The 216-byte SHM header for cross-process MVCC coordination.
///
/// Immutable fields are set at creation and never change. Dynamic fields
/// use `AtomicU64`/`AtomicU32` for lock-free cross-thread access.
///
/// The seqlock protocol (§5.6.1) on `snapshot_seq` protects the
/// `(commit_seq, schema_epoch, ecs_epoch)` triple from torn reads.
pub struct SharedMemoryLayout {
    // -- Immutable fields --
    page_size: PageSize,
    max_txn_slots: u32,
    lock_table_offset: u64,
    witness_offset: u64,
    txn_slot_offset: u64,
    committed_readers_offset: u64,
    committed_readers_bytes: u64,
    layout_checksum: u64,

    // -- Dynamic fields (atomics) --
    next_txn_id: AtomicU64,
    next_commit_seq: AtomicU64,
    snapshot_seq: AtomicU64,
    commit_seq: AtomicU64,
    schema_epoch: AtomicU64,
    ecs_epoch: AtomicU64,
    gc_horizon: AtomicU64,
    serialized_writer_txn_id: AtomicU64,
    serialized_writer_pid_and_gen: AtomicU64,
    serialized_writer_pid_birth: AtomicU64,
    serialized_writer_lease_expiry: AtomicU64,
    snapshot_publisher_owner: AtomicU64,
    snapshot_publisher_pid_birth: AtomicU64,
    mapped_region: Option<ShmRegion>,
}

impl SharedMemoryLayout {
    /// Total header size in bytes.
    pub const HEADER_SIZE: usize = offsets::HEADER_SIZE;

    /// Create a new SHM header layout.
    ///
    /// Computes region offsets (lock table, witness, txn slots, committed
    /// readers) starting right after the header, and the xxh3_64 checksum
    /// over immutable fields.
    #[must_use]
    pub fn new(page_size: PageSize, max_txn_slots: u32) -> Self {
        let max_txn_slots = if max_txn_slots == 0 {
            DEFAULT_MAX_TXN_SLOTS
        } else {
            max_txn_slots
        };

        // Region offsets: each region starts right after the previous.
        // These are logical offsets within the SHM file, starting after the header.
        let lock_table_offset = Self::HEADER_SIZE as u64;

        // Lock table: one u64 per page slot (simplified sizing).
        let lock_table_size = u64::from(max_txn_slots) * 64;
        let witness_offset = lock_table_offset + lock_table_size;

        // Witness region: sized proportionally to txn slots.
        let witness_size = u64::from(max_txn_slots) * 128;
        let txn_slot_offset = witness_offset + witness_size;

        // Txn slot region: 128 bytes per slot (SharedTxnSlot).
        let txn_slot_size = u64::from(max_txn_slots) * 128;
        let committed_readers_offset = txn_slot_offset + txn_slot_size;

        // Committed readers bitmap.
        let committed_readers_bytes = u64::from(max_txn_slots.div_ceil(8));

        let checksum = Self::compute_checksum_from_parts(
            page_size,
            max_txn_slots,
            lock_table_offset,
            witness_offset,
            txn_slot_offset,
            committed_readers_offset,
            committed_readers_bytes,
        );

        Self {
            page_size,
            max_txn_slots,
            lock_table_offset,
            witness_offset,
            txn_slot_offset,
            committed_readers_offset,
            committed_readers_bytes,
            layout_checksum: checksum,
            next_txn_id: AtomicU64::new(1),
            next_commit_seq: AtomicU64::new(1),
            snapshot_seq: AtomicU64::new(0),
            commit_seq: AtomicU64::new(0),
            schema_epoch: AtomicU64::new(0),
            ecs_epoch: AtomicU64::new(0),
            gc_horizon: AtomicU64::new(0),
            serialized_writer_txn_id: AtomicU64::new(0),
            serialized_writer_pid_and_gen: AtomicU64::new(0),
            serialized_writer_pid_birth: AtomicU64::new(0),
            serialized_writer_lease_expiry: AtomicU64::new(0),
            snapshot_publisher_owner: AtomicU64::new(0),
            snapshot_publisher_pid_birth: AtomicU64::new(0),
            mapped_region: None,
        }
    }

    /// Deserialize a `SharedMemoryLayout` from a byte buffer.
    ///
    /// Validates magic, version, page size, and checksum.
    ///
    /// # Errors
    ///
    /// Returns `MvccError::ShmTooSmall` if `buf.len() < HEADER_SIZE`.
    /// Returns `MvccError::ShmBadMagic` if magic bytes don't match.
    /// Returns `MvccError::ShmVersionMismatch` if version != 1.
    /// Returns `MvccError::ShmInvalidPageSize` if page_size is not valid.
    /// Returns `MvccError::ShmChecksumMismatch` if checksum fails.
    pub fn open(buf: &[u8]) -> Result<Self, MvccError> {
        if buf.len() < Self::HEADER_SIZE {
            return Err(MvccError::ShmTooSmall);
        }

        // Validate magic.
        if buf[offsets::MAGIC..offsets::MAGIC + offsets::MAGIC_LEN] != MAGIC {
            return Err(MvccError::ShmBadMagic);
        }

        // Validate version.
        let version = read_u32(buf, offsets::VERSION);
        if version != LAYOUT_VERSION {
            return Err(MvccError::ShmVersionMismatch);
        }

        // Read and validate page size.
        let page_size_raw = read_u32(buf, offsets::PAGE_SIZE);
        let page_size = PageSize::new(page_size_raw).ok_or(MvccError::ShmInvalidPageSize)?;

        let max_txn_slots = read_u32(buf, offsets::MAX_TXN_SLOTS);

        // Read region offsets.
        let lock_table_offset = read_u64(buf, offsets::LOCK_TABLE_OFFSET);
        let witness_offset = read_u64(buf, offsets::WITNESS_OFFSET);
        let txn_slot_offset = read_u64(buf, offsets::TXN_SLOT_OFFSET);
        let committed_readers_offset = read_u64(buf, offsets::COMMITTED_READERS_OFFSET);
        let committed_readers_bytes = read_u64(buf, offsets::COMMITTED_READERS_BYTES);

        // Validate checksum.
        let stored_checksum = read_u64(buf, offsets::LAYOUT_CHECKSUM);
        let computed = Self::compute_checksum_from_parts(
            page_size,
            max_txn_slots,
            lock_table_offset,
            witness_offset,
            txn_slot_offset,
            committed_readers_offset,
            committed_readers_bytes,
        );
        if stored_checksum != computed {
            return Err(MvccError::ShmChecksumMismatch);
        }

        // Read dynamic fields.
        let next_txn_id = read_u64(buf, offsets::NEXT_TXN_ID);
        let next_commit_seq = read_u64(buf, offsets::NEXT_COMMIT_SEQ);
        let snapshot_seq = read_u64(buf, offsets::SNAPSHOT_SEQ);
        let commit_seq = read_u64(buf, offsets::COMMIT_SEQ);
        let schema_epoch = read_u64(buf, offsets::SCHEMA_EPOCH);
        let ecs_epoch = read_u64(buf, offsets::ECS_EPOCH);
        let gc_horizon = read_u64(buf, offsets::GC_HORIZON);
        let sw_txn_id = read_u64(buf, offsets::SERIALIZED_WRITER_TXN_ID);
        let sw_pid_and_gen = read_u64(buf, offsets::SERIALIZED_WRITER_PID_AND_GEN);
        let sw_pid_birth = read_u64(buf, offsets::SERIALIZED_WRITER_PID_BIRTH);
        let sw_lease = read_u64(buf, offsets::SERIALIZED_WRITER_LEASE_EXPIRY);
        let snapshot_publisher_owner = read_u64(buf, offsets::SNAPSHOT_PUBLISHER_OWNER);
        let snapshot_publisher_pid_birth = read_u64(buf, offsets::SNAPSHOT_PUBLISHER_PID_BIRTH);

        Ok(Self {
            page_size,
            max_txn_slots,
            lock_table_offset,
            witness_offset,
            txn_slot_offset,
            committed_readers_offset,
            committed_readers_bytes,
            layout_checksum: stored_checksum,
            next_txn_id: AtomicU64::new(next_txn_id),
            next_commit_seq: AtomicU64::new(next_commit_seq.max(commit_seq.saturating_add(1))),
            snapshot_seq: AtomicU64::new(snapshot_seq),
            commit_seq: AtomicU64::new(commit_seq),
            schema_epoch: AtomicU64::new(schema_epoch),
            ecs_epoch: AtomicU64::new(ecs_epoch),
            gc_horizon: AtomicU64::new(gc_horizon),
            serialized_writer_txn_id: AtomicU64::new(sw_txn_id),
            serialized_writer_pid_and_gen: AtomicU64::new(sw_pid_and_gen),
            serialized_writer_pid_birth: AtomicU64::new(sw_pid_birth),
            serialized_writer_lease_expiry: AtomicU64::new(sw_lease),
            snapshot_publisher_owner: AtomicU64::new(snapshot_publisher_owner),
            snapshot_publisher_pid_birth: AtomicU64::new(snapshot_publisher_pid_birth),
            mapped_region: None,
        })
    }

    /// Open a live shared-memory layout over an existing SHM region.
    ///
    /// The immutable header is validated once, after which all dynamic fields
    /// are read and written directly through the shared region.
    ///
    /// # Errors
    ///
    /// Propagates the same validation failures as [`open`](Self::open).
    pub fn open_region(region: ShmRegion) -> Result<Self, MvccError> {
        let mut layout = {
            let guard = region.lock();
            Self::open(&guard[..])?
        };
        layout.mapped_region = Some(region);
        Ok(layout)
    }

    /// Open an existing SHM region or initialize a zeroed one in place.
    ///
    /// This keeps the header bytes resident in the mapped region so later
    /// loads/stores remain visible across handles and processes.
    ///
    /// # Errors
    ///
    /// Returns the same validation errors as [`open_region`](Self::open_region).
    pub fn open_or_initialize_region(
        region: ShmRegion,
        page_size: PageSize,
        max_txn_slots: u32,
    ) -> Result<Self, MvccError> {
        if region.len() < Self::HEADER_SIZE {
            return Err(MvccError::ShmTooSmall);
        }

        {
            let mut guard = region.lock();
            let magic = &guard[offsets::MAGIC..offsets::MAGIC + offsets::MAGIC_LEN];
            if magic.iter().all(|byte| *byte == 0) {
                let seed = Self::new(page_size, max_txn_slots).to_bytes();
                guard[..Self::HEADER_SIZE].copy_from_slice(&seed);
            }
        }

        Self::open_region(region)
    }

    /// Serialize the entire header to a 216-byte `Vec<u8>`.
    #[must_use]
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut buf = vec![0u8; Self::HEADER_SIZE];

        // Magic + version.
        buf[offsets::MAGIC..offsets::MAGIC + offsets::MAGIC_LEN].copy_from_slice(&MAGIC);
        write_u32(&mut buf, offsets::VERSION, LAYOUT_VERSION);
        write_u32(&mut buf, offsets::PAGE_SIZE, self.page_size.get());
        write_u32(&mut buf, offsets::MAX_TXN_SLOTS, self.max_txn_slots);
        write_u32(&mut buf, offsets::ALIGN0, 0);

        // Dynamic fields (snapshot from atomics).
        write_u64(
            &mut buf,
            offsets::NEXT_TXN_ID,
            self.load_u64_field(offsets::NEXT_TXN_ID, &self.next_txn_id, Ordering::Acquire),
        );
        write_u64(
            &mut buf,
            offsets::NEXT_COMMIT_SEQ,
            self.load_u64_field(
                offsets::NEXT_COMMIT_SEQ,
                &self.next_commit_seq,
                Ordering::Acquire,
            ),
        );
        write_u64(
            &mut buf,
            offsets::SNAPSHOT_SEQ,
            self.load_u64_field(offsets::SNAPSHOT_SEQ, &self.snapshot_seq, Ordering::Acquire),
        );
        write_u64(
            &mut buf,
            offsets::COMMIT_SEQ,
            self.load_u64_field(offsets::COMMIT_SEQ, &self.commit_seq, Ordering::Acquire),
        );
        write_u64(
            &mut buf,
            offsets::SCHEMA_EPOCH,
            self.load_u64_field(offsets::SCHEMA_EPOCH, &self.schema_epoch, Ordering::Acquire),
        );
        write_u64(
            &mut buf,
            offsets::ECS_EPOCH,
            self.load_u64_field(offsets::ECS_EPOCH, &self.ecs_epoch, Ordering::Acquire),
        );
        write_u64(
            &mut buf,
            offsets::GC_HORIZON,
            self.load_u64_field(offsets::GC_HORIZON, &self.gc_horizon, Ordering::Acquire),
        );

        // Serialized writer.
        write_u64(
            &mut buf,
            offsets::SERIALIZED_WRITER_TXN_ID,
            self.load_u64_field(
                offsets::SERIALIZED_WRITER_TXN_ID,
                &self.serialized_writer_txn_id,
                Ordering::Acquire,
            ),
        );
        write_u64(
            &mut buf,
            offsets::SERIALIZED_WRITER_PID_AND_GEN,
            self.load_u64_field(
                offsets::SERIALIZED_WRITER_PID_AND_GEN,
                &self.serialized_writer_pid_and_gen,
                Ordering::Acquire,
            ),
        );
        write_u64(
            &mut buf,
            offsets::SERIALIZED_WRITER_PID_BIRTH,
            self.load_u64_field(
                offsets::SERIALIZED_WRITER_PID_BIRTH,
                &self.serialized_writer_pid_birth,
                Ordering::Acquire,
            ),
        );
        write_u64(
            &mut buf,
            offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
            self.load_u64_field(
                offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
                &self.serialized_writer_lease_expiry,
                Ordering::Acquire,
            ),
        );

        // Snapshot publisher owner stamp (dynamic crash-recovery metadata).
        write_u64(
            &mut buf,
            offsets::SNAPSHOT_PUBLISHER_OWNER,
            self.load_u64_field(
                offsets::SNAPSHOT_PUBLISHER_OWNER,
                &self.snapshot_publisher_owner,
                Ordering::Acquire,
            ),
        );
        write_u64(
            &mut buf,
            offsets::SNAPSHOT_PUBLISHER_PID_BIRTH,
            self.load_u64_field(
                offsets::SNAPSHOT_PUBLISHER_PID_BIRTH,
                &self.snapshot_publisher_pid_birth,
                Ordering::Acquire,
            ),
        );

        // Region offsets (immutable).
        write_u64(&mut buf, offsets::LOCK_TABLE_OFFSET, self.lock_table_offset);
        write_u64(&mut buf, offsets::WITNESS_OFFSET, self.witness_offset);
        write_u64(&mut buf, offsets::TXN_SLOT_OFFSET, self.txn_slot_offset);
        write_u64(
            &mut buf,
            offsets::COMMITTED_READERS_OFFSET,
            self.committed_readers_offset,
        );
        write_u64(
            &mut buf,
            offsets::COMMITTED_READERS_BYTES,
            self.committed_readers_bytes,
        );

        // Checksum (immutable).
        write_u64(&mut buf, offsets::LAYOUT_CHECKSUM, self.layout_checksum);

        // Padding is already zeroed.
        buf
    }

    fn load_u64_field(&self, offset: usize, fallback: &AtomicU64, ordering: Ordering) -> u64 {
        self.mapped_region.as_ref().map_or_else(
            || fallback.load(ordering),
            |region| {
                region
                    .atomic_load_u64_le(offset, ordering)
                    .expect("mapped MVCC SHM layout offset is valid")
            },
        )
    }

    fn store_u64_field(&self, offset: usize, fallback: &AtomicU64, value: u64, ordering: Ordering) {
        if let Some(region) = &self.mapped_region {
            region
                .atomic_store_u64_le(offset, value, ordering)
                .expect("mapped MVCC SHM layout offset is valid");
        } else {
            fallback.store(value, ordering);
        }
    }

    fn compare_exchange_u64_field(
        &self,
        offset: usize,
        fallback: &AtomicU64,
        current: u64,
        new: u64,
        success: Ordering,
        failure: Ordering,
    ) -> std::result::Result<u64, u64> {
        self.mapped_region.as_ref().map_or_else(
            || fallback.compare_exchange(current, new, success, failure),
            |region| {
                region
                    .atomic_compare_exchange_u64_le(offset, current, new, success, failure)
                    .expect("mapped MVCC SHM layout offset is valid")
            },
        )
    }

    // -----------------------------------------------------------------------
    // Seqlock protocol
    // -----------------------------------------------------------------------

    /// Busy-spin iterations before a publisher or reader starts yielding.
    const PUBLISH_WAIT_SPIN_PHASE: u32 = 1 << 10;

    fn wait_for_snapshot_publisher(spins: &mut u32) {
        *spins = spins.saturating_add(1);
        if *spins > Self::PUBLISH_WAIT_SPIN_PHASE {
            std::thread::yield_now();
        } else {
            std::hint::spin_loop();
        }
    }

    fn snapshot_publisher_is_alive(
        &self,
        owner: u64,
        owner_alive: &impl Fn(u32, u64) -> bool,
    ) -> bool {
        let pid = owner as u32;
        if owner & SNAPSHOT_PUBLISHER_INITIALIZING != 0 {
            // During the short initialization window the birth marker is not
            // published yet. PID existence is sufficient to avoid stealing
            // from a live initializer; a dead initializer can still be
            // recovered because the callback returns false for a missing PID.
            return owner_alive(pid, 0);
        }
        let pid_birth = self.load_u64_field(
            offsets::SNAPSHOT_PUBLISHER_PID_BIRTH,
            &self.snapshot_publisher_pid_birth,
            Ordering::Acquire,
        );
        owner_alive(pid, pid_birth)
    }

    /// Atomically replace `current_owner` with a fully initialized owner stamp.
    ///
    /// The initializing bit closes the otherwise unsafe window between the
    /// owner CAS and publication of the PID birth marker. Waiters never use a
    /// previous owner's birth marker to decide that a newly initialized owner
    /// is dead.
    fn try_claim_snapshot_publisher(
        &self,
        current_owner: u64,
        identity: SnapshotPublisherIdentity,
    ) -> bool {
        if self
            .compare_exchange_u64_field(
                offsets::SNAPSHOT_PUBLISHER_OWNER,
                &self.snapshot_publisher_owner,
                current_owner,
                identity.initializing_token,
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .is_err()
        {
            return false;
        }

        self.store_u64_field(
            offsets::SNAPSHOT_PUBLISHER_PID_BIRTH,
            &self.snapshot_publisher_pid_birth,
            identity.pid_birth,
            Ordering::Release,
        );

        self.compare_exchange_u64_field(
            offsets::SNAPSHOT_PUBLISHER_OWNER,
            &self.snapshot_publisher_owner,
            identity.initializing_token,
            identity.active_token,
            Ordering::AcqRel,
            Ordering::Acquire,
        )
        .is_ok()
    }

    fn release_snapshot_publisher_without_publish(&self, identity: SnapshotPublisherIdentity) {
        if self.load_u64_field(
            offsets::SNAPSHOT_PUBLISHER_OWNER,
            &self.snapshot_publisher_owner,
            Ordering::Acquire,
        ) != identity.active_token
        {
            return;
        }

        // Leave the birth marker in place while releasing ownership. It is
        // ignored whenever the owner token is zero and overwritten by the
        // next claimant. Clearing it first would create a crash window where
        // a rapidly reused PID could make a dead owner appear live.
        let _ = self.compare_exchange_u64_field(
            offsets::SNAPSHOT_PUBLISHER_OWNER,
            &self.snapshot_publisher_owner,
            identity.active_token,
            0,
            Ordering::Release,
            Ordering::Acquire,
        );
    }

    fn begin_snapshot_publish_with(
        &self,
        identity: SnapshotPublisherIdentity,
        owner_alive: &impl Fn(u32, u64) -> bool,
        force_recovery: bool,
    ) -> SnapshotPublishPermit {
        let mut spins = 0_u32;
        loop {
            let sequence =
                self.load_u64_field(offsets::SNAPSHOT_SEQ, &self.snapshot_seq, Ordering::Acquire);
            let owner = self.load_u64_field(
                offsets::SNAPSHOT_PUBLISHER_OWNER,
                &self.snapshot_publisher_owner,
                Ordering::Acquire,
            );

            let sequence_is_odd = sequence % 2 == 1;
            let may_claim = if owner == 0 {
                !sequence_is_odd || force_recovery
            } else {
                !self.snapshot_publisher_is_alive(owner, owner_alive)
            };

            if !may_claim || !self.try_claim_snapshot_publisher(owner, identity) {
                Self::wait_for_snapshot_publisher(&mut spins);
                continue;
            }

            let claimed_sequence =
                self.load_u64_field(offsets::SNAPSHOT_SEQ, &self.snapshot_seq, Ordering::Acquire);
            if claimed_sequence % 2 == 1 {
                // A dead owner was taken over without opening an even window:
                // readers remain excluded until this publisher overwrites the
                // complete triple. An unowned odd sequence is accepted only by
                // explicit recovery, where no live publisher may exist.
                if owner != 0 || force_recovery {
                    return SnapshotPublishPermit {
                        identity,
                        odd_sequence: claimed_sequence,
                    };
                }
                self.release_snapshot_publisher_without_publish(identity);
                Self::wait_for_snapshot_publisher(&mut spins);
                continue;
            }

            if self
                .compare_exchange_u64_field(
                    offsets::SNAPSHOT_SEQ,
                    &self.snapshot_seq,
                    claimed_sequence,
                    claimed_sequence.wrapping_add(1),
                    Ordering::AcqRel,
                    Ordering::Acquire,
                )
                .is_ok()
            {
                return SnapshotPublishPermit {
                    identity,
                    odd_sequence: claimed_sequence.wrapping_add(1),
                };
            }

            self.release_snapshot_publisher_without_publish(identity);
            Self::wait_for_snapshot_publisher(&mut spins);
        }
    }

    /// Begin a snapshot publish cycle (increment seqlock from even to odd).
    ///
    /// GH #199: entry requires **exclusive ownership** of the even→odd
    /// transition. An odd sequence means another publisher is inside the
    /// critical section, so this spins until that publisher completes rather
    /// than barging in — two concurrent publishers would otherwise interleave
    /// their field stores and leave the sequence odd forever (readers spin).
    ///
    /// Crash-staleness is recovered only after proving the stamped owner dead.
    /// There is no elapsed-time takeover: a descheduled live publisher retains
    /// ownership indefinitely, preserving the seqlock safety contract.
    ///
    /// Protocol constraints (deliberate, documented trade-offs):
    ///
    /// - **Dead-owner detection is procfs-based.** All publishers sharing a
    ///   region must live in one PID namespace; a peer in a different
    ///   namespace could judge a live publisher dead (its PID is not visible
    ///   in `/proc`) and steal the critical section. On platforms without
    ///   `/proc` (and on non-unix), a stamped owner is presumed alive
    ///   forever, so crash recovery of an owner-stamped odd sequence is
    ///   unavailable there — publishers wait until `reconcile` semantics or
    ///   process restart clear the region.
    /// - **Legacy unowned-odd sequences** (left by a pre-ownership binary
    ///   crashing mid-publish) are recovered only by the explicit
    ///   `force_recovery` reconcile path; ordinary publishers wait. Callers
    ///   that open a shared region with possibly-stale contents must run
    ///   reconciliation before the first publish/read on that region.
    /// - **Mixed-version sharing is unsupported.** A pre-ownership binary
    ///   attached to the same region still barges on odd sequences
    ///   (reintroducing GH #199); `LAYOUT_VERSION` was not bumped because the
    ///   byte layout is unchanged, so version negotiation cannot catch this —
    ///   deployments must not mix binaries across this protocol change.
    fn begin_snapshot_publish(&self) -> SnapshotPublishPermit {
        self.begin_snapshot_publish_with(
            SnapshotPublisherIdentity::current(),
            &snapshot_publisher_alive_os,
            false,
        )
    }

    /// End a snapshot publish cycle (increment seqlock from odd to even).
    ///
    /// Uses owner- and generation-checked CAS operations so a stale publisher
    /// can never end a replacement publisher's cycle.
    fn end_snapshot_publish(&self, permit: SnapshotPublishPermit) {
        assert_eq!(
            self.load_u64_field(
                offsets::SNAPSHOT_PUBLISHER_OWNER,
                &self.snapshot_publisher_owner,
                Ordering::Acquire,
            ),
            permit.identity.active_token,
            "snapshot publication ended by a non-owner"
        );
        self.compare_exchange_u64_field(
            offsets::SNAPSHOT_SEQ,
            &self.snapshot_seq,
            permit.odd_sequence,
            permit.odd_sequence.wrapping_add(1),
            Ordering::Release,
            Ordering::Acquire,
        )
        .expect("snapshot publisher must end its owned odd sequence");

        // The birth marker deliberately remains after ownership is released.
        // It is irrelevant while owner == 0 and the next initializer replaces
        // it before publishing its active token. Keeping it avoids a crash
        // window in which a reused PID could be mistaken for this owner.
        self.compare_exchange_u64_field(
            offsets::SNAPSHOT_PUBLISHER_OWNER,
            &self.snapshot_publisher_owner,
            permit.identity.active_token,
            0,
            Ordering::Release,
            Ordering::Acquire,
        )
        .expect("snapshot publisher owner token changed before release");
    }

    /// Load a consistent `(commit_seq, schema_epoch, ecs_epoch)` triple
    /// via the seqlock spin-retry protocol.
    ///
    /// Spins while `snapshot_seq` is odd (write in progress) or changes
    /// between the pre-read and post-read.
    #[must_use]
    pub fn load_consistent_snapshot(&self) -> ShmSnapshot {
        let mut spins = 0_u32;
        loop {
            let seq1 =
                self.load_u64_field(offsets::SNAPSHOT_SEQ, &self.snapshot_seq, Ordering::Acquire);
            if seq1 % 2 == 1 {
                Self::wait_for_snapshot_publisher(&mut spins);
                continue;
            }

            let cs = self.load_u64_field(offsets::COMMIT_SEQ, &self.commit_seq, Ordering::Acquire);
            let se =
                self.load_u64_field(offsets::SCHEMA_EPOCH, &self.schema_epoch, Ordering::Acquire);
            let ee = self.load_u64_field(offsets::ECS_EPOCH, &self.ecs_epoch, Ordering::Acquire);

            let seq2 =
                self.load_u64_field(offsets::SNAPSHOT_SEQ, &self.snapshot_seq, Ordering::Acquire);
            if seq1 == seq2 {
                return ShmSnapshot {
                    commit_seq: CommitSeq::new(cs),
                    schema_epoch: SchemaEpoch::new(se),
                    ecs_epoch: ee,
                };
            }
            Self::wait_for_snapshot_publisher(&mut spins);
        }
    }

    /// Convenience: atomically publish a new snapshot triple.
    ///
    /// DDL publication ordering (§5.6.1, spec line 6800): `schema_epoch` is
    /// stored **before** `commit_seq` so any reader that observes the new
    /// `commit_seq` also observes the corresponding schema epoch change.
    pub fn publish_snapshot(
        &self,
        commit_seq: CommitSeq,
        schema_epoch: SchemaEpoch,
        ecs_epoch: u64,
    ) {
        let permit = self.begin_snapshot_publish();
        let current_commit_seq =
            self.load_u64_field(offsets::COMMIT_SEQ, &self.commit_seq, Ordering::Acquire);

        // Concurrent commits can finish publication out of allocation order.
        // Never let a late lower sequence overwrite a newer complete triple.
        let published = commit_seq.get() >= current_commit_seq;
        if published {
            // DDL ordering: schema_epoch (Release) before commit_seq (Release).
            self.store_u64_field(
                offsets::SCHEMA_EPOCH,
                &self.schema_epoch,
                schema_epoch.get(),
                Ordering::Release,
            );
            self.store_u64_field(
                offsets::ECS_EPOCH,
                &self.ecs_epoch,
                ecs_epoch,
                Ordering::Release,
            );
            self.store_u64_field(
                offsets::COMMIT_SEQ,
                &self.commit_seq,
                commit_seq.get(),
                Ordering::Release,
            );
        }
        self.end_snapshot_publish(permit);
        // Logged outside the seqlock critical section: while the sequence is
        // odd every reader spins, so nothing that can allocate, format, or
        // panic belongs between begin and end.
        if !published {
            tracing::debug!(
                attempted_commit_seq = commit_seq.get(),
                current_commit_seq,
                "skipped stale out-of-order shared snapshot publication"
            );
        }
    }

    // -----------------------------------------------------------------------
    // Reconciliation
    // -----------------------------------------------------------------------

    /// Reconcile SHM state with durable reality after recovery.
    ///
    /// Clamps each field to the durable value: if SHM is ahead, rewind;
    /// if behind, advance. The update is protected by the seqlock.
    pub fn reconcile(
        &self,
        durable_commit_seq: CommitSeq,
        durable_schema_epoch: SchemaEpoch,
        durable_ecs_epoch: u64,
    ) {
        // Recovery may claim an abandoned owner (or a legacy unowned odd
        // sequence) while keeping the sequence odd, then overwrite the
        // complete triple. It still waits for a proven-live owner rather than
        // stealing its critical section. Readers never observe the partial
        // state that a crashed publisher may have left behind.
        let permit = self.begin_snapshot_publish_with(
            SnapshotPublisherIdentity::current(),
            &snapshot_publisher_alive_os,
            true,
        );

        // DDL ordering: schema_epoch before commit_seq (§5.6.1).
        self.store_u64_field(
            offsets::SCHEMA_EPOCH,
            &self.schema_epoch,
            durable_schema_epoch.get(),
            Ordering::Release,
        );
        self.store_u64_field(
            offsets::ECS_EPOCH,
            &self.ecs_epoch,
            durable_ecs_epoch,
            Ordering::Release,
        );
        self.store_u64_field(
            offsets::COMMIT_SEQ,
            &self.commit_seq,
            durable_commit_seq.get(),
            Ordering::Release,
        );

        self.end_snapshot_publish(permit);
    }

    // -----------------------------------------------------------------------
    // Serialized writer indicator
    // -----------------------------------------------------------------------

    /// Attempt to acquire the serialized writer indicator.
    ///
    /// Returns `true` if acquired (field was 0 → now `writer_txn_id_raw`).
    /// Returns `false` if another writer holds it.
    pub fn acquire_serialized_writer(
        &self,
        writer_txn_id_raw: u64,
        pid: u32,
        pid_birth: u64,
        lease_expiry_epoch_secs: u64,
    ) -> bool {
        assert_ne!(
            writer_txn_id_raw, 0,
            "serialized writer txn id must be non-zero"
        );
        // CAS 0 → writer_txn_id_raw.
        if self
            .compare_exchange_u64_field(
                offsets::SERIALIZED_WRITER_TXN_ID,
                &self.serialized_writer_txn_id,
                0,
                writer_txn_id_raw,
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .is_err()
        {
            return false;
        }

        let old_packed = self.load_u64_field(
            offsets::SERIALIZED_WRITER_PID_AND_GEN,
            &self.serialized_writer_pid_and_gen,
            Ordering::Acquire,
        );
        let old_gen = (old_packed >> 32) as u32;
        let new_gen = old_gen.wrapping_add(1);
        let new_packed = u64::from(pid) | (u64::from(new_gen) << 32);

        self.store_u64_field(
            offsets::SERIALIZED_WRITER_PID_AND_GEN,
            &self.serialized_writer_pid_and_gen,
            new_packed,
            Ordering::Release,
        );
        self.store_u64_field(
            offsets::SERIALIZED_WRITER_PID_BIRTH,
            &self.serialized_writer_pid_birth,
            pid_birth,
            Ordering::Release,
        );
        self.store_u64_field(
            offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
            &self.serialized_writer_lease_expiry,
            lease_expiry_epoch_secs,
            Ordering::Release,
        );
        true
    }

    /// Release the serialized writer indicator.
    ///
    /// Returns `true` if released (writer txn id matched).
    /// Per spec: clear writer txn id BEFORE releasing mutex.
    pub fn release_serialized_writer(&self, writer_txn_id_raw: u64) -> bool {
        // Read our own aux fields before releasing the lock so we can carefully CAS them.
        let my_packed = self.load_u64_field(
            offsets::SERIALIZED_WRITER_PID_AND_GEN,
            &self.serialized_writer_pid_and_gen,
            Ordering::Acquire,
        );
        let my_birth = self.load_u64_field(
            offsets::SERIALIZED_WRITER_PID_BIRTH,
            &self.serialized_writer_pid_birth,
            Ordering::Acquire,
        );
        let my_lease = self.load_u64_field(
            offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
            &self.serialized_writer_lease_expiry,
            Ordering::Acquire,
        );

        if self
            .compare_exchange_u64_field(
                offsets::SERIALIZED_WRITER_TXN_ID,
                &self.serialized_writer_txn_id,
                writer_txn_id_raw,
                0,
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .is_err()
        {
            return false;
        }

        // Clear auxiliary fields using CAS to avoid stomping on a new writer's
        // fields if they managed to acquire the lock immediately after we released it.
        // Because `pid_and_gen` incorporates a generation counter incremented on every
        // acquire, this CAS is guaranteed to fail if a new writer acquired the lock,
        // even if the new writer is from the exact same process (same PID).
        let _ = self.compare_exchange_u64_field(
            offsets::SERIALIZED_WRITER_PID_AND_GEN,
            &self.serialized_writer_pid_and_gen,
            my_packed,
            0,
            Ordering::AcqRel,
            Ordering::Relaxed,
        );
        let _ = self.compare_exchange_u64_field(
            offsets::SERIALIZED_WRITER_PID_BIRTH,
            &self.serialized_writer_pid_birth,
            my_birth,
            0,
            Ordering::AcqRel,
            Ordering::Relaxed,
        );
        let _ = self.compare_exchange_u64_field(
            offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
            &self.serialized_writer_lease_expiry,
            my_lease,
            0,
            Ordering::AcqRel,
            Ordering::Relaxed,
        );
        true
    }

    /// Check whether a serialized writer is currently active.
    ///
    /// Returns `Some(TxnId)` if a writer holds the indicator, `None` otherwise.
    #[must_use]
    pub fn check_serialized_writer(&self) -> Option<TxnId> {
        let writer_txn_id_raw = self.load_u64_field(
            offsets::SERIALIZED_WRITER_TXN_ID,
            &self.serialized_writer_txn_id,
            Ordering::Acquire,
        );
        TxnId::new(writer_txn_id_raw)
    }

    /// Check serialized-writer exclusion for concurrent writers (§5.8.1).
    ///
    /// Returns `Ok(())` if no serialized writer is active (or if a stale
    /// indicator was successfully cleared). Returns `Err(MvccError::Busy)`
    /// if a non-stale serialized writer is active.
    ///
    /// The stale-indicator cleanup loop is linearizable:
    /// - Acquire-load the writer txn id.
    /// - If stale, CAS-clear with AcqRel.
    /// - Retry on CAS races.
    pub fn check_serialized_writer_exclusion(
        &self,
        now_epoch_secs: u64,
        process_alive: impl Fn(u32, u64) -> bool,
    ) -> Result<(), MvccError> {
        let mut noop = |_writer_txn_id_raw: u64| {};
        self.check_serialized_writer_exclusion_with_hook(now_epoch_secs, process_alive, &mut noop)
    }

    fn check_serialized_writer_exclusion_with_hook<F>(
        &self,
        now_epoch_secs: u64,
        process_alive: impl Fn(u32, u64) -> bool,
        on_stale_before_cas: &mut F,
    ) -> Result<(), MvccError>
    where
        F: FnMut(u64),
    {
        loop {
            let writer_txn_id_raw = self.load_u64_field(
                offsets::SERIALIZED_WRITER_TXN_ID,
                &self.serialized_writer_txn_id,
                Ordering::Acquire,
            );
            if writer_txn_id_raw == 0 {
                return Ok(());
            }

            let packed_pid = self.load_u64_field(
                offsets::SERIALIZED_WRITER_PID_AND_GEN,
                &self.serialized_writer_pid_and_gen,
                Ordering::Acquire,
            );
            let pid = packed_pid as u32;
            let pid_birth = self.load_u64_field(
                offsets::SERIALIZED_WRITER_PID_BIRTH,
                &self.serialized_writer_pid_birth,
                Ordering::Acquire,
            );
            let lease_expiry = self.load_u64_field(
                offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
                &self.serialized_writer_lease_expiry,
                Ordering::Acquire,
            );

            let lease_set = lease_expiry != 0;
            let lease_expired = lease_set && now_epoch_secs >= lease_expiry;
            let process_dead = pid != 0 && pid_birth != 0 && !process_alive(pid, pid_birth);

            // If a lease is set, it is authoritative. Only fall back to process
            // liveness when the lease field is missing.
            let writer_live = if lease_set {
                !lease_expired
            } else {
                !process_dead
            };

            if writer_live {
                tracing::warn!(
                    writer_txn_id_raw,
                    pid,
                    pid_birth,
                    lease_expiry,
                    "serialized writer active: concurrent writer excluded"
                );
                return Err(MvccError::Busy);
            }

            // The writer appears dead. But this might be a torn read where a new writer
            // just CAS'd txn_id but hasn't yet updated the aux fields. In this microscopic
            // window, the aux fields belong to the previous writer.
            // If we spin briefly, the new writer will have updated the aux fields.
            let mut is_torn = false;
            for _ in 0..10 {
                std::hint::spin_loop();
                if self.load_u64_field(
                    offsets::SERIALIZED_WRITER_TXN_ID,
                    &self.serialized_writer_txn_id,
                    Ordering::Acquire,
                ) != writer_txn_id_raw
                    || self.load_u64_field(
                        offsets::SERIALIZED_WRITER_PID_AND_GEN,
                        &self.serialized_writer_pid_and_gen,
                        Ordering::Acquire,
                    ) != packed_pid
                {
                    is_torn = true;
                    break;
                }
            }
            if is_torn {
                continue; // It was a torn read (or a completely new writer). Retry.
            }

            tracing::debug!(
                writer_txn_id_raw,
                pid,
                pid_birth,
                lease_expiry,
                lease_expired,
                process_dead,
                "serialized writer indicator appears stale; attempting CAS clear"
            );

            on_stale_before_cas(writer_txn_id_raw);

            if self
                .compare_exchange_u64_field(
                    offsets::SERIALIZED_WRITER_TXN_ID,
                    &self.serialized_writer_txn_id,
                    writer_txn_id_raw,
                    0,
                    Ordering::AcqRel,
                    Ordering::Acquire,
                )
                .is_ok()
            {
                // Clear auxiliary fields using CAS to avoid stomping a new writer's fields.
                let _ = self.compare_exchange_u64_field(
                    offsets::SERIALIZED_WRITER_PID_AND_GEN,
                    &self.serialized_writer_pid_and_gen,
                    packed_pid,
                    0,
                    Ordering::AcqRel,
                    Ordering::Relaxed,
                );
                let _ = self.compare_exchange_u64_field(
                    offsets::SERIALIZED_WRITER_PID_BIRTH,
                    &self.serialized_writer_pid_birth,
                    pid_birth,
                    0,
                    Ordering::AcqRel,
                    Ordering::Relaxed,
                );
                let _ = self.compare_exchange_u64_field(
                    offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
                    &self.serialized_writer_lease_expiry,
                    lease_expiry,
                    0,
                    Ordering::AcqRel,
                    Ordering::Relaxed,
                );

                tracing::info!(
                    writer_txn_id_raw,
                    "cleared stale serialized writer indicator via CAS"
                );
                return Ok(());
            }

            // CAS race: some other cleaner or legitimate release/reacquire won.
            // Loop and re-check.
        }
    }

    // -----------------------------------------------------------------------
    // Field accessors
    // -----------------------------------------------------------------------

    /// Load the current commit sequence.
    #[must_use]
    pub fn load_commit_seq(&self) -> CommitSeq {
        CommitSeq::new(self.load_u64_field(
            offsets::COMMIT_SEQ,
            &self.commit_seq,
            Ordering::Acquire,
        ))
    }

    /// Load the current GC horizon.
    #[must_use]
    pub fn load_gc_horizon(&self) -> CommitSeq {
        CommitSeq::new(self.load_u64_field(
            offsets::GC_HORIZON,
            &self.gc_horizon,
            Ordering::Acquire,
        ))
    }

    /// Store the GC horizon.
    pub fn store_gc_horizon(&self, horizon: CommitSeq) {
        self.store_u64_field(
            offsets::GC_HORIZON,
            &self.gc_horizon,
            horizon.get(),
            Ordering::Release,
        );
    }

    /// Allocate the next `TxnId` via CAS loop.
    ///
    /// Returns `None` if the id space is exhausted.
    pub fn alloc_txn_id(&self) -> Option<TxnId> {
        loop {
            let current =
                self.load_u64_field(offsets::NEXT_TXN_ID, &self.next_txn_id, Ordering::Acquire);
            if current > TxnId::MAX_RAW {
                return None;
            }
            let next = current.checked_add(1)?;
            if self
                .compare_exchange_u64_field(
                    offsets::NEXT_TXN_ID,
                    &self.next_txn_id,
                    current,
                    next,
                    Ordering::AcqRel,
                    Ordering::Acquire,
                )
                .is_ok()
            {
                return TxnId::new(current);
            }
        }
    }

    /// Load the current schema epoch.
    #[must_use]
    pub fn load_schema_epoch(&self) -> SchemaEpoch {
        SchemaEpoch::new(self.load_u64_field(
            offsets::SCHEMA_EPOCH,
            &self.schema_epoch,
            Ordering::Acquire,
        ))
    }

    /// Load the current ECS epoch.
    #[must_use]
    pub fn load_ecs_epoch(&self) -> u64 {
        self.load_u64_field(offsets::ECS_EPOCH, &self.ecs_epoch, Ordering::Acquire)
    }

    /// Load the next commit-sequence reservation watermark.
    #[must_use]
    pub fn load_next_commit_seq(&self) -> CommitSeq {
        CommitSeq::new(self.load_u64_field(
            offsets::NEXT_COMMIT_SEQ,
            &self.next_commit_seq,
            Ordering::Acquire,
        ))
    }

    /// Database page size.
    #[must_use]
    pub fn page_size(&self) -> PageSize {
        self.page_size
    }

    /// Maximum number of transaction slots.
    #[must_use]
    pub fn max_txn_slots(&self) -> u32 {
        self.max_txn_slots
    }

    /// Lock table region offset.
    #[must_use]
    pub fn lock_table_offset(&self) -> u64 {
        self.lock_table_offset
    }

    /// Witness region offset.
    #[must_use]
    pub fn witness_offset(&self) -> u64 {
        self.witness_offset
    }

    /// Transaction slot region offset.
    #[must_use]
    pub fn txn_slot_offset(&self) -> u64 {
        self.txn_slot_offset
    }

    /// Committed readers region offset.
    #[must_use]
    pub fn committed_readers_offset(&self) -> u64 {
        self.committed_readers_offset
    }

    /// Committed readers region size in bytes.
    #[must_use]
    pub fn committed_readers_bytes(&self) -> u64 {
        self.committed_readers_bytes
    }

    /// Layout checksum (xxh3_64).
    #[must_use]
    pub fn layout_checksum(&self) -> u64 {
        self.layout_checksum
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Compute the xxh3_64 checksum over all immutable fields.
    fn compute_checksum_from_parts(
        page_size: PageSize,
        max_txn_slots: u32,
        lock_table_offset: u64,
        witness_offset: u64,
        txn_slot_offset: u64,
        committed_readers_offset: u64,
        committed_readers_bytes: u64,
    ) -> u64 {
        // Feed immutable fields in a canonical LE byte order.
        let mut data = Vec::with_capacity(64);
        data.extend_from_slice(&MAGIC);
        data.extend_from_slice(&LAYOUT_VERSION.to_le_bytes());
        data.extend_from_slice(&page_size.get().to_le_bytes());
        data.extend_from_slice(&max_txn_slots.to_le_bytes());
        data.extend_from_slice(&lock_table_offset.to_le_bytes());
        data.extend_from_slice(&witness_offset.to_le_bytes());
        data.extend_from_slice(&txn_slot_offset.to_le_bytes());
        data.extend_from_slice(&committed_readers_offset.to_le_bytes());
        data.extend_from_slice(&committed_readers_bytes.to_le_bytes());
        xxh3_64(&data)
    }
}

impl std::fmt::Debug for SharedMemoryLayout {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SharedMemoryLayout")
            .field("page_size", &self.page_size)
            .field("max_txn_slots", &self.max_txn_slots)
            .field(
                "next_commit_seq",
                &self.load_u64_field(
                    offsets::NEXT_COMMIT_SEQ,
                    &self.next_commit_seq,
                    Ordering::Relaxed,
                ),
            )
            .field(
                "commit_seq",
                &self.load_u64_field(offsets::COMMIT_SEQ, &self.commit_seq, Ordering::Relaxed),
            )
            .field(
                "schema_epoch",
                &self.load_u64_field(offsets::SCHEMA_EPOCH, &self.schema_epoch, Ordering::Relaxed),
            )
            .field(
                "gc_horizon",
                &self.load_u64_field(offsets::GC_HORIZON, &self.gc_horizon, Ordering::Relaxed),
            )
            .field("layout_checksum", &self.layout_checksum)
            .field("mapped_region", &self.mapped_region.is_some())
            .finish_non_exhaustive()
    }
}
// ---------------------------------------------------------------------------
// Wire-format helpers (little-endian)
// ---------------------------------------------------------------------------

fn read_u32(buf: &[u8], offset: usize) -> u32 {
    let bytes: [u8; 4] = buf[offset..offset + 4]
        .try_into()
        .expect("slice length mismatch");
    u32::from_le_bytes(bytes)
}

fn read_u64(buf: &[u8], offset: usize) -> u64 {
    let bytes: [u8; 8] = buf[offset..offset + 8]
        .try_into()
        .expect("slice length mismatch");
    u64::from_le_bytes(bytes)
}

fn write_u32(buf: &mut [u8], offset: usize, val: u32) {
    buf[offset..offset + 4].copy_from_slice(&val.to_le_bytes());
}

fn write_u64(buf: &mut [u8], offset: usize, val: u64) {
    buf[offset..offset + 8].copy_from_slice(&val.to_le_bytes());
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, atomic::AtomicBool};
    use std::thread;

    // -- Construction / serialization --

    #[test]
    fn test_default_layout() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 128);
        assert_eq!(layout.page_size(), PageSize::DEFAULT);
        assert_eq!(layout.max_txn_slots(), 128);
        assert!(layout.lock_table_offset() >= SharedMemoryLayout::HEADER_SIZE as u64);
        assert!(layout.witness_offset() > layout.lock_table_offset());
        assert!(layout.txn_slot_offset() > layout.witness_offset());
        assert!(layout.committed_readers_offset() > layout.txn_slot_offset());
    }

    #[test]
    fn test_header_size_is_216() {
        assert_eq!(SharedMemoryLayout::HEADER_SIZE, 216);
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let bytes = layout.to_bytes();
        assert_eq!(bytes.len(), 216);
    }

    #[test]
    fn test_roundtrip_serialization() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 256);
        // Publish some dynamic state.
        layout.publish_snapshot(CommitSeq::new(42), SchemaEpoch::new(7), 99);
        layout.store_gc_horizon(CommitSeq::new(10));

        let bytes = layout.to_bytes();
        let restored = SharedMemoryLayout::open(&bytes).unwrap();

        assert_eq!(restored.page_size(), layout.page_size());
        assert_eq!(restored.max_txn_slots(), layout.max_txn_slots());
        assert_eq!(restored.lock_table_offset(), layout.lock_table_offset());
        assert_eq!(restored.witness_offset(), layout.witness_offset());
        assert_eq!(restored.txn_slot_offset(), layout.txn_slot_offset());
        assert_eq!(
            restored.committed_readers_offset(),
            layout.committed_readers_offset()
        );
        assert_eq!(
            restored.committed_readers_bytes(),
            layout.committed_readers_bytes()
        );
        assert_eq!(restored.layout_checksum(), layout.layout_checksum());
        assert_eq!(
            restored.load_commit_seq().get(),
            layout.load_commit_seq().get()
        );
        assert_eq!(
            restored.load_schema_epoch().get(),
            layout.load_schema_epoch().get()
        );
        assert_eq!(restored.load_ecs_epoch(), layout.load_ecs_epoch());
        assert_eq!(
            restored.load_gc_horizon().get(),
            layout.load_gc_horizon().get()
        );
    }

    #[test]
    fn test_open_bad_magic() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let mut bytes = layout.to_bytes();
        bytes[0] = b'X'; // corrupt magic
        assert_eq!(
            SharedMemoryLayout::open(&bytes).unwrap_err(),
            MvccError::ShmBadMagic
        );
    }

    #[test]
    fn test_open_bad_version() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let mut bytes = layout.to_bytes();
        write_u32(&mut bytes, offsets::VERSION, 99);
        assert_eq!(
            SharedMemoryLayout::open(&bytes).unwrap_err(),
            MvccError::ShmVersionMismatch
        );
    }

    #[test]
    fn test_open_bad_checksum() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let mut bytes = layout.to_bytes();
        // Corrupt an immutable field without updating checksum.
        write_u32(&mut bytes, offsets::MAX_TXN_SLOTS, 999);
        assert_eq!(
            SharedMemoryLayout::open(&bytes).unwrap_err(),
            MvccError::ShmChecksumMismatch
        );
    }

    // -- Alignment --

    #[test]
    fn test_atomic_u64_offsets_divisible_by_8() {
        // All u64 field offsets in the wire format must be 8-byte aligned.
        let u64_offsets = [
            offsets::NEXT_TXN_ID,
            offsets::SNAPSHOT_SEQ,
            offsets::COMMIT_SEQ,
            offsets::SCHEMA_EPOCH,
            offsets::ECS_EPOCH,
            offsets::GC_HORIZON,
            offsets::SERIALIZED_WRITER_TXN_ID,
            offsets::SERIALIZED_WRITER_PID_AND_GEN,
            offsets::SERIALIZED_WRITER_PID_BIRTH,
            offsets::SERIALIZED_WRITER_LEASE_EXPIRY,
            offsets::LOCK_TABLE_OFFSET,
            offsets::WITNESS_OFFSET,
            offsets::TXN_SLOT_OFFSET,
            offsets::COMMITTED_READERS_OFFSET,
            offsets::COMMITTED_READERS_BYTES,
            offsets::NEXT_COMMIT_SEQ,
            offsets::LAYOUT_CHECKSUM,
            offsets::SNAPSHOT_PUBLISHER_OWNER,
            offsets::SNAPSHOT_PUBLISHER_PID_BIRTH,
        ];
        for &off in &u64_offsets {
            assert_eq!(off % 8, 0, "offset {off} not 8-byte aligned");
        }
    }

    #[test]
    fn test_atomic_u32_offsets_divisible_by_4() {
        let u32_offsets = [
            offsets::VERSION,
            offsets::PAGE_SIZE,
            offsets::MAX_TXN_SLOTS,
            offsets::ALIGN0,
        ];
        for &off in &u32_offsets {
            assert_eq!(off % 4, 0, "offset {off} not 4-byte aligned");
        }
    }

    // -- Checksum --

    #[test]
    fn test_checksum_immutable_only() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let cksum1 = layout.layout_checksum();

        // Mutate a dynamic field — checksum must NOT change.
        layout.publish_snapshot(CommitSeq::new(999), SchemaEpoch::new(888), 777);

        // Recompute from parts (immutable fields haven't changed).
        let cksum2 = SharedMemoryLayout::compute_checksum_from_parts(
            layout.page_size(),
            layout.max_txn_slots(),
            layout.lock_table_offset(),
            layout.witness_offset(),
            layout.txn_slot_offset(),
            layout.committed_readers_offset(),
            layout.committed_readers_bytes(),
        );
        assert_eq!(
            cksum1, cksum2,
            "checksum must only depend on immutable fields"
        );
    }

    #[test]
    fn test_checksum_deterministic() {
        let a = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let b = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        assert_eq!(a.layout_checksum(), b.layout_checksum());
    }

    #[test]
    fn test_checksum_different_params_differ() {
        let a = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let b = SharedMemoryLayout::new(PageSize::DEFAULT, 128);
        assert_ne!(
            a.layout_checksum(),
            b.layout_checksum(),
            "different max_txn_slots must produce different checksums"
        );

        let c = SharedMemoryLayout::new(PageSize::new(8192).unwrap(), 64);
        assert_ne!(
            a.layout_checksum(),
            c.layout_checksum(),
            "different page_size must produce different checksums"
        );
    }

    // -- Seqlock --

    #[test]
    fn test_seqlock_begin_end_publish() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        assert_eq!(layout.snapshot_seq.load(Ordering::Relaxed), 0);

        let permit = layout.begin_snapshot_publish();
        assert_eq!(
            layout.snapshot_seq.load(Ordering::Relaxed) % 2,
            1,
            "after begin, seq must be odd"
        );

        layout.end_snapshot_publish(permit);
        assert_eq!(
            layout.snapshot_seq.load(Ordering::Relaxed) % 2,
            0,
            "after end, seq must be even"
        );
        assert_eq!(layout.snapshot_seq.load(Ordering::Relaxed), 2);
    }

    #[test]
    fn test_load_consistent_snapshot() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        layout.publish_snapshot(CommitSeq::new(5), SchemaEpoch::new(3), 17);

        let snap = layout.load_consistent_snapshot();
        assert_eq!(snap.commit_seq, CommitSeq::new(5));
        assert_eq!(snap.schema_epoch, SchemaEpoch::new(3));
        assert_eq!(snap.ecs_epoch, 17);
    }

    #[test]
    fn test_seqlock_threaded_retry() {
        let layout = Arc::new(SharedMemoryLayout::new(PageSize::DEFAULT, 64));
        let layout2 = Arc::clone(&layout);

        // Writer thread: publish many snapshots.
        let writer = thread::spawn(move || {
            for i in 1..=1000_u64 {
                layout2.publish_snapshot(CommitSeq::new(i), SchemaEpoch::new(i * 2), i * 3);
            }
        });

        // Reader thread: load consistent snapshots; must never see torn values.
        let reader_layout = Arc::clone(&layout);
        let reader = thread::spawn(move || {
            let mut reads = 0_u64;
            while reads < 5000 {
                let snap = reader_layout.load_consistent_snapshot();
                // Consistency: schema_epoch = 2 * commit_seq, ecs_epoch = 3 * commit_seq.
                let cs = snap.commit_seq.get();
                if cs > 0 {
                    assert_eq!(
                        snap.schema_epoch.get(),
                        cs * 2,
                        "torn read: schema_epoch mismatch at cs={cs}"
                    );
                    assert_eq!(
                        snap.ecs_epoch,
                        cs * 3,
                        "torn read: ecs_epoch mismatch at cs={cs}"
                    );
                }
                reads += 1;
            }
        });

        writer.join().unwrap();
        reader.join().unwrap();
    }

    #[test]
    fn test_seqlock_crash_repair() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);

        // Simulate a legacy crash: sequence left odd without an owner stamp.
        layout.snapshot_seq.store(1, Ordering::Release);
        layout.schema_epoch.store(999, Ordering::Release);
        assert_eq!(layout.snapshot_seq.load(Ordering::Relaxed) % 2, 1);

        // Reconciliation repairs the odd seqlock.
        layout.reconcile(CommitSeq::new(10), SchemaEpoch::new(5), 3);

        assert_eq!(
            layout.snapshot_seq.load(Ordering::Relaxed) % 2,
            0,
            "reconcile must repair odd snapshot_seq"
        );

        let snap = layout.load_consistent_snapshot();
        assert_eq!(snap.commit_seq, CommitSeq::new(10));
        assert_eq!(snap.schema_epoch, SchemaEpoch::new(5));
        assert_eq!(snap.ecs_epoch, 3);
    }

    #[test]
    fn test_seqlock_ddl_ordering() {
        // Verify that publish_snapshot stores schema_epoch before commit_seq
        // (DDL ordering). We can't directly test ordering, but we verify
        // the end result is consistent.
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        layout.publish_snapshot(CommitSeq::new(100), SchemaEpoch::new(50), 25);

        let snap = layout.load_consistent_snapshot();
        assert_eq!(snap.commit_seq, CommitSeq::new(100));
        assert_eq!(snap.schema_epoch, SchemaEpoch::new(50));
        assert_eq!(snap.ecs_epoch, 25);
    }

    #[test]
    fn test_stale_out_of_order_publication_cannot_rewind_snapshot() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        layout.publish_snapshot(CommitSeq::new(100), SchemaEpoch::new(50), 25);

        // Concurrent commits can finish in the opposite order from sequence
        // allocation. The late, older commit must not rewind any member of the
        // already-published triple.
        layout.publish_snapshot(CommitSeq::new(99), SchemaEpoch::new(49), 24);

        assert_eq!(
            layout.load_consistent_snapshot(),
            ShmSnapshot {
                commit_seq: CommitSeq::new(100),
                schema_epoch: SchemaEpoch::new(50),
                ecs_epoch: 25,
            }
        );
    }

    #[test]
    fn test_seqlock_ecs_epoch_included() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        layout.publish_snapshot(CommitSeq::new(1), SchemaEpoch::new(1), 42);

        let snap = layout.load_consistent_snapshot();
        assert_eq!(
            snap.ecs_epoch, 42,
            "ecs_epoch must be included in seqlock-protected snapshot"
        );
    }

    // -- Reconciliation --

    #[test]
    fn test_reconcile_clamp_ahead() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        // SHM is ahead of durable state.
        layout.publish_snapshot(CommitSeq::new(100), SchemaEpoch::new(50), 30);

        layout.reconcile(CommitSeq::new(80), SchemaEpoch::new(40), 20);

        let snap = layout.load_consistent_snapshot();
        assert_eq!(snap.commit_seq, CommitSeq::new(80));
        assert_eq!(snap.schema_epoch, SchemaEpoch::new(40));
        assert_eq!(snap.ecs_epoch, 20);
    }

    #[test]
    fn test_reconcile_advance_behind() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        // SHM is behind durable state.
        layout.publish_snapshot(CommitSeq::new(10), SchemaEpoch::new(5), 3);

        layout.reconcile(CommitSeq::new(50), SchemaEpoch::new(25), 15);

        let snap = layout.load_consistent_snapshot();
        assert_eq!(snap.commit_seq, CommitSeq::new(50));
        assert_eq!(snap.schema_epoch, SchemaEpoch::new(25));
        assert_eq!(snap.ecs_epoch, 15);
    }

    #[test]
    fn test_reconcile_repair_odd_seq() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);

        // Leave an unowned legacy seqlock in an odd, partially written state.
        layout.snapshot_seq.store(1, Ordering::Release);
        layout.commit_seq.store(99, Ordering::Release);
        let seq_before = layout.snapshot_seq.load(Ordering::Relaxed);
        assert_eq!(seq_before % 2, 1);

        layout.reconcile(CommitSeq::new(1), SchemaEpoch::new(1), 1);

        let seq_after = layout.snapshot_seq.load(Ordering::Relaxed);
        assert_eq!(seq_after % 2, 0, "reconcile must leave seqlock even");
    }

    // -- Serialized writer --

    #[test]
    fn test_serialized_writer_acquire_release() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);

        assert!(layout.check_serialized_writer().is_none());
        assert!(layout.acquire_serialized_writer(42, 1234, 999, 10_000));
        assert!(layout.check_serialized_writer().is_some());
        assert_eq!(layout.check_serialized_writer().unwrap().get(), 42);

        assert!(layout.release_serialized_writer(42));
        assert!(layout.check_serialized_writer().is_none());
    }

    #[test]
    fn test_serialized_writer_idempotent_release() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);

        assert!(layout.acquire_serialized_writer(42, 1234, 999, 10_000));
        assert!(layout.release_serialized_writer(42));
        // Second release with the same writer_txn_id should fail (already cleared).
        assert!(!layout.release_serialized_writer(42));
    }

    #[test]
    fn test_serialized_writer_blocks_second_writer_txn_id() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);

        assert!(layout.acquire_serialized_writer(42, 1234, 999, 10_000));
        // Another writer_txn_id should be blocked.
        assert!(!layout.acquire_serialized_writer(99, 5678, 888, 10_000));
        // Original can still release.
        assert!(layout.release_serialized_writer(42));
    }

    #[test]
    fn test_serialized_writer_lease_set() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let lease_expiry = 10_000_u64;
        assert!(layout.acquire_serialized_writer(42, 1234, 999, lease_expiry));

        assert_eq!(
            layout
                .serialized_writer_lease_expiry
                .load(Ordering::Relaxed),
            lease_expiry
        );
        assert_eq!(
            layout.serialized_writer_pid_and_gen.load(Ordering::Relaxed) as u32,
            1234
        );
        assert_eq!(
            layout.serialized_writer_pid_birth.load(Ordering::Relaxed),
            999
        );
    }

    // -- Edge cases --

    #[test]
    fn test_buffer_too_small() {
        let buf = vec![0u8; 100]; // less than HEADER_SIZE
        assert_eq!(
            SharedMemoryLayout::open(&buf).unwrap_err(),
            MvccError::ShmTooSmall
        );
    }

    #[test]
    fn test_stale_indicator_cleared_by_cas() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let now = 100_u64;

        // Expired lease => stale.
        assert!(layout.acquire_serialized_writer(42, 1234, 999, now.saturating_sub(1)));
        assert!(layout.check_serialized_writer().is_some());

        let res = layout.check_serialized_writer_exclusion(now, |_pid, _birth| false);
        assert!(res.is_ok(), "stale indicator should be cleared");
        assert!(layout.check_serialized_writer().is_none());
        assert_eq!(
            layout.serialized_writer_pid_and_gen.load(Ordering::Relaxed) as u32,
            0
        );
        assert_eq!(
            layout.serialized_writer_pid_birth.load(Ordering::Relaxed),
            0
        );
        assert_eq!(
            layout
                .serialized_writer_lease_expiry
                .load(Ordering::Relaxed),
            0
        );
    }

    #[test]
    fn test_stale_indicator_clears_when_now_equals_lease_expiry() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let now = 500_u64;

        // Boundary condition: lease expires exactly at `now`.
        assert!(layout.acquire_serialized_writer(77, 4321, 123, now));
        assert!(layout.check_serialized_writer().is_some());

        let res = layout.check_serialized_writer_exclusion(now, |_pid, _birth| true);
        assert!(res.is_ok(), "lease boundary should be treated as stale");
        assert!(
            layout.check_serialized_writer().is_none(),
            "stale indicator must be cleared at lease boundary"
        );
    }

    #[test]
    fn test_cas_retry_on_new_writer_during_stale_clear() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let now = 100_u64;

        // Start with an expired (stale) indicator.
        assert!(layout.acquire_serialized_writer(42, 1234, 999, now.saturating_sub(1)));

        // Inject: between load and CAS, simulate legitimate release + new writer acquire.
        let mut injected = false;
        let res = layout.check_serialized_writer_exclusion_with_hook(
            now,
            |_pid, _birth| true,
            &mut |writer_txn_id| {
                if injected {
                    return;
                }
                injected = true;
                assert_eq!(writer_txn_id, 42);
                assert!(layout.release_serialized_writer(42));
                assert!(layout.acquire_serialized_writer(99, 5678, 888, now + 10_000));
            },
        );

        assert_eq!(
            res.unwrap_err(),
            MvccError::Busy,
            "new writer should block stale cleanup completion"
        );
        assert_eq!(layout.check_serialized_writer().unwrap().get(), 99);
    }

    #[test]
    fn test_various_page_sizes() {
        for &ps_raw in &[512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] {
            let ps = PageSize::new(ps_raw).unwrap();
            let layout = SharedMemoryLayout::new(ps, 64);
            let bytes = layout.to_bytes();
            let restored = SharedMemoryLayout::open(&bytes).unwrap();
            assert_eq!(restored.page_size(), ps);
        }
    }

    // -- TxnId allocation --

    #[test]
    fn test_alloc_txn_id() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let id1 = layout.alloc_txn_id().unwrap();
        let id2 = layout.alloc_txn_id().unwrap();
        assert_eq!(id1.get(), 1);
        assert_eq!(id2.get(), 2);
    }

    #[test]
    fn test_alloc_txn_id_threaded() {
        let layout = Arc::new(SharedMemoryLayout::new(PageSize::DEFAULT, 64));
        let mut all_ids: Vec<u64> = (0..4)
            .map(|_| {
                let l = Arc::clone(&layout);
                thread::spawn(move || {
                    let mut ids = Vec::with_capacity(100);
                    for _ in 0..100 {
                        ids.push(l.alloc_txn_id().unwrap().get());
                    }
                    ids
                })
            })
            .flat_map(|h| h.join().unwrap())
            .collect();
        all_ids.sort_unstable();
        all_ids.dedup();
        assert_eq!(all_ids.len(), 400, "all 400 TxnIds must be unique");
    }

    // -- Bead bd-3t3.5 completion tests --

    #[test]
    fn test_shm_magic_version_checksum() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 128);
        let bytes = layout.to_bytes();

        // Verify magic.
        assert_eq!(&bytes[0..8], b"FSQLSHM\0", "magic must be FSQLSHM\\0");

        // Verify version.
        let version = read_u32(&bytes, offsets::VERSION);
        assert_eq!(version, 1, "layout version must be 1");

        // Verify checksum matches recomputation.
        let stored = read_u64(&bytes, offsets::LAYOUT_CHECKSUM);
        assert_eq!(
            stored,
            layout.layout_checksum(),
            "stored checksum must match computed checksum"
        );
    }

    #[test]
    #[allow(clippy::items_after_statements)]
    fn test_align_padding_fields_are_zero() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let bytes = layout.to_bytes();

        let align0 = read_u32(&bytes, offsets::ALIGN0);
        assert_eq!(align0, 0, "_align0 padding must be 0");
    }

    #[test]
    fn test_dead_snapshot_publisher_takeover_keeps_readers_excluded() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let dead = SnapshotPublisherIdentity::for_test(111, 1, 1_001);
        let abandoned = layout.begin_snapshot_publish_with(dead, &|_, _| true, false);
        layout.schema_epoch.store(999, Ordering::Release);
        assert_eq!(abandoned.odd_sequence % 2, 1);

        let replacement = SnapshotPublisherIdentity::for_test(222, 1, 2_002);
        let permit = layout.begin_snapshot_publish_with(
            replacement,
            &|pid, birth| !(pid == dead.pid && birth == dead.pid_birth),
            false,
        );

        assert_eq!(
            permit.odd_sequence, abandoned.odd_sequence,
            "dead-owner takeover must not open an even reader window"
        );
        layout.schema_epoch.store(20, Ordering::Release);
        layout.ecs_epoch.store(30, Ordering::Release);
        layout.commit_seq.store(10, Ordering::Release);
        layout.end_snapshot_publish(permit);

        assert_eq!(
            layout.load_consistent_snapshot(),
            ShmSnapshot {
                commit_seq: CommitSeq::new(10),
                schema_epoch: SchemaEpoch::new(20),
                ecs_epoch: 30,
            }
        );
        assert_eq!(layout.snapshot_publisher_owner.load(Ordering::Acquire), 0);
        assert_eq!(
            layout.snapshot_publisher_pid_birth.load(Ordering::Acquire),
            replacement.pid_birth,
            "released owner birth remains as inert PID-reuse evidence"
        );
    }

    #[test]
    fn test_recovery_waits_for_live_snapshot_publisher() {
        let layout = Arc::new(SharedMemoryLayout::new(PageSize::DEFAULT, 64));
        let first_permit = layout.begin_snapshot_publish();
        let recovery_acquired = Arc::new(AtomicBool::new(false));

        let recovery = {
            let layout = Arc::clone(&layout);
            let recovery_acquired = Arc::clone(&recovery_acquired);
            thread::spawn(move || {
                let identity = SnapshotPublisherIdentity::for_test(222, 2, 2_002);
                let permit = layout.begin_snapshot_publish_with(identity, &|_, _| true, true);
                recovery_acquired.store(true, Ordering::Release);
                layout.end_snapshot_publish(permit);
            })
        };

        thread::sleep(std::time::Duration::from_millis(20));
        assert!(
            !recovery_acquired.load(Ordering::Acquire),
            "recovery must not steal a proven-live publisher's critical section"
        );

        layout.end_snapshot_publish(first_permit);
        recovery.join().unwrap();
        assert!(recovery_acquired.load(Ordering::Acquire));
        assert_eq!(layout.snapshot_seq.load(Ordering::Acquire) % 2, 0);
    }

    /// GH #199: a second live publisher must not enter the critical section
    /// while the first is inside. Pre-fix, `begin_snapshot_publish` treated
    /// any odd sequence as crash-stale and returned immediately, so two
    /// begin/end pairs left the sequence odd and readers spun forever.
    #[test]
    fn test_concurrent_publishers_serialize_and_leave_even_sequence() {
        let layout = Arc::new(SharedMemoryLayout::new(PageSize::DEFAULT, 64));

        // First publisher enters.
        let first_permit = layout.begin_snapshot_publish();
        assert_eq!(layout.snapshot_seq.load(Ordering::Relaxed) % 2, 1);

        // Second publisher must block until the first completes.
        let second_acquired = Arc::new(AtomicBool::new(false));
        let second = {
            let layout = Arc::clone(&layout);
            let second_acquired = Arc::clone(&second_acquired);
            thread::spawn(move || {
                let permit = layout.begin_snapshot_publish();
                second_acquired.store(true, Ordering::Release);
                layout.end_snapshot_publish(permit);
            })
        };

        // Give the second publisher time to reach the spin-wait; it cannot
        // have advanced the sequence while the first cycle is open.
        thread::sleep(std::time::Duration::from_millis(20));
        assert!(
            !second_acquired.load(Ordering::Acquire),
            "second live publisher must remain blocked behind the owner"
        );
        assert_eq!(
            layout.snapshot_seq.load(Ordering::Relaxed) % 2,
            1,
            "second publisher must not enter while the first cycle is open"
        );

        // First publisher completes; the second can now run its full cycle.
        layout.end_snapshot_publish(first_permit);
        second.join().unwrap();
        assert!(second_acquired.load(Ordering::Acquire));

        let final_seq = layout.snapshot_seq.load(Ordering::Relaxed);
        assert_eq!(
            final_seq % 2,
            0,
            "completed publications must leave an even sequence"
        );
        assert_eq!(final_seq, 4, "two full publish cycles = four increments");
    }

    #[test]
    fn test_load_consistent_snapshot_retries_until_even() {
        // Use a thread to verify the reader retries while seq is odd,
        // then succeeds once the writer completes.
        let layout = Arc::new(SharedMemoryLayout::new(PageSize::DEFAULT, 64));

        // Publish initial values.
        layout.publish_snapshot(CommitSeq::new(10), SchemaEpoch::new(20), 30);

        // Begin a new publish (seq goes odd).
        let permit = layout.begin_snapshot_publish();
        // Write new values while in odd state.
        layout.schema_epoch.store(200, Ordering::Release);
        layout.ecs_epoch.store(300, Ordering::Release);
        layout.commit_seq.store(100, Ordering::Release);

        let reader_layout = Arc::clone(&layout);
        let reader = thread::spawn(move || reader_layout.load_consistent_snapshot());

        // Brief delay so the reader spins on the odd seqlock.
        thread::sleep(std::time::Duration::from_millis(5));

        // Complete the publish (seq goes even).
        layout.end_snapshot_publish(permit);

        let snap = reader.join().unwrap();
        // Reader must see the final published values, not partial.
        assert_eq!(snap.commit_seq.get(), 100);
        assert_eq!(snap.schema_epoch.get(), 200);
        assert_eq!(snap.ecs_epoch, 300);
    }

    #[test]
    fn test_serialized_writer_zero_means_no_writer() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        assert!(
            layout.check_serialized_writer().is_none(),
            "writer_txn_id=0 must mean no active serialized writer"
        );
    }

    #[test]
    fn test_serialized_writer_aux_cleared_on_release() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        assert!(layout.acquire_serialized_writer(42, 1234, 999, 10_000));

        // Verify aux fields are set.
        assert_eq!(
            layout.serialized_writer_pid_and_gen.load(Ordering::Relaxed) as u32,
            1234
        );
        assert_eq!(
            layout.serialized_writer_pid_birth.load(Ordering::Relaxed),
            999
        );

        // Release.
        assert!(layout.release_serialized_writer(42));

        // Token cleared first, then aux fields.
        assert!(layout.check_serialized_writer().is_none());
        assert_eq!(
            layout.serialized_writer_pid_and_gen.load(Ordering::Relaxed) as u32,
            0,
            "PID must be cleared on release"
        );
        assert_eq!(
            layout.serialized_writer_pid_birth.load(Ordering::Relaxed),
            0,
            "PID birth must be cleared on release"
        );
        assert_eq!(
            layout
                .serialized_writer_lease_expiry
                .load(Ordering::Relaxed),
            0,
            "lease expiry must be cleared on release"
        );
    }

    #[test]
    fn test_zero_max_txn_slots_uses_default() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 0);
        assert_eq!(
            layout.max_txn_slots(),
            128,
            "zero max_txn_slots must use default (128)"
        );
    }

    #[test]
    fn test_shm_reconciliation_never_ahead_of_durable() {
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);

        // Case 1: SHM ahead of durable — must be corrected down.
        layout.publish_snapshot(CommitSeq::new(100), SchemaEpoch::new(50), 30);
        layout.reconcile(CommitSeq::new(95), SchemaEpoch::new(45), 25);
        let snap = layout.load_consistent_snapshot();
        assert_eq!(
            snap.commit_seq.get(),
            95,
            "commit_seq must be clamped to durable"
        );
        assert_eq!(
            snap.schema_epoch.get(),
            45,
            "schema_epoch must be clamped to durable"
        );

        // Case 2: SHM behind durable — must advance.
        layout.reconcile(CommitSeq::new(200), SchemaEpoch::new(100), 60);
        let snap = layout.load_consistent_snapshot();
        assert_eq!(
            snap.commit_seq.get(),
            200,
            "commit_seq must advance to durable"
        );
        assert_eq!(
            snap.schema_epoch.get(),
            100,
            "schema_epoch must advance to durable"
        );
    }

    #[test]
    fn test_ddl_schema_epoch_stored_before_commit_seq() {
        // Verify DDL ordering by interleaving: publish with schema_epoch=2*cs,
        // then verify consistent snapshot always satisfies the invariant.
        // The seqlock ensures atomic visibility; the ordering within the
        // publish window ensures schema_epoch is committed before commit_seq.
        let layout = Arc::new(SharedMemoryLayout::new(PageSize::DEFAULT, 64));
        let writer_layout = Arc::clone(&layout);

        let writer = thread::spawn(move || {
            for i in 1..=500_u64 {
                writer_layout.publish_snapshot(CommitSeq::new(i), SchemaEpoch::new(i * 2), i * 3);
            }
        });

        // Reader: snapshot must always be consistent (schema_epoch = 2 * commit_seq).
        let reader_layout = Arc::clone(&layout);
        let reader = thread::spawn(move || {
            for _ in 0..2000 {
                let snap = reader_layout.load_consistent_snapshot();
                let cs = snap.commit_seq.get();
                if cs > 0 {
                    assert_eq!(
                        snap.schema_epoch.get(),
                        cs * 2,
                        "DDL ordering violation: schema_epoch inconsistent with commit_seq"
                    );
                    assert_eq!(
                        snap.ecs_epoch,
                        cs * 3,
                        "ecs_epoch inconsistent with commit_seq"
                    );
                }
            }
        });

        writer.join().unwrap();
        reader.join().unwrap();
    }

    #[test]
    fn test_open_or_initialize_region_shares_live_state_between_handles() {
        let region = ShmRegion::new(SharedMemoryLayout::HEADER_SIZE);
        let layout_a =
            SharedMemoryLayout::open_or_initialize_region(region.share(), PageSize::DEFAULT, 64)
                .unwrap();
        let layout_b =
            SharedMemoryLayout::open_or_initialize_region(region, PageSize::DEFAULT, 64).unwrap();

        assert_eq!(layout_a.alloc_txn_id().unwrap().get(), 1);
        assert_eq!(layout_b.alloc_txn_id().unwrap().get(), 2);

        layout_a.publish_snapshot(CommitSeq::new(9), SchemaEpoch::new(4), 11);
        let snapshot = layout_b.load_consistent_snapshot();
        assert_eq!(snapshot.commit_seq, CommitSeq::new(9));
        assert_eq!(snapshot.schema_epoch, SchemaEpoch::new(4));
        assert_eq!(snapshot.ecs_epoch, 11);

        assert!(layout_a.acquire_serialized_writer(77, 1234, 5678, 9999));
        assert_eq!(layout_b.check_serialized_writer().unwrap().get(), 77);
        assert!(layout_b.release_serialized_writer(77));
        assert!(layout_a.check_serialized_writer().is_none());
    }

    #[test]
    fn test_mapped_region_publishers_serialize_across_handles() {
        let region = ShmRegion::new(SharedMemoryLayout::HEADER_SIZE);
        let layout_a = Arc::new(
            SharedMemoryLayout::open_or_initialize_region(region.share(), PageSize::DEFAULT, 64)
                .unwrap(),
        );
        let layout_b = Arc::new(
            SharedMemoryLayout::open_or_initialize_region(region, PageSize::DEFAULT, 64).unwrap(),
        );

        let first_permit = layout_a.begin_snapshot_publish();
        let second_acquired = Arc::new(AtomicBool::new(false));
        let second = {
            let layout = Arc::clone(&layout_b);
            let second_acquired = Arc::clone(&second_acquired);
            thread::spawn(move || {
                let permit = layout.begin_snapshot_publish();
                second_acquired.store(true, Ordering::Release);
                layout.end_snapshot_publish(permit);
            })
        };

        thread::sleep(std::time::Duration::from_millis(20));
        assert!(
            !second_acquired.load(Ordering::Acquire),
            "mapped handle must observe and honor the first owner's stamp"
        );

        layout_a.end_snapshot_publish(first_permit);
        second.join().unwrap();
        assert!(second_acquired.load(Ordering::Acquire));
        assert_eq!(
            layout_a.load_u64_field(
                offsets::SNAPSHOT_SEQ,
                &layout_a.snapshot_seq,
                Ordering::Acquire,
            ),
            4,
            "two mapped publish cycles must each complete odd to even"
        );
    }

    #[test]
    fn test_no_reinterpret_cast_safe_mmap_only() {
        // Verify that SharedMemoryLayout uses offset-based typed accessors,
        // not repr(C) reinterpret cast. This is enforced by:
        // 1. workspace-level `unsafe_code = "forbid"` (compile-time)
        // 2. The struct uses native Rust types, not a #[repr(C)] overlay
        //
        // We verify at runtime that the struct is NOT #[repr(C)] by checking
        // that its in-memory size differs from the wire-format size (216).
        let mem_size = std::mem::size_of::<SharedMemoryLayout>();
        // Native Rust layout will likely differ from wire-format 216 bytes
        // because AtomicU64 may have platform-specific alignment/padding.
        // The wire format uses explicit offset-based read/write helpers.
        //
        // Key assertion: we can serialize and deserialize without unsafe.
        let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
        let bytes = layout.to_bytes();
        assert_eq!(bytes.len(), 216);
        let _restored = SharedMemoryLayout::open(&bytes).unwrap();

        // Verify wire-format size is fixed at 216, independent of Rust layout.
        assert_eq!(
            SharedMemoryLayout::HEADER_SIZE,
            216,
            "wire-format header must be exactly 216 bytes"
        );
        // The Rust struct size is NOT 216 — it uses Rust-native layout.
        // This confirms we're NOT doing reinterpret-cast from mmap bytes.
        let _ = mem_size; // used for compile-time verification only
    }

    // -- Property tests --

    mod proptests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            fn seqlock_never_returns_mixed_snapshot(
                cs in 0_u64..10_000,
                se in 0_u64..10_000,
                ee in 0_u64..10_000,
            ) {
                let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
                layout.publish_snapshot(CommitSeq::new(cs), SchemaEpoch::new(se), ee);

                let snap = layout.load_consistent_snapshot();
                // In single-threaded scenario, snapshot must match exactly.
                prop_assert_eq!(snap.commit_seq.get(), cs);
                prop_assert_eq!(snap.schema_epoch.get(), se);
                prop_assert_eq!(snap.ecs_epoch, ee);
            }

            #[test]
            fn reconciliation_sets_exact_values(
                durable_cs in 0_u64..10_000,
                durable_se in 0_u64..10_000,
                durable_ee in 0_u64..10_000,
            ) {
                let layout = SharedMemoryLayout::new(PageSize::DEFAULT, 64);
                // Start with some arbitrary state.
                layout.publish_snapshot(CommitSeq::new(5000), SchemaEpoch::new(5000), 5000);

                layout.reconcile(
                    CommitSeq::new(durable_cs),
                    SchemaEpoch::new(durable_se),
                    durable_ee,
                );

                let snap = layout.load_consistent_snapshot();
                prop_assert_eq!(snap.commit_seq.get(), durable_cs);
                prop_assert_eq!(snap.schema_epoch.get(), durable_se);
                prop_assert_eq!(snap.ecs_epoch, durable_ee);
            }

            #[test]
            fn checksum_deterministic_property(
                ps_idx in 0_usize..8,
                slots in 1_u32..512,
            ) {
                let page_sizes = [512, 1024, 2048, 4096, 8192, 16384, 32768, 65536_u32];
                let ps = PageSize::new(page_sizes[ps_idx]).unwrap();
                let a = SharedMemoryLayout::new(ps, slots);
                let b = SharedMemoryLayout::new(ps, slots);
                prop_assert_eq!(a.layout_checksum(), b.layout_checksum());
            }
        }
    }
}