fusee-sim 1.0.0

Deterministic simulation testing for Fusee: seeded scheduler, faulty disks and networks, and correctness checkers
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
//! The simulated world: shards on one machine, a client, an external
//! service, a seeded scheduler and fault injection.

use crate::disk::SimFile;
use crate::env::{Io, Machine, MachineRef, SimEnv, SimLog, SimLogRef};
use crate::rng::Rng;
use crate::scenario::{Check, Outcome, Setup, UserAction};
use fusee::codec::Codec;
use fusee::ctx::{ChildOptions, Ctx, RetryPolicy, StepOptions};
use fusee::error::Error;
use fusee::limits::{LimitBackend, LimitSpec, LocalLimits};
use fusee::queue::{BATCH_TYPE, ENDED, EVENT, Event, QueueState, Receipt, Rule};
use fusee::runtime::{
    IdPolicy, Message, OnFinished, OnRunning, ParentClose, ResultState, RunOutcome, RuntimeConfig,
    Services, ShardRuntime, SharedRegistry, StartRequest, UpdateRequest, WorkflowFn,
};
use fusee::schedule::{Overlap, SCHEDULE_TYPE, ScheduleSpec, ScheduleState, schedule_id};
use fusee_core::record::{Payload, Record, Status};
use fusee_core::shard::{Output, shard_of};
use serde::{Deserialize, Serialize};
use std::cell::{Cell, RefCell};
use std::cmp::Reverse;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap};
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::task::{Context, Poll, Waker};
use std::time::Duration;

/// Knobs for one simulation run.
#[derive(Clone, Debug)]
pub struct SimConfig {
    pub seed: u64,
    pub shards: u32,
    pub workflows: u32,
    /// Simulated milliseconds of client activity with faults on.
    pub duration_ms: u64,
    /// Mean simulated milliseconds between crashes (0 = never).
    pub crash_every_ms: u64,
    pub tear: bool,
    pub flush_fail_per_mille: u32,
    pub max_io_ms: u64,
    pub max_msg_ms: u64,
    pub park_after_ms: u64,
    pub checkpoint_interval_ms: u64,
    pub segment_bytes: u64,
    /// Log format 3: the shards share one log ([`fusee::wal`]); otherwise
    /// each has its own segments.
    pub shared_log: bool,
    /// The shared log offloads records whose encoding is longer than this
    /// to its blob area (0: none). Low, so that ordinary records go there.
    pub offload_bytes: u64,
    /// Simulated milliseconds every acknowledged workflow gets to finish
    /// once faults stop.
    pub settle_ms: u64,
    /// A user scenario ([`crate::scenario`]) in place of the built-in
    /// workload.
    pub user: Option<crate::scenario::UserSetup>,
    #[doc(hidden)]
    pub sabotage: fusee::runtime::Sabotage,
    /// Sabotage: the shared log writes a batch as soon as the blobs it
    /// references are written, before they are durable.
    #[doc(hidden)]
    pub early_blobs: bool,
}

impl SimConfig {
    pub fn for_seed(seed: u64) -> Self {
        let mut r = Rng::new(seed);
        Self {
            seed,
            shards: 1 + r.below(3) as u32,
            workflows: 10 + r.below(40) as u32,
            duration_ms: 2_000,
            crash_every_ms: [0, 150, 400, 900][r.below(4) as usize],
            tear: true,
            flush_fail_per_mille: [0, 0, 2, 10][r.below(4) as usize],
            max_io_ms: 1 + r.below(4),
            max_msg_ms: r.below(3),
            park_after_ms: [0, 5, 50, u64::MAX][r.below(4) as usize],
            checkpoint_interval_ms: [20, 100, 500][r.below(3) as usize],
            segment_bytes: [16 << 10, 64 << 10, 1 << 20][r.below(3) as usize],
            // By parity, so the other knobs of every seed stay as they were.
            shared_log: seed % 2 == 1,
            // From bits of the seed the other knobs do not use.
            offload_bytes: [0, 16, 40, 120][(seed / 18 % 4) as usize],
            settle_ms: 60_000,
            user: None,
            sabotage: Default::default(),
            early_blobs: false,
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct Report {
    pub seed: u64,
    pub crashes: u32,
    pub flush_failures: u32,
    pub workflows: u32,
    pub finished: u32,
    pub service_calls: u64,
    pub sim_ms: u64,
    pub records: u64,
    /// Feature coverage: update replies, query answers, completions
    /// accepted, scheduled runs, resets acknowledged, heartbeat progress
    /// reports.
    pub updates: u32,
    pub queries: u32,
    pub completions: u32,
    pub scheduled: u32,
    pub resets: u32,
    pub heartbeats: u32,
    /// Read-only calls that ran again with a different input after a crash
    /// took back what they had read from.
    pub reads_rerun: u32,
    /// Debounced or batched events acknowledged, and the window runs they
    /// joined.
    pub queued: u32,
    pub windows: u32,
    /// Stream chunks readers received, and readers that read a stream to
    /// its end.
    pub chunks_read: u64,
    pub streams_read: u32,
    /// The seed ran on the shared log (log format 3), and the batches the
    /// last process life ended with.
    pub shared_log: bool,
    pub log_batches: u64,
    /// Records the shared log offloaded to its blob area, over all lives.
    pub offloaded: u64,
}

#[derive(Clone, Debug)]
pub struct Failure {
    pub seed: u64,
    pub message: String,
    pub trace: Vec<String>,
    /// A user scenario's seed ([`crate::scenario::Scenario::run`]).
    pub scenario: bool,
}

impl std::fmt::Display for Failure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "seed {} failed: {}", self.seed, self.message)?;
        if self.scenario {
            writeln!(f, "reproduce: scenario.run({})", self.seed)?;
        } else {
            writeln!(f, "reproduce: fusee-sim --seed {}", self.seed)?;
        }
        for l in self.trace.iter().rev().take(60).rev() {
            writeln!(f, "  {l}")?;
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Simulated time for step functions.

thread_local! {
    static NOW: Cell<u64> = const { Cell::new(0) };
    static TIMERS: RefCell<BinaryHeap<Reverse<(u64, u64)>>> = const { RefCell::new(BinaryHeap::new()) };
    static WAKERS: RefCell<BTreeMap<u64, Waker>> = const { RefCell::new(BTreeMap::new()) };
    static TIMER_SEQ: Cell<u64> = const { Cell::new(0) };
}

/// Sleeps in simulated time (for step functions that model slow services).
pub struct SimSleep {
    at: u64,
    armed: Option<u64>,
}

pub fn sim_sleep(ms: u64) -> SimSleep {
    SimSleep {
        at: NOW.with(Cell::get) + ms,
        armed: None,
    }
}

impl Future for SimSleep {
    type Output = ();
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        if NOW.with(Cell::get) >= self.at {
            return Poll::Ready(());
        }
        if self.armed.is_none() {
            let id = TIMER_SEQ.with(|s| {
                let v = s.get();
                s.set(v + 1);
                v
            });
            TIMERS.with(|t| t.borrow_mut().push(Reverse((self.at, id))));
            WAKERS.with(|w| w.borrow_mut().insert(id, cx.waker().clone()));
            self.armed = Some(id);
        } else if let Some(id) = self.armed {
            WAKERS.with(|w| w.borrow_mut().insert(id, cx.waker().clone()));
        }
        Poll::Pending
    }
}

fn sim_timers_next() -> Option<u64> {
    TIMERS.with(|t| t.borrow().peek().map(|Reverse((at, _))| *at))
}

fn sim_timers_fire(now: u64) -> bool {
    NOW.with(|n| n.set(now));
    let mut any = false;
    loop {
        let due = TIMERS.with(|t| {
            let mut t = t.borrow_mut();
            match t.peek() {
                Some(Reverse((at, id))) if *at <= now => {
                    let id = *id;
                    t.pop();
                    Some(id)
                }
                _ => None,
            }
        });
        let Some(id) = due else { break };
        if let Some(w) = WAKERS.with(|w| w.borrow_mut().remove(&id)) {
            w.wake();
            any = true;
        }
    }
    any
}

fn sim_timers_clear() {
    TIMERS.with(|t| t.borrow_mut().clear());
    WAKERS.with(|w| w.borrow_mut().clear());
}

// ---------------------------------------------------------------------------
// The external service.

/// A mock external service. It logs every call with its idempotency key and
/// input. Responses differ per call, so a step that runs again yields a
/// different value; if any later step had already run on the first value,
/// the log shows one key called with two different inputs.
#[derive(Default)]
pub struct Service {
    pub calls: BTreeMap<String, Vec<u64>>,
    pub violations: Vec<String>,
    pub total: u64,
    pub seed: u64,
    /// Progress each step key reported through heartbeats.
    pub progress: BTreeMap<String, BTreeSet<u32>>,
    pub stalled: BTreeSet<String>,
    /// Completion tokens handed out, in order: (workflow ID, token).
    pub tokens: Vec<(String, String)>,
    pub issued: BTreeMap<String, u32>,
    /// Runs seen per scheduled workflow ID.
    pub ticks: BTreeMap<String, BTreeSet<u64>>,
    /// Inputs of read-only calls per key. Reads may run on state a crash
    /// takes back, so a key read with different inputs is allowed; it is
    /// counted to show that read-only steps really run ahead of the disk.
    pub reads: BTreeMap<String, Vec<u64>>,
    pub reads_rerun: u32,
    /// Tokens a model step asked for per key, and the keys whose call
    /// failed once.
    pub talk_calls: BTreeMap<String, u64>,
    pub talk_failed: BTreeSet<String>,
}

impl Service {
    fn violation(&mut self, v: String) {
        if self.violations.len() < 16 {
            self.violations.push(v);
        }
    }

    /// A step resumed from heartbeat progress `start`: it must be progress
    /// the step reported (a crash may only take back newer progress).
    pub fn resume(&mut self, key: &str, start: u32) {
        if start > 0
            && !self
                .progress
                .get(key)
                .is_some_and(|p| p.contains(&(start - 1)))
        {
            self.violation(format!(
                "step {key} resumed from progress {start}, which it never reported"
            ));
        }
    }

    pub fn report_progress(&mut self, key: &str, p: u32) {
        self.progress.entry(key.to_owned()).or_default().insert(p);
    }

    /// True once per key: the step stalls without heartbeats.
    pub fn stall_once(&mut self, key: &str) -> bool {
        self.stalled.insert(key.to_owned())
    }

    /// Hands out a completion token. The first attempt per workflow fails.
    pub fn issue(&mut self, wf: &str, token: &str) -> Result<(), String> {
        let n = self.issued.entry(wf.to_owned()).or_insert(0);
        *n += 1;
        if *n == 1 {
            return Err("approval system unavailable".into());
        }
        self.tokens.push((wf.to_owned(), token.to_owned()));
        Ok(())
    }

    pub fn tick(&mut self, wf: &str, run: u64) {
        let runs = self.ticks.entry(wf.to_owned()).or_default();
        runs.insert(run);
        if runs.len() > 1 {
            let v = format!(
                "scheduled workflow {wf} was started as runs {runs:?}: one scheduled time fired twice"
            );
            self.violation(v);
        }
    }

    /// A read-only call: logged apart from calls with effects and never a
    /// violation. Answers differ per call like [`Service::call`].
    pub fn read(&mut self, key: &str, input: u64, fail_first: u32) -> Result<u64, String> {
        self.total += 1;
        let reads = self.reads.entry(key.to_owned()).or_default();
        if reads.first().is_some_and(|first| *first != input) && !reads.contains(&input) {
            self.reads_rerun += 1;
        }
        reads.push(input);
        let n = reads.len() as u64;
        if n <= u64::from(fail_first) {
            return Err(format!("transient read failure {n}"));
        }
        let mut h = self.seed ^ 0x8422_2325_cbf2_9ce4;
        for b in key.as_bytes() {
            h = (h ^ u64::from(*b)).wrapping_mul(0x100_0000_01b3);
        }
        h ^= input.rotate_left(29) ^ n.wrapping_mul(0x9e37_79b9_7f4a_7c15);
        Ok(h % 1_000_000)
    }

    /// A model's next token: a new value on every call, as sampling gives
    /// (so a token generated again differs from the first). Fails once per
    /// even key at the third token.
    pub fn token(&mut self, key: &str, i: usize) -> Result<u64, String> {
        self.total += 1;
        let n = self.talk_calls.entry(key.to_owned()).or_insert(0);
        *n += 1;
        let mut h = self.seed ^ 0x7a1c_e5d1_4b3f_0e29;
        for b in key.as_bytes() {
            h = (h ^ u64::from(*b)).wrapping_mul(0x100_0000_01b3);
        }
        if i == 2 && h.is_multiple_of(2) && self.talk_failed.insert(key.to_owned()) {
            return Err(format!("the model failed at token {i}"));
        }
        h ^= (i as u64).rotate_left(23) ^ n.wrapping_mul(0x9e37_79b9_7f4a_7c15);
        Ok(h % 1_000_000)
    }

    pub fn call(&mut self, key: &str, input: u64, fail_first: u32) -> Result<u64, String> {
        self.total += 1;
        let calls = self.calls.entry(key.to_owned()).or_default();
        if let Some(first) = calls.first()
            && *first != input
            && self.violations.len() < 16
        {
            self.violations.push(format!(
                "service key {key} was called with input {first} and later with {input}: \
                     a step ran on state a crash took back"
            ));
        }
        calls.push(input);
        let n = calls.len() as u64;
        if n <= u64::from(fail_first) {
            return Err(format!("transient failure {n}"));
        }
        let mut h = self.seed ^ 0xcbf2_9ce4_8422_2325;
        for b in key.as_bytes() {
            h = (h ^ u64::from(*b)).wrapping_mul(0x100_0000_01b3);
        }
        h ^= input.rotate_left(17) ^ n.wrapping_mul(0x9e37_79b9_7f4a_7c15);
        Ok(h % 1_000_000)
    }
}

pub type ServiceRef = Arc<Mutex<Service>>;

// ---------------------------------------------------------------------------
// Workflows.

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum Job {
    Order {
        n: u64,
    },
    Fanout {
        n: u32,
    },
    Pinger {
        target: String,
    },
    /// Updates and queries.
    Counter,
    /// Continue-as-new.
    Looper {
        left: u32,
        acc: u64,
    },
    /// Patching: new code is deployed at the first crash.
    Patchy {
        n: u64,
    },
    /// Heartbeats and heartbeat timeouts.
    Export,
    /// Async completion.
    Approval,
    /// Parent close policy.
    Parent {
        kids: u32,
    },
    /// Reset.
    Resettable {
        n: u64,
    },
    /// Read-only steps feeding a step with effects.
    Reader {
        n: u64,
    },
    /// Streams: a model call as a step streams its tokens (continuing after
    /// the recorded prefix when it runs again), and the workflow streams
    /// its progress.
    Talker {
        n: u32,
    },
}

/// What a [`Job::Talker`] returns: a hash of its tokens in order, which the
/// checker compares with what readers of its stream saw.
pub fn tokens_hash(tokens: &[u64]) -> u64 {
    tokens.iter().fold(0xcbf2_9ce4_8422_2325, |h, t| {
        (h ^ t).wrapping_mul(0x100_0000_01b3)
    })
}

/// The progress a [`Job::Talker`] streams.
pub const TALKER_PROGRESS: [&str; 3] = ["start", "generated", "end"];

/// A deterministic stand-in for encryption (the simulator must be
/// reproducible from its seed).
#[derive(Debug)]
pub struct SimCodec;

impl Codec for SimCodec {
    fn encode(&self, p: &[u8]) -> Result<Vec<u8>, String> {
        let mut v = b"SIMC".to_vec();
        v.extend(p.iter().rev().map(|b| b ^ 0x5a));
        Ok(v)
    }

    fn decode(&self, d: &[u8]) -> Result<Vec<u8>, String> {
        match d.strip_prefix(b"SIMC") {
            Some(r) => Ok(r.iter().rev().map(|b| b ^ 0x5a).collect()),
            None => Ok(d.to_vec()),
        }
    }
}

pub(crate) fn erase<I, O, F, Fut>(f: F) -> WorkflowFn
where
    I: Serialize + serde::de::DeserializeOwned + 'static,
    O: Serialize + serde::de::DeserializeOwned + 'static,
    F: Fn(Ctx, I) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<O, Error>> + 'static,
{
    let f = Arc::new(f);
    Arc::new(move |ctx: Ctx| {
        let f = f.clone();
        let input = ctx.input_bytes(|bytes| serde_json::from_slice::<I>(bytes));
        Box::pin(async move {
            let map = |e: Error| -> (Status, Payload) {
                let (s, m) = match e {
                    Error::Cancelled(r) => (Status::Cancelled, r),
                    Error::TimedOut => (Status::TimedOut, "timed out".into()),
                    other => (Status::Failed, other.to_string()),
                };
                (s, Arc::from(m.into_bytes()))
            };
            let input: I = input.map_err(|e| map(Error::Codec(e.to_string())))?;
            let out = f(ctx, input).await.map_err(map)?;
            serde_json::to_vec(&out)
                .map(Arc::from)
                .map_err(|e| map(Error::Codec(e.to_string())))
        }) as Pin<Box<dyn Future<Output = RunOutcome>>>
    })
}

/// The client's request for a debounced start or a batched event.
fn enqueue_action(
    workflow: &str,
    key: &str,
    event_id: &str,
    input: serde_json::Value,
    rule: Rule,
    continue_at: u32,
) -> Action {
    let event = Event {
        id: event_id.into(),
        rule,
        input,
    };
    let (wtype, coordinator, state, event) = fusee::queue::request(workflow, key, &event);
    let mut state: QueueState = serde_json::from_value(state).unwrap_or_default();
    state.continue_at = continue_at;
    Action::Enqueue {
        coordinator,
        wtype,
        workflow: workflow.into(),
        state: serde_json::to_vec(&state).unwrap_or_default(),
        event_id: event_id.into(),
        event: serde_json::to_vec(&event).unwrap_or_default(),
    }
}

async fn call(
    ctx: &Ctx,
    svc: &ServiceRef,
    name: &str,
    input: u64,
    fail_first: u32,
) -> Result<u64, Error> {
    let mut opts = StepOptions::default().retry(RetryPolicy::exponential(
        fail_first + 2,
        std::time::Duration::from_millis(1),
        std::time::Duration::from_millis(4),
    ));
    if name == "charge" || name == "ship" {
        opts = opts.limit("svc");
        // Odd seeds queue for the permit by priority and tenant (neither
        // is journaled, so replay passes the same ones again).
        if svc.lock().is_ok_and(|s| s.seed % 2 == 1) {
            opts = opts.priority((input % 3) as i32 - 1);
            if input % 2 == 1 {
                opts = opts
                    .fairness(&format!("tenant-{}", input % 5))
                    .fairness_weight(1 + (input % 3) as u32);
            }
        }
    }
    ctx.step(name, || {
        let key = ctx.key();
        let svc = svc.clone();
        async move {
            let delay = {
                let s = svc.lock().map_err(|_| "poisoned".to_string())?;
                (s.total + input) % 3
            };
            sim_sleep(delay).await;

            svc.lock()
                .map_err(|_| "poisoned".to_string())?
                .call(&key, input, fail_first)
        }
    })
    .options(opts)
    .await
}

/// A read-only step: runs without waiting for the records before it.
async fn read(
    ctx: &Ctx,
    svc: &ServiceRef,
    name: &str,
    input: u64,
    fail_first: u32,
) -> Result<u64, Error> {
    let opts = StepOptions::default()
        .read_only()
        .retry(RetryPolicy::exponential(
            fail_first + 2,
            std::time::Duration::from_millis(1),
            std::time::Duration::from_millis(4),
        ));
    ctx.step(name, || {
        let key = ctx.key();
        let svc = svc.clone();
        async move {
            let delay = {
                let s = svc.lock().map_err(|_| "poisoned".to_string())?;
                (s.total + input) % 2
            };
            sim_sleep(delay).await;
            svc.lock()
                .map_err(|_| "poisoned".to_string())?
                .read(&key, input, fail_first)
        }
    })
    .options(opts)
    .await
}

pub fn register(registry: &SharedRegistry, svc: &ServiceRef, deployed: Arc<AtomicBool>) {
    let mut r = registry.write().unwrap_or_else(|e| e.into_inner());
    let s = svc.clone();
    let deployed2 = deployed.clone();
    r.register(
        "job",
        1,
        erase(move |ctx: Ctx, job: Job| {
            let svc = s.clone();
            let deployed2 = deployed2.clone();
            async move {
                match job {
                    Job::Order { n } => {
                        let quote = call(&ctx, &svc, "quote", n, 0).await?;
                        let charge = call(&ctx, &svc, "charge", quote, 2).await?;
                        ctx.sleep("cool-off", std::time::Duration::from_millis(3))
                            .await?;
                        let approved: Option<u64> = match ctx
                            .wait_for("approve")
                            .timeout(std::time::Duration::from_millis(60))
                            .await
                        {
                            Ok(v) => v,
                            Err(Error::Cancelled(_)) => {
                                let r = call(&ctx, &svc, "refund", charge, 0).await?;
                                return Err(Error::Cancelled(format!("refunded {r}")));
                            }
                            Err(e) => return Err(e),
                        };
                        match approved {
                            Some(v) => {
                                let id = format!("{}/ship", ctx.workflow_id());
                                ctx.child::<u64, u64>("ship", &id, &(charge ^ v)).await
                            }
                            None => call(&ctx, &svc, "refund", charge, 0).await,
                        }
                    }
                    Job::Fanout { n } => {
                        let branches: Vec<_> = (0..n)
                            .map(|i| {
                                let ctx = ctx.clone();
                                let svc = svc.clone();
                                async move {
                                    call(&ctx, &svc, &format!("leg{i}"), u64::from(i), i % 2).await
                                }
                            })
                            .collect();
                        let mut sum = 0u64;
                        for r in ctx.all(branches).await {
                            sum = sum.wrapping_add(r?);
                        }
                        let t = ctx.now_ms()?;
                        let total = call(&ctx, &svc, "total", sum ^ (t % 7), 0).await?;
                        Ok(total)
                    }
                    Job::Pinger { target } => {
                        let v = call(&ctx, &svc, "prep", 1, 0).await?;
                        ctx.signal_workflow(&target, "approve", &v)?;
                        Ok(v)
                    }
                    Job::Counter => {
                        let total = Rc::new(Cell::new(0u64));
                        let t = total.clone();
                        ctx.on_query("total", move |_: serde_json::Value| t.get());
                        let (t, c, s2) = (total.clone(), ctx.clone(), svc.clone());
                        ctx.on_update(
                            "add",
                            |v: &u64| {
                                if *v >= 1000 {
                                    Err(format!("{v} is too large"))
                                } else {
                                    Ok(())
                                }
                            },
                            move |v: u64| {
                                let (t, c, svc) = (t.clone(), c.clone(), s2.clone());
                                async move {
                                    let r = call(&c, &svc, "apply", v, 0).await?;
                                    t.set(t.get() + r % 100);
                                    Ok(t.get())
                                }
                            },
                        );
                        let _ = ctx
                            .wait_for::<u64>("close")
                            .timeout(Duration::from_millis(150))
                            .await?;
                        Ok(total.get())
                    }
                    Job::Looper { left, acc } => {
                        let v =
                            call(&ctx, &svc, "loop", acc.wrapping_add(u64::from(left)), 0).await?;
                        if left == 0 {
                            return Ok(acc ^ v);
                        }
                        ctx.continue_as_new(&Job::Looper {
                            left: left - 1,
                            acc: acc ^ v,
                        })
                        .await
                    }
                    Job::Patchy { n } => {
                        let a = call(&ctx, &svc, "a", n, 0).await?;
                        ctx.sleep("gap", Duration::from_millis(5)).await?;
                        if deployed2.load(Ordering::Relaxed) && ctx.patched("fix")? {
                            call(&ctx, &svc, "new", a, 0).await
                        } else {
                            call(&ctx, &svc, "old", a, 0).await
                        }
                    }
                    Job::Export => {
                        let opts = StepOptions::default()
                            .retry(RetryPolicy::exponential(
                                5,
                                Duration::from_millis(1),
                                Duration::from_millis(2),
                            ))
                            .heartbeat_timeout(Duration::from_millis(20));
                        let (c, s2) = (ctx.clone(), svc.clone());
                        let pages: u32 = ctx
                            .step("export", move || {
                                let (c, svc) = (c.clone(), s2.clone());
                                async move {
                                    let key = c.key();
                                    let start: u32 = c
                                        .progress::<u32>()
                                        .map_err(|e| e.to_string())?
                                        .unwrap_or(0);
                                    svc.lock()
                                        .map_err(|_| "poisoned".to_string())?
                                        .resume(&key, start);
                                    for p in start..8 {
                                        let stall = p == 3
                                            && svc
                                                .lock()
                                                .map_err(|_| "poisoned".to_string())?
                                                .stall_once(&key);
                                        sim_sleep(if stall { 40 } else { 2 }).await;
                                        svc.lock()
                                            .map_err(|_| "poisoned".to_string())?
                                            .report_progress(&key, p);
                                        c.heartbeat(&(p + 1)).map_err(|e| e.to_string())?;
                                    }
                                    Ok::<u32, String>(8)
                                }
                            })
                            .options(opts)
                            .await?;
                        Ok(u64::from(pages))
                    }
                    Job::Approval => {
                        let (s2, id) = (svc.clone(), ctx.workflow_id().to_owned());
                        let v: u64 = ctx
                            .await_completion("approval", move |c: fusee::Completion| {
                                let token = c.token;
                                let (svc, id) = (s2.clone(), id.clone());
                                async move {
                                    svc.lock()
                                        .map_err(|_| "poisoned".to_string())?
                                        .issue(&id, &token)
                                }
                            })
                            .options(StepOptions::default().retry(RetryPolicy::exponential(
                                3,
                                Duration::from_millis(1),
                                Duration::from_millis(2),
                            )))
                            .await?;
                        call(&ctx, &svc, "after", v, 0).await
                    }
                    Job::Parent { kids } => {
                        for k in 0..kids {
                            let close = if k % 2 == 0 {
                                ParentClose::Terminate
                            } else {
                                ParentClose::Abandon
                            };
                            ctx.start_child(
                                "kid",
                                &format!("{}/kid{k}", ctx.workflow_id()),
                                &u64::from(k),
                            )
                            .options(ChildOptions {
                                close,
                                timeout: None,
                            })
                            .await?;
                        }
                        let v = call(&ctx, &svc, "parent", 1, 0).await?;
                        let _ = ctx
                            .wait_for::<u64>("done")
                            .timeout(Duration::from_millis(30))
                            .await?;
                        Ok(v)
                    }
                    Job::Reader { n } => {
                        // Each read's input comes from the read before it,
                        // which may not be durable yet. `store` has effects:
                        // its gate waits for every read before it.
                        let a = read(&ctx, &svc, "fetch", n, 0).await?;
                        let b = read(&ctx, &svc, "enrich", a, 1).await?;
                        let c = call(&ctx, &svc, "store", b, 1).await?;
                        let d = read(&ctx, &svc, "verify", c, 0).await?;
                        Ok(d)
                    }
                    Job::Talker { n } => {
                        let progress = ctx.stream("progress")?;
                        progress.append(&TALKER_PROGRESS[0])?;
                        let (c, s2) = (ctx.clone(), svc.clone());
                        let opts = StepOptions::default().retry(RetryPolicy::exponential(
                            4,
                            Duration::from_millis(1),
                            Duration::from_millis(2),
                        ));
                        let tokens: Vec<u64> = ctx
                            .step("talk", move || {
                                let (c, svc) = (c.clone(), s2.clone());
                                async move {
                                    let key = c.key();
                                    let out = c.stream("tokens").map_err(|e| e.to_string())?;
                                    // Continue after what was recorded.
                                    let mut toks: Vec<u64> =
                                        out.recorded().map_err(|e| e.to_string())?;
                                    for i in toks.len()..n as usize {
                                        sim_sleep(1).await;
                                        let t = svc
                                            .lock()
                                            .map_err(|_| "poisoned".to_string())?
                                            .token(&key, i)?;
                                        out.append(&t).map_err(|e| e.to_string())?;
                                        toks.push(t);
                                    }
                                    Ok::<_, String>(toks)
                                }
                            })
                            .options(opts)
                            .await?;
                        progress.append(&TALKER_PROGRESS[1])?;
                        ctx.sleep("rest", Duration::from_millis(4)).await?;
                        progress.append(&TALKER_PROGRESS[2])?;
                        progress.close()?;
                        Ok(tokens_hash(&tokens))
                    }
                    Job::Resettable { n } => {
                        let a = call(&ctx, &svc, "a", n, 0).await?;
                        let b = call(&ctx, &svc, "b", a, 0).await?;
                        let c = call(&ctx, &svc, "c", b, 0).await?;
                        let _ = ctx
                            .wait_for::<u64>("go")
                            .timeout(Duration::from_millis(80))
                            .await?;
                        Ok(a ^ b ^ c)
                    }
                }
            }
        }),
    );
    let s = svc.clone();
    r.register(
        "kid",
        1,
        erase(move |ctx: Ctx, k: u64| {
            let svc = s.clone();
            async move {
                let wait = if k.is_multiple_of(2) { 400 } else { 60 };
                let _ = ctx
                    .wait_for::<u64>("never")
                    .timeout(Duration::from_millis(wait))
                    .await?;
                call(&ctx, &svc, "kid", k, 0).await
            }
        }),
    );
    let s = svc.clone();
    r.register(
        "tick",
        1,
        erase(move |ctx: Ctx, slow: bool| {
            let svc = s.clone();
            async move {
                let (id, run) = (ctx.workflow_id().to_owned(), ctx.run_id());
                let v: u64 = ctx
                    .step("tick", || {
                        let svc = svc.clone();
                        let id = id.clone();
                        async move {
                            svc.lock()
                                .map_err(|_| "poisoned".to_string())?
                                .tick(&id, run);
                            Ok::<u64, String>(run)
                        }
                    })
                    .await?;
                if slow {
                    ctx.sleep("work", Duration::from_millis(35)).await?;
                }
                Ok(v)
            }
        }),
    );
    let s = svc.clone();
    r.register(
        "ship",
        1,
        erase(move |ctx: Ctx, x: u64| {
            let svc = s.clone();
            async move { call(&ctx, &svc, "ship", x, 1).await }
        }),
    );
    // What debounce and batch windows start: returns its input.
    r.register(
        "echo",
        1,
        erase(move |ctx: Ctx, v: serde_json::Value| async move {
            ctx.step("echo", || {
                let v = v.clone();
                async move { Ok::<_, Error>(v) }
            })
            .await
        }),
    );
}

// ---------------------------------------------------------------------------
// The client and checkers.

#[derive(Clone, Debug)]
enum Action {
    Start {
        id: String,
        job: Job,
    },
    /// A start of a user scenario's workflow type.
    StartAs {
        id: String,
        wtype: String,
        input: Vec<u8>,
    },
    Signal {
        id: String,
        name: String,
        payload: Vec<u8>,
        dedup: u64,
    },
    Cancel {
        id: String,
    },
    Terminate {
        id: String,
    },
    Update {
        id: String,
        update_id: String,
        value: u64,
    },
    Query {
        id: String,
    },
    Reset {
        id: String,
    },
    Schedule {
        name: String,
        spec: ScheduleSpec,
    },
    Complete {
        wf: String,
        token: String,
    },
    /// A call to a key of an object type ([`fusee::object`]): update-with-
    /// start on the key's run, retried with one call ID until answered.
    ObjectCall {
        id: String,
        wtype: String,
        handler: String,
        call_id: String,
        arg: Vec<u8>,
    },
    /// A debounced start or batched event: update-with-start on the key's
    /// coordinator, retried with one event ID until it has a receipt.
    Enqueue {
        coordinator: String,
        wtype: &'static str,
        /// The workflow type its window starts.
        workflow: String,
        state: Vec<u8>,
        event_id: String,
        event: Vec<u8>,
    },
}

#[derive(Clone, Debug)]
enum WorldEv {
    Client(usize),
    Deliver {
        to: u32,
        from: u32,
        id: u64,
        msg: Message,
        epoch: u64,
    },
    Ack {
        to: u32,
        id: u64,
        epoch: u64,
    },
    Crash,
    /// A supervisor restarts a process whose shard stopped (not a fault).
    Restart,
    /// Reader `n` reads its stream again.
    Read(usize),
}

/// A client reading one stream from start to end, reconnecting at its
/// offset after every crash.
#[derive(Clone, Debug)]
struct SimReader {
    id: String,
    name: String,
    run: Option<u64>,
    next: u64,
    done: bool,
}

/// What readers saw of one stream: every chunk by offset, and its end.
#[derive(Clone, Debug, Default)]
struct SeenStream {
    chunks: Vec<serde_json::Value>,
    end: Option<String>,
}

pub struct World {
    cfg: SimConfig,
    m: MachineRef,
    rng: Rng,
    shards: Vec<Option<ShardRuntime<SimEnv>>>,
    /// The machine's shared log, reopened with the process.
    log: Option<SimLogRef>,
    registry: SharedRegistry,
    svc: ServiceRef,
    /// New code (a patch) is deployed at the first crash.
    deployed: Arc<AtomicBool>,
    limits: Arc<LocalLimits>,
    max_permits: u32,
    codec: Option<Arc<dyn Codec>>,
    /// Replies per update ID (every call must get the same result).
    update_replies: BTreeMap<String, (Status, Vec<u8>)>,
    /// The latest query answer per workflow (must never go back).
    query_answers: BTreeMap<String, u64>,
    /// Every query answer per workflow.
    all_answers: BTreeMap<String, BTreeSet<u64>>,
    /// Completions acknowledged per workflow.
    completions: BTreeMap<String, Vec<String>>,
    tokens_seen: usize,
    /// Workflows with a reset in flight: their result may change.
    resetting: BTreeSet<String>,
    parents: Vec<(String, u32)>,
    events: BTreeMap<(u64, u64), WorldEv>,
    ev_seq: u64,
    actions: Vec<Action>,
    /// Action index -> acknowledged.
    acked: Vec<bool>,
    /// Debounced or batched event ID -> the run its receipt named.
    receipts: BTreeMap<String, String>,
    /// Object call ID -> its answer, when the client first sent it, and
    /// when the first answer came.
    calls: BTreeMap<String, crate::scenario::CallReply>,
    call_sent: BTreeMap<String, u64>,
    tokens: BTreeMap<u64, usize>,
    next_token: u64,
    results: BTreeMap<String, (Status, Vec<u8>)>,
    faults: bool,
    report: Report,
    trace: Vec<String>,
    /// A user scenario's requests (taken by `plan`) and checks.
    user_actions: Vec<(u64, UserAction)>,
    checks: Vec<Check>,
    /// Stream readers, their reads in flight, and what they saw by
    /// `(workflow ID, run, stream)`.
    readers: Vec<SimReader>,
    read_tokens: BTreeMap<u64, usize>,
    seen: BTreeMap<(String, u64, String), SeenStream>,
}

impl World {
    pub fn new(cfg: SimConfig) -> Self {
        let mut rng = Rng::new(cfg.seed);
        let m = Rc::new(RefCell::new(Machine {
            disk: Default::default(),
            now: 1_000_000,
            rng: rng.fork(),
            io: BTreeMap::new(),
            io_seq: 0,
            epoch: 0,
            flush_fail_per_mille: cfg.flush_fail_per_mille,
            max_io_ms: cfg.max_io_ms,
            log: Vec::new(),
        }));
        let svc: ServiceRef = Arc::new(Mutex::new(Service {
            seed: cfg.seed,
            ..Default::default()
        }));
        let registry: SharedRegistry = Arc::new(RwLock::new(Default::default()));
        let deployed = Arc::new(AtomicBool::new(false));
        let mut setup = None;
        match &cfg.user {
            Some(user) => {
                let mut s = Setup::new(cfg.seed, registry.clone());
                (user.0)(&mut s);
                setup = Some(s);
            }
            None => register(&registry, &svc, deployed.clone()),
        }
        if let Ok(mut r) = registry.write() {
            fusee::schedule::register_builtins(&mut r);
        }
        sim_timers_clear();
        NOW.with(|n| n.set(1_000_000));
        let mut w = Self {
            cfg: cfg.clone(),
            m,
            rng,
            shards: Vec::new(),
            log: None,
            registry,
            svc,
            deployed,
            limits: Arc::new(LocalLimits::new()),
            max_permits: 0,
            codec: if cfg.seed % 3 == 1 {
                Some(Arc::new(SimCodec))
            } else {
                None
            },
            update_replies: BTreeMap::new(),
            query_answers: BTreeMap::new(),
            all_answers: BTreeMap::new(),
            completions: BTreeMap::new(),
            tokens_seen: 0,
            resetting: BTreeSet::new(),
            parents: Vec::new(),
            events: BTreeMap::new(),
            ev_seq: 0,
            actions: Vec::new(),
            acked: Vec::new(),
            receipts: BTreeMap::new(),
            calls: BTreeMap::new(),
            call_sent: BTreeMap::new(),
            tokens: BTreeMap::new(),
            next_token: 1,
            results: BTreeMap::new(),
            faults: true,
            report: Report {
                seed: cfg.seed,
                workflows: cfg.workflows,
                ..Default::default()
            },
            trace: Vec::new(),
            user_actions: Vec::new(),
            checks: Vec::new(),
            readers: Vec::new(),
            read_tokens: BTreeMap::new(),
            seen: BTreeMap::new(),
        };
        if let Some(s) = setup {
            w.report.workflows = s
                .actions
                .iter()
                .filter(|a| matches!(a.1, UserAction::Start { .. }))
                .count() as u32;
            w.user_actions = s.actions;
            w.checks = s.checks;
            w.plan_user();
        } else {
            w.plan();
        }
        w
    }

    /// A user scenario's requests, then crashes as for the built-in
    /// workload.
    fn plan_user(&mut self) {
        let start = self.now();
        for (at, a) in std::mem::take(&mut self.user_actions) {
            let action = match a {
                UserAction::Start { id, wtype, input } => Action::StartAs { id, wtype, input },
                UserAction::Signal { id, name, payload } => Action::Signal {
                    id,
                    name,
                    payload,
                    dedup: 1 + self.rng.next() % (1 << 40),
                },
                UserAction::Cancel { id } => Action::Cancel { id },
                UserAction::Terminate { id } => Action::Terminate { id },
                UserAction::ReadStream { id, name } => {
                    let n = self.readers.len();
                    self.readers.push(SimReader {
                        id,
                        name,
                        run: None,
                        next: 0,
                        done: false,
                    });
                    self.at(start + at, WorldEv::Read(n));
                    continue;
                }
                UserAction::Call {
                    wtype,
                    key,
                    handler,
                    call_id,
                    arg,
                } => Action::ObjectCall {
                    id: fusee::object::object_id(&wtype, &key),
                    wtype,
                    handler,
                    call_id,
                    arg,
                },
                UserAction::Enqueue {
                    workflow,
                    key,
                    event_id,
                    input,
                    rule,
                } => {
                    // A third of the seeds make coordinators continue as new
                    // every few events, carrying windows and event IDs.
                    let continue_at = [0, 12, 30][(self.cfg.seed % 3) as usize];
                    enqueue_action(&workflow, &key, &event_id, input, rule, continue_at)
                }
            };
            let ix = self.actions.len();
            self.actions.push(action);
            self.at(start + at, WorldEv::Client(ix));
        }
        self.acked = vec![false; self.actions.len()];
        self.plan_crashes(start);
    }

    fn plan_crashes(&mut self, start: u64) {
        if self.cfg.crash_every_ms > 0 {
            let mut t = start;
            loop {
                t += 1 + self.rng.below(self.cfg.crash_every_ms * 2);
                if t > start + self.cfg.duration_ms {
                    break;
                }
                self.at(t, WorldEv::Crash);
            }
        }
    }

    fn now(&self) -> u64 {
        self.m.borrow().now
    }

    fn log(&mut self, s: String) {
        let t = self.now();
        self.trace.push(format!("[{t}] {s}"));
        if self.trace.len() > 400 {
            self.trace.drain(..100);
        }
    }

    fn at(&mut self, t: u64, ev: WorldEv) {
        let s = self.ev_seq;
        self.ev_seq += 1;
        self.events.insert((t, s), ev);
    }

    fn plan(&mut self) {
        let start = self.now();
        let n = self.cfg.workflows;
        let span = self.cfg.duration_ms / 2;
        let mut orders = Vec::new();
        for i in 0..n {
            let id = format!("wf-{i}");
            let job = match self.rng.below(20) {
                0..=7 | 18 => {
                    orders.push(id.clone());
                    Job::Order { n: u64::from(i) }
                }
                19 => Job::Reader { n: u64::from(i) },
                8..=9 => Job::Fanout {
                    n: 2 + self.rng.below(5) as u32,
                },
                10 => Job::Pinger {
                    target: orders.last().cloned().unwrap_or_else(|| "nobody".into()),
                },
                11 => Job::Counter,
                12 => Job::Looper {
                    left: 1 + self.rng.below(4) as u32,
                    acc: u64::from(i),
                },
                13 => Job::Patchy { n: u64::from(i) },
                14 => Job::Export,
                15 => Job::Approval,
                16 => {
                    let kids = 1 + self.rng.below(4) as u32;
                    self.parents.push((id.clone(), kids));
                    Job::Parent { kids }
                }
                _ => Job::Resettable { n: u64::from(i) },
            };
            let t = start + self.rng.below(span);
            let ix = self.actions.len();
            self.actions.push(Action::Start {
                id: id.clone(),
                job: job.clone(),
            });
            self.at(t, WorldEv::Client(ix));
            match &job {
                Job::Counter => {
                    for u in 0..1 + self.rng.below(4) {
                        let ix = self.actions.len();
                        self.actions.push(Action::Update {
                            id: id.clone(),
                            update_id: format!("{id}/u{u}"),
                            value: self.rng.below(1200),
                        });
                        let t2 = t + 1 + self.rng.below(120);
                        self.at(t2, WorldEv::Client(ix));
                        // A retried call with the same update ID.
                        if self.rng.chance(300) {
                            let t3 = t2 + self.rng.below(60);
                            self.at(t3, WorldEv::Client(ix));
                        }
                        // A query while the update's handler may be running.
                        let ix = self.actions.len();
                        self.actions.push(Action::Query { id: id.clone() });
                        let t4 = t2 + 1 + self.rng.below(5);
                        self.at(t4, WorldEv::Client(ix));
                    }
                    for _ in 0..2 + self.rng.below(6) {
                        let ix = self.actions.len();
                        self.actions.push(Action::Query { id: id.clone() });
                        let t2 = t + 1 + self.rng.below(150);
                        self.at(t2, WorldEv::Client(ix));
                    }
                }
                Job::Parent { .. } if self.rng.chance(400) => {
                    let ix = self.actions.len();
                    self.actions.push(Action::Terminate { id: id.clone() });
                    let t2 = t + self.rng.below(40);
                    self.at(t2, WorldEv::Client(ix));
                }
                Job::Resettable { .. } if self.rng.chance(600) => {
                    let ix = self.actions.len();
                    self.actions.push(Action::Reset { id: id.clone() });
                    let t2 = t + self.rng.below(80);
                    self.at(t2, WorldEv::Client(ix));
                }
                _ => {}
            }
            if matches!(job, Job::Order { .. }) {
                if self.rng.chance(500) {
                    let ix = self.actions.len();
                    let dedup = 1 + self.rng.next() % (1 << 40);
                    let value = self.rng.below(1000);
                    self.actions.push(Action::Signal {
                        id: id.clone(),
                        name: "approve".into(),
                        payload: serde_json::to_vec(&value).unwrap_or_default(),
                        dedup,
                    });
                    let t2 = t + 1 + self.rng.below(80);
                    self.at(t2, WorldEv::Client(ix));
                }
                if self.rng.chance(120) {
                    let ix = self.actions.len();
                    self.actions.push(Action::Cancel { id: id.clone() });
                    let t3 = t + self.rng.below(40);
                    self.at(t3, WorldEv::Client(ix));
                }
            }
        }
        if self.cfg.seed.is_multiple_of(2) {
            let overlap = [
                Overlap::Skip,
                Overlap::BufferOne,
                Overlap::AllowAll,
                Overlap::CancelOther,
            ][self.rng.below(4) as usize];
            let spec = ScheduleSpec {
                every_ms: Some([23, 41][self.rng.below(2) as usize]),
                overlap,
                jitter_ms: self.rng.below(5),
                workflow: "tick".into(),
                input: serde_json::json!(self.rng.chance(300)),
                ..Default::default()
            };
            let ix = self.actions.len();
            self.actions.push(Action::Schedule {
                name: "tick".into(),
                spec,
            });
            let t = start + self.rng.below(span);
            self.at(t, WorldEv::Client(ix));
            // The schedule is deleted when the client is done.
            let ix = self.actions.len();
            self.actions.push(Action::Terminate {
                id: schedule_id("tick"),
            });
            self.at(start + self.cfg.duration_ms, WorldEv::Client(ix));
        }
        if self.cfg.seed % 4 == 3 {
            self.plan_queues(start, span);
        }
        self.plan_streams(start, span);
        self.acked = vec![false; self.actions.len()];
        self.plan_crashes(start);
    }

    /// Model calls that stream their tokens, and readers of their streams
    /// that start before, during and after them. A random stream of its
    /// own, so the rest of the seed's plan stays as it was.
    fn plan_streams(&mut self, start: u64, span: u64) {
        let mut r = Rng::new(self.cfg.seed ^ 0x5717_ea35_c0de_0001);
        for k in 0..1 + r.below(3) {
            let id = format!("talk-{k}");
            let t = start + r.below(span.max(1));
            let ix = self.actions.len();
            self.actions.push(Action::Start {
                id: id.clone(),
                job: Job::Talker {
                    n: 3 + r.below(10) as u32,
                },
            });
            self.at(t, WorldEv::Client(ix));
            for name in ["tokens", "progress", "tokens"] {
                if name == "tokens" && r.chance(300) {
                    continue;
                }
                let n = self.readers.len();
                self.readers.push(SimReader {
                    id: id.clone(),
                    name: name.into(),
                    run: None,
                    next: 0,
                    done: false,
                });
                self.at(t.saturating_sub(5) + r.below(80), WorldEv::Read(n));
            }
        }
    }

    /// Debounced starts and batched events of `echo`, in bursts, each with
    /// its own event ID (the input is the ID, so checks can find it).
    fn plan_queues(&mut self, start: u64, span: u64) {
        let continue_at = [0, 12, 30][(self.cfg.seed / 4 % 3) as usize];
        for k in 0..4u64 {
            let rule = if k % 2 == 1 {
                Rule::Batch(fusee::Batch::new(
                    2 + self.rng.below(3) as u32,
                    Duration::from_millis(20 + self.rng.below(60)),
                ))
            } else {
                Rule::Debounce(
                    fusee::Debounce::new(Duration::from_millis(5 + self.rng.below(40)))
                        .max_wait(Duration::from_millis(30 + self.rng.below(80))),
                )
            };
            let mut t = start + self.rng.below(span);
            for i in 0..3 + self.rng.below(6) {
                t += if self.rng.chance(300) {
                    40 + self.rng.below(100)
                } else {
                    self.rng.below(15)
                };
                let id = format!("q{k}/e{i}");
                let ix = self.actions.len();
                self.actions.push(enqueue_action(
                    "echo",
                    &format!("key-{k}"),
                    &id,
                    serde_json::json!(id),
                    rule.clone(),
                    continue_at,
                ));
                self.at(t, WorldEv::Client(ix));
            }
        }
    }

    /// The built-in workload's windows: a batch run's input holds each of
    /// its events once, and no event is in two runs; a debounced run's
    /// input is one of its own window's starts.
    fn check_queue_contents(&self) -> Result<(), Failure> {
        for a in &self.actions {
            let Action::Enqueue {
                wtype, event_id, ..
            } = a
            else {
                continue;
            };
            let Some(run) = self.receipts.get(event_id) else {
                continue;
            };
            let got: serde_json::Value = match self.results.get(run) {
                Some((Status::Ok, p)) => serde_json::from_slice(p).unwrap_or_default(),
                other => {
                    return Err(self.fail(format!(
                        "window run {run} of event {event_id} ended with {other:?}"
                    )));
                }
            };
            // The events whose receipts name this run.
            let mates: Vec<&String> = self
                .receipts
                .iter()
                .filter(|(_, r)| *r == run)
                .map(|(e, _)| e)
                .collect();
            if *wtype == BATCH_TYPE {
                // Each event once in its own run, and nothing else there:
                // so no event is in two runs.
                let events = got.as_array().cloned().unwrap_or_default();
                let n = events
                    .iter()
                    .filter(|e| e.as_str() == Some(event_id))
                    .count();
                if n != 1 || events.len() != mates.len() {
                    return Err(self.fail(format!(
                        "batch run {run} holds event {event_id} {n} times and {} events \
                         in all, but {} receipts name it: {got}",
                        events.len(),
                        mates.len()
                    )));
                }
            } else if !mates.iter().any(|e| got.as_str() == Some(e.as_str())) {
                return Err(self.fail(format!(
                    "debounced run {run} took {got}, not a start of its window"
                )));
            }
        }
        Ok(())
    }

    fn runtime_config(&self, shard: u32) -> RuntimeConfig {
        RuntimeConfig {
            shard,
            shards: self.cfg.shards,
            retention_ms: 3_600_000,
            park_after_ms: self.cfg.park_after_ms,
            // A third of the seeds park every suspended run at each sweep
            // through the memory budget instead of waiting `park_after_ms`.
            memory_budget: if self.cfg.seed % 3 == 1 { 1 } else { 0 },
            checkpoint_interval_ms: self.cfg.checkpoint_interval_ms,
            max_loaded: 100_000,
            segment_bytes: self.cfg.segment_bytes,
            inline_flush: false,
            inline_max_us: 0,
            keep_history: self.cfg.seed.is_multiple_of(2),
            // From bits of the seed the other knobs do not use: index bases
            // written a few runs at a time between checkpoints, and often,
            // so that they interleave with parks, wakes and crashes.
            index_chunk_records: [1, 3, 64, fusee::runtime::INDEX_CHUNK_RECORDS]
                [(self.cfg.seed / 72 % 4) as usize],
            index_rebase_deltas: [1, 2, 8, 64][(self.cfg.seed / 288 % 4) as usize],
            sabotage: self.cfg.sabotage,
            ..RuntimeConfig::default()
        }
    }

    fn open_all(&mut self) -> Result<(), String> {
        self.shards.clear();
        self.log = None;
        if self.cfg.shared_log {
            self.log = Some(
                SimLog::open(
                    &self.m,
                    self.cfg.shards,
                    self.cfg.segment_bytes,
                    // From bits of the seed the other knobs do not use.
                    1 + (self.cfg.seed / 2 % 3) as usize,
                    (self.cfg.seed / 6 % 3) as usize,
                    crate::env::Offload {
                        threshold: self.cfg.offload_bytes,
                        early: self.cfg.early_blobs,
                    },
                )
                .map_err(|e| format!("the shared log failed to recover: {e}"))?,
            );
        }
        for shard in 0..self.cfg.shards {
            let env = match &self.log {
                Some(log) => SimEnv::with_log(shard, self.m.clone(), log.clone()),
                None => SimEnv::new(shard, self.m.clone()),
            };
            let notify: fusee::env::Notify = Arc::new(|| {});
            let services = Services {
                codec: self.codec.clone(),
                limits: self.limits.clone(),
            };
            self.limits.define(
                "svc",
                LimitSpec {
                    concurrent: 2,
                    per_second: 0.0,
                },
            );
            match ShardRuntime::open_with(
                env,
                self.runtime_config(shard),
                self.registry.clone(),
                false,
                services,
                notify,
            ) {
                Ok(rt) => self.shards.push(Some(rt)),
                Err(e) => return Err(format!("shard {shard} failed to recover: {e}")),
            }
        }
        Ok(())
    }

    fn shard_rt(&self, shard: u32) -> Option<&ShardRuntime<SimEnv>> {
        self.shards.get(shard as usize).and_then(|s| s.as_ref())
    }

    fn fail(&self, message: String) -> Failure {
        let mut trace = self.trace.clone();
        trace.extend(self.m.borrow().log.iter().rev().take(20).rev().cloned());
        Failure {
            seed: self.cfg.seed,
            message,
            trace,
            scenario: self.cfg.user.is_some(),
        }
    }

    fn do_action(&mut self, ix: usize) {
        if self.acked[ix] {
            return;
        }
        let token = self.next_token;
        self.next_token += 1;
        let action = self.actions[ix].clone();
        let shards = self.cfg.shards;
        let r = match &action {
            Action::Start { id, job } => {
                let shard = shard_of(id, shards);
                // A client that retries uses the existing run.
                let mut req = StartRequest::new(
                    id,
                    "job",
                    Arc::from(serde_json::to_vec(job).unwrap_or_default()),
                    IdPolicy::ATTACH,
                );
                req.token = Some(token);
                self.shard_rt(shard).map(|rt| rt.start(req).map(|_| ()))
            }
            Action::StartAs { id, wtype, input } => {
                let shard = shard_of(id, shards);
                let mut req = StartRequest::new(id, wtype, Arc::from(&input[..]), IdPolicy::ATTACH);
                req.token = Some(token);
                self.shard_rt(shard).map(|rt| rt.start(req).map(|_| ()))
            }
            Action::Schedule { name, spec } => {
                let id = schedule_id(name);
                let state = ScheduleState {
                    name: name.clone(),
                    spec: spec.clone(),
                    ..Default::default()
                };
                let mut req = StartRequest::new(
                    &id,
                    SCHEDULE_TYPE,
                    Arc::from(serde_json::to_vec(&state).unwrap_or_default()),
                    IdPolicy::ATTACH,
                );
                req.token = Some(token);
                self.shard_rt(shard_of(&id, shards))
                    .map(|rt| rt.start(req).map(|_| ()))
            }
            Action::Terminate { id } => self
                .shard_rt(shard_of(id, shards))
                .map(|rt| rt.terminate(id, "client terminated", Some(token))),
            Action::Update {
                id,
                update_id,
                value,
            } => {
                let payload: Payload = Arc::from(serde_json::to_vec(value).unwrap_or_default());
                self.shard_rt(shard_of(id, shards))
                    .map(|rt| rt.update(id, "add", update_id, payload, Some(token)))
            }
            Action::Query { id } => self
                .shard_rt(shard_of(id, shards))
                .map(|rt| rt.query(id, "total", Arc::from(&b"null"[..]), token)),
            Action::Reset { id } => {
                self.resetting.insert(id.clone());
                self.results.remove(id);
                self.shard_rt(shard_of(id, shards))
                    .map(|rt| rt.reset(id, "b", true, None, Some(token)).map(|_| ()))
            }
            Action::Complete { wf, token: t } => {
                let payload: Payload =
                    Arc::from(serde_json::to_vec(&(wf.len() as u64 * 7)).unwrap_or_default());
                self.shard_rt(shard_of(wf, shards))
                    .map(|rt| rt.complete(t, Status::Ok, payload, Some(token)))
            }
            Action::Signal {
                id,
                name,
                payload,
                dedup,
            } => {
                // Only signal workflows whose start was acknowledged.
                let started = self
                    .actions
                    .iter()
                    .zip(&self.acked)
                    .any(|(a, ok)| {
                        matches!(a, Action::Start { id: s, .. } | Action::StartAs { id: s, .. } if s == id)
                            && *ok
                    });
                if !started {
                    let t = self.now() + 5;
                    self.at(t, WorldEv::Client(ix));
                    return;
                }
                let shard = shard_of(id, shards);
                let payload: Payload = Arc::from(&payload[..]);
                self.shard_rt(shard)
                    .map(|rt| rt.signal(id, name, payload, *dedup, Some(token)))
            }
            Action::Cancel { id } => {
                let shard = shard_of(id, shards);
                self.shard_rt(shard)
                    .map(|rt| rt.cancel(id, "client cancelled", Some(token)))
            }
            Action::ObjectCall {
                id,
                wtype,
                handler,
                call_id,
                arg,
            } => {
                // As `ObjectRef::call_with_id` sends it: update-with-start.
                let now = self.now();
                self.call_sent.entry(call_id.clone()).or_insert(now);
                let mut req = StartRequest::new(
                    id,
                    wtype,
                    Arc::from(&b"null"[..]),
                    IdPolicy {
                        running: OnRunning::UseExisting,
                        finished: OnFinished::Allow,
                    },
                );
                req.update = Some(UpdateRequest {
                    name: handler.clone(),
                    id: call_id.clone(),
                    payload: Arc::from(&arg[..]),
                    token: Some(token),
                });
                let r = self
                    .shard_rt(shard_of(id, shards))
                    .map(|rt| rt.start(req).map(|_| ()));
                self.log(format!("call {call_id} to {id}.{handler}: {r:?}"));
                if !matches!(r, Some(Ok(()))) {
                    // Transient (busy, recovering): send it again.
                    let t = self.now() + 10;
                    self.at(t, WorldEv::Client(ix));
                    return;
                }
                r
            }
            Action::Enqueue {
                coordinator,
                wtype,
                state,
                event_id,
                event,
                ..
            } => {
                // As `Engine::debounce_raw` sends it: update-with-start.
                let mut req = StartRequest::new(
                    coordinator,
                    wtype,
                    Arc::from(&state[..]),
                    IdPolicy {
                        running: OnRunning::UseExisting,
                        finished: OnFinished::Allow,
                    },
                );
                req.update = Some(UpdateRequest {
                    name: EVENT.into(),
                    id: event_id.clone(),
                    payload: Arc::from(&event[..]),
                    token: Some(token),
                });
                let r = self
                    .shard_rt(shard_of(coordinator, shards))
                    .map(|rt| rt.start(req).map(|_| ()));
                self.log(format!("event {event_id} to {coordinator}: {r:?}"));
                if !matches!(r, Some(Ok(()))) {
                    // Every refusal here is transient (busy, recovering).
                    let t = self.now() + 10;
                    self.at(t, WorldEv::Client(ix));
                    return;
                }
                r
            }
        };
        if let Action::Update { id, update_id, .. } = &action {
            self.log(format!("update {update_id} to {id}: {r:?} (token {token})"));
        }
        match r {
            Some(Ok(())) => {
                self.tokens.insert(token, ix);
            }
            Some(Err(Error::NotFound(_) | Error::Failed(_) | Error::Rejected(_))) => {
                // Signal or cancel for a run that finished or was never
                // started: there is nothing to deliver.
                self.acked[ix] = true;
                if let Action::Reset { id } = &action {
                    self.resetting.remove(id);
                }
            }
            _ => {
                let t = self.now() + 10;
                self.at(t, WorldEv::Client(ix));
            }
        }
    }

    fn on_output(&mut self, shard: u32, out: Output) -> Result<(), Failure> {
        match out {
            Output::Ack(t) | Output::Reject(t) => {
                if let Some(ix) = self.tokens.remove(&t) {
                    self.acked[ix] = true;
                    match self.actions[ix].clone() {
                        Action::Reset { id } => {
                            let found = self
                                .shard_rt(shard_of(&id, self.cfg.shards))
                                .and_then(|rt| rt.find(&id));
                            self.log(format!(
                                "reset {id} acknowledged; the ID now names run {found:?}"
                            ));
                            self.report.resets += 1;
                            self.resetting.remove(&id);
                            self.results.remove(&id);
                        }
                        Action::Complete { wf, token } => {
                            let accepted = self.completions.entry(wf.clone()).or_default();
                            if !accepted.contains(&token) {
                                accepted.push(token);
                            }
                            if accepted.len() > 1 {
                                let all = accepted.clone();
                                return Err(self.fail(format!(
                                    "two completions were accepted for {wf}: {all:?}"
                                )));
                            }
                        }
                        _ => {}
                    }
                }
            }
            Output::Refuse { token, message } => {
                if let Some(ix) = self.tokens.remove(&token) {
                    if let Action::ObjectCall { call_id, .. } = &self.actions[ix] {
                        // The key could not take it now (its run was ending
                        // or not running code yet): the client tries again.
                        self.log(format!(
                            "call {call_id} refused: {}; again",
                            String::from_utf8_lossy(&message)
                        ));
                        let t = self.now() + 10;
                        self.at(t, WorldEv::Client(ix));
                        return Ok(());
                    }
                    if let Action::Enqueue { event_id, .. } = &self.actions[ix] {
                        return Err(self.fail(format!(
                            "event {event_id} was refused: {}",
                            String::from_utf8_lossy(&message)
                        )));
                    }
                    self.acked[ix] = true;
                }
            }
            Output::Reply {
                token,
                status,
                payload,
            } if self.read_tokens.contains_key(&token) => {
                if let Some(n) = self.read_tokens.remove(&token) {
                    self.on_read(n, status, &payload)?;
                }
            }
            Output::Reply {
                token,
                status,
                payload,
            } => {
                if let Some(ix) = self.tokens.remove(&token) {
                    if let Action::Enqueue { event_id, .. } = self.actions[ix].clone() {
                        return self.on_receipt(ix, event_id, status, &payload);
                    }
                    if let Action::ObjectCall { call_id, .. } = self.actions[ix].clone() {
                        return self.on_call_reply(ix, call_id, status, &payload);
                    }
                    self.acked[ix] = true;
                    match self.actions[ix].clone() {
                        Action::Update { update_id, .. } => {
                            let got = (status, payload.to_vec());
                            match self.update_replies.get(&update_id) {
                                Some(prev) if *prev != got => {
                                    return Err(self.fail(format!(
                                        "update {update_id} answered {:?} {:?} and later {:?} {:?}",
                                        prev.0,
                                        String::from_utf8_lossy(&prev.1),
                                        got.0,
                                        String::from_utf8_lossy(&got.1)
                                    )));
                                }
                                Some(_) => {}
                                None => {
                                    self.update_replies.insert(update_id, got);
                                }
                            }
                        }
                        Action::Query { id } => {
                            let v: u64 = serde_json::from_slice(&payload).unwrap_or(0);
                            let prev = self.query_answers.get(&id).copied().unwrap_or(0);
                            if v < prev {
                                return Err(self.fail(format!(
                                    "query of {id} answered {v} after answering {prev}: an answer \
                                     left before the state it read was durable"
                                )));
                            }
                            self.all_answers.entry(id.clone()).or_default().insert(v);
                            self.query_answers.insert(id, v);
                        }
                        _ => {}
                    }
                }
            }
            Output::Result { status, .. } if status.is_handover() => {}
            Output::Result {
                run,
                status,
                payload,
            } => {
                // Recovery regenerates the outputs of older runs too; only
                // the ID's current run speaks for the ID.
                let id = self
                    .shard_rt(shard)
                    .and_then(|rt| rt.summary(run).filter(|s| rt.find(&s.id) == Some(run)))
                    .map(|s| s.id);
                if let Some(id) = id {
                    self.log(format!("released result of {id} run {run}: {status:?}"));
                    self.record_result(&id, status, payload.to_vec())?;
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// A key answered a call. Every answer to one call ID must be the same:
    /// a call runs once, however often the client sent it.
    fn on_call_reply(
        &mut self,
        ix: usize,
        call_id: String,
        status: Status,
        payload: &[u8],
    ) -> Result<(), Failure> {
        let now = self.now();
        let status = status.as_str().to_owned();
        if let Some(prev) = self.calls.get(&call_id) {
            if prev.status != status || prev.payload != payload {
                return Err(self.fail(format!(
                    "call {call_id} was answered {} {:?} and later {status} {:?}: it ran twice",
                    prev.status,
                    String::from_utf8_lossy(&prev.payload),
                    String::from_utf8_lossy(payload)
                )));
            }
        } else {
            let sent_ms = self.call_sent.get(&call_id).copied().unwrap_or(now);
            self.log(format!(
                "call {call_id} answered {status} {}",
                String::from_utf8_lossy(payload)
            ));
            self.calls.insert(
                call_id,
                crate::scenario::CallReply {
                    status,
                    payload: payload.to_vec(),
                    sent_ms,
                    answered_ms: now,
                },
            );
        }
        self.acked[ix] = true;
        Ok(())
    }

    /// A coordinator answered an event: a receipt, or "ended first", which
    /// the client answers by sending the event again.
    fn on_receipt(
        &mut self,
        ix: usize,
        event_id: String,
        status: Status,
        payload: &[u8],
    ) -> Result<(), Failure> {
        match status {
            Status::Ok => {
                let r: Receipt = serde_json::from_slice(payload).map_err(|e| {
                    self.fail(format!("event {event_id} got an unreadable receipt: {e}"))
                })?;
                self.log(format!("event {event_id} joined {}", r.id));
                if let Some(prev) = self.receipts.get(&event_id)
                    && *prev != r.id
                {
                    return Err(self.fail(format!(
                        "event {event_id} joined run {prev} and later run {}",
                        r.id
                    )));
                }
                self.receipts.insert(event_id, r.id);
                self.acked[ix] = true;
            }
            Status::Failed if payload == ENDED => {
                self.log(format!("event {event_id}: its coordinator ended; again"));
                let t = self.now() + 1;
                self.at(t, WorldEv::Client(ix));
            }
            s => {
                return Err(self.fail(format!(
                    "event {event_id} failed: {s:?} {}",
                    String::from_utf8_lossy(payload)
                )));
            }
        }
        Ok(())
    }

    fn record_result(&mut self, id: &str, status: Status, payload: Vec<u8>) -> Result<(), Failure> {
        if self.resetting.contains(id) {
            return Ok(());
        }
        match self.results.get(id) {
            Some((s, p)) if *s != status || *p != payload => {
                return Err(self.fail(format!(
                    "workflow {id} reported result {:?} {:?} and later {:?} {:?}",
                    s,
                    String::from_utf8_lossy(p),
                    status,
                    String::from_utf8_lossy(&payload)
                )));
            }
            Some(_) => {}
            None => {
                self.log(format!("result {id} {status:?}"));
                self.results.insert(id.to_owned(), (status, payload));
            }
        }
        Ok(())
    }

    /// Ticks every shard until nothing moves.
    fn settle(&mut self) -> Result<(), Failure> {
        for _ in 0..100_000 {
            let mut progress = false;
            let now = self.now();
            progress |= sim_timers_fire(now);
            for shard in 0..self.cfg.shards {
                let Some(rt) = self.shard_rt(shard) else {
                    continue;
                };
                let tick = rt.tick();
                let released = rt.take_released();
                let outgoing = rt.take_outgoing();
                let acks = rt.take_message_acks();
                let failed = rt.with(|i| i.failed().map(str::to_owned));
                progress |=
                    tick.busy || !released.is_empty() || !outgoing.is_empty() || !acks.is_empty();
                for out in released {
                    self.on_output(shard, out)?;
                }
                let epoch = self.m.borrow().epoch;
                for o in outgoing {
                    let d = self.rng.below(self.cfg.max_msg_ms + 1);
                    self.at(
                        now + d,
                        WorldEv::Deliver {
                            to: o.target,
                            from: shard,
                            id: o.id,
                            msg: o.msg,
                            epoch,
                        },
                    );
                }
                for (to, id) in acks {
                    let d = self.rng.below(self.cfg.max_msg_ms + 1);
                    self.at(now + d, WorldEv::Ack { to, id, epoch });
                }
                if let Some(f) = failed
                    && !self
                        .events
                        .values()
                        .any(|e| matches!(e, WorldEv::Crash | WorldEv::Restart))
                {
                    self.log(format!(
                        "shard {shard} stopped: {f}; restarting the process"
                    ));
                    self.at(now + 1, WorldEv::Restart);
                }
            }
            progress |= self.new_tokens();
            if !progress {
                return Ok(());
            }
        }
        Err(self.fail("the shards never settled".into()))
    }

    /// The client completes every token the approval system handed out,
    /// stale ones included (those must be refused).
    fn new_tokens(&mut self) -> bool {
        let fresh: Vec<(String, String)> = {
            let s = match self.svc.lock() {
                Ok(s) => s,
                Err(_) => return false,
            };
            s.tokens[self.tokens_seen.min(s.tokens.len())..].to_vec()
        };
        if fresh.is_empty() {
            return false;
        }
        self.tokens_seen += fresh.len();
        let now = self.now();
        for (wf, token) in fresh {
            let ix = self.actions.len();
            self.actions.push(Action::Complete { wf, token });
            self.acked.push(false);
            let t = now + self.rng.below(20);
            self.at(t, WorldEv::Client(ix));
        }
        true
    }

    fn next_time(&self) -> Option<u64> {
        let now = self.now();
        let mut t: Option<u64> = None;
        let mut take = |x: Option<u64>| {
            if let Some(x) = x {
                t = Some(t.map_or(x, |y: u64| y.min(x)));
            }
        };
        take(self.events.keys().next().map(|k| k.0));
        take(self.m.borrow().io.keys().next().map(|k| k.0));
        take(sim_timers_next());
        for s in self.shards.iter().flatten() {
            if s.with(|i| i.failed().is_none()) {
                take(s.tick().next_wake_ms);
            }
        }
        t.map(|t| t.max(now))
    }

    fn crash(&mut self) -> Result<(), Failure> {
        self.report.crashes += 1;
        self.report.offloaded += self
            .log
            .as_ref()
            .map_or(0, |l| l.borrow().wal.blob_stats().records);
        self.log("crash: process dies; unflushed writes are lost or torn".into());
        self.check_limits()?;
        self.shards.clear();
        // The restarted process runs new code (a patch) and has fresh
        // limits: permits held by the dead process are gone with it.
        self.deployed.store(true, Ordering::Relaxed);
        self.limits = Arc::new(LocalLimits::new());
        sim_timers_clear();
        {
            let mut m = self.m.borrow_mut();
            let tear = self.cfg.tear;
            let mut rng = m.rng.fork();
            m.disk.crash(&mut rng, tear);
            m.io.clear();
            m.epoch += 1;
        }
        self.events
            .retain(|_, e| !matches!(e, WorldEv::Deliver { .. } | WorldEv::Ack { .. }));
        self.tokens.clear();
        self.read_tokens.clear();
        self.open_all().map_err(|e| self.fail(e))?;
        self.log(format!("recovered {} shards", self.cfg.shards));
        // Readers connect again at their offsets.
        let now = self.now();
        for n in 0..self.readers.len() {
            if !self.readers[n].done {
                self.at(now + 1 + n as u64 % 4, WorldEv::Read(n));
            }
        }
        // Client retries everything not acknowledged.
        let now = self.now();
        for ix in 0..self.actions.len() {
            if !self.acked[ix]
                && self
                    .events
                    .values()
                    .all(|e| !matches!(e, WorldEv::Client(i) if *i == ix))
            {
                let started_before = true;
                if started_before {
                    self.at(now + 1, WorldEv::Client(ix));
                }
            }
        }
        self.check_after_recovery()
    }

    fn check_after_recovery(&mut self) -> Result<(), Failure> {
        let shards = self.cfg.shards;
        for ix in 0..self.actions.len() {
            if !self.acked[ix] {
                continue;
            }
            match self.actions[ix].clone() {
                Action::Start { id, .. } | Action::StartAs { id, .. } => {
                    let shard = shard_of(&id, shards);
                    if self.shard_rt(shard).and_then(|rt| rt.find(&id)).is_none() {
                        return Err(self.fail(format!("acknowledged start of {id} was lost")));
                    }
                }
                Action::Signal { id, dedup, .. } => {
                    let shard = shard_of(&id, shards);
                    let Some(rt) = self.shard_rt(shard) else {
                        continue;
                    };
                    match rt.inspect(&id) {
                        Ok(Some(d)) if d.summary.finished_ms.is_none() => {
                            let found = d.journal.iter().any(
                                |r| matches!(r, Record::Signal { dedup: x, .. } if *x == dedup),
                            );
                            if !found {
                                return Err(self.fail(format!(
                                    "acknowledged signal {dedup} to {id} is missing from its journal: {:?} {:?}",
                                    d.summary, d.journal
                                )));
                            }
                        }
                        Ok(_) => {}
                        Err(e) => return Err(self.fail(format!("inspect {id}: {e}"))),
                    }
                }
                Action::Enqueue {
                    coordinator,
                    event_id,
                    ..
                } => {
                    // Its run has started, or the coordinator that holds
                    // its window (or sends the run's start) still exists.
                    let Some(run) = self.receipts.get(&event_id) else {
                        continue;
                    };
                    let exists = |id: &str| {
                        self.shard_rt(shard_of(id, shards))
                            .and_then(|rt| rt.find(id))
                            .is_some()
                    };
                    if !exists(run) && !exists(&coordinator) {
                        return Err(self.fail(format!(
                            "acknowledged event {event_id} was lost: neither its run {run} nor \
                             its coordinator {coordinator} exists"
                        )));
                    }
                }
                _ => {}
            }
        }
        self.check_answers()?;
        // Results seen before must not change.
        let known: Vec<String> = self.results.keys().cloned().collect();
        for id in known {
            let shard = shard_of(&id, shards);
            let r = self.shard_rt(shard).map(|rt| rt.result(&id));
            match r {
                Some(Ok(ResultState::Done {
                    status,
                    payload,
                    run,
                })) => {
                    self.log(format!("after recovery {id} is run {run}: {status:?}"));
                    self.record_result(&id, status, payload.to_vec())?
                }
                Some(Ok(ResultState::NotFound)) => {
                    return Err(
                        self.fail(format!("finished workflow {id} vanished after recovery"))
                    );
                }
                Some(Ok(ResultState::Running(_))) => {
                    let job = self.actions.iter().find_map(|a| match a {
                        Action::Start { id: x, job } if *x == id => Some(format!("{job:?}")),
                        Action::StartAs { id: x, wtype, .. } if *x == id => Some(wtype.clone()),
                        _ => None,
                    });
                    let detail = self
                        .shard_rt(shard)
                        .and_then(|rt| rt.inspect(&id).ok().flatten());
                    return Err(self.fail(format!(
                        "workflow {id} ({}) had reported a result but runs again after recovery: {detail:?}",
                        job.unwrap_or_default()
                    )));
                }
                Some(Err(e)) => return Err(self.fail(format!("result {id}: {e}"))),
                None => {}
            }
        }
        self.check_service()
    }

    /// Every query answer must be a state the journal recorded: the total
    /// after some update (each handler returns the total it set).
    fn check_answers(&mut self) -> Result<(), Failure> {
        let shards = self.cfg.shards;
        for (id, answers) in self.all_answers.clone() {
            let Some(rt) = self.shard_rt(shard_of(&id, shards)) else {
                continue;
            };
            let Ok(Some(d)) = rt.inspect(&id) else {
                continue;
            };
            let full = d
                .journal
                .iter()
                .any(|r| !matches!(r, Record::Start { .. } | Record::Done { .. }));
            if !full && d.summary.finished_ms.is_some() {
                continue;
            }
            let mut states: BTreeSet<u64> = [0].into();
            for r in &d.journal {
                if let Record::UpdateDone {
                    status: Status::Ok,
                    payload,
                    ..
                } = r
                {
                    let p = self
                        .codec
                        .as_ref()
                        .and_then(|c| c.decode(payload).ok())
                        .unwrap_or_else(|| payload.to_vec());
                    if let Ok(v) = serde_json::from_slice::<u64>(&p) {
                        states.insert(v);
                    }
                }
            }
            if let Some(a) = answers.iter().find(|a| !states.contains(a)) {
                return Err(self.fail(format!(
                    "query of {id} answered {a}, a state its journal never recorded ({states:?}): \
                     the answer left before that state was durable"
                )));
            }
        }
        Ok(())
    }

    fn check_limits(&mut self) -> Result<(), Failure> {
        self.max_permits = self.max_permits.max(self.limits.max_in_use("svc"));
        if self.max_permits > 2 {
            return Err(self.fail(format!(
                "limit 'svc' (2 permits) had {} permits out at once",
                self.max_permits
            )));
        }
        Ok(())
    }

    /// End-of-run checks: no run stopped on a determinism error (replaying
    /// patched, continued and reset runs reproduced their journals), every
    /// accepted update ran once, children followed their close policy.
    fn check_final(&mut self) -> Result<(), Failure> {
        self.check_limits()?;
        self.check_streams()?;
        if self.cfg.user.is_none() {
            self.check_queue_contents()?;
        }
        // Runs a debounce or batch window started: one per window, and only
        // for windows some acknowledged event joined.
        let mut targets = BTreeSet::new();
        let mut direct = BTreeSet::new();
        for a in &self.actions {
            match a {
                Action::Enqueue { workflow, .. } => {
                    targets.insert(workflow.clone());
                }
                Action::StartAs { id, .. } => {
                    direct.insert(id.clone());
                }
                _ => {}
            }
        }
        let windows: BTreeSet<&String> = self.receipts.values().collect();
        for shard in 0..self.cfg.shards {
            let Some(rt) = self.shard_rt(shard) else {
                continue;
            };
            for s in rt.list(1_000_000) {
                if s.status == "stuck" {
                    return Err(self.fail(format!(
                        "{} stopped on a determinism error: {}",
                        s.id,
                        s.error.unwrap_or_default()
                    )));
                }
                if targets.contains(&s.wtype) && !direct.contains(&s.id) && !windows.contains(&s.id)
                {
                    return Err(self.fail(format!(
                        "run {} of {} was started by a window no acknowledged event joined",
                        s.id, s.wtype
                    )));
                }
            }
        }
        let shards = self.cfg.shards;
        let counters: Vec<String> = self
            .actions
            .iter()
            .filter_map(|a| match a {
                Action::Start {
                    id,
                    job: Job::Counter,
                } => Some(id.clone()),
                _ => None,
            })
            .collect();
        for id in &counters {
            // A total only grows, so no answer may exceed the final one.
            if let (Some(answer), Some((Status::Ok, p))) =
                (self.query_answers.get(id), self.results.get(id))
            {
                let last: u64 = serde_json::from_slice(p).unwrap_or(u64::MAX);
                if *answer > last {
                    return Err(self.fail(format!(
                        "query of {id} answered {answer} but its final total is {last}: an \
                         answer reflected state a crash took back"
                    )));
                }
            }
        }
        for id in counters {
            let Some(rt) = self.shard_rt(shard_of(&id, shards)) else {
                continue;
            };
            if let Ok(Some(d)) = rt.inspect(&id) {
                let mut seen = BTreeSet::new();
                for r in &d.journal {
                    if let Record::Update { id: u, .. } = r
                        && !seen.insert(u.to_string())
                    {
                        return Err(self.fail(format!(
                            "update {u} was accepted twice by {id}: {:?}",
                            d.journal
                        )));
                    }
                }
            }
        }
        for (parent, kids) in self.parents.clone() {
            for k in 0..kids {
                let kid = format!("{parent}/kid{k}");
                let Some(rt) = self.shard_rt(shard_of(&kid, shards)) else {
                    continue;
                };
                let Some(s) = rt.find(&kid).and_then(|r| rt.summary(r)) else {
                    continue;
                };
                let ok = match k % 2 {
                    0 => matches!(s.status.as_str(), "terminated" | "ok"),
                    _ => s.status == "ok",
                };
                if !ok {
                    return Err(self.fail(format!(
                        "child {kid} ({}) ended '{}' against its parent close policy",
                        if k % 2 == 0 { "terminate" } else { "abandon" },
                        s.status
                    )));
                }
            }
        }
        Ok(())
    }

    fn check_service(&self) -> Result<(), Failure> {
        let v = self
            .svc
            .lock()
            .map(|s| s.violations.clone())
            .unwrap_or_default();
        if let Some(first) = v.first() {
            return Err(self.fail(first.clone()));
        }
        Ok(())
    }

    fn handle(&mut self, ev: WorldEv) -> Result<(), Failure> {
        let epoch = self.m.borrow().epoch;
        match ev {
            WorldEv::Client(ix) => self.do_action(ix),
            WorldEv::Deliver {
                to,
                from,
                id,
                msg,
                epoch: e,
            } => {
                if e == epoch
                    && let Some(rt) = self.shard_rt(to)
                {
                    let _ = rt.message(msg, Some((from, id)));
                }
            }
            WorldEv::Ack { to, id, epoch: e } => {
                if e == epoch
                    && let Some(rt) = self.shard_rt(to)
                {
                    rt.outbox_ack(id);
                }
            }
            WorldEv::Crash => {
                if self.faults {
                    self.crash()?;
                }
            }
            WorldEv::Restart => self.crash()?,
            WorldEv::Read(n) => self.do_read(n),
        }
        Ok(())
    }

    /// Reader `n` asks for its stream from its offset (a long poll of 20
    /// simulated ms).
    fn do_read(&mut self, n: usize) {
        let Some(r) = self.readers.get(n).cloned() else {
            return;
        };
        if r.done || self.read_tokens.values().any(|x| *x == n) {
            return;
        }
        let token = self.next_token;
        self.next_token += 1;
        let res = self
            .shard_rt(shard_of(&r.id, self.cfg.shards))
            .map(|rt| rt.stream_read(&r.id, r.run, &r.name, r.next, 20, token));
        match res {
            Some(Ok(())) => {
                self.read_tokens.insert(token, n);
            }
            // Not started yet, or the shard is down: ask again soon.
            _ => {
                let t = self.now() + 5;
                self.at(t, WorldEv::Read(n));
            }
        }
    }

    /// A read's answer: every chunk must be the one readers saw at its
    /// offset before (a chunk a reader saw never changes or goes away),
    /// with no offset skipped, and a stream ends once.
    fn on_read(&mut self, n: usize, status: Status, payload: &[u8]) -> Result<(), Failure> {
        let now = self.now();
        if status != Status::Ok {
            self.at(now + 5, WorldEv::Read(n));
            return Ok(());
        }
        let b = fusee::StreamBatch::parse(payload)
            .map_err(|e| self.fail(format!("a stream read answered something unreadable: {e}")))?;
        let r = self.readers[n].clone();
        if b.from != r.next || r.run.is_some_and(|run| run != b.run) {
            return Err(self.fail(format!(
                "reader {n} of {} {} asked for offset {} of run {:?} and got offset {} of run {}",
                r.id, r.name, r.next, r.run, b.from, b.run
            )));
        }
        let key = (r.id.clone(), b.run, r.name.clone());
        let mut seen = self.seen.remove(&key).unwrap_or_default();
        for c in &b.chunks {
            let v = match &c.value {
                fusee::ChunkValue::Json(v) => v.clone(),
                fusee::ChunkValue::Bytes(b) => serde_json::json!(b),
            };
            match seen.chunks.get(c.offset as usize) {
                Some(prev) if *prev != v => {
                    return Err(self.fail(format!(
                        "chunk {} of {} {} was {prev} for one reader and {v} for another: a reader saw a chunk a crash took back",
                        c.offset, r.id, r.name
                    )));
                }
                Some(_) => {}
                None if c.offset as usize == seen.chunks.len() => seen.chunks.push(v),
                None => {
                    return Err(self.fail(format!(
                        "a reader of {} {} got chunk {} while readers had seen {}",
                        r.id,
                        r.name,
                        c.offset,
                        seen.chunks.len()
                    )));
                }
            }
        }
        self.report.chunks_read += b.chunks.len() as u64;
        if let Some(end) = &b.end {
            if b.next < seen.chunks.len() as u64 {
                return Err(self.fail(format!(
                    "{} {} ended at {} chunks after readers saw {}",
                    r.id,
                    r.name,
                    b.next,
                    seen.chunks.len()
                )));
            }
            match &seen.end {
                Some(prev) if *prev != end.status => {
                    return Err(self.fail(format!(
                        "{} {} ended '{prev}' for one reader and '{}' for another",
                        r.id, r.name, end.status
                    )));
                }
                _ => seen.end = Some(end.status.clone()),
            }
            self.report.streams_read += 1;
        }
        self.seen.insert(key, seen);
        let reader = &mut self.readers[n];
        reader.run = Some(b.run);
        reader.next = b.next;
        if b.end.is_some() {
            reader.done = true;
        } else {
            self.at(now + (n as u64 % 3), WorldEv::Read(n));
        }
        Ok(())
    }

    /// Every finished stream holds exactly what its workflow made: the
    /// tokens its step returned (a step that ran again continued after the
    /// recorded prefix) and its progress, appended once across replays.
    fn check_streams(&self) -> Result<(), Failure> {
        for a in &self.actions {
            let Action::Start {
                id,
                job: Job::Talker { .. },
            } = a
            else {
                continue;
            };
            let Some((Status::Ok, p)) = self.results.get(id) else {
                continue;
            };
            let hash: u64 = serde_json::from_slice(p).unwrap_or(0);
            for ((sid, _, name), s) in &self.seen {
                if sid != id {
                    continue;
                }
                if s.end.as_deref() != Some("ok") {
                    return Err(self.fail(format!(
                        "{id} {name} ended {:?}, not ok: {:?}",
                        s.end, s.chunks
                    )));
                }
                match name.as_str() {
                    "tokens" => {
                        let toks: Vec<u64> = s
                            .chunks
                            .iter()
                            .map(|v| v.as_u64().unwrap_or(u64::MAX))
                            .collect();
                        if tokens_hash(&toks) != hash {
                            return Err(self.fail(format!(
                                "readers of {id} saw tokens {toks:?}, not the ones its step returned"
                            )));
                        }
                    }
                    _ => {
                        let want: Vec<serde_json::Value> = TALKER_PROGRESS
                            .iter()
                            .map(|s| serde_json::json!(s))
                            .collect();
                        if s.chunks != want {
                            return Err(self.fail(format!(
                                "readers of {id} saw progress {:?}: a replay appended again, or an append was lost",
                                s.chunks
                            )));
                        }
                    }
                }
            }
        }
        Ok(())
    }

    fn handle_io(&mut self, io: Io) {
        let epoch = self.m.borrow().epoch;
        match io {
            Io::Flush { shard, epoch: e } if e == epoch => {
                if let Some(rt) = self.shard_rt(shard) {
                    rt.with(|i| i.env.complete_flush());
                }
            }
            Io::Checkpoint { shard, epoch: e } if e == epoch => {
                if let Some(rt) = self.shard_rt(shard) {
                    rt.with(|i| i.env.complete_checkpoint());
                }
            }
            Io::LogFlush { epoch: e, seq } if e == epoch => {
                if let Some(log) = self.log.clone() {
                    SimLog::complete(&log, &self.m, seq);
                    // The narrow window of two writes in flight: one is
                    // durable, an earlier or later one is not. Crash in it
                    // now and then.
                    if self.faults
                        && self.cfg.crash_every_ms > 0
                        && log.borrow().in_flight() > 0
                        && self.rng.below(20) == 0
                    {
                        let now = self.now();
                        self.at(now, WorldEv::Crash);
                    }
                }
            }
            Io::BlobFlush { epoch: e, id } if e == epoch => {
                if let Some(log) = self.log.clone() {
                    SimLog::complete_blobs(&log, &self.m, id);
                    // Blobs durable, the batches that reference them not
                    // yet: crash in that window now and then.
                    if self.faults && self.cfg.crash_every_ms > 0 && self.rng.below(20) == 0 {
                        let now = self.now();
                        self.at(now, WorldEv::Crash);
                    }
                }
            }
            _ => {}
        }
    }

    /// Advances to the next moment anything happens.
    fn step(&mut self) -> Result<bool, Failure> {
        self.settle()?;
        let shared_block = self
            .log
            .as_ref()
            .and_then(|l| l.borrow().violation().map(str::to_owned));
        if let Some(v) = shared_block {
            return Err(self.fail(v));
        }
        let Some(t) = self.next_time() else {
            return Ok(false);
        };
        self.m.borrow_mut().now = t;
        NOW.with(|n| n.set(t));
        loop {
            let io = {
                let mut m = self.m.borrow_mut();
                match m.io.keys().next().copied() {
                    Some(k) if k.0 <= t => m.io.remove(&k),
                    _ => None,
                }
            };
            match io {
                Some(io) => self.handle_io(io),
                None => break,
            }
        }
        loop {
            let ev = match self.events.keys().next().copied() {
                Some(k) if k.0 <= t => self.events.remove(&k),
                _ => None,
            };
            match ev {
                Some(ev) => self.handle(ev)?,
                None => break,
            }
        }
        Ok(true)
    }

    pub fn run(mut self) -> Result<Report, Failure> {
        self.open_all().map_err(|e| self.fail(e))?;
        let start = self.now();
        let fault_end = start + self.cfg.duration_ms;
        while self.now() < fault_end {
            if !self.step()? {
                break;
            }
            self.check_service()?;
        }
        // Faults stop. Every acknowledged workflow must now finish.
        self.faults = false;
        self.m.borrow_mut().flush_fail_per_mille = 0;
        self.events.retain(|_, e| !matches!(e, WorldEv::Crash));
        if self
            .shards
            .iter()
            .flatten()
            .any(|s| s.with(|i| i.failed().is_some()))
        {
            self.faults = true;
            self.crash()?;
            self.faults = false;
        }
        let deadline = fault_end + self.cfg.settle_ms;
        loop {
            let all_done = self.all_finished();
            if all_done {
                break;
            }
            // No more crashes change the code: a stuck run stays stuck.
            if let Some((id, why)) = self.first_stuck() {
                return Err(self.fail(format!("{id} stopped on a determinism error: {why}")));
            }
            if self.now() > deadline {
                let pending = self.unfinished();
                return Err(self.fail(format!(
                    "{} workflows did not finish after faults stopped: {}",
                    pending.len(),
                    pending.join(", ")
                )));
            }
            if !self.step()? {
                // Nothing scheduled: give retransmission timers a chance.
                let t = self.now() + 500;
                self.m.borrow_mut().now = t;
                NOW.with(|n| n.set(t));
            }
        }
        self.check_after_recovery()?;
        self.check_final()?;
        self.run_user_checks()?;
        self.report.finished = self.results.len() as u32;
        self.report.updates = self.update_replies.len() as u32;
        self.report.queries = self.query_answers.len() as u32;
        self.report.completions = self.completions.values().map(|v| v.len() as u32).sum();
        self.report.queued = self.receipts.len() as u32;
        self.report.windows = self.receipts.values().collect::<BTreeSet<_>>().len() as u32;
        if let Ok(s) = self.svc.lock() {
            self.report.scheduled = s.ticks.len() as u32;
            self.report.heartbeats = s.progress.values().map(|p| p.len() as u32).sum();
            self.report.reads_rerun = s.reads_rerun;
            self.report.shared_log = self.log.is_some();
            self.report.log_batches = self
                .log
                .as_ref()
                .map_or(0, |l| l.borrow().wal.position() - 1);
        }
        self.report.offloaded += self
            .log
            .as_ref()
            .map_or(0, |l| l.borrow().wal.blob_stats().records);
        self.report.sim_ms = self.now() - start;
        self.report.service_calls = self.svc.lock().map(|s| s.total).unwrap_or(0);
        self.report.records = self
            .shards
            .iter()
            .flatten()
            .map(|s| s.metrics().records)
            .sum();
        self.report.flush_failures = self
            .m
            .borrow()
            .log
            .iter()
            .filter(|l| l.contains("flush failed"))
            .count() as u32;
        Ok(self.report)
    }

    fn run_user_checks(&mut self) -> Result<(), Failure> {
        if self.checks.is_empty() {
            return Ok(());
        }
        let mut streams = BTreeMap::new();
        // Keys sort by run, so each ID's newest run read comes last.
        for ((id, _, name), s) in &self.seen {
            streams.insert(
                (id.clone(), name.clone()),
                (s.chunks.clone(), s.end.clone()),
            );
        }
        let outcome = Outcome {
            seed: self.cfg.seed,
            crashes: self.report.crashes,
            results: self.results.clone(),
            receipts: self.receipts.clone(),
            streams,
            calls: self.calls.clone(),
        };
        for check in std::mem::take(&mut self.checks) {
            let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| check(&outcome)));
            let message = match r {
                Ok(Ok(())) => continue,
                Ok(Err(m)) => m,
                Err(p) => p
                    .downcast_ref::<String>()
                    .cloned()
                    .or_else(|| p.downcast_ref::<&str>().map(|s| (*s).to_owned()))
                    .unwrap_or_else(|| "the check panicked".into()),
            };
            return Err(self.fail(format!("check failed: {message}")));
        }
        Ok(())
    }

    fn first_stuck(&self) -> Option<(String, String)> {
        let shards = self.cfg.shards;
        self.started_ids()
            .into_iter()
            .filter(|id| !self.results.contains_key(id))
            .find_map(|id| {
                let rt = self.shard_rt(shard_of(&id, shards))?;
                let why = rt.stuck_reason(rt.find(&id)?)?;
                Some((id, why))
            })
    }

    fn started_ids(&self) -> Vec<String> {
        let mut ids: Vec<String> = self
            .actions
            .iter()
            .zip(&self.acked)
            .filter_map(|(a, ok)| match a {
                Action::Start { id, .. } | Action::StartAs { id, .. } if *ok => Some(id.clone()),
                _ => None,
            })
            .collect();
        // The run every acknowledged debounced or batched event joined must
        // start and finish too.
        let runs: BTreeSet<&String> = self.receipts.values().collect();
        ids.extend(runs.into_iter().cloned());
        ids
    }

    fn all_finished(&mut self) -> bool {
        if self.acked.iter().any(|a| !a) || self.readers.iter().any(|r| !r.done) {
            return false;
        }
        let shards = self.cfg.shards;
        // Children that exist must have ended too (an abandoned child may
        // start late, after its start message was resent).
        for (parent, kids) in self.parents.clone() {
            for k in 0..kids {
                let kid = format!("{parent}/kid{k}");
                let running = self.shard_rt(shard_of(&kid, shards)).is_some_and(|rt| {
                    rt.find(&kid)
                        .and_then(|r| rt.summary(r))
                        .is_some_and(|s| s.finished_ms.is_none())
                });
                if running {
                    return false;
                }
            }
        }
        for id in self.started_ids() {
            if self.results.contains_key(&id) {
                continue;
            }
            let r = self
                .shard_rt(shard_of(&id, shards))
                .and_then(|rt| rt.result(&id).ok());
            if let Some(ResultState::Done {
                status, payload, ..
            }) = r
            {
                if self.record_result(&id, status, payload.to_vec()).is_err() {
                    return false;
                }
            } else {
                return false;
            }
        }
        true
    }

    fn unfinished(&self) -> Vec<String> {
        let shards = self.cfg.shards;
        let mut out = Vec::new();
        for (ix, a) in self.actions.iter().enumerate() {
            if !self.acked[ix] {
                out.push(format!("{a:?} (unacknowledged)"));
            }
        }
        for r in self.readers.iter().filter(|r| !r.done) {
            out.push(format!("reader {r:?} (not at the end of its stream)"));
        }
        for id in self.started_ids() {
            if !self.results.contains_key(&id) {
                let rt = self.shard_rt(shard_of(&id, shards));
                let s = rt.and_then(|rt| rt.find(&id).and_then(|r| rt.summary(r)));
                let j = rt
                    .and_then(|rt| rt.inspect(&id).ok().flatten())
                    .map(|d| d.journal);
                let timers = rt.map(|rt| {
                    rt.with(|i| {
                        (
                            i.core.timers_len(),
                            i.core.next_timer(),
                            fusee::env::Env::now_ms(&i.env),
                        )
                    })
                });
                let child = format!("{id}/ship");
                let crt = self.shard_rt(shard_of(&child, shards));
                let cs = crt.and_then(|rt| rt.find(&child).and_then(|r| rt.summary(r)));
                let outboxes: Vec<usize> = self
                    .shards
                    .iter()
                    .flatten()
                    .map(|s| s.outbox_len())
                    .collect();
                out.push(format!("{id}: {s:?}\n    timers {timers:?}\n    journal {j:?}\n    child {cs:?}\n    outboxes {outboxes:?}"));
            }
        }
        out
    }
}

/// Runs one seed.
pub fn run_seed(cfg: SimConfig) -> Result<Report, Failure> {
    let _ = SimFile::default();
    World::new(cfg).run()
}