reddb-io-server 1.27.4

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
use super::*;
use crate::api::DurabilityMode;
use crate::storage::wal::{WalReader, WalRecord, WalWriter};
use reddb_engine::storage::resident_file::EMBEDDED_WAL_HEADROOM_LIMIT;
use std::cell::RefCell;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, AtomicU8, Ordering};
use std::sync::{Arc, Condvar, Mutex};

#[path = "checkpoint_undo.rs"]
mod checkpoint_undo;

struct NativeReplayTarget<'a> {
    store: &'a UnifiedStore,
    update_btrees: bool,
}

impl reddb_engine::storage::store_wal::StoreReplayTarget for NativeReplayTarget<'_> {
    fn set_aux_metadata(&self, bytes: &[u8]) -> Result<(), StoreError> {
        self.store.resident.set_aux_metadata(bytes.to_vec());
        Ok(())
    }
    fn has_collection(&self, name: &str) -> bool {
        self.store.get_collection(name).is_some()
    }
    fn create_collection(&self, name: &str) -> Result<(), StoreError> {
        self.store.create_collection_in_memory(name)
    }
    fn drop_collection(&self, name: &str) -> Result<(), StoreError> {
        self.store.drop_collection_in_memory(name)
    }
    fn upsert(&self, collection: &str, record: &[u8]) -> Result<(), StoreError> {
        self.store
            .apply_replayed_upsert(collection, record, self.update_btrees)
    }
    fn delete(&self, collection: &str, id: EntityId) -> Result<(), StoreError> {
        self.store
            .apply_replayed_delete(collection, id, self.update_btrees)
    }
    fn refresh(&self, collection: &str, records: &[Vec<u8>]) -> Result<(), StoreError> {
        self.store
            .apply_replayed_refresh_collection(collection, records, self.update_btrees)
    }
}

/// Adaptive group-commit window applied when the configured
/// `GroupCommitOptions::window_ms` is 0 (the historical default).
///
/// Sub-millisecond so individual single-writer commits don't see a
/// visible latency penalty (a 200 µs floor sits below typical NVMe
/// fsync latency of 50–150 µs by ~one fsync), while still giving a
/// pipelined writer a chance to drop a second statement into the
/// same drain cycle. A lone synchronous `insert_one` still pays one
/// fsync per row in the worst case, but two back-to-back inserts on
/// the same connection now coalesce into one drain. See P1 in
/// `docs/perf/insert_sequential-2026-05-05.md`.
///
/// Override via `REDDB_GROUP_COMMIT_WINDOW_US` (microseconds). Set
/// to `0` to disable the floor entirely (legacy behaviour). Set
/// `REDDB_GROUP_COMMIT_WINDOW_MS` to any non-zero value to bypass
/// this floor — explicit ms config wins.
const DEFAULT_ADAPTIVE_WINDOW_US: u64 = 200;

/// Shorthand — `Arc<parking_lot::Mutex<WalWriter>>` avoids poisoning
/// (one writer panicking mid-append used to taint every subsequent
/// lock acquisition) and shaves a few syscalls off the fast path.
/// The group-commit coordinator + writer threads all acquire this
/// mutex in the hot insert path, so the unpoison/fast-park win
/// compounds under 16-way concurrency.
type WalMutex = parking_lot::Mutex<WalWriter>;

/// Shorthand for the group-commit coordinator's state pair. Same
/// non-poisoning + lighter-park motivation as `WalMutex`; writer
/// threads `wait` on this condvar until the coordinator publishes a
/// new `durable_lsn`, so the park cost shows up on every
/// WalDurableGrouped transaction.
type CommitStateMutex = parking_lot::Mutex<CommitState>;
type CommitStateCondvar = parking_lot::Condvar;
use std::time::{Duration, Instant};

static NEXT_STORE_TX_ID: AtomicU64 = AtomicU64::new(1);

pub(crate) use reddb_engine::storage::store_wal::StoreWalAction;

#[derive(Debug, Default)]
pub(crate) struct DeferredStoreWalActions {
    pub(super) actions: Vec<StoreWalAction>,
}

pub(super) struct StoreWalCaptureGuard<'a> {
    active: bool,
    session: Option<&'a crate::storage::embedded::EmbeddedCommitSession>,
}

impl<'a> StoreWalCaptureGuard<'a> {
    pub(super) fn begin(
        session: Option<&'a crate::storage::embedded::EmbeddedCommitSession>,
    ) -> Self {
        assert!(
            !deferred_store_wal_capture_active(),
            "store WAL captures cannot nest"
        );
        begin_deferred_store_wal_capture();
        Self {
            active: true,
            session,
        }
    }

    pub(super) fn finish(mut self) -> DeferredStoreWalActions {
        let actions = take_deferred_store_wal_capture();
        self.active = false;
        actions
    }
}

impl Drop for StoreWalCaptureGuard<'_> {
    fn drop(&mut self) {
        if self.active {
            if let Some(session) = self.session {
                session.fence_interrupted_write();
            }
            let _ = take_deferred_store_wal_capture();
        }
    }
}

impl DeferredStoreWalActions {
    pub(crate) fn is_empty(&self) -> bool {
        self.actions.is_empty()
    }

    pub(crate) fn extend(&mut self, other: Self) {
        self.actions.extend(other.actions);
    }
}

thread_local! {
    static DEFERRED_STORE_WAL_ACTIONS: RefCell<Option<Vec<StoreWalAction>>> =
        const { RefCell::new(None) };
}

fn begin_deferred_store_wal_capture() {
    DEFERRED_STORE_WAL_ACTIONS.with(|cell| {
        let mut guard = cell.borrow_mut();
        debug_assert!(guard.is_none());
        *guard = Some(Vec::new());
    });
}

fn capture_deferred_store_wal_actions(actions: Vec<StoreWalAction>) -> bool {
    DEFERRED_STORE_WAL_ACTIONS.with(|cell| {
        let mut guard = cell.borrow_mut();
        if let Some(pending) = guard.as_mut() {
            pending.extend(actions);
            true
        } else {
            false
        }
    })
}

fn deferred_store_wal_capture_active() -> bool {
    DEFERRED_STORE_WAL_ACTIONS.with(|cell| cell.borrow().is_some())
}

fn take_deferred_store_wal_capture() -> DeferredStoreWalActions {
    DEFERRED_STORE_WAL_ACTIONS.with(|cell| DeferredStoreWalActions {
        actions: cell.borrow_mut().take().unwrap_or_default(),
    })
}

#[derive(Debug)]
struct CommitState {
    durable_lsn: u64,
    pending_target_lsn: u64,
    pending_statements: usize,
    pending_wal_bytes: u64,
    first_pending_at: Option<Instant>,
    shutdown: bool,
    last_error: Option<String>,
}

impl CommitState {
    fn new(initial_durable_lsn: u64) -> Self {
        Self {
            durable_lsn: initial_durable_lsn,
            pending_target_lsn: initial_durable_lsn,
            pending_statements: 0,
            pending_wal_bytes: 0,
            first_pending_at: None,
            shutdown: false,
            last_error: None,
        }
    }
}

/// Lock-free append queue sitting in front of `WalWriter`.
///
/// Writers atomically reserve a byte range via `next_lsn.fetch_add`
/// and push their encoded bytes into a parking_lot-guarded vector.
/// The group-commit coordinator is the sole drainer: it sorts the
/// pending entries by LSN (so the file bytes land at the offsets
/// each writer reserved) and hands them to `WalWriter::append_bytes`.
///
/// This replaces the old `wal.lock() ... append ... append ... drop`
/// hot path where 16 concurrent writers serialised on the WAL
/// mutex for ~13µs each. Hold time on the queue's parking_lot
/// mutex is ~200ns (just a Vec::push) — 65× shorter, so the mutex
/// convoy on concurrent inserts disappears.
pub(crate) struct WalAppendQueue {
    /// Keep reserved offsets valid until their callers observe durability.
    /// Reset also excludes an old flusher publishing state into the new WAL.
    epoch: parking_lot::RwLock<()>,
    /// Tuple of (monotonically-increasing LSN, pending-bytes vec)
    /// protected by one mutex. Keeping the LSN reservation AND the
    /// push under the same lock guarantees that every queue entry
    /// is visible the moment its LSN is assigned — no gap between
    /// `fetch_add` and `push` for the leader to spin on.
    ///
    /// Earlier versions reserved LSN with an `AtomicU64::fetch_add`
    /// outside the lock; the leader drain observed reordered
    /// `(lsn, bytes)` tuples whose pushes happened in a different
    /// order than their fetch_add, creating "holes" in the LSN
    /// sequence that the drain loop interpreted as "wait for the
    /// missing enqueuer" — under tokio scheduling pressure the
    /// missing enqueuer was preempted indefinitely and the
    /// drain loop busy-waited forever (WAL stayed at 8 bytes).
    pending: parking_lot::Mutex<WalQueueState>,
    /// Bytes of encoded records sitting in `pending.entries` (ADR 0073 §2,
    /// the `wal_buffers` pool). A relaxed atomic rather than a field inside
    /// `WalQueueState` so a reader sampling the accounting never contends
    /// with an enqueuer for the queue mutex.
    queued_bytes: AtomicU64,
}

struct WalQueueState {
    next_lsn: u64,
    entries: Vec<(u64, Vec<u8>)>,
}

impl WalAppendQueue {
    fn new(initial_lsn: u64) -> Self {
        Self {
            epoch: parking_lot::RwLock::new(()),
            pending: parking_lot::Mutex::new(WalQueueState {
                next_lsn: initial_lsn,
                entries: Vec::with_capacity(64),
            }),
            queued_bytes: AtomicU64::new(0),
        }
    }

    /// Reserve an LSN range of `bytes.len()` bytes and push onto the
    /// queue. Returns the commit LSN (end of reserved range), which
    /// the caller passes to `wait_until_durable`. LSN assignment and
    /// push happen under the same mutex — no gap for the drain loop
    /// to busy-spin on.
    fn enqueue(&self, bytes: Vec<u8>) -> u64 {
        let len = bytes.len() as u64;
        let mut state = self.pending.lock();
        let start_lsn = state.next_lsn;
        state.next_lsn = start_lsn + len;
        state.entries.push((start_lsn, bytes));
        self.queued_bytes.fetch_add(len, Ordering::Relaxed);
        start_lsn + len
    }

    /// Drain all queued entries in LSN order. Caller holds the WAL
    /// file mutex while writing the drained bytes so the on-disk
    /// layout matches the reserved LSN offsets.
    fn drain_sorted(&self) -> Vec<(u64, Vec<u8>)> {
        let mut state = self.pending.lock();
        let mut v = std::mem::take(&mut state.entries);
        let drained: u64 = v.iter().map(|(_, bytes)| bytes.len() as u64).sum();
        drop(state);
        // Saturating: the drain is the only consumer, so `queued_bytes` can
        // never sit below what this call took, but an accounting counter that
        // wraps would poison `red.stats` far past the bug that caused it.
        let _ = self
            .queued_bytes
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |queued| {
                Some(queued.saturating_sub(drained))
            });
        v.sort_by_key(|(lsn, _)| *lsn);
        v
    }

    /// Whether any entry is queued. Leader uses this to decide
    /// whether to spin once more or go back to the condvar.
    fn has_pending(&self) -> bool {
        !self.pending.lock().entries.is_empty()
    }

    /// Encoded record bytes currently buffered in memory.
    fn queued_bytes(&self) -> u64 {
        self.queued_bytes.load(Ordering::Relaxed)
    }

    /// Reset the LSN cursor and discard any queued entries. Used
    /// after `wal.truncate()` — the wal-side byte counter goes back
    /// to the header size, so the queue (which tracks LSNs in the
    /// same byte space) must follow or every subsequent enqueue
    /// returns a target the drain loop can never reach.
    fn reset(&self, next_lsn: u64) {
        let mut state = self.pending.lock();
        state.next_lsn = next_lsn;
        state.entries.clear();
        drop(state);
        self.queued_bytes.store(0, Ordering::Relaxed);
    }
}

pub(crate) struct StoreCommitCoordinator {
    mode: DurabilityMode,
    config: crate::api::GroupCommitOptions,
    wal_path: PathBuf,
    wal: Arc<WalMutex>,
    /// Lock-free front door for writers. Populated alongside
    /// `WalDurableGrouped` / `Async` modes so concurrent inserts
    /// never contend on `wal` for the append step. Strict mode
    /// bypasses the queue and calls `WalWriter::append` directly
    /// to preserve the one-fsync-per-commit semantic.
    queue: Arc<WalAppendQueue>,
    state: Arc<(CommitStateMutex, CommitStateCondvar)>,
    /// Number of `wal.sync()` calls issued by the group-commit
    /// drain loop. Used by tests to observe coalescing — a burst
    /// of N concurrent commits should bump this by far less than N
    /// when the adaptive window is doing its job. Strict-mode
    /// commits go through `force_sync` which also bumps this.
    fsync_count: Arc<AtomicU64>,
    wal_generation: AtomicU64,
    /// Includes recovered epochs that were never published in the catalog.
    ownership_last_epoch: AtomicU64,
    // 0: existing WAL not scanned; 1: no ownership records; 2: protected.
    ownership_wal_state: AtomicU8,
}

/// Connects a fenced pager ownership epoch to the store's durable WAL.
#[allow(dead_code)] // Installed once recovery and format fencing are active.
struct StoreOwnershipRecorder {
    coordinator: Arc<StoreCommitCoordinator>,
    epoch: u64,
    images: parking_lot::Mutex<CheckpointPageImages>,
}

#[derive(Default)]
struct CheckpointPageImages {
    reserved: roaring::RoaringBitmap,
    captured: roaring::RoaringBitmap,
}

impl StoreOwnershipRecorder {
    #[allow(dead_code)]
    fn begin(coordinator: Arc<StoreCommitCoordinator>, epoch: u64) -> io::Result<Self> {
        coordinator.append_durable_page_ownership(
            reddb_file::page_ownership_journal::PageOwnershipRecord::Begin { epoch },
        )?;
        Ok(Self {
            coordinator,
            epoch,
            images: parking_lot::Mutex::new(CheckpointPageImages::default()),
        })
    }

    fn append_pages(
        &self,
        pages: &[u32],
        make_record: impl Fn(u64, u32) -> reddb_file::page_ownership_journal::PageOwnershipRecord,
    ) -> io::Result<()> {
        for chunk in pages.chunks(256) {
            let records: Vec<_> = chunk
                .iter()
                .map(|&id| make_record(self.epoch, id))
                .collect();
            self.coordinator
                .append_durable_page_ownership_batch(&records)?;
        }
        Ok(())
    }
}

impl crate::storage::engine::pager::PageOwnershipRecorder for StoreOwnershipRecorder {
    fn needs_page_image(&self, page_id: u32) -> io::Result<bool> {
        // The manifest is the commit boundary, never an undo target. Its
        // private overflow replacement is already covered by reservations.
        if page_id < reddb_engine::storage::pager_io::FIRST_ALLOCATABLE_PAGE {
            return Ok(false);
        }
        let images = self.images.lock();
        Ok(!images.reserved.contains(page_id) && !images.captured.contains(page_id))
    }

    fn capture_page_image(&self, page: &crate::storage::engine::Page) -> io::Result<()> {
        let mut images = self.images.lock();
        let page_id = page.page_id();
        if images.reserved.contains(page_id) || images.captured.contains(page_id) {
            return Ok(());
        }
        let mut bytes = Vec::new();
        reddb_file::encode_main_wal_record_frame_into(
            reddb_file::MainWalRecordFrameRef::FullPageImage {
                tx_id: 0,
                page_id,
                ckpt_epoch: self.epoch,
                data: page.as_bytes(),
            },
            crate::storage::wal::record::WAL_DEFAULT_TERM,
            &mut bytes,
        )?;
        self.coordinator.append_durable_ownership_bytes(bytes)?;
        images.captured.insert(page_id);
        Ok(())
    }

    fn reserve_pages(&self, start: u32, count: u32) -> io::Result<()> {
        use reddb_file::page_ownership_journal::PageOwnershipRecord;
        let end = start.checked_add(count).ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidInput, "ownership range overflow")
        })?;
        let mut next = start;
        while next < end {
            let chunk_end = next.saturating_add(256).min(end);
            let records: Vec<_> = (next..chunk_end)
                .map(|page_id| PageOwnershipRecord::Reserve {
                    epoch: self.epoch,
                    page_id,
                })
                .collect();
            self.coordinator
                .append_durable_page_ownership_batch(&records)?;
            self.images.lock().reserved.insert_range(next..chunk_end);
            next = chunk_end;
        }
        Ok(())
    }

    fn detach_free_pages(&self, pages: &[u32]) -> io::Result<()> {
        self.append_pages(pages, |epoch, page_id| {
            reddb_file::page_ownership_journal::PageOwnershipRecord::DetachFree { epoch, page_id }
        })
    }

    fn retire_pages(&self, pages: &[u32]) -> io::Result<()> {
        self.append_pages(pages, |epoch, page_id| {
            reddb_file::page_ownership_journal::PageOwnershipRecord::Retire { epoch, page_id }
        })
    }
}

impl StoreCommitCoordinator {
    pub(crate) fn open(
        wal_path: impl Into<PathBuf>,
        mode: DurabilityMode,
        config: crate::api::GroupCommitOptions,
    ) -> io::Result<Self> {
        let wal_path = wal_path.into();
        let wal = WalWriter::open(&wal_path)?;
        let initial_durable_lsn = wal.durable_lsn();
        let initial_current_lsn = wal.current_lsn();
        let wal = Arc::new(WalMutex::new(wal));
        let queue = Arc::new(WalAppendQueue::new(initial_current_lsn));
        let state = Arc::new((
            CommitStateMutex::new(CommitState::new(initial_durable_lsn)),
            CommitStateCondvar::new(),
        ));
        let fsync_count = Arc::new(AtomicU64::new(0));

        if matches!(
            mode,
            DurabilityMode::WalDurableGrouped | DurabilityMode::Async
        ) {
            let wal_bg = Arc::clone(&wal);
            let queue_bg = Arc::clone(&queue);
            let state_bg = Arc::clone(&state);
            let fsync_bg = Arc::clone(&fsync_count);
            // P1: adaptive group-commit window. Historical default is
            // `window_ms = 0` ("no wait"), which under single-writer
            // OLTP means one fsync per autocommit row — the throughput
            // floor. When window_ms is 0 we fall back to a small
            // microsecond floor (`DEFAULT_ADAPTIVE_WINDOW_US`, override
            // via `REDDB_GROUP_COMMIT_WINDOW_US`) so a pipelined writer
            // can drop a second statement into the same drain cycle.
            // Explicit non-zero `window_ms` config takes precedence.
            //
            // The loop already short-circuits on
            // `pending_statements >= max_statements` /
            // `pending_wal_bytes >= max_wal_bytes`, so the window only
            // delays the leader when the queue is otherwise empty —
            // exactly the case we want to coalesce.
            let window = Self::resolve_window(&config);
            let max_statements = config.max_statements.max(1);
            let max_wal_bytes = config.max_wal_bytes.max(1);
            std::thread::spawn(move || {
                Self::run_group_commit_loop(
                    wal_bg,
                    queue_bg,
                    state_bg,
                    fsync_bg,
                    window,
                    max_statements,
                    max_wal_bytes,
                );
            });
        }

        Ok(Self {
            mode,
            config,
            wal_path,
            wal,
            queue,
            state,
            fsync_count,
            wal_generation: AtomicU64::new(0),
            ownership_last_epoch: AtomicU64::new(0),
            ownership_wal_state: AtomicU8::new(0),
        })
    }

    /// Resolve the effective group-commit window from the configured
    /// options + the `REDDB_GROUP_COMMIT_WINDOW_US` env override.
    ///
    /// Precedence (highest first):
    ///   1. `REDDB_GROUP_COMMIT_WINDOW_US=N` — N µs (0 disables).
    ///   2. `config.window_ms != 0` — explicit ms config wins.
    ///   3. `DEFAULT_ADAPTIVE_WINDOW_US` — adaptive floor for the
    ///      historical zero-default. Set the env var to 0 to opt out.
    fn resolve_window(config: &crate::api::GroupCommitOptions) -> Duration {
        if let Ok(raw) = std::env::var("REDDB_GROUP_COMMIT_WINDOW_US") {
            if let Ok(parsed) = raw.parse::<u64>() {
                return Duration::from_micros(parsed);
            }
        }
        if config.window_ms != 0 {
            return Duration::from_millis(config.window_ms);
        }
        Duration::from_micros(DEFAULT_ADAPTIVE_WINDOW_US)
    }

    /// Total `wal.sync()` calls issued since this coordinator opened.
    /// Public for tests that want to observe fsync coalescing under
    /// concurrent autocommits.
    #[cfg(test)]
    pub(crate) fn fsync_count(&self) -> u64 {
        self.fsync_count.load(Ordering::Relaxed)
    }

    /// WAL buffer memory held right now: encoded records waiting in the
    /// group-commit queue plus the writer's fixed append buffer, which exists
    /// for the coordinator's whole life (ADR 0073 §2, the `wal_buffers` pool).
    pub(crate) fn buffered_bytes(&self) -> u64 {
        self.queue.queued_bytes() + crate::storage::wal::WAL_BUFFER_BYTES as u64
    }

    pub(crate) fn append_actions(&self, actions: &[StoreWalAction]) -> io::Result<()> {
        self.append_actions_impl(actions, false).map(|_| ())
    }

    pub(super) fn append_checkpoint_actions(
        &self,
        actions: &[StoreWalAction],
    ) -> io::Result<super::checkpoint_wal::CheckpointWalBatch> {
        self.append_actions_impl(actions, true)
    }

    fn append_actions_impl(
        &self,
        actions: &[StoreWalAction],
        retain: bool,
    ) -> io::Result<super::checkpoint_wal::CheckpointWalBatch> {
        use super::checkpoint_wal::CheckpointWalBatch;
        if actions.is_empty() {
            return Ok(CheckpointWalBatch::default());
        }
        let _epoch = self.queue.epoch.read();
        let tx_id = NEXT_STORE_TX_ID.fetch_add(1, Ordering::SeqCst);
        let encoded: Vec<Vec<u8>> = actions.iter().map(StoreWalAction::encode).collect();
        let wal_bytes = encoded.iter().fold(0u64, |total, action| {
            total.saturating_add(action.len() as u64)
        });
        let (blob, mut references) = if retain {
            CheckpointWalBatch::encode(
                tx_id,
                &encoded,
                self.wal_generation.load(Ordering::Acquire),
            )?
        } else {
            (
                WalRecord::TxCommitBatch {
                    tx_id,
                    actions: encoded,
                }
                .encode(),
                CheckpointWalBatch::default(),
            )
        };
        let start_lsn = if matches!(self.mode, DurabilityMode::Strict) {
            let start = self.wal.lock().append_bytes(&blob)?;
            self.force_sync()?;
            start
        } else {
            let length = blob.len() as u64;
            let end = self.queue.enqueue(blob);
            self.wait_until_durable(end, wal_bytes)?;
            end.checked_sub(length).ok_or_else(|| {
                io::Error::new(io::ErrorKind::InvalidData, "invalid WAL batch offset")
            })?
        };
        references.relocate(start_lsn)?;
        Ok(references)
    }

    /// Compare exact durable bytes in bounded chunks. A proof from an older
    /// truncated WAL generation cannot prove a row in the current file.
    pub(super) fn checkpoint_row_matches(
        &self,
        reference: super::checkpoint_wal::WalRowReference,
        expected: &[u8],
    ) -> io::Result<bool> {
        if expected.len() != reference.length as usize {
            return Ok(false);
        }
        let mut position = 0;
        let mut matches = true;
        let present = self.visit_checkpoint_row(reference, |chunk| {
            matches &= chunk == &expected[position..position + chunk.len()];
            position += chunk.len();
        })?;
        Ok(present && matches)
    }

    /// Materialize only one already-validated durable row for a projection batch.
    pub(super) fn checkpoint_row_bytes(
        &self,
        reference: super::checkpoint_wal::WalRowReference,
    ) -> io::Result<Vec<u8>> {
        let mut bytes = Vec::new();
        let present = self.visit_checkpoint_row(reference, |chunk| {
            if bytes.is_empty() {
                bytes.reserve_exact(reference.length as usize);
            }
            bytes.extend_from_slice(chunk);
        })?;
        if !present {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "checkpoint WAL reference changed during projection",
            ));
        }
        Ok(bytes)
    }

    fn visit_checkpoint_row(
        &self,
        reference: super::checkpoint_wal::WalRowReference,
        mut visit: impl FnMut(&[u8]),
    ) -> io::Result<bool> {
        use std::io::{Read, Seek, SeekFrom};
        let _epoch = self.queue.epoch.read_recursive();
        if reference.generation != self.wal_generation.load(Ordering::Acquire) {
            return Ok(false);
        }
        let end = reference
            .offset
            .checked_add(u64::from(reference.length))
            .ok_or_else(|| {
                io::Error::new(io::ErrorKind::InvalidData, "WAL proof offset overflow")
            })?;
        let (state_lock, _) = &*self.state;
        if end > state_lock.lock().durable_lsn {
            return Ok(false);
        }
        let mut file = std::fs::File::open(&self.wal_path)?;
        file.seek(SeekFrom::Start(reference.offset))?;
        let mut buffer = [0u8; 16 * 1024];
        let mut checksum = crc32fast::Hasher::new();
        let mut remaining = reference.length as usize;
        while remaining != 0 {
            let length = remaining.min(buffer.len());
            let chunk = &mut buffer[..length];
            file.read_exact(chunk)?;
            checksum.update(chunk);
            visit(chunk);
            remaining -= length;
        }
        if checksum.finalize() != reference.checksum {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "checkpoint WAL proof checksum mismatch",
            ));
        }
        Ok(true)
    }

    /// Encode + enqueue a single, non-batched [`WalRecord`] (issue
    /// #693). Honours the same Strict / Grouped / Async branching as
    /// [`Self::append_actions`] so durability semantics are uniform
    /// across record kinds.
    pub(crate) fn append_single_record(&self, record: WalRecord) -> io::Result<()> {
        if matches!(record, WalRecord::PageOwnership { .. }) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "page ownership requires a durable append",
            ));
        }
        let _epoch = self.queue.epoch.read();
        let blob = record.encode();
        let wal_bytes = blob.len() as u64;
        if matches!(self.mode, DurabilityMode::Strict) {
            {
                let mut wal = self.wal.lock();
                wal.append(&record)?;
            }
            self.force_sync()?;
            return Ok(());
        }
        let commit_lsn = self.queue.enqueue(blob);
        self.wait_until_durable(commit_lsn, wal_bytes)?;
        Ok(())
    }

    /// Ownership intents must reach stable storage before the pager consumes
    /// or changes a page, even when user transactions use async durability.
    #[allow(dead_code)] // Activated with the pager recovery and format fence.
    pub(super) fn append_durable_page_ownership(
        &self,
        record: reddb_file::page_ownership_journal::PageOwnershipRecord,
    ) -> io::Result<()> {
        self.append_durable_page_ownership_batch(&[record])
    }

    /// Append a bounded set of ownership intents behind one durability fence.
    #[allow(dead_code)] // Activated with the pager recovery and format fence.
    pub(super) fn append_durable_page_ownership_batch(
        &self,
        records: &[reddb_file::page_ownership_journal::PageOwnershipRecord],
    ) -> io::Result<()> {
        if records.is_empty() {
            return Ok(());
        }
        let mut bytes = Vec::new();
        for &record in records {
            // Validate before WalRecord::encode, which expects a valid frame.
            reddb_file::page_ownership_journal::encode(record)?;
            WalRecord::PageOwnership { record }.encode_into(&mut bytes);
        }
        for record in records {
            self.ownership_last_epoch
                .fetch_max(record.epoch(), Ordering::AcqRel);
        }
        self.append_durable_ownership_bytes(bytes)
    }

    fn append_durable_ownership_bytes(&self, bytes: Vec<u8>) -> io::Result<()> {
        let _epoch = self.queue.epoch.read();
        // Set before writing: a failed or uncertain append must never allow
        // checkpoint truncation to discard the ownership journal.
        self.ownership_wal_state.store(2, Ordering::Release);
        if matches!(self.mode, DurabilityMode::Strict) {
            self.wal.lock().append_bytes(&bytes)?;
            return self.force_sync();
        }

        let length = bytes.len() as u64;
        let target = self.queue.enqueue(bytes);
        let (state_lock, cond) = &*self.state;
        let mut state = state_lock.lock();
        state.pending_target_lsn = state.pending_target_lsn.max(target);
        state.pending_statements = state.pending_statements.saturating_add(1);
        state.pending_wal_bytes = state.pending_wal_bytes.saturating_add(length);
        state.first_pending_at.get_or_insert_with(Instant::now);
        cond.notify_all();
        loop {
            if let Some(error) = state.last_error.clone() {
                return Err(io::Error::other(error));
            }
            if state.durable_lsn >= target {
                return Ok(());
            }
            cond.wait(&mut state);
        }
    }

    pub(crate) fn force_sync(&self) -> io::Result<()> {
        // Strict append already owns a read lease. Do not wait behind a
        // checkpoint that needs that append to finish before resetting.
        let _epoch = self.queue.epoch.read_recursive();
        {
            let mut wal = self.wal.lock();
            wal.sync()?;
            self.fsync_count.fetch_add(1, Ordering::Relaxed);
            let durable = wal.durable_lsn();
            drop(wal);
            let (state_lock, cond) = &*self.state;
            let mut state = state_lock.lock();
            state.durable_lsn = durable;
            state.pending_target_lsn = durable.max(state.pending_target_lsn);
            state.pending_statements = 0;
            state.pending_wal_bytes = 0;
            state.first_pending_at = None;
            state.last_error = None;
            cond.notify_all();
        }
        Ok(())
    }

    pub(crate) fn truncate(&self) -> io::Result<()> {
        let _epoch = self.queue.epoch.write();
        if self.ownership_wal_state.load(Ordering::Acquire) == 0 {
            let reader = WalReader::open(&self.wal_path)?;
            let mut protected = false;
            for item in reader.iter() {
                match item {
                    Ok((_, WalRecord::PageOwnership { .. })) => {
                        protected = true;
                        break;
                    }
                    Ok(_) => {}
                    Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => break,
                    Err(error) => return Err(error),
                }
            }
            self.ownership_wal_state
                .store(if protected { 2 } else { 1 }, Ordering::Release);
        }
        if self.ownership_wal_state.load(Ordering::Acquire) == 2 {
            // A Finish record alone cannot prove that both superblock copies
            // are fenced; only recovery may eventually authorize truncation.
            return Err(io::Error::other(
                "cannot truncate WAL while page ownership records require recovery",
            ));
        }
        self.wal_generation.fetch_add(1, Ordering::AcqRel);
        let mut wal = self.wal.lock();
        wal.truncate()?;
        let durable = wal.durable_lsn();
        let current = wal.current_lsn();
        drop(wal);

        // Queue's next_lsn tracks byte offsets in the same space as
        // wal.current_lsn. After truncate both must be reset together
        // — otherwise enqueue returns a target_lsn in the old range
        // that drain can never reach, and wait_until_durable hangs.
        self.queue.reset(current);

        let (state_lock, cond) = &*self.state;
        let mut state = state_lock.lock();
        state.durable_lsn = durable;
        state.pending_target_lsn = durable;
        state.pending_statements = 0;
        state.pending_wal_bytes = 0;
        state.first_pending_at = None;
        state.last_error = None;
        cond.notify_all();
        Ok(())
    }

    fn release_completed_ownership_wal(&self) -> io::Result<()> {
        use reddb_file::page_ownership_journal::{encode, pending_epoch, PageOwnershipRecord};
        let _epoch = self.queue.epoch.write();
        let mut journal = Vec::new();
        let mut last = None;
        for item in WalReader::open(&self.wal_path)?.iter() {
            match item {
                Ok((_, WalRecord::PageOwnership { record })) => {
                    journal.extend_from_slice(&encode(record)?);
                    last = Some(record);
                }
                Ok(_) => {}
                Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "incomplete WAL before ownership completion",
                    ));
                }
                Err(error) => return Err(error),
            }
        }
        if pending_epoch(&journal)?.is_some()
            || !matches!(last, Some(PageOwnershipRecord::Finish { .. }))
        {
            return Err(io::Error::other("page ownership WAL is not complete"));
        }
        self.ownership_wal_state.store(1, Ordering::Release);
        Ok(())
    }

    fn recover_page_ownership(&self, store: &UnifiedStore, journal: &[u8]) -> io::Result<()> {
        use reddb_file::page_ownership_journal::{
            pending_epoch, recovery_plan, PageOwnershipRecord,
        };
        if journal.is_empty() {
            return Ok(());
        }
        let pager = store
            .pager
            .as_ref()
            .ok_or_else(|| io::Error::other("ownership WAL requires a pager"))?;
        let header = pager
            .header()
            .map_err(|error| io::Error::other(error.to_string()))?;
        if header.version < reddb_file::file_format::PAGE_OWNERSHIP_FILE_VERSION {
            return Err(io::Error::other(
                "page ownership journal requires recovery with a fenced file version",
            ));
        }
        let pending = pending_epoch(journal).map_err(|error| {
            io::Error::other(format!("page ownership journal requires recovery: {error}"))
        })?;
        if let Some(mut pending) = pending {
            let catalog_epoch = store.ownership_catalog_epoch.load(Ordering::Acquire);
            let (_, retained_pages) = pager
                .deferred_reclamation_pages()
                .map_err(|error| io::Error::other(error.to_string()))?;
            // A retirement intent precedes the catalog's deferred-free ledger.
            // Publication does not release pages that this ledger still owns,
            // including pages allocated and retired within the same epoch.
            if catalog_epoch == pending.epoch {
                for page in &retained_pages {
                    pending.retired_pages.remove(page);
                }
            }
            let mut plan = recovery_plan(
                &pending,
                catalog_epoch,
                reddb_engine::storage::pager_io::FIRST_ALLOCATABLE_PAGE,
                header.page_count,
            )?;
            if retained_pages
                .iter()
                .any(|page| plan.release.binary_search(page).is_ok())
            {
                return Err(io::Error::other(
                    "ownership recovery would release a catalog-retained page",
                ));
            }
            plan.preserve.extend(retained_pages);
            plan.preserve.sort_unstable();
            plan.preserve.dedup();
            pager
                .validate_owned_pages(&plan.preserve)
                .map_err(|error| io::Error::other(error.to_string()))?;
            pager
                .recover_unowned_pages(&plan.release)
                .map_err(|error| io::Error::other(error.to_string()))?;
            #[cfg(test)]
            if std::env::var_os("REDDB_EXPERIMENTAL_EXIT_DURING_OWNERSHIP_RECOVERY").is_some() {
                std::process::exit(187);
            }
            pager
                .sync_recovered_allocator()
                .map_err(|error| io::Error::other(error.to_string()))?;
            #[cfg(test)]
            if std::env::var_os("REDDB_EXPERIMENTAL_EXIT_AFTER_OWNERSHIP_SYNC").is_some() {
                std::process::exit(188);
            }
            self.append_durable_page_ownership(PageOwnershipRecord::Finish {
                epoch: pending.epoch,
            })?;
            #[cfg(test)]
            if std::env::var_os("REDDB_EXPERIMENTAL_EXIT_AFTER_OWNERSHIP_FINISH").is_some() {
                std::process::exit(189);
            }
        }
        self.release_completed_ownership_wal()
    }

    pub(super) fn next_page_ownership_epoch(&self, store: &UnifiedStore) -> io::Result<u64> {
        self.ownership_last_epoch
            .load(Ordering::Acquire)
            .max(store.ownership_catalog_epoch.load(Ordering::Acquire))
            .checked_add(1)
            .ok_or_else(|| io::Error::other("page ownership epoch exhausted"))
    }

    /// Begin an epoch only when the caller has excluded all other pager mutators.
    pub(super) fn begin_page_ownership_epoch(
        &self,
        store: &UnifiedStore,
        epoch: u64,
    ) -> io::Result<()> {
        if store.ownership_active_epoch.load(Ordering::Acquire) != 0
            || epoch <= store.ownership_catalog_epoch.load(Ordering::Acquire)
            || epoch <= self.ownership_last_epoch.load(Ordering::Acquire)
        {
            return Err(io::Error::other("invalid page ownership epoch"));
        }
        let pager = store
            .pager
            .as_ref()
            .ok_or_else(|| io::Error::other("ownership epoch requires a pager"))?;
        pager
            .fence_file_format_version(reddb_file::file_format::PAGE_CHECKPOINT_UNDO_FILE_VERSION)
            .map_err(|error| io::Error::other(error.to_string()))?;
        let coordinator = Arc::clone(
            store
                .commit
                .as_ref()
                .ok_or_else(|| io::Error::other("ownership epoch requires a WAL coordinator"))?,
        );
        let recorder = Arc::new(StoreOwnershipRecorder::begin(coordinator, epoch)?);
        pager.install_ownership_recorder(recorder)?;
        store.ownership_active_epoch.store(epoch, Ordering::Release);
        Ok(())
    }

    /// Complete the epoch only while the same exclusion remains in force.
    pub(super) fn finish_page_ownership_epoch(
        &self,
        store: &UnifiedStore,
        epoch: u64,
    ) -> io::Result<()> {
        if epoch == 0 || store.ownership_active_epoch.load(Ordering::Acquire) != epoch {
            return Err(io::Error::other("page ownership epoch is not active"));
        }
        if store.ownership_catalog_epoch.load(Ordering::Acquire) != epoch {
            return Err(io::Error::other("page ownership epoch is not published"));
        }
        let pager = store
            .pager
            .as_ref()
            .ok_or_else(|| io::Error::other("ownership epoch requires a pager"))?;
        pager
            .sync_recovered_allocator()
            .map_err(|error| io::Error::other(error.to_string()))?;
        self.append_durable_page_ownership(
            reddb_file::page_ownership_journal::PageOwnershipRecord::Finish { epoch },
        )?;
        pager.clear_ownership_recorder()?;
        store.ownership_active_epoch.store(0, Ordering::Release);
        self.release_completed_ownership_wal()
    }

    pub(crate) fn replay_into(&self, store: &UnifiedStore) -> io::Result<()> {
        if !self.wal_path.exists() {
            return Ok(());
        }

        let reader = match WalReader::open(&self.wal_path) {
            Ok(reader) => reader,
            Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(()),
            Err(err) => return Err(err),
        };

        let mut tx_states = std::collections::HashMap::<u64, bool>::new();
        let mut pending = Vec::<(u64, Vec<u8>)>::new();
        let mut ownership_journal = Vec::new();
        let mut latest_ownership_epoch = 0;
        let mut incomplete_wal_tail = false;
        let mut applied = false;

        for record in reader.iter() {
            let (_lsn, record) = match record {
                Ok(record) => record,
                Err(err) if err.kind() == io::ErrorKind::UnexpectedEof => {
                    incomplete_wal_tail = true;
                    break;
                }
                Err(err) => return Err(err),
            };
            match record {
                WalRecord::PageOwnership { record } => {
                    latest_ownership_epoch = latest_ownership_epoch.max(record.epoch());
                    self.ownership_last_epoch
                        .fetch_max(record.epoch(), Ordering::AcqRel);
                    ownership_journal
                        .extend_from_slice(&reddb_file::page_ownership_journal::encode(record)?);
                }
                WalRecord::TxCommitBatch { actions, .. } => {
                    for payload in actions {
                        let action = StoreWalAction::decode(&payload)?;
                        applied = true;
                        store.apply_replayed_action(&action, false).map_err(|err| {
                            io::Error::other(format!("failed to replay store wal action: {err}"))
                        })?;
                        // Retain only the existing bounded exact-row proof.
                        // These images came from a complete committed WAL batch;
                        // replay need not rebuild a whole tree for a sparse edit.
                        store.remember_checkpoint_rows(vec![action], Default::default());
                    }
                }
                WalRecord::Begin { tx_id } => {
                    tx_states.insert(tx_id, false);
                }
                WalRecord::Commit { tx_id } => {
                    tx_states.insert(tx_id, true);
                }
                WalRecord::Rollback { tx_id } => {
                    tx_states.remove(&tx_id);
                }
                WalRecord::PageWrite {
                    tx_id,
                    page_id: _,
                    data,
                } => pending.push((tx_id, data)),
                WalRecord::Checkpoint { .. } => {}
                WalRecord::VectorInsert {
                    collection,
                    entity_id,
                    vector,
                } => {
                    // Issue #694 — capture `vector.turbo` WAL inserts so the
                    // boot-time TurboQuant index rebuild can drain them in
                    // WAL order (deterministic against the pre-restart
                    // state under a fixed codec seed). The legacy `vector`
                    // path doesn't read this map.
                    let mut map = store.replayed_turbo_inserts.lock();
                    map.entry(collection).or_default().push((entity_id, vector));
                }
                WalRecord::ProbabilisticDelta {
                    kind,
                    operation,
                    name,
                    operands,
                } => {
                    let mut deltas = store.replayed_probabilistic_deltas.lock();
                    deltas.push((kind, operation, name, operands));
                }
                WalRecord::FullPageImage { .. } => {
                    // Pager-level FPI (gh-478); store replay ignores.
                }
            }
        }

        if incomplete_wal_tail && !ownership_journal.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "incomplete WAL prevents ownership recovery",
            ));
        }
        self.recover_page_ownership(store, &ownership_journal)?;

        for (tx_id, payload) in pending {
            if !tx_states.get(&tx_id).copied().unwrap_or(false) {
                continue;
            }
            let action = StoreWalAction::decode(&payload)?;
            applied = true;
            store.apply_replayed_action(&action, false).map_err(|err| {
                io::Error::other(format!("failed to replay store wal action: {err}"))
            })?;
            store.remember_checkpoint_rows(vec![action], Default::default());
        }

        if applied {
            // Opening is exclusive: no client can mutate published trees
            // while replay constructs replacement generations.
            let epoch = latest_ownership_epoch
                .max(store.ownership_catalog_epoch.load(Ordering::Acquire))
                .checked_add(1)
                .ok_or_else(|| io::Error::other("page ownership epoch exhausted"))?;
            self.begin_page_ownership_epoch(store, epoch)?;
            // Even when the replayed actions leave all B-trees unchanged, the
            // epoch must be in the catalog before its journal can finish.
            store.mark_paged_registry_dirty();
            store
                .reconcile_replayed_btrees()
                .map_err(|err| io::Error::other(err.to_string()))?;
            self.finish_page_ownership_epoch(store, epoch)?;
        }
        Ok(())
    }

    fn wait_until_durable(&self, target_lsn: u64, wal_bytes: u64) -> io::Result<()> {
        match self.mode {
            DurabilityMode::Strict => self.force_sync(),
            // Async: record the pending target so the background
            // flusher eventually covers it, but don't block the
            // caller. Matches PG `synchronous_commit=off` semantics —
            // crash inside the flush window loses unflushed commits.
            DurabilityMode::Async => {
                let (state_lock, cond) = &*self.state;
                let mut state = state_lock.lock();
                state.pending_target_lsn = state.pending_target_lsn.max(target_lsn);
                state.pending_statements = state.pending_statements.saturating_add(1);
                state.pending_wal_bytes = state.pending_wal_bytes.saturating_add(wal_bytes);
                state.first_pending_at.get_or_insert_with(Instant::now);
                cond.notify_all();
                Ok(())
            }
            DurabilityMode::WalDurableGrouped => {
                let (state_lock, cond) = &*self.state;
                let mut state = state_lock.lock();
                state.pending_target_lsn = state.pending_target_lsn.max(target_lsn);
                state.pending_statements = state.pending_statements.saturating_add(1);
                state.pending_wal_bytes = state.pending_wal_bytes.saturating_add(wal_bytes);
                state.first_pending_at.get_or_insert_with(Instant::now);
                cond.notify_all();

                loop {
                    if let Some(err) = state.last_error.clone() {
                        return Err(io::Error::other(err));
                    }
                    if state.durable_lsn >= target_lsn {
                        return Ok(());
                    }
                    // parking_lot::Condvar mutates the guard in place —
                    // no LockResult to unwrap, no poisoning to fold.
                    cond.wait(&mut state);
                }
            }
        }
    }

    fn run_group_commit_loop(
        wal: Arc<WalMutex>,
        queue: Arc<WalAppendQueue>,
        state: Arc<(CommitStateMutex, CommitStateCondvar)>,
        fsync_count: Arc<AtomicU64>,
        window: Duration,
        max_statements: usize,
        max_wal_bytes: u64,
    ) {
        let (state_lock, cond) = &*state;
        loop {
            let target_lsn = {
                let mut guard = state_lock.lock();

                while !guard.shutdown && guard.pending_target_lsn <= guard.durable_lsn {
                    cond.wait(&mut guard);
                }

                if guard.shutdown {
                    return;
                }

                let immediate = window.is_zero()
                    || guard.pending_statements >= max_statements
                    || guard.pending_wal_bytes >= max_wal_bytes;

                if !immediate {
                    let deadline = guard.first_pending_at.unwrap_or_else(Instant::now) + window;
                    let now = Instant::now();
                    if now < deadline {
                        let timeout = deadline.saturating_duration_since(now);
                        let _ = cond.wait_for(&mut guard, timeout);
                        if guard.shutdown {
                            return;
                        }
                        if guard.pending_target_lsn <= guard.durable_lsn {
                            continue;
                        }
                        let should_wait_again = guard.pending_statements < max_statements
                            && guard.pending_wal_bytes < max_wal_bytes
                            && guard
                                .first_pending_at
                                .map(|first| first.elapsed() < window)
                                .unwrap_or(false);
                        if should_wait_again {
                            continue;
                        }
                    }
                }

                guard.pending_target_lsn
            };

            // Drain the current epoch even when reset is waiting: outstanding
            // append leases cannot finish until this flusher acknowledges them.
            // Retain the lease through state publication to exclude stale ACKs
            // after a checkpoint resets offsets back to the WAL header.
            let _epoch = queue.epoch.read_recursive();

            // Drain all queued entries. Since `WalAppendQueue::enqueue`
            // assigns LSN and pushes under a single mutex, the drained
            // tuples are guaranteed to form a contiguous byte range
            // starting at `wal.current_lsn()` — no gaps, no leftover
            // handling.
            let batches = queue.drain_sorted();

            let sync_result = {
                let mut wal = wal.lock();
                let mut write_err: Option<io::Error> = None;
                for (_lsn, bytes) in batches {
                    if let Err(e) = wal.append_bytes(&bytes) {
                        write_err = Some(e);
                        break;
                    }
                }
                match write_err {
                    Some(e) => Err(e),
                    None => wal.sync().map(|_| {
                        // Count the fsync exactly once per drain
                        // cycle so tests can compare it against the
                        // number of `append_actions` callers that
                        // entered the queue.
                        fsync_count.fetch_add(1, Ordering::Relaxed);
                        wal.durable_lsn()
                    }),
                }
            };

            // Late enqueuers that arrived after our drain stay in the
            // queue — don't clear the `pending_*` counters yet and
            // don't claim we reached `target_lsn` unless the fsync
            // actually covers it.
            let more_pending = queue.has_pending();

            let mut guard = state_lock.lock();
            match sync_result {
                Ok(durable_lsn) => {
                    guard.durable_lsn = durable_lsn;
                    if !more_pending {
                        guard.pending_statements = 0;
                        guard.pending_wal_bytes = 0;
                        guard.first_pending_at = None;
                    }
                    guard.last_error = None;
                    let _ = target_lsn;
                }
                Err(err) => {
                    guard.last_error = Some(err.to_string());
                }
            }
            cond.notify_all();
        }
    }
}

impl Drop for StoreCommitCoordinator {
    fn drop(&mut self) {
        let (state_lock, cond) = &*self.state;
        // parking_lot::Mutex::lock is infallible (no poisoning).
        let mut state = state_lock.lock();
        state.shutdown = true;
        cond.notify_all();
    }
}

impl UnifiedStore {
    pub(crate) fn deferred_store_wal_capture_active() -> bool {
        deferred_store_wal_capture_active()
    }

    pub(crate) fn begin_deferred_store_wal_capture() {
        begin_deferred_store_wal_capture();
    }

    pub(crate) fn take_deferred_store_wal_capture() -> DeferredStoreWalActions {
        take_deferred_store_wal_capture()
    }

    pub(crate) fn append_deferred_store_wal_actions(
        &self,
        actions: DeferredStoreWalActions,
    ) -> Result<(), StoreError> {
        if actions.actions.is_empty() {
            return Ok(());
        }
        if self.config.embedded_wal_path.is_some() {
            return self.append_embedded_store_wal_actions(&actions.actions);
        }
        // Deferred transactions need a committed WAL batch even in Strict
        // mode. WAL-only versions have no pager pages to flush yet.
        if let Some(commit) = &self.commit {
            let references = commit
                .append_checkpoint_actions(&actions.actions)
                .map_err(StoreError::Io)?;
            self.remember_checkpoint_rows(actions.actions, references);
            Ok(())
        } else {
            self.flush_paged_state()
        }
    }

    /// Emit a [`WalRecord::VectorInsert`] for a `vector.turbo`
    /// collection (issue #693). Returns `Ok(())` when no commit
    /// coordinator is configured (in-memory mode) so callers don't
    /// have to special-case the missing WAL — the in-memory index
    /// update remains durable enough for in-memory runtimes by
    /// construction.
    pub(crate) fn append_vector_insert_record(
        &self,
        collection: &str,
        entity_id: u64,
        vector: &[f32],
    ) -> std::io::Result<()> {
        let Some(commit) = &self.commit else {
            return Ok(());
        };
        let record = WalRecord::VectorInsert {
            collection: collection.to_string(),
            entity_id,
            vector: vector.to_vec(),
        };
        commit.append_single_record(record)
    }

    pub(crate) fn append_probabilistic_delta_record(
        &self,
        kind: u8,
        operation: u8,
        name: &str,
        operands: Vec<Vec<u8>>,
    ) -> std::io::Result<()> {
        let Some(commit) = &self.commit else {
            return Ok(());
        };
        let record = WalRecord::ProbabilisticDelta {
            kind,
            operation,
            name: name.to_string(),
            operands,
        };
        commit.append_single_record(record)
    }

    pub(crate) fn finish_paged_write(
        &self,
        actions: impl IntoIterator<Item = StoreWalAction>,
    ) -> Result<(), StoreError> {
        self.finish_paged_write_for_projection(actions, false)
    }

    pub(crate) fn finish_paged_write_for_projection(
        &self,
        actions: impl IntoIterator<Item = StoreWalAction>,
        track: bool,
    ) -> Result<(), StoreError> {
        let actions: Vec<StoreWalAction> = actions.into_iter().collect();
        if deferred_store_wal_capture_active() {
            let captured = capture_deferred_store_wal_actions(actions);
            debug_assert!(captured);
            return Ok(());
        }
        if self.config.embedded_wal_path.is_some() {
            return self.append_embedded_store_wal_actions(&actions);
        }
        let mut references = super::checkpoint_wal::CheckpointWalBatch::default();
        let result = match self.config.durability_mode {
            DurabilityMode::Strict => {
                if let Some(commit) = &self.commit {
                    if track {
                        references = commit
                            .append_checkpoint_actions(&actions)
                            .map_err(StoreError::Io)?;
                    } else {
                        commit.append_actions(&actions).map_err(StoreError::Io)?;
                    }
                }
                self.flush_paged_state()
            }
            DurabilityMode::WalDurableGrouped | DurabilityMode::Async => {
                if let Some(commit) = &self.commit {
                    if track {
                        references = commit
                            .append_checkpoint_actions(&actions)
                            .map_err(StoreError::Io)?;
                    } else {
                        commit.append_actions(&actions).map_err(StoreError::Io)?;
                    }
                    Ok(())
                } else {
                    self.flush_paged_state()
                }
            }
        };
        result?;
        if track {
            self.remember_checkpoint_rows(actions, references);
        }
        Ok(())
    }

    pub(crate) fn apply_encoded_store_wal_action(&self, payload: &[u8]) -> Result<(), StoreError> {
        let action = StoreWalAction::decode(payload).map_err(StoreError::Io)?;
        self.apply_replayed_action(&action, true)
    }

    fn append_embedded_store_wal_actions(
        &self,
        actions: &[StoreWalAction],
    ) -> Result<(), StoreError> {
        let Some(path) = self.config.embedded_wal_path.as_deref() else {
            return Ok(());
        };
        if actions.is_empty() {
            return Ok(());
        }
        let payloads: Vec<Vec<u8>> = actions.iter().map(StoreWalAction::encode).collect();
        let encoded_len = crate::storage::EmbeddedRdbArtifact::wal_payloads_encoded_len(&payloads)
            .map_err(|err| StoreError::Io(std::io::Error::other(err.to_string())))?;
        match self.append_embedded_payloads(path, &payloads) {
            Ok(_) => Ok(()),
            Err(crate::api::RedDBError::InvalidOperation(msg))
                if msg.contains("embedded wal region full") =>
            {
                self.recover_full_embedded_wal(path, &payloads, encoded_len)
            }
            Err(err) => Err(StoreError::Io(std::io::Error::other(err.to_string()))),
        }
    }

    fn recover_full_embedded_wal(
        &self,
        path: &Path,
        payloads: &[Vec<u8>],
        encoded_len: u64,
    ) -> Result<(), StoreError> {
        let _vault_guard = self.vault_publication_lock.lock();
        // The failed append preceded this lock: another writer may already
        // have reclaimed space. Retry through the same FIFO coordinator so
        // stale full results cannot each serialize another complete snapshot.
        match self.append_embedded_payloads(path, payloads) {
            Ok(_) => return Ok(()),
            Err(crate::api::RedDBError::InvalidOperation(message))
                if message.contains("embedded wal region full") => {}
            Err(error) => return Err(StoreError::Io(std::io::Error::other(error.to_string()))),
        }
        // A checkpoint can fail after publishing its image. Preserve the
        // session failure fence through errors and unwinding, just as append's
        // FlushBatch guard does; in-memory rollback alone cannot resolve disk.
        struct CheckpointFence<'a>(Option<&'a crate::storage::embedded::EmbeddedCommitSession>);
        impl Drop for CheckpointFence<'_> {
            fn drop(&mut self) {
                if let Some(session) = self.0 {
                    session.fence_interrupted_write();
                }
            }
        }
        let mut fence = CheckpointFence(Some(
            self.embedded_commit_session
                .get()
                .expect("WAL-full recovery follows an append attempt"),
        ));
        let started = Instant::now();
        let snapshot = self.to_binary_dump_bytes();
        let capacity = encoded_len.max((snapshot.len() as u64).min(EMBEDDED_WAL_HEADROOM_LIMIT));
        let checkpoint = crate::storage::EmbeddedRdbArtifact::write_snapshot_with_wal_capacity(
            path, &snapshot, capacity,
        )
        .map_err(|err| StoreError::Io(std::io::Error::other(err.to_string())))?;
        fence.0 = None;
        tracing::debug!(
            target: "reddb::embedded_checkpoint",
            snapshot_bytes = snapshot.len(),
            wal_region_bytes = checkpoint.manifest.wal_region_bytes,
            elapsed_us = started.elapsed().as_micros() as u64,
            "embedded WAL overflow checkpoint"
        );
        self.append_embedded_payloads(path, payloads)
            .map(|_| ())
            .map_err(|err| StoreError::Io(std::io::Error::other(err.to_string())))
    }

    fn append_embedded_payloads(
        &self,
        path: &Path,
        payloads: &[Vec<u8>],
    ) -> crate::RedDBResult<crate::storage::EmbeddedRdbOpen> {
        let session = self
            .embedded_commit_session
            .get_or_init(|| crate::storage::EmbeddedRdbArtifact::group_commit_session(path));
        let options = self.config.group_commit;
        let grouped = self.config.durability_mode == DurabilityMode::WalDurableGrouped;
        let window = if grouped {
            StoreCommitCoordinator::resolve_window(&options)
        } else {
            Duration::ZERO
        };
        session.append(
            payloads,
            reddb_file::embedded::EmbeddedGroupCommitOptions {
                window,
                max_statements: if grouped { options.max_statements } else { 1 },
                max_wal_bytes: options.max_wal_bytes,
            },
        )
    }

    pub(super) fn enqueue_grouped_actions(
        &self,
        actions: &[StoreWalAction],
    ) -> Result<Option<crate::storage::embedded::PendingEmbeddedAppend>, StoreError> {
        if self.config.durability_mode != DurabilityMode::WalDurableGrouped
            || deferred_store_wal_capture_active()
        {
            return Ok(None);
        }
        let Some(path) = self.config.embedded_wal_path.as_deref() else {
            return Ok(None);
        };
        let session = self
            .embedded_commit_session
            .get_or_init(|| crate::storage::EmbeddedRdbArtifact::group_commit_session(path));
        let payloads: Vec<Vec<u8>> = actions.iter().map(StoreWalAction::encode).collect();
        let options = self.config.group_commit;
        session
            .enqueue(
                &payloads,
                reddb_file::embedded::EmbeddedGroupCommitOptions {
                    window: StoreCommitCoordinator::resolve_window(&options),
                    max_statements: options.max_statements,
                    max_wal_bytes: options.max_wal_bytes,
                },
            )
            .map(Some)
            .map_err(|error| StoreError::Io(io::Error::other(error.to_string())))
    }

    pub(super) fn complete_grouped_actions(
        &self,
        pending: &crate::storage::embedded::PendingEmbeddedAppend,
        actions: &[StoreWalAction],
    ) -> Result<(), StoreError> {
        match pending.wait() {
            Ok(_) => Ok(()),
            Err(crate::RedDBError::InvalidOperation(message))
                if message.contains("embedded wal region full") =>
            {
                self.append_embedded_store_wal_actions(actions)
            }
            Err(error) => Err(StoreError::Io(io::Error::other(error.to_string()))),
        }
    }

    pub(crate) fn apply_replayed_action(
        &self,
        action: &StoreWalAction,
        update_btrees: bool,
    ) -> Result<(), StoreError> {
        reddb_engine::storage::store_wal::apply_replayed_action(
            &NativeReplayTarget {
                store: self,
                update_btrees,
            },
            action,
        )
    }

    /// Atomic full-collection replace — issue #595 slice 9c.
    ///
    /// Builds a fresh `SegmentManager` populated with `entities`,
    /// atomically swaps it into the collections map, rebuilds the
    /// paged B-tree from the same records, and emits a single
    /// `RefreshCollection` WAL action.
    ///
    /// Concurrent readers that resolved an `Arc<SegmentManager>` for
    /// this collection before the swap continue reading the prior
    /// contents; readers after the swap see the new contents. The
    /// transition is single-pointer so partial state is unobservable.
    ///
    /// A crash between in-memory mutation and WAL fsync leaves the
    /// previous WAL stream intact — on recovery the prior
    /// `RefreshCollection` (or whatever last bulk-applied to the
    /// collection) is replayed, so the prior contents are observed.
    pub fn refresh_collection(
        &self,
        name: &str,
        entities: Vec<UnifiedEntity>,
    ) -> Result<Vec<Vec<u8>>, StoreError> {
        let fv = self.format_version();

        // Build a fresh manager. Done outside the collections lock so
        // the critical section is just the pointer swap below.
        let new_manager = Arc::new(SegmentManager::with_config(
            name,
            self.config.manager_config.clone(),
        ));

        let mut prepared = entities;
        for entity in &mut prepared {
            if entity.id.raw() == 0 {
                entity.id = self.next_entity_id();
            } else {
                self.register_entity_id(entity.id);
            }
            if let EntityKind::TableRow { ref mut row_id, .. } = entity.kind {
                if *row_id == 0 {
                    *row_id = new_manager.next_row_id();
                } else {
                    new_manager.register_row_id(*row_id);
                }
            }
            entity.ensure_table_logical_id();
        }

        let serialized: Vec<Vec<u8>> = prepared
            .iter()
            .map(|e| Self::serialize_entity_record(e, None, fv))
            .collect();

        new_manager.bulk_insert(prepared.clone()).map_err(|e| {
            StoreError::Io(std::io::Error::other(format!(
                "refresh_collection: bulk_insert into new manager failed: {e}"
            )))
        })?;

        self.swap_collection_state(name, new_manager, &prepared, &serialized, true);

        self.finish_paged_write([StoreWalAction::RefreshCollection {
            collection: name.to_string(),
            records: serialized.clone(),
        }])?;

        Ok(serialized)
    }

    /// Atomic swap of the in-memory collection state. Used by both
    /// the live `refresh_collection` path and WAL replay. The new
    /// manager is already populated; this fn replaces the live Arc,
    /// purges side-state pointing at the previous contents, and
    /// rebuilds the paged B-tree from the supplied records.
    fn swap_collection_state(
        &self,
        name: &str,
        new_manager: Arc<SegmentManager>,
        prepared: &[UnifiedEntity],
        serialized: &[Vec<u8>],
        update_btrees: bool,
    ) {
        self.resident
            .swap_collection_state(name, new_manager, prepared);

        if let Some(pager) = self.pager.as_ref().filter(|_| update_btrees) {
            let new_btree = Arc::new(BTree::new(Arc::clone(pager)));
            let mut sorted: Vec<(Vec<u8>, Vec<u8>)> = prepared
                .iter()
                .zip(serialized.iter())
                .map(|(e, r)| (e.id.raw().to_be_bytes().to_vec(), r.clone()))
                .collect();
            sorted.sort_by(|a, b| a.0.cmp(&b.0));
            if !sorted.is_empty() {
                let _ = new_btree.bulk_insert_sorted(&sorted);
            }
            self.btree_indices
                .write()
                .insert(name.to_string(), new_btree);
            self.mark_paged_registry_dirty();
        }
    }

    fn apply_replayed_refresh_collection(
        &self,
        collection: &str,
        records: &[Vec<u8>],
        update_btrees: bool,
    ) -> Result<(), StoreError> {
        let (new_manager, prepared) = self.resident.prepare_replayed_refresh_collection(
            collection,
            records,
            self.format_version(),
            &self.config.manager_config,
        )?;
        self.swap_collection_state(collection, new_manager, &prepared, records, update_btrees);
        Ok(())
    }

    /// Replica-side analogue of [`Self::refresh_collection`] — issue
    /// #596 slice 9d. Takes pre-serialized record bytes from the
    /// primary's CDC stream (the same bytes the primary wrote into the
    /// `RefreshCollection` WAL action), applies the atomic swap, and
    /// emits the same `RefreshCollection` WAL action against the
    /// replica's local store WAL so the post-swap state survives a
    /// replica restart through the normal recovery path.
    ///
    /// Idempotency: re-applying the same records bytes is a full
    /// rebuild from the same payload, so the resulting backing-
    /// collection contents are equal to the prior call's result.
    /// The primary-side `LogicalChangeApplier` short-circuits exact
    /// duplicate LSN+payload combinations before reaching here.
    pub fn refresh_collection_from_records(
        &self,
        name: &str,
        records: Vec<Vec<u8>>,
    ) -> Result<(), StoreError> {
        self.apply_replayed_refresh_collection(name, &records, true)?;
        self.finish_paged_write([StoreWalAction::RefreshCollection {
            collection: name.to_string(),
            records,
        }])?;
        Ok(())
    }

    pub(crate) fn create_collection_in_memory(&self, name: &str) -> Result<(), StoreError> {
        self.resident
            .create_collection_in_memory(name, &self.config.manager_config)
    }

    fn drop_collection_in_memory(&self, name: &str) -> Result<(), StoreError> {
        self.resident.drop_collection_in_memory(name, || {
            if let Some(tree) = self.btree_indices.write().remove(name) {
                self.retire_btree(tree);
            }
        })
    }

    fn apply_replayed_upsert(
        &self,
        collection: &str,
        record: &[u8],
        update_btrees: bool,
    ) -> Result<(), StoreError> {
        let id = self.resident.apply_replayed_upsert(
            collection,
            record,
            self.format_version(),
            &self.config.manager_config,
        )?;

        if let Some(pager) = self.pager.as_ref().filter(|_| update_btrees) {
            let mut btree_indices = self.btree_indices.write();
            let btree = btree_indices
                .entry(collection.to_string())
                .or_insert_with(|| Arc::new(BTree::new(Arc::clone(pager))));
            let root_before = btree.root_page_id();
            let key = id.raw().to_be_bytes();
            match btree.insert(&key, record) {
                Ok(_) => {}
                Err(BTreeError::DuplicateKey) => {
                    let _ = btree.delete(&key);
                    let _ = btree.insert(&key, record);
                }
                Err(err) => {
                    return Err(StoreError::Io(io::Error::other(format!(
                        "replay upsert btree error: {err}"
                    ))));
                }
            }
            if root_before != btree.root_page_id() {
                self.mark_paged_registry_dirty();
            }
        }

        Ok(())
    }

    fn apply_replayed_delete(
        &self,
        collection: &str,
        id: EntityId,
        update_btrees: bool,
    ) -> Result<(), StoreError> {
        self.resident.apply_replayed_delete(collection, id, || {
            if self.pager.is_some() && update_btrees {
                let btree_indices = self.btree_indices.read();
                if let Some(btree) = btree_indices.get(collection) {
                    let root_before = btree.root_page_id();
                    let key = id.raw().to_be_bytes();
                    let _ = btree.delete(&key);
                    if root_before != btree.root_page_id() {
                        self.mark_paged_registry_dirty();
                    }
                }
            }
        })
    }
}

#[cfg(all(test, target_os = "linux"))]
#[path = "storage_apocalypse.rs"]
mod storage_apocalypse;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::{DurabilityMode, GroupCommitOptions};
    use std::sync::{Barrier, Mutex as StdMutex, OnceLock};
    use std::time::SystemTime;

    #[test]
    fn ownership_epoch_rejects_nested_begin_and_wrong_finish() {
        let directory = tempfile::tempdir().expect("temporary store");
        let store = UnifiedStore::open(directory.path().join("data.rdb")).expect("store");
        store.create_collection("rows").expect("collection");
        store.persist().expect("baseline");
        let coordinator = store.commit.as_ref().expect("WAL coordinator");
        assert!(coordinator.begin_page_ownership_epoch(&store, 0).is_err());
        assert!(coordinator.finish_page_ownership_epoch(&store, 1).is_err());
        coordinator
            .begin_page_ownership_epoch(&store, 2)
            .expect("begin epoch");
        assert!(coordinator.begin_page_ownership_epoch(&store, 3).is_err());
        assert!(coordinator.finish_page_ownership_epoch(&store, 3).is_err());
        assert!(coordinator.finish_page_ownership_epoch(&store, 2).is_err());
        store.flush_paged_registry().expect("publish epoch");
        coordinator
            .finish_page_ownership_epoch(&store, 2)
            .expect("finish epoch");
        assert_eq!(store.ownership_active_epoch.load(Ordering::Acquire), 0);
    }

    #[test]
    fn native_pager_records_page_ownership_in_durable_wal() {
        use reddb_file::page_ownership_journal::PageOwnershipRecord;

        let directory = tempfile::tempdir().expect("temporary store");
        let path = directory.path().join("data.rdb");
        let wal_path = reddb_file::layout::unified_wal_path(&path);
        let pager = crate::storage::engine::Pager::open(&path, Default::default())
            .expect("open native pager");
        let coordinator = Arc::new(
            StoreCommitCoordinator::open(
                &wal_path,
                DurabilityMode::Async,
                GroupCommitOptions::default(),
            )
            .expect("open WAL"),
        );
        let recorder = Arc::new(
            StoreOwnershipRecorder::begin(Arc::clone(&coordinator), 7).expect("begin epoch"),
        );
        pager
            .install_ownership_recorder(recorder)
            .expect("install recorder");
        let page = pager
            .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
            .expect("allocate")
            .page_id();
        pager
            .record_retirement_intent(&[page])
            .expect("record retirement");
        pager.free_retired_pages(&[page]).expect("retire");
        let records: Vec<_> = WalReader::open(&wal_path)
            .expect("read WAL")
            .iter()
            .map(|item| item.expect("WAL record").1)
            .collect();
        assert_eq!(
            records,
            vec![
                WalRecord::PageOwnership {
                    record: PageOwnershipRecord::Begin { epoch: 7 }
                },
                WalRecord::PageOwnership {
                    record: PageOwnershipRecord::Reserve {
                        epoch: 7,
                        page_id: page
                    }
                },
                WalRecord::PageOwnership {
                    record: PageOwnershipRecord::Retire {
                        epoch: 7,
                        page_id: page
                    }
                },
                WalRecord::PageOwnership {
                    record: PageOwnershipRecord::Retire {
                        epoch: 7,
                        page_id: page
                    }
                },
            ]
        );
        assert!(
            coordinator.fsync_count() >= 3,
            "async ownership writes must sync"
        );
        assert!(
            coordinator.truncate().is_err(),
            "ownership WAL remains protected"
        );
    }

    #[test]
    fn replay_reclaims_unpublished_ownership_reservations() {
        use reddb_file::page_ownership_journal::PageOwnershipRecord;

        let directory = tempfile::tempdir().expect("temporary store");
        let path = directory.path().join("data.rdb");
        {
            let store = UnifiedStore::open(&path).expect("create store");
            store.create_collection("rows").expect("collection");
            store.persist().expect("persist baseline");
        }
        let wal_path = reddb_file::layout::unified_wal_path(&path);
        let ids;
        {
            let pager =
                crate::storage::engine::Pager::open(&path, Default::default()).expect("pager");
            let first = pager.page_count().expect("page count");
            let coordinator = StoreCommitCoordinator::open(
                &wal_path,
                DurabilityMode::Strict,
                GroupCommitOptions::default(),
            )
            .expect("WAL");
            coordinator
                .append_durable_page_ownership_batch(&[
                    PageOwnershipRecord::Begin { epoch: 2 },
                    PageOwnershipRecord::Reserve {
                        epoch: 2,
                        page_id: first,
                    },
                    PageOwnershipRecord::Reserve {
                        epoch: 2,
                        page_id: first + 1,
                    },
                ])
                .expect("reserve pages durably");
            ids = [
                pager
                    .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
                    .expect("allocate")
                    .page_id(),
                pager
                    .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
                    .expect("allocate")
                    .page_id(),
            ];
            assert_eq!(ids, [first, first + 1]);
            pager.sync().expect("persist abandoned pages");
        }
        let reopened = UnifiedStore::open(&path).expect("recover ownership");
        let pager = reopened.pager.as_ref().expect("pager");
        let a = pager
            .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
            .expect("reuse")
            .page_id();
        let b = pager
            .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
            .expect("reuse")
            .page_id();
        assert_ne!(a, b);
        assert!(ids.contains(&a) && ids.contains(&b));
        let records: Vec<_> = WalReader::open(&wal_path)
            .expect("read WAL")
            .iter()
            .collect::<io::Result<Vec<_>>>()
            .expect("records");
        assert!(matches!(
            records.last(),
            Some((
                _,
                WalRecord::PageOwnership {
                    record: PageOwnershipRecord::Finish { epoch: 2 }
                }
            ))
        ));
        reopened
            .commit
            .as_ref()
            .expect("coordinator")
            .truncate()
            .expect("completed ownership can be truncated");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn ownership_recovery_survives_interruption_and_retry() {
        use reddb_file::page_ownership_journal::PageOwnershipRecord;

        const CHILD: &str = "REDDB_EXPERIMENTAL_OWNERSHIP_RECOVERY_PATH";
        if let Some(path) = std::env::var_os(CHILD) {
            let _store = UnifiedStore::open(path).expect("recover store");
            panic!("recovery interruption hook was not reached");
        }
        for (published, hook, exit_code) in [
            (
                false,
                "REDDB_EXPERIMENTAL_EXIT_DURING_OWNERSHIP_RECOVERY",
                187,
            ),
            (false, "REDDB_EXPERIMENTAL_EXIT_AFTER_OWNERSHIP_SYNC", 188),
            (false, "REDDB_EXPERIMENTAL_EXIT_AFTER_OWNERSHIP_FINISH", 189),
            (
                true,
                "REDDB_EXPERIMENTAL_EXIT_DURING_OWNERSHIP_RECOVERY",
                187,
            ),
            (true, "REDDB_EXPERIMENTAL_EXIT_AFTER_OWNERSHIP_SYNC", 188),
            (true, "REDDB_EXPERIMENTAL_EXIT_AFTER_OWNERSHIP_FINISH", 189),
        ] {
            let directory = tempfile::tempdir().expect("temporary store");
            let path = directory.path().join("data.rdb");
            {
                let store = UnifiedStore::open(&path).expect("create store");
                store.create_collection("rows").expect("collection");
                store.persist().expect("baseline");
                if published {
                    store.ownership_active_epoch.store(2, Ordering::Release);
                    store.flush_paged_registry().expect("publish epoch");
                    store.ownership_active_epoch.store(0, Ordering::Release);
                }
            }
            let wal_path = reddb_file::layout::unified_wal_path(&path);
            let ids;
            {
                let pager =
                    crate::storage::engine::Pager::open(&path, Default::default()).expect("pager");
                let first = pager.page_count().expect("page count");
                let coordinator = StoreCommitCoordinator::open(
                    &wal_path,
                    DurabilityMode::Strict,
                    GroupCommitOptions::default(),
                )
                .expect("WAL");
                let intent = |page_id| {
                    if published {
                        PageOwnershipRecord::Retire { epoch: 2, page_id }
                    } else {
                        PageOwnershipRecord::Reserve { epoch: 2, page_id }
                    }
                };
                coordinator
                    .append_durable_page_ownership_batch(&[
                        PageOwnershipRecord::Begin { epoch: 2 },
                        intent(first),
                        intent(first + 1),
                    ])
                    .expect("reserve");
                ids = [
                    pager
                        .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
                        .expect("allocate")
                        .page_id(),
                    pager
                        .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
                        .expect("allocate")
                        .page_id(),
                ];
                pager.sync().expect("persist abandoned pages");
            }
            for _ in 0..if exit_code == 189 { 1 } else { 2 } {
                let output = std::process::Command::new(std::env::current_exe().expect("test executable"))
                    .args(["--exact", "storage::unified::store::commit::tests::ownership_recovery_survives_interruption_and_retry", "--nocapture"])
                    .env(CHILD, &path)
                    .env(hook, "1")
                    .output().expect("run interrupted recovery");
                assert_eq!(
                    output.status.code(),
                    Some(exit_code),
                    "published={published} {hook}: {}",
                    String::from_utf8_lossy(&output.stderr)
                );
            }
            let reopened = UnifiedStore::open(&path).expect("recover after interruptions");
            let pager = reopened.pager.as_ref().expect("pager");
            let a = pager
                .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
                .expect("reuse")
                .page_id();
            let b = pager
                .allocate_page(crate::storage::engine::PageType::BTreeLeaf)
                .expect("reuse")
                .page_id();
            assert_ne!(a, b, "published={published} {hook}: duplicate page");
            assert!(
                ids.contains(&a) && ids.contains(&b),
                "published={published} {hook}: lost recovered page"
            );
        }
    }

    #[test]
    fn ownership_intents_are_durable_in_every_mode() {
        use reddb_file::page_ownership_journal::PageOwnershipRecord;

        for mode in [
            DurabilityMode::Strict,
            DurabilityMode::WalDurableGrouped,
            DurabilityMode::Async,
        ] {
            let directory = tempfile::tempdir().expect("temporary WAL");
            let path = directory.path().join("ownership");
            let coordinator =
                StoreCommitCoordinator::open(&path, mode, GroupCommitOptions::default())
                    .expect("open coordinator");
            let intent = PageOwnershipRecord::Reserve {
                epoch: 1,
                page_id: 67,
            };
            let detach = PageOwnershipRecord::DetachFree {
                epoch: 1,
                page_id: 68,
            };
            coordinator
                .append_durable_page_ownership_batch(&[intent, detach])
                .expect("durable intent");
            assert!(
                coordinator.fsync_count() > 0,
                "ownership intent must be synced in {mode:?}"
            );
            let records: Vec<_> = WalReader::open(&path)
                .expect("open WAL")
                .iter()
                .collect::<io::Result<Vec<_>>>()
                .expect("read records");
            assert!(
                matches!(records.as_slice(), [(_, WalRecord::PageOwnership { record: first }), (_, WalRecord::PageOwnership { record: second })] if *first == intent && *second == detach)
            );
            assert!(coordinator
                .append_single_record(WalRecord::PageOwnership { record: intent })
                .is_err());
            assert!(coordinator
                .append_durable_page_ownership(PageOwnershipRecord::Begin { epoch: 0 })
                .is_err());
            assert!(
                coordinator.truncate().is_err(),
                "active ownership must prevent truncation"
            );
            let before = std::fs::read(&path).expect("ownership WAL");
            drop(coordinator);
            let reopened = StoreCommitCoordinator::open(&path, mode, GroupCommitOptions::default())
                .expect("reopen coordinator");
            assert!(
                reopened.truncate().is_err(),
                "reopen must rediscover ownership records"
            );
            assert_eq!(std::fs::read(&path).expect("preserved WAL"), before);
        }
    }

    #[test]
    fn unresolved_ownership_intent_blocks_store_replay() {
        for truncated in [false, true] {
            let directory = tempfile::tempdir().expect("temporary store");
            let path = directory.path().join("data.rdb");
            {
                let store = UnifiedStore::open(&path).expect("create store");
                store.create_collection("rows").expect("create collection");
                store.persist().expect("persist baseline");
            }
            let wal_path = reddb_file::layout::unified_wal_path(&path);
            {
                let mut writer = WalWriter::open(&wal_path).expect("open WAL");
                writer
                    .append(&WalRecord::PageOwnership {
                        record: reddb_file::page_ownership_journal::PageOwnershipRecord::Reserve {
                            epoch: 1,
                            page_id: 67,
                        },
                    })
                    .expect("append ownership intent");
                writer.sync().expect("sync ownership intent");
            }
            if truncated {
                let wal = std::fs::OpenOptions::new()
                    .write(true)
                    .open(&wal_path)
                    .expect("WAL");
                let length = wal.metadata().expect("length").len();
                wal.set_len(length - 1).expect("truncate last byte");
                wal.sync_all().expect("sync truncation");
            }
            let before = std::fs::read(&path).expect("baseline data");
            let error = match UnifiedStore::open(&path) {
                Ok(_) => panic!("unresolved ownership must prevent opening"),
                Err(error) => error,
            };
            let expected = if truncated {
                "incomplete page ownership WAL record"
            } else {
                "page ownership journal requires recovery"
            };
            assert!(error.to_string().contains(expected), "{error}");
            assert_eq!(std::fs::read(&path).expect("preserved data"), before);
        }
    }

    #[test]
    fn logical_wal_replay_restores_a_deleted_physical_identity() {
        for flat in [false, true] {
            let store = UnifiedStore::new();
            store.create_collection("replay").expect("collection");
            let id = EntityId::new(42);
            let entity = UnifiedEntity::table_row(id, "replay", 42, vec![Value::Integer(1)]);
            if flat {
                store
                    .bulk_insert("replay", vec![entity])
                    .expect("flat seed");
            } else {
                store.insert_auto("replay", entity).expect("hash seed");
            }
            let metadata = Metadata::with_fields(HashMap::from([(
                "source".into(),
                MetadataValue::String("wal".into()),
            )]));
            for sequence in 100..104 {
                let delete = StoreWalAction::DeleteEntityRecord {
                    collection: "replay".into(),
                    entity_id: id.raw(),
                };
                store
                    .apply_encoded_store_wal_action(&delete.encode())
                    .expect("replay delete");
                let mut restored =
                    UnifiedEntity::table_row(id, "replay", 42, vec![Value::Integer(2)]);
                restored.ensure_table_logical_id();
                restored.sequence_id = sequence;
                let upsert = StoreWalAction::upsert_entity(
                    "replay",
                    &restored,
                    Some(&metadata),
                    store.format_version(),
                );
                store
                    .apply_encoded_store_wal_action(&upsert.encode())
                    .expect("replay upsert");
                let manager = store.get_collection("replay").expect("restored collection");
                let stored = manager.get(id).expect("restored entity is readable");
                assert_eq!(store.entity_cache_stats().entries, 0);
                assert_eq!(manager.count(), 1);
                assert_eq!(manager.query_all(|_| true).len(), 1);
                assert_eq!(stored.sequence_id, sequence);
                assert_eq!(
                    UnifiedStore::serialize_entity_record(
                        &stored,
                        manager.get_metadata(id).as_ref(),
                        store.format_version()
                    ),
                    UnifiedStore::serialize_entity_record(
                        &restored,
                        Some(&metadata),
                        store.format_version()
                    )
                );
                assert!(store.get_any(id).is_some());
            }
        }
    }

    fn embedded_fixture(
        mode: DurabilityMode,
        background_bytes: usize,
    ) -> (tempfile::TempDir, PathBuf, UnifiedStore) {
        let directory = tempfile::tempdir().expect("directory");
        let path = directory.path().join("data.rdb");
        let mut store =
            UnifiedStore::with_config(UnifiedStoreConfig::default().with_durability_mode(mode));
        store.create_collection("keys").expect("collection");
        store.set_aux_metadata(vec![42; background_bytes]);
        crate::storage::EmbeddedRdbArtifact::create_with_snapshot(
            &path,
            &store.to_binary_dump_bytes(),
        )
        .expect("snapshot");
        store.config.embedded_wal_path = Some(path.clone());
        (directory, path, store)
    }

    #[test]
    fn embedded_stale_full_result_does_not_checkpoint_reclaimed_space() {
        for mode in [DurabilityMode::Strict, DurabilityMode::WalDurableGrouped] {
            let (_directory, path, store) = embedded_fixture(mode, 0);
            store
                .append_embedded_store_wal_actions(&[StoreWalAction::CreateCollection {
                    name: "keys".into(),
                }])
                .expect("another writer appended after reclaiming space");
            let before = crate::storage::EmbeddedRdbArtifact::open(&path).expect("before");
            let payloads = vec![StoreWalAction::CreateCollection {
                name: "after".into(),
            }
            .encode()];
            let length = crate::storage::EmbeddedRdbArtifact::wal_payloads_encoded_len(&payloads)
                .expect("encoded length");
            // Model a full result obtained before that other writer's checkpoint.
            store
                .recover_full_embedded_wal(&path, &payloads, length)
                .expect("revalidate after acquiring publication");
            let after = crate::storage::EmbeddedRdbArtifact::open(&path).expect("after");
            assert_eq!(
                after.selected_superblock.generation,
                before.selected_superblock.generation + 1,
                "only the retry is published, without an unnecessary checkpoint"
            );
            assert_eq!(
                crate::storage::EmbeddedRdbArtifact::read_wal_payloads(&after)
                    .expect("both appends retained")
                    .len(),
                2
            );
        }
    }

    #[test]
    fn embedded_full_wal_reserves_bounded_headroom_for_populated_store() {
        for mode in [DurabilityMode::Strict, DurabilityMode::WalDurableGrouped] {
            let (_directory, path, store) = embedded_fixture(mode, 2 * 1024 * 1024);
            let initial = crate::storage::EmbeddedRdbArtifact::open(&path).expect("initial");
            let action = StoreWalAction::CreateCollection {
                name: "keys".into(),
            };
            let payloads = vec![action.encode()];
            let length = crate::storage::EmbeddedRdbArtifact::wal_payloads_encoded_len(&payloads)
                .expect("encoded length");
            crate::storage::EmbeddedRdbArtifact::append_wal_payloads(
                &path,
                &vec![payloads[0].clone(); (initial.manifest.wal_region_bytes / length) as usize],
            )
            .expect("fill the initial region with valid records");
            store
                .append_embedded_store_wal_actions(&[action])
                .expect("overflow");
            let open = crate::storage::EmbeddedRdbArtifact::open(&path).expect("checkpoint");
            assert_eq!(open.manifest.wal_region_bytes, 1024 * 1024);
            let snapshot = crate::storage::EmbeddedRdbArtifact::read_snapshot(&open)
                .expect("snapshot")
                .expect("populated image");
            let recovered =
                UnifiedStore::load_from_bytes_with_config(&snapshot, UnifiedStoreConfig::default())
                    .expect("recover snapshot");
            for payload in
                crate::storage::EmbeddedRdbArtifact::read_wal_payloads(&open).expect("WAL")
            {
                recovered
                    .apply_encoded_store_wal_action(&payload)
                    .expect("replay");
            }
            assert_eq!(
                recovered.resident.aux_metadata.read().len(),
                2 * 1024 * 1024
            );
            assert!(recovered.get_collection("keys").is_some());
        }
    }

    #[test]
    fn embedded_grouped_overwrites_recover_across_repeated_full_wal_regions() {
        fn row(writer: usize, round: usize) -> UnifiedEntity {
            let mut state = (writer as u64 + 1) * 31 + round as u64 * 13;
            let payload: String = (0..4096)
                .map(|_| {
                    state = state.wrapping_mul(1664525).wrapping_add(1013904223);
                    char::from(b'!' + ((state >> 24) % 90) as u8)
                })
                .collect();
            let mut entity = UnifiedEntity::table_row(EntityId::new(0), "keys", 0, vec![]);
            entity.data = EntityData::Row(RowData::with_names(
                vec![
                    Value::text(format!("k{writer}")),
                    Value::text(format!("{round}:{payload}")),
                ],
                vec!["key".into(), "value".into()],
            ));
            entity
        }
        let (_directory, path, store) =
            embedded_fixture(DurabilityMode::WalDurableGrouped, 2 * 1024 * 1024);
        assert!(
            StoreWalAction::upsert_entity("keys", &row(0, 0), None, store.format_version())
                .encode()
                .len()
                >= 2048,
            "fixture must exert physical WAL pressure after value compression"
        );
        let mut ids: Vec<_> = (0..8)
            .map(|writer| {
                store
                    .write_keyed_row("keys", row(writer, 0), None, None)
                    .expect("seed")
            })
            .collect();
        let initial = crate::storage::EmbeddedRdbArtifact::open(&path).expect("initial roots");
        let mut previous_snapshot = crate::storage::EmbeddedRdbArtifact::read_snapshot(&initial)
            .expect("initial snapshot")
            .expect("image");
        // Each wave writes over 2 MiB through the same 1 MiB reservation.
        // Checking image changes after each wave prevents a larger WAL from
        // merely postponing every checkpoint beyond the test's duration.
        for wave in 0..3 {
            let barrier = Barrier::new(ids.len());
            ids = std::thread::scope(|scope| {
                let workers: Vec<_> = ids
                    .iter()
                    .enumerate()
                    .map(|(writer, previous)| {
                        let store = &store;
                        let barrier = &barrier;
                        scope.spawn(move || {
                            barrier.wait();
                            let mut id = *previous;
                            for round in 0..64 {
                                id = store
                                    .write_keyed_row(
                                        "keys",
                                        row(writer, wave * 64 + round),
                                        None,
                                        Some(id),
                                    )
                                    .expect("durable overwrite");
                            }
                            id
                        })
                    })
                    .collect();
                workers
                    .into_iter()
                    .map(|worker| worker.join().expect("writer"))
                    .collect()
            });
            let open = crate::storage::EmbeddedRdbArtifact::open(&path).expect("wave roots");
            assert_eq!(open.manifest.wal_region_bytes, EMBEDDED_WAL_HEADROOM_LIMIT);
            let snapshot = crate::storage::EmbeddedRdbArtifact::read_snapshot(&open)
                .expect("wave snapshot")
                .expect("image");
            assert!(snapshot != previous_snapshot, "wave {wave} must checkpoint");
            previous_snapshot = snapshot;
        }
        let open = crate::storage::EmbeddedRdbArtifact::open(&path).expect("recover roots");
        let snapshot = crate::storage::EmbeddedRdbArtifact::read_snapshot(&open)
            .expect("snapshot")
            .expect("image");
        let recovered =
            UnifiedStore::load_from_bytes_with_config(&snapshot, UnifiedStoreConfig::default())
                .expect("snapshot replay");
        for payload in crate::storage::EmbeddedRdbArtifact::read_wal_payloads(&open).expect("WAL") {
            recovered
                .apply_encoded_store_wal_action(&payload)
                .expect("WAL replay");
        }
        assert_eq!(recovered.get_collection("keys").expect("keys").count(), 8);
        for (writer, id) in ids.iter().enumerate() {
            let entity = recovered
                .get("keys", *id)
                .expect("last acknowledged version");
            assert_eq!(
                entity.data.as_row().expect("row").get_field("value"),
                row(writer, 191)
                    .data
                    .as_row()
                    .expect("expected row")
                    .get_field("value")
            );
        }
    }

    /// Serialise tests that mutate `REDDB_GROUP_COMMIT_WINDOW_US`.
    /// The env table is process-global, so two parallel test threads
    /// flipping it would race the assertions in the test that
    /// happens to read it last.
    fn env_lock() -> &'static StdMutex<()> {
        static LOCK: OnceLock<StdMutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| StdMutex::new(()))
    }

    fn temp_wal(name: &str) -> PathBuf {
        let nanos = SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let path = reddb_file::layout::store_commit_coord_temp_wal_path(
            &std::env::temp_dir(),
            name,
            std::process::id(),
            nanos,
        );
        let _ = std::fs::remove_file(&path);
        path
    }

    fn replay_crash_payload(id: u64, round: u64) -> String {
        let mut state = id * 31 + round * 13 + 1;
        (0..600)
            .map(|_| {
                state = state.wrapping_mul(1664525).wrapping_add(1013904223);
                char::from(b'!' + ((state >> 24) % 90) as u8)
            })
            .collect()
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn interrupted_replay_reclaims_unpublished_pages() {
        interrupted_replay_reclaims_pages(false, false);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn interrupted_replay_reclaims_durable_unpublished_pages() {
        interrupted_replay_reclaims_pages(false, true);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn interrupted_replay_preserves_published_pages() {
        interrupted_replay_reclaims_pages(true, false);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn interrupted_checkpoint_reclaims_unpublished_pages() {
        const CHILD_PATH: &str = "REDDB_EXPERIMENTAL_CHECKPOINT_REBUILD_PATH";
        let config = || {
            UnifiedStoreConfig::default()
                .with_durability_mode(DurabilityMode::Strict)
                .with_page_cache_slots(4)
        };
        if let Some(path) = std::env::var_os(CHILD_PATH) {
            let store = UnifiedStore::open_with_config(path, config()).unwrap();
            // Inject resident changes without a WAL record: after the aborted
            // checkpoint the old durable rows must remain authoritative.
            for id in 1..=200 {
                let entity = UnifiedEntity::table_row(
                    EntityId::new(id),
                    "rows",
                    id,
                    vec![Value::text(replay_crash_payload(id, 8))],
                );
                let record =
                    UnifiedStore::serialize_entity_record(&entity, None, STORE_VERSION_V11);
                store.apply_replayed_upsert("rows", &record, false).unwrap();
            }
            store.persist().expect("must exit during checkpoint");
            panic!("checkpoint rebuild hook was not reached");
        }
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("data.rdb");
        {
            let store = UnifiedStore::open_with_config(&path, config()).unwrap();
            store.create_collection("rows").unwrap();
            for id in 1..=200 {
                store
                    .insert(
                        "rows",
                        UnifiedEntity::table_row(
                            EntityId::new(id),
                            "rows",
                            id,
                            vec![Value::text(format!("initial-{id}"))],
                        ),
                    )
                    .unwrap();
            }
            store.persist().unwrap();
        }
        let mut first = 0;
        let mut last = 0;
        for cycle in 1..=12 {
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "storage::unified::store::commit::tests::interrupted_checkpoint_reclaims_unpublished_pages",
                    "--nocapture",
                ])
                .env(CHILD_PATH, &path)
                .env("REDDB_EXPERIMENTAL_EXIT_DURING_CHECKPOINT_REBUILD", "1")
                .output()
                .unwrap();
            assert_eq!(
                output.status.code(),
                Some(183),
                "{}",
                String::from_utf8_lossy(&output.stderr)
            );
            assert!(String::from_utf8_lossy(&output.stderr)
                .contains("experimental checkpoint exit before manifest"));
            last = std::fs::metadata(&path).unwrap().len();
            if cycle == 1 {
                first = last;
            }
            eprintln!("checkpoint_exit_cycle={cycle} data_logical={last}");
        }
        let recovered = UnifiedStore::open_with_config(&path, config()).unwrap();
        assert_eq!(recovered.get_collection("rows").unwrap().count(), 200);
        for id in 1..=200 {
            assert_eq!(
                recovered
                    .get("rows", EntityId::new(id))
                    .unwrap()
                    .data
                    .as_row()
                    .unwrap()
                    .columns,
                vec![Value::text(format!("initial-{id}"))]
            );
        }
        assert!(last <= first + 2 * crate::storage::engine::PAGE_SIZE as u64,
            "repeated interrupted checkpoint must bound page growth: first={first}, after_12={last}");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn interrupted_checkpoint_with_wal_reclaims_unpublished_pages() {
        use std::os::unix::fs::MetadataExt;
        const CHILD_PATH: &str = "REDDB_EXPERIMENTAL_CHECKPOINT_WAL_PATH";
        const ROUND: &str = "REDDB_EXPERIMENTAL_CHECKPOINT_WAL_ROUND";
        let config = || {
            UnifiedStoreConfig::default()
                .with_durability_mode(DurabilityMode::Strict)
                .with_page_cache_slots(4)
        };
        if let Some(path) = std::env::var_os(CHILD_PATH) {
            let round: u64 = std::env::var(ROUND).unwrap().parse().unwrap();
            let store = UnifiedStore::open_with_config(path, config()).unwrap();
            if round > 1 {
                for id in 1..=200 {
                    assert_eq!(
                        store
                            .get("rows", EntityId::new(id))
                            .unwrap()
                            .data
                            .as_row()
                            .unwrap()
                            .columns,
                        vec![Value::text(replay_crash_payload(id, round - 1))]
                    );
                }
            }
            let actions: Vec<_> = (1..=200)
                .map(|id| {
                    let entity = UnifiedEntity::table_row(
                        EntityId::new(id),
                        "rows",
                        id,
                        vec![Value::text(replay_crash_payload(id, round))],
                    );
                    StoreWalAction::upsert_entity("rows", &entity, None, STORE_VERSION_V11)
                })
                .collect();
            for action in &actions {
                store.apply_replayed_action(action, false).unwrap();
            }
            // Use the actual strict WAL coordinator. No checkpoint proof is
            // installed, so the replacement-tree path must still be taken.
            store
                .commit
                .as_ref()
                .unwrap()
                .append_actions(&actions)
                .unwrap();
            store.persist().expect("must exit during checkpoint");
            panic!("checkpoint rebuild hook was not reached");
        }
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("data.rdb");
        {
            let store = UnifiedStore::open_with_config(&path, config()).unwrap();
            store.create_collection("rows").unwrap();
            for id in 1..=200 {
                store
                    .insert(
                        "rows",
                        UnifiedEntity::table_row(
                            EntityId::new(id),
                            "rows",
                            id,
                            vec![Value::text(format!("initial-{id}"))],
                        ),
                    )
                    .unwrap();
            }
            store.persist().unwrap();
        }
        let mut fourth = 0;
        let mut last = 0;
        for round in 1..=12 {
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "storage::unified::store::commit::tests::interrupted_checkpoint_with_wal_reclaims_unpublished_pages",
                    "--nocapture",
                ])
                .env(CHILD_PATH, &path)
                .env(ROUND, round.to_string())
                .env("REDDB_EXPERIMENTAL_EXIT_DURING_CHECKPOINT_REBUILD", "1")
                .output().unwrap();
            assert_eq!(
                output.status.code(),
                Some(183),
                "round {round}: {}",
                String::from_utf8_lossy(&output.stderr)
            );
            assert!(String::from_utf8_lossy(&output.stderr)
                .contains("experimental checkpoint exit before manifest"));
            let meta = std::fs::metadata(&path).unwrap();
            last = meta.len();
            if round == 4 {
                fourth = last;
            }
            eprintln!(
                "checkpoint_wal_exit_round={round} data_logical={last} data_allocated={}",
                meta.blocks() * 512
            );
        }
        let recovered = UnifiedStore::open_with_config(&path, config()).unwrap();
        assert_eq!(recovered.get_collection("rows").unwrap().count(), 200);
        for id in 1..=200 {
            assert_eq!(
                recovered
                    .get("rows", EntityId::new(id))
                    .unwrap()
                    .data
                    .as_row()
                    .unwrap()
                    .columns,
                vec![Value::text(replay_crash_payload(id, 12))]
            );
        }
        recovered.persist().unwrap();
        drop(recovered);
        let reopened = UnifiedStore::open_with_config(&path, config()).unwrap();
        for id in 1..=200 {
            assert_eq!(
                reopened
                    .get("rows", EntityId::new(id))
                    .unwrap()
                    .data
                    .as_row()
                    .unwrap()
                    .columns,
                vec![Value::text(replay_crash_payload(id, 12))]
            );
        }
        assert!(last <= fourth + 2 * crate::storage::engine::PAGE_SIZE as u64,
            "repeated interrupted WAL-backed checkpoints must bound page growth: round4={fourth}, round12={last}");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn interrupted_retired_reclamation_preserves_reuse() {
        const CHILD_PATH: &str = "REDDB_EXPERIMENTAL_RETIRED_PATH";
        fn mutate(store: &UnifiedStore, round: u64) {
            for id in 1..=200 {
                let entity = UnifiedEntity::table_row(
                    EntityId::new(id),
                    "rows",
                    id,
                    vec![Value::text(replay_crash_payload(id, round))],
                );
                let record =
                    UnifiedStore::serialize_entity_record(&entity, None, STORE_VERSION_V11);
                store.apply_replayed_upsert("rows", &record, false).unwrap();
            }
        }
        let config = || {
            UnifiedStoreConfig::default()
                .with_durability_mode(DurabilityMode::Strict)
                .with_page_cache_slots(4)
        };
        if let Some(path) = std::env::var_os(CHILD_PATH) {
            let store = UnifiedStore::open_with_config(path, config()).unwrap();
            mutate(&store, 2);
            store.persist().expect("must exit at retired free boundary");
            panic!("retired free hook was not reached");
        }
        let mut reuse = Vec::new();
        for (label, env_name, exit_code, message) in [
            (
                "before",
                "REDDB_EXPERIMENTAL_EXIT_BEFORE_RETIRED_FREE",
                184,
                "experimental exit before retired free",
            ),
            (
                "enqueued",
                "REDDB_EXPERIMENTAL_EXIT_AFTER_RETIRED_ENQUEUE",
                186,
                "experimental exit after retired enqueue",
            ),
            (
                "after",
                "REDDB_EXPERIMENTAL_EXIT_AFTER_RETIRED_FREE",
                185,
                "experimental exit after retired free",
            ),
        ] {
            let directory = tempfile::tempdir().unwrap();
            let path = directory.path().join("data.rdb");
            {
                let store = UnifiedStore::open_with_config(&path, config()).unwrap();
                store.create_collection("rows").unwrap();
                for id in 1..=200 {
                    store
                        .insert(
                            "rows",
                            UnifiedEntity::table_row(
                                EntityId::new(id),
                                "rows",
                                id,
                                vec![Value::text(format!("initial-{id}"))],
                            ),
                        )
                        .unwrap();
                }
                store.persist().unwrap();
                mutate(&store, 1);
                store.persist().unwrap();
            }
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "storage::unified::store::commit::tests::interrupted_retired_reclamation_preserves_reuse",
                    "--nocapture",
                ])
                .env(CHILD_PATH, &path)
                .env(env_name, "1")
                .output().unwrap();
            assert_eq!(
                output.status.code(),
                Some(exit_code),
                "{label}: {}",
                String::from_utf8_lossy(&output.stderr)
            );
            assert!(String::from_utf8_lossy(&output.stderr).contains(message));
            eprintln!(
                "retired_reclaim_{label} child={}",
                String::from_utf8_lossy(&output.stderr).trim()
            );
            let store = UnifiedStore::open_with_config(&path, config()).unwrap();
            for id in 1..=200 {
                assert_eq!(
                    store
                        .get("rows", EntityId::new(id))
                        .unwrap()
                        .data
                        .as_row()
                        .unwrap()
                        .columns,
                    vec![Value::text(replay_crash_payload(id, 2))]
                );
            }
            let pager = store.pager.as_ref().unwrap();
            let before_pages = pager.page_count().unwrap();
            let mut ids = std::collections::HashSet::new();
            let mut reused = 0;
            for _ in 0..256 {
                let page = pager
                    .allocate_page(crate::storage::engine::page::PageType::BTreeLeaf)
                    .unwrap();
                let id = page.page_id();
                assert!(ids.insert(id), "duplicate allocation");
                reused += u32::from(id < before_pages);
                pager.write_page(id, page).unwrap();
            }
            pager.sync().unwrap();
            eprintln!(
                "retired_reclaim_{label} before_pages={before_pages} reused={reused} appended={}",
                256 - reused
            );
            drop(store);
            let reopened = UnifiedStore::open_with_config(&path, config()).unwrap();
            for id in 1..=200 {
                assert_eq!(
                    reopened
                        .get("rows", EntityId::new(id))
                        .unwrap()
                        .data
                        .as_row()
                        .unwrap()
                        .columns,
                    vec![Value::text(replay_crash_payload(id, 2))]
                );
            }
            reuse.push(reused);
        }
        assert!(
            reuse[0] >= reuse[2] && reuse[1] >= reuse[2],
            "recovery must not lose retired pages before allocator publication: before={} enqueued={} after={}",
            reuse[0],
            reuse[1],
            reuse[2]
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn experimental_retaining_released_root_is_not_recoverable() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("data.rdb");
        let config = || {
            UnifiedStoreConfig::default()
                .with_durability_mode(DurabilityMode::Strict)
                .with_page_cache_slots(4)
        };
        {
            let store = UnifiedStore::open_with_config(&path, config()).unwrap();
            store.create_collection("rows").unwrap();
            for id in 1..=200 {
                store
                    .insert(
                        "rows",
                        UnifiedEntity::table_row(
                            EntityId::new(id),
                            "rows",
                            id,
                            vec![Value::text(format!("initial-{id}"))],
                        ),
                    )
                    .unwrap();
            }
            store.persist().unwrap();
            for id in 1..=200 {
                let entity = UnifiedEntity::table_row(
                    EntityId::new(id),
                    "rows",
                    id,
                    vec![Value::text(replay_crash_payload(id, 1))],
                );
                let record =
                    UnifiedStore::serialize_entity_record(&entity, None, STORE_VERSION_V11);
                store.apply_replayed_upsert("rows", &record, false).unwrap();
            }
            store.persist().unwrap();
        }
        let output = std::process::Command::new(std::env::current_exe().unwrap())
            .args(["--exact", "storage::unified::store::commit::tests::interrupted_retired_reclamation_preserves_reuse", "--nocapture"])
            .env("REDDB_EXPERIMENTAL_RETIRED_PATH", &path)
            .env("REDDB_EXPERIMENTAL_RETAIN_RELEASED_ROOT", "1")
            .env("REDDB_EXPERIMENTAL_EXIT_AFTER_RETIRED_FREE", "1")
            .output().unwrap();
        assert_eq!(
            output.status.code(),
            Some(185),
            "{}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert!(String::from_utf8_lossy(&output.stderr)
            .contains("experimental exit after retired free"));

        let reopened = UnifiedStore::open_with_config(&path, config()).unwrap();
        for id in 1..=200 {
            assert_eq!(
                reopened
                    .get("rows", EntityId::new(id))
                    .unwrap()
                    .data
                    .as_row()
                    .unwrap()
                    .columns,
                vec![Value::text(replay_crash_payload(id, 2))]
            );
        }
        let retired_roots: Vec<_> = reopened
            .retired_btrees
            .lock()
            .iter()
            .map(|entry| entry.tree.root_page_id())
            .collect();
        let pager = reopened.pager.as_ref().unwrap();
        let root_types: Vec<_> = retired_roots
            .iter()
            .map(|id| pager.read_page(*id).unwrap().page_type().unwrap())
            .collect();
        assert!(
            root_types.contains(&crate::storage::engine::page::PageType::FreelistTrunk),
            "expected a retained root to have become a freelist trunk: {root_types:?}"
        );
        let error = reopened
            .persist()
            .expect_err("retained root was reused as allocator metadata");
        assert!(
            error
                .to_string()
                .contains("unexpected retired tree page type"),
            "{error}"
        );
        eprintln!("retained_root_types={root_types:?} persist_error={error}");
    }

    #[cfg(target_os = "linux")]
    fn interrupted_replay_reclaims_pages(after_publication: bool, force_sync: bool) {
        use std::os::unix::fs::MetadataExt;
        const CHILD_PATH: &str = "REDDB_EXPERIMENTAL_REPLAY_REBUILD_PATH";
        let config = || {
            UnifiedStoreConfig::default()
                .with_durability_mode(DurabilityMode::Strict)
                .with_page_cache_slots(4)
        };
        if let Some(path) = std::env::var_os(CHILD_PATH) {
            let _store = UnifiedStore::open_with_config(path, config())
                .expect("must exit while replay rebuilds");
            panic!("replay rebuild hook was not reached");
        }
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("data.rdb");
        {
            let store = UnifiedStore::open_with_config(&path, config()).unwrap();
            store.create_collection("rows").unwrap();
            for id in 1..=200 {
                store
                    .insert(
                        "rows",
                        UnifiedEntity::table_row(
                            EntityId::new(id),
                            "rows",
                            id,
                            vec![Value::text(format!("initial-{id}"))],
                        ),
                    )
                    .unwrap();
            }
            store.persist().unwrap();
        }
        {
            let coordinator = StoreCommitCoordinator::open(
                reddb_file::layout::unified_wal_path(&path),
                DurabilityMode::Strict,
                GroupCommitOptions::default(),
            )
            .unwrap();
            let actions = (1..=200)
                .map(|id| {
                    let entity = UnifiedEntity::table_row(
                        EntityId::new(id),
                        "rows",
                        id,
                        vec![Value::text(replay_crash_payload(id, 8))],
                    );
                    StoreWalAction::upsert_entity("rows", &entity, None, STORE_VERSION_V11)
                })
                .collect::<Vec<_>>();
            coordinator.append_actions(&actions).unwrap();
        }
        let snapshot = || {
            let data = std::fs::metadata(&path).unwrap();
            let wal = std::fs::metadata(reddb_file::layout::unified_wal_path(&path)).unwrap();
            (
                data.len(),
                data.blocks() * 512,
                wal.len(),
                wal.blocks() * 512,
            )
        };
        let baseline = snapshot();
        let mut previous = baseline;
        let mut first_failure = baseline;
        for cycle in 1..=12 {
            let test_name = if after_publication {
                "storage::unified::store::commit::tests::interrupted_replay_preserves_published_pages"
            } else if force_sync {
                "storage::unified::store::commit::tests::interrupted_replay_reclaims_durable_unpublished_pages"
            } else {
                "storage::unified::store::commit::tests::interrupted_replay_reclaims_unpublished_pages"
            };
            let mut command = std::process::Command::new(std::env::current_exe().unwrap());
            command
                .args(["--exact", test_name, "--nocapture"])
                .env(CHILD_PATH, &path);
            if !force_sync {
                command.env("REDDB_EXPERIMENTAL_REPLAY_NO_SYNC", "1");
            } else {
                command.env_remove("REDDB_EXPERIMENTAL_REPLAY_NO_SYNC");
            }
            let message = if after_publication {
                command.env("REDDB_EXPERIMENTAL_EXIT_AFTER_REPLAY_PUBLICATION", "1");
                "experimental replay exit after manifest"
            } else {
                command.env("REDDB_EXPERIMENTAL_EXIT_DURING_REPLAY_REBUILD", "1");
                "experimental replay rebuild exit before manifest"
            };
            let output = command.output().unwrap();
            assert_eq!(
                output.status.code(),
                Some(182),
                "cycle {cycle}: {}",
                String::from_utf8_lossy(&output.stderr)
            );
            assert!(String::from_utf8_lossy(&output.stderr).contains(message));
            let now = snapshot();
            eprintln!("replay_exit_cycle={cycle} data_logical={} data_allocated={} wal_logical={} wal_allocated={}",now.0,now.1,now.2,now.3);
            if cycle == 1 {
                first_failure = now;
            }
            previous = now;
        }
        let recovered = UnifiedStore::open_with_config(&path, config()).unwrap();
        assert_eq!(recovered.get_collection("rows").unwrap().count(), 200);
        for id in 1..=200 {
            assert_eq!(
                recovered
                    .get("rows", EntityId::new(id))
                    .unwrap()
                    .data
                    .as_row()
                    .unwrap()
                    .columns,
                vec![Value::text(replay_crash_payload(id, 8))]
            );
        }
        let pages = recovered.pager.as_ref().unwrap().page_count().unwrap();
        let free = recovered.pager.as_ref().unwrap().free_page_count().unwrap();
        recovered.persist().unwrap();
        drop(recovered);
        let final_sizes = snapshot();
        let reopened = UnifiedStore::open_with_config(&path, config()).unwrap();
        assert_eq!(reopened.get_collection("rows").unwrap().count(), 200);
        for id in 1..=200 {
            assert_eq!(
                reopened
                    .get("rows", EntityId::new(id))
                    .unwrap()
                    .data
                    .as_row()
                    .unwrap()
                    .columns,
                vec![Value::text(replay_crash_payload(id, 8))]
            );
        }
        eprintln!("replay_rebuild baseline={baseline:?} after_12={previous:?} after_recovery_checkpoint={final_sizes:?} pages={pages} free={free}");
        // Probe actual allocation rather than the cached free-page counter:
        // newly allocated IDs reveal whether interrupted generations returned
        // to the allocator after recovery and a completed checkpoint.
        let pager = reopened.pager.as_ref().unwrap();
        let before_probe = pager.page_count().unwrap();
        let mut ids = std::collections::HashSet::new();
        let mut reused = 0;
        for _ in 0..256 {
            let page = pager
                .allocate_page(crate::storage::engine::page::PageType::BTreeLeaf)
                .unwrap();
            let id = page.page_id();
            assert!(ids.insert(id), "allocator returned the same page twice");
            reused += u32::from(id < before_probe);
            pager.write_page(id, page).unwrap();
        }
        pager.sync().unwrap();
        let after_probe = pager.page_count().unwrap();
        eprintln!("replay_reuse_probe count=256 before_pages={before_probe} after_pages={after_probe} reused={reused} appended={}", 256 - reused);
        drop(reopened);
        let verified = UnifiedStore::open_with_config(&path, config()).unwrap();
        for id in 1..=200 {
            assert_eq!(
                verified
                    .get("rows", EntityId::new(id))
                    .unwrap()
                    .data
                    .as_row()
                    .unwrap()
                    .columns,
                vec![Value::text(replay_crash_payload(id, 8))]
            );
        }
        assert!(
            previous.0 <= first_failure.0 + 2 * crate::storage::engine::PAGE_SIZE as u64,
            "repeated interrupted replay must bound page growth: first={}, after_12={}",
            first_failure.0,
            previous.0
        );
    }

    #[test]
    fn paged_wal_replay_survives_immediate_process_exit() {
        const CHILD_PATH: &str = "REDDB_TEST_REPLAY_CRASH_PATH";
        let config = || UnifiedStoreConfig::default().with_page_cache_slots(4);
        if let Some(path) = std::env::var_os(CHILD_PATH) {
            let store = UnifiedStore::open_with_config(path, config()).expect("replay child");
            assert_eq!(store.get_collection("rows").unwrap().count(), 400);
            // No Rust destructors or graceful checkpoint may repair the image.
            std::process::exit(0);
        }
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("data.rdb");
        {
            let store = UnifiedStore::open_with_config(&path, config()).unwrap();
            store.create_collection("rows").unwrap();
            for id in 1..=400 {
                store
                    .insert(
                        "rows",
                        UnifiedEntity::table_row(
                            EntityId::new(id),
                            "rows",
                            id,
                            vec![Value::text(format!("initial-{id}"))],
                        ),
                    )
                    .unwrap();
            }
            store.persist().unwrap();
        }
        {
            let coordinator = StoreCommitCoordinator::open(
                reddb_file::layout::unified_wal_path(&path),
                DurabilityMode::Strict,
                GroupCommitOptions::default(),
            )
            .unwrap();
            for round in 0..3 {
                let actions = (1..=400)
                    .map(|id| {
                        let payload = replay_crash_payload(id, round);
                        let entity = UnifiedEntity::table_row(
                            EntityId::new(id),
                            "rows",
                            id,
                            vec![Value::text(payload)],
                        );
                        StoreWalAction::upsert_entity("rows", &entity, None, STORE_VERSION_V11)
                    })
                    .collect::<Vec<_>>();
                coordinator.append_actions(&actions).unwrap();
            }
        }
        let status = std::process::Command::new(std::env::current_exe().unwrap())
            .args(["--exact", "storage::unified::store::commit::tests::paged_wal_replay_survives_immediate_process_exit", "--nocapture"])
            .env(CHILD_PATH, &path).status().unwrap();
        assert!(status.success(), "replay process failed");
        let reopened = UnifiedStore::open_with_config(&path, config())
            .expect("cold reopen after replay crash");
        assert_eq!(reopened.get_collection("rows").unwrap().count(), 400);
        for id in 1..=400 {
            let entity = reopened
                .get("rows", EntityId::new(id))
                .expect("retained entity");
            let EntityData::Row(row) = entity.data else {
                panic!("expected row")
            };
            assert_eq!(row.columns, vec![Value::text(replay_crash_payload(id, 2))]);
        }
        drop(reopened);
        let stable_bytes = std::fs::metadata(&path).unwrap().len();
        let repeated = UnifiedStore::open_with_config(&path, config()).unwrap();
        assert_eq!(repeated.get_collection("rows").unwrap().count(), 400);
        assert_eq!(
            std::fs::metadata(&path).unwrap().len(),
            stable_bytes,
            "idempotent replay must not allocate another tree generation"
        );
    }

    #[test]
    fn checkpoint_reset_waits_for_an_enqueued_commit_acknowledgement() {
        let _env = env_lock().lock().unwrap_or_else(|p| p.into_inner());
        let path = temp_wal("checkpoint-ack");
        let coordinator = Arc::new(
            StoreCommitCoordinator::open(
                path.clone(),
                DurabilityMode::WalDurableGrouped,
                GroupCommitOptions::default(),
            )
            .unwrap(),
        );
        // Pause the writer after enqueue, before it can register/wait for its
        // acknowledgement. The flusher also needs this state lock to drain.
        let state = coordinator.state.0.lock();
        let (written_tx, written_rx) = std::sync::mpsc::channel();
        let writer_coordinator = Arc::clone(&coordinator);
        let writer = std::thread::spawn(move || {
            let result = writer_coordinator.append_actions(&[StoreWalAction::CreateCollection {
                name: "acknowledged".into(),
            }]);
            written_tx.send(result).unwrap();
        });
        let deadline = Instant::now() + Duration::from_secs(2);
        while !coordinator.queue.has_pending() {
            assert!(Instant::now() < deadline, "writer never enqueued");
            std::thread::yield_now();
        }
        let (started_tx, started_rx) = std::sync::mpsc::channel();
        let (reset_tx, reset_rx) = std::sync::mpsc::channel();
        let reset_coordinator = Arc::clone(&coordinator);
        let reset = std::thread::spawn(move || {
            started_tx.send(()).unwrap();
            reset_tx.send(reset_coordinator.truncate()).unwrap();
        });
        started_rx.recv_timeout(Duration::from_secs(2)).unwrap();
        let deadline = Instant::now() + Duration::from_millis(100);
        while coordinator.queue.has_pending() && Instant::now() < deadline {
            std::thread::sleep(Duration::from_millis(1));
        }
        let discarded_before_ack = !coordinator.queue.has_pending();
        drop(state);
        let acknowledged = written_rx.recv_timeout(Duration::from_secs(2));
        if acknowledged.is_err() {
            // Release an old implementation's stranded waiter before failing
            // the assertion, so the regression itself cannot leak a worker.
            let mut state = coordinator.state.0.lock();
            state.shutdown = true;
            state.last_error = Some("checkpoint acknowledgement regression cleanup".into());
            coordinator.state.1.notify_all();
        }
        writer.join().unwrap();
        reset_rx
            .recv_timeout(Duration::from_secs(2))
            .unwrap()
            .unwrap();
        reset.join().unwrap();
        assert!(
            !discarded_before_ack,
            "checkpoint discarded an unacknowledged WAL record"
        );
        assert!(
            acknowledged.unwrap().is_ok(),
            "durable writer did not finish"
        );
        drop(coordinator);
        let _ = std::fs::remove_file(path);
    }

    /// Concurrent autocommits MUST coalesce into far fewer fsyncs
    /// than the number of callers when the adaptive group-commit
    /// window is active. Without the 200µs floor, every caller
    /// would race the drain loop and pay an independent fsync.
    ///
    /// The test fires 32 concurrent `append_actions` calls and asserts
    /// the coordinator issued strictly fewer fsyncs than callers.
    /// (We don't pin to "one fsync" because timing on loaded CI hosts
    /// can split the burst across a couple of drain cycles — the win
    /// is the ratio, not the absolute count.)
    #[test]
    fn group_commit_coalesces_concurrent_autocommits() {
        let _env = env_lock().lock().unwrap_or_else(|p| p.into_inner());
        // Make sure no stale env var skews this run — the test
        // exercises the default 200µs floor.
        std::env::remove_var("REDDB_GROUP_COMMIT_WINDOW_US");

        let path = temp_wal("coalesce");
        let coord = Arc::new(
            StoreCommitCoordinator::open(
                path.clone(),
                DurabilityMode::WalDurableGrouped,
                GroupCommitOptions::default(),
            )
            .expect("open commit coordinator"),
        );

        const WRITERS: usize = 32;
        let barrier = Arc::new(Barrier::new(WRITERS));
        let mut handles = Vec::with_capacity(WRITERS);
        for tx in 0..WRITERS {
            let coord_c = Arc::clone(&coord);
            let barrier_c = Arc::clone(&barrier);
            handles.push(std::thread::spawn(move || {
                // Synchronise the start so every writer races the
                // drain loop together — this is the workload shape
                // the adaptive window is supposed to coalesce.
                barrier_c.wait();
                let action = StoreWalAction::CreateCollection {
                    name: format!("c{tx}"),
                };
                coord_c
                    .append_actions(std::slice::from_ref(&action))
                    .expect("append_actions");
            }));
        }
        for h in handles {
            h.join().expect("writer thread");
        }

        let fsyncs = coord.fsync_count();
        assert!(fsyncs > 0, "expected at least one fsync, got {fsyncs}");
        assert!(
            fsyncs < WRITERS as u64,
            "expected fsyncs ({fsyncs}) to be strictly less than \
             concurrent writers ({WRITERS}); coalescing failed"
        );

        drop(coord);
        let _ = std::fs::remove_file(&path);
    }

    /// Sanity check: with the env override forcing a zero window,
    /// fsync coalescing degrades — callers race and we approach
    /// one fsync per caller. This proves the window is the actual
    /// knob doing the work above (i.e. the test isn't passing for
    /// some unrelated reason like buffered-IO coalescing).
    #[test]
    fn zero_window_disables_coalescing_floor() {
        let _env = env_lock().lock().unwrap_or_else(|p| p.into_inner());
        std::env::set_var("REDDB_GROUP_COMMIT_WINDOW_US", "0");

        let path = temp_wal("zero_window");
        let coord = Arc::new(
            StoreCommitCoordinator::open(
                path.clone(),
                DurabilityMode::WalDurableGrouped,
                GroupCommitOptions::default(),
            )
            .expect("open commit coordinator"),
        );

        const WRITERS: usize = 8;
        let barrier = Arc::new(Barrier::new(WRITERS));
        let mut handles = Vec::with_capacity(WRITERS);
        for tx in 0..WRITERS {
            let coord_c = Arc::clone(&coord);
            let barrier_c = Arc::clone(&barrier);
            handles.push(std::thread::spawn(move || {
                barrier_c.wait();
                let action = StoreWalAction::CreateCollection {
                    name: format!("z{tx}"),
                };
                coord_c
                    .append_actions(std::slice::from_ref(&action))
                    .expect("append_actions");
            }));
        }
        for h in handles {
            h.join().expect("writer thread");
        }

        // With zero window, fsync count is bounded above by WRITERS
        // (every caller might trigger its own drain) and below by 1
        // (the queue may still naturally batch under contention).
        // The point of the assertion is to confirm the env override
        // is wired through — the open() above used the env knob.
        let fsyncs = coord.fsync_count();
        assert!(fsyncs >= 1, "expected at least one fsync, got {fsyncs}");

        std::env::remove_var("REDDB_GROUP_COMMIT_WINDOW_US");
        drop(coord);
        let _ = std::fs::remove_file(&path);
    }

    /// `resolve_window` precedence: env > config.window_ms > default.
    #[test]
    fn resolve_window_precedence() {
        let _env = env_lock().lock().unwrap_or_else(|p| p.into_inner());
        // Default: window_ms=0 → adaptive 200µs floor.
        std::env::remove_var("REDDB_GROUP_COMMIT_WINDOW_US");
        let cfg = GroupCommitOptions::default();
        assert_eq!(
            StoreCommitCoordinator::resolve_window(&cfg),
            Duration::from_micros(DEFAULT_ADAPTIVE_WINDOW_US)
        );

        // Explicit ms config wins over the default floor.
        let cfg_ms = GroupCommitOptions {
            window_ms: 5,
            ..GroupCommitOptions::default()
        };
        assert_eq!(
            StoreCommitCoordinator::resolve_window(&cfg_ms),
            Duration::from_millis(5)
        );

        // Env override wins over both.
        std::env::set_var("REDDB_GROUP_COMMIT_WINDOW_US", "750");
        assert_eq!(
            StoreCommitCoordinator::resolve_window(&cfg),
            Duration::from_micros(750)
        );
        assert_eq!(
            StoreCommitCoordinator::resolve_window(&cfg_ms),
            Duration::from_micros(750)
        );

        // Env=0 explicitly disables the floor.
        std::env::set_var("REDDB_GROUP_COMMIT_WINDOW_US", "0");
        assert_eq!(
            StoreCommitCoordinator::resolve_window(&cfg),
            Duration::from_micros(0)
        );

        std::env::remove_var("REDDB_GROUP_COMMIT_WINDOW_US");
    }
}