fusee-server 1.0.0

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

mod generation;
mod leases;
mod log3;
mod queue;
mod remote;
pub(crate) use remote::{ReplicaStorage, WorkerSession};
mod snapshot;
#[cfg(test)]
mod tests;
mod timings;
mod transfer;
mod transport;
mod writes;
pub use timings::{StageDistribution, stage_summary};

use crate::proto;
use crate::security::{Acceptor, Connector, Purpose, Secured};
use fusee::engine::EnvFactory;
use fusee::env::{Env, Notify};
use fusee::local::{
    BatchWriter, FlushMethod, LocalEnv, LocalOptions, Preallocate, flush_file, sync_dir,
};
use fusee::{Config, Durability};
use fusee_core::Lsn;
use fusee_core::frame;
use fusee_core::record::{Payload, RecordBuf};
use fusee_core::segment::Batch;
use fusee_core::store::Checkpoint;
use fusee_core::vsr::{
    Action, Entry, Msg, Op, Recovered, Replica, ReplicaId, Timing, decode_entry, decode_msg,
    encode_entry, encode_msg,
};
use fusee_core::wire::{Reader, put_u64};
use std::collections::BTreeMap;
use std::fs::{File, OpenOptions};
use std::io::{self, BufWriter, Read, Write};
use std::os::unix::fs::FileExt;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use tokio::io::AsyncReadExt;
use tokio::sync::{mpsc, oneshot, watch};

#[derive(Clone, Debug)]
pub struct ReplicaConfig {
    pub id: ReplicaId,
    /// Every replica's `host:port`, indexed by replica ID (an empty string
    /// for an ID that is not a member). For a new group, its members; once
    /// the membership has changed, the saved membership takes precedence
    /// and this only says where to find the group.
    pub peers: Vec<String>,
    /// A new member of a running group: it starts with an empty directory,
    /// asks the members in `peers` for the membership that includes it
    /// (see [`ReplicaNode::change_members`]), then catches up. Ignored once
    /// the directory holds a membership.
    pub join: bool,
    pub dir: PathBuf,
    pub local: LocalOptions,
    pub timing: Timing,
    /// Mutual TLS and the token between members. With TLS, every member
    /// presents its certificate in both directions and verifies its peers'
    /// against the CA; with a token, every stream proves it before its
    /// first frame. A member refuses frames on a stream that did neither.
    pub security: crate::security::Security,
}

/// A replica's role and progress. Status receivers are notified when `view`,
/// `primary`, `ready` or `stopped` changes; the progress fields are updated
/// in place on every commit without waking anyone, so read them by polling.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct NodeStatus {
    pub view: u64,
    pub primary: bool,
    /// Primary, with every entry committed and applied: the engine may open.
    pub ready: bool,
    /// The replica stopped after an unrecoverable storage error.
    pub stopped: bool,
    /// The membership epoch this replica is in (see [`ReplicaNode::membership`]).
    pub epoch: u64,
    /// A membership change removed this replica: it takes part in nothing
    /// and can be stopped once the new members have caught up.
    pub retired: bool,
    /// A new member that has not yet learned the membership including it.
    pub joining: bool,
    pub op: Op,
    /// Committed prefix covered by a durable portable checkpoint.
    pub checkpoint: Op,
    pub commit: Op,
    pub applied: Op,
}

#[derive(Default)]
struct Traffic {
    timings: timings::Timings,
    sent: AtomicU64,
    received: AtomicU64,
}

/// Observe protocol progress and successful TCP application bytes on this
/// member. Byte counts include frame headers, retransmissions and heartbeats.
#[derive(Clone)]
pub struct ReplicaObserver {
    status: watch::Receiver<NodeStatus>,
    traffic: Arc<Traffic>,
}

impl ReplicaObserver {
    /// Enable per-member measurements. The normal protocol does not read
    /// a clock on these paths until a consumer requests timings.
    pub fn enable_timings(&self) {
        self.traffic.timings.enable();
    }

    pub fn timings(&self) -> BTreeMap<&'static str, StageDistribution> {
        self.traffic.timings.snapshot()
    }

    /// Wait for the protocol to stop, for example after a disk error.
    pub async fn wait_stopped(&mut self) {
        let _ = self.status.wait_for(|s| s.stopped).await;
    }

    /// Waits until the role, view, readiness or stop state changes (the
    /// progress fields change without notice). False once the node is gone.
    pub async fn changed(&mut self) -> bool {
        self.status.changed().await.is_ok()
    }

    pub fn snapshot(&self) -> (NodeStatus, u64, u64) {
        (
            *self.status.borrow(),
            self.traffic.sent.load(Ordering::Relaxed),
            self.traffic.received.load(Ordering::Relaxed),
        )
    }
}

#[derive(Default)]
struct Slot {
    callback: Option<Box<dyn FnOnce(io::Result<Lsn>) + Send>>,
    result: Option<io::Result<Lsn>>,
    notify: Option<Notify>,
}

struct Proposal {
    queued: Option<Instant>,
    control: Option<leases::Request>,
    body: Payload,
    last_lsn: Lsn,
    epoch: u64,
    slot: Arc<Mutex<Slot>>,
}

enum Pending {
    /// A permit command or a segment-group flush batch.
    Slot {
        return_session: bool,
        slot: Arc<Mutex<Slot>>,
        last_lsn: Lsn,
        proposed: Option<Instant>,
    },
    /// A log batch: each shard's last LSN in it.
    Sections {
        shards: Vec<(Arc<log3::ShardSlot>, Lsn)>,
        /// When it was proposed (group commit reads it; always set).
        at: Instant,
        proposed: Option<Instant>,
        /// Written to the backups' sockets (timings only).
        sent: Option<Instant>,
    },
}

impl Barrier {
    fn refuse(self) {
        let e = || other("replica lost the primary role");
        match self {
            Self::Checkpoint { reply, .. } => {
                let _ = reply.send(Err(e()));
            }
            Self::Recovery { reply, .. } => {
                let _ = reply.send(Err(e()));
            }
        }
    }
}

impl Pending {
    fn fail(self, e: &str) {
        match self {
            Self::Slot { slot, .. } => finish(&slot, Err(other(e))),
            Self::Sections { shards, .. } => {
                for (slot, _) in shards {
                    slot.fail();
                }
            }
        }
    }
}

/// What a group's data entries are, decided by its first engine.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Kind {
    /// No workflow data yet.
    Undecided,
    /// One shard, flush batches in log format 2, materialized into the
    /// engine's segment files (a segment group: dedicated servers, whose
    /// remote workers read those segments).
    Segments,
    /// Log-format-3 batches of an engine with this many shards.
    Log(u32),
}

impl Kind {
    fn encode(self) -> u64 {
        match self {
            Self::Undecided => 0,
            Self::Segments => 1,
            Self::Log(n) => 2 + u64::from(n),
        }
    }
    fn decode(v: u64) -> Self {
        match v {
            0 => Self::Undecided,
            1 => Self::Segments,
            n => Self::Log((n - 2) as u32),
        }
    }
}

/// The shard count an embedded engine of a new group uses.
fn default_shards() -> u32 {
    std::thread::available_parallelism()
        .map(|n| n.get().min(8) as u32)
        .unwrap_or(1)
}

/// A shard's synchronous request to the I/O thread.
enum Barrier {
    /// A segment group's shard is about to drop engine segments: the applied hint
    /// must be durable first.
    Checkpoint {
        epoch: u64,
        reply: std::sync::mpsc::SyncSender<io::Result<()>>,
    },
    /// A log group's shard opens: the committed log batches it recovers
    /// from, as of now.
    Recovery {
        epoch: u64,
        reply: std::sync::mpsc::SyncSender<io::Result<Arc<log3::Recovery>>>,
    },
}

// Proposals waiting for the protocol thread. A shard has at most one flush
// in flight; the rest are permit commands. Producers wait for room.
const PROPOSAL_QUEUE: usize = 1024;
// One checkpoint barrier per shard is in flight at a time.
const BARRIER_QUEUE: usize = 16;

struct Shared {
    status: watch::Sender<NodeStatus>,
    proposals: queue::Sender<Proposal>,
    barriers: queue::Sender<Barrier>,
    checkpoints: mpsc::Sender<(u64, snapshot::Sources)>,
    /// Changes whenever this node stops (or starts) being a ready primary;
    /// storage handles from an older epoch refuse to write.
    epoch: AtomicU64,
    engine_dir: Mutex<PathBuf>,
    local: LocalOptions,
    next_ticket: AtomicU64,
    traffic: Arc<Traffic>,
    /// [`Kind::encode`] of the group, set when this replica becomes the
    /// ready primary and by the first engine configuration.
    kind: AtomicU64,
    /// Sections staged by shards of a log group.
    batcher: log3::Batcher,
    /// Wakes the I/O thread: sections staged, a log write finished.
    wake: Arc<tokio::sync::Notify>,
    /// Bumped when the I/O thread wants every shard to checkpoint, so it
    /// can build a checkpoint image and trim the protocol log.
    checkpoint_round: AtomicU64,
    /// One bit per shard: the next image waits for this shard's checkpoint.
    checkpoint_wanted: AtomicU64,
    /// While this replica is the ready primary, the time ([`now_ms`]) until
    /// which it holds a read lease; 0 otherwise. Cleared before anything of
    /// a view change leaves this node.
    lease_until: AtomicU64,
    /// The current membership and every known member's address (index:
    /// member ID; empty: unknown).
    members: std::sync::RwLock<(fusee_core::vsr::Membership, Vec<String>)>,
    /// Dialers started after a membership change (aborted on drop).
    dialers: Mutex<BTreeMap<ReplicaId, tokio::task::JoinHandle<()>>>,
    /// Membership change requests for the I/O thread.
    reconfigure: Mutex<Vec<Vec<(ReplicaId, String)>>>,
    /// The I/O thread's protocol state, for a lone shard that proposes its
    /// own section while the I/O thread waits (see [`Core::inline_round`]).
    core: std::sync::OnceLock<std::sync::Weak<Mutex<Core>>>,
    /// Standby shards wait here for this member to become the ready
    /// primary (notified whenever the role, readiness or stop state changes).
    role: (Mutex<()>, std::sync::Condvar),
    _writer: File,
}

impl Shared {
    fn role_changed(&self) {
        let _guard = self.role.0.lock().unwrap_or_else(|e| e.into_inner());
        self.role.1.notify_all();
    }

    fn kind(&self) -> Kind {
        Kind::decode(self.kind.load(Ordering::Acquire))
    }

    /// The group's kind, deciding it as `fallback` if no data says yet.
    fn decide(&self, fallback: Kind) -> Kind {
        match self.kind.compare_exchange(
            Kind::Undecided.encode(),
            fallback.encode(),
            Ordering::AcqRel,
            Ordering::Acquire,
        ) {
            Ok(_) => fallback,
            Err(v) => Kind::decode(v),
        }
    }
}

/// One replica of a shard group.
pub struct ReplicaNode {
    shared: Arc<Shared>,
    status: watch::Receiver<NodeStatus>,
    tasks: Vec<tokio::task::JoinHandle<()>>,
    stop: Option<oneshot::Sender<()>>,
    core_thread: Option<std::thread::JoinHandle<()>>,
}

impl Drop for ReplicaNode {
    fn drop(&mut self) {
        if let Some(stop) = self.stop.take() {
            let _ = stop.send(());
        }
        for t in &self.tasks {
            t.abort();
        }
        for t in self
            .shared
            .dialers
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .values()
        {
            t.abort();
        }
        if let Some(thread) = self.core_thread.take()
            && thread.thread().id() != std::thread::current().id()
        {
            let _ = thread.join();
        }
    }
}

/// `e` with what was being done and to which path, so a storage error that
/// stops a replica says where it came from.
pub(super) fn annotate(e: io::Error, what: &str, path: &Path) -> io::Error {
    io::Error::new(e.kind(), format!("{what} {}: {e}", path.display()))
}

fn other(e: impl std::fmt::Display) -> io::Error {
    io::Error::other(e.to_string())
}

// ---------------------------------------------------------------- disk

struct Superblock {
    view: u64,
    last_normal: u64,
    commit: Op,
    applied: Op,
}

fn read_superblock(path: &Path) -> io::Result<Superblock> {
    let empty = Superblock {
        view: 0,
        last_normal: 0,
        commit: 0,
        applied: 0,
    };
    let bytes = match std::fs::read(path) {
        Ok(bytes) => bytes,
        Err(e) if e.kind() == io::ErrorKind::NotFound => {
            let log = path.with_file_name("log");
            match log.metadata() {
                Ok(meta) if meta.len() > 0 => {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "missing replica superblock for existing log",
                    ));
                }
                Err(e) if e.kind() != io::ErrorKind::NotFound => return Err(e),
                _ => return Ok(empty),
            }
        }
        Err(e) => return Err(e),
    };
    if bytes.len() != 36
        || crc32c::crc32c(&bytes[..32])
            != u32::from_le_bytes([bytes[32], bytes[33], bytes[34], bytes[35]])
    {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "damaged replica superblock",
        ));
    }
    let u = |i: usize| {
        let mut b = [0u8; 8];
        b.copy_from_slice(&bytes[i..i + 8]);
        u64::from_le_bytes(b)
    };
    if u(24) > u(16) || u(8) > u(0) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "inconsistent replica superblock",
        ));
    }
    Ok(Superblock {
        view: u(0),
        last_normal: u(8),
        commit: u(16),
        applied: u(24),
    })
}

/// The commit hint file of a log directory: the commit position, known
/// committed in the group and durable in this log when written. Rewritten
/// in place in the background (12 bytes, checksummed: a torn write reads as
/// absent), so the I/O thread never syncs a superblock just to keep it.
/// Recovery takes the higher of it and the superblock's commit.
const COMMIT_HINT: &str = "commit";

fn read_commit_hint(dir: &Path) -> Op {
    match std::fs::read(dir.join(COMMIT_HINT)) {
        Ok(b) if b.len() >= 12 => {
            let commit = u64::from_le_bytes(b[..8].try_into().unwrap());
            let crc = u32::from_le_bytes(b[8..12].try_into().unwrap());
            if crc32c::crc32c(&b[..8]) == crc {
                commit
            } else {
                0
            }
        }
        _ => 0,
    }
}

/// The membership file, in `vsr/` (it outlives log generations): the
/// committed reconfiguration entry that began the epoch and the members
/// before it, checksummed. Absent for a group that never changed.
const MEMBERS: &str = "members";

fn read_membership(vsr: &Path) -> io::Result<Option<fusee_core::vsr::Membership>> {
    let bytes = match std::fs::read(vsr.join(MEMBERS)) {
        Ok(b) => b,
        Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
        Err(e) => return Err(e),
    };
    let bad = || {
        io::Error::new(
            io::ErrorKind::InvalidData,
            "damaged replica membership file",
        )
    };
    if bytes.len() < 4 {
        return Err(bad());
    }
    let (body, crc) = bytes.split_at(bytes.len() - 4);
    if crc32c::crc32c(body) != u32::from_le_bytes(crc.try_into().unwrap()) {
        return Err(bad());
    }
    let mut r = Reader::new(body);
    let entry = decode_entry(&mut r).map_err(|_| bad())?;
    let previous = r.bytes().map_err(|_| bad())?.to_vec();
    let mut m = fusee_core::vsr::Membership::from_entry(&entry).ok_or_else(bad)?;
    m.previous = previous;
    Ok(Some(m))
}

fn write_membership(
    vsr: &Path,
    m: &fusee_core::vsr::Membership,
    method: FlushMethod,
) -> io::Result<()> {
    let Some(entry) = &m.entry else {
        return Ok(());
    };
    let mut b = Vec::new();
    encode_entry(entry, &mut b);
    fusee_core::wire::put_bytes(&mut b, &m.previous);
    let crc = crc32c::crc32c(&b);
    b.extend_from_slice(&crc.to_le_bytes());
    let tmp = vsr.join("members.tmp");
    {
        let mut f = File::create(&tmp)?;
        f.write_all(&b)?;
        flush_file(&f, method)?;
    }
    std::fs::rename(&tmp, vsr.join(MEMBERS))?;
    sync_dir(vsr, method)
}

/// Parses a member list: `host:port,...` (IDs are positions) or
/// `id=host:port,...`. Returns addresses indexed by ID (empty: no member).
pub fn parse_members(list: &str) -> io::Result<Vec<String>> {
    let bad = |m: &str| io::Error::new(io::ErrorKind::InvalidInput, m.to_string());
    let items: Vec<&str> = list
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect();
    let mut out = vec![String::new(); fusee_core::vsr::MAX_MEMBERS];
    let mut max = 0;
    for (i, item) in items.iter().enumerate() {
        let (id, addr) = match item.split_once('=') {
            Some((id, addr)) => (
                id.trim()
                    .parse::<usize>()
                    .map_err(|_| bad("a member ID must be a number"))?,
                addr.trim(),
            ),
            None => (i, *item),
        };
        if id >= fusee_core::vsr::MAX_MEMBERS || addr.is_empty() || !out[id].is_empty() {
            return Err(bad(
                "member IDs must be distinct, below 32, each with an address",
            ));
        }
        out[id] = addr.to_string();
        max = max.max(id + 1);
    }
    out.truncate(max);
    Ok(out)
}

fn encode_commit_hint(commit: Op) -> Vec<u8> {
    let mut b = commit.to_le_bytes().to_vec();
    b.extend_from_slice(&crc32c::crc32c(&b).to_le_bytes());
    b
}

struct VsrLog {
    dir: PathBuf,
    file: Arc<File>,
    method: FlushMethod,
    dirty: bool,
    end: u64,
    capacity: u64,
    /// `LocalOptions::direct_io`: write through `O_DIRECT` where the
    /// filesystem allows it (Linux).
    want_direct: bool,
    /// The `O_DIRECT` descriptor of `log`.
    direct: Option<Arc<File>>,
    /// Set for good once the filesystem refused an `O_DIRECT` write.
    refused: Arc<std::sync::atomic::AtomicBool>,
    /// Every write covers whole 4 KiB blocks (with `direct`; tests also set
    /// it alone): it starts at the block `end` is in and rewrites `tail`.
    aligned: bool,
    /// The bytes of the last partial block, from `end` rounded down to a
    /// block to `end`. Only the previous write put them there, and it has
    /// completed before the next write shares its block
    /// ([`writes::Writes`]), so rewriting them is exact.
    tail: Vec<u8>,
    /// Tests: the filesystem refuses every `O_DIRECT` write.
    #[cfg(test)]
    reject_direct: bool,
}

const BLOCK: usize = fusee_core::wal::BLOCK as usize;

// Overwrite prepared extents on the normal commit path. Extending i_size
// for every entry otherwise makes fdatasync commit filesystem metadata too.
const VSR_EXTENT: u64 = 1 << 20;

fn reserve_vsr(file: &File, capacity: u64, needed: u64) -> io::Result<u64> {
    if needed <= capacity {
        return Ok(capacity);
    }
    let target = needed
        .checked_next_multiple_of(VSR_EXTENT)
        .ok_or_else(|| other("replica log capacity overflow"))?;
    let zeros = [0u8; 64 << 10];
    let mut pos = capacity;
    while pos < target {
        let size = (target - pos).min(zeros.len() as u64) as usize;
        file.write_all_at(&zeros[..size], pos)?;
        pos += size as u64;
    }
    Ok(target)
}

/// Zero-fills `from..to` (block-aligned) through an `O_DIRECT` descriptor.
fn zero_direct(file: &File, from: u64, to: u64) -> io::Result<()> {
    const CHUNK: usize = 64 << 10;
    let zeros = fusee::local::AlignedBuf::new(CHUNK);
    let mut pos = from;
    while pos < to {
        let size = (to - pos).min(CHUNK as u64) as usize;
        file.write_all_at(&zeros.as_slice()[..size], pos)?;
        pos += size as u64;
    }
    Ok(())
}

/// Drops a file's clean cached pages, so `O_DIRECT` writes over bytes that
/// were read or written buffered do not first invalidate them.
#[cfg(target_os = "linux")]
fn drop_cached(file: &File) {
    use std::os::fd::AsRawFd;
    // SAFETY: an advisory call on a descriptor `file` owns; no pointers.
    let _ = unsafe { libc::posix_fadvise(file.as_raw_fd(), 0, 0, libc::POSIX_FADV_DONTNEED) };
}

#[cfg(not(target_os = "linux"))]
fn drop_cached(_file: &File) {}

impl VsrLog {
    fn empty(dir: &Path, method: FlushMethod) -> io::Result<Self> {
        let file = OpenOptions::new()
            .create_new(true)
            .write(true)
            .open(dir.join("log"))?;
        Ok(Self::with_file(dir, file, method, 0, 0))
    }

    fn with_file(dir: &Path, file: File, method: FlushMethod, end: u64, capacity: u64) -> Self {
        Self {
            dir: dir.to_owned(),
            file: Arc::new(file),
            method,
            dirty: false,
            end,
            capacity,
            want_direct: false,
            direct: None,
            refused: Arc::default(),
            aligned: false,
            tail: Vec::new(),
            #[cfg(test)]
            reject_direct: false,
        }
    }

    /// Writes go through `O_DIRECT` from now on if `want` (Linux, where the
    /// filesystem accepts it; else buffered, with one line on stderr).
    fn use_direct(&mut self, want: bool) -> io::Result<()> {
        self.want_direct = want;
        self.direct = None;
        self.aligned = false;
        self.tail.clear();
        if !want || !cfg!(target_os = "linux") || self.refused.load(Ordering::Acquire) {
            return Ok(());
        }
        let opened =
            fusee::local::with_direct(OpenOptions::new().write(true)).open(self.dir.join("log"));
        match opened {
            Ok(f) => self.direct = Some(Arc::new(f)),
            Err(e) if e.raw_os_error() == Some(libc::EINVAL) => {
                if !self.refused.swap(true, Ordering::AcqRel) {
                    fusee::local::note_direct_refused(&self.dir);
                }
                return Ok(());
            }
            Err(e) => return Err(e),
        }
        self.align()
    }

    /// Every write from now on covers whole blocks: keeps the last partial
    /// block, read back from the file.
    fn align(&mut self) -> io::Result<()> {
        let start = self.end - self.end % BLOCK as u64;
        let mut tail = vec![0; (self.end - start) as usize];
        if !tail.is_empty() {
            File::open(self.dir.join("log"))?.read_exact_at(&mut tail, start)?;
        }
        self.tail = tail;
        self.aligned = true;
        drop_cached(&self.file);
        Ok(())
    }

    /// Where background writes go.
    fn target(&self) -> writes::LogFile {
        writes::LogFile {
            file: self.file.clone(),
            direct: self.direct.clone(),
            method: self.method,
            refused: self.refused.clone(),
            dir: self.dir.clone(),
            #[cfg(test)]
            reject_direct: self.reject_direct,
        }
    }

    /// Prepares the log through `needed`: zero-filled whole extents, so
    /// writes on the commit path overwrite allocated blocks and change no
    /// metadata. Through `O_DIRECT` the zeros are flushed at once (no flush
    /// follows a write that carries `RWF_DSYNC`).
    fn reserve(&mut self, needed: u64) -> io::Result<()> {
        if needed <= self.capacity {
            return Ok(());
        }
        if let Some(d) = self
            .direct
            .clone()
            .filter(|_| !self.refused.load(Ordering::Acquire))
        {
            let target = needed
                .checked_next_multiple_of(VSR_EXTENT)
                .ok_or_else(|| other("replica log capacity overflow"))?;
            // Past the end of the file, the rest of its last block reads as
            // zeros; the next aligned write rewrites that block whole.
            let from = self.capacity.next_multiple_of(BLOCK as u64);
            match zero_direct(&d, from, target) {
                Err(e) if e.raw_os_error() == Some(libc::EINVAL) => {
                    if !self.refused.swap(true, Ordering::AcqRel) {
                        fusee::local::note_direct_refused(&self.dir);
                    }
                }
                Err(e) => return Err(e),
                Ok(()) => {
                    flush_file(&d, self.method)?;
                    self.capacity = target;
                    return Ok(());
                }
            }
        }
        self.capacity = reserve_vsr(&self.file, self.capacity, needed)?;
        Ok(())
    }
    fn frame(e: &Entry, out: &mut Vec<u8>) {
        let mut body = Vec::with_capacity(e.body.len() + 32);
        encode_entry(e, &mut body);
        out.extend_from_slice(&(body.len() as u32).to_le_bytes());
        out.extend_from_slice(&crc32c::crc32c(&body).to_le_bytes());
        out.extend_from_slice(&body);
    }

    /// Opens the log and reads it back: entries, and a damaged range if a
    /// record in the middle fails its checksum while later ones are intact.
    fn open(
        dir: &Path,
        method: FlushMethod,
        committed: Op,
        base: Op,
        base_checksum: u64,
    ) -> io::Result<(Self, Vec<Entry>, Option<(Op, Op)>)> {
        std::fs::create_dir_all(dir)?;
        let path = dir.join("log");
        let mut bytes = Vec::new();
        match File::open(&path) {
            Ok(mut f) => {
                f.read_to_end(&mut bytes)?;
            }
            Err(e) if e.kind() == io::ErrorKind::NotFound => {}
            Err(e) => return Err(e),
        }
        let mut entries = Vec::new();
        let mut pos = 0usize;
        let mut damaged = None;
        let mut valid_end = 0usize;
        while pos + 8 <= bytes.len() {
            let len =
                u32::from_le_bytes([bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]])
                    as usize;
            let crc = u32::from_le_bytes([
                bytes[pos + 4],
                bytes[pos + 5],
                bytes[pos + 6],
                bytes[pos + 7],
            ]);
            let body = bytes.get(pos + 8..pos + 8 + len);
            let ok = body.is_some_and(|b| crc32c::crc32c(b) == crc);
            if let (true, Some(b)) = (ok, body)
                && let Ok(e) = decode_entry(&mut Reader::new(b))
            {
                let (prior, hash) = entries
                    .last()
                    .map_or((base, base_checksum), |p: &Entry| (p.op, p.checksum));
                if e.op <= prior || e.op == Op::MAX {
                    return Err(other("out-of-order replica log entry"));
                }
                let expected = prior + 1;
                if e.op > expected {
                    damaged = Some((damaged.map_or(expected, |(first, _)| first), e.op - 1));
                } else if e.parent != hash {
                    // A checksummed frame from a different chain does not
                    // authenticate a prepared position. Keep the raw file
                    // until peer repair supplies the correct bytes.
                    damaged = Some((damaged.map_or(expected, |(first, _)| first), e.op));
                    pos += 8 + len;
                    valid_end = pos;
                    continue;
                }
                entries.push(e);
                pos += 8 + len;
                valid_end = pos;
                continue;
            }
            if len == 0 && bytes[pos..].iter().all(|b| *b == 0) {
                break; // the unused, zero-filled suffix
            }
            let block = pos.next_multiple_of(BLOCK);
            if block > pos && bytes[pos..block.min(bytes.len())].iter().all(|b| *b == 0) {
                // A write placed while another was in flight is padded
                // with zeros to the end of its block. Padding shorter than
                // a length field reads, with the next frame's first bytes,
                // as a nonzero length: no valid frame starts here (it would
                // have been taken above), so the zeros are padding.
                pos = block;
                continue;
            }
            // Invalid frame: look for a later valid one (damage) or stop (torn).
            let expected = entries.last().map(|e: &Entry| e.op + 1).unwrap_or(base + 1);
            let mut later = None;
            let mut p = pos + 1;
            while p + 8 <= bytes.len() {
                let l = u32::from_le_bytes([bytes[p], bytes[p + 1], bytes[p + 2], bytes[p + 3]])
                    as usize;
                let c =
                    u32::from_le_bytes([bytes[p + 4], bytes[p + 5], bytes[p + 6], bytes[p + 7]]);
                if let Some(b) = bytes.get(p + 8..p + 8 + l)
                    && l > 0
                    && crc32c::crc32c(b) == c
                    && let Ok(e) = decode_entry(&mut Reader::new(b))
                    && e.op > expected
                {
                    later = Some((p, e.op));
                    break;
                }
                p += 1;
            }
            match later {
                Some((p, op)) => {
                    damaged = Some((damaged.map_or(expected, |(start, _)| start), op - 1));
                    pos = p;
                }
                None => break,
            }
        }
        let last = entries.last().map_or(base, |e| e.op);
        if last < committed {
            // The superblock survived the flush for these entries. Keep the
            // evidence until repair installs a peer's committed contents.
            damaged = Some((damaged.map_or(last + 1, |(start, _)| start), committed));
        }
        if damaged.is_none() && bytes[valid_end..].iter().any(|b| *b != 0) {
            // A torn tail: never acknowledged.
            let f = OpenOptions::new().write(true).open(&path)?;
            f.set_len(valid_end as u64)?;
            flush_file(&f, method)?;
        }
        let file = OpenOptions::new()
            .create(true)
            .truncate(false)
            .write(true)
            .open(&path)?;
        let capacity = file.metadata()?.len();
        Ok((
            Self::with_file(dir, file, method, valid_end as u64, capacity),
            entries,
            damaged,
        ))
    }

    /// The entries after `after` (op, checksum) in the log file at `path`,
    /// read from its first `end` bytes: the chain from `after` as far as it
    /// is intact and continuous. Runs off the I/O thread (a staged
    /// generation's tail), so it reads what the file holds rather than
    /// cloning the replica's entries: a frame still being written, a torn
    /// one or a gap ends the tail, and the caller takes everything after
    /// it from memory.
    fn read_tail(path: &Path, after: (Op, u64), end: u64) -> io::Result<Vec<Entry>> {
        Self::read_tail_from(path, after, end, 8 << 20)
    }

    /// [`read_tail`](Self::read_tail), first reading the last `window` bytes.
    fn read_tail_from(
        path: &Path,
        after: (Op, u64),
        end: u64,
        mut window: u64,
    ) -> io::Result<Vec<Entry>> {
        // The tail is the end of the file: read a window there, widening it
        // until it starts at or before the entry after `after` (a log of a
        // gigabyte took the staging thread most of a second to read whole).
        let file = File::open(path)?;
        loop {
            let start = end.saturating_sub(window);
            let mut bytes = vec![0; (end - start) as usize];
            file.read_exact_at(&mut bytes, start)?;
            // Mid-file, the window starts inside some frame: resume at the
            // first checksummed one.
            let first = if start == 0 {
                Some(0)
            } else {
                Self::next_frame(&bytes, 0)
            };
            let (tail, began_after) = match first {
                Some(pos) => Self::scan_tail(&bytes, start, pos, after),
                None => (Vec::new(), false),
            };
            if start == 0 || began_after || !tail.is_empty() {
                return Ok(tail);
            }
            window = window.saturating_mul(4);
        }
    }

    /// The first position at or after `from` where a whole frame with a
    /// valid checksum and entry starts.
    fn next_frame(bytes: &[u8], from: usize) -> Option<usize> {
        let mut p = from;
        while p + 8 <= bytes.len() {
            let len = u32::from_le_bytes(bytes[p..p + 4].try_into().unwrap()) as usize;
            if len > 0 && p + 8 + len <= bytes.len() {
                let crc = u32::from_le_bytes(bytes[p + 4..p + 8].try_into().unwrap());
                let body = &bytes[p + 8..p + 8 + len];
                if crc32c::crc32c(body) == crc && decode_entry(&mut Reader::new(body)).is_ok() {
                    return Some(p);
                }
            }
            p += 1;
        }
        None
    }

    /// The chain after `after` in `bytes` (the file from offset `base`)
    /// from `pos`, and whether the scan met an entry at or before `after`
    /// (so the window held the tail's start).
    fn scan_tail(bytes: &[u8], base: u64, mut pos: usize, after: (Op, u64)) -> (Vec<Entry>, bool) {
        let (mut next, mut parent) = (after.0.saturating_add(1), after.1);
        let mut tail = Vec::new();
        let mut began = false;
        while pos + 8 <= bytes.len() {
            let len = u32::from_le_bytes(bytes[pos..pos + 4].try_into().unwrap()) as usize;
            let crc = u32::from_le_bytes(bytes[pos + 4..pos + 8].try_into().unwrap());
            let body = bytes
                .get(pos + 8..pos + 8 + len)
                .filter(|b| len > 0 && crc32c::crc32c(b) == crc);
            let Some(body) = body else {
                // Zero padding to the end of a block of the file (a write
                // placed beside another), or the end of what is written.
                let at = base + pos as u64;
                let block = (at.next_multiple_of(BLOCK as u64) - base) as usize;
                if block > pos && bytes[pos..block.min(bytes.len())].iter().all(|b| *b == 0) {
                    pos = block;
                    continue;
                }
                break;
            };
            let e = match decode_entry(&mut Reader::new(body)) {
                Ok(e) => e,
                Err(_) => break,
            };
            pos += 8 + len;
            if e.op < next {
                began = true;
                if e.op == after.0 && e.checksum != after.1 {
                    // Not the chain the caller holds.
                    return (Vec::new(), true);
                }
                continue;
            }
            if e.op != next || e.parent != parent {
                break;
            }
            began = true;
            parent = e.checksum;
            next += 1;
            tail.push(e);
        }
        (tail, began)
    }

    /// Appends entries at the end on this thread (a staged generation's
    /// catch-up; the replica's own appends go through `writes`).
    fn append(&mut self, entries: &[Entry]) -> io::Result<()> {
        let mut out = Vec::new();
        for e in entries {
            Self::frame(e, &mut out);
        }
        self.write_frames(out)
    }

    /// Writes framed entries at the end on this thread: durable when it
    /// returns with `O_DIRECT` and `RWF_DSYNC`, else at the next sync.
    fn write_frames(&mut self, frames: Vec<u8>) -> io::Result<()> {
        let p = self.place(frames, false)?;
        if !self.target().write(p.bytes.as_slice(), p.offset)? {
            self.dirty = true;
        }
        Ok(())
    }

    /// Positions bytes at the end of the log, in prepared extents. `pad`:
    /// another write may be in flight with this one, so the bytes are
    /// padded to the end of their last block, as the shared log pads a
    /// pipelined batch ([`fusee_core::wal::batch_span`]). Aligned writes
    /// start at the block the end is in, with the kept tail before the
    /// bytes, and run to the end of their last block.
    fn place(&mut self, mut bytes: Vec<u8>, pad: bool) -> io::Result<writes::Placement> {
        if self.direct.is_some() && self.refused.load(Ordering::Acquire) {
            // The filesystem refused an O_DIRECT write: buffered from now on.
            self.direct = None;
            self.aligned = false;
            self.tail.clear();
        }
        let offset = self.end;
        let len = bytes.len();
        let span = if pad {
            fusee_core::wal::batch_span(offset, len, true)
        } else {
            len as u64
        };
        let next = offset
            .checked_add(span)
            .ok_or_else(|| other("replica log length overflow"))?;
        let padded = next.is_multiple_of(BLOCK as u64);
        if !self.aligned {
            bytes.resize(span as usize, 0);
            self.reserve(next)?;
            self.end = next;
            return Ok(writes::Placement {
                offset,
                bytes: writes::WriteBuf::Plain(bytes),
                padded,
            });
        }
        let start = offset - self.tail.len() as u64;
        let total = self.tail.len() + span as usize;
        let whole = total.next_multiple_of(BLOCK);
        self.reserve(start + whole as u64)?;
        self.end = next;
        let mut buf = fusee::local::AlignedBuf::new(whole);
        let s = buf.as_mut_slice();
        s[..self.tail.len()].copy_from_slice(&self.tail);
        s[self.tail.len()..self.tail.len() + len].copy_from_slice(&bytes);
        let keep = (next % BLOCK as u64) as usize;
        self.tail = s[total - keep..total].to_vec();
        Ok(writes::Placement {
            offset: start,
            bytes: writes::WriteBuf::Aligned { buf, len: whole },
            padded,
        })
    }

    fn sync(&mut self) -> io::Result<()> {
        if self.dirty {
            flush_file(&self.file, self.method)?;
            self.dirty = false;
        }
        Ok(())
    }

    /// Atomically replaces the whole log.
    fn replace<'a>(&mut self, entries: impl Iterator<Item = &'a Entry>) -> io::Result<()> {
        let tmp = self.dir.join("log.tmp");
        let mut end = 0u64;
        let capacity = {
            let f = File::create(&tmp)?;
            let mut frame = Vec::new();
            {
                let mut stream = BufWriter::with_capacity(1 << 20, &f);
                for entry in entries {
                    frame.clear();
                    Self::frame(entry, &mut frame);
                    stream.write_all(&frame)?;
                    end = end
                        .checked_add(frame.len() as u64)
                        .ok_or_else(|| other("replica log length overflow"))?;
                }
                stream.flush()?;
            }
            let capacity = reserve_vsr(
                &f,
                end,
                end.max(1)
                    .checked_next_multiple_of(VSR_EXTENT)
                    .ok_or_else(|| other("replica log capacity overflow"))?,
            )?;
            flush_file(&f, self.method)?;
            capacity
        };
        std::fs::rename(&tmp, self.dir.join("log"))?;
        sync_dir(&self.dir, self.method)?;
        self.file = Arc::new(OpenOptions::new().write(true).open(self.dir.join("log"))?);
        self.end = end;
        self.capacity = capacity;
        self.dirty = false;
        let aligned = self.aligned && self.direct.is_none();
        self.use_direct(self.want_direct)?;
        if aligned {
            self.align()?;
        }
        Ok(())
    }

    fn save(&mut self, sb: &Superblock) -> io::Result<()> {
        // Barrier: the log this superblock vouches for is durable first.
        self.sync()?;
        let mut b = Vec::with_capacity(36);
        put_u64(&mut b, sb.view);
        put_u64(&mut b, sb.last_normal);
        put_u64(&mut b, sb.commit);
        put_u64(&mut b, sb.applied);
        let crc = crc32c::crc32c(&b);
        b.extend_from_slice(&crc.to_le_bytes());
        let tmp = self.dir.join("superblock.tmp");
        {
            let mut f = File::create(&tmp)?;
            f.write_all(&b)?;
            flush_file(&f, self.method)?;
        }
        std::fs::rename(&tmp, self.dir.join("superblock"))?;
        sync_dir(&self.dir, self.method)
    }
}

// ---------------------------------------------------------------- node

/// The protocol's clock, in milliseconds since this process first read it.
/// Election and heartbeat intervals must survive wall-clock corrections, so
/// it is monotonic; read leases assume its rate is within
/// [`fusee_core::vsr::MAX_CLOCK_DRIFT_PPM`] of real time, so it also counts
/// time the host spends suspended: it advances with the monotonic clock,
/// plus however far, at most, `CLOCK_BOOTTIME` (Linux; it counts
/// suspension, as macOS's `CLOCK_MONOTONIC` already does) has run ahead of
/// it. A suspension is counted, and a boot clock that some tool moves back
/// (libfaketime fakes it) neither moves this clock back nor stops it.
fn now_ms() -> u64 {
    static START: std::sync::OnceLock<(u64, u64)> = std::sync::OnceLock::new();
    static AHEAD: AtomicU64 = AtomicU64::new(0);
    static LAST: AtomicU64 = AtomicU64::new(0);
    let (mono, boot) = (clock_ms(libc::CLOCK_MONOTONIC), boot_ms());
    let start = *START.get_or_init(|| (mono.unwrap_or(0), boot.unwrap_or(0)));
    let elapsed = mono.map_or(0, |m| m.saturating_sub(start.0));
    let ahead = boot.map_or(0, |b| b.saturating_sub(start.1).saturating_sub(elapsed));
    let now = elapsed + AHEAD.fetch_max(ahead, Ordering::Relaxed).max(ahead);
    // Never back, even if a read fails.
    LAST.fetch_max(now, Ordering::Relaxed).max(now)
}

#[cfg(target_os = "linux")]
fn boot_ms() -> Option<u64> {
    clock_ms(libc::CLOCK_BOOTTIME)
}

#[cfg(not(target_os = "linux"))]
fn boot_ms() -> Option<u64> {
    None
}

fn clock_ms(clock: libc::clockid_t) -> Option<u64> {
    let mut ts = libc::timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };
    // SAFETY: `ts` is a valid timespec for the call to fill in.
    (unsafe { libc::clock_gettime(clock, &mut ts) } == 0)
        .then(|| ts.tv_sec as u64 * 1000 + ts.tv_nsec as u64 / 1_000_000)
}

impl ReplicaNode {
    /// Starts the replica: recovers its protocol log, re-applies committed
    /// batches to its engine directory, and joins the group.
    pub async fn start(cfg: ReplicaConfig) -> io::Result<Self> {
        use fusee_core::vsr::{MAX_GROUP, MAX_MEMBERS, Membership};
        let invalid = |m: &str| io::Error::new(io::ErrorKind::InvalidInput, m.to_string());
        let id = usize::from(cfg.id);
        if id >= MAX_MEMBERS
            || cfg.peers.get(id).is_none_or(String::is_empty)
            || cfg.peers.len() > MAX_MEMBERS
        {
            return Err(invalid(
                "this replica's ID must be below 32 and have an address among the peers",
            ));
        }
        cfg.timing.validate().map_err(invalid)?;
        let vsr_dir = cfg.dir.join("vsr");
        let saved = read_membership(&vsr_dir)?;
        let named = cfg.peers.iter().filter(|p| !p.is_empty()).count();
        if saved.is_none() && !cfg.join && !(1..=MAX_GROUP).contains(&named) {
            return Err(invalid("a group has 1 to 9 replicas"));
        }
        if saved.is_none() && !cfg.join && cfg.peers.iter().take(named).any(String::is_empty) {
            return Err(invalid(
                "a new group's members have IDs 0, 1, 2, ...; give every one an address",
            ));
        }
        if saved.is_none()
            && cfg.join
            && std::fs::metadata(vsr_dir.join("log")).is_ok_and(|m| m.len() > 0)
        {
            return Err(invalid(
                "a joining replica starts with an empty directory; this one holds a replica log",
            ));
        }
        // Addresses by member ID: the configured ones, then the saved
        // membership's (which win once the membership has changed).
        let mut addresses = cfg.peers.clone();
        addresses.resize(MAX_MEMBERS, String::new());
        if let Some(m) = &saved {
            for (member, addr) in m.addresses() {
                addresses[usize::from(member)] = addr;
            }
        }
        let joining = saved.is_none() && cfg.join;
        let membership = match &saved {
            Some(m) => m.clone(),
            None if joining => Membership {
                epoch: 0,
                members: (0..MAX_MEMBERS as u8)
                    .filter(|m| !addresses[usize::from(*m)].is_empty())
                    .collect(),
                entry: None,
                previous: Vec::new(),
            },
            None => Membership::initial(named as u8),
        };
        let n = membership.members.len() as u8;
        let acceptor = Arc::new(Acceptor::new(&cfg.security, Purpose::Replica)?);
        let connector = Arc::new(Connector::new(&cfg.security, Purpose::Replica, cfg.id)?);
        let writer_lock = fusee::local::lock_directory(&cfg.dir.join("engine"))?;
        std::fs::create_dir_all(&vsr_dir)?;
        sync_dir(&cfg.dir, cfg.local.flush)?;
        let current = generation::Generation::read(&vsr_dir)?;
        let engine_dir = cfg
            .dir
            .join(current.as_ref().map_or("engine", |g| &g.engine));
        let log_dir = current
            .as_ref()
            .map_or_else(|| vsr_dir.clone(), |g| g.log_dir(&vsr_dir));
        let (base, base_checksum) = current
            .as_ref()
            .map_or((0, 0), |g| (g.image.op, g.image.checksum));
        let shard_dir = engine_dir.join("shard-000");
        std::fs::create_dir_all(&shard_dir)?;
        let mut local = cfg.local.clone();
        // Two writers touch the segment files (the applier and the engine's
        // recovery); neither may zero-fill over the other's data.
        local.preallocate = Preallocate::None;
        local.direct_io = false;
        let mut sb = read_superblock(&log_dir.join("superblock"))?;
        sb.commit = sb.commit.max(read_commit_hint(&log_dir));
        if sb.applied < base || sb.commit < base {
            return Err(other("replica superblock precedes checkpoint"));
        }
        let (mut log, entries, damaged) =
            VsrLog::open(&log_dir, local.flush, sb.commit, base, base_checksum)?;
        // The protocol log is written only by this I/O thread and its
        // helpers, so it takes `direct_io` as configured (the engine's own
        // files above do not).
        log.use_direct(cfg.local.direct_io)?;
        let mut quarantine = false;
        let ledger_bytes = if let Some(g) = &current {
            generation::retire_prefix_log(&vsr_dir, local.flush)?;
            let restore = || -> io::Result<Vec<u8>> {
                let ledger = g.ledger(&vsr_dir)?;
                snapshot::rebuild(
                    &vsr_dir.join(g.image.name()),
                    &g.image,
                    &engine_dir,
                    &ledger,
                    local.flush,
                )?;
                Ok(ledger)
            };
            match restore() {
                Ok(ledger) => ledger,
                Err(e)
                    if matches!(
                        e.kind(),
                        io::ErrorKind::NotFound
                            | io::ErrorKind::UnexpectedEof
                            | io::ErrorKind::InvalidData
                            | io::ErrorKind::Other
                    ) =>
                {
                    // The saved view and committed watermark remain authoritative.
                    // Refuse to vote or apply anything until an eligible donor
                    // supplies a verified replacement covering this checkpoint.
                    eprintln!(
                        "Fusee replica {} is fetching its damaged checkpoint: {e}",
                        cfg.id
                    );
                    quarantine = true;
                    leases::Ledger::default().snapshot()?
                }
                Err(e) => return Err(e),
            }
        } else {
            leases::Ledger::default().snapshot()?
        };
        let base_ledger = leases::Ledger::restore(&ledger_bytes)?;
        let (base, base_checksum, damaged) = if quarantine {
            (0, 0, Some((1, base)))
        } else {
            (base, base_checksum, damaged)
        };
        let mut writer = BatchWriter::new(&shard_dir.join("log"), local.clone())?;
        // Re-apply committed batches the engine directory may have lost.
        let commit = sb.commit;
        let mut applied = if current.is_some() { base } else { sb.applied };
        {
            let from = applied;
            let through = damaged.map_or(commit, |(first, _)| commit.min(first.saturating_sub(1)));
            for e in entries.iter().filter(|e| e.op > from && e.op <= through) {
                if is_segment_batch(&e.body) {
                    let batch = proto::decode_batch(&mut Reader::new(&e.body)).map_err(other)?;
                    writer.write(&batch)?;
                }
                applied = e.op;
            }
        }
        let rec = Recovered {
            view: sb.view,
            last_normal: sb.last_normal,
            commit,
            entries,
            base_checksum,
            base,
            damaged,
            membership: Some(membership.clone()),
            joining,
        };
        let seed = fusee::local::unix_ms() ^ (u64::from(cfg.id) << 48);
        let replica = Replica::new(cfg.id, n, rec, cfg.timing.clone(), seed, now_ms());
        if !quarantine {
            log.save(&Superblock {
                view: replica.view(),
                last_normal: sb.last_normal,
                commit: replica.commit(),
                applied,
            })?;
        }
        if damaged.is_none() && log.capacity == 0 {
            // First install the empty superblock, so a crash during preparation
            // cannot leave an ambiguous nonempty log without its superblock.
            log.reserve(1)?;
            flush_file(&log.file, log.method)?;
        }
        let (status_tx, status_rx) = watch::channel(NodeStatus::default());
        let (ptx, prx) = queue::bounded(PROPOSAL_QUEUE);
        let (btx, brx) = queue::bounded(BARRIER_QUEUE);
        let (checkpoints, captures) = mpsc::channel(64);
        let wake = Arc::new(tokio::sync::Notify::new());
        let shared = Arc::new(Shared {
            status: status_tx,
            proposals: ptx,
            barriers: btx,
            checkpoints,
            epoch: AtomicU64::new(1),
            engine_dir: Mutex::new(engine_dir.clone()),
            local: local.clone(),
            next_ticket: AtomicU64::new(1),
            traffic: Arc::new(Traffic::default()),
            kind: AtomicU64::new(Kind::Undecided.encode()),
            batcher: log3::Batcher::default(),
            wake: wake.clone(),
            checkpoint_round: AtomicU64::new(0),
            checkpoint_wanted: AtomicU64::new(0),
            lease_until: AtomicU64::new(0),
            members: std::sync::RwLock::new((membership.clone(), addresses.clone())),
            dialers: Mutex::new(BTreeMap::new()),
            reconfigure: Mutex::new(Vec::new()),
            core: std::sync::OnceLock::new(),
            role: (Mutex::new(()), std::sync::Condvar::new()),
            _writer: writer_lock,
        });
        let log_io = writes::LogIo::start(wake.clone())?;
        // The I/O thread: one current-thread runtime that reads every peer
        // socket, runs the protocol and owns the log.
        let protocol_runtime = tokio::runtime::Builder::new_current_thread()
            .enable_io()
            .enable_time()
            .build()?;
        let tasks: Vec<tokio::task::JoinHandle<()>> = Vec::new();
        let listener = tokio::net::TcpListener::bind(&addresses[usize::from(cfg.id)]).await?;
        let listener = {
            let _owner = protocol_runtime.enter();
            tokio::net::TcpListener::from_std(listener.into_std()?)?
        };
        if let Ok(addr) = listener.local_addr() {
            crate::security::warn_if_exposed(
                "replica",
                addr,
                acceptor.has_tls(),
                acceptor.has_token(),
            );
        }
        let (admitted_tx, admitted) = mpsc::unbounded_channel();
        let inbox = Inbox {
            tx: admitted_tx,
            acceptor,
            shared: shared.clone(),
            me: cfg.id,
            handshakes: tokio::runtime::Handle::current(),
        };
        let dialing = Dialing {
            handle: tokio::runtime::Handle::current(),
            connector,
            me: cfg.id,
        };
        let mut senders = BTreeMap::new();
        dialing.dial(&shared, &mut senders);
        let core = Core {
            id: cfg.id,
            replica,
            log,
            writes: writes::Writes::new(log_io, local.emulated_flush),
            writer,
            applied,
            staged_since_hint: false,
            senders,
            outbox: vec![Vec::new(); MAX_MEMBERS],
            prepare_cache: None,
            queued: Vec::new(),
            pending: BTreeMap::new(),
            leases: base_ledger,
            lease_applied: base,
            checkpoints: transfer::Checkpoints::new(
                vsr_dir.clone(),
                current,
                ledger_bytes,
                quarantine,
                n,
            ),
            session: None,
            shared: shared.clone(),
            ready: false,
            last_hint: now_ms(),
            commit_hint: (0, None),
            recovery: None,
            released: false,
            round_ns: 100_000,
            last_entry_at: None,
            halted: None,
            dialing,
            vsr_dir: vsr_dir.clone(),
            read_at: None,
            unsent: Vec::new(),
            durable_at: None,
            ack_queued: false,
        };
        let core: CoreCell = Arc::new(Mutex::new(core));
        let _ = shared.core.set(Arc::downgrade(&core));
        let (stop, stopped) = oneshot::channel();
        let thread = match std::thread::Builder::new()
            .name(format!("fusee-replica-{}", cfg.id))
            .spawn(move || {
                queue::mark_consumer();
                let local = tokio::task::LocalSet::new();
                local.block_on(&protocol_runtime, async move {
                    tokio::task::spawn_local(listen(listener, inbox));
                    tokio::task::spawn_local(read_admitted(admitted, core.clone()));
                    drive(core, prx, brx, captures, stopped).await;
                })
            }) {
            Ok(thread) => thread,
            Err(e) => {
                for task in tasks {
                    task.abort();
                }
                return Err(e);
            }
        };
        Ok(Self {
            shared,
            status: status_rx,
            tasks,
            stop: Some(stop),
            core_thread: Some(thread),
        })
    }

    pub(crate) fn subscribe(&self) -> watch::Receiver<NodeStatus> {
        self.status.clone()
    }

    pub fn observer(&self) -> ReplicaObserver {
        ReplicaObserver {
            status: self.status.clone(),
            traffic: self.shared.traffic.clone(),
        }
    }

    pub fn status(&self) -> NodeStatus {
        *self.status.borrow()
    }

    /// Resolves once this replica is primary with everything committed and
    /// applied, so an engine opened now sees the whole committed log.
    pub async fn wait_ready(&mut self) {
        let _ = self.status.wait_for(|s| s.ready || s.stopped).await;
    }

    /// Resolves once this replica is no longer a ready primary.
    pub async fn wait_lost(&mut self) {
        let _ = self.status.wait_for(|s| !s.ready).await;
    }

    /// The engine configuration for this replica while it is primary. Call
    /// it after [`wait_ready`](Self::wait_ready): its storage refuses writes
    /// once this replica stops being the ready primary.
    ///
    /// The engine has the group's shard count: the one its log was written
    /// with, or for a new group one per core up to eight. Keep it: records
    /// of a shard the engine does not open would never be replayed.
    /// A hot-standby engine configuration: an engine opened with it on a
    /// backup (or at any time) opens at once with every shard standing by
    /// (threads, runtimes, open run stores, registered workflows), and its
    /// shards start the moment this replica becomes the ready primary: each
    /// recovers from the committed log batches the I/O thread holds in
    /// memory and the files it already has open, with no engine to open.
    /// Calls wait meanwhile. Like an engine from
    /// [`engine_config`](Self::engine_config) it stops when this replica
    /// loses the primary role: shut it down and open another standby
    /// engine. Log groups only (a segment group's shard refuses to stand by).
    ///
    /// ```text
    /// loop {
    ///     let engine = Engine::open(node.standby_config()).await?; // at once
    ///     register_workflows(&engine);
    ///     node.wait_ready().await;   // promoted: the shards are running
    ///     node.wait_lost().await;
    ///     let _ = engine.shutdown().await;
    /// }
    /// ```
    pub fn standby_config(&self) -> Config {
        let storage = ReplicaStorage::new(self);
        let shards = match self.shared.kind() {
            Kind::Log(n) => n,
            _ => default_shards(),
        };
        // The primary epoch the engine's shards are promoted in: one, for
        // every shard and the permit backend alike.
        let tenure = Arc::new(AtomicU64::new(0));
        let limits = leases::Backend::standby(self.shared.clone(), tenure.clone());
        let factory: EnvFactory = Arc::new(move |shard| {
            let standby = Standby {
                shards,
                tenure: tenure.clone(),
            };
            Ok(Box::new(storage.open_standby(shard, standby)?) as Box<dyn Env>)
        });
        let mut cfg = Config::memory();
        cfg.durability = Durability::Custom(factory);
        cfg.shards = Some(shards);
        cfg.local = self.shared.local.clone();
        cfg.limits = Some(Arc::new(limits));
        cfg
    }

    pub fn engine_config(&self) -> Config {
        let shared = self.shared.clone();
        let epoch = shared.epoch.load(Ordering::Acquire);
        let storage = ReplicaStorage::new(self);
        let kind = shared.decide(Kind::Log(default_shards()));
        let shards = match kind {
            Kind::Log(n) => n,
            _ => 1,
        };
        let factory: EnvFactory = Arc::new(move |shard| {
            let env = match kind {
                Kind::Log(n) => storage.open_log(shard, n, epoch)?,
                _ => storage.open(shard, epoch)?,
            };
            Ok(Box::new(env) as Box<dyn Env>)
        });
        let mut cfg = Config::memory();
        cfg.durability = Durability::Custom(factory);
        cfg.shards = Some(shards);
        cfg.local = self.shared.local.clone();
        cfg.limits = Some(Arc::new(leases::Backend::new(self.shared.clone(), epoch)));
        cfg
    }

    /// Whether this replica is the ready primary and holds a read lease now:
    /// a quorum answered its ping recently enough that no other member can
    /// lead a newer view yet, so reads of its engine (queries, results,
    /// inspect, list) see every write the group acknowledged. The lease is
    /// renewed with every heartbeat and lapses about
    /// [`Timing::lease_ms`] after the last quorum of answers.
    pub fn lease_valid(&self) -> bool {
        self.status.borrow().ready && now_ms() < self.shared.lease_until.load(Ordering::Acquire)
    }

    /// For a read that must not be stale: returns at once under a lease,
    /// else once a no-op commits through a quorum (one round trip). Fails
    /// if this replica is not the ready primary or no quorum answers; the
    /// read must then not be served from this replica.
    pub async fn confirm_reads(&self) -> io::Result<()> {
        ReplicaStorage::new(self).confirm_quorum().await
    }

    /// The membership epoch this replica is in, and its members with their
    /// addresses, in order (the first leads the epoch's first view).
    pub fn membership(&self) -> (u64, Vec<(ReplicaId, String)>) {
        let m = self
            .shared
            .members
            .read()
            .unwrap_or_else(|e| e.into_inner());
        (
            m.0.epoch,
            m.0.members
                .iter()
                .map(|id| (*id, m.1[usize::from(*id)].clone()))
                .collect(),
        )
    }

    /// Changes the group's membership to `members` (each ID with its
    /// address), from any member: the primary proposes it as the last entry
    /// of the current epoch; once it commits, the new members that hold the
    /// log change to the next epoch with a view change, members left out
    /// retire, and new members (started with [`ReplicaConfig::join`]) fetch
    /// the log. A quorum of the new members must be members now, so one
    /// change adds or removes members, or replaces a minority: replace a
    /// dead member by adding its successor in its place. Returns the new
    /// epoch once this replica has entered it (a removed replica enters it
    /// to retire). Like a failover, the change stops the primary's engine:
    /// it reopens on the first member of the new list (the current primary
    /// when it stays).
    pub async fn change_members(
        &self,
        members: Vec<(ReplicaId, String)>,
        timeout: Duration,
    ) -> io::Result<u64> {
        let (epoch, current) = {
            let m = self
                .shared
                .members
                .read()
                .unwrap_or_else(|e| e.into_inner());
            (m.0.epoch, m.0.clone())
        };
        // Refuse a change the primary would refuse, with its reason.
        current
            .next(ReplicaId::MAX, &members)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
        let mut status = self.status.clone();
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            self.shared
                .reconfigure
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .push(members.clone());
            self.shared.wake.notify_one();
            // Ask again now and then: the request, or the entry, can be lost
            // with a primary.
            let retry = tokio::time::Instant::now() + Duration::from_millis(500);
            match tokio::time::timeout_at(
                retry.min(deadline),
                status.wait_for(|s| s.epoch > epoch || s.stopped),
            )
            .await
            {
                Ok(Ok(s)) if s.stopped => return Err(other("replica stopped")),
                Ok(Ok(s)) => return Ok(s.epoch),
                Ok(Err(_)) => return Err(other("replica stopped")),
                Err(_) if tokio::time::Instant::now() >= deadline => {
                    return Err(io::Error::new(
                        io::ErrorKind::TimedOut,
                        "the membership change did not complete in time (is a quorum of the old and of the new members up?)",
                    ));
                }
                Err(_) => {}
            }
        }
    }

    /// A handle that answers [`Self::lease_valid`] and
    /// [`Self::confirm_reads`] from other tasks.
    pub fn reads(&self) -> ReadGuard {
        ReadGuard(ReplicaStorage::new(self))
    }

    /// Stops the replica at once, as a crash would (in-flight work is lost
    /// unless a quorum already holds it).
    pub fn kill(self) {
        drop(self);
    }
}

/// Checks that reads of a replicated engine are served under a lease (see
/// [`ReplicaNode::lease_valid`]).
#[derive(Clone)]
pub struct ReadGuard(ReplicaStorage);

impl ReadGuard {
    pub fn lease_valid(&self) -> bool {
        self.0.lease_valid()
    }

    pub async fn confirm_reads(&self) -> io::Result<()> {
        self.0.confirm_quorum().await
    }
}

/// Which member an inbound stream may carry frames from.
#[derive(Clone, Copy)]
enum Sender {
    /// No security: the frame's sender byte is trusted.
    Any,
    /// Mutual TLS without a token: the stream belongs to its first sender,
    /// which must be one of these members (a bit per member ID).
    First(u32),
    /// The member the token exchange authenticated.
    Only(ReplicaId),
}

/// What an inbound stream needs before it may reach the protocol.
struct Inbox {
    /// Admitted streams, to be read on the I/O thread.
    tx: mpsc::UnboundedSender<Admitted>,
    acceptor: Arc<Acceptor>,
    /// The members' addresses, which change with the membership.
    shared: Arc<Shared>,
    me: ReplicaId,
    /// Runs TLS handshakes, so that a peer (or anyone who can reach the
    /// port) never spends the I/O thread's time on key exchanges.
    handshakes: tokio::runtime::Handle,
}

/// An authenticated inbound stream and whom it may speak for.
struct Admitted {
    stream: Secured<tokio::net::TcpStream>,
    sender: Sender,
}

async fn listen(l: tokio::net::TcpListener, inbox: Inbox) {
    let inbox = Arc::new(inbox);
    let protocol = tokio::runtime::Handle::current();
    loop {
        let Ok((s, peer)) = l.accept().await else {
            continue;
        };
        let _ = s.set_nodelay(true);
        if inbox.acceptor.is_open() {
            let _ = inbox.tx.send(Admitted {
                stream: Secured::Plain(s),
                sender: Sender::Any,
            });
            continue;
        }
        // The handshake runs before a single frame is read: an unauthenticated
        // stream never reaches the protocol.
        let inbox = inbox.clone();
        if !inbox.acceptor.has_tls() {
            // A token exchange alone is a few hashes: run it here.
            tokio::spawn(async move {
                let accepted = inbox.acceptor.accept_tcp(s).await;
                inbox.admit(accepted, peer);
            });
            continue;
        }
        let Ok(s) = s.into_std() else { continue };
        let protocol = protocol.clone();
        inbox.handshakes.clone().spawn(async move {
            let handshaken = match tokio::net::TcpStream::from_std(s) {
                Ok(s) => inbox.acceptor.handshake_tls(s).await,
                Err(e) => Err(e),
            };
            match handshaken {
                Ok(h) => {
                    protocol.spawn(async move {
                        let accepted = inbox.acceptor.finish(h).await;
                        inbox.admit(accepted, peer);
                    });
                }
                Err(e) => inbox.refuse(peer, &e),
            }
        });
    }
}

impl Inbox {
    fn refuse(&self, peer: std::net::SocketAddr, e: &io::Error) {
        crate::security::refused(
            format_args!("replica {}: refused a connection from {peer}", self.me),
            e,
        );
    }

    /// Ties an accepted stream to the members it may speak for, then hands
    /// it to the I/O thread's readers.
    fn admit(
        &self,
        accepted: io::Result<(Secured<tokio::net::TcpStream>, Option<ReplicaId>)>,
        peer: std::net::SocketAddr,
    ) {
        let (stream, proven) = match accepted {
            Ok(accepted) => accepted,
            Err(e) => return self.refuse(peer, &e),
        };
        // Every replica this one knows the address of: the members, those
        // the last change removed, and (for a joining replica) those it
        // was told to ask.
        let peers = self
            .shared
            .members
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .1
            .clone();
        let others = peers
            .iter()
            .enumerate()
            .filter(|(i, p)| !p.is_empty() && *i != usize::from(self.me))
            .fold(0u32, |mask, (i, _)| mask | 1 << i);
        // Under mutual TLS, only the members whose host the certificate
        // names: a certificate the CA signed for anything else (a worker)
        // never speaks for a member.
        let allowed = others
            & match &stream {
                Secured::Tls(s) => s
                    .peer_certificate()
                    .map_or(0, |cert| crate::security::members_named(&cert, &peers)),
                Secured::Plain(_) => others,
            };
        let sender = match proven {
            Some(id) if member(allowed, id) => Sender::Only(id),
            Some(id) => {
                return self.refuse(
                    peer,
                    &other(format!(
                        "it authenticated as member {id}, which is not another member or which its certificate does not name"
                    )),
                );
            }
            None if allowed == 0 => {
                return self.refuse(peer, &other("its certificate names no other member's host"));
            }
            None => Sender::First(allowed),
        };
        let _ = self.tx.send(Admitted { stream, sender });
    }
}

fn member(mask: u32, id: ReplicaId) -> bool {
    u32::from(id) < 32 && mask & (1 << id) != 0
}

/// Starts a reader on the I/O thread for every admitted stream.
async fn read_admitted(mut admitted: mpsc::UnboundedReceiver<Admitted>, core: CoreCell) {
    while let Some(a) = admitted.recv().await {
        let core = core.clone();
        match a.stream {
            Secured::Plain(s) => tokio::task::spawn_local(receive(s, a.sender, core)),
            Secured::Tls(s) => tokio::task::spawn_local(receive(s, a.sender, core)),
        };
    }
}

/// Reads one peer's stream and handles each frame on the spot: no channel
/// and no other task between the socket and the protocol. Every read ends
/// with one round (one log write, one socket write per peer).
async fn receive<R: tokio::io::AsyncRead + Unpin>(mut s: R, mut sender: Sender, core: CoreCell) {
    let traffic = lock(&core).shared.traffic.clone();
    let mut dec = frame::Decoder::new();
    let mut buf = vec![0u8; 1 << 16];
    let mut out = Vec::new();
    // The member whose frames this stream carried.
    let mut carried = None;
    loop {
        let n = match s.read(&mut buf).await {
            Ok(0) | Err(_) => {
                // A crashed process's streams close at once: if this was
                // the primary's, its backups need not wait out a timeout.
                if let Some(id) = carried {
                    let mut c = lock(&core);
                    c.replica.peer_closed(id, now_ms());
                }
                return;
            }
            Ok(n) => n,
        };
        traffic.received.fetch_add(n as u64, Ordering::Relaxed);
        let read_at = traffic.timings.start();
        dec.feed(&buf[..n]);
        let mut c = lock(&core);
        if c.halted.is_some() {
            return;
        }
        c.read_at = read_at;
        let mut keep = true;
        let result = (|| -> io::Result<()> {
            loop {
                match dec.next() {
                    Ok(Some((from, body))) => {
                        match sender {
                            Sender::Any => {}
                            Sender::First(allowed) if member(allowed, from) => {
                                sender = Sender::Only(from);
                            }
                            Sender::Only(id) if id == from => {}
                            // A frame from a member this stream may not speak
                            // for: drop the stream and everything after it.
                            Sender::First(_) | Sender::Only(_) => {
                                keep = false;
                                break;
                            }
                        }
                        carried = Some(from);
                        c.receive(from, &body, &mut out)?;
                    }
                    Ok(None) => break,
                    Err(_) => {
                        keep = false;
                        break;
                    }
                }
            }
            c.round(&mut out)
        })();
        c.read_at = None;
        if let Err(e) = result {
            c.halt(e);
            return;
        }
        // An acknowledgment can make the primary's own write the one a
        // quorum waits for: have that write wake the main task, and let it
        // look now if the write already finished.
        if c.released || c.arm_durable_wake() || !busy_poll().is_zero() {
            // Let the main task spin for the shards' next sections (or,
            // busy polling, for the next frame).
            c.shared.wake.notify_one();
        }
        if !keep {
            return;
        }
    }
}

/// The protocol state. The I/O thread holds it while it handles frames,
/// timers and proposals; a lone shard takes it, without waiting, to
/// propose its own section ([`Core::inline_round`]).
type CoreCell = Arc<Mutex<Core>>;

fn lock(core: &CoreCell) -> std::sync::MutexGuard<'_, Core> {
    core.lock().unwrap_or_else(|e| e.into_inner())
}

struct Core {
    id: ReplicaId,
    replica: Replica,
    log: VsrLog,
    /// The primary's background log writes.
    writes: writes::Writes,
    /// Segment groups: committed batches materialized into the engine's
    /// segment files.
    writer: BatchWriter,
    applied: Op,
    staged_since_hint: bool,
    senders: BTreeMap<ReplicaId, Arc<transport::Peer>>,
    /// This round's frames for each peer, written once at its end.
    outbox: Vec<Vec<u8>>,
    /// The last Prepare encoded this round, sent unchanged to every backup.
    prepare_cache: Option<((u64, Op, Op, u64, Op), Vec<u8>)>,
    /// Permit commands and segment-group batches taken off the proposal queue.
    queued: Vec<Proposal>,
    pending: BTreeMap<Op, Pending>,
    leases: leases::Ledger,
    lease_applied: Op,
    checkpoints: transfer::Checkpoints,
    // First control op, logical clock at election, local monotonic start.
    session: Option<(Op, u64, u64)>,
    shared: Arc<Shared>,
    ready: bool,
    last_hint: u64,
    /// This round released commits to their waiters.
    released: bool,
    /// Group commit: a round's usual time in ns, and when the last log batch
    /// was proposed (see `pipeline_full`).
    round_ns: u64,
    last_entry_at: Option<Instant>,
    /// The last recovery snapshot handed to opening shards, by commit.
    recovery: Option<(Op, Arc<log3::Recovery>)>,
    /// The commit hint last queued, and the file it goes to.
    commit_hint: (Op, Option<(PathBuf, Arc<File>)>),
    /// A storage error stopped the protocol.
    halted: Option<io::Error>,
    /// Starts connections to members a membership change adds.
    dialing: Dialing,
    vsr_dir: PathBuf,
    /// Timings only: when the inbound read this round handles returned.
    read_at: Option<Instant>,
    /// Timings only: log batches proposed this round, not yet sent.
    unsent: Vec<Op>,
    /// Timings only (backup): this round's write became durable, and an
    /// acknowledgment is queued.
    durable_at: Option<Instant>,
    ack_queued: bool,
}

/// Whether an entry body is a segment-group (format 2) flush batch: not a permit
/// command, a log batch or a membership change.
fn is_segment_batch(body: &[u8]) -> bool {
    !leases::is_control(body) && !log3::is_log_batch(body) && !fusee_core::vsr::is_reconfig(body)
}

/// Connects to every member (and every member the last change removed,
/// which still serve the log to members catching up).
struct Dialing {
    handle: tokio::runtime::Handle,
    connector: Arc<Connector>,
    me: ReplicaId,
}

impl Dialing {
    fn dial(&self, shared: &Arc<Shared>, senders: &mut BTreeMap<ReplicaId, Arc<transport::Peer>>) {
        let (membership, addresses) = shared
            .members
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .clone();
        let wanted: std::collections::BTreeSet<ReplicaId> = membership
            .members
            .iter()
            .chain(&membership.previous)
            .copied()
            .filter(|m| *m != self.me && !addresses[usize::from(*m)].is_empty())
            .collect();
        senders.retain(|id, _| wanted.contains(id));
        let mut dialers = shared.dialers.lock().unwrap_or_else(|e| e.into_inner());
        dialers.retain(|id, task| {
            let keep = wanted.contains(id);
            if !keep {
                task.abort();
            }
            keep
        });
        for id in wanted {
            if senders.contains_key(&id) {
                continue;
            }
            let peer = Arc::new(transport::Peer::default());
            senders.insert(id, peer.clone());
            let addr = addresses[usize::from(id)].clone();
            let traffic = shared.traffic.clone();
            let connector = self.connector.clone();
            dialers.insert(
                id,
                self.handle
                    .spawn(async move { peer.run(&addr, &traffic, &connector).await }),
            );
        }
    }
}

fn finish(slot: &Arc<Mutex<Slot>>, r: io::Result<Lsn>) {
    let mut state = slot.lock().unwrap_or_else(|e| e.into_inner());
    if let Some(callback) = state.callback.take() {
        drop(state);
        callback(r);
    } else {
        state.result = Some(r);
        let notify = state.notify.clone();
        drop(state);
        if let Some(notify) = notify {
            notify();
        }
    }
}

/// The longest the I/O thread spins, after releasing a commit to shards,
/// for their next section before it parks. The budget follows how soon
/// sections came after recent releases ([`fusee::spin::Budget`]).
const RELEASE_SPIN_CAP: Duration = Duration::from_micros(50);

/// Log batches the primary keeps proposed and uncommitted at most before
/// staged sections wait to be merged into the next (group commit; see
/// `Core::pipeline_full`).
const IN_FLIGHT_ENTRIES: u64 = 4;

/// While an entry is uncommitted, another goes out only once the oldest has
/// waited this much longer than a round usually takes (in eighths), and no
/// sooner than [`ENTRY_GAP`] after the last: the I/O thread's cost is per
/// entry, so under load entries grow instead of multiplying.
const SLOW_ROUND_EIGHTHS: u64 = 12;
const ENTRY_GAP: Duration = Duration::from_micros(40);

/// Sections of one shard a replicated engine keeps proposed and
/// uncommitted at once: while an entry is uncommitted, a shard's next
/// records stage for the next entry instead of waiting for the commit.
const SECTIONS_IN_FLIGHT: usize = 4;

/// Busy-polled receive: `FUSEE_REPLICA_BUSY_POLL_US`, the time the I/O
/// thread keeps polling its sockets without blocking after any activity, on
/// any host. Off (0) by default; use it only where it shortens the round
/// trip measurably.
fn busy_poll() -> Duration {
    static POLL: std::sync::OnceLock<Duration> = std::sync::OnceLock::new();
    *POLL.get_or_init(|| {
        std::env::var("FUSEE_REPLICA_BUSY_POLL_US")
            .ok()
            .and_then(|v| v.trim().parse::<u64>().ok())
            .map_or(Duration::ZERO, |us| Duration::from_micros(us.min(10_000)))
    })
}

/// Spins on the I/O thread's runtime: each turn lets its readers run and
/// polls the reactor without blocking, so frames that arrive meanwhile are
/// handled too. Returns whether `ready()` became true.
async fn spin(budget: Duration, ready: impl Fn() -> bool) -> bool {
    if ready() {
        return true;
    }
    if budget.is_zero() {
        return false;
    }
    let until = Instant::now() + budget;
    loop {
        tokio::task::yield_now().await;
        if ready() {
            return true;
        }
        if Instant::now() >= until {
            return false;
        }
    }
}

/// The I/O thread's main task: shard proposals, checkpoint barriers and
/// captures, timers. Peer frames are handled by their readers ([`receive`]).
async fn drive(
    core: CoreCell,
    mut proposals: queue::Receiver<Proposal>,
    mut barriers: queue::Receiver<Barrier>,
    mut captures: mpsc::Receiver<(u64, snapshot::Sources)>,
    mut stopped: oneshot::Receiver<()>,
) {
    let wake = lock(&core).shared.wake.clone();
    let release_spin = fusee::spin::Budget::new(RELEASE_SPIN_CAP);
    let mut out = Vec::new();
    let mut next_tick = 0;
    // Ticks run at half the heartbeat interval (1 to 5 ms), so heartbeats
    // and timeouts are late by at most that.
    let tick_ms = (lock(&core).replica.timing().heartbeat_ms / 2).clamp(1, 5);
    let mut waiting_barriers: Vec<Barrier> = Vec::new();
    loop {
        if !matches!(stopped.try_recv(), Err(oneshot::error::TryRecvError::Empty)) {
            break;
        }
        if now_ms() >= next_tick && lock(&core).replica.election_due(now_ms()) {
            // About to suspect the primary. If this thread was not scheduled
            // for a while, its heartbeats may be sitting in the sockets:
            // let the readers run (a yield polls the reactor first) and
            // look again before starting an election.
            tokio::task::yield_now().await;
        }
        {
            let mut c = lock(&core);
            let held = c.shared.traffic.timings.start();
            if c.halted.is_some() {
                break;
            }
            let result = (|| -> io::Result<()> {
                while let Some(p) = proposals.try_recv() {
                    c.queued.push(p);
                }
                while let Ok((epoch, sources)) = captures.try_recv() {
                    c.capture_checkpoint(epoch, sources)?;
                }
                let now = now_ms();
                if now >= next_tick {
                    next_tick = now + tick_ms;
                    let ticked = c.shared.traffic.timings.start();
                    if !c.checkpoints.quarantine {
                        c.replica.on_tick(now, &mut out);
                        c.reconfigure(&mut out);
                    }
                    let part = c.shared.traffic.timings.start();
                    c.shared.traffic.timings.tick_protocol.observe(ticked);
                    c.process(&mut out)?;
                    c.shared.traffic.timings.tick_process.observe(part);
                    let part = c.shared.traffic.timings.start();
                    c.service_checkpoints(&mut out)?;
                    c.shared.traffic.timings.tick_checkpoints.observe(part);
                    c.shared.traffic.timings.io_tick.observe(ticked);
                }
                c.round(&mut out)?;
                while let Some(b) = barriers.try_recv() {
                    waiting_barriers.push(b);
                }
                for request in waiting_barriers.drain(..) {
                    c.answer(request)?;
                }
                Ok(())
            })();
            if let Err(e) = result {
                c.halt(e);
                break;
            }
            c.shared.traffic.timings.io_lock_hold.observe(held);
        }
        // Idle. Right after a release, a shard's next section is usually a
        // few microseconds away: spin for it (polling the sockets meanwhile)
        // rather than pay a wake-up, then park until a shard, a log write,
        // a peer or a timer wakes us. Waiting for acknowledgments parks at
        // once: the reactor wakes the reader.
        let idle_at = Instant::now();
        let after_release = std::mem::take(&mut lock(&core).released);
        let ready = || {
            lock(&core).may_propose()
                || !proposals.is_empty()
                || lock(&core).writes.has_done()
                || lock(&core).halted.is_some()
        };
        let budget = if after_release {
            release_spin.get()
        } else {
            Duration::ZERO
        }
        .max(busy_poll());
        // A write that finishes while this thread waits wakes it only if a
        // quorum waits for it (or more frames wait to be written).
        lock(&core).arm_durable_wake();
        if !spin(budget, ready).await {
            let tick = tokio::time::Instant::now()
                + Duration::from_millis(next_tick.saturating_sub(now_ms()).max(1));
            tokio::select! {
                _ = &mut stopped => break,
                () = wake.notified() => {}
                Some(p) = proposals.recv() => lock(&core).queued.push(p),
                Some(b) = barriers.recv() => waiting_barriers.push(b),
                Some((epoch, sources)) = captures.recv() => {
                    let result = lock(&core).capture_checkpoint(epoch, sources);
                    if let Err(e) = result {
                        lock(&core).halt(e);
                    }
                }
                () = tokio::time::sleep_until(tick) => {}
            }
        }
        if after_release {
            release_spin.record(idle_at.elapsed());
        }
    }
    {
        let mut c = lock(&core);
        if let Some(e) = c.halted.take() {
            // A failed write of the protocol log: stop, like a shard.
            eprintln!("Fusee replica {} stopped after a storage error: {e}", c.id);
        }
        c.halted = Some(other("replica stopped"));
        c.lose_primacy();
        c.shared.status.send_modify(|s| {
            s.ready = false;
            s.primary = false;
            s.stopped = true;
        });
        c.shared.role_changed();
    }
    proposals.close();
    barriers.close();
    for b in waiting_barriers {
        b.refuse();
    }
    while let Some(p) = proposals.recv().await {
        finish(&p.slot, Err(other("replica stopped before commit")));
    }
}

impl Core {
    fn answer(&mut self, request: Barrier) -> io::Result<()> {
        let epoch = self.shared.epoch.load(Ordering::Acquire);
        match request {
            Barrier::Checkpoint { epoch: e, reply } if self.ready && e == epoch => {
                self.save_hint()?;
                let _ = reply.send(Ok(()));
            }
            Barrier::Recovery { epoch: e, reply } if self.ready && e == epoch => {
                let commit = self.replica.commit();
                let cached = self
                    .recovery
                    .as_ref()
                    .filter(|(at, r)| *at == commit && r.epoch == epoch)
                    .map(|(_, r)| r.clone());
                let r = match cached {
                    Some(r) => r,
                    None => {
                        let r = Arc::new(log3::Recovery {
                            epoch,
                            entries: self
                                .replica
                                .entries()
                                .filter(|e| e.op <= commit && log3::is_log_batch(&e.body))
                                .cloned()
                                .collect(),
                        });
                        self.recovery = Some((commit, r.clone()));
                        r
                    }
                };
                let _ = reply.send(Ok(r));
            }
            other => other.refuse(),
        }
        Ok(())
    }

    /// Membership change requests: proposed here on the primary, else sent
    /// to the primary of this replica's view.
    fn reconfigure(&mut self, out: &mut Vec<Action>) {
        let requests = std::mem::take(
            &mut *self
                .shared
                .reconfigure
                .lock()
                .unwrap_or_else(|e| e.into_inner()),
        );
        for members in requests {
            if self.replica.is_primary() {
                match self.replica.reconfigure(&members, out) {
                    Ok(_) | Err("a membership change is already in progress") => {}
                    Err(e) => {
                        eprintln!("Fusee replica {}: membership change refused: {e}", self.id)
                    }
                }
            } else {
                let primary = self.replica.primary_of(self.replica.view());
                let epoch = self.replica.membership().epoch + 1;
                if primary != self.id && usize::from(primary) < fusee_core::vsr::MAX_MEMBERS {
                    out.push(Action::Send {
                        to: primary,
                        msg: Msg::Reconfigure {
                            body: fusee_core::vsr::encode_reconfig(epoch, &members),
                        },
                    });
                }
            }
        }
    }

    /// Before waiting: arms the primary's own finished writes to wake the
    /// I/O thread only when its durability would commit entries (see
    /// [`writes::Writes::wake_when_done`]). Returns whether a write has
    /// finished meanwhile.
    fn arm_durable_wake(&self) -> bool {
        self.writes
            .wake_when_done(self.replica.commit_waits_on_own_durability());
        self.writes.has_done()
    }

    /// A round run by a shard thread for its own section, while the I/O
    /// thread waits: propose what is staged, send the Prepare, hand the
    /// primary's own write to its helper. The shard then waits for the
    /// commit; the I/O thread, woken by the acknowledgment,
    /// releases it. One thread hand-off fewer per sequential step (the
    /// shared log's inline write, for the replicated log).
    fn inline_round(&mut self) {
        if self.halted.is_some() {
            return;
        }
        let mut out = Vec::new();
        if let Err(e) = self.round(&mut out) {
            self.halt(e);
            return;
        }
        if self.arm_durable_wake() || !out.is_empty() {
            self.shared.wake.notify_one();
        }
    }

    /// Staged sections wait for a commit: adaptive group commit. With
    /// nothing uncommitted they go out at once; with an entry in flight they
    /// wait for it to commit and go out together, unless that round runs
    /// slow (then up to [`IN_FLIGHT_ENTRIES`], [`ENTRY_GAP`] apart).
    fn pipeline_full(&self) -> bool {
        let uncommitted = self.replica.op().saturating_sub(self.replica.commit());
        if uncommitted == 0 {
            return false;
        }
        if uncommitted >= IN_FLIGHT_ENTRIES {
            return true;
        }
        // The oldest log batch in flight; with permit commands only, two
        // uncommitted entries fill the pipeline.
        let Some(at) = self.pending.values().find_map(|p| match p {
            Pending::Sections { at, .. } => Some(*at),
            Pending::Slot { .. } => None,
        }) else {
            return uncommitted >= 2;
        };
        let slow = at.elapsed().as_nanos() as u64 > self.round_ns * SLOW_ROUND_EIGHTHS / 8;
        let spaced = self.last_entry_at.is_none_or(|t| t.elapsed() >= ENTRY_GAP);
        !(slow && spaced)
    }

    /// Staged sections may be proposed now (see [`Self::propose_staged`]).
    fn may_propose(&self) -> bool {
        self.shared.batcher.has_staged() && !(self.ready && self.pipeline_full())
    }

    fn halt(&mut self, e: io::Error) {
        if self.halted.is_none() {
            self.halted = Some(e);
        }
        self.shared.wake.notify_one();
    }

    /// One round: propose what shards staged, act on the protocol's output
    /// (one log write), then write each peer's frames once.
    fn round(&mut self, out: &mut Vec<Action>) -> io::Result<()> {
        self.propose_staged(out);
        self.process(out)?;
        // A commit in this round (an acknowledgment it read) let what staged
        // meanwhile go out: in the same round's frames.
        if self.may_propose() {
            self.propose_staged(out);
            self.process(out)?;
        }
        self.flush_outbox();
        Ok(())
    }

    fn flush_outbox(&mut self) {
        self.prepare_cache = None;
        for (to, bytes) in self.outbox.iter_mut().enumerate() {
            if bytes.is_empty() {
                continue;
            }
            let bytes = std::mem::take(bytes);
            if let Some(tx) = self.senders.get(&(to as ReplicaId)) {
                tx.send(bytes, &self.shared.traffic);
            }
        }
        let timings = &self.shared.traffic.timings;
        if !self.unsent.is_empty() {
            let now = timings.start();
            for op in self.unsent.drain(..) {
                if let Some(Pending::Sections { proposed, sent, .. }) = self.pending.get_mut(&op) {
                    *sent = now;
                    timings.take_to_sent.between(*proposed, now);
                }
            }
        }
        if std::mem::take(&mut self.ack_queued) {
            let now = timings.start();
            timings.durable_to_ack.between(self.durable_at.take(), now);
            timings.prepare_to_ack.between(self.read_at, now);
        }
    }

    /// Frames `msg` for `to` into this round's output. A Prepare is encoded
    /// once and the same bytes go to every backup.
    fn queue_send(&mut self, to: ReplicaId, msg: &Msg) {
        let Some(buf) = self.outbox.get_mut(usize::from(to)) else {
            return;
        };
        if matches!(msg, Msg::PrepareOk { .. }) {
            self.ack_queued = self.shared.traffic.timings.enabled();
        }
        if let Msg::Prepare {
            view,
            entries,
            commit,
        } = msg
            && let (Some(first), Some(last)) = (entries.first(), entries.last())
        {
            let key = (*view, first.op, last.op, last.checksum, *commit);
            if let Some((k, bytes)) = &self.prepare_cache
                && *k == key
            {
                buf.extend_from_slice(bytes);
                return;
            }
            let timings = &self.shared.traffic.timings;
            timings.count(&timings.entries_per_prepare, entries.len());
            let mut body = Vec::new();
            encode_msg(msg, &mut body);
            let mut f = Vec::with_capacity(body.len() + frame::HEADER);
            frame::encode(self.id, &body, &mut f);
            buf.extend_from_slice(&f);
            self.prepare_cache = Some((key, f));
            return;
        }
        let mut body = Vec::new();
        encode_msg(msg, &mut body);
        frame::encode(self.id, &body, buf);
    }

    /// Proposes permit commands, segment-group batches and every staged section
    /// (one log batch) as consecutive entries of one Prepare.
    fn propose_staged(&mut self, out: &mut Vec<Action>) {
        // A membership change ends this epoch: what shards stage waits for
        // it (and fails with the role, as in any failover).
        if self.ready && self.replica.sealed() {
            return;
        }
        // Group commit: while enough entries are uncommitted, staged
        // sections wait, and those of every shard that stages meanwhile go
        // out as one entry when one commits. A shard has one section in
        // flight, so it waits at most one commit; an idle group proposes at
        // once. Permit commands do not wait.
        let staged = if self.ready && self.pipeline_full() {
            Vec::new()
        } else {
            self.shared.batcher.take()
        };
        if staged.is_empty() && self.queued.is_empty() {
            return;
        }
        let queued = std::mem::take(&mut self.queued);
        let epoch = self.shared.epoch.load(Ordering::Acquire);
        if !self.ready {
            for p in queued {
                finish(&p.slot, Err(other("this replica is not the primary")));
            }
            for s in staged {
                s.slot.fail();
            }
            return;
        }
        let mut bodies = Vec::new();
        let mut pendings = Vec::new();
        for p in queued {
            if p.epoch != epoch {
                finish(&p.slot, Err(other("this replica is not the primary")));
                continue;
            }
            self.shared.traffic.timings.app_queue.observe(p.queued);
            let proposed = self.shared.traffic.timings.start();
            let claim = matches!(p.control, Some(leases::Request::Claim));
            // The op this body will get.
            let op = self.replica.op() + 1 + bodies.len() as Op;
            let body = if let Some(request) = p.control {
                let now = now_ms();
                let command = match request {
                    leases::Request::Embedded(command) => command,
                    // Tickets start at one. Releasing ticket zero changes no
                    // grants, and old replicas already understand this record.
                    leases::Request::Probe => {
                        leases::Command::new(crate::limits::op::RELEASE, String::new(), 0, 0, 0.0)
                    }
                    leases::Request::Claim => {
                        self.session = Some((op, self.leases.time, now));
                        leases::Command::renew()
                    }
                    leases::Request::Worker(session, command) => {
                        if self.session.map(|s| s.0) != Some(session) {
                            finish(&p.slot, Err(other("remote worker session is stale")));
                            continue;
                        }
                        command
                    }
                };
                let (session, base, start) =
                    *self.session.get_or_insert((op, self.leases.time, now));
                match leases::encode(
                    command,
                    session,
                    base.saturating_add(now.saturating_sub(start)),
                ) {
                    Ok(data) => data,
                    Err(e) => {
                        finish(&p.slot, Err(e));
                        continue;
                    }
                }
            } else {
                p.body
            };
            bodies.push(body);
            pendings.push(Pending::Slot {
                return_session: claim,
                slot: p.slot,
                last_lsn: p.last_lsn,
                proposed,
            });
        }
        let mut staged: Vec<log3::Staged> = staged
            .into_iter()
            .filter_map(|s| {
                if s.epoch == epoch {
                    Some(s)
                } else {
                    s.slot.fail();
                    None
                }
            })
            .collect();
        if !staged.is_empty() {
            let timings = &self.shared.traffic.timings;
            for s in &staged {
                timings.stage_to_take.since_ns(s.staged_ns);
            }
            // One section per shard, in shard order. A shard has one flush
            // in flight, but consecutive sections would merge here.
            staged.sort_by_key(|s| s.shard);
            let mut merged: Vec<log3::Staged> = Vec::with_capacity(staged.len());
            for s in staged {
                if let Some(prev) = merged.last_mut()
                    && prev.shard == s.shard
                    && s.batch.first_lsn == prev.batch.last_lsn + 1
                {
                    prev.batch.section.extend_from_slice(&s.batch.section);
                    prev.batch.last_lsn = s.batch.last_lsn;
                    prev.batch.records += s.batch.records;
                    prev.slot = s.slot;
                    continue;
                }
                merged.push(s);
            }
            let shards = match self.shared.kind() {
                Kind::Log(n) => n,
                _ => merged.iter().map(|s| s.shard + 1).max().unwrap_or(1),
            };
            let sections: Vec<fusee_core::wal::SectionRef<'_>> = merged
                .iter()
                .map(|s| fusee_core::wal::SectionRef {
                    shard: s.shard,
                    first_lsn: s.batch.first_lsn,
                    count: s.batch.records,
                    records: &s.batch.section,
                })
                .collect();
            bodies.push(Arc::from(log3::encode(shards, &sections)));
            let timings = &self.shared.traffic.timings;
            timings.count(&timings.sections_per_entry, merged.len());
            pendings.push(Pending::Sections {
                shards: merged
                    .into_iter()
                    .map(|s| (s.slot, s.batch.last_lsn))
                    .collect(),
                at: Instant::now(),
                proposed: timings.start(),
                sent: None,
            });
            self.last_entry_at = Some(Instant::now());
        }
        match self.replica.propose_many(bodies, out) {
            Some((first, _)) => {
                for (op, p) in (first..).zip(pendings) {
                    if matches!(
                        p,
                        Pending::Sections {
                            proposed: Some(_),
                            ..
                        }
                    ) {
                        self.unsent.push(op);
                    }
                    self.pending.insert(op, p);
                }
            }
            None => {
                for p in pendings {
                    p.fail("this replica is not the primary");
                }
            }
        }
    }

    fn lose_primacy(&mut self) {
        // Before any frame of the view change is written.
        self.shared.lease_until.store(0, Ordering::Release);
        self.session = None;
        self.ready = false;
        self.shared.epoch.fetch_add(1, Ordering::AcqRel);
        for pending in std::mem::take(&mut self.pending).into_values() {
            pending.fail("this replica lost the primary role");
        }
        for s in self.shared.batcher.take() {
            s.slot.fail();
        }
        for p in std::mem::take(&mut self.queued) {
            finish(&p.slot, Err(other("this replica lost the primary role")));
        }
        self.recovery = None;
    }

    /// Places frames for a background write (see [`writes`]).
    fn place<'a>(
        log: &'a mut VsrLog,
    ) -> impl FnMut(Vec<u8>, bool) -> io::Result<writes::Placement> + 'a {
        move |bytes, pad| log.place(bytes, pad)
    }

    /// Takes finished background writes.
    fn poll_writes(&mut self, out: &mut Vec<Action>) -> io::Result<()> {
        if !self.writes.has_done() {
            return Ok(());
        }
        let target = self.log.target();
        if let Some(op) = self.writes.poll(&target, &mut Self::place(&mut self.log))? {
            self.replica.on_durable(op, out);
        }
        Ok(())
    }

    /// Waits for every background write: a barrier.
    fn drain_writes(&mut self) -> io::Result<Option<Op>> {
        if self.writes.idle() {
            return Ok(None);
        }
        let target = self.log.target();
        self.writes.drain(&target, &mut Self::place(&mut self.log))
    }

    /// Writes this round's frames: on the primary in the background (an
    /// entry commits on any durable quorum, so its own flush is not on the
    /// commit path), on a backup inline, then reports durability.
    fn write_round(
        &mut self,
        frames: Vec<u8>,
        last: Op,
        sync: bool,
        out: &mut Vec<Action>,
    ) -> io::Result<()> {
        if frames.is_empty() {
            return Ok(());
        }
        let start = self.shared.traffic.timings.start();
        if !sync && self.replica.is_primary() {
            // The backups' Prepare first: one of their flushes, not this
            // one, is on the commit path, and handing the write to a helper
            // costs a wake-up.
            self.flush_outbox();
            let target = self.log.target();
            self.writes
                .submit(frames, last, &target, &mut Self::place(&mut self.log))?;
            self.shared.traffic.timings.protocol_append.observe(start);
            return Ok(());
        }
        if let Some(op) = self.drain_writes()? {
            self.replica.on_durable(op, out);
        }
        let timings = &self.shared.traffic.timings;
        timings.prepare_to_write.between(self.read_at, start);
        self.log.write_frames(frames)?;
        timings.protocol_append.observe(start);
        let start = timings.start();
        if !self.shared.local.emulated_flush.is_zero() {
            std::thread::sleep(self.shared.local.emulated_flush);
        }
        self.log.sync()?;
        timings.protocol_sync.observe(start);
        self.durable_at = timings.start();
        self.replica.on_durable(last, out);
        Ok(())
    }

    fn process(&mut self, out: &mut Vec<Action>) -> io::Result<()> {
        for _ in 0..1000 {
            self.poll_writes(out)?;
            if out.is_empty() {
                break;
            }
            let actions: Vec<Action> = std::mem::take(out);
            let mut frames = Vec::new();
            let mut last = 0;
            // A replacement writes the whole log as it now is in memory,
            // which already includes any entry persisted after it here.
            let mut replaced = false;
            for a in actions {
                match a {
                    Action::Send { to, msg } => self.queue_send(to, &msg),
                    Action::Persist { entries } => {
                        if replaced {
                            continue;
                        }
                        for e in &entries {
                            VsrLog::frame(e, &mut frames);
                        }
                        last = entries.last().map_or(last, |e| e.op);
                    }
                    Action::Replace { .. } => {
                        frames.clear();
                        // In-flight writes go to the file being replaced.
                        self.drain_writes()?;
                        self.log.replace(self.replica.entries())?;
                        replaced = true;
                    }
                    Action::Committed { .. } => {}
                    Action::SaveView {
                        view,
                        last_normal,
                        commit,
                    } => {
                        // A barrier: everything before it is durable first.
                        let frames = std::mem::take(&mut frames);
                        self.write_round(frames, last, true, out)?;
                        if let Some(op) = self.drain_writes()? {
                            self.replica.on_durable(op, out);
                        }
                        self.writer.sync_staged()?;
                        self.log.save(&Superblock {
                            view,
                            last_normal,
                            commit,
                            applied: self.applied,
                        })?;
                    }
                    Action::Role { primary, .. } => {
                        if !primary {
                            self.lose_primacy();
                        }
                    }
                    Action::SaveMembership { membership } => {
                        // A barrier too: durable before anything after it.
                        let frames = std::mem::take(&mut frames);
                        self.write_round(frames, last, true, out)?;
                        if let Some(op) = self.drain_writes()? {
                            self.replica.on_durable(op, out);
                        }
                        write_membership(&self.vsr_dir, &membership, self.log.method)?;
                        {
                            let mut m = self
                                .shared
                                .members
                                .write()
                                .unwrap_or_else(|e| e.into_inner());
                            for (id, addr) in membership.addresses() {
                                m.1[usize::from(id)] = addr;
                            }
                            m.0 = membership;
                        }
                        self.dialing.dial(&self.shared, &mut self.senders);
                    }
                }
            }
            if replaced {
                let op = self.replica.op();
                self.replica.on_durable(op, out);
            } else {
                self.write_round(frames, last, false, out)?;
            }
        }
        self.apply()?;
        self.publish();
        Ok(())
    }

    // A saved "applied" watermark allows recovery to skip these entries.
    // Only publish it after their materialized segments are themselves durable.
    fn save_hint(&mut self) -> io::Result<()> {
        let start = self.shared.traffic.timings.start();
        // The superblock vouches for the log: finish the background writes.
        if let Some(op) = self.drain_writes()? {
            // On the primary this only moves the commit position, which the
            // next round releases.
            self.replica.on_durable(op, &mut Vec::new());
        }
        self.writer.sync_staged()?;
        self.staged_since_hint = false;
        self.log.save(&Superblock {
            view: self.replica.view(),
            last_normal: self.replica.last_normal(),
            commit: self.replica.commit(),
            applied: self.applied,
        })?;
        self.shared.traffic.timings.hint.observe(start);
        self.last_hint = now_ms();
        Ok(())
    }

    /// Releases committed entries to their waiters, then materializes
    /// committed segment-group batches into the engine's segment files.
    ///
    /// Release happens at the commit position: a quorum holds those entries
    /// durably, whether or not this replica's own write has finished (the
    /// safety argument is in `docs/replication.md`). Materialization keeps
    /// to `min(commit, durable)`: recovery re-stages every committed entry
    /// above the saved applied hint, and the hint only moves after
    /// `sync_staged`. Both loops finish before this returns, so a
    /// checkpoint barrier, capture or view save, all handled after
    /// `process`, never sees a released segment-group batch that is not staged.
    fn apply(&mut self) -> io::Result<()> {
        if self.checkpoints.quarantine {
            return Ok(());
        }
        let commit = self.replica.commit();
        while self.lease_applied < commit {
            let op = self.lease_applied + 1;
            let Some(e) = self.replica.entry(op) else {
                break;
            };
            let answer = if leases::is_control(&e.body) {
                Some(self.leases.apply(&e.body)?)
            } else {
                None
            };
            self.lease_applied = op;
            match self.pending.remove(&op) {
                Some(Pending::Slot {
                    return_session,
                    slot,
                    last_lsn,
                    proposed,
                }) => {
                    self.shared.traffic.timings.quorum.observe(proposed);
                    self.released = true;
                    let lsn = match answer {
                        Some(_) if return_session => op,
                        Some(answer) => answer,
                        None => last_lsn,
                    };
                    finish(&slot, Ok(lsn));
                }
                Some(Pending::Sections {
                    shards,
                    at,
                    proposed,
                    sent,
                }) => {
                    // A round: proposal to commit (an average of the last
                    // eight), for group commit's slow-round rule.
                    let round = at.elapsed().as_nanos().min(u128::from(u64::MAX)) as u64;
                    self.round_ns = (self.round_ns * 7 + round) / 8;
                    let timings = &self.shared.traffic.timings;
                    timings.quorum.observe(proposed);
                    if proposed.is_some() {
                        // Only a commit that an acknowledgment brought (not
                        // one the primary's own write completed) has a read.
                        let now = timings.start();
                        if self.read_at.is_some() {
                            timings.sent_to_ack.between(sent, self.read_at);
                            timings.ack_to_release.between(self.read_at, now);
                        }
                    }
                    let released = timings.stamp();
                    self.released = true;
                    for (slot, lsn) in shards {
                        slot.committed.fetch_max(lsn, Ordering::AcqRel);
                        slot.released_ns.store(released, Ordering::Relaxed);
                        slot.wake();
                    }
                }
                None => {}
            }
        }
        let through = self.lease_applied.min(self.replica.durable());
        for op in self.applied + 1..=through {
            let e = self
                .replica
                .entry(op)
                .ok_or_else(|| other("released entry left the protocol log"))?;
            if is_segment_batch(&e.body) {
                let start = self.shared.traffic.timings.start();
                let batch = proto::decode_batch(&mut Reader::new(&e.body)).map_err(other)?;
                self.writer.stage(&batch)?;
                self.staged_since_hint = true;
                self.shared.traffic.timings.materialize.observe(start);
            }
            self.applied = op;
        }
        // Only materialized segments need the periodic hint and its sync.
        let now = now_ms();
        if now.saturating_sub(self.last_hint) >= 200 {
            if self.staged_since_hint {
                self.save_hint()?;
            } else {
                self.queue_commit_hint(now)?;
            }
        }
        Ok(())
    }

    /// Records the commit position in the background, so a replica that
    /// restarts with a damaged committed tail keeps it for repair instead
    /// of taking it for an unacknowledged one.
    fn queue_commit_hint(&mut self, now: u64) -> io::Result<()> {
        let commit = self.replica.commit().min(self.replica.durable());
        if commit <= self.commit_hint.0 {
            return Ok(());
        }
        self.last_hint = now;
        let file = match &self.commit_hint.1 {
            Some((dir, file)) if *dir == self.log.dir => file.clone(),
            _ => {
                let file = Arc::new(
                    OpenOptions::new()
                        .create(true)
                        .truncate(false)
                        .write(true)
                        .open(self.log.dir.join(COMMIT_HINT))?,
                );
                self.commit_hint.1 = Some((self.log.dir.clone(), file.clone()));
                file
            }
        };
        self.commit_hint.0 = commit;
        self.writes
            .hint(file, encode_commit_hint(commit), self.log.method);
        Ok(())
    }

    /// What the group's data says it is: the newest data entry, else the
    /// checkpoint image.
    fn observed_kind(&self) -> Kind {
        for e in self.replica.entries().rev() {
            if log3::is_log_batch(&e.body) {
                return log3::decode(&e.body).map_or(Kind::Undecided, |(n, _)| Kind::Log(n));
            }
            if is_segment_batch(&e.body) {
                return Kind::Segments;
            }
        }
        match self.checkpoints.image() {
            Some(image) if image.shards > 0 => Kind::Log(image.shards),
            Some(image) if image.lsn > 0 => Kind::Segments,
            _ => Kind::Undecided,
        }
    }

    fn publish(&mut self) {
        let primary = self.replica.is_primary();
        let caught_up = self.replica.commit() == self.replica.op()
            && self.applied == self.replica.commit()
            && self.lease_applied == self.replica.commit();
        let ready = primary && (self.ready || caught_up);
        if ready && !self.ready {
            self.shared.epoch.fetch_add(1, Ordering::AcqRel);
            // What the engine of this epoch opens as.
            let kind = self.observed_kind();
            self.shared.kind.store(kind.encode(), Ordering::Release);
        }
        if !ready && self.ready {
            self.lose_primacy();
        }
        self.ready = ready;
        let lease = if ready {
            self.replica.lease_until().unwrap_or(0)
        } else {
            0
        };
        self.shared.lease_until.store(lease, Ordering::Release);
        let s = NodeStatus {
            view: self.replica.view(),
            primary,
            ready,
            stopped: false,
            epoch: self.replica.membership().epoch,
            retired: self.replica.retired(),
            joining: self.replica.joining(),
            op: self.replica.op(),
            checkpoint: self.replica.base(),
            commit: self.replica.commit(),
            applied: self.applied,
        };
        // Progress fields change with every commit. Readers that poll see
        // them in place; waiters are woken only when the role, view,
        // readiness, stop state or membership changes, which is all any of
        // them waits on.
        let changed = self.shared.status.send_if_modified(|old| {
            let notify = old.view != s.view
                || old.primary != s.primary
                || old.ready != s.ready
                || old.stopped != s.stopped
                || old.epoch != s.epoch
                || old.retired != s.retired
                || old.joining != s.joining;
            *old = s;
            notify
        });
        if changed {
            self.shared.role_changed();
        }
    }
}

// ---------------------------------------------------------------- env

/// How a shard's flushes reach the group.
enum Mode {
    /// A segment group's only shard: each flush is one entry.
    Segments {
        slot: Arc<Mutex<Slot>>,
        inflight: bool,
    },
    /// A log group: the shard's section joins the next log batch.
    Log {
        index: u32,
        slot: Arc<log3::ShardSlot>,
        /// The last position submitted and not yet committed, and the
        /// committed position last reported to the runtime.
        want: Option<Lsn>,
        reported: Lsn,
        recovery: Option<Arc<log3::Recovery>>,
        /// Timings only: when the shard last saw its records committed.
        resumed_ns: u64,
    },
}

/// The primary engine's storage: the local engine directory for the run
/// store and checkpoints; the replication group for the log.
pub struct ReplicatedEnv {
    local: LocalEnv,
    shared: Arc<Shared>,
    epoch: u64,
    mode: Mode,
    checkpoint_error: Option<io::Error>,
    checkpoint_prefix: Option<snapshot::CheckpointPrefix>,
    shard: PathBuf,
    _worker_lock: File,
    /// A standby shard: it waits for this replica to become the ready
    /// primary ([`Env::wait_lease`]).
    standby: Option<Standby>,
}

/// What a standby shard needs to start when its replica is promoted.
struct Standby {
    /// The engine's shard count, which must be the group's.
    shards: u32,
    /// The primary epoch its engine runs in (0: not promoted yet).
    tenure: Arc<AtomicU64>,
}

impl ReplicatedEnv {
    fn new(
        local: LocalEnv,
        shared: Arc<Shared>,
        epoch: u64,
        mode: Mode,
        shard: PathBuf,
        lock: File,
    ) -> Self {
        Self {
            local,
            shared,
            epoch,
            mode,
            checkpoint_error: None,
            checkpoint_prefix: None,
            shard,
            _worker_lock: lock,
            standby: None,
        }
    }

    fn deposed(&self) -> bool {
        self.shared.proposals.is_closed() || self.shared.epoch.load(Ordering::Acquire) != self.epoch
    }
}

impl Env for ReplicatedEnv {
    fn monotonic_us(&self) -> u64 {
        fusee::local::monotonic_us()
    }
    fn now_ms(&self) -> u64 {
        self.local.now_ms()
    }
    fn random(&mut self) -> u64 {
        self.local.random()
    }
    fn set_notify(&mut self, notify: Notify) {
        match &self.mode {
            Mode::Segments { slot, .. } => {
                if let Ok(mut s) = slot.lock() {
                    s.notify = Some(notify.clone());
                }
            }
            Mode::Log { slot, .. } => {
                *slot.notify.lock().unwrap_or_else(|e| e.into_inner()) = Some(notify.clone());
            }
        }
        self.local.set_notify(notify);
    }
    fn submit(&mut self, batch: Batch) {
        match &mut self.mode {
            Mode::Segments { slot, inflight } => {
                *inflight = true;
                let mut body = Vec::with_capacity(batch.bytes as usize + 64);
                proto::encode_batch(&mut body, &batch);
                let p = Proposal {
                    queued: self.shared.traffic.timings.start(),
                    control: None,
                    body: Arc::from(body),
                    last_lsn: batch.last_lsn,
                    epoch: self.epoch,
                    slot: slot.clone(),
                };
                if self.shared.epoch.load(Ordering::Acquire) != self.epoch {
                    finish(slot, Err(other("this replica is no longer the primary")));
                    return;
                }
                if self.shared.proposals.send(p).is_err() {
                    finish(slot, Err(other("the replica has stopped")));
                }
            }
            Mode::Log {
                index,
                slot,
                want,
                resumed_ns,
                ..
            } => {
                *want = Some(batch.last_lsn);
                if batch.section.is_empty() && batch.records > 0 {
                    slot.fail();
                    return;
                }
                let timings = &self.shared.traffic.timings;
                timings.resume_to_stage.since_ns(std::mem::take(resumed_ns));
                // A lone shard (nothing else staged or queued, few shards
                // working) proposes its section itself if the I/O thread is
                // not busy, rather than wake it.
                let alone = !self.shared.batcher.has_staged()
                    && self.shared.proposals.is_empty()
                    && fusee::spin::few_busy();
                self.shared.batcher.stage(log3::Staged {
                    epoch: self.epoch,
                    shard: *index,
                    batch,
                    slot: slot.clone(),
                    staged_ns: timings.stamp(),
                });
                if alone
                    && let Some(core) = self.shared.core.get().and_then(std::sync::Weak::upgrade)
                    && let Ok(mut core) = core.try_lock()
                {
                    core.inline_round();
                    return;
                }
                // Free unless the I/O thread is parked: it spins on staged
                // sections before it parks.
                self.shared.wake.notify_one();
            }
        }
    }
    fn poll_flushed(&mut self) -> Option<io::Result<Lsn>> {
        let deposed = self.deposed();
        match &mut self.mode {
            Mode::Segments { slot, inflight } => {
                let r = slot.lock().ok()?.result.take();
                if r.is_some() {
                    *inflight = false;
                    return r;
                }
                // The replica stopped, or this node lost the primary role:
                // the flush will never be confirmed here.
                if *inflight && deposed {
                    *inflight = false;
                    return Some(Err(other("this replica is no longer the primary")));
                }
                None
            }
            Mode::Log {
                slot,
                want,
                reported,
                resumed_ns,
                ..
            } => {
                let w = (*want)?;
                let committed = slot.committed.load(Ordering::Acquire);
                if committed >= w {
                    *want = None;
                    *reported = committed;
                    let timings = &self.shared.traffic.timings;
                    timings
                        .release_to_resume
                        .since_ns(slot.released_ns.swap(0, Ordering::Relaxed));
                    *resumed_ns = timings.stamp();
                    return Some(Ok(committed));
                }
                if slot.failed.load(Ordering::Acquire) || deposed {
                    *want = None;
                    return Some(Err(other("this replica is no longer the primary")));
                }
                // An earlier section of several in flight committed.
                if committed > *reported {
                    *reported = committed;
                    return Some(Ok(committed));
                }
                None
            }
        }
    }
    fn max_in_flight(&self) -> usize {
        match self.mode {
            Mode::Log { .. } => SECTIONS_IN_FLIGHT,
            Mode::Segments { .. } => 1,
        }
    }
    fn store_append(&mut self, frame: &[u8]) -> io::Result<u64> {
        self.local.store_append(frame)
    }
    fn store_append_history(
        &mut self,
        frame: &[u8],
        run: fusee_core::RunId,
        lsn: Lsn,
    ) -> io::Result<u64> {
        self.local.store_append_history(frame, run, lsn)
    }
    fn store_read(&mut self, offset: u64, len: u32) -> io::Result<Vec<u8>> {
        self.local.store_read(offset, len)
    }
    fn store_len(&self) -> u64 {
        self.local.store_len()
    }
    fn store_generation(&self) -> u64 {
        self.local.store_generation()
    }
    fn compact_store(
        &mut self,
        live: &[(fusee_core::RunId, fusee_core::shard::BlobRef)],
    ) -> io::Result<bool> {
        self.local.compact_store(live)
    }
    fn visibility_candidates(
        &mut self,
        filter: &fusee::visibility::ListFilter,
        limit: usize,
    ) -> Option<fusee::visibility::Candidates> {
        self.local.visibility_candidates(filter, limit)
    }
    fn checkpoint(&mut self, checkpoint: Checkpoint, delete_through_segment: Option<u64>) {
        let prefix = snapshot::CheckpointPrefix {
            lsn: checkpoint.lsn,
            generation: checkpoint.store_generation,
            len: checkpoint.store_len,
        };
        if let Mode::Log { .. } = self.mode {
            // No engine segments: the protocol log is the only copy, and
            // only a checkpoint image trims it.
            self.checkpoint_prefix = Some(prefix);
            self.local.checkpoint(checkpoint, None);
            return;
        }
        // Before dropping any engine segment, the replica must durably
        // record that replay will start after all entries the checkpoint covers.
        let (tx, rx) = std::sync::mpsc::sync_channel(1);
        let result = self
            .shared
            .barriers
            .send(Barrier::Checkpoint {
                epoch: self.epoch,
                reply: tx,
            })
            .map_err(|_| other("replica stopped before checkpoint"))
            .and_then(|()| {
                rx.recv()
                    .map_err(|_| other("replica stopped during checkpoint"))?
            });
        match result {
            Ok(()) => {
                self.checkpoint_prefix = Some(prefix);
                self.local.checkpoint(checkpoint, delete_through_segment);
            }
            Err(e) => self.checkpoint_error = Some(e),
        }
    }
    fn poll_checkpoint(&mut self) -> Option<io::Result<Lsn>> {
        let result = self
            .checkpoint_error
            .take()
            .map(Err)
            .or_else(|| self.local.poll_checkpoint())?;
        let index = match &self.mode {
            Mode::Log { index, .. } => Some(*index),
            Mode::Segments { .. } => None,
        };
        Some(result.and_then(|lsn| {
            if let Some(prefix) = self.checkpoint_prefix.take()
                && prefix.lsn == lsn
                && self.shared.epoch.load(Ordering::Acquire) == self.epoch
                && let Ok(slot) = self.shared.checkpoints.try_reserve()
            {
                let sources = snapshot::Sources::pin(&self.shard, prefix, index)?;
                slot.send((self.epoch, sources));
            }
            Ok(lsn)
        }))
    }
    fn load_checkpoint(&mut self) -> io::Result<Option<Checkpoint>> {
        self.local.load_checkpoint()
    }
    fn load_store(&mut self, generation: u64, len: u64) -> io::Result<Vec<u8>> {
        self.local.load_store(generation, len)
    }
    fn recover_store(
        &mut self,
        generation: u64,
        len: u64,
        visit: &mut dyn FnMut(u64, u32, fusee_core::store::StoreEntry),
    ) -> io::Result<()> {
        self.local.recover_store(generation, len, visit)
    }
    fn segments(&mut self) -> io::Result<Vec<u64>> {
        match self.mode {
            Mode::Log { .. } => Ok(Vec::new()),
            Mode::Segments { .. } => self.local.segments(),
        }
    }
    fn read_segment(&mut self, segment: u64) -> io::Result<Vec<u8>> {
        self.local.read_segment(segment)
    }
    fn sync_recovered(&mut self) -> io::Result<()> {
        match self.mode {
            Mode::Log { .. } => Ok(()),
            Mode::Segments { .. } => self.local.sync_recovered(),
        }
    }
    fn discard_segment(&mut self, segment: u64) -> io::Result<()> {
        match self.mode {
            Mode::Log { .. } => Ok(()),
            Mode::Segments { .. } => self.local.discard_segment(segment),
        }
    }
    fn open_log(&mut self, segment: u64) -> io::Result<()> {
        match self.mode {
            Mode::Log { .. } => Ok(()),
            Mode::Segments { .. } => self.local.open_log(segment),
        }
    }
    fn shared_log(&self) -> bool {
        matches!(self.mode, Mode::Log { .. })
    }
    fn recover_log(&mut self, after: Lsn) -> io::Result<(Vec<(Lsn, RecordBuf)>, Lsn)> {
        let Mode::Log {
            index,
            slot,
            recovery,
            ..
        } = &mut self.mode
        else {
            return Err(other(
                "a replicated segment group's shard has per-shard segments",
            ));
        };
        let r = recovery
            .take()
            .ok_or_else(|| other("this replica is no longer the primary"))?;
        let (records, last) = r.records(*index, after)?;
        slot.committed.fetch_max(last, Ordering::AcqRel);
        Ok((records, last))
    }
    fn log_segment(&self) -> u64 {
        // A new round asks every active shard for a checkpoint.
        self.shared.checkpoint_round.load(Ordering::Acquire)
    }
    fn checkpoint_wanted(&self) -> bool {
        match self.mode {
            Mode::Log { index, .. } => {
                index < 64
                    && self.shared.checkpoint_wanted.load(Ordering::Acquire) & (1 << index) != 0
            }
            Mode::Segments { .. } => false,
        }
    }
    fn standby(&self) -> bool {
        self.standby.is_some()
    }
    fn wait_lease(&mut self, timeout: Duration) -> io::Result<bool> {
        self.wait_promotion(timeout)
    }
}

#[cfg(test)]
mod materialization_tests {
    use super::*;
    use fusee_core::record::{Record, Status};
    use fusee_core::segment::LogWriter;

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn recovery_replays_quorum_data_after_loss_of_staged_segment_bytes() {
        let dir = std::env::temp_dir().join(format!(
            "fusee-materialize-{}-{}",
            std::process::id(),
            fusee::local::unix_ms()
        ));
        let local = LocalOptions {
            flush: FlushMethod::Fsync,
            preallocate: Preallocate::None,
            ..Default::default()
        };
        let mut engine_log = LogWriter::new(0, 1, 1, 64 << 10, 1);
        let record: fusee_core::RecordRef<'_> = Record::Done {
            run: 1,
            status: Status::Ok,
            finished_ms: 10,
            payload: b"42",
            by: None,
        };
        engine_log.append(&record, 10).unwrap();
        let batch = engine_log.take().unwrap();
        let mut body = Vec::new();
        proto::encode_batch(&mut body, &batch);
        let vsr = dir.join("vsr");
        let (mut log, _, _) = VsrLog::open(&vsr, local.flush, 0, 0, 0).unwrap();
        log.append(&[Entry::new(1, 0, 0, Arc::from(body))]).unwrap();
        log.save(&Superblock {
            view: 0,
            last_normal: 0,
            commit: 1,
            applied: 0,
        })
        .unwrap();
        drop(log);
        let segment_dir = dir.join("engine/shard-000/log");
        let mut writer = BatchWriter::new(&segment_dir, local.clone()).unwrap();
        writer.stage(&batch).unwrap();
        let file = std::fs::read_dir(segment_dir)
            .unwrap()
            .next()
            .unwrap()
            .unwrap()
            .path();
        let expected = std::fs::read(&file).unwrap();
        drop(writer);
        // A power loss can leave the materialized file truncated; the VSR
        // entry and the earlier applied watermark are known durable.
        std::fs::write(&file, &expected[..12]).unwrap();
        let node = ReplicaNode::start(ReplicaConfig {
            id: 0,
            peers: vec!["127.0.0.1:0".into(); 3],
            join: false,
            dir: dir.clone(),
            local,
            timing: Default::default(),
            security: Default::default(),
        })
        .await
        .unwrap();
        assert_eq!(std::fs::read(file).unwrap(), expected);
        assert_eq!(read_superblock(&vsr.join("superblock")).unwrap().applied, 1);
        node.kill();
        std::fs::remove_dir_all(dir).unwrap();
    }

    fn chain(n: Op) -> Vec<Entry> {
        let mut parent = 0;
        (1..=n)
            .map(|op| {
                let e = Entry::new(op, 1, parent, Arc::from(vec![op as u8; 100 + op as usize]));
                parent = e.checksum;
                e
            })
            .collect()
    }

    fn scratch(name: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "fusee-vsrlog-{name}-{}-{}",
            std::process::id(),
            fusee::local::unix_ms()
        ));
        let _ = std::fs::remove_dir_all(&dir);
        dir
    }

    /// The writes a log would issue, applied to a disk image in memory.
    fn apply(image: &mut Vec<u8>, p: &writes::Placement) {
        let bytes = p.bytes.as_slice();
        let end = p.offset as usize + bytes.len();
        if image.len() < end {
            image.resize(end, 0);
        }
        image[p.offset as usize..end].copy_from_slice(bytes);
    }

    fn read_image(dir: &Path, image: &[u8], committed: Op) -> (Vec<Entry>, Option<(Op, Op)>) {
        let _ = std::fs::remove_dir_all(dir);
        std::fs::create_dir_all(dir).unwrap();
        std::fs::write(dir.join("log"), image).unwrap();
        let (_, back, damaged) = VsrLog::open(dir, FlushMethod::Fsync, committed, 0, 0).unwrap();
        (back, damaged)
    }

    /// A log written in whole aligned blocks (as through `O_DIRECT`).
    fn aligned_log(dir: &Path) -> VsrLog {
        let (mut log, _, _) = VsrLog::open(dir, FlushMethod::Fsync, 0, 0, 0).unwrap();
        log.align().unwrap();
        log
    }

    /// A staged generation's tail, read from the end of the log file: the
    /// entries after any position, across padded writes, whether the first
    /// window held the tail's start or had to widen; nothing for a position
    /// whose checksum is not the file's, or past its end.
    #[test]
    fn a_tail_is_read_from_the_end_of_the_log_file() {
        let dir = scratch("tail");
        let entries = chain(300);
        let mut log = VsrLog::open(&dir, FlushMethod::Fsync, 0, 0, 0).unwrap().0;
        for (i, e) in entries.iter().enumerate() {
            let mut frame = Vec::new();
            VsrLog::frame(e, &mut frame);
            let p = log.place(frame, i % 3 == 0).unwrap();
            log.file.write_all_at(p.bytes.as_slice(), p.offset).unwrap();
        }
        log.sync().unwrap();
        let (path, end) = (dir.join("log"), log.end);
        for window in [64, 4096, 1 << 20] {
            for after in [0usize, 1, 150, 299] {
                let (op, sum) = if after == 0 {
                    (0, 0)
                } else {
                    (entries[after - 1].op, entries[after - 1].checksum)
                };
                let tail = VsrLog::read_tail_from(&path, (op, sum), end, window).unwrap();
                assert_eq!(tail, entries[after..], "window {window}, after {after}");
            }
            let wrong = VsrLog::read_tail_from(&path, (150, 1), end, window).unwrap();
            assert!(wrong.is_empty());
            let last = entries.last().unwrap();
            let none =
                VsrLog::read_tail_from(&path, (last.op, last.checksum), end, window).unwrap();
            assert!(none.is_empty());
        }
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Writes placed while another is in flight are padded to the end of
    /// their block; the log reads back whole across the padding, and a lost
    /// padded write before a landed one is damage to repair, never a gap
    /// silently skipped. The same with whole-block writes, where a packed
    /// write's last block is written again, with the same bytes, by the next.
    #[test]
    fn padded_writes_read_back_and_a_lost_one_is_reported_as_damage() {
        for aligned in [false, true] {
            let dir = scratch(if aligned { "padded-aligned" } else { "padded" });
            let entries = chain(6);
            let mut log = if aligned {
                aligned_log(&dir)
            } else {
                VsrLog::open(&dir, FlushMethod::Fsync, 0, 0, 0).unwrap().0
            };
            let mut placed = Vec::new();
            for (i, e) in entries.iter().enumerate() {
                let mut frame = Vec::new();
                VsrLog::frame(e, &mut frame);
                let p = log.place(frame, i % 2 == 0).unwrap();
                let end = p.offset + p.bytes.as_slice().len() as u64;
                if i % 2 == 0 {
                    assert!(p.padded && end.is_multiple_of(4096));
                }
                if aligned {
                    assert!(p.offset.is_multiple_of(4096) && end.is_multiple_of(4096));
                }
                log.file.write_all_at(p.bytes.as_slice(), p.offset).unwrap();
                placed.push(p);
            }
            log.sync().unwrap();
            drop(log);
            let (_, back, damaged) = VsrLog::open(&dir, FlushMethod::Fsync, 6, 0, 0).unwrap();
            assert_eq!(back, entries);
            assert_eq!(damaged, None);
            // Entry 3's padded write never landed; entry 4's did.
            let mut image = Vec::new();
            for (i, p) in placed.iter().enumerate() {
                if i != 2 {
                    apply(&mut image, p);
                }
            }
            let (back, damaged) = read_image(&dir, &image, 2);
            assert_eq!(damaged, Some((3, 3)), "aligned {aligned}");
            assert_eq!(
                back.iter().map(|e| e.op).collect::<Vec<_>>(),
                [1, 2, 4, 5, 6]
            );
            std::fs::remove_dir_all(dir).unwrap();
        }
    }

    /// Crash states of the protocol log, up to two writes in flight as the
    /// primary's background writes run them, written packed or in whole
    /// blocks (as through `O_DIRECT`): every write that completed in order
    /// keeps every entry it carried, whatever happened to the writes in
    /// flight (landed, lost, or torn at any 512-byte sector, including one
    /// rewriting a completed write's last block), and nothing read back
    /// differs from what was written. (It found that padding shorter than a
    /// length field before a block boundary read as damage.)
    #[test]
    fn writes_keep_every_durable_entry_in_any_crash_state() {
        let dir = scratch("crash-states");
        let mut rng = 0x5eed_u64;
        let mut next = |n: u64| {
            rng ^= rng << 13;
            rng ^= rng >> 7;
            rng ^= rng << 17;
            rng % n
        };
        let entries = chain(400);
        for aligned in [false, true] {
            let mut checked = 0;
            for trial in 0..300 {
                let log_dir = dir.join(format!("log-{aligned}-{trial}"));
                std::fs::create_dir_all(&log_dir).unwrap();
                let mut log = if aligned {
                    aligned_log(&log_dir)
                } else {
                    VsrLog::open(&log_dir, FlushMethod::Fsync, 0, 0, 0)
                        .unwrap()
                        .0
                };
                let mut image = Vec::new();
                // (placement, last op), oldest first; completion in order.
                let mut in_flight: std::collections::VecDeque<(writes::Placement, Op)> =
                    Default::default();
                let mut waiting: Vec<u8> = Vec::new();
                let mut waiting_last = 0;
                let mut busy = false;
                let mut durable = 0;
                let mut op = 0usize;
                let crash_after = 5 + next(120) as usize;
                for _ in 0..crash_after {
                    if op < entries.len() {
                        // A round: one to three entries.
                        for _ in 0..1 + next(3) {
                            if op < entries.len() {
                                VsrLog::frame(&entries[op], &mut waiting);
                                waiting_last = entries[op].op;
                                op += 1;
                            }
                        }
                        if !in_flight.is_empty() {
                            busy = true;
                        }
                    }
                    // Start what may start (the rule in `writes::Writes`).
                    if !waiting.is_empty()
                        && in_flight.len() < writes::IN_FLIGHT
                        && in_flight.back().is_none_or(|(p, _)| p.padded)
                    {
                        let pad = busy || !in_flight.is_empty();
                        let p = log.place(std::mem::take(&mut waiting), pad).unwrap();
                        in_flight.push_back((p, waiting_last));
                    }
                    // The oldest write completes now and then.
                    if !in_flight.is_empty() && next(3) == 0 {
                        let (p, last) = in_flight.pop_front().unwrap();
                        apply(&mut image, &p);
                        durable = last;
                        if in_flight.is_empty() && waiting.is_empty() {
                            busy = false;
                        }
                    }
                }
                // Crash: each write in flight landed, was lost, or tore.
                for (p, _) in &in_flight {
                    match next(3) {
                        0 => apply(&mut image, p),
                        1 => {}
                        _ => {
                            let bytes = p.bytes.as_slice();
                            let end = p.offset as usize + bytes.len();
                            if image.len() < end {
                                image.resize(end, 0);
                            }
                            for s in (0..bytes.len()).step_by(512) {
                                if next(2) == 0 {
                                    let e = (s + 512).min(bytes.len());
                                    let at = p.offset as usize + s;
                                    image[at..at + e - s].copy_from_slice(&bytes[s..e]);
                                }
                            }
                        }
                    }
                }
                let (back, _) = read_image(&log_dir, &image, durable);
                for e in &back {
                    assert_eq!(
                        *e,
                        entries[e.op as usize - 1],
                        "aligned {aligned}, trial {trial}: entry {} differs",
                        e.op
                    );
                }
                let held: std::collections::BTreeSet<Op> = back.iter().map(|e| e.op).collect();
                for o in 1..=durable {
                    assert!(
                        held.contains(&o),
                        "aligned {aligned}, trial {trial}: durable entry {o} lost"
                    );
                }
                checked += durable;
            }
            assert!(checked > 1000, "too few durable entries checked: {checked}");
        }
        std::fs::remove_dir_all(dir).unwrap();
    }

    /// A filesystem that refuses an `O_DIRECT` write (`EINVAL`) moves the log
    /// to buffered writes for good: the refused write is written again
    /// buffered, later ones are packed, and everything reads back.
    #[test]
    fn a_refused_direct_write_falls_back_to_buffered_writes() {
        let dir = scratch("refused");
        let entries = chain(8);
        let mut log = aligned_log(&dir);
        // A descriptor standing in for O_DIRECT (macOS has none) whose
        // writes the "filesystem" refuses.
        log.direct = Some(log.file.clone());
        log.reject_direct = true;
        for e in &entries[..4] {
            let mut frame = Vec::new();
            VsrLog::frame(e, &mut frame);
            log.write_frames(frame).unwrap();
        }
        assert!(log.refused.load(Ordering::Acquire));
        assert!(
            log.direct.is_none() && !log.aligned,
            "buffered after the refusal"
        );
        for e in &entries[4..] {
            let mut frame = Vec::new();
            VsrLog::frame(e, &mut frame);
            log.write_frames(frame).unwrap();
        }
        log.sync().unwrap();
        drop(log);
        let (_, back, damaged) = VsrLog::open(&dir, FlushMethod::Fsync, 8, 0, 0).unwrap();
        assert_eq!(back, entries);
        assert_eq!(damaged, None);
        std::fs::remove_dir_all(dir).unwrap();
    }

    /// Background writes complete in placement order, two at a time at
    /// most, and a drain waits for all of them; packed or in whole blocks.
    #[test]
    fn background_writes_report_durability_in_order() {
        for aligned in [false, true] {
            let dir = scratch(if aligned { "writes-aligned" } else { "writes" });
            let entries = chain(40);
            let mut log = if aligned {
                aligned_log(&dir)
            } else {
                VsrLog::open(&dir, FlushMethod::Fsync, 0, 0, 0).unwrap().0
            };
            let wake = Arc::new(tokio::sync::Notify::new());
            let mut writes =
                writes::Writes::new(writes::LogIo::start(wake).unwrap(), Duration::ZERO);
            let target = log.target();
            let mut durable = 0;
            for e in &entries {
                let mut frame = Vec::new();
                VsrLog::frame(e, &mut frame);
                writes
                    .submit(frame, e.op, &target, &mut |b, pad| log.place(b, pad))
                    .unwrap();
                if let Some(op) = writes
                    .poll(&target, &mut |b, pad| log.place(b, pad))
                    .unwrap()
                {
                    assert!(op > durable && op <= e.op);
                    durable = op;
                }
            }
            let last = writes
                .drain(&target, &mut |b, pad| log.place(b, pad))
                .unwrap();
            assert!(writes.idle());
            assert_eq!(last.unwrap_or(durable), 40);
            drop(writes);
            drop(log);
            let (_, back, damaged) = VsrLog::open(&dir, FlushMethod::Fsync, 40, 0, 0).unwrap();
            assert_eq!(back, entries);
            assert_eq!(damaged, None);
            std::fs::remove_dir_all(dir).unwrap();
        }
    }

    /// On Linux with `direct_io`, the log really opens with `O_DIRECT` (on a
    /// filesystem that allows it; otherwise it says so and stays buffered),
    /// and appends, a replacement and a reopen keep every entry.
    #[test]
    fn the_protocol_log_writes_through_o_direct_where_the_filesystem_allows() {
        let dir = scratch("direct");
        let entries = chain(30);
        let (mut log, _, _) = VsrLog::open(&dir, FlushMethod::Dsync, 0, 0, 0).unwrap();
        log.use_direct(true).unwrap();
        let direct = log.direct.is_some();
        if cfg!(target_os = "linux") {
            assert!(direct || log.refused.load(Ordering::Acquire));
        } else {
            assert!(!direct && !log.aligned);
        }
        for e in &entries[..10] {
            let mut frame = Vec::new();
            VsrLog::frame(e, &mut frame);
            log.write_frames(frame).unwrap();
        }
        log.sync().unwrap();
        log.replace(entries[..20].iter()).unwrap();
        assert_eq!(
            log.direct.is_some(),
            direct,
            "a replaced log is written as before"
        );
        for e in &entries[20..] {
            let mut frame = Vec::new();
            VsrLog::frame(e, &mut frame);
            log.write_frames(frame).unwrap();
        }
        log.sync().unwrap();
        drop(log);
        let (mut log, back, damaged) = VsrLog::open(&dir, FlushMethod::Dsync, 30, 0, 0).unwrap();
        assert_eq!(back, entries);
        assert_eq!(damaged, None);
        // Reopened in place, the kept tail block continues the log.
        log.use_direct(true).unwrap();
        let more = {
            let mut parent = entries.last().unwrap().checksum;
            (31..=35)
                .map(|op| {
                    let e = Entry::new(op, 1, parent, Arc::from(vec![7u8; 333]));
                    parent = e.checksum;
                    e
                })
                .collect::<Vec<_>>()
        };
        for e in &more {
            let mut frame = Vec::new();
            VsrLog::frame(e, &mut frame);
            log.write_frames(frame).unwrap();
        }
        log.sync().unwrap();
        drop(log);
        let (_, back, damaged) = VsrLog::open(&dir, FlushMethod::Dsync, 35, 0, 0).unwrap();
        assert_eq!(back.len(), 35);
        assert_eq!(&back[30..], &more[..]);
        assert_eq!(damaged, None);
        std::fs::remove_dir_all(dir).unwrap();
    }
}