detcore-model 0.5.5

Core data model and types for Detcore, the Hermit deterministic execution engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

//! Detcore configuration and widely used types.

use std::collections::BTreeSet;
use std::ffi::OsString;
use std::fmt;
use std::num::NonZeroU32;
use std::num::NonZeroU64;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::SystemTime;

use chrono::DateTime;
use chrono::Utc;
use clap::Parser;
use reverie::BackendCapabilities;
use serde::Deserialize;
use serde::Serialize;

use crate::happens_before::HappensBeforeProgram;
use crate::network_trace::NetworkTraceConfig;
use crate::pid::DetTid;
use crate::schedule::SigWrapper;
use crate::time::NANOS_PER_RCB;
use crate::time::NANOS_PER_SCHED;
use crate::time::RcbTimeMultiplier;

const fn default_backend_capabilities() -> BackendCapabilities {
    DEFAULT_BACKEND_CAPABILITIES
}

/// The capabilities a [`Config`] has before the host prepares it for the
/// backend that runs it: ptrace's, the backend Detcore's own tests use, except
/// that nine facts stay off (`supports_blocked_wait_signal_interruption` and
/// the four split out of it, which [`set_kernel_signal_facts`] clears
/// together, `delivers_preemption_timer_events`,
/// `runs_guest_code_between_injections`, `reports_sigreturns`,
/// `hardware_watchpoints`). Until
/// `supports_blocked_wait_signal_interruption`,
/// `delivers_preemption_timer_events` and
/// `runs_guest_code_between_injections` became backend capabilities they were
/// `Config` fields that only the host set, for the backend it had selected,
/// and were false otherwise; a configuration nobody prepared keeps that
/// behaviour, and the four readings of the first with it. A configuration
/// nobody prepared cannot know that sigreturns are reported, so that is off
/// too, and it sets no hardware watchpoints (the DBT runtime's standalone
/// fallback starts from these). `reports_kernel_child_exit_signals` keeps
/// ptrace's value, as `reports_child_exit_publication` does: it took over the
/// child-exit ledger's gates from that field, so such a configuration behaves
/// as before. The host copies the running backend's own answer over all of
/// these before a run.
pub const DEFAULT_BACKEND_CAPABILITIES: BackendCapabilities = {
    let mut backend = BackendCapabilities::PTRACE;
    set_kernel_signal_facts(&mut backend, false);
    backend.delivers_preemption_timer_events = false;
    backend.runs_guest_code_between_injections = false;
    backend.reports_sigreturns = false;
    backend.hardware_watchpoints = 0;
    // Nor does it know which counter the run's RCB clock is.
    backend.rcb_clock_contract = None;
    backend
};

/// The RCB clock a [`Config`] was prepared for.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum RcbClockClaim {
    /// Nobody prepared the configuration for a qualified clock.
    #[default]
    Unset,
    /// Prepared for this qualified clock key.
    Known(reverie::pmu_config::ClockKey),
    /// Prepared for a backend-local RCB clock stated only by this opaque,
    /// versioned token. The token names what makes two recordings
    /// comparable (backend kind, the counter it counts, and a version);
    /// Detcore compares tokens for equality and never interprets them.
    BackendLocal(&'static str),
}

/// Sets `supports_blocked_wait_signal_interruption` and the four capabilities
/// Detcore read from it until A5 step 3c.0 split them out
/// (`kernel_signal_state_is_guests`, `kernel_signal_targets_are_predictable`,
/// `host_task_reads_reach_guest_threads`, `host_timed_signals_are_physical`)
/// to `on`, together. Every backend has the five equal, so a configuration
/// that set or cleared the one field before the split sets or clears all five
/// with this, and behaves as it did.
pub const fn set_kernel_signal_facts(backend: &mut BackendCapabilities, on: bool) {
    backend.supports_blocked_wait_signal_interruption = on;
    backend.kernel_signal_state_is_guests = on;
    backend.kernel_signal_targets_are_predictable = on;
    backend.host_task_reads_reach_guest_threads = on;
    backend.host_timed_signals_are_physical = on;
}

/// One mount row whose kernel-private root must be replaced before it becomes
/// guest-visible.
///
/// The CLI populates this only after proving, with held file descriptors in the
/// completed mount namespace, that the mount is one Hermit created.  The raw
/// mount ID is namespace-local, so these entries are valid only for the one
/// container run whose configuration carries them.
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct MountInfoRootRewrite {
    /// Mount ID read from the held target descriptor's `/proc/self/fdinfo`.
    pub raw_mount_id: u64,
    /// Stable guest-visible replacement for the row's root field.
    pub deterministic_root: Vec<u8>,
    /// Exact encoded kernel root prefix used for descendant mount rows.
    ///
    /// This is present only for a proven private `/tmp`. Mounts installed below
    /// that directory before it is bound over guest `/tmp` otherwise expose the
    /// randomly named backing directory in mountinfo field 5.
    #[serde(default)]
    pub raw_root_prefix: Option<Vec<u8>>,
    /// Guest-visible prefix replacing `raw_root_prefix`.
    #[serde(default)]
    pub deterministic_root_prefix: Option<Vec<u8>>,
    /// Exact encoded host path prefix used for descendant mountpoints.
    #[serde(default)]
    pub raw_mountpoint_prefix: Option<Vec<u8>>,
    /// Guest-visible prefix replacing `raw_mountpoint_prefix`.
    #[serde(default)]
    pub deterministic_mountpoint_prefix: Option<Vec<u8>>,
}

/// The seccomp state of the thread that starts the guest, which the guest
/// inherits: every filter that thread has applies to the guest's system calls,
/// and to the calls Detcore injects into it. Detcore refuses the guest's own
/// `seccomp(2)` and `prctl(PR_SET_SECCOMP)`, so these and the backend's own
/// filter are the only filters a guest has.
#[derive(
    Clone,
    Copy,
    Debug,
    Default,
    PartialEq,
    Eq,
    Hash,
    Serialize,
    Deserialize
)]
pub enum InheritedSeccomp {
    /// Not measured, or not readable, so a filter may apply.
    #[default]
    Unknown,
    /// No seccomp (`Seccomp: 0`).
    Disabled,
    /// Strict mode (`Seccomp: 1`).
    Strict,
    /// Filter mode (`Seccomp: 2`), with the number of filters when
    /// `Seccomp_filters` gives it.
    Filter(Option<u32>),
}

impl InheritedSeccomp {
    /// The state of the calling thread, which a process it forks inherits,
    /// from `/proc/thread-self/status`.
    pub fn measure() -> Self {
        std::fs::read_to_string("/proc/thread-self/status")
            .map_or(Self::Unknown, |status| Self::parse(&status))
    }

    /// The state the `Seccomp` and `Seccomp_filters` lines of the text of a
    /// `/proc/<pid>/status` file give. A missing or unexpected `Seccomp` line
    /// is unknown.
    pub fn parse(status: &str) -> Self {
        let field = |name: &str| {
            status
                .lines()
                .find_map(|line| line.strip_prefix(name)?.strip_prefix(':'))
                .map(str::trim)
        };
        match field("Seccomp") {
            Some("0") => Self::Disabled,
            Some("1") => Self::Strict,
            Some("2") => Self::Filter(field("Seccomp_filters").and_then(|n| n.parse().ok())),
            _ => Self::Unknown,
        }
    }
}

/// What a launch tells Detcore about the guest's first image
/// ([`Config::launch_image`]).
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct LaunchImage {
    /// The first image as its `/proc/self/exe` link names it: the program's
    /// canonical path, or for a script the interpreter it runs
    /// (`detcore::exec_record::ExecRecord::resolve`). `None` where the launch
    /// could not resolve it, which leaves the first image without a record.
    pub exe: Option<PathBuf>,
    /// The image the kernel exec'd in the guest's place, where a launcher
    /// loads the guest itself (SaBRe's loader, DynamoRIO's library), so that
    /// the kernel's `/proc/<pid>/exe` names the launcher. Detcore keeps a recorded image
    /// only where the kernel's link names that image or this one
    /// (`detcore::exec_record`). `None` where the kernel execs the guest.
    pub launcher: Option<PathBuf>,
}

/// One mount namespace, as `DetConfig::mountinfo_mount_namespace` names it.
///
/// `id` is the kernel's 64-bit namespace ID (`ioctl(NS_GET_MNTNS_ID)` on a
/// `/proc/<pid>/ns/mnt` descriptor, on kernels that have it). Linux draws it from
/// one counter that only increases (`__ns_tree_gen_id` in kernel/nstree.c), so
/// it names one namespace for the life of the boot. `device` and `inode` are
/// the descriptor's nsfs device and inode numbers, which Linux recycles once a
/// namespace is gone; they alone identify the namespace only on a kernel
/// without the ioctl, where `id` is `None`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct MountNamespaceIdentity {
    /// The kernel's namespace ID, where the kernel reports one.
    pub id: Option<u64>,
    /// The nsfs device number of the namespace's descriptor.
    pub device: u64,
    /// The nsfs inode number of the namespace's descriptor.
    pub inode: u64,
}

/// What `--detlog-anon` records for each anonymous mapping.
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Serialize,
    Deserialize,
    clap::ValueEnum
)]
pub enum DetlogAnon {
    /// One digest per mapping.
    Mappings,
    /// One digest per mapping, and a short one for each of its 4 KiB pages that is not all zero.
    Pages,
}

/// A frozen copy of a directory whose files the guest maps read-only while
/// another process rewrites them in place. On Android that is the system
/// property area, `/dev/__properties__`: init writes every property change into
/// those shared files, and bionic (the dynamic linker's private copy as well as
/// libc's) rereads its cached values whenever their serial moves, so property
/// writes made outside the run during it change which code the guest runs.
///
/// An open of `guest_dir/<name>` for reading, by any guest thread from its
/// first system call, opens `copy_dir/<name>` instead when that copy exists. A
/// write open, or a name with no copy, is left as it is.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PropertySnapshot {
    /// The directory the guest names: `/dev/__properties__`.
    pub guest_dir: PathBuf,
    /// The directory holding the copies, one per file of `guest_dir` Hermit
    /// could read, made before the guest's first exec.
    pub copy_dir: PathBuf,
    /// Each live file copied: its host device and inode, by which an open
    /// that reaches it by any spelling (a symbolic link, a bind mount, a
    /// descriptor link) is recognized, and its name in `copy_dir`.
    #[serde(default)]
    pub live_files: Vec<PropertyFile>,
    /// Each live file the capture saw but could not copy, because it stopped
    /// being readable mid-capture. It has no copy, so an open that reaches it
    /// by any spelling reads the live area, which is refused under the
    /// default fail-closed policy.
    #[serde(default)]
    pub uncopied_files: Vec<PropertyFile>,
    /// Whether the copies are one consistent cut: `properties_serial` read
    /// the same before and after they were taken. Otherwise (properties kept
    /// changing, or the serial could not be read) each file is coherent on
    /// its own, but two files may come from different moments. Shown in the
    /// run summary; not stored in preemption or schedule recordings.
    #[serde(default)]
    pub consistent_cut: bool,
}

impl PropertySnapshot {
    /// The live file the snapshot copied with host device `dev` and inode
    /// `ino`, if any.
    pub fn live_file(&self, dev: u64, ino: u64) -> Option<&PropertyFile> {
        self.live_files
            .iter()
            .find(|file| (file.dev, file.ino) == (dev, ino))
    }

    /// The live property file with host device `dev` and inode `ino` the
    /// capture saw, copied or not: reading it reads the live area. Matching
    /// is by identity alone, here and in [`Self::live_file`]: if a captured
    /// file is deleted during the run and its inode reused by an unrelated
    /// file, a pure read of that file is falsely redirected to the copy (a
    /// copied identity) or treated as the live area and refused in a
    /// fail-closed run (an uncopied one). Either way no live property read
    /// is let through.
    pub fn known_live_file(&self, dev: u64, ino: u64) -> Option<&PropertyFile> {
        self.live_file(dev, ino).or_else(|| {
            self.uncopied_files
                .iter()
                .find(|file| (file.dev, file.ino) == (dev, ino))
        })
    }
}

/// One live property file a [`PropertySnapshot`] copied.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PropertyFile {
    /// The live file's host `st_dev`.
    pub dev: u64,
    /// The live file's host `st_ino`.
    pub ino: u64,
    /// Its name, in the guest directory and in the copy directory.
    pub name: String,
}

/// Configuration options for detcore.
#[derive(Debug, Serialize, Deserialize, Clone, Parser)]
pub struct Config {
    /// Disable virtual/logical time. Note that virtual time is required for virtual metadata.
    #[clap(long = "no-virtualize-time", action = clap::ArgAction::SetFalse)]
    pub virtualize_time: bool,

    /// Disable virtual cpuid
    #[clap(long = "no-virtualize-cpuid", action = clap::ArgAction::SetFalse)]
    pub virtualize_cpuid: bool,

    /// The CPU the guest sees: virtual x86-64-v2/v3 or intercepted host
    /// features without hardware randomness. The long help states each
    /// profile's host requirements and determinism limits. Like
    /// HERMIT_EPOCH, HERMIT_CPU_PROFILE supplies the value when the flag is
    /// absent; an explicit flag wins, and a value that names no profile is
    /// refused. Unset means auto, for the command line and
    /// [`Config::default`] alike: `hermit run` and `hermit record start`
    /// resolve it to a named profile before launch, and the library's launch
    /// functions resolve it from the program they launch. A serialized
    /// configuration or recording without the field still means x86-64-v2
    /// (`CpuProfile`'s own default), never auto. Admission is the same either
    /// way.
    #[clap(long, value_enum, default_value = "auto", env = "HERMIT_CPU_PROFILE", long_help = crate::cpu_profile::CPU_PROFILE_LONG_HELP)]
    #[serde(default)]
    pub cpu_profile: crate::cpu_profile::CpuProfile,

    /// aarch64: rewrite the canonical load/store-exclusive retry loops of
    /// every executable file mapping in the guest's memory, as each is
    /// mapped. Set by the CLI for `--rewrite-exclusives` in a run that has no
    /// mount namespace to carry rewritten files; never read from a recording.
    #[clap(skip)]
    #[serde(default)]
    pub rewrite_exclusives_in_memory: bool,

    /// aarch64: count the store-exclusives the guest executes and fails, as
    /// a run that records or replays preemptions does. Set by the CLI for
    /// each run of `--verify` and `--verify-run`, so that a divergence a
    /// retried load/store-exclusive loop explains can be named; never read
    /// from a recording.
    #[clap(skip)]
    #[serde(default)]
    pub count_store_exclusives: bool,

    /// aarch64: the guest paths `--rewrite-exclusives` rewrote into
    /// bind-mounted copies, or found nothing to rewrite in: the executable
    /// and each `--rewrite-exclusives-also` file. Set by the CLI for a run
    /// that rewrites with that overlay, empty otherwise, so that the run can
    /// name the other mapped files whose load/store-exclusive loops it left;
    /// never read from a recording.
    #[clap(skip)]
    #[serde(default)]
    pub rewrite_exclusives_overlay: Vec<PathBuf>,

    /// aarch64: whether `--rewrite-exclusives` rewrites by overlaying
    /// rewritten files, as the run's flags request. Set by the CLI from
    /// the flags alone, so early replay admission can state the run's
    /// rewrite provenance while `rewrite_exclusives_overlay` -- the
    /// files the overlay was given, known only once they are prepared
    /// -- is still empty; never read from a recording.
    #[clap(skip)]
    #[serde(default)]
    pub rewrite_exclusives_overlay_requested: bool,

    /// The host facts a v3 guest's leaf 0xD and xstate answers come from, set
    /// by the CLI after the host qualifies; `None` under v2. Neither profile
    /// field enters the legacy form the DBT runtime reads (DBT refuses v3).
    #[clap(skip)]
    #[serde(default)]
    pub cpu_profile_host: Option<crate::cpu_profile::HostXstate>,

    /// Native CPUID source for host-no-rdrand, selected during admission from
    /// the caller's original allowed CPU set, before container placement.
    /// The ptrace launch pins its actual tracer to this CPU. Never restored
    /// from recording metadata: host-profile recording/replay is refused.
    #[clap(skip)]
    #[serde(default)]
    pub cpu_profile_source: Option<usize>,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-845): Review in-process backend descriptor discovery.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-845): Review host-clock futex deadline detection.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-845): Review backend-owned syscall-clobber determinism.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-845): Review backend-local exit-group RPC cancellation.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1058): Review process-signal identity translation.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1125): Review backend-owned capability-control prctls.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1152): Review deferred vfork child registration.
    /// How the execution backend runs the guest, as the backend reports it
    /// through Reverie (`reverie::Backend::capabilities`).
    ///
    /// Detcore reads facts about the backend only from here, never from the
    /// backend's name, so a new backend is modelled correctly by reporting its
    /// capabilities. The host copies the running backend's answer into this
    /// field before the run starts. The default,
    /// [`DEFAULT_BACKEND_CAPABILITIES`], describes a ptrace-hosted guest, the
    /// backend Detcore's own tests use, with the three host-set facts and
    /// `reports_sigreturns` off.
    ///
    /// JSON that the guest can see is written by [`to_legacy_backend_json`],
    /// which carries this field as the separate keys used before it existed;
    /// [`from_legacy_backend_json`] reads them back.
    #[serde(default = "default_backend_capabilities")]
    #[clap(skip = DEFAULT_BACKEND_CAPABILITIES)]
    pub backend: BackendCapabilities,

    /// The RCB clock the host prepared this run for
    /// (`hermit_cli::prepare_backend_config`). Detcore checks it against the
    /// clock it actually finds before it records or replays a history, and
    /// never trusts it alone. It is not serialized: a configuration read from
    /// JSON or bincode has none, and its histories are unqualified.
    #[serde(skip)]
    #[clap(skip)]
    pub rcb_clock_claim: RcbClockClaim,

    /// The guest may start with a terminal: one of the launcher's standard
    /// descriptors is a terminal, the launcher has a controlling terminal that
    /// the guest inherits with its session, or the launcher could not tell.
    /// Such a terminal makes Linux send signals at moments the host sets:
    /// SIGHUP and SIGCONT when it hangs up, SIGINT, SIGQUIT and SIGTSTP for its
    /// interrupt, quit and suspend characters, SIGWINCH when it is resized, and
    /// SIGTTIN and SIGTTOU for a background process group's reads and writes.
    /// The guest makes no traced call that arms them, so where Detcore treats
    /// such signals as reaching the guest from the host
    /// (`BackendCapabilities::host_timed_signals_are_physical`)
    /// the scheduler records all eight as host-timed for every process before the
    /// guest's first instruction, and no gated wait ends for them
    /// (<https://github.com/rrnewton/hermit/issues/3146>).
    ///
    /// The host sets it while it prepares the backend configuration. It has no
    /// legacy key: [`to_legacy_backend_json`] leaves it out and
    /// [`from_legacy_backend_json`] reads it back as false.
    #[serde(default)]
    #[clap(skip)]
    pub guest_may_inherit_a_terminal: bool,

    /// The forwarding policy (`detcore::detlog::ForwardPolicy::encode`) every
    /// in-guest Tool must forward its DETLOG records with, on the socket Hermit
    /// passed it (`hermit run --verify` under SaBRe or in-guest LiteInst);
    /// `None` when no socket is passed.
    ///
    /// The host sets it, not a flag, and every Tool image receives it in the
    /// configuration handshake on its coordinator connection, which guest code
    /// cannot change. A Tool image whose forwarder is missing, or forwards by
    /// another policy (guest code that ran first changed its private
    /// forwarding variables, so some records would be dropped before they are
    /// counted), then reports every forwarded-record count as UNCOUNTED
    /// (`detcore::detlog::require_forwarding`), so verification refuses the run
    /// instead of accepting a log without those records. Like
    /// `guest_may_inherit_a_terminal`, it has no legacy key.
    #[serde(default)]
    #[clap(skip)]
    pub in_guest_detlog_forward_policy: Option<String>,

    /// Whether every in-guest Tool image must run with syscall site patching
    /// off and collect its exit statistics (`hermit --backend=in-guest-trap`).
    ///
    /// The host sets it, not a flag, and every Tool image receives it in the
    /// configuration handshake on its coordinator connection, which guest code
    /// cannot change. The in-guest LiteInst runtime takes both settings from
    /// the guest environment, which guest code running before or during its
    /// installation could change, so once installation returns a Tool image
    /// compares the settings the runtime actually captured with this one and
    /// refuses to run the program when they differ
    /// (`detcore::in_guest_site_patching`). Like
    /// `in_guest_detlog_forward_policy`, it has no legacy key.
    #[serde(default)]
    #[clap(skip)]
    pub in_guest_site_patching_off: bool,

    /// The signals an in-guest runtime keeps for itself, which a guest may
    /// not send to another process or arm as a notification signal: bit
    /// `n - 1` for signal `n`, as Detcore's other signal sets are kept.
    ///
    /// Such a signal sent from one process to another would run the target's
    /// runtime handler on a thread that does not hold the scheduler's turn
    /// (A5 step 3c, design section 16.30,
    /// <https://github.com/rrnewton/hermit/issues/4485>). So Detcore answers a
    /// send of one of these signals to a process other than the caller's own
    /// (`kill`, `tkill`, `tgkill`, `rt_sigqueueinfo`, `rt_tgsigqueueinfo`,
    /// `pidfd_send_signal`), and any arming of one (`fcntl(F_SETSIG)`,
    /// `timer_create`, `prctl(PR_SET_PDEATHSIG)`), with `EPERM` and a WARN
    /// ([`Config::reserves_in_guest_signal`]). A send to the caller's own
    /// process is unchanged. Empty (0), the default, changes nothing.
    ///
    /// The host sets it, not a flag, when it prepares an in-guest run on the
    /// default route: SIGSYS, SIGSEGV while the runtime subscribes
    /// instructions, and SIGTRAP while LiteInst's guard router may be
    /// installed: on `--backend=liteinst`, never with
    /// `in_guest_site_patching_off` (a liteinst image whose environment turns
    /// patching off has no router but keeps SIGTRAP reserved, a known
    /// over-refusal).
    /// Every other backend, and the in-guest loader route, leave it empty.
    /// Every Tool image receives it in the configuration handshake, which
    /// guest code cannot change. Like `in_guest_site_patching_off`, it has no
    /// legacy key.
    #[serde(default)]
    #[clap(skip)]
    pub in_guest_reserved_signals: u64,

    /// Whether Detcore refuses the guest calls that would let the kernel send
    /// a guest process a signal at a moment host timing decides (A5 step 3c,
    /// slice 0b; design sections 16.30, D28 and D28a,
    /// <https://github.com/rrnewton/hermit/issues/4485>).
    ///
    /// While the in-guest runtime runs its own code, ordinary signals are
    /// blocked, and Linux keeps a blocked signal pending even under
    /// `SIG_IGN`, so such a signal could become pending at a host-timed
    /// moment whatever the guest's action. So where this is set, Detcore
    /// answers with `EPERM` and a WARN the operation that completes such a
    /// producer: an owner for a socket or for a descriptor with `O_ASYNC`
    /// (`F_SETOWN`, `F_SETOWN_EX`, `FIOSETOWN`, `SIOCSPGRP`), `O_ASYNC` for a
    /// descriptor with an owner (`F_SETFL`, `FIOASYNC`), a lease
    /// (`F_SETLEASE`) and a directory notification (`F_NOTIFY`). It holds a
    /// parent-death signal it can send itself, as it does on ptrace, and
    /// refuses the run by name (`pdeathsig-would-fire`) if one would fire; it
    /// refuses with `EPERM` one it cannot hold. A SIGKILL parent-death signal
    /// keeps the kernel's path. False, the default, changes nothing.
    ///
    /// The host sets it, not a flag, when it prepares an in-guest run on the
    /// default route, where it also clears the physical `RLIMIT_CPU` soft
    /// limit and refuses an inherited descriptor that is already such a
    /// producer (D25). Every other backend, and the in-guest loader route,
    /// leave it false. Every Tool image receives it in the configuration
    /// handshake, which guest code cannot change. Like
    /// `in_guest_site_patching_off`, it has no legacy key.
    #[serde(default)]
    #[clap(skip)]
    pub in_guest_refuses_host_timed_producers: bool,

    /// The guest's first image, as its `/proc/self/exe` link names it, and
    /// the image the kernel exec'd in its place where a launcher loads the
    /// guest ([`LaunchImage`]). Detcore answers that link from it until the
    /// guest's next exec, which Detcore resolves itself. `None` leaves the
    /// first image's link to the kernel.
    ///
    /// The host sets it, not a flag, from the program it launches. Like
    /// `in_guest_detlog_forward_policy` it has no legacy key, so DBT's
    /// legacy form never carries it.
    #[serde(default)]
    #[clap(skip)]
    pub launch_image: Option<LaunchImage>,

    /// The mount IDs and filesystem types of the mount namespace hermit-cli
    /// was launched in, read in its outer process before it creates its
    /// container ([`crate::host_input::LaunchNamespaceMounts`]). A descriptor
    /// the guest inherits from outside the container (the user's redirected
    /// stdio, `/dev/null`, a `--verify` stdin snapshot) names one of these
    /// mounts in its `fdinfo`, which the container's own mountinfo does not
    /// list, so Detcore reads the guest's standard descriptor kinds with it
    /// (`crate::host_input::StdioKind::of_guest_descriptor`). Empty for a
    /// library caller, whose descriptors' mounts are in its own namespace.
    ///
    /// The host sets it, not a flag. In-process only, as `happens_before`
    /// is: never serialized, so neither a recording nor DBT's legacy form
    /// carries it, and a replay never reads another host's mount IDs.
    #[serde(skip)]
    #[clap(skip)]
    pub launch_namespace_mounts: crate::host_input::LaunchNamespaceMounts,

    /// aarch64: the files the run's `--rewrite-exclusives` left unchanged,
    /// wholly or in part, because no mapping symbol it accepts marks their
    /// code: none, or one that is neither `$x` nor `$d`. The CLI sets it; the
    /// run names them in its summary.
    #[serde(default)]
    #[clap(skip)]
    pub rewrite_exclusives_unmarked: Vec<std::path::PathBuf>,

    /// The seccomp state the guest inherits from the thread that starts it
    /// ([`InheritedSeccomp`]). The caller that starts the guest states it, as
    /// only the caller knows the filters its launch installs, such as those of
    /// a `pre_exec` callback; `hermit run` measures it. Not stated, it is
    /// unknown. Nothing reads it. Whether a guest may start under an inherited
    /// filter is decided by the `hermit` command, before it starts the guest,
    /// from its own reading of `/proc/self/status` (`host_seccomp::admit`).
    /// It has no legacy key:
    /// [`to_legacy_backend_json`] leaves it out and
    /// [`from_legacy_backend_json`] reads it back as unknown.
    #[serde(default)]
    #[clap(skip)]
    pub inherited_seccomp: InheritedSeccomp,

    /// Epoch of the logical time.
    ///
    /// This is the datetime from which all time and date modtimes begin and
    /// monotonically increase. It is in RFC3339 format such as `2026-01-01T00:00:00Z`.
    /// The stable default here is for library callers and wire-format fixtures;
    /// the `hermit run` and `hermit oci run` commands replace an omitted CLI
    /// default with one host wall-clock sample taken before backend dispatch.
    ///
    /// A guest that checks TLS certificates over the host network (`--network=host` or
    /// `--record-networking`) checks them against this clock, so an epoch outside a server
    /// certificate's validity fails the HTTPS connections to that server: curl exits 60 with "SSL
    /// certificate problem: certificate is not yet valid" (or "certificate has expired"), and a
    /// libcurl caller sees "SSL peer certificate or SSH remote key was not OK". For such a run omit
    /// `--epoch`: `hermit run` then starts the clock at the current time and saves it in the run's
    /// manifest, and `hermit run --from last-run` repeats the run with that epoch.
    #[clap(
        long,
        env = "HERMIT_EPOCH",
        value_name = "YYYY-MM-DDThh:mm:ssZ",
        default_value = DEFAULT_EPOCH_STR,
        hide_default_value = true
    )]
    pub epoch: DateTime<Utc>,

    /// Use this number to seed the PRNG randomness for both RNG and scheduler.
    /// This acts as a global fallback in case either `sched_seed` or `rng-seed`
    /// are not explicitly specified
    #[clap(
        long = "seed",
        env = "HERMIT_PRNG",
        default_value = "0",
        value_name = "uint64"
    )]
    pub seed: u64,

    /// Use this number to seed the PRNG that supplies randomness to the guest.
    /// This supplies guest system calls that expose randomness, as well as
    /// the `/dev/[u]random` files. It does not affect the `rdrand` instruction,
    /// which is disabled in the guest.
    #[clap(long, value_name = "uint64")]
    pub rng_seed: Option<u64>,

    /// Seeds the PRNG which drives syscall response fuzzing (i.e. chaotically exercising syscall
    /// nondeterminism).  Like other seeds, this is initialized from the `--seed` if not
    /// specifically provided.
    #[clap(long, value_name = "uint64")]
    pub fuzz_seed: Option<u64>,

    /// Logical clock multiplier. Values above one make time appear to go faster within the sandbox.
    #[clap(long, value_name = "float")]
    pub clock_multiplier: Option<f64>,

    /// Disable substitution of virtual (deterministic) file metadata in lieu
    /// of the real metadata returned by `stat`/`statx`. This also preserves raw
    /// mountinfo device numbers so those interfaces continue to agree. Raw
    /// device values are host/filesystem observations and are not promised to
    /// reproduce across machines. Virtual metadata implies `virtualize_time`.
    #[clap(long = "no-virtualize-metadata", action = clap::ArgAction::SetFalse)]
    pub virtualize_metadata: bool,

    /// Proven Hermit-owned mount roots to hide from `/proc/*/mountinfo`.
    ///
    /// This is runtime provenance, not a user option.  `serde(default)` keeps
    /// older serialized configurations compatible and makes backends which do
    /// not use the common container setup explicitly receive no rewrite claim.
    #[serde(default)]
    #[clap(skip)]
    pub mountinfo_root_rewrites: Vec<MountInfoRootRewrite>,

    /// Backend-proven pairs of (`mountinfo` raw device, `stat`/`statx` raw device).
    ///
    /// A backend may synthesize mountinfo independently from its pathname
    /// metadata implementation.  These pairs state that the two raw numbers
    /// describe the same filesystem, so Detcore can feed both surfaces through
    /// one device identity.  The pairs are runtime provenance, not a user
    /// option; an absent pair must never be inferred from numeric coincidence.
    #[serde(default)]
    #[clap(skip)]
    pub mountinfo_device_rewrites: Vec<(u64, u64)>,

    /// Recording/container namespace mount IDs in canonical row order.
    ///
    /// Detcore validates every `/proc/*/mountinfo` view against this order.
    /// It does not number mount IDs by position in it; see
    /// `mount_id_assignment_order`. It is runtime provenance rather than a
    /// user option; replay retains recording-time raw IDs because its read
    /// events contain recording-time kernel bytes.
    #[serde(default)]
    #[clap(skip)]
    pub mountinfo_mount_ids: Vec<u64>,

    /// Whether `mountinfo_mount_ids` is an exact producer-owned snapshot.
    ///
    /// The distinction matters for an empty mountinfo file: an absent snapshot
    /// asks Detcore to observe the completed guest namespace, while a captured
    /// empty snapshot must remain empty during replay.
    #[serde(default)]
    #[clap(skip)]
    pub mountinfo_mount_ids_captured: bool,

    /// The mount namespace `mountinfo_mount_ids` was captured in
    /// ([`MountNamespaceIdentity`]). A backend that checks its provenance
    /// (KVM) requires its own namespace to be this one, instead of
    /// recapturing the namespace's rows: a host can detach a copied host mount
    /// from a private namespace at any time by removing that mount's
    /// directory, so two captures of one namespace need not agree. `None`
    /// when nothing was captured, and in recordings. Runtime provenance, not a
    /// user option.
    #[serde(default)]
    #[clap(skip)]
    pub mountinfo_mount_namespace: Option<MountNamespaceIdentity>,

    /// Raw mount IDs in the order Detcore numbered them for the guest.
    ///
    /// Detcore numbers a raw mount ID when the guest first observes it through
    /// `/proc/*/mountinfo` or `/proc/*/fdinfo/*`; `mountinfo_mount_ids` only
    /// validates mountinfo views. A live run starts empty. Recording persists
    /// the completed order so replay gives every raw ID its recorded number
    /// rather than deriving one from its fresh namespace or launch descriptor
    /// shape.
    #[serde(default)]
    #[clap(skip)]
    pub mount_id_assignment_order: Vec<u64>,

    /// The guest's container mount table, which Detcore renders as every
    /// spelling of `/proc/<pid>/mountinfo` and `/proc/<pid>/mounts` on every
    /// backend (`crate::container_mounts`,
    /// <https://github.com/rrnewton/hermit/issues/4213>). hermit-cli builds it
    /// once, in the completed container namespace before the guest starts,
    /// or, for `--no-namespace` and a library caller on KVM that built no
    /// container, in the namespace the guest runs in (its launch mounts and
    /// base mountpoints); a recording carries it, so its replay renders the
    /// recorded table. `None` (a library caller on a backend other than KVM,
    /// whose command may chroot or mount where Hermit cannot see, and every
    /// older configuration) leaves both files to the kernel, sanitized as
    /// before. Runtime provenance, not a user option.
    #[serde(default)]
    #[clap(skip)]
    pub container_mount_table: Option<crate::container_mounts::ContainerMountTable>,

    /// The guest path of the empty, read-only directory hermit-cli mounted in
    /// the guest's container for Detcore's placeholder directories where the
    /// guest's procfs is not the kernel's (KVM: the backend provides process
    /// signal control, `procfs_inode::tool_can_name_guest_descriptors`): a
    /// tmpfs at `/proc/fs/nfsd`, procfs's own permanently empty mount point,
    /// which Detcore's `--proc` level table hides from the guest at every
    /// level, and which the guest's mount table leaves out. Each descriptor
    /// Detcore serves there for `/proc` itself or the caller's own
    /// `/proc/<pid>/fd` is open on this directory, and Detcore answers every
    /// call through it from its model; an open, stat or statx of the guest's
    /// that reaches the directory otherwise fails as `/proc/fs` does. `None`
    /// where the guest's procfs is the kernel's (ptrace, the in-guest
    /// backends: nothing is mounted, see [`Self::container_procfs`]), and
    /// where hermit-cli mounted none or a later mount covers it:
    /// `--no-namespace`, `--namespace-only`, record and replay, a user mount
    /// over the placeholder, every library caller, and every older
    /// configuration. On KVM, Detcore then serves no placeholder directory,
    /// and an open of `/proc` or a descriptor directory is refused (`EACCES`).
    /// Runtime provenance, not a user option.
    #[serde(default)]
    #[clap(skip)]
    pub directory_placeholder: Option<PathBuf>,

    /// Whether the guest runs in hermit-cli's container, whose procfs mount
    /// at `/proc` is its own (every container hermit-cli runs a guest in, not
    /// `--no-namespace`, not a library caller, not an older configuration).
    /// Where the guest's procfs is the kernel's (ptrace), Detcore serves
    /// `/proc` itself and the caller's own `/proc/<pid>/fd` then, each on a
    /// descriptor on that procfs's root, and serves neither without it.
    /// Runtime provenance, not a user option.
    #[serde(default)]
    #[clap(skip)]
    pub container_procfs: bool,

    /// Sequentialize thread execution deterministically.
    #[clap(long)]
    pub sequentialize_threads: bool,

    /// Choose which side of an ordinary fork/clone runs first after the child is registered.
    /// Random choices are deterministic under `--sched-seed`.
    #[serde(default)]
    #[clap(long, default_value = "child", value_name = "child|parent|random")]
    pub runs_post_fork: RunsPostFork,

    /// Use the optimized partial syscall subscription set instead of intercepting every syscall.
    /// This permits unlisted syscalls to bypass Detcore and therefore weakens deterministic
    /// accounting; leave it disabled for fail-closed execution.
    #[serde(default)]
    #[clap(long)]
    pub passthru_opt: bool,

    /// Leave to the kernel, without a stop, the few syscalls that Detcore always passes
    /// through unchanged and that never wait for another thread: getppid, umask, getgroups,
    /// capget, sched_get_priority_max, sched_get_priority_min and get_robust_list. Each such
    /// call then costs no tracer round trip, but it also charges no virtual time, logs no
    /// DETLOG line, is not counted in the run summary, and is not a point where the thread's
    /// timeslice can end, so the run's virtual clock, schedule and log differ from a run
    /// without this flag, the same in every run with it. Off by default; refused under
    /// --strict, which promises that Detcore observes every syscall, without RCB time, and
    /// with a preemption record to write or replay, whose callback history counts these calls.
    #[serde(default)]
    #[clap(long)]
    pub untrap_passthrough_syscalls: bool,

    /// In chaos mode, uses much cheaper approximate preemption timers.  Only makes sense
    /// when recording preemptions for later (precise) replay.
    #[clap(long)]
    pub imprecise_timers: bool,

    /// Stop the guest at every timeslice end, including one where no other
    /// guest thread could run. By default Hermit does not stop a thread that
    /// is the only one able to run before it next enters Detcore (every other
    /// thread waits on a futex without a timeout, on a child or in
    /// `rt_sigsuspend`; no timer, sleeper, poll deadline or host call is
    /// pending): it processes those timeslice ends when the thread next
    /// enters Detcore, at the same branch counts and in the same order, so the
    /// schedule, the virtual clock and the chaos draws are the same either
    /// way and only the stops are saved. This option turns that off, for
    /// comparison.
    #[serde(default)]
    #[clap(long)]
    pub eager_lone_preemptions: bool,

    /// Turn off Detcore's own copy of each guest thread's signal dispositions
    /// and masks (A5, docs/SIGNAL_STATE_CHECK.md). The copy is on by default
    /// where it can run (ptrace with serialized threads): it is checked
    /// against the kernel, and decisions A5 converted read it in place of the
    /// kernel's `/proc` view. With it off, those decisions read `/proc`, and
    /// no handler return is reported. A recorded schedule states the setting
    /// it ran under, and replaying it under the other setting is refused.
    #[serde(default = "signal_state_copy_default")]
    #[clap(long = "no-signal-state-copy", action = clap::ArgAction::SetFalse)]
    pub signal_state_copy: bool,

    /// Whether a signal delivery that the kernel contradicts, where Detcore's
    /// copy and the kernel are both certain (A5 step 3.2), ends the run as an
    /// internal failure (`HERMIT_INTERNAL_FAILURE class=signal-delivery-check`)
    /// rather than being logged as a divergence that drops the copy. It is a
    /// Detcore defect either way. `hermit run --strict` sets it; nothing else
    /// does, and it changes nothing while the copy matches the kernel.
    ///
    /// The host sets it. It has no legacy key: [`to_legacy_backend_json`]
    /// leaves it out and [`from_legacy_backend_json`] reads it back as false.
    #[serde(default)]
    #[clap(skip)]
    pub signal_delivery_check_fatal: bool,

    /// `--strict`'s binder policy: the run never holds a binder descriptor,
    /// in a fresh run and in a replay alike, as binder reply payloads are not
    /// recorded (<https://github.com/rrnewton/hermit/issues/5124>). One, or
    /// a descriptor whose identity cannot be read, is refused where it
    /// arrives (an open, a receipt, the run's start), and every binder
    /// request on a descriptor that may be one is refused.
    /// `hermit run --strict` sets it; nothing else does.
    ///
    /// The host sets it. It has no legacy key: [`to_legacy_backend_json`]
    /// leaves it out and [`from_legacy_backend_json`] reads it back as false.
    #[serde(default)]
    #[clap(skip)]
    pub strict_binder: bool,

    /// Schedule threads chaotically.
    ///
    /// The behavior of this flag is subject to change. Current behavior is to randomize thread
    /// priorities at every logical timeslice. Other randomization strategies are possible with
    /// `--sched-heuristic`.
    ///
    /// Thread scheduling remains deterministic, determined by the random seed.
    #[clap(long, long_help = chaos_long_help())]
    pub chaos: bool,

    /// Draw the mean of each chaos slice from MIN..MAX virtual nanoseconds (both included),
    /// log-uniformly, instead of using `--max-timeslice` as every slice's mean, and cap every
    /// slice at MAX. Slices stay exponentially distributed around the drawn mean, so some fall
    /// below MIN. Races differ in the slice scale they need: some
    /// need a preemption in a short window, others a long uninterrupted run of one thread, and a
    /// seed sweep under one timeslice explores only one scale. Under a range every run mixes all
    /// the scales in it. The draws come from the scheduler seed's chaos stream, so a seed
    /// reproduces them, and a preemption record pins every slice end as usual. Sets the run's
    /// `--max-timeslice` to MAX: requires `--chaos` and PMU-backed preemption, and conflicts
    /// with an explicit `--max-timeslice`, with `--target-timeslice` and with `hermit run
    /// --chaos-seeded-timeslice`. A small MIN makes runs slower. Example: `1000..200000000`.
    #[serde(default)]
    #[clap(
        long,
        value_name = "MIN..MAX",
        requires = "chaos",
        conflicts_with = "target_timeslice"
    )]
    pub chaos_timeslice_range: Option<TimesliceRange>,

    /// How `--chaos-timeslice-range` draws. `slice` (the default) draws each slice's mean from
    /// the range. `run` instead draws the run's `--max-timeslice` once, log-uniformly from the
    /// range as a pure function of the scheduler seed, and the run is then an ordinary chaos run
    /// under it: a sweep's seeds spread over the scales of the range, each seed at one scale.
    /// That reaches a race needing one scale throughout in more seeds, at a higher cost per run
    /// for the fine draws. The drawn value is printed in `--summary-json`
    /// (`preemption.max_timeslice_ns`) and stored in a `--record-preemptions-to` record, from
    /// which a replay takes it. Without an explicit range, `run` uses
    /// [`RECOMMENDED_CHAOS_TIMESLICE_RANGE`]; `slice` still requires a range.
    #[serde(default)]
    #[clap(
        long,
        value_enum,
        default_value_t,
        requires = "chaos",
        requires_if("slice", "chaos_timeslice_range"),
        conflicts_with = "target_timeslice",
        long_help = chaos_timeslice_draw_long_help()
    )]
    pub chaos_timeslice_draw: ChaosTimesliceDraw,

    /// The `--max-timeslice` this run drew under `--chaos-timeslice-draw=run`, or took from the
    /// preemption record it replays. `hermit run` sets it, and `max_timeslice` to the same
    /// value, before the run starts; the run's own preemption record stores it. When set, no
    /// slice draws its mean from `--chaos-timeslice-range`.
    #[serde(default)]
    #[clap(skip)]
    pub chaos_timeslice: Option<NonZeroU64>,

    /// Uses the `--fuzz-seed` to generate randomness and fuzz nondeterminism in the futex semantics.
    #[clap(long)]
    pub fuzz_futexes: bool,

    /// Targeted chaos: bias scheduling toward known concurrency race patterns
    /// instead of exploring interleavings uniformly. At the scheduler's existing
    /// nondeterminism points it uses `--fuzz-seed` to (a) deliver a
    /// process-directed signal to a randomly chosen thread in the group (signal
    /// timing races) and (b) randomize the requeue position of a force-unblocked
    /// thread (lock-ordering / wakeup races). With `--chaos`, outside a replay,
    /// it also (c) starts a new thread other than a vfork child at the first or
    /// last priority instead of a random one (fork and exec races); a vfork
    /// child keeps its random priority. It is meant for `--chaos` runs,
    /// but (a) and (b) apply whenever it is set, with or without `--chaos`;
    /// like the rest of chaos mode it remains reproducible under a fixed seed.
    #[clap(long)]
    pub chaos_target_races: bool,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1149)
    // TODO-HUMAN-REVIEW(PR-1151)
    /// Reproducible per-thread slowdown factors for chaos mode. A factor greater
    /// than one makes each RCB consume proportionally more virtual time, while a
    /// factor below one makes it consume less. Thus scheduling deadlines and the
    /// guest-visible virtual clock describe the same slowed execution rather than
    /// applying an out-of-band scheduling bias. The factor is a pure function of
    /// scheduler seed, stable deterministic thread id, and chaos epoch. A fixed
    /// seed therefore reproduces both timing and interleavings.
    #[clap(long)]
    pub chaos_per_thread_slowdown: bool,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1149)
    // TODO-HUMAN-REVIEW(PR-1151)
    /// Maximum ratio between the slowest and fastest per-thread slowdown factor
    /// for `--chaos-per-thread-slowdown`. Each thread's factor is drawn
    /// log-uniformly from `[1/R, R]` where `R` is this value. Must fit the Q32
    /// virtual-time representation and be `>= 1.0`; `1.0` disables the spread.
    #[clap(long, default_value = "10.0", value_name = "double")]
    pub chaos_slowdown_max_factor: f64,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1151)
    /// Length of a deterministic slowdown epoch in elapsed per-thread logical
    /// nanoseconds. At the first scheduler commit at or after each boundary the
    /// factor is redrawn as `factor(seed, stable_dettid, epoch)`. This is never
    /// wall time. `0` means one epoch for the entire run, making constant slowdown
    /// the single-epoch special case. Recorded preemption artifacts carry exact
    /// epoch transitions and factors for replay. Inert without chaos slowdown.
    #[clap(long, default_value = "0", value_name = "nanos")]
    pub chaos_epoch_length_ns: u64,

    /// Let a chaos preemption land up to this many instructions past the
    /// conditional branch its timeslice ends on. `0` (the default) is off.
    ///
    /// A chaos timeslice ends after a retired conditional branch, so a race
    /// window with no conditional branch in it is never split. With this set,
    /// each timeslice that ends on its timer is delivered `k` single steps
    /// past that branch instead, `k` drawn uniformly from `0..=N` for each
    /// timeslice from the seed, so a preemption can land inside straight-line
    /// code. A preemption record keeps each `k` and its branch count, and its
    /// replay stops at the same instruction. Each step is one ptrace stop, so
    /// a preemption costs up to `N` more. Needs `--chaos` and the ptrace
    /// backend's precise timers on x86_64; at most 256.
    #[serde(default)]
    #[clap(long, default_value = "0", value_name = "instructions")]
    pub chaos_instruction_offset: u64,

    /// Record the timing of preemption events for future replay or experimentation.
    /// This is only useful in chaos modes.
    #[clap(long)]
    pub record_preemptions: bool,

    /// Record and replay the complete slice-boundary protocol used by strict
    /// trace verification. This mode refuses mismatches, extra observations,
    /// incomplete replay and legacy schedule files; it does not resynchronize.
    /// Ordinary recording/replay and schedule editing retain their legacy mode.
    #[serde(default)]
    #[clap(long)]
    pub strict_schedule_boundaries: bool,

    /// File to write the record of preemptions (in JSON).  Implies `--record-preemptions`.
    #[clap(long, value_name = "filepath")]
    pub record_preemptions_to: Option<PathBuf>,

    /// JSON file to read recorded preemptions from.  When `--chaos` mode is activated, these
    /// recorded preemption points take the place of randomized scheduling decisions.
    #[clap(long, value_name = "filepath", conflicts_with = "replay_schedule_from")]
    pub replay_preemptions_from: Option<PathBuf>,

    /// File to read recorded schedule trace from. This execution will replay the schedule verbatim
    /// from the file.
    #[clap(
        long,
        value_name = "filepath",
        conflicts_with = "replay_preemptions_from"
    )]
    pub replay_schedule_from: Option<PathBuf>,

    /// If we run out of events while replaying a schedule, treat that as a fatal event and panic,
    /// rather than continuing execution.
    #[clap(long)]
    pub replay_exhausted_panic: bool,

    /// Replay a schedule or preemption record leniently: count desyncs and
    /// continue instead of stopping at the first one (strict by default,
    /// dh-mvz7y). Internal only: set by the analyze, bisect and minimize
    /// entry points, whose replay is best-effort; there is no user flag.
    #[serde(default)]
    #[clap(skip)]
    pub lenient_replay: bool,

    /// Retired: strict replay is the default (dh-mvz7y), so this is a no-op
    /// kept only so scripts still passing it do not break; it warns once.
    #[clap(long = "die-on-desync", hide = true)]
    pub die_on_desync: bool,

    /// The preemption record `--replay-preemptions-from` reads is not the recording of this
    /// run's schedule but an edited one, for example one `hermit analyze` knocked preemptions
    /// out of. A thread then can reach recorded slices that have already ended, which a faithful
    /// replay never does: the replay skips them and goes on with the first recorded slice still
    /// ahead, at its priority, instead of stopping the run. (At a thread's start the record's
    /// first priority, which the scheduler has already applied, holds until that slice ends.)
    #[serde(default)]
    #[clap(long, requires = "replay_preemptions_from")]
    pub replay_edited_preemptions: bool,
    /// Once the schedule replayed from `--replay-schedule-from` runs out,
    /// order the threads queued in turn at each priority by the virtual time
    /// each has run since then, least first, and equal run times by least
    /// recent selection, instead of in queue order. A thread's run time is
    /// what its own clock has advanced since that point; a thread seen for
    /// the first time, or runnable again after it was not, starts at no less
    /// than the least run time of the runnable threads. That is all it
    /// changes: a selected thread still runs until its slice ends, there is no
    /// preemption on wakeup, and threads pushed to the front of the queue (a
    /// timed wake) and pollers keep their places. Without it threads of equal
    /// priority take turns, so a thread that only computes or reads the clock
    /// keeps the CPU for a whole timeslice at each committing call of another.
    /// It reads nothing from the recording and draws nothing at random, so
    /// the rest of the run is as neutral as without it: what happens there
    /// depends only on the replayed prefix.
    #[clap(
        long,
        requires = "replay_schedule_from",
        conflicts_with_all = ["chaos", "replay_exhausted_panic"]
    )]
    #[serde(default)]
    pub replay_continue_fairly: bool,

    /// Given schedule events traced on recording or replaying, print the stack trace at the moment
    /// after the Nth event in the trace. Optionally, provide an output file into which the stack
    /// trace will be printed, otherwise it goes to stderr. With a recorded or replayed schedule, N
    /// counts every traced event, polling retries and events the schedule does not hold included,
    /// as the desync and missed-stack-trace lines do; the event number a schedule release reports
    /// leaves polling retries out, so it is not such an index. Under
    /// `--strict-schedule-boundaries`, N is instead a position in the complete boundary stream,
    /// `TimesliceEnd` records included: a replay requires it to lie inside the stored stream,
    /// while a recording counts the stream as it writes it.
    #[clap(long,
           short = 's',
           value_name = "index[,path]",
           value_parser = parse_index_with_path)]
    pub stacktrace_event: Vec<(u64, Option<PathBuf>)>,

    /// Internal, for `hermit analyze`: under `--replay-schedule-from`, each
    /// `--stacktrace-event` index names a position of the replayed schedule,
    /// not a traced event count. The stack prints when the replay consumes the
    /// recorded event at that position, however many events the run observed
    /// before it: a polling retry the recording did not make, say. A position
    /// at or past the schedule's end names the events the run makes after the
    /// replay consumed its last recorded event. A position the replay skips to
    /// resync, or drops with the rest of a released schedule, never prints and
    /// is reported as missed.
    #[serde(default)]
    #[clap(
        long,
        hide = true,
        requires = "replay_schedule_from",
        conflicts_with = "strict_schedule_boundaries"
    )]
    pub stacktrace_at_schedule_positions: bool,

    /// Internal feature used to signal the guest with SIGINT at every `--stacktrace-event`, this is
    /// in-lieu of using hermit's internal stacktrace printing facility, to instead have an external
    /// debugger handle it.  Accepts either signal names or numbers.
    #[clap(long, value_name = "signame")]
    pub stacktrace_signal: Option<SigWrapper>,

    /// Write an access trace to this path as the run records its schedule; its end line is written
    /// when the run ends, and a trace without it was cut short. The trace is JSON Lines with one
    /// record per syscall (its prehook and posthook positions in the recorded schedule, the thread,
    /// the thread's syscall count, the arguments, the result, and the resources it used: descriptor
    /// slots and the open files behind them, file contents, pipes, sockets, futex words, memory
    /// ranges, timers, threads and processes), one per signal delivery (its code, fault address and
    /// sender), and the first 256 bytes of each write to fd 1 or 2. It records resource accesses
    /// beside the schedule for inspection and comparison. An observer: it adds no scheduler request and
    /// moves no virtual time, so the schedule and the guest's results are those of a run without
    /// it, except that Hermit's own command line, which a guest can read as PID 1's, shows the
    /// option, and that the trace file itself is inside the container: it exists from before the
    /// guest's first instruction and grows during the run, so a guest that lists or watches its
    /// directory sees it. Needs a recorded schedule (`--record-preemptions-to` or `--record-preemptions`),
    /// whose positions the records name, sequentialized threads and the ptrace backend. The format
    /// is `detcore_model::access_trace`.
    #[serde(default)]
    #[clap(long = "record-access-trace-to", value_name = "path")]
    pub access_trace: Option<PathBuf>,

    /// With `--record-access-trace-to`, leave out the records whose last schedule event comes
    /// before this position. 0, the default, keeps every record. Resource state from before
    /// the position, such as which open file a descriptor number names, is then only partly
    /// visible in the trace.
    #[serde(default)]
    #[clap(long, value_name = "event", default_value = "0")]
    pub access_trace_from: u64,

    /// With `--record-access-trace-to`, write at most this many records from
    /// `--access-trace-from` on. Past it the trace stops, without its end line, so a reader
    /// sees it cut short; a run whose records all fit still ends with it. Unlimited by
    /// default.
    #[serde(default)]
    #[clap(long, value_name = "records")]
    pub access_trace_max_records: Option<u64>,

    /// With `--record-access-trace-to`, write at most this many bytes of header and records.
    /// The record that would pass it is not written: the trace stops there, without its end
    /// line, so a reader sees it cut short, and Hermit says so once on stderr. Defaults to
    /// 1073741824 (1 GiB, `detcore_model::access_trace::ACCESS_TRACE_DEFAULT_MAX_BYTES`), about
    /// three million records at their typical 350 bytes, so a long run cannot fill the disk by
    /// default; pass a larger count for a longer trace.
    #[serde(default)]
    #[clap(long, value_name = "bytes")]
    pub access_trace_max_bytes: Option<u64>,

    /// Report each access to these guest bytes, through a hardware watchpoint, as a `watch_hit`
    /// record in the access trace (`--record-access-trace-to`): the thread, the instruction after
    /// the access, the event it follows and, for a thread's first 64 hits, its stack. Repeat it for
    /// up to the backend's watchpoint count (4 on x86-64). ADDR is a guest virtual address (hex
    /// with 0x, or decimal), LEN 1, 2, 4 or 8 bytes with ADDR aligned to it (8 by default), and w
    /// reports writes (the default), rw reads and writes. Every thread arms its watches at its
    /// first stop from `--watch-from` on at which it does not block SIGTRAP; the trace's
    /// `watch_summary` lines say which threads armed, when, and how many hits each had. A hit stops
    /// the thread in the tracer only: it adds no scheduler request and moves no virtual time, so
    /// the schedule and the guest's results are those of the run without it. Needs the ptrace
    /// backend's hardware watchpoints and a precise timer whose overflow records are mapped.
    #[serde(default)]
    #[clap(long = "watch", value_name = "addr[:len][:w|rw]")]
    pub watch: Vec<crate::watch::WatchArg>,

    /// With `--watch`, arm the watches only once the run has recorded this many schedule events
    /// (0, the default, arms them from the start).
    #[serde(default)]
    #[clap(long, value_name = "event", default_value = "0")]
    pub watch_from: u64,

    /// **Deprecated:** Print a stacktrace each time the program is preempted.  Only makes sense in `--chaos` mode
    /// and typically goes with preemption recording/replaying.
    #[clap(long)]
    pub preemption_stacktrace: bool,

    /// File to write preemption stacktraces to. Implies `--preemption-stacktrace`. If a
    /// log file is not specified, preemption stacktraces are printed to stderr by default.
    #[clap(long, value_name = "filepath")]
    pub preemption_stacktrace_log_file: Option<PathBuf>,

    /// Enable deterministic IO by reassuring we always read/write the maximum possible bytes
    /// from IO syscalls. There might be cases that read/write syscalls return less bytes than
    /// requests. Detcore, makes an effort to request additional bytes until we reach the ones
    /// requested or EOF.
    #[clap(long)]
    pub deterministic_io: bool,

    /// Fail immediately on unsupported syscalls instead of forwarding them.
    /// Ordinary `hermit run` enables this policy; compatibility requires the
    /// explicit `--allow-unsupported-syscalls` opt-out.
    #[clap(long)]
    pub panic_on_unsupported_syscalls: bool,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-644): Review backend-safe fail-closed termination.
    /// Return a typed Tool error instead of unwinding through a backend callback.
    #[serde(default)]
    #[clap(skip)]
    pub exit_on_unsupported_syscall: bool,
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-644): Review process-tree shutdown for ptrace fail-closed mode.
    /// Terminate the whole tracer when an unsupported syscall is observed.
    #[serde(default)]
    #[clap(skip)]
    pub shutdown_on_unsupported_syscall: bool,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-644): Review the internal cross-process warning report channel.
    /// Internal inherited file descriptor used to aggregate unsupported syscalls.
    #[serde(default)]
    #[clap(skip)]
    pub unsupported_syscall_report_fd: Option<i32>,

    /// Panic when a precise PMU timer overshoots its expected RCB target instead of logging an
    /// error and continuing through normal timer handling. Intended for Detcore debugging.
    #[serde(default)]
    #[clap(
        long = "panic-on-rbc-overshoot",
        visible_alias = "panic-on-rcb-overshoot"
    )]
    pub panic_on_rcb_overshoot: bool,

    /// **Internal:** Set to `true` if we're inside a UTS namespace.
    // FIXME: This can be removed once spawn_fn-based tests support namespaces.
    #[clap(skip)]
    pub has_uts_namespace: bool,

    /// **Internal:** Print a one-time notice on Hermit's standard error when the guest binds an
    /// abstract `AF_UNIX` name, or fails to with `EADDRINUSE`. `hermit run` sets it for a run that
    /// asked for `--network=host` on the ptrace backend: the guest then shares the host's network
    /// namespace, and with it the abstract names of every program on the host, other Hermit runs
    /// included, and Detcore runs in Hermit's own process, so the notice reaches Hermit's stderr
    /// and never the guest's. Nothing the guest observes depends on it. The DBT backend's runtime
    /// reads the frozen legacy form ([`to_legacy_backend_json`]), which leaves this field out.
    #[serde(default)]
    #[clap(skip)]
    pub notice_host_network_abstract_binds: bool,

    /// How the guest's process and thread IDs are numbered
    /// ([`GuestIds`](crate::pid::GuestIds)). The host sets it, not a flag:
    /// `hermit run` from `--no-namespace` and `--pid-base`. Recorded schedules
    /// store it, and a replay under another numbering is refused.
    #[serde(default)]
    #[clap(skip)]
    pub guest_ids: crate::pid::GuestIds,

    /// **Internal:** For each of the guest's standard streams 0, 1 and 2, the lowest of them that
    /// is one open file description with it, as `kcmp(KCMP_FILE)` answered for the descriptors
    /// the launcher hands the guest (Hermit's KVM launch, where the guest's output is not
    /// captured: the stdin it reserved, and its own standard output and error, which the
    /// executor passes through). `None` where that is not known (any other launch, a captured or
    /// replaced stream, or a kernel that refuses kcmp): each stream is then a description of its
    /// own. Detcore's lock ledger takes the streams of one group as the one description they are
    /// (package K slice K8-h). The backend's own answer, a provider query naming which guest
    /// descriptors are one host description, would also cover descriptors no launcher hands
    /// over; no backend offers one yet. The DBT backend's runtime reads the frozen legacy form
    /// ([`to_legacy_backend_json`]), which leaves this field out.
    #[serde(default)]
    #[clap(skip)]
    pub standard_stream_aliases: Option<[u8; 3]>,

    /// **Internal:** Path to the replay data folder.
    #[clap(skip)]
    pub replay_data: Option<PathBuf>,

    /// Kill all remaining tasks iff daemons are the only ones left.
    /// Disabled by default.
    #[clap(long)]
    pub kill_daemons: bool,

    /// Serve the guest to one gdb client on 127.0.0.1:`--gdbserver-port`, and
    /// wait for it before the guest runs; attach with
    /// `gdb -ex 'target remote 127.0.0.1:PORT' PROGRAM`. The port is bound in the
    /// host's network namespace, so the guest keeps the network `--network`
    /// gives it, and a `--strict` run can be debugged with every strict refusal
    /// in force. The debugger is an input of the run: breakpoints, writes to the
    /// guest's memory or registers and the signals it sends all change it. With
    /// a gdbserver, Detcore also takes no reports of the guest's signal-handler
    /// returns, so a multithreaded guest whose handlers take scheduler turns can
    /// be scheduled differently than without one (docs/SIGNAL_STATE_CHECK.md).
    /// Only the ptrace backend (the default, and `--backend=e9patch`, whose
    /// rewritten guest runs under it) serves gdb: `--backend=kvm`, `dbt`,
    /// `sabre`, `liteinst` and `in-guest-trap`, and `--namespace-only`, refuse
    /// `--gdbserver` and `--dap-listen`.
    #[clap(long)]
    pub gdbserver: bool,
    /// The loopback TCP port `--gdbserver` serves on; 0 lets the system pick a
    /// free one, which the run prints.
    #[clap(long, value_name = "uint16", default_value = "1234")]
    pub gdbserver_port: u16,

    /// Configure the maximum time a guest thread may run without returning to Detcore. This is
    /// measured in virtual nanoseconds and enforced with retired conditional branch (RCB)
    /// counting. `--preemption-timeout` is retained as a deprecated alias.
    ///
    /// Set this to `disabled` or `0` to disable PMU-backed preemption. Positive values must be at
    /// least one RCB (10 virtual nanoseconds at the default clock multiplier) and require
    /// user-space hardware performance counters.
    ///
    /// Small values are expensive. A thread that computes without returning to Detcore is stopped
    /// by a PMU timer event at the end of every timeslice, and on the ptrace backend a precise
    /// timer (the default) is placed by stepping the guest through up to the last skid margin of
    /// the slice: 100 to 10000 RCBs depending on the processor, or the `--skid-margin` override. A
    /// slice no longer than the margin is stepped through whole. A CPU-bound guest can run
    /// thousands of times slower than at the default (a pipeline test took 0.3 s at the default and
    /// 994 s at 1000), and a `--timeout` that expires may mean a slow run rather than a hung one.
    /// `hermit run`, `hermit analyze` and `hermit bisect` say so on stderr when a slice is at most
    /// ten skid margins long, so that at least a tenth of it is stepped (1000000 virtual
    /// nanoseconds at the default clock multiplier on a processor with a 10000-RCB margin); a
    /// preemption replay, whose recording sets the slices, and `--imprecise-timers`, which steps
    /// nothing, say nothing. `--summary` reports how many timeslices the run had.
    #[serde(alias = "preemption_timeout")]
    #[clap(
                long,
                visible_alias = "preemption-timeout",
                value_name = "uint64|'disabled'",
                default_value = "200000000",
                value_parser = parse_timeslice)]
    pub max_timeslice: MaybeTimeslice,

    /// Target logical timeslice, in virtual nanoseconds, checked at syscall boundaries and the
    /// other points where the guest returns control to Detcore: signals, timer events, and
    /// trapped `rdtsc` and `cpuid` instructions unless `--target-timeslice-syscalls-only` is given.
    /// This avoids PMU preemption for workloads that enter the kernel frequently. Omit this option
    /// to use only `--max-timeslice`.
    #[serde(default)]
    #[clap(long, value_name = "virtual-nanoseconds")]
    pub target_timeslice: Option<NonZeroU64>,

    /// Do not end an expired `--target-timeslice` at a trapped `rdtsc` or `cpuid` while a PMU
    /// maximum (`--max-timeslice`) is armed; end it at the next syscall, signal or timer event,
    /// or at the PMU maximum. A trapped instruction can sit inside a user-space spinlock: QEMU's
    /// multi-threaded TCG reads the host TSC while holding its `vm_clock_lock`, and a vCPU that
    /// yields there leaves the other vCPUs spinning for whole PMU slices. Has no effect without
    /// `--target-timeslice`, or with `--max-timeslice disabled`, where the trap is the only way
    /// out of a busy-wait on the TSC.
    #[serde(default)]
    #[clap(long, requires = "target_timeslice")]
    pub target_timeslice_syscalls_only: bool,

    /// After a `FUTEX_WAKE` or `FUTEX_WAKE_BITSET` that woke at least one waiter, move the waker to
    /// the back of its priority band and have it yield, so that woken threads of its priority run
    /// before it continues. Without
    /// it, a thread that releases a contended lock keeps running until its slice ends and can
    /// re-take the lock many times before the waiter is scheduled: QEMU's multi-threaded TCG vCPUs
    /// starve its main loop of the big QEMU lock this way. The yield is a scheduler turn of its
    /// own: the waker's timeslice keeps its deadlines. Each extra thread switch is charged
    /// scheduler time that the guest can observe (see `--scheduler-turn-cost`), and costs wall
    /// time: a two-vCPU QEMU boot took 5% to 48% longer with it. The waker keeps its priority, so a
    /// woken thread of a lower priority still runs after it; `--chaos` redraws threads' priorities
    /// from their chaos PRNG, which can put a woken thread in a lower band; and a random
    /// `--sched-heuristic` does not select first in, first out. In those cases a woken thread is not
    /// guaranteed to run first. Applies to the precise futex mode while threads are sequentialized,
    /// on the ptrace backend (including e9patch preprocessing, which runs on it); replay applies it
    /// as the recorded run did.
    #[serde(default)]
    #[clap(long)]
    pub futex_wake_yields: bool,

    /// Hand the turn to a thread polling a pipe or FIFO once another thread's action has readied it
    /// (<https://github.com/rrnewton/hermit/issues/4380>), within this many virtual nanoseconds of
    /// the acting thread's own time. A write that moves bytes into a scheduler-managed pipe, a read
    /// that makes room in one, an explicit close of one end, or an open of a FIFO end readies the
    /// threads polling for that: the scheduler restores their priority, and once the acting
    /// thread's time is this far past its first readying action, at the next point where it
    /// returns to Detcore (a syscall's entry or exit, or a signal or trapped instruction), it is
    /// moved behind them and yields, so they run before it continues. A poller is restored to its
    /// own priority and the acting thread moves only within its band, so where `--chaos` has drawn
    /// a poller a lower priority than the acting thread, that poller still runs after it and is
    /// not guaranteed to run first. `0` hands off at the end of
    /// the readying syscall. Unset (the default), nothing changes: a polled thread whose peer is
    /// ready waits for the acting thread's slice to end, as before. Each hand-off adds at least two
    /// charged scheduler turns that the guest can observe as time (see `--scheduler-turn-cost`).
    /// A thread that computes after its write without returning to Detcore delays the hand-off
    /// until it next does, or its slice ends; an implicit close (an exit, an `O_CLOEXEC` exec)
    /// readies nothing; and an end reopened by path (`/proc/self/fd/N`, as `/dev/fd/N` process
    /// substitution does) is a channel of its own, so the other side's actions do not ready it. Recorded
    /// schedules store the value, and a replay under another value is refused. Refused without
    /// sequentialized threads, on the DBT, KVM and LiteInst backends, with `--namespace-only`,
    /// `--strace-only` or `--debug-externalize-sockets`, and
    /// under a `--sched-heuristic` that does not select by priority; never applied in record mode.
    #[serde(default)]
    #[clap(long, value_name = "nanos")]
    pub pipe_wake_latency: Option<u64>,

    /// How a run of consecutive scheduler turns that each end an expired timed wait (a futex wait
    /// answered `ETIMEDOUT`, or a sleep already due) is charged while another thread is queued:
    /// `once`, one scheduler turn for the whole run, or `each`, a turn per wait. Unset, `each`
    /// under `--chaos` or a `--sched-heuristic` that does not select by priority, where the time
    /// each charge adds stretches short waits as race search relies on, and `once` otherwise, so
    /// that a thread polling with waits shorter than a turn charge blocks and lets other threads
    /// run. A replay of a schedule or of preemptions that leaves this unset takes the value its
    /// record stores (a record that stores none, written before the option existed or by an
    /// `each` run, charged each wait), and an explicit value that disagrees with the record is
    /// refused. `hermit analyze` and `hermit bisect` run every trial under the value of the
    /// recording they start from, or `each` when analyze records its own chaos target. Under
    /// `hermit record` and `hermit replay` every wait is charged, whatever the value.
    #[serde(default)]
    #[clap(long, value_enum, value_name = "each|once")]
    pub expired_wait_charge: Option<ExpiredWaitCharge>,

    /// When a queued plain sleep (a request for `SleepUntil` alone, not yet due) becomes a timed
    /// waiter: `first-pass`, at the first quiescent scheduler pass that finds it queued, whatever
    /// the priorities, as a Linux hrtimer is armed when the sleep starts; or `when-chosen`, only
    /// when its thread is chosen before its deadline, so a sleeper behind threads of better
    /// priority wakes later. Unset, `when-chosen` under `--chaos` or a `--sched-heuristic` that
    /// does not select by priority, where those late wakes are what real sleep-ordered races
    /// depend on, and on a backend whose sleepers' signal mask and dispositions the scheduler
    /// does not read (DBT, SaBRe and LiteInst without the in-guest delivery gate, KVM), and
    /// `first-pass` otherwise. On such a backend a sleep parked at the first pass would be ended
    /// by any signal the scheduler sends it, including one Linux ignores or the sleeper blocks,
    /// so `first-pass` there, asked for or taken from a record, is refused by name before the
    /// guest starts. A replay of a schedule or of preemptions that
    /// leaves this unset takes the value its record stores (a record that stores none, written
    /// before the option existed or by a `when-chosen` run, registered sleeps when chosen), and an
    /// explicit value that disagrees with the record is refused. `hermit analyze` and `hermit
    /// bisect` run every trial under the value of the recording they start from, or
    /// `when-chosen` when analyze records its own chaos target. Under `hermit record` and
    /// `hermit replay` a sleep is registered when chosen, as before, whatever the value.
    #[serde(default)]
    #[clap(long, value_enum, value_name = "first-pass|when-chosen")]
    pub sleep_registration: Option<SleepRegistration>,

    /// Write a work/span report of the run's schedule to this path as JSON when the run ends
    /// normally: work (the sum of every thread's guest CPU time, in virtual nanoseconds), a
    /// lower span over the scheduler's certain happens-before edges (program order, spawn, a
    /// child's exit to the wait that reaps it, conflicting resources), and an upper span that
    /// also orders a thread that waited after everything other threads did meanwhile. The
    /// report lists the dependencies it does not model: what a wait waited for and who sent a
    /// signal, so the lower span can be optimistic; and what passes without a wait (data, an end
    /// of file or a peer's close found on a channel, a pending signal taken by `sigtimedwait`,
    /// shared memory, files and devices), which neither span orders; and the CPU time of a call a
    /// thread was running outside the schedule when it was killed. An observer: no scheduling
    /// decision reads it, so the schedule
    /// and the guest's results are those of a run without it, except that Hermit's own command
    /// line, which a guest can read as PID 1's, shows the option. Needs sequentialized threads
    /// and the ptrace backend. Unset (the default), nothing is gathered.
    #[serde(default)]
    #[clap(long, value_name = "path")]
    pub work_span_report: Option<std::path::PathBuf>,

    /// Virtual nanoseconds that each committed scheduler turn adds to the global clock, before
    /// `--clock-multiplier` (default 500000). The charge keeps time moving while threads take
    /// turns without running. Every thread observes it as a jump in time across a thread switch,
    /// so a guest that times short intervals while another thread runs, such as a QEMU guest
    /// kernel calibrating its TSC against the emulated PIT, can see a gap far larger than any
    /// work done. A smaller charge makes those jumps smaller; a finite poll or futex timeout then
    /// takes more turns to expire. The charge, after the multiplier, must be between 1 and
    /// 1000000000. If a charged turn would pass the end of representable virtual time, the run
    /// stops with HERMIT_VIRTUAL_CLOCK_EXHAUSTED and exit status 125.
    #[serde(default)]
    #[clap(long, value_name = "virtual-nanoseconds")]
    pub scheduler_turn_cost: Option<NonZeroU64>,

    /// Charge a scheduler turn that ends a timeslice because its time ran out (a preemption)
    /// at most the virtual time its thread ran since its previous turn, instead of the full
    /// turn charge (`--scheduler-turn-cost`, default 500000 virtual nanoseconds). Without it,
    /// every preemption adds the full charge however short the slice was, so a small
    /// `--max-timeslice`, or a short `--chaos` draw, multiplies the guest-visible time of
    /// CPU-bound work by about `1 + charge / slice`: 501 times at `--max-timeslice=1000`,
    /// enough for a test's own timeouts to expire before it reaches the code under test. With
    /// it the factor is at most 2, what a slice one full charge long already costs. A
    /// preemption whose thread ran at least one full charge since its previous turn is
    /// charged as before; a thread's turns include its syscalls, so one that follows a syscall
    /// by less is charged only the time since that syscall, however long the slice. A capped
    /// turn is charged at least one virtual nanosecond, unless its full charge after
    /// `--clock-multiplier` is zero. An explicit `sched_yield`, and every
    /// turn that does not end a timeslice, keep the full charge. This changes what the guest
    /// reads from `clock_gettime(CLOCK_MONOTONIC)` and the other virtual clocks, and so when
    /// its timers and timeouts expire: recorded schedules store the setting, and a replay
    /// under the other is refused.
    /// Refused where no timeslice ends by expiry (`--max-timeslice=disabled` without
    /// `--target-timeslice`) or no Detcore scheduler orders the threads (`--namespace-only`,
    /// `--strace-only`, `--no-sequentialize-threads`), and with the DBT backend; never applied
    /// in record mode.
    #[serde(default)]
    #[clap(long)]
    pub cap_preemption_charge: bool,

    /// End the run in virtual time: once the run's virtual clock is more than this many virtual
    /// nanoseconds past the run's start, Hermit grants no further scheduler turn and the run ends
    /// with exit status 124 and the stderr line `HERMIT_RUN_TIMEOUT class=virtual-timeout
    /// bound_ns=N`. The clock is Detcore's global virtual time since the run began, the same
    /// virtual time `--summary` reports as elapsed (`virttime_elapsed` in `--summary-json`); it is
    /// checked each time the scheduler commits it at the start of a turn, before the turn's thread
    /// runs, and the check never moves it. A guest that has already exited, or whose remaining
    /// processes a committed exit, kill, in-flight exec or fatal signal has marked doomed, is not
    /// stopped by it. A failed exec clears its in-flight mark before the old image resumes.
    /// Process-group SIGKILL marks are conservative: if a marked process survives a failed group
    /// kill, the bound does not stop it. Unlike `--timeout`, a
    /// wall-clock bound in seconds that fires wherever the guest has got to when the host's clock
    /// runs out (`class=run-timeout`), for a guest whose input all comes from inside the
    /// container this bound fires at the same point of the same schedule on every host and in
    /// every run, so the output written before it, `--summary` and a `--verify` comparison are
    /// reproducible. A stopped run is not a guest result, so `--verify` compares it only under
    /// `--verify-allow=failure` or `both`; the default `success` refuses run 1, as it refuses a
    /// deadlock under Hermit's model. A thread that polls something outside the container, such
    /// as a pipe a host process feeds, advances virtual time with each retry, and the number of
    /// retries depends on the host, so such a run can stop at another turn; `--verify
    /// --verify-strict` reports that as a divergence. Virtual time does not pass while every thread is blocked in a call
    /// outside the container, so bound such a wait with `--timeout`. On the ptrace backend the
    /// run is then finished as for a guest that exited: its output is kept and `--summary` says
    /// the bound fired. On the kvm backend the run exits 124 without a summary, and `--verify`
    /// with the bound is refused there. Needs sequentialized threads and virtual time: refused
    /// with `--no-sequentialize-threads`, `--namespace-only`, `--strace-only` or
    /// `--no-virtualize-time`, and on the dbt, liteinst, in-guest-trap and sabre backends, where
    /// it has not been qualified. Also refused before guest or debugger startup with
    /// `--gdbserver` or `--dap-listen`: a debugger can suppress a fatal signal after Detcore
    /// marks its process doomed, leaving a survivor outside the bound. Remove the debugger
    /// option or the virtual bound, or use `--timeout` for a wall-clock bound instead.
    #[serde(default)]
    #[clap(long, value_name = "virtual-nanoseconds")]
    pub virtual_timeout: Option<NonZeroU64>,

    /// Shut down immediately upon SIGINT, rather than letting the guest handle it.
    #[clap(long)]
    pub sigint_instakill: bool,

    /// Warn if binds are non-zero.
    #[clap(long)]
    pub warn_non_zero_binds: bool,

    /// Apply a specialized scheduling heuristic which may help exercise certain bugs.
    #[clap(long, default_value = "none", value_name = "str")]
    // TODO: Rename this to scheduler_strategy?
    pub sched_heuristic: SchedHeuristic,

    /// Use this number to seed the PRNG that supplies randomness to the scheduler.
    #[clap(long, env = "HERMIT_SCHED_SEED", value_name = "uint64")]
    pub sched_seed: Option<u64>,

    /// External network record or replay, set by `hermit run --record-networking`
    /// or `--replay-networking`; `Off` otherwise, in which case Detcore never
    /// consults it. Its perturbation seed has no fallback to `seed` or
    /// `sched_seed`.
    #[serde(default)]
    #[clap(skip)]
    pub network_trace: NetworkTraceConfig,

    /// Configure the probability for the Sticky Random scheduler to stay in a thread.
    /// For value 0.0, we are behaving like Random.
    /// For value 1.0, we are behaving like a DFS, where the same thread is
    /// always picked as long as it is available in the Run queue. After
    /// this thread is exhausted, the next thread will be chosen randomly.
    /// For value 0.5, we have a 50/50 chance to pick the same thread.
    #[clap(long, default_value = "0.0", value_name = "double")]
    pub sched_sticky_random_param: f64,

    /// **Internal:** An internal flag for indicating to Detcore whether we are in `hermit record` or
    /// `hermit replay` mode.  This is necessary because there are DIFFERENT global
    /// invariants in record mode (e.g. files dont exist).  If we move to a chroot model
    /// and reproduce more, recording less, then this flag should become obsolete.
    #[clap(skip = false)]
    pub recordreplay_modes: bool,

    /// **Internal:** Set only by `hermit replay`, alongside `recordreplay_modes`. Replay serves
    /// socket reads, writes, accepts and connects, and `poll`, `ppoll`, `select` and
    /// `pselect6`, from the recording, so they cannot block. They still run as backgrounded
    /// blocking I/O, as during recording, but Detcore's scheduler commits no other turn until
    /// they finish. Otherwise the descriptor-table changes of different threads could
    /// interleave differently from one replay to the next.
    ///
    /// It never enters the legacy form ([`to_legacy_backend_json`]), so a standalone DBT
    /// run's `HERMIT_DBT_DETCONFIG` reads it as false. Record and replay run only on the
    /// ptrace backend.
    #[serde(default)]
    #[clap(skip = false)]
    pub replaying: bool,

    /// **Internal:** debugging option to stop execution after a specific scheduler commit, aka turn number
    /// (non-negative integer). This only makes sense if `--sequentialize-threads` is specified, as the scheduler is otherwise not engaged.
    #[clap(long, value_name = "turn_N")]
    pub stop_after_turn: Option<u64>,

    /// **Internal:** debugging option to stop execution after a scheduler loop iteration (non-negative integer).
    /// This only makes sense if `--sequentialize-threads` is specified, as the scheduler is otherwise not engaged.
    #[clap(long, value_name = "iter_N")]
    pub stop_after_iter: Option<u64>,

    /// Send SIGNAL to the run's root process once the scheduler has committed
    /// turn N, as a timer expiry is sent (`turn=N:SIGNAL`, SIGNAL a name such
    /// as `SIGUSR1` or a number, not SIGKILL or SIGSTOP). It is sent before
    /// the next turn is selected, after the first `COMMIT turn M` with M >= N:
    /// turn numbers also count the turns the scheduler skips, so if turn N
    /// was skipped, after the next commit. The turn is a deterministic point
    /// of the run, so a process in the run that waits for the signal acts at
    /// the same point every time: for example a controller that stops QEMU and
    /// saves a snapshot there. The run itself goes on. Only with
    /// `--sequentialize-threads`, where the scheduler numbers turns.
    #[clap(long, value_name = "turn=N:SIGNAL", value_parser = parse_signal_at)]
    #[serde(default)]
    pub signal_at: Option<SignalAt>,

    /// The slice bound, in virtual nanoseconds, once the guest has crashed:
    /// `off` (the default) keeps `--max-timeslice` to the end; `default` is
    /// the default `--max-timeslice` (200000000); a number is that bound.
    ///
    /// A crash is a fatal signal on its way to ending a process: SIGABRT
    /// however it was sent, or SIGSEGV, SIGBUS, SIGILL, SIGFPE or SIGSYS
    /// raised by the kernel for a fault. One whose delivery takes the default
    /// action switches at once. One the guest catches arms the switch, which
    /// is taken when a process the crashed one created meanwhile (or the
    /// crashed process itself) execs while the handler still runs (Android's
    /// crash handler runs `crash_dump` that way), and dropped when the handler
    /// returns (a runtime that turns a fault into an exception, as ART and
    /// JVMs do for null checks). A handler left with a plain `longjmp`, which
    /// keeps the signal blocked, counts as still running. From the switch on,
    /// the slices of the crashed process and of the processes it creates are
    /// bounded by this value instead of the run's, so a run whose race needs
    /// fine slices does not spend its time slicing the crash dumper; other
    /// processes (a harness running trials as children) keep the run's bound.
    /// The switch happens at the same point of every run and replay that
    /// reaches it, and a preemption record stores both the setting and where
    /// it switched; a replay takes the record's setting, and refuses another
    /// (it cannot turn a recorded switch off). A no-op unless the run's own
    /// bound is below this one. `--summary` reports it.
    #[clap(
        long,
        value_name = "uint64|'default'|'off'",
        default_value = "off",
        value_parser = parse_post_crash_timeslice
    )]
    #[serde(default)]
    pub post_crash_timeslice: MaybeTimeslice,

    /// **Internal:** Debugging option to treat all sockets as mysterious external, nondeterministic
    /// entities, rather than container-internal and determinstically scheduled.
    #[clap(long)]
    pub debug_externalize_sockets: bool,

    /// **Internal:** Debugging option to change how futexes are implemented, either precisely modeled
    /// by hermit, by polling the kernel with non-blocking futex operations, or treated as external
    /// (nondeterministic) operations which unblock at imprecise times.
    #[clap(
        long,
        value_name = "precise|polling|external",
        default_value = "precise"
    )]
    pub debug_futex_mode: BlockingMode,

    /// Do not count the retired conditional branches (RCBs) of each thread towards its logical
    /// time.  Instead, count each checkin with the scheduler as a fixed increment to logical time.
    /// Even when this option is set, HW RCB performance counters may still be enabled if a
    /// max-timeslice is specified.
    #[clap(long)]
    pub no_rcb_time: bool,

    /// An option to enable logging the hash of heap memory maps for the purpose of determinism checking
    #[clap(long)]
    pub detlog_heap: bool,

    /// An option to enable logging the hash of stack memory maps for the purpose of determinism checking
    ///
    /// THIS HASH COVERS argv AND THE ENVIRONMENT, which the kernel places at the
    /// top of the initial process stack. Two runs whose command lines differ by a
    /// single character therefore produce different stack hashes from the first
    /// sample, even when the command lines are the same LENGTH and every stack
    /// address matches. Measured: equal-length-but-different argv diverged the
    /// hash 14 records in, while byte-identical argv held it for 5023 records.
    ///
    /// Holding a run-directory name to a fixed WIDTH is a sufficient control when
    /// only addresses matter, and is NOT sufficient here. Comparing two runs
    /// under this flag requires byte-identical argv and environment; otherwise
    /// the first divergence you find is your own input.
    #[clap(long)]
    pub detlog_stack: bool,

    /// Log a hash of every private, writable anonymous mapping at each syscall, for memory
    /// determinism checking: allocator arenas (scudo's, jemalloc's, glibc's mmap chunks), thread
    /// stacks and TLS blocks, none of which `--detlog-heap` (the brk heap) or `--detlog-stack`
    /// (the main thread's stack) covers. A mapping named by `PR_SET_VMA_ANON_NAME`
    /// (`[anon:<name>]`) counts as anonymous. `--detlog-anon=pages` adds a short digest for each
    /// 4 KiB page that is not all zero, to find the page where two runs part.
    ///
    /// ptrace backend only (also with e9patch preprocessing): the mappings come from the guest
    /// task's own `/proc/<tid>/maps`, which an in-guest tool shares and a VMM's does not hold.
    /// Other backends refuse the flag. EXPENSIVE: every byte of every such mapping is read at
    /// every syscall, in 1 MiB pieces, so a guest with a large heap pays for its whole size each
    /// time; a mapping over 256 MiB (a sanitizer's shadow, a heap reserved up front) is recorded
    /// by its size alone. Reading an untouched page maps the shared zero page there, which
    /// `mincore` then reports resident, as with `--detlog-heap` and `--detlog-stack`. A thread
    /// stack holds thread IDs and, below its stack pointer, the scratch Hermit writes when it
    /// injects a call, so runs whose IDs differ differ there legitimately. Page indices count
    /// from the mapping's start, so a mapping that merges or moves renumbers its pages.
    #[clap(
        long,
        value_enum,
        value_name = "pages",
        num_args = 0..=1,
        require_equals = true,
        default_missing_value = "mappings"
    )]
    #[serde(default)]
    pub detlog_anon: Option<DetlogAnon>,

    /// Log a hash of the guest REGISTER FILE at guest-logical-control points, for determinism
    /// checking. stdout, the INFO log, the stack and the heap are all hashed today; the register
    /// file is not, so two backends can differ in register state and every existing check still
    /// reports parity.
    ///
    /// SAMPLED ONLY AT GUEST-LOGICAL-CONTROL POINTS -- see `Detcore::detlog_registers`. Registers
    /// are NOT sampled inside a tool handler: a backend running its handler in-guest executes code
    /// the ptrace reference never executes, so a difference there is correct behaviour, not a
    /// determinism bug.
    #[clap(long)]
    pub detlog_regs: bool,

    /// Log a hash of each syscall's OUTPUT BUFFER, taken at the syscall boundary from the
    /// address and length in the syscall's own arguments.
    ///
    /// WHAT IT SEES THAT THE MAPPING HASHES DO NOT. `--detlog-heap` and `--detlog-stack` hash a
    /// whole named mapping, so their coverage is decided by where the guest happened to ALLOCATE
    /// a buffer. Measured, three runs per cell, same netlink exchange with only the receive
    /// buffer's home changed: a `[stack]` buffer is missed by `--detlog-heap`, a `[heap]` buffer
    /// is missed by `--detlog-stack`, and a BSS/static or anonymous-`mmap` buffer is missed by
    /// BOTH even with both enabled. Anonymous `mmap` is where glibc puts any `malloc` above the
    /// 128 KiB `M_MMAP_THRESHOLD`. Reading the extent out of the syscall arguments makes the
    /// buffer's home irrelevant.
    ///
    /// WHY IT IS NOT REDUNDANT WITH `--verify`. A syscall whose buffer is a bare pointer in
    /// Reverie prints the ADDRESS, not the contents, so a `recvmsg` returning a stable
    /// `Ok(1468)` whose payload varies produces a character-identical record and `--verify`
    /// reports `bitwise_parity: true`. 44.1% of the syscalls in a QEMU/Linux boot move bytes
    /// through such a buffer.
    ///
    /// COST is proportional to bytes actually moved, NOT to syscall count or mapping size:
    /// ~0.75 s per GB of guest I/O. A QEMU/Linux boot moves 139.1 MB through these buffers,
    /// against the 10.9 TB `--detlog-heap` hashes over the same run.
    ///
    /// NAME IS PROVISIONAL: `io-buffers` is the owner's candidate and is not settled.
    ///
    /// ON BY DEFAULT. It was opt-in until 2026-08-24, and opt-in made the
    /// determinism gate weaker than its name: with the hash absent, the netlink
    /// `recvmsg` above compares equal and `--verify` reports success. A check
    /// that must be requested is not a standard. The opt-out exists for the
    /// deliberate case (bulk I/O where the cost matters and content parity is
    /// not the question), not as the ordinary setting.
    ///
    /// COST OF THE DEFAULT, measured 2026-08-24 on a 316-core x86_64 Linux
    /// build host: a typical small test guest pays about ONE MILLISECOND
    /// (`/bin/true` 0.029s -> 0.030s, `/bin/ls` 0.041s -> 0.041s, 8 runs each).
    /// 64 MiB through `cat` costs +0.07-0.10s in a RELEASE build, which is the
    /// ~1.1-1.6 s/GB matching the figure quoted above. The same workload in a
    /// DEBUG build costs +3.4s, roughly 50x more, because the hash loop is
    /// unoptimized -- so a debug-built node moving tens of megabytes is the one
    /// place the default is felt.
    #[clap(long = "no-detlog-io-buffers", action = clap::ArgAction::SetFalse)]
    pub detlog_io_buffers: bool,

    /// Sampling cadence for `--detlog-regs`: hash every Nth guest-logical-control point.
    ///
    /// COST TIER. 1 (the default) is the FULL tier -- every control point hashed -- and is what a
    /// short test should use. Measured cost at this scale is within run-to-run noise: /bin/true
    /// (49 control points), `wc -l /etc/passwd` (135) and a 5-iteration shell loop (195) were
    /// 0.04-0.07s with the flag on and the same with it off. A larger N is the SPOT-CHECK tier for
    /// runs where full hashing is too expensive; it trades detection latency for cost, since a
    /// divergence is only seen at the next sampled point. Every emitted line records the tier it
    /// was produced under, so a cell can state which tier it met instead of leaving it implicit.
    #[clap(long, default_value = "1", value_name = "uint64")]
    pub detlog_regs_cadence: u64,

    /// Configure a time offset (in seconds) between a container OS considered booted and a guest is executed
    /// This primarily affects 'sysinfo' syscall's 'uptime' field reporting
    #[clap(long, default_value = "120", value_name = "uint64")]
    pub sysinfo_uptime_offset: u64,

    /// Configure memory available for the container.  Takes a number of bytes, or shorthand (e.g.
    /// "1GB"). Right now this doesn't enforce an upper bound, but does affect the amount of memory
    /// reported to the guest.
    #[clap(long, default_value = "1GB", value_parser = try_parse_memory, value_name = "bytesize")]
    pub memory: u64,

    /// Present a machine with this many CPUs, 1 to 128, so that `nproc`,
    /// `sysconf(_SC_NPROCESSORS_ONLN/CONF)` and thread pools size themselves
    /// to it. Threads still run one at a time. The affinity mask,
    /// `getcpu`, CPUID's APIC IDs, `/proc/cpuinfo`, `/proc/stat`, a task's
    /// `Cpus_allowed` and `/sys/devices/system/cpu` (its CPU lists, `cpuN`
    /// directories, topology and cache files) all describe the same N
    /// single-core sockets on one NUMA node. `sched_setaffinity` follows
    /// Linux: a mask with none of the N CPUs is EINVAL. The host must list
    /// at least N CPUs under `/sys/devices/system/cpu`. Without this flag the
    /// guest sees Hermit's historical single CPU, whose `sched_setaffinity`
    /// accepts any mask.
    #[serde(default)]
    #[clap(long, value_name = "count", value_parser = parse_virtual_cpus)]
    pub virtual_cpus: Option<NonZeroU32>,

    /// How much of `/proc` the guest sees. Every level is the same on every
    /// backend and host. `minimal` (the default) shows a small set, modelled
    /// on gVisor's; `standard` shows every system-wide name Detcore serves or
    /// plans to serve. A system-wide or `/proc/sys` name outside the chosen level
    /// does not exist: lookups give ENOENT and listings leave it out. So does
    /// a name in a process's or a thread's directory, except a few that Hermit
    /// still leaves to the kernel (`mem`, `uid_map`, `gid_map`, `setgroups`),
    /// as are whether a process exists and the entries of its `fd`,
    /// `fdinfo`, `ns` and `net`. A spelling through a symbolic link or `..` is
    /// checked only when it opens a file.
    #[serde(default)]
    #[clap(
        long = "proc",
        default_value = "minimal",
        value_name = "minimal|standard"
    )]
    pub proc_level: ProcLevel,

    /// Configure extra interrupt points based on thread id and rcb counter. Detcore will raise a precise
    /// timer for this RCB whenever it detects that current current thread timeslice intercects any of the
    /// interrupt points specified
    #[clap(long, value_name = "tid:rcbs", value_parser = try_parse_numbers_with_colon)]
    pub interrupt_at: Vec<(DetTid, u64)>,

    /// Resolved happens-before program: deterministic ordering edges between
    /// anchored events (see `detcore_model::happens_before`). This is populated
    /// programmatically by hermit-cli after loading and resolving a
    /// `--happens-before` spec against the guest binary; it is not a direct CLI
    /// flag and is not serialized (it is reconstructed from the spec file each
    /// run, so `#[serde(skip)]` avoids requiring serde on `Sysno`-bearing
    /// positions and keeps save-config output stable). The scheduler enforces
    /// these edges only when `sequentialize_threads` is set.
    #[serde(skip)]
    #[clap(skip)]
    pub happens_before: Option<HappensBeforeProgram>,

    /// Whether Detcore records the host identity of each file the guest
    /// opens, sending each record to the global state, which writes the run's
    /// records to [`Self::host_input_log`] when the run ends (see
    /// [`crate::host_input`]). `hermit run --verify` sets it, so that a
    /// divergence caused by a host file replaced during a run can be named.
    /// The records are never part of the compared log. Serialized, so that it
    /// reaches the Detcore tool on every backend, including one that runs
    /// inside the guest and receives this `Config` over RPC; never a CLI flag.
    #[serde(default)]
    #[clap(skip)]
    pub record_host_inputs: bool,

    /// Where the global state writes the run's host-input records, one
    /// [`crate::host_input::HostInputRecord`] JSON line each and a
    /// [`crate::host_input::HostInputLogEnd`] line, when the run ends.
    /// `hermit run --verify` sets a private file. A host path, read only by
    /// the global state, which every backend that hosts it builds from this
    /// `Config` in-process; so set programmatically, like `happens_before`,
    /// and never serialized.
    #[serde(skip)]
    #[clap(skip)]
    pub host_input_log: Option<PathBuf>,

    /// Captured inputs that the launcher copied into private container files.
    /// Only the host global state consumes these identities, just like
    /// `host_input_log`; guest-side tools still report the actual open's stat.
    /// Never serialized: every backend constructs that global state from this
    /// in-process config. Physical identities remain necessary for deferred
    /// content checks even when records publish the captured input's origin.
    #[serde(skip)]
    #[clap(skip)]
    pub host_input_snapshots: Vec<crate::host_input::HostInputSnapshot>,

    /// The frozen copy of the Android system property area that the guest
    /// reads in place of the live one ([`PropertySnapshot`]). hermit-cli makes
    /// the copy and sets this before the guest's first exec on a host that has
    /// `/dev/__properties__`; never a CLI flag. Serialized, because Detcore's
    /// open handler reads it in each guest process's tool, which receives this
    /// `Config` encoded; it came after the legacy form froze, so it never
    /// enters that form and reads as unset there.
    #[serde(default)]
    #[clap(skip)]
    pub property_snapshot: Option<PropertySnapshot>,

    /// A guest address range, `[start, end)`, holding code whose syscalls
    /// the backend lets through without Detcore seeing them, which the guest
    /// cannot otherwise use without faulting. With
    /// [`Self::record_host_inputs`] set, a guest call that may change the
    /// memory there (a fixed mapping over it, a protection or advice change
    /// or unmapping that covers it, an open of a process-memory file for
    /// writing) is reported as a change at `/`, so no host input change is
    /// named in that run (see [`crate::host_input`]). The backend that hosts
    /// Detcore in-process sets it, so set programmatically, like
    /// `happens_before`, and never serialized.
    #[serde(skip)]
    #[clap(skip)]
    pub untraced_code_range: Option<(u64, u64)>,

    /// Opt-in capture of a core file for a guest thread killed by a signal
    /// whose default action dumps core; `None`, the default, writes nothing.
    /// A host path and host-side limits, read only by the global state at a
    /// fatal exit stop, so set programmatically, like `host_input_log`, and
    /// never serialized.
    #[serde(skip)]
    #[clap(skip)]
    pub fatal_core_capture: Option<FatalCoreCapture>,

    /// Whether the in-guest runtime delivers the guest's signals through
    /// Detcore's schedule (A5 step 3c.1, gate D35 of the design,
    /// <https://github.com/rrnewton/hermit/issues/4485>): while its code runs
    /// it blocks every signal it does not reserve, and at each return to
    /// guest code it predicts the kernel's deliveries, reports them in two
    /// phases, and makes a predicted death a scheduled one. Detcore answers
    /// Reverie's `Tool::in_guest_signal_delivery` from it.
    ///
    /// The host sets it, not a flag, when it prepares an in-guest run on the
    /// default route, and only where every condition of the gate holds:
    /// site patching off, sequentialized threads, no configuration in which
    /// the two-phase protocol cannot be kept (`detcore::two_phase_refusal`),
    /// and a scheduler that runs as the init of the guest's PID namespace,
    /// which only `hermit run`'s container launch knows. Preparing any run
    /// clears it first, so a caller's configuration cannot carry it in; the
    /// in-guest loader route never sets it. Every Tool image receives it in
    /// the configuration handshake, which guest code cannot change. False,
    /// the default, changes nothing. Like `in_guest_reserved_signals`, it
    /// has no legacy key.
    #[serde(default)]
    #[clap(skip)]
    pub in_guest_signal_delivery: bool,

    /// Whether `--replay-preemptions-from` names a record that carries
    /// callback checkpoints, so priority-history replay ends each annotated
    /// row at its recorded callback endpoint instead of at the first stop that
    /// reaches the row's RCB count
    /// (<https://github.com/rrnewton/hermit/issues/4082>).
    ///
    /// The host sets it, not a flag, from the validated record before the
    /// scheduler or any Tool reads the configuration, and refuses a
    /// configuration it does not qualify for such a record
    /// (`preemption_admission::refuse_unqualified_annotated_replay`). Preparing
    /// any run sets it from the record, so a caller's configuration cannot
    /// carry it in; Detcore refuses a record whose checkpoints disagree with
    /// it. A record so annotated was made with `--record-preemptions`, so the
    /// replay observes schedule events as its recording did
    /// (`observes_schedevents`). False, the default, changes nothing. Like
    /// `in_guest_signal_delivery`, it has no legacy key.
    #[serde(default)]
    #[clap(skip)]
    pub annotated_priority_replay: bool,
}

/// Where and how much [`Config::fatal_core_capture`] may write.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FatalCoreCapture {
    /// Directory receiving the compressed cores. Its regular files, whoever
    /// wrote them, count against `max_total_bytes`.
    pub dir: PathBuf,
    /// Prefix of every file name this run writes, so the run can find and
    /// remove its own cores.
    pub file_prefix: String,
    /// Most bytes one compressed core may occupy.
    pub max_core_bytes: u64,
    /// Most bytes all regular files in `dir` may occupy together.
    pub max_total_bytes: u64,
    /// Longest one capture may take before it gives up.
    pub time_limit: std::time::Duration,
}

fn try_parse_numbers_with_colon(from_str: &str) -> anyhow::Result<(DetTid, u64)> {
    if let Some((thread_id_str, time_str)) = from_str.split_once(':') {
        Ok((
            thread_id_str
                .parse::<DetTid>()
                .map_err(anyhow::Error::msg)?,
            time_str.parse::<u64>().map_err(anyhow::Error::msg)?,
        ))
    } else {
        anyhow::bail!(
            "unable to parse <thread_id>:<logical_time> from '{}'",
            from_str
        )
    }
}

fn try_parse_memory(from_str: &str) -> anyhow::Result<u64> {
    <bytesize::ByteSize as FromStr>::from_str(from_str)
        .map(|res| res.as_u64())
        .map_err(anyhow::Error::msg)
}

/// The most CPUs `--virtual-cpus` presents: the bits of the 128-bit mask
/// Detcore's `sched_getaffinity` answers with, whose highest CPU is the
/// `kernel_max` (127) it reports.
pub const MAX_VIRTUAL_CPUS: u32 = 128;

fn parse_virtual_cpus(value: &str) -> Result<NonZeroU32, String> {
    value
        .parse::<u32>()
        .ok()
        .filter(|count| (1..=MAX_VIRTUAL_CPUS).contains(count))
        .and_then(NonZeroU32::new)
        .ok_or_else(|| {
            format!(
                "expected a CPU count from 1 to {MAX_VIRTUAL_CPUS}, for example --virtual-cpus 4"
            )
        })
}

/// Why a run may not rewrite exclusive loops in the guest's memory
/// ([`Config::rewrite_exclusives_in_memory_refusal`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RewriteExclusivesRefusal {
    /// The host does not execute the FEAT_LSE atomics the rewrite emits.
    HostLacksLseAtomics,
    /// Threads run concurrently, so one could execute code Detcore writes.
    ThreadsNotSequentialized,
}

impl std::fmt::Display for RewriteExclusivesRefusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::HostLacksLseAtomics => write!(
                f,
                "rewriting exclusive loops in the guest's memory rewrites them into FEAT_LSE \
                 atomic instructions, which this host does not execute (no HWCAP_ATOMICS); \
                 the rewritten image would raise SIGILL"
            ),
            Self::ThreadsNotSequentialized => write!(
                f,
                "rewriting exclusive loops in the guest's memory writes code that another \
                 thread could be running; run with threads sequentialized"
            ),
        }
    }
}

impl Config {
    /// How many CPUs the guest sees: `--virtual-cpus`, or Hermit's
    /// historical one.
    pub fn virtual_cpu_count(&self) -> u32 {
        self.virtual_cpus.map_or(1, NonZeroU32::get)
    }

    /// Whether the scheduler models which thread the kernel gives a signal,
    /// from the signal masks it reads from the kernel, and keeps the
    /// per-process ledger of signals discarded at send time; the guest side
    /// publishes a waiting thread's mask and records handler-restored
    /// real-time signals for it (`publish_wait_mask`,
    /// `record_handler_restore_mask`). It needs serialized threads, so that a
    /// mask it reads cannot change before the kernel acts on it, and a backend
    /// whose kernel signal targets a tool can predict
    /// (`BackendCapabilities::kernel_signal_targets_are_predictable`).
    pub fn models_signal_targets(&self) -> bool {
        self.sequentialize_threads && self.backend.kernel_signal_targets_are_predictable
    }

    /// Whether the scheduler reads a sleeper's signal mask and dispositions
    /// (`Scheduler::signal_sleep`): where it models the kernel's signal targets
    /// (`models_signal_targets`), or under the in-guest delivery gate. Elsewhere
    /// (DBT, SaBRe and LiteInst without the gate, KVM) a sleep parked as a
    /// timed waiter is woken by any signal the scheduler sends it, including a
    /// default-ignored `SIGCHLD` or a signal the sleeper blocks, which Linux
    /// never lets end a sleep.
    pub fn reads_sleeper_signal_state(&self) -> bool {
        self.models_signal_targets() || self.in_guest_signal_delivery
    }

    /// Whether `signal` is one of [`Config::in_guest_reserved_signals`]. Never
    /// signal 0 (an existence check sends nothing), nor a number outside 1 to
    /// 64, which Linux refuses with EINVAL before any target is named. Always
    /// false while the set is empty, which it is on every backend but the
    /// in-guest ones' default route.
    pub fn reserves_in_guest_signal(&self, signal: i64) -> bool {
        (1..=64).contains(&signal) && self.in_guest_reserved_signals & (1_u64 << (signal - 1)) != 0
    }

    /// This configuration with `change` applied to its backend capabilities.
    /// [`BackendCapabilities`] cannot be built field by field outside Reverie,
    /// so a caller that needs to adjust individual facts starts from the value
    /// already here.
    pub fn with_backend(mut self, change: impl FnOnce(&mut BackendCapabilities)) -> Self {
        change(&mut self.backend);
        self
    }

    /// Whether the epoch is the stable library default omitted by `Display`.
    pub fn has_default_epoch(&self) -> bool {
        self.epoch == DEFAULT_EPOCH_STR.parse::<DateTime<Utc>>().unwrap()
    }

    /// Replace the stable library/test default with the host wall clock captured
    /// by the outer `hermit run` invocation. The caller owns the single
    /// host-clock read boundary; all guest clock and metadata observations
    /// consume the resulting concrete epoch.
    pub fn capture_epoch_from_host_time(&mut self, now: SystemTime) {
        self.epoch = epoch_from_host_time(now);
    }

    /// Virtual nanoseconds one RCB takes at this clock multiplier and, under a
    /// chaos per-thread slowdown, at its largest factor: the most any thread's
    /// RCB takes, so a timeslice of `n` virtual nanoseconds is at least
    /// `n / largest_nanos_per_rcb()` RCBs for every thread.
    ///
    /// The two multipliers are combined first and then applied to
    /// `NANOS_PER_RCB`, the order of the one-RCB PMU epsilon that Detcore's
    /// post-hook compares with the end of the maximum
    /// (`post_handler_hook_observed` and `callback_checkpoint_expiry_due` in
    /// `detcore/src/lib.rs`), which [`Self::minimum_max_timeslice_nanos`]
    /// bounds. Floating-point multiplication is not associative: at a clock
    /// multiplier of 2.5 and a largest factor of 4.4, `(10 * 2.5) * 4.4` is one
    /// unit in the last place above 110, which would raise that minimum to 111.
    pub fn largest_nanos_per_rcb(&self) -> f64 {
        let slowdown = if self.chaos && self.chaos_per_thread_slowdown {
            self.chaos_slowdown_max_factor
        } else {
            1.0
        };
        NANOS_PER_RCB * (self.clock_multiplier.unwrap_or(1.0) * slowdown)
    }

    /// Smallest PMU-backed maximum representable by one RCB at this clock multiplier.
    pub fn minimum_max_timeslice_nanos(&self) -> u64 {
        (self.largest_nanos_per_rcb().ceil() as u64).max(NANOS_PER_RCB as u64)
    }

    /// Check invariants that must hold at every execution boundary without mutating the config.
    pub fn validate_invariants(&self) {
        assert!(self.sched_sticky_random_param >= 0.0);
        assert!(self.sched_sticky_random_param <= 1.0);
        // AUTONOMOUS-BOT-IMPLEMENTED
        // TODO-HUMAN-REVIEW(PR-1149)
        assert!(
            self.chaos_slowdown_max_factor.is_finite()
                && self.chaos_slowdown_max_factor >= 1.0
                && self.chaos_slowdown_max_factor <= RcbTimeMultiplier::MAX,
            "chaos_slowdown_max_factor must be finite and in [1.0, {}], got {}",
            RcbTimeMultiplier::MAX,
            self.chaos_slowdown_max_factor
        );
        if let Some(multiplier) = self.clock_multiplier {
            assert!(
                multiplier.is_finite() && multiplier > 0.0,
                "clock_multiplier must be finite and positive"
            );
        }
        let minimum_max_timeslice = self.minimum_max_timeslice_nanos();
        assert!(
            self.max_timeslice
                .is_none_or(|timeslice| u64::from(timeslice) >= minimum_max_timeslice),
            "max_timeslice must be at least one RCB ({} virtual nanoseconds)",
            minimum_max_timeslice
        );
    }

    /// Whether this run may rewrite exclusive loops in the guest's memory,
    /// given whether the host executes FEAT_LSE atomics. `None` admits the
    /// run. Every launch path checks this at Detcore's operational
    /// admission, so a library caller gets the CLI's refusal without
    /// running the CLI's validator: without LSE a rewritten site would
    /// raise SIGILL, and with threads running concurrently one could be
    /// inside the code while Detcore writes it.
    pub fn rewrite_exclusives_in_memory_refusal(
        &self,
        host_has_lse_atomics: bool,
    ) -> Option<RewriteExclusivesRefusal> {
        if !self.rewrite_exclusives_in_memory {
            return None;
        }
        if !self.sequentialize_threads {
            return Some(RewriteExclusivesRefusal::ThreadsNotSequentialized);
        }
        if !host_has_lse_atomics {
            return Some(RewriteExclusivesRefusal::HostLacksLseAtomics);
        }
        None
    }

    /// Sanity check the flags, and update any wherever flag B is implied by A.
    pub fn validate(&mut self) {
        self.validate_invariants();

        if self.die_on_desync {
            // Retired (dh-mvz7y): strict replay is the default, so the flag
            // is a no-op; warn once so scripts still passing it notice.
            static WARNED: std::sync::Once = std::sync::Once::new();
            WARNED.call_once(|| {
                eprintln!(
                    "warning: --die-on-desync is retired and has no effect; replay is strict by default"
                );
            });
            self.die_on_desync = false;
        }

        // TODO(T124429978) Restore the eprintln! calls below to tracing::warn! when the tracing
        // subscriber is set up early enough for these warnings to print.

        if self.record_preemptions_to.is_some() {
            self.record_preemptions = true;
        }
        // TODO: separate out recording flags: --record-preemptions vs --record-schedule-trace
        // if self.record_preemptions && !self.chaos {
        //     tracing::warn!(
        //         "Setting --record-preemptions when not in chaos mode doesn't do anything."
        //     );
        // }

        if self.replay_schedule_from.is_some() && self.replay_preemptions_from.is_some() {
            panic!("Cannot set both --replay-preemptions-from and --replay-schedule-from!!");
        }

        if self.chaos {
            self.sequentialize_threads = true;
        }

        if self.replay_preemptions_from.is_some() && self.imprecise_timers {
            eprintln!(
                "WARNING: Setting --imprecise timers with --replay-preemptions-from is probably not what you want. They won't replay precisely."
            );
        }

        if self.stop_after_turn.is_some() && !self.sequentialize_threads {
            eprintln!(
                "WARNING: --stop-after-turn will have no effect if --no-sequentialize-threads is enabled"
            );
            self.stop_after_turn = None;
        }
        if self.stop_after_iter.is_some() && !self.sequentialize_threads {
            eprintln!(
                "WARNING: --stop-after-iter will have no effect if --no-sequentialize-threads is enabled"
            );
            self.stop_after_iter = None;
        }
        if self.signal_at.is_some() && !self.sequentialize_threads {
            eprintln!(
                "WARNING: --signal-at will have no effect if --no-sequentialize-threads is enabled"
            );
            self.signal_at = None;
        }

        if self.debug_externalize_sockets && !self.sequentialize_threads {
            eprintln!(
                "WARNING: --debug-externalize-sockets will have no effect if --no-sequentialize-threads is enabled"
            );
            self.debug_externalize_sockets = false;
        }

        if !self.stacktrace_event.is_empty()
            && !self.record_preemptions
            && self.replay_schedule_from.is_none()
        {
            eprintln!(
                "WARNING: -s/--stacktrace-event has no effect if not recording/replaying events!"
            );
        }

        if self.preemption_stacktrace_log_file.is_some() {
            self.preemption_stacktrace = true;
        }
    }

    /// Whether configured preemption can use this backend's timer mechanism.
    ///
    /// This is independent of RCB accounting and does not certify a clock.
    pub fn use_preemption_timer(&self) -> bool {
        self.max_timeslice.is_some() && self.backend.delivers_preemption_timer_events
    }

    /// Whether a thread that is the only one able to run may skip its
    /// timeslice-end stops and have them processed when it next enters
    /// Detcore (see [`Config::eager_lone_preemptions`]). Only where a
    /// timeslice end reads nothing from the guest but its branch count, and
    /// nothing but the thread's next entry could act before the next one:
    /// precise PMU timers keyed to retired branches at one virtual-time rate,
    /// no slice-boundary protocol, no stack traces at preemptions or events,
    /// no turn-numbered triggers (`--stop-after-turn`, `--stop-after-iter`,
    /// `--signal-at`), no happens-before program, no manual interrupt points,
    /// no syscall-boundary deadline (`--target-timeslice`) and no instruction
    /// offsets ([`Config::slice_ends_need_a_stop`]: an offset is stepped from a
    /// real stop at its branch target, and processing a skipped end can draw
    /// one). Never with a gdbserver either: a debugger is served at the stops,
    /// and between skipped ends it would see Detcore's slice bookkeeping lag
    /// the guest's program counter. A recorded schedule writes such an end
    /// without the RIP it has no stop to read (`SchedEvent::stopless`), and a
    /// schedule replay takes without a stop only the ends its recording took
    /// that way. Detcore's `replay_allows_stopless_end` decides each end of a
    /// replay, including the per-thread rates and the instruction offsets a
    /// preemption record may carry.
    pub fn replays_lone_preemptions(&self) -> bool {
        !self.eager_lone_preemptions
            && self.use_preemption_timer()
            && self.use_rcb_time()
            && self.sequentialize_threads
            && !self.imprecise_timers
            && !self.chaos_per_thread_slowdown
            && !self.strict_schedule_boundaries
            && self.target_timeslice.is_none()
            && !self.preemption_stacktrace
            && self.stacktrace_event.is_empty()
            && self.stop_after_turn.is_none()
            && self.stop_after_iter.is_none()
            && self.signal_at.is_none()
            && self.happens_before.is_none()
            && self.interrupt_at.is_empty()
            && !self.slice_ends_need_a_stop()
            && !self.gdbserver
    }

    /// Should we use RCB in computing logical time?
    ///
    /// The answer is NO either if `--no-rcb-time` is specified or if HW counters are disabled by
    /// setting `--max-timeslice=disabled`.
    pub fn use_rcb_time(&self) -> bool {
        self.max_timeslice.is_some() && !self.no_rcb_time
    }

    /// Whether every chaos timeslice end needs the guest really stopped at
    /// its PMU branch target: `--chaos-instruction-offset` single-steps from
    /// that stop to where the timeslice ends. Any optimization that takes a
    /// slice end without stopping the guest there (for example a lone
    /// runnable thread's slice end processed at its branch count, which
    /// [`Config::replays_lone_preemptions`] turns off) must be off while this
    /// holds, or the offset is lost and a replay of the record no longer
    /// stops where the recorded run did.
    pub fn slice_ends_need_a_stop(&self) -> bool {
        self.chaos && self.chaos_instruction_offset > 0
    }

    /// Whether chaos mode makes this run's scheduling decisions: `--chaos`,
    /// unless the run replays a recorded schedule (`--replay-schedule-from`).
    ///
    /// Chaos draws a thread's timeslice ends and its new priority at each of
    /// them, and lets a `sched_yield` loop open a yield epoch. A schedule
    /// replay takes those decisions from its record instead: it ends a slice
    /// where the record does and runs the thread the record ran. A chaos draw
    /// there would end a slice the record does not have, and the extra
    /// scheduler turn moves the virtual clock the guest reads, so a replay
    /// with `--chaos` makes exactly the decisions a replay without it makes.
    /// That holds past the end of the schedule too: the rest of the run is
    /// scheduled as a run without `--chaos`, as a replay without it is.
    ///
    /// What chaos does to virtual time itself (`--chaos-per-thread-slowdown`)
    /// is not a scheduling decision and stays on. A replay computes those
    /// factors from its own scheduler seed and slowdown options, so it
    /// reproduces the recorded run's virtual time only when it is given the
    /// same ones.
    ///
    /// Nor does this predicate cover what `--fuzz-seed` draws: targeted chaos
    /// (`--chaos-target-races`: the thread a process-directed signal goes to,
    /// and whether a force-unblocked thread is requeued at the front) and
    /// `--fuzz-futexes`. The scheduler reads those flags, not `--chaos`, so
    /// adding `--chaos` to a schedule replay changes none of their draws. The
    /// record does not carry them; a replay draws them again from its own
    /// `--fuzz-seed`, so it reproduces them only when it is given the
    /// recorded run's.
    pub fn chaos_draws_schedule(&self) -> bool {
        self.chaos && self.replay_schedule_from.is_none()
    }

    /// Whether a `FUTEX_WAKE` that woke `woken` waiters makes the waker yield
    /// to them (`--futex-wake-yields`).
    ///
    /// A thread that releases a contended lock usually keeps running: the
    /// waiter it woke is queued behind it, and the waker can re-take the lock,
    /// uncontended, many times before that waiter runs.
    /// glibc and Rust mutexes both let a releasing thread barge back in, and on
    /// real hardware the woken thread wins on another CPU within the release
    /// window. Under sequentialized threads it never gets one, so QEMU's
    /// multi-threaded TCG vCPUs starved its main loop of the big QEMU lock for
    /// whole PMU slices at a time
    /// (<https://github.com/rrnewton/hermit/issues/3874>). When this holds, the
    /// scheduler moves the waker to the back of its priority band, and the
    /// waker takes a yield turn at the syscall's post-hook, so woken threads
    /// of its priority run before it continues (it keeps its band; `--chaos`
    /// can redraw priorities, and a random heuristic does not select first
    /// in, first out). The yield leaves the waker's timeslice alone.
    ///
    /// A thread switch is not free in virtual time: every scheduler decision
    /// is charged, so a policy that switches more often moves time that the
    /// guest observes. A QEMU guest kernel that calibrates its TSC against the
    /// emulated PIT failed that calibration with this policy, because the
    /// main loop now ran in the middle of it.
    ///
    /// The decision reads only the configuration and the wake's result, the
    /// number of waiters the scheduler took off the futex in the waker's turn,
    /// which is already deterministic. A wake that woke no one keeps the
    /// slice. Replay applies the policy exactly as the recorded run did: a
    /// preemption record holds slice ends but not this queue order, so the
    /// requeue and the yield must happen again for the replayed run to reach
    /// the same schedule. Because the yield consumes no slice end and draws
    /// nothing from the thread's chaos PRNG, making it again keeps the replay
    /// aligned with its record.
    pub fn yields_after_futex_wake(&self, woken: u64) -> bool {
        self.futex_wake_yields && woken > 0
    }

    /// The virtual nanoseconds each committed scheduler turn adds to the
    /// global clock: `--scheduler-turn-cost` (default [`NANOS_PER_SCHED`])
    /// times `--clock-multiplier`, truncated as it always was. A configured
    /// cost must give a charge of at least one nanosecond, because the charge
    /// is what advances the clock through turns that run no guest code, such
    /// as the retries of a finite poll; a scaled charge of zero would leave
    /// such a timeout unable to expire. A configured cost, or its scaled
    /// charge, above [`MAX_SCHEDULER_TURN_COST`] is refused too, so that a
    /// configured charge never exceeds one second per turn.
    pub fn scheduler_turn_charge(&self) -> Result<u64, String> {
        let multiplier = self.clock_multiplier.unwrap_or(1.0);
        let Some(cost) = self.scheduler_turn_cost else {
            return Ok((NANOS_PER_SCHED * multiplier) as u64);
        };
        let cost = u64::from(cost);
        if cost > MAX_SCHEDULER_TURN_COST {
            return Err(format!(
                "--scheduler-turn-cost must be at most {MAX_SCHEDULER_TURN_COST} virtual \
                 nanoseconds (received {cost})"
            ));
        }
        let charge = cost as f64 * multiplier;
        if !(1.0..=MAX_SCHEDULER_TURN_COST as f64).contains(&charge) {
            return Err(format!(
                "--scheduler-turn-cost {cost} at --clock-multiplier {multiplier} charges {charge} \
                 virtual nanoseconds per scheduler turn; the charge must be at least 1, so that \
                 timed waits keep advancing, and at most {MAX_SCHEDULER_TURN_COST}"
            ));
        }
        Ok(charge as u64)
    }

    /// Why `post_crash_timeslice`, which is set, would not be in effect under
    /// this configuration ([`Self::post_crash_timeslice_in_effect`]), or
    /// `None` when it is, or is not set.
    pub fn post_crash_timeslice_refusal(&self) -> Option<String> {
        let bound = self.post_crash_timeslice?;
        if self.post_crash_timeslice_in_effect().is_some() {
            return None;
        }
        Some(if !self.sequentialize_threads {
            "the switch needs sequentialized threads".to_owned()
        } else {
            match self.max_timeslice {
                None => "the switch needs a --max-timeslice".to_owned(),
                Some(max) => format!(
                    "a bound of {bound} is not above --max-timeslice={max}; replay with the \
                     recording's --max-timeslice"
                ),
            }
        })
    }

    /// The post-crash bound this run switches to (`post_crash_timeslice`), or
    /// `None` when the switch is off: unset, without sequentialized threads
    /// (there is no scheduler turn to switch at), without a `max_timeslice`,
    /// or not above it.
    pub fn post_crash_timeslice_in_effect(&self) -> Option<NonZeroU64> {
        let bound = self.post_crash_timeslice?;
        let run = self.max_timeslice?;
        (self.sequentialize_threads && bound > run).then_some(bound)
    }

    /// The `--pipe-wake-latency` in effect (<https://github.com/rrnewton/hermit/issues/4380>): the
    /// option's value in run mode, with sequentialized threads and a heuristic that selects by
    /// priority; `None` everywhere else, so nothing of the hand-off runs.
    pub fn pipe_wake_latency_in_effect(&self) -> Option<u64> {
        self.pipe_wake_latency.filter(|_| {
            self.use_nonblocking_sockets()
                && !self.recordreplay_modes
                && self.sched_heuristic.is_priority_ordered()
        })
    }

    /// Whether a run of consecutive turns that each end an expired timed wait is charged one
    /// scheduler turn (`--expired-wait-charge`): the explicit value, or by default outside race
    /// search (`--chaos`, or a `--sched-heuristic` that does not select by priority). Never under
    /// `hermit record` and `hermit replay`. A replay that leaves the option unset takes its
    /// record's value instead (`Scheduler::new`).
    pub fn expired_wait_runs_charged_once(&self) -> bool {
        !self.recordreplay_modes
            && match self.expired_wait_charge {
                Some(charge) => charge == ExpiredWaitCharge::Once,
                None => !self.chaos && self.sched_heuristic.is_priority_ordered(),
            }
    }

    /// Whether a queued plain sleep is registered as a timed waiter at the first quiescent pass
    /// that finds it queued (`--sleep-registration`): the explicit value, or by default outside
    /// race search (`--chaos`, or a `--sched-heuristic` that does not select by priority) where
    /// the scheduler reads a sleeper's signal state (`reads_sleeper_signal_state`). Never under
    /// `hermit record` and `hermit replay`, whose schedule must not change. A replay that leaves
    /// the option unset takes its record's value instead (`Scheduler::new`).
    ///
    /// A registered sleep is parked, and a signal the scheduler sends a parked sleep wakes it
    /// whatever the signal is unless the scheduler can read the sleeper's mask and dispositions
    /// (`Scheduler::signal_sleep`). Without them (DBT, SaBRe and LiteInst without the in-guest
    /// gate, KVM), a default-ignored `SIGCHLD` or a blocked timer signal would end every plain
    /// sleep with `EINTR`, where Linux never wakes it. So the default registers when chosen on
    /// those backends, as before (<https://github.com/rrnewton/hermit/pull/4682>); an explicit
    /// `first-pass` there, or one taken from a record, is refused before the guest starts, and
    /// the scheduler does not register there whatever this says, so a record states what it did.
    pub fn queued_sleeps_registered_at_first_pass(&self) -> bool {
        !self.recordreplay_modes
            && match self.sleep_registration {
                Some(registration) => registration == SleepRegistration::FirstPass,
                None => {
                    !self.chaos
                        && self.sched_heuristic.is_priority_ordered()
                        && self.reads_sleeper_signal_state()
                }
            }
    }

    /// The settings of this run under which Detcore can record a descriptor
    /// without its file type, by their option names: without metadata
    /// virtualization it keeps no stat of a descriptor it opens, which is what
    /// tells a FIFO from a plain file. An access trace names them in its header
    /// (`TraceHeader::untyped_descriptors`).
    pub fn untyped_descriptor_settings(&self) -> Vec<String> {
        let mut settings = Vec::new();
        if !self.virtualize_metadata {
            settings.push("--no-virtualize-metadata".to_string());
        }
        settings
    }

    /// Should we convert sockets to SOCK_NONBLOCK?
    pub fn use_nonblocking_sockets(&self) -> bool {
        self.sequentialize_threads && !self.debug_externalize_sockets
    }

    /// Should we call trace_schedevent to trace each SchedEvent?
    /// This applies to both record and replay for scheduled events.
    pub fn should_trace_schedevent(&self) -> bool {
        self.record_preemptions || self.replay_schedule_from.is_some()
    }

    /// Whether this run produces and traces schedule events: the decision
    /// `should_trace_schedevent` makes for recording and schedule replay, and
    /// also replay of a callback-annotated priority history
    /// (`annotated_priority_replay`). Such a record was made with
    /// `--record-preemptions`, so its replay makes every decision this gates
    /// as its recording did, whether or not it writes a record, and its
    /// callback checkpoints compare against the progress those events count.
    pub fn observes_schedevents(&self) -> bool {
        self.should_trace_schedevent() || self.annotated_priority_replay
    }

    /// Why this effective configuration cannot replay a callback-annotated
    /// priority history, or `None` when it can. Its row ends are actual
    /// callback endpoints at exact RCB counts, guarded one RCB past each: that
    /// needs the backend's own Detcore preemption timer events
    /// (`delivers_preemption_timer_events`), sequentialized threads (the
    /// scheduler that grants each history and acknowledges each row), an
    /// active maximum timeslice, precise timers and RCB time. The answer
    /// names the first unmet requirement. It reads capabilities, never a
    /// backend's name, and never changes the configuration.
    pub fn annotated_priority_replay_unqualified(&self) -> Option<&'static str> {
        if !self.backend.delivers_preemption_timer_events {
            Some("cannot deliver Detcore preemption timer events")
        } else if !self.sequentialize_threads {
            Some("does not sequentialize threads (--no-sequentialize-threads)")
        } else if self.max_timeslice.is_none() {
            Some("has its preemption timer disabled (--max-timeslice=disabled)")
        } else if self.imprecise_timers {
            Some("requests imprecise timers (--imprecise-timers)")
        } else if self.no_rcb_time {
            Some("requests logical time without RCBs (--no-rcb-time)")
        } else {
            None
        }
    }

    /// Returns manual interuption points for a given thread
    pub fn interrupts_for_thread(&self, thread_id: DetTid) -> BTreeSet<u64> {
        self.interrupt_at
            .iter()
            .filter_map(|(tid, time)| {
                if tid.eq(&thread_id) {
                    Some(*time)
                } else {
                    None
                }
            })
            .collect::<BTreeSet<u64>>()
    }
}

impl fmt::Display for Config {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.virtualize_time {
            write!(f, " --no-virtualize-time")?;
        }
        if !self.virtualize_cpuid {
            write!(f, " --no-virtualize-cpuid")?;
        }
        // The command line's default is auto, so every named profile, v2
        // included, is written out: a rerun of this line runs that profile,
        // and never derives one again.
        if self.cpu_profile != crate::cpu_profile::CpuProfile::Auto {
            write!(f, " --cpu-profile={}", self.cpu_profile)?;
        }
        if !self.virtualize_metadata {
            write!(f, " --no-virtualize-metadata")?;
        }
        if self.passthru_opt {
            write!(f, " --passthru-opt")?;
        }
        if self.untrap_passthrough_syscalls {
            write!(f, " --untrap-passthrough-syscalls")?;
        }
        match self.runs_post_fork {
            RunsPostFork::Child => {}
            RunsPostFork::Parent => write!(f, " --runs-post-fork=parent")?,
            RunsPostFork::Random => write!(f, " --runs-post-fork=random")?,
        }
        if !self.has_default_epoch() {
            write!(f, " --epoch={}", self.epoch.to_rfc3339())?;
        }
        if self.seed != 0 {
            write!(f, " --seed={}", self.seed)?;
        }

        if let Some(rng_seed) = self.rng_seed {
            write!(f, " --rng-seed={}", rng_seed)?;
        }
        if let Some(fuzz_seed) = self.fuzz_seed {
            write!(f, " --fuzz-seed={}", fuzz_seed)?;
        }

        if self.fuzz_futexes {
            write!(f, " --fuzz-futexes")?;
        }
        if self.chaos_target_races {
            write!(f, " --chaos-target-races")?;
        }
        // AUTONOMOUS-BOT-IMPLEMENTED
        // TODO-HUMAN-REVIEW(PR-1149)
        if self.chaos_per_thread_slowdown {
            write!(f, " --chaos-per-thread-slowdown")?;
            write!(
                f,
                " --chaos-slowdown-max-factor={}",
                self.chaos_slowdown_max_factor
            )?;
            // AUTONOMOUS-BOT-IMPLEMENTED
            // TODO-HUMAN-REVIEW(PR-1151)
            if self.chaos_epoch_length_ns > 0 {
                write!(f, " --chaos-epoch-length-ns={}", self.chaos_epoch_length_ns)?;
            }
        }
        if self.chaos_instruction_offset > 0 {
            write!(
                f,
                " --chaos-instruction-offset={}",
                self.chaos_instruction_offset
            )?;
        }
        if let Some(m) = self.clock_multiplier {
            write!(f, " --clock-multiplier={}", m)?;
        }
        if self.imprecise_timers {
            write!(f, " --imprecise-timers")?;
        }
        if self.eager_lone_preemptions {
            write!(f, " --eager-lone-preemptions")?;
        }
        if !self.signal_state_copy {
            write!(f, " --no-signal-state-copy")?;
        }
        if self.chaos {
            write!(f, " --chaos")?;
        }
        if let Some(range) = self.chaos_timeslice_range {
            write!(f, " --chaos-timeslice-range={range}")?;
        }
        if self.chaos_timeslice_draw == ChaosTimesliceDraw::Run {
            write!(f, " --chaos-timeslice-draw=run")?;
        }
        if self.strict_schedule_boundaries {
            write!(f, " --strict-schedule-boundaries")?;
        }
        if self.record_preemptions {
            write!(f, " --record-preemptions")?;
        }

        if let Some(p) = &self.record_preemptions_to {
            let s = p.to_str().expect("valid unicode path");
            write!(f, " --record-preemptions-to={}", shell_words::quote(s))?;
        }
        if let Some(p) = &self.replay_preemptions_from {
            let s = p.to_str().expect("valid unicode path");
            write!(f, " --replay-preemptions-from={}", shell_words::quote(s))?;
        }
        if let Some(p) = &self.replay_schedule_from {
            let s = p.to_str().expect("valid unicode path");
            write!(f, " --replay-schedule-from={}", shell_words::quote(s))?;
        }
        if self.replay_exhausted_panic {
            write!(f, " --replay-exhausted-panic")?;
        }
        if self.replay_edited_preemptions {
            write!(f, " --replay-edited-preemptions")?;
        }
        if self.replay_continue_fairly {
            write!(f, " --replay-continue-fairly")?;
        }
        for (index, path) in &self.stacktrace_event {
            write!(f, " --stacktrace-event={}", index)?;
            if let Some(p) = path {
                let s = p.to_str().expect("valid unicode path");
                write!(f, ",{}", shell_words::quote(s))?;
            }
        }
        if let Some(p) = &self.access_trace {
            let s = p.to_str().expect("valid unicode path");
            write!(f, " --record-access-trace-to={}", shell_words::quote(s))?;
        }
        if self.access_trace_from != 0 {
            write!(f, " --access-trace-from={}", self.access_trace_from)?;
        }
        if let Some(records) = self.access_trace_max_records {
            write!(f, " --access-trace-max-records={}", records)?;
        }
        if let Some(bytes) = self.access_trace_max_bytes {
            write!(f, " --access-trace-max-bytes={}", bytes)?;
        }
        if self.stacktrace_at_schedule_positions {
            write!(f, " --stacktrace-at-schedule-positions")?;
        }
        for watch in &self.watch {
            write!(f, " --watch={}", watch)?;
        }
        if self.watch_from != 0 {
            write!(f, " --watch-from={}", self.watch_from)?;
        }
        if self.preemption_stacktrace {
            write!(f, " --preemption-stacktrace")?;
        }
        if self.panic_on_unsupported_syscalls {
            write!(f, " --panic-on-unsupported-syscalls")?;
        }
        if self.panic_on_rcb_overshoot {
            write!(f, " --panic-on-rbc-overshoot")?;
        }
        if self.kill_daemons {
            write!(f, " --kill-daemons")?;
        }
        if self.gdbserver {
            write!(f, " --gdbserver")?;
        }
        if self.gdbserver_port != /* default */ 1234u16 {
            write!(f, " --gdbserver-port={}", self.gdbserver_port)?;
        }
        match &self.max_timeslice {
            // The range sets the maximum itself (its MAX, or the value it
            // drew or a replayed record gave), and refuses an explicit one.
            Some(x)
                if self.chaos_timeslice_range.is_some()
                    && (self.chaos_timeslice_range.map(|range| range.max) == Some(*x)
                        || self.chaos_timeslice == Some(*x)) => {}
            Some(x) => {
                if *x != NonZeroU64::new(200_000_000).unwrap() {
                    write!(f, " --max-timeslice={}", x)?;
                }
            }
            None => {
                write!(f, " --max-timeslice=disabled")?;
            }
        }
        if let Some(target_timeslice) = self.target_timeslice {
            write!(f, " --target-timeslice={}", target_timeslice)?;
        }
        if self.target_timeslice_syscalls_only {
            write!(f, " --target-timeslice-syscalls-only")?;
        }
        if self.futex_wake_yields {
            write!(f, " --futex-wake-yields")?;
        }
        if let Some(latency) = self.pipe_wake_latency {
            write!(f, " --pipe-wake-latency={}", latency)?;
        }
        if let Some(charge) = self.expired_wait_charge {
            write!(f, " --expired-wait-charge={}", charge.as_str())?;
        }
        if let Some(registration) = self.sleep_registration {
            write!(f, " --sleep-registration={}", registration.as_str())?;
        }
        if let Some(report) = &self.work_span_report {
            write!(f, " --work-span-report={}", report.display())?;
        }
        if let Some(cost) = self.scheduler_turn_cost {
            write!(f, " --scheduler-turn-cost={}", cost)?;
        }
        if self.cap_preemption_charge {
            write!(f, " --cap-preemption-charge")?;
        }
        if let Some(bound) = self.virtual_timeout {
            write!(f, " --virtual-timeout={}", bound)?;
        }
        if self.sigint_instakill {
            write!(f, " --sigint-instakill")?;
        }
        if self.warn_non_zero_binds {
            write!(f, " --warn-non-zero-binds")?;
        }
        match &self.sched_heuristic {
            SchedHeuristic::None => {}
            SchedHeuristic::ConnectBind => {
                write!(f, " --sched-heuristic=connectbind")?;
            }
            SchedHeuristic::Random => {
                write!(f, " --sched-heuristic=random")?;
            }
            SchedHeuristic::StickyRandom => {
                write!(f, " --sched-heuristic=stickyrandom")?;
            }
        }
        if let Some(s) = self.sched_seed {
            write!(f, " --sched-seed={}", s)?;
        }
        if self.sched_sticky_random_param != 0.0 {
            write!(
                f,
                " --sched-sticky-random-param={}",
                self.sched_sticky_random_param
            )?;
        }
        if let Some(t) = self.stop_after_turn {
            write!(f, " --stop-after-turn={}", t)?;
        }
        if let Some(i) = self.stop_after_iter {
            write!(f, " --stop-after-iter={}", i)?;
        }
        if let Some(at) = self.signal_at {
            write!(f, " --signal-at={}", at)?;
        }
        if let Some(bound) = self.post_crash_timeslice {
            write!(f, " --post-crash-timeslice={}", bound)?;
        }
        if self.debug_externalize_sockets {
            write!(f, " --debug-externalize-sockets")?;
        }
        match &self.debug_futex_mode {
            BlockingMode::External => {
                write!(f, " --debug-futex-mode=external")?;
            }
            BlockingMode::Polling => {
                write!(f, " --debug-futex-mode=polling")?;
            }
            BlockingMode::Precise => { /* default */ }
        }
        if self.no_rcb_time {
            write!(f, " --no-rcb-time")?;
        }
        if self.detlog_heap {
            write!(f, " --detlog-heap")?;
        }
        if self.detlog_stack {
            write!(f, " --detlog-stack")?;
        }
        match self.detlog_anon {
            Some(DetlogAnon::Mappings) => write!(f, " --detlog-anon")?,
            Some(DetlogAnon::Pages) => write!(f, " --detlog-anon=pages")?,
            None => {}
        }
        if self.detlog_regs {
            write!(f, " --detlog-regs")?;
        }
        if self.detlog_regs_cadence != /* default */ 1 {
            write!(f, " --detlog-regs-cadence={}", self.detlog_regs_cadence)?;
        }
        if !self.detlog_io_buffers {
            write!(f, " --no-detlog-io-buffers")?;
        }
        if self.sysinfo_uptime_offset != /* default */ 120 {
            write!(f, " --sysinfo-uptime-offset={}", self.sysinfo_uptime_offset)?;
        }
        if self.memory != 1_000_000_000 {
            write!(f, " --memory={}", self.memory)?;
        }
        if let Some(cpus) = self.virtual_cpus {
            write!(f, " --virtual-cpus={cpus}")?;
        }
        match self.proc_level {
            ProcLevel::Minimal => {}
            ProcLevel::Standard => write!(f, " --proc=standard")?,
        }
        for (tid, rcb) in &self.interrupt_at {
            write!(f, " --interrupt-at={}:{}", tid, rcb)?;
        }
        Ok(())
    }
}

/// How much of `/proc` the guest sees (`--proc`). Each level contains the
/// one before it.
#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    Serialize,
    Deserialize,
    Parser,
    PartialEq,
    Eq,
    PartialOrd,
    Ord
)]
pub enum ProcLevel {
    /// The names most programs read, modelled on gVisor's `/proc`.
    #[default]
    Minimal,
    /// Every name Detcore serves or plans to serve.
    Standard,
}

impl FromStr for ProcLevel {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "minimal" => Ok(Self::Minimal),
            "standard" => Ok(Self::Standard),
            _ => Err(format!(
                "Expected minimal|standard, could not parse: {:?}",
                s
            )),
        }
    }
}

/// Which side of an ordinary fork/clone receives the first post-registration turn.
#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    Serialize,
    Deserialize,
    Parser,
    PartialEq,
    Eq
)]
pub enum RunsPostFork {
    /// Run the newly registered child before its parent resumes.
    #[default]
    Child,
    /// Allow the parent to resume before the newly registered child starts.
    Parent,
    /// Deterministically choose child-first or parent-first from the scheduler seed.
    Random,
}

impl FromStr for RunsPostFork {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "child" => Ok(Self::Child),
            "parent" => Ok(Self::Parent),
            "random" => Ok(Self::Random),
            _ => Err(format!(
                "Expected Child|Parent|Random, could not parse: {:?}",
                s
            )),
        }
    }
}

/// How should we handle syscalls which may block, but are internal to the hermit container?
/// These syscalls are determinizable, but there are multiple methods of doing so.
/// These choices *do not* apply to blocking syscalls that wait for external conditions outside the
/// container, such as network responses.
///
/// Mostly it helps to switch this as: (1) a debugging aid to figure out what is going wrong with a
/// given guest program, or (2) in order to find the more performant mode for a given guest program.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Parser, PartialEq, Eq)]
pub enum BlockingMode {
    /// Handle the internal blocking syscall as though it was external, and unblocks at an
    /// unpredictable nondeterministic time.  These blocked threads will be parked in the
    /// scheduler's BlockedPool.
    ///
    /// (TODO: In the future these scheduling decisions will be recorded, and this comment needs to
    /// be updated accordingly.)
    External,
    /// Transform each blocking syscall into non-blocking, and then the scheduler will use that
    /// non-blocking form to repeatedly poll for completion of the operation.  When polling occurs
    /// (and the backoff policy there on) is decided by the scheduler.
    /// See NOTE [Blocking Syscalls via Internal Polling] in this folder.
    Polling,
    /// Precisely model the blocking and unblocking behavior inside hermit.
    /// TODO: This work is not completed yet for all forms of blocking syscalls.
    Precise,
}

impl FromStr for BlockingMode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "polling" => Ok(BlockingMode::Polling),
            "precise" => Ok(BlockingMode::Precise),
            "external" => Ok(BlockingMode::External),
            _ => Err(format!(
                "Expected Polling|Precise|External, could not parse: {:?}",
                s
            )),
        }
    }
}

/// How a run of consecutive scheduler turns that each end an expired timed wait is charged while
/// another thread is queued (`--expired-wait-charge`).
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    Serialize,
    Deserialize,
    clap::ValueEnum
)]
#[serde(rename_all = "lowercase")]
pub enum ExpiredWaitCharge {
    /// Every expired wait is charged a scheduler turn.
    Each,
    /// A run of expired waits is charged one scheduler turn.
    Once,
}

impl ExpiredWaitCharge {
    /// The rule a run follows when it charges a run of expired waits once
    /// (`once`) or not (`each`).
    pub fn from_charged_once(once: bool) -> Self {
        if once { Self::Once } else { Self::Each }
    }

    /// The option's value as `--expired-wait-charge` spells it.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Each => "each",
            Self::Once => "once",
        }
    }
}

/// When a queued plain sleep becomes a timed waiter (`--sleep-registration`).
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Hash,
    Serialize,
    Deserialize,
    clap::ValueEnum
)]
#[serde(rename_all = "kebab-case")]
pub enum SleepRegistration {
    /// At the first quiescent scheduler pass that finds the sleep queued.
    FirstPass,
    /// Only when the sleeper's thread is chosen before its deadline.
    WhenChosen,
}

impl SleepRegistration {
    /// The rule a run follows when it registers queued sleeps at the first
    /// pass (`first-pass`) or not (`when-chosen`).
    pub fn from_registered_at_first_pass(first_pass: bool) -> Self {
        if first_pass {
            Self::FirstPass
        } else {
            Self::WhenChosen
        }
    }

    /// The option's value as `--sleep-registration` spells it.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::FirstPass => "first-pass",
            Self::WhenChosen => "when-chosen",
        }
    }
}

#[derive(
    Debug,
    Default,
    Clone,
    Copy,
    Serialize,
    Deserialize,
    Parser,
    PartialEq,
    Eq
)]
/// Apply a specialized scheduling heuristic which may help exercise certain bugs.
pub enum SchedHeuristic {
    /// Don't modify the scheduling algorithm.
    // TODO: Is the default a round robin?
    #[default]
    None,
    /// Prioritize connect and deprioritize bind to exercise races
    ConnectBind,
    /// Random: Randomly pick any available thread to make progress.
    Random,
    /// Sticky Random: Randomly pick any available thread. On the next round,
    /// and after the thread is parked, randomly choose if we will continue
    /// executing on the same thread, or picking another one.
    StickyRandom,
    // TODO: make all sleeps "instant".
}

impl SchedHeuristic {
    /// Whether selection follows priority order: `None` and `ConnectBind` pop
    /// the first queued entry, so a backed-off poller can be starved; `Random`
    /// and `StickyRandom` draw a uniform index into the queue and never are.
    /// The scheduler's run queue and `hermit run`'s refusals share this one
    /// definition.
    pub fn is_priority_ordered(self) -> bool {
        matches!(self, SchedHeuristic::None | SchedHeuristic::ConnectBind)
    }
}

// Lame to not derive this, but even `derive_more` won't do enums.
impl FromStr for SchedHeuristic {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "none" | "roundrobin" => Ok(SchedHeuristic::None),
            "connectbind" => Ok(SchedHeuristic::ConnectBind),
            "random" => Ok(SchedHeuristic::Random),
            "stickyrandom" => Ok(SchedHeuristic::StickyRandom),
            _ => Err(format!(
                "Expected None|ConnectBind|Random|StickyRandom, could not parse: {:?}",
                s
            )),
        }
    }
}

/// An optional virtual-timeslice duration. `None` disables that preemption mechanism.
pub type MaybeTimeslice = Option<NonZeroU64>;

/// How `--chaos-timeslice-range` draws (`--chaos-timeslice-draw`).
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Default,
    Serialize,
    Deserialize,
    clap::ValueEnum
)]
#[serde(rename_all = "kebab-case")]
pub enum ChaosTimesliceDraw {
    /// Each chaos slice draws its mean from the range.
    #[default]
    Slice,
    /// The run draws its `--max-timeslice` from the range once.
    Run,
}

/// The range used by `--chaos-timeslice-draw=run` when no range is given.
/// One microsecond to 200 milliseconds spans fine preemption windows and
/// the ordinary maximum. Plain `--chaos` keeps its fixed maximum: fine draws
/// can cost much more, so this discovery mode remains explicit.
pub const RECOMMENDED_CHAOS_TIMESLICE_RANGE: TimesliceRange = TimesliceRange {
    min: NonZeroU64::new(1_000).unwrap(),
    max: NonZeroU64::new(200_000_000).unwrap(),
};

/// Long help for `--chaos`, including the explicit discovery mode.
pub fn chaos_long_help() -> String {
    let range = RECOMMENDED_CHAOS_TIMESLICE_RANGE;
    format!(
        "Schedule threads chaotically.\n\n\
         The behavior of this flag is subject to change. Current behavior is to randomize thread \
         priorities at every logical timeslice. Other randomization strategies are possible with \
         `--sched-heuristic`.\n\n\
         Thread scheduling remains deterministic, determined by the random seed.\n\n\
         By default, every chaos slice's mean is the run's `--max-timeslice` (200000000 virtual \
         nanoseconds unless given), so one seed sweep explores mostly one slice scale. For race \
         discovery, sweep seeds with `--chaos-timeslice-draw=run`: each run draws its \
         `--max-timeslice` once, log-uniformly from the recommended range {range} virtual \
         nanoseconds (or from `--chaos-timeslice-range`), as a pure function of the scheduler \
         seed. Fine draws explore short preemption windows, but can take orders of magnitude \
         longer: size time limits for them. A replay adopts a recorded drawn maximum without \
         drawing again. See `--chaos-timeslice-draw`."
    )
}

/// Long help for the draw policy, sharing the recommended range with resolution.
pub fn chaos_timeslice_draw_long_help() -> String {
    let range = RECOMMENDED_CHAOS_TIMESLICE_RANGE;
    format!(
        "How a chaos run draws its timeslice. Requires `--chaos`.\n\n\
         `run` (the discovery mode) draws the run's `--max-timeslice` once, log-uniformly from \
         `--chaos-timeslice-range`, or from the recommended range {range} virtual nanoseconds \
         (1 us to 200 ms) without an explicit range. The draw is a pure function of the scheduler \
         seed; the run is then an ordinary chaos run under it, so a seed sweep spreads its seeds \
         over the range, each seed at one scale. Without a seed, every run uses seed 0 and draws \
         the same value. Fine draws can be much slower: size time limits for the fine end.\n\n\
         Fine draws also incur frequent scheduler-turn charges in guest-visible time, which can \
         trigger a guest's own watchdogs. See `--cap-preemption-charge` for the explicit option \
         that caps expired-timeslice charges. The range parser requires MIN to be at least \
         10 virtual nanoseconds and no larger than MAX.\n\n\
         `--summary-json` reports the drawn value (`preemption.max_timeslice_ns`), and a \
         `--record-preemptions-to` record stores it. A replay adopts a recorded drawn maximum \
         without drawing again; it needs no timeslice option. A legacy record with no drawn \
         value does not override the requested draw policy. `hermit analyze` and `hermit bisect` \
         take a recorded value and refuse `run` in their run arguments, since all trials must \
         share one maximum.\n\n\
         `slice` (the default) draws each slice's mean from an explicitly supplied range; it is \
         what a range alone does. Without a range or an explicit `run` draw, the ordinary fixed \
         maximum remains unchanged."
    )
}

/// The bounds of `--chaos-timeslice-range`, in virtual nanoseconds, both
/// included: `MIN..MAX`, with `MIN` at least one RCB and no larger than `MAX`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct TimesliceRange {
    pub min: NonZeroU64,
    pub max: NonZeroU64,
}

impl TimesliceRange {
    /// The point of the range a uniform draw `u` in `[0, 1)` maps to under a
    /// log-uniform law: `exp(ln MIN + u (ln MAX - ln MIN))`, in virtual
    /// nanoseconds. Each decade of the range gets an equal share of draws.
    pub fn log_uniform(&self, u: f64) -> f64 {
        let (low, high) = ((self.min.get() as f64).ln(), (self.max.get() as f64).ln());
        (low + u * (high - low))
            .exp()
            .clamp(self.min.get() as f64, self.max.get() as f64)
    }
}

impl FromStr for TimesliceRange {
    type Err = String;
    fn from_str(src: &str) -> Result<Self, Self::Err> {
        let (min, max) = src
            .split_once("..")
            .ok_or_else(|| format!("expected MIN..MAX virtual nanoseconds, got {src:?}"))?;
        let bound = |text: &str| -> Result<NonZeroU64, String> {
            let value = text
                .parse::<u64>()
                .map_err(|error| format!("{text:?} is not a number of nanoseconds: {error}"))?;
            if value < NANOS_PER_RCB as u64 {
                return Err(format!(
                    "{value} ns is below one RCB ({} virtual nanoseconds), the shortest PMU-backed \
                     timeslice",
                    NANOS_PER_RCB as u64
                ));
            }
            Ok(NonZeroU64::new(value).expect("at least one RCB"))
        };
        let (min, max) = (bound(min)?, bound(max)?);
        if min > max {
            return Err(format!("the range {min}..{max} is empty: MIN exceeds MAX"));
        }
        Ok(Self { min, max })
    }
}

// A saved or legacy configuration is checked as the command line is.
impl TryFrom<String> for TimesliceRange {
    type Error = String;
    fn try_from(text: String) -> Result<Self, Self::Error> {
        text.parse()
    }
}

impl From<TimesliceRange> for String {
    fn from(range: TimesliceRange) -> Self {
        range.to_string()
    }
}

impl std::fmt::Display for TimesliceRange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}..{}", self.min, self.max)
    }
}

/// Deprecated name for an optional PMU-backed virtual-timeslice duration.
#[deprecated(note = "use MaybeTimeslice")]
pub type MaybePreemptionTimeout = MaybeTimeslice;

/// A signal sent to the run's root process at a scheduler turn
/// (`Config::signal_at`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SignalAt {
    /// The scheduler turn after whose commit the signal is sent.
    pub turn: u64,
    /// The signal's number.
    pub signal: i32,
}

impl std::fmt::Display for SignalAt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "turn={}:{}", self.turn, self.signal)
    }
}

/// Parse `turn=N:SIGNAL` (`Config::signal_at`).
fn parse_signal_at(src: &str) -> Result<SignalAt, String> {
    let rest = src
        .strip_prefix("turn=")
        .ok_or_else(|| format!("expected turn=N:SIGNAL, got {src:?}"))?;
    let (turn, signal) = rest
        .split_once(':')
        .ok_or_else(|| format!("expected turn=N:SIGNAL, got {src:?}"))?;
    let turn = turn
        .parse::<u64>()
        .map_err(|e| format!("the turn in {src:?} is not a non-negative integer: {e}"))?;
    let signal = match signal.parse::<i32>() {
        Ok(number) => nix::sys::signal::Signal::try_from(number),
        Err(_) => signal.parse::<nix::sys::signal::Signal>(),
    }
    .map_err(|_| format!("{signal:?} in {src:?} names no signal"))?;
    // Neither can be caught, blocked or ignored, so Linux acts on them in the
    // kernel at once: SIGKILL retires the root outside the scheduler's
    // SIGKILL retirement protocol, and SIGSTOP stops it outside the schedule.
    if matches!(
        signal,
        nix::sys::signal::Signal::SIGKILL | nix::sys::signal::Signal::SIGSTOP
    ) {
        return Err(format!(
            "{signal} in {src:?} cannot be caught or blocked; --signal-at sends only \
             signals the guest can handle"
        ));
    }
    Ok(SignalAt {
        turn,
        signal: signal as i32,
    })
}

fn parse_timeslice(src: &str) -> Result<MaybeTimeslice, ParseTimesliceError> {
    if let Ok(n) = src.parse::<u64>() {
        if n != 0 && n < NANOS_PER_RCB as u64 {
            Err(ParseTimesliceError::new(
                "PMU-backed timeslices must be at least one RCB (10 virtual nanoseconds)",
            ))
        } else {
            Ok(NonZeroU64::new(n))
        }
    } else {
        match src {
            "disabled" => Ok(None),
            _ => Err(ParseTimesliceError::new(
                "Unable to parse timeslice, expected disabled or a non-negative integer",
            )),
        }
    }
}

/// The default `--max-timeslice`, in virtual nanoseconds, which
/// `--post-crash-timeslice=default` names.
pub const DEFAULT_MAX_TIMESLICE: u64 = 200_000_000;

fn parse_post_crash_timeslice(src: &str) -> Result<MaybeTimeslice, ParseTimesliceError> {
    match src {
        "off" => Ok(None),
        "default" => Ok(NonZeroU64::new(DEFAULT_MAX_TIMESLICE)),
        _ => match parse_timeslice(src) {
            Ok(Some(bound)) => Ok(Some(bound)),
            _ => Err(ParseTimesliceError::new(
                "expected off, default, or a timeslice of at least one RCB (10 virtual \
                 nanoseconds)",
            )),
        },
    }
}

fn parse_index_with_path(src: &str) -> Result<(u64, Option<PathBuf>), String> {
    let convert = |e| format!("Failed to parse int index before comma: {e}");
    if let Some((index_str, path)) = src.split_once(',') {
        let ix = index_str.parse::<u64>().map_err(convert)?;
        let pathbuf = PathBuf::from_str(path).map_err(|_| "the impossible happened")?;
        Ok((ix, Some(pathbuf)))
    } else {
        let ix = src.parse::<u64>().map_err(convert)?;
        Ok((ix, None))
    }
}

#[derive(Debug)]
struct ParseTimesliceError {
    details: String,
}

impl fmt::Display for ParseTimesliceError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.details)
    }
}

impl ParseTimesliceError {
    fn new(msg: &str) -> ParseTimesliceError {
        ParseTimesliceError {
            details: msg.to_string(),
        }
    }
}

impl std::error::Error for ParseTimesliceError {
    fn description(&self) -> &str {
        &self.details
    }
}

/// The default epoch used by DetCore for things like initial file modtimes.
///
/// N.B. Default to a reasonable date. Some programs (like zip) have trouble with the
/// original unix epoch (time zero).
pub static DEFAULT_EPOCH_STR: &str = "2026-01-01T00:00:00Z";

pub use crate::canonical_test_epoch::CANONICAL_TEST_EPOCH;
pub use crate::canonical_test_epoch::CANONICAL_TEST_EPOCH_ARG;

/// Convert one invocation's captured host instant without losing subsecond
/// precision. CLI and comparison orchestration share this input conversion;
/// guest clock progression never reads the host clock through it.
pub fn epoch_from_host_time(now: SystemTime) -> DateTime<Utc> {
    DateTime::<Utc>::from(now)
}

impl Config {
    /// Construct the config using environment variables only, not CLI args.
    pub fn from_env() -> Self {
        let args: [OsString; 2] = [
            OsString::from("CMD"), // Silly/unused.
            OsString::from(format!("--epoch={}", DEFAULT_EPOCH_STR)),
        ];
        Config::parse_from(args.iter())
    }

    /// Returns effective "rng-seed" parameter taking in account "seed"
    /// parameter if former isn't specified
    pub fn rng_seed(&self) -> u64 {
        self.rng_seed.unwrap_or(self.seed)
    }

    /// Returns the fuzz_seed, as specified by the user or defaulting to the primary seed if
    /// unspecified.
    pub fn fuzz_seed(&self) -> u64 {
        self.fuzz_seed.unwrap_or(self.seed)
    }

    /// Returns effective "sched-seed" parameter taking in account "seed"
    /// parameter if former isn't specified
    pub fn sched_seed(&self) -> u64 {
        self.sched_seed.unwrap_or(self.seed)
    }
}

/// The largest `--scheduler-turn-cost` accepted, in virtual nanoseconds, both
/// before and after the clock multiplier scales it: one second per scheduler
/// turn.
pub const MAX_SCHEDULER_TURN_COST: u64 = 1_000_000_000;

/// N.B. we don't want to specify two different notions of "default", so we use the
/// `Clap` instance above.
/// Environment variable carrying the coordinator's fingerprint to an
/// out-of-process plugin: `detcore::config_wire_fingerprint`, which extends
/// [`config_wire_fingerprint`] with Detcore's global RPC definitions.
///
/// Named alongside the other `REVERIE_SABRE_HERMIT_*` launch variables so the
/// two travel together and a reader finds them in one place.
pub const CONFIG_FINGERPRINT_ENV: &str = "REVERIE_SABRE_HERMIT_CONFIG_FINGERPRINT";

/// Environment variable carrying the coordinator's fingerprint
/// (`detcore::config_wire_fingerprint`, which extends
/// [`config_wire_fingerprint`] with Detcore's global RPC definitions) to the
/// in-guest LiteInst runtime (`libdetcore_liteinst.so`), which checks it
/// before it decodes the configuration the coordinator sends. Without the
/// check, a runtime built from another tree fails with a bare decoding error
/// that names neither build (<https://github.com/rrnewton/hermit/issues/3986>).
/// The runtime removes it once checked, as it removes its DETLOG variables:
/// the value is zeroed in the environment block and the variable is unset, so
/// the fingerprint never reaches the guest.
pub const LITEINST_CONFIG_FINGERPRINT_ENV: &str = "HERMIT_LITEINST_CONFIG_FINGERPRINT";

const CONFIG_DEFINITION_SOURCES: &[&[u8]] = &[
    include_bytes!("config.rs"),
    // `RawInode`, the host identity the inode RPCs (`DeterminizeInode`,
    // `TouchFile`, `SetFileMtime`) carry. Its layout changed from one `u64`
    // to a device and an inode number, which neither encoding of `Config`
    // shows.
    include_bytes!("fd.rs"),
    include_bytes!("happens_before.rs"),
    include_bytes!("network_trace.rs"),
    include_bytes!("pid.rs"),
    include_bytes!("schedule.rs"),
    include_bytes!("time.rs"),
    // `WatchArg`, the element of `Config::watch`. The encoded default holds
    // no watch, so neither encoding of `Config` shows its layout.
    include_bytes!("watch.rs"),
];

/// A fingerprint of this build's [`Config`] payload and the configuration and
/// clock RPC definitions shared by a plugin and its coordinator.
///
/// # Why this exists
///
/// An out-of-process plugin such as `libdetcore_sabre.so` is a separate Cargo
/// artifact that lands in the same target directory as `hermit`. Changing
/// `Config` or `DetTime` -- or merely switching branches -- leaves the plugin
/// stale while everything still *looks* built. `Config` is transferred during
/// the RPC handshake, and `DetTime` is the first field in every Detcore request.
/// A stale plugin decodes either against the wrong layout and the failure
/// surfaces as an opaque codec error: measured, one added `bool` field
/// produced `Decode(InvalidBooleanValue(20))` at connect, which points nowhere
/// near "your plugin is from a different build" and cost a long diagnosis while
/// blocking every SaBRe measurement.
///
/// # What it measures
///
/// Two encodings of `Config::default()` and the source definitions for the
/// configuration and clock RPC fields are fingerprinted with separate domains:
///
/// - the exact legacy-bincode bytes used by Reverie RPC, which detect changes
///   such as `u32` to `u64` even when both default to JSON number zero; and
/// - the JSON encoding, which carries every field name and makes a pure rename
///   visible even though bincode is positional; and
/// - the source files defining `Config`, its local serialized field types,
///   `DetTime`, and the `RawInode` the inode RPCs carry, which catch
///   wire-incompatible changes hidden by a default such as `Option<u64>::None`
///   to `Option<u32>::None`, or an added clock or inode field that leaves both
///   encodings of `Config` unchanged.
///
/// The source and JSON domains are deliberately stricter than the wire format
/// strictly requires. A documentation-only edit in one of these files can
/// require rebuilding the plugin; missing a wire-incompatible hidden variant
/// can make it decode the handshake or a subsequent request at the wrong offsets.
fn config_wire_default() -> Config {
    // `Config::default()` is intentionally environment-aware through Clap.
    // A coordinator may therefore inherit HERMIT_EPOCH/HERMIT_PRNG,
    // HERMIT_SCHED_SEED or HERMIT_CPU_PROFILE even though the separately loaded plugin receives a
    // minimal guest environment. Those invocation values are payload data,
    // not wire shape, and must not make two artifacts from the same source
    // reject one another. Explicit arguments take precedence over Clap's
    // environment provider; clear the scheduler override afterward to retain
    // the real environment-free default of `None` in the encoded shape.
    let mut config = Config::parse_from([
        "config-wire-fingerprint",
        &format!("--epoch={DEFAULT_EPOCH_STR}"),
        "--seed=0",
        "--sched-seed=0",
        "--cpu-profile=x86-64-v2",
    ]);
    config.sched_seed = None;
    // `BackendCapabilities` is defined in Reverie, outside
    // `CONFIG_DEFINITION_SOURCES`, so the source domain cannot see a change to
    // it; only its encoding can. Every field but the optional one encodes at
    // a fixed width (a bool or the `u8` width of `hardware_watchpoints` in
    // one byte, the `u64` signal set in eight), so any constant carries those
    // widths (see
    // `config_fingerprint_sees_the_width_of_every_backend_capability`). KVM's
    // is kept, as it was when one field was optional and only KVM's set it.
    // The optional `rcb_clock_contract` is set to ptrace's, the one contract
    // any backend states, because a `None` would encode as the same bare tag
    // whatever the contract's type became.
    config.backend = BackendCapabilities::KVM;
    config.backend.rcb_clock_contract = BackendCapabilities::PTRACE.rcb_clock_contract;
    config
}

pub fn config_wire_fingerprint() -> String {
    let config = config_wire_default();
    let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy())
        .expect("canonical Config wire default must encode with Reverie's bincode configuration");
    let named_shape = serde_json::to_string(&config)
        .expect("canonical Config wire default must encode as JSON for field-name checking");
    fingerprint_of_config_material(&wire, &named_shape, CONFIG_DEFINITION_SOURCES)
}

/// What differs when a coordinator announced `expected` as its
/// [`config_wire_fingerprint`] and this build's is another, or `None` when
/// they are equal. The in-guest LiteInst runtime reports this before it
/// decodes the coordinator's configuration
/// (<https://github.com/rrnewton/hermit/issues/3986>).
pub fn config_fingerprint_mismatch(expected: &std::ffi::OsStr) -> Option<String> {
    let ours = config_wire_fingerprint();
    (expected.to_str() != Some(ours.as_str())).then(|| {
        format!(
            "its configuration and clock RPC fingerprint is {ours}, Hermit's is {}",
            expected.to_string_lossy()
        )
    })
}

/// Domain-separated FNV-1a over wire bytes, named JSON, and defining source.
/// This is a mismatch detector, not a security boundary. Length-prefixing each
/// domain prevents two different source-file boundaries from hashing the same
/// concatenation.
fn fingerprint_of_config_material(
    wire: &[u8],
    named_shape: &str,
    definition_sources: &[&[u8]],
) -> String {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    let mut update = |domain: u8, bytes: &[u8]| {
        for byte in std::iter::once(&domain)
            .chain((bytes.len() as u64).to_le_bytes().iter())
            .chain(bytes.iter())
        {
            hash ^= u64::from(*byte);
            hash = hash.wrapping_mul(0x1000_0000_01b3);
        }
    };
    update(0, wire);
    update(1, named_shape.as_bytes());
    for source in definition_sources {
        update(2, source);
    }
    format!("{hash:016x}")
}

/// `Config::signal_state_copy` for a serialized configuration written before
/// the field existed: on, the default.
fn signal_state_copy_default() -> bool {
    true
}

impl Default for Config {
    fn default() -> Self {
        let v: Vec<String> = vec![];
        Config::parse_from(v.iter())
    }
}

/// Serializes `config` as JSON in the form it had before [`Config::backend`]
/// existed.
///
/// [`Config::backend`] is left out. In its place, at the same position in
/// the object, are the fifteen
/// separate backend keys that a serialized configuration carried before,
/// under the names and in the order it carried them, each with a value
/// computed from `config`. [`Config::record_host_inputs`], which the legacy
/// form never had, is left out too, and reads back as false: this form serves
/// only DBT, whose launcher collects no host inputs. So is
/// [`Config::guest_may_inherit_a_terminal`], which is read only where
/// `BackendCapabilities::host_timed_signals_are_physical` holds, false for
/// DBT, and [`Config::in_guest_detlog_forward_policy`] and
/// [`Config::in_guest_site_patching_off`], which is unset for DBT, and
/// [`Config::launch_image`], which came after it. So is [`Config::target_timeslice_syscalls_only`],
/// added after this form froze; DBT runs without a PMU maximum, where the
/// option has no effect. So are [`Config::futex_wake_yields`],
/// [`Config::scheduler_turn_cost`], [`Config::cap_preemption_charge`] and
/// [`Config::virtual_timeout`], also added after this form froze, which
/// `hermit run` refuses with the DBT backend, and [`Config::replaying`], set
/// only by `hermit replay`, which runs only on the ptrace backend. So is
/// [`Config::inherited_seccomp`], added after this form froze, which reads
/// back as unknown, and [`Config::virtual_cpus`], which `hermit run`
/// refuses with the DBT backend and reads back as unset. So is
/// [`Config::in_guest_reserved_signals`], empty for DBT (it is set only for
/// in-guest runs), which reads back as empty, and
/// [`Config::in_guest_refuses_host_timed_producers`] and
/// [`Config::in_guest_signal_delivery`], false for DBT, which read back as
/// false. So is [`Config::notice_host_network_abstract_binds`], added after this form
/// froze, which reads back as false: DBT's runtime then prints no notice for
/// an abstract `AF_UNIX` bind. So is [`Config::eager_lone_preemptions`], added after this form froze,
/// which has no effect without a PMU timer and reads back as false. So is
/// [`Config::standard_stream_aliases`], added after this form froze, set only for KVM, which
/// reads back as unset. So is [`Config::stacktrace_at_schedule_positions`], also added later,
/// which reads back as false: the DBT runtime takes no stack traces. Every
/// other field is serialized exactly as `serde_json::to_string(config)`
/// serializes it.
///
/// A standalone DBT run (one with no coordinator) reads its configuration
/// from `HERMIT_DBT_DETCONFIG` in this form or in the versioned envelope of
/// [`encode_backend_config_json`]. `hermit run --backend=dbt` writes neither:
/// it launches DBT coordinated, and the runtime receives the typed `Config`
/// through the coordinator's first RPC frame.
///
/// Read the result with [`from_legacy_backend_json`], which recovers
/// [`Config::backend`] from the fifteen keys. A plain `Config`
/// deserialization ignores the keys and gives the field its serde default.
pub fn to_legacy_backend_json(config: &Config) -> serde_json::Result<String> {
    if config.strict_schedule_boundaries {
        return Err(serde::ser::Error::custom(
            "strict schedule boundaries cannot be represented by the legacy backend configuration",
        ));
    }
    let mut json = Vec::with_capacity(4096);
    let mut serializer = serde_json::Serializer::new(&mut json);
    config.serialize(legacy_backend_json::ConfigSerializer {
        inner: &mut serializer,
        config,
    })?;
    String::from_utf8(json).map_err(serde::ser::Error::custom)
}

/// The JSON keys, in order, under which a serialized [`Config`] carried the
/// backend facts before they moved into [`Config::backend`], each with the
/// value it has for `config`.
///
/// `backend_is_kvm` named the backend rather than a fact; its value is
/// `provides_process_signal_control`, a capability only KVM reports.
/// `kvm_shared_dequeue_timers` was a startup policy, not a fact: the
/// production launcher, hermit-cli, set it exactly when that capability and
/// [`Config::sequentialize_threads`] held. Its value is that conjunction, and
/// [`from_legacy_backend_json`] refuses any other value.
fn legacy_backend_keys(config: &Config) -> [(&'static str, bool); 15] {
    let backend = &config.backend;
    [
        // No backend installs a CPUID policy of its own any more, so the key
        // is always written false; see `from_legacy_backend_json`.
        ("cpuid_virtualized_by_backend", false),
        // Every backend now takes every madvise advice and refuses what it does
        // not implement itself, so the key is always written true, its value on
        // DBT, the form's only reader; see `from_legacy_backend_json`.
        ("backend_supports_madvise", true),
        (
            "discover_live_file_metadata",
            backend.tool_shares_guest_descriptor_table,
        ),
        (
            "detect_host_clock_futex_timeouts",
            backend.guest_clock_reads_bypass_backend,
        ),
        // Reverie's backend contract now requires every backend to leave the
        // syscall-clobbered registers as a native return would (with the
        // exceptions it names), so the key is always written false, its value
        // on DBT, the form's only reader; see `from_legacy_backend_json`.
        ("syscall_clobbers_virtualized_by_backend", false),
        (
            "cancel_killed_thread_rpcs",
            backend.needs_killed_thread_rpc_cancellation,
        ),
        (
            "backend_reports_physical_process_exits",
            backend.reports_physical_process_exits,
        ),
        (
            "backend_tracks_process_children",
            backend.tracks_process_children,
        ),
        (
            "backend_runs_exit_robust_list",
            backend.runs_exit_robust_list,
        ),
        (
            "backend_requires_thread_directed_process_signals",
            backend.requires_thread_directed_process_signals,
        ),
        ("backend_is_kvm", backend.provides_process_signal_control),
        (
            "kvm_shared_dequeue_timers",
            backend.provides_process_signal_control && config.sequentialize_threads,
        ),
        (
            "backend_supports_parked_write_signal_interruption",
            backend.supports_parked_write_signal_interruption,
        ),
        // No backend virtualizes the capability prctls any more; they reach
        // the kernel on every backend. Always false; see
        // `from_legacy_backend_json`.
        ("backend_virtualizes_capability_prctls", false),
        // A vfork child may register before or after the parent's call returns
        // on every backend now, and Detcore waits for it either way, so the key
        // is always written false, its value on DBT, the form's only reader;
        // see `from_legacy_backend_json`.
        ("backend_defers_vfork_child_registration", false),
    ]
}

/// Parses JSON written by [`to_legacy_backend_json`], or any JSON in the form
/// a serialized [`Config`] had before [`Config::backend`] existed, and recovers
/// [`Config::backend`] from the fifteen legacy backend keys.
///
/// The keys are read exactly as they were read while each was a `Config`
/// field: a key that is absent takes the default that field had, and a key of
/// the wrong type fails the parse. Each key then supplies every capability that
/// replaced a read of it, so a decoded configuration behaves as the same JSON
/// did before:
///
/// - `discover_live_file_metadata` supplies the three descriptor and loopback
///   facts that were each a read of it;
/// - `backend_is_kvm` supplies the two behaviours it selected that remain
///   capabilities, `provides_process_signal_control` and
///   `emulates_child_waits` (the other three are now answered by the backend
///   itself: refusing a non-leader exec is a typed refusal it reports per
///   exec, the `gettimeofday` repair runs on every backend through its
///   `time(2)` probe and `Guest::storable_memory_ranges`, and the user
///   address limit is each guest's own `Guest::user_address_limit` report);
/// - every other key supplies the one capability of the same meaning.
///
/// `backend_supports_madvise` supplies no capability any more: it said the
/// tool could forward every madvise advice to the backend, which every backend
/// now accepts, refusing an advice it does not implement itself. Since
/// `supports_madv_dontneed` was deleted, `MADV_DONTNEED` had been forwarded
/// whatever this key said.
///
/// This inverts [`to_legacy_backend_json`] for every capability value the keys
/// can express, which includes every backend's own constant except for
/// capabilities added after the legacy form froze: those never enter it, as
/// `record_host_inputs` does not, and read back as their default. There are
/// fifteen. `process_exits_complete_asynchronously` and
/// `virtualizes_guest_sigalrm` are true only for in-guest LiteInst, which
/// never travels through this DBT-only form.
/// `supports_blocked_wait_signal_interruption`,
/// `delivers_preemption_timer_events` and
/// `runs_guest_code_between_injections` read back as false, the value DBT
/// has and the value their earlier `Config` fields read back as; so do
/// `reports_sigreturns` and the four capabilities split out of
/// `supports_blocked_wait_signal_interruption` (`kernel_signal_state_is_guests`,
/// `kernel_signal_targets_are_predictable`,
/// `host_task_reads_reach_guest_threads`, `host_timed_signals_are_physical`),
/// false for DBT too. `provides_guest_backtraces` reads back as true, the
/// ptrace value it takes from the starting constant and DBT's value; the
/// two backends that report false (in-guest LiteInst and KVM) travel
/// through this form only inside the versioned envelope.
/// `places_orphans` reads back as false and `publishable_parent_death_signals`
/// as the empty set, DBT's values; KVM, the one backend that reports
/// otherwise, travels through this form only inside the versioned envelope
/// too. `rcb_clock_contract` reads back as `None`, DBT's value; ptrace, the
/// one backend that states a contract, travels through this form only
/// inside the versioned envelope as well.
/// `hardware_watchpoints` reads back as 0, DBT's.
///
/// `cpuid_virtualized_by_backend` supplies no capability either: it said the
/// backend installed a CPUID policy of its own, which no backend does any
/// more (KVM stopped with Reverie 0f4d8852). It is still written, as false,
/// so the legacy form keeps its bytes, and an input that
/// says `true` fails the parse rather than being read as a policy no backend
/// has.
///
/// `backend_defers_vfork_child_registration` supplies no capability either: it
/// said the backend registered a vfork child only after the parent's call
/// returned, which Reverie's backend contract now allows of every backend. It
/// is written as false, its value on DBT, so the legacy form keeps its bytes.
/// The reader still takes it as one of the fifteen keys, so it keeps its
/// position in the array form, must be a boolean and may appear once, and
/// ignores its value (KVM wrote true).
///
/// `backend_supports_madvise` is written as true, its value on DBT, so the
/// legacy form keeps its bytes. The reader still takes it as one of the fifteen
/// keys, so it keeps its position in the array form, must be a boolean and may
/// appear once, and ignores its value (KVM wrote false).
///
/// `syscall_clobbers_virtualized_by_backend` supplies no capability either: it
/// said the backend left the registers a `syscall` instruction clobbers as a
/// native return would, which Reverie's backend contract now requires of every
/// backend (with the exceptions that contract names). It is written as false,
/// its value on DBT, so the legacy form keeps its bytes. The reader still takes it as one of the fifteen keys, so
/// it keeps its position in the array form, must be a boolean and may appear
/// once, and ignores its value (SaBRe and KVM wrote true).
///
/// `backend_virtualizes_capability_prctls` supplies no capability either: it
/// said the backend answered the capability-set prctls itself, and every
/// backend now passes them to the kernel. It is written as false, so the
/// legacy form keeps its bytes, and `true` fails the parse.
///
/// `kvm_shared_dequeue_timers` supplies no capability. It was read into a
/// `Config` field that chose the controlled signal path, and that choice is
/// now made from the capabilities, [`Config::sequentialize_threads`] and the
/// control the backend offers. It is read as it was, a boolean that is false
/// when absent, and the parse then fails unless it equals `backend_is_kvm &&
/// sequentialize_threads`, the value the production launcher always wrote. An
/// accepted input whose capabilities are those of the backend that runs it
/// selects the path it selected before. An input that would select another,
/// such as `true` without `backend_is_kvm`, or absent beside `backend_is_kvm`
/// and sequentialized threads, fails the parse rather than being silently
/// reinterpreted. (`backend_is_kvm` given to a backend that offers no control,
/// such as DBT, already misdescribed that backend and stays outside this
/// guarantee; the production launcher never writes it.) A failed parse is
/// handled like any malformed value: the DBT standalone JSON reader falls
/// back to its strict default configuration and emits its
/// `could not parse HERMIT_DBT_DETCONFIG` warning.
///
/// Everything else is read as the legacy form was read, by `Config`'s own
/// derived deserializer with the legacy keys taken out before it sees them:
///
/// - In an object, a key the legacy form did not name is ignored, whatever its
///   value. `backend` is such a key, so it is ignored rather than read as the
///   field it is now; no production writer emits it. A key the legacy form
///   named fails the parse when it appears twice or holds the wrong type.
/// - A JSON array lists the fields by position, as serde reads any derived
///   struct. The positions are those of the legacy form, which are the key
///   order of [`to_legacy_backend_json`]: the fifteen legacy keys stand where
///   [`Config::backend`] stands. Too many elements fail the parse; too few
///   leave the fields
///   after them missing, each taking its default or failing the parse as the
///   derived deserializer decides for that field.
pub fn from_legacy_backend_json(json: &str) -> serde_json::Result<Config> {
    let mut deserializer = serde_json::Deserializer::from_str(json);
    let (mut config, legacy) = serde::Deserializer::deserialize_struct(
        &mut deserializer,
        "Config",
        &[],
        legacy_backend_json::LegacyConfigVisitor,
    )?;
    deserializer.end()?;
    if legacy.cpuid_virtualized_by_backend {
        return Err(<serde_json::Error as serde::de::Error>::custom(
            "cpuid_virtualized_by_backend is true, but no backend installs a CPUID \
             policy of its own; the key must be false or absent",
        ));
    }
    if legacy.backend_virtualizes_capability_prctls {
        return Err(<serde_json::Error as serde::de::Error>::custom(
            "backend_virtualizes_capability_prctls is true, but no backend answers the \
             capability prctls itself; the key must be false or absent",
        ));
    }
    let controlled = legacy.backend_is_kvm && config.sequentialize_threads;
    if legacy.kvm_shared_dequeue_timers != controlled {
        return Err(<serde_json::Error as serde::de::Error>::custom(
            format_args!(
                "kvm_shared_dequeue_timers is {} but backend_is_kvm && \
             sequentialize_threads is {controlled}; the key is no longer an \
             independent setting and must equal that conjunction",
                legacy.kvm_shared_dequeue_timers,
            ),
        ));
    }
    config.backend = legacy.capabilities();
    Ok(config)
}

/// Preserve the exact backend facts alongside an already encoded legacy config.
///
/// The legacy form couples some capabilities and omits others. When it can
/// express `backend` exactly, return `legacy_json` byte for byte, so a reader
/// of the legacy form reads it unchanged. Otherwise wrap it in a versioned
/// envelope whose typed backend replaces the legacy projection on decoding.
/// Non-backend fields keep the legacy form's semantics in either case.
pub fn encode_backend_config_json(
    legacy_json: String,
    backend: BackendCapabilities,
) -> serde_json::Result<String> {
    if from_legacy_backend_json(&legacy_json)?.backend == backend {
        return Ok(legacy_json);
    }
    serde_json::to_string(&VersionedBackendConfig {
        dbt_config_version: 1,
        config: serde_json::from_str(&legacy_json)?,
        backend,
    })
}

/// Read a legacy configuration or a versioned envelope with exact backend facts.
///
/// Any valid legacy object or positional array retains
/// [`from_legacy_backend_json`]'s behavior, including ignored unknown fields.
/// Otherwise the input must be an envelope: unknown versions, duplicate fields
/// and missing typed capabilities are parse errors.
/// This parser does not choose the caller's error policy. The admitted DBT
/// standalone JSON path retains its strict-default parse fallback; coordinated
/// startup receives its authoritative typed Config through the RPC handshake.
pub fn decode_backend_config_json(json: &str) -> serde_json::Result<Config> {
    // Legacy inputs take precedence: even dbt_config_version was an ignored
    // unknown key in that form, and its presence must not change a valid run.
    if let Ok(config) = from_legacy_backend_json(json) {
        return Ok(config);
    }
    // Deserialize the original bytes, not a Value map that would erase
    // duplicate keys before the typed deserializers can reject them.
    let envelope: VersionedBackendConfig<'_> = serde_json::from_str(json)?;
    if envelope.dbt_config_version != 1 {
        return Err(<serde_json::Error as serde::de::Error>::custom(
            format_args!(
                "unsupported DBT configuration version {}",
                envelope.dbt_config_version
            ),
        ));
    }
    // `BackendCapabilities` reads a capability Reverie added later (with
    // `#[serde(default)]`, such as `rcb_clock_contract`) as its default when
    // it is absent. The envelope is written whole by this build's
    // `encode_backend_config_json`, so every capability must be named: an
    // absent one is refused rather than defaulted.
    let named: EnvelopeBackendKeys = serde_json::from_str(json)?;
    let expected = serde_json::to_value(envelope.backend)?;
    if let Some(missing) = expected
        .as_object()
        .into_iter()
        .flat_map(|capabilities| capabilities.keys())
        .find(|capability| !named.backend.contains_key(capability.as_str()))
    {
        return Err(<serde_json::Error as serde::de::Error>::custom(
            format_args!("missing field `{missing}` in the envelope's backend"),
        ));
    }
    let mut config = from_legacy_backend_json(envelope.config.get())?;
    config.backend = envelope.backend;
    Ok(config)
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct VersionedBackendConfig<'a> {
    dbt_config_version: u32,
    #[serde(borrow)]
    config: &'a serde_json::value::RawValue,
    backend: BackendCapabilities,
}

/// The capability names a versioned envelope gives, read apart from the
/// typed [`VersionedBackendConfig`], which has already refused duplicate and
/// unknown keys.
#[derive(Deserialize)]
struct EnvelopeBackendKeys {
    backend: serde_json::Map<String, serde_json::Value>,
}

/// The names of the fifteen legacy backend keys, in the order
/// [`legacy_backend_keys`] gives them.
const LEGACY_BACKEND_KEY_NAMES: [&str; 15] = [
    "cpuid_virtualized_by_backend",
    "backend_supports_madvise",
    "discover_live_file_metadata",
    "detect_host_clock_futex_timeouts",
    "syscall_clobbers_virtualized_by_backend",
    "cancel_killed_thread_rpcs",
    "backend_reports_physical_process_exits",
    "backend_tracks_process_children",
    "backend_runs_exit_robust_list",
    "backend_requires_thread_directed_process_signals",
    "backend_is_kvm",
    "kvm_shared_dequeue_timers",
    "backend_supports_parked_write_signal_interruption",
    "backend_virtualizes_capability_prctls",
    "backend_defers_vfork_child_registration",
];

const fn legacy_backend_key_default_true() -> bool {
    true
}

/// The fifteen legacy backend keys with the types and serde defaults they had
/// as `Config` fields. It is deserialized from the keys
/// [`legacy_backend_json::LegacyConfigVisitor`] takes out of the input, so a
/// key given twice fails here as it failed as a `Config` field.
#[derive(Deserialize)]
struct LegacyBackendKeys {
    #[serde(default)]
    cpuid_virtualized_by_backend: bool,
    // Supplies no capability; declared so that the key, like the others,
    // fails the parse when it is given twice.
    #[expect(
        dead_code,
        reason = "read only to refuse a repeated key; its value is ignored"
    )]
    #[serde(default = "legacy_backend_key_default_true")]
    backend_supports_madvise: bool,
    #[serde(default)]
    discover_live_file_metadata: bool,
    #[serde(default)]
    detect_host_clock_futex_timeouts: bool,
    // Supplies no capability; declared so that the key, like the others,
    // fails the parse when it is given twice.
    #[expect(
        dead_code,
        reason = "read only to refuse a repeated key; its value is ignored"
    )]
    #[serde(default)]
    syscall_clobbers_virtualized_by_backend: bool,
    #[serde(default)]
    cancel_killed_thread_rpcs: bool,
    #[serde(default)]
    backend_reports_physical_process_exits: bool,
    #[serde(default = "legacy_backend_key_default_true")]
    backend_tracks_process_children: bool,
    #[serde(default = "legacy_backend_key_default_true")]
    backend_runs_exit_robust_list: bool,
    #[serde(default)]
    backend_requires_thread_directed_process_signals: bool,
    #[serde(default)]
    backend_is_kvm: bool,
    /// Read as it was while it was a `Config` field, then checked by
    /// [`from_legacy_backend_json`] against `backend_is_kvm` and
    /// [`Config::sequentialize_threads`]; it supplies no capability.
    #[serde(default)]
    kvm_shared_dequeue_timers: bool,
    #[serde(default = "legacy_backend_key_default_true")]
    backend_supports_parked_write_signal_interruption: bool,
    #[serde(default)]
    backend_virtualizes_capability_prctls: bool,
    // Supplies no capability; declared so that the key, like the others,
    // fails the parse when it is given twice.
    #[expect(
        dead_code,
        reason = "read only to refuse a repeated key; its value is ignored"
    )]
    #[serde(default)]
    backend_defers_vfork_child_registration: bool,
}

impl LegacyBackendKeys {
    /// The capabilities these keys describe; the inverse of
    /// [`legacy_backend_keys`]. Every field is assigned, so the starting
    /// constant contributes nothing a key decides.
    fn capabilities(&self) -> BackendCapabilities {
        let mut backend = BackendCapabilities::PTRACE;
        backend.tool_shares_guest_descriptor_table = self.discover_live_file_metadata;
        backend.rediscovers_descriptors_after_exec = self.discover_live_file_metadata;
        backend.loopback_pollers_yield_to_peers = self.discover_live_file_metadata;
        backend.guest_clock_reads_bypass_backend = self.detect_host_clock_futex_timeouts;
        backend.needs_killed_thread_rpc_cancellation = self.cancel_killed_thread_rpcs;
        // A legacy encoding predates this key. Only the ptrace tracer reports
        // child-exit publication, and it is the one backend that needs no
        // killed-thread RPC cancellation.
        backend.reports_child_exit_publication = !self.cancel_killed_thread_rpcs;
        // Newer than the legacy form. It took over the child-exit ledger's gates
        // from reports_child_exit_publication and has its value on every backend
        // the form can express (true for ptrace and the default, false for DBT,
        // the form's only reader), so it is read back the same way.
        backend.reports_kernel_child_exit_signals = backend.reports_child_exit_publication;
        backend.reports_physical_process_exits = self.backend_reports_physical_process_exits;
        backend.tracks_process_children = self.backend_tracks_process_children;
        backend.runs_exit_robust_list = self.backend_runs_exit_robust_list;
        backend.requires_thread_directed_process_signals =
            self.backend_requires_thread_directed_process_signals;
        backend.supports_parked_write_signal_interruption =
            self.backend_supports_parked_write_signal_interruption;
        backend.provides_process_signal_control = self.backend_is_kvm;
        backend.emulates_child_waits = self.backend_is_kvm;
        // A legacy encoding predates this key. DBT, the form's only reader,
        // promotes shared open-file descriptions before a native fork or exec
        // and alone requires thread-directed process signals (DBT's runtime
        // also states it from any source, `assert_dbt_execution_model`).
        backend.promotes_open_files_before_native_lifecycle =
            self.backend_requires_thread_directed_process_signals;
        // Added after the legacy form froze. Each was a `Config` field with no
        // legacy key that read back as false (or, for the four split out of
        // `supports_blocked_wait_signal_interruption`, a reading of one), and
        // each is false for DBT, the form's only reader, so a DBT
        // configuration stays exact.
        set_kernel_signal_facts(&mut backend, false);
        backend.reports_sigreturns = false;
        backend.delivers_preemption_timer_events = false;
        backend.runs_guest_code_between_injections = false;
        // Added after the legacy form froze, and false for DBT, the form's
        // only reader, whose guest threads are host tasks whose exits run
        // Linux's robust-list walk. KVM, the one backend that reports true,
        // is preserved exactly only by the versioned envelope of
        // `encode_backend_config_json`, never by these keys.
        backend.kernel_never_walks_robust_lists = false;
        // Added after the legacy form froze (Reverie 9443a6b2): it reads
        // back as the starting constant's true, which is also DBT's value,
        // the form's only reader, so a DBT configuration stays exact. The
        // two backends that report false (in-guest LiteInst and KVM) are
        // preserved exactly only by the versioned envelope of
        // `encode_backend_config_json`, never by these keys.
        backend.provides_guest_backtraces = true;
        // Added after the legacy form froze (Reverie f614c7c8): false and the
        // empty set, DBT's values, the form's only reader. KVM, the one
        // backend that reports otherwise, is preserved exactly only by the
        // versioned envelope of `encode_backend_config_json`, never by these
        // keys.
        backend.places_orphans = false;
        backend.publishable_parent_death_signals = 0;
        // Added after the legacy form froze (Reverie 1161 made it a `u8`
        // width). The legacy form carries no width for it: by absence it
        // says the bool false, and the minimal width that represents a
        // legacy bool false is 0, so it reads back as 0. A backend with a
        // nonzero width (ptrace reports 4 on x86_64) is preserved exactly
        // only by the versioned envelope of `encode_backend_config_json`,
        // never by these keys.
        backend.hardware_watchpoints = 0;
        // Added after the legacy form froze (Reverie 7535309b): `None`, no
        // qualified RCB clock, DBT's value, the form's only reader. Ptrace,
        // the one backend that states a contract, is preserved exactly only
        // by the versioned envelope of `encode_backend_config_json`, never by
        // these keys.
        backend.rcb_clock_contract = None;
        backend
    }
}

/// The serde adapters behind [`to_legacy_backend_json`] and
/// [`from_legacy_backend_json`].
///
/// The serializer adapter wraps the JSON serializer only at the top level,
/// where `Config`'s derived `Serialize` opens one struct and writes its fields
/// in declaration order. Nested values go straight to the wrapped serializer,
/// so they, and every field other than the two it replaces, are encoded
/// unchanged.
///
/// The deserializer adapters likewise wrap only the top-level object or array,
/// and hand every nested value to the wrapped JSON deserializer.
mod legacy_backend_json {
    use std::fmt;

    use serde::Deserialize;
    use serde::Serialize;
    use serde::de;
    use serde::de::DeserializeSeed;
    use serde::de::IgnoredAny;
    use serde::de::MapAccess;
    use serde::de::SeqAccess;
    use serde::de::Visitor;
    use serde::de::value::BoolDeserializer;
    use serde::de::value::MapAccessDeserializer;
    use serde::de::value::MapDeserializer;
    use serde::de::value::SeqAccessDeserializer;
    use serde::de::value::StringDeserializer;
    use serde::de::value::U64Deserializer;
    use serde::ser::Error as _;
    use serde::ser::Impossible;
    use serde::ser::SerializeStruct;
    use serde::ser::Serializer;

    use super::BackendCapabilities;
    use super::Config;
    use super::LEGACY_BACKEND_KEY_NAMES;
    use super::LegacyBackendKeys;
    use super::legacy_backend_keys;

    /// The keys `Config` has now that its legacy form did not. The legacy
    /// form ignored them, as it ignored every key it did not name.
    /// `record_host_inputs` has no legacy key and no legacy value: the legacy
    /// form serves only DBT, whose launcher collects no host inputs, so it is
    /// never written and always reads as false.
    /// So is `guest_may_inherit_a_terminal`, which is read only where
    /// `BackendCapabilities::host_timed_signals_are_physical` holds;
    /// that is false for DBT.
    /// `target_timeslice_syscalls_only` came after the legacy form froze; the
    /// legacy form never carried it, so it is never written and reads as false,
    /// the behaviour every legacy reader already has.
    /// `in_guest_detlog_forward_policy` is unset for DBT (its runtime forwards
    /// no records on a socket), so it is never written and reads as unset.
    /// `futex_wake_yields`, `pipe_wake_latency`, `scheduler_turn_cost` and
    /// `cap_preemption_charge` and `virtual_timeout` came after the legacy form
    /// froze too, and `hermit run` refuses them with the DBT backend.
    /// `replaying` is never written either: only `hermit replay` sets it, and
    /// replay runs only on the ptrace backend.
    /// `in_guest_site_patching_off` is false for DBT (it is set only for
    /// in-guest LiteInst), so it is never written and reads as false.
    /// `in_guest_reserved_signals` is empty for DBT (it is set only for
    /// in-guest runs), so it is never written and reads as empty.
    /// `in_guest_refuses_host_timed_producers` is false for DBT (it is set
    /// only for in-guest runs), so it is never written and reads as false.
    /// So is `in_guest_signal_delivery`, for the same reason.
    /// `launch_image` came after the legacy form froze: it is never written
    /// and reads as unset. DBT's runtime takes it from the coordinator's
    /// configuration instead, which declares DynamoRIO's library as the
    /// launcher.
    /// `inherited_seccomp` came after the legacy form froze too. It is never
    /// written and reads as unknown.
    /// `cpu_profile` and `cpu_profile_host` came after it as well. DBT refuses
    /// the x86-64-v3 profile, so they are never written and read as v2.
    /// `signal_state_copy` came after it too. The copy is never kept on DBT
    /// (its kernel signal state is not the guest's), so it is never written
    /// and reads as on, its serde default.
    /// And `container_mount_table`, which describes one run's container: the
    /// frozen form omits it, so it is never written and reads as absent. The
    /// coordinated startup's typed configuration, which is how Hermit
    /// launches DBT, carries the table.
    /// `mountinfo_mount_namespace` too, for the same reason: it is never
    /// written and reads as absent. So are `directory_placeholder` and
    /// `container_procfs`, which describe one run's container; the
    /// coordinated startup's typed configuration carries them.
    /// `signal_delivery_check_fatal` matters only where the copy is kept, so
    /// it is never written and reads as false.
    /// `strict_binder` is the host's `--strict` policy for binder, which the
    /// frozen form predates: never written, read as false.
    /// `work_span_report` came after it as well: the report is written by the
    /// scheduler, not the in-guest runtime, so it is never written and reads
    /// as unset.
    /// `signal_at` came after the form froze; the scheduler acts on it, not
    /// the runtime, so it is never written and reads as unset.
    /// `annotated_priority_replay` came after it too, and is false for every
    /// backend that reads the legacy form: priority replay needs a preemption
    /// timer, which none of them delivers, so it is never written and reads
    /// as false.
    /// `virtual_cpus` came after it too, and `hermit run` refuses it with the
    /// DBT backend: it is never written and reads as unset.
    /// `expired_wait_charge` too: the scheduler charges turns, not the
    /// runtime, so it is never written and reads as unset.
    /// `notice_host_network_abstract_binds` came after it too. It is never written and
    /// reads as false, so the DBT runtime prints no notice for an abstract
    /// `AF_UNIX` bind under host networking.
    /// `replay_edited_preemptions` came after it too. It is never written and
    /// reads as false; it cannot matter there, since a timerless backend
    /// such as DBT refuses `--replay-preemptions-from`, which it requires.
    /// `eager_lone_preemptions` came after it too. DBT has no PMU timer, so
    /// the option has no effect there; it is never written and reads as
    /// false.
    /// `access_trace`, `access_trace_from`, `access_trace_max_records` and
    /// `access_trace_max_bytes` came after it too; the scheduler writes the
    /// trace, and `hermit run` refuses it on DBT, so they are never written
    /// and read as unset, 0, unset and unset. So did `watch` and `watch_from`,
    /// which need the trace and the ptrace backend's watchpoints: never
    /// written, read as none and 0.
    // `chaos_timeslice_range` came after it too: the scheduler draws the
    // slices, not the runtime, so it is never written and reads as unset.
    // `chaos_timeslice_draw` and `chaos_timeslice` came with it, for the same
    // reason; they read as `slice` and unset.
    // `guest_ids` came after it too: DBT numbers its guest itself, so it
    // is never written and reads as `GuestIds::Namespace`, its serde
    // default.
    // `detlog_anon` came after it too: a coordinator-side ptrace-only
    // diagnostic the other backends refuse, so it is never written and
    // reads as absent, its serde default.
    /// `replay_continue_fairly` came after it too; only the scheduler reads
    /// it, so it is never written and reads as false.
    /// `standard_stream_aliases` came after it too, and is set only for KVM;
    /// it is never written and reads as unset.
    /// `sleep_registration` too: the scheduler registers sleeps, not the
    /// runtime, so it is never written and reads as unset.
    /// `rewrite_exclusives_unmarked` came after it as well: the CLI rewrites
    /// the files before the run and the run's global state reports them, not
    /// the runtime, so it is never written and reads as empty.
    /// `rewrite_exclusives_in_memory` came after it as well: the in-memory
    /// rewrite of aarch64 exclusive loops is done by the ptrace backend's
    /// global state, and no backend that reads the legacy form does it, so it
    /// is never written and reads as false.
    /// `rewrite_exclusives_overlay` came after it too: the CLI replaced the
    /// files before the run, and only the record states it, so it is never
    /// written and reads as false.
    /// `post_crash_timeslice` came after it too, and the scheduler acts on it
    /// as well, so it is never written and reads as off.
    /// `stacktrace_at_schedule_positions` came after it too. It is never
    /// written and reads as false: the DBT runtime takes no stack traces.
    /// `property_snapshot` came after it too, and only hermit-cli sets it, on
    /// Android, where DBT does not run; it is never written and reads as
    /// unset.
    /// `rewrite_exclusives_overlay` likewise: the run's global state names the
    /// files the overlay rewrite left, so it is never written and reads as
    /// empty.
    /// `chaos_instruction_offset` came after it as well: the ptrace backend's
    /// precise timer takes its single steps, and `hermit run` refuses it on
    /// every other backend, so it is never written and reads as 0.
    /// `rewrite_exclusives_unmarked` likewise: it is never written and reads
    /// as empty.
    /// `rewrite_exclusives_overlay_requested` came after it too: the CLI
    /// states the requested overlay from its flags, and only the record
    /// states its provenance, so it is never written and reads as false.
    /// `untrap_passthrough_syscalls` came after it too. `hermit run` refuses it
    /// on every backend but ptrace, so it is never written and reads as false.
    const FIELDS_WITHOUT_A_LEGACY_KEY: [&str; 61] = [
        "backend",
        "strict_schedule_boundaries",
        "record_host_inputs",
        "guest_may_inherit_a_terminal",
        "in_guest_detlog_forward_policy",
        "in_guest_site_patching_off",
        "in_guest_reserved_signals",
        "in_guest_refuses_host_timed_producers",
        "launch_image",
        "target_timeslice_syscalls_only",
        "futex_wake_yields",
        "pipe_wake_latency",
        "expired_wait_charge",
        "sleep_registration",
        "replaying",
        "scheduler_turn_cost",
        "cap_preemption_charge",
        "virtual_timeout",
        "inherited_seccomp",
        "cpu_profile",
        "cpu_profile_host",
        "cpu_profile_source",
        "signal_state_copy",
        "container_mount_table",
        "mountinfo_mount_namespace",
        "directory_placeholder",
        "container_procfs",
        "signal_delivery_check_fatal",
        "strict_binder",
        "work_span_report",
        "signal_at",
        "post_crash_timeslice",
        "in_guest_signal_delivery",
        "annotated_priority_replay",
        "virtual_cpus",
        "notice_host_network_abstract_binds",
        "replay_edited_preemptions",
        "lenient_replay",
        "eager_lone_preemptions",
        "chaos_timeslice_range",
        "chaos_timeslice_draw",
        "chaos_timeslice",
        "guest_ids",
        "detlog_anon",
        "access_trace",
        "access_trace_from",
        "access_trace_max_records",
        "access_trace_max_bytes",
        "replay_continue_fairly",
        "standard_stream_aliases",
        "rewrite_exclusives_unmarked",
        "rewrite_exclusives_in_memory",
        "rewrite_exclusives_overlay",
        "rewrite_exclusives_overlay_requested",
        "count_store_exclusives",
        "stacktrace_at_schedule_positions",
        "property_snapshot",
        "watch",
        "watch_from",
        "chaos_instruction_offset",
        "untrap_passthrough_syscalls",
    ];

    /// Reads the top-level object or array of a legacy configuration. The
    /// derived `Config` deserializer reads every field; this takes the legacy
    /// backend keys out of the input before it sees them and collects them for
    /// [`LegacyBackendKeys`].
    pub(super) struct LegacyConfigVisitor;

    impl<'de> Visitor<'de> for LegacyConfigVisitor {
        type Value = (Config, LegacyBackendKeys);

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("struct Config")
        }

        fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {
            let mut legacy = Vec::new();
            let config = Config::deserialize(MapAccessDeserializer::new(LegacyKeys {
                inner: map,
                legacy: &mut legacy,
            }))?;
            Ok((config, legacy_backend_keys_from(legacy)?))
        }

        fn visit_seq<A: SeqAccess<'de>>(self, seq: A) -> Result<Self::Value, A::Error> {
            let fields = config_field_order().map_err(<A::Error as de::Error>::custom)?;
            let mut legacy = Vec::new();
            let config = Config::deserialize(SeqAccessDeserializer::new(LegacyPositions {
                inner: seq,
                fields: fields.iter(),
                legacy: &mut legacy,
            }))?;
            Ok((config, legacy_backend_keys_from(legacy)?))
        }
    }

    /// Reads the collected legacy keys with their old types and defaults. A
    /// key that appeared twice is collected twice, and fails here as a
    /// duplicate field.
    fn legacy_backend_keys_from<E: de::Error>(
        legacy: Vec<(&'static str, bool)>,
    ) -> Result<LegacyBackendKeys, E> {
        LegacyBackendKeys::deserialize(MapDeserializer::<_, E>::new(legacy.into_iter()))
    }

    /// The top-level object's entries, with each legacy backend key read as the
    /// boolean it was and collected rather than passed on, and each key of
    /// [`FIELDS_WITHOUT_A_LEGACY_KEY`] skipped as the unknown key it was.
    struct LegacyKeys<'l, A> {
        inner: A,
        legacy: &'l mut Vec<(&'static str, bool)>,
    }

    impl<'de, A: MapAccess<'de>> MapAccess<'de> for LegacyKeys<'_, A> {
        type Error = A::Error;

        fn next_key_seed<K: DeserializeSeed<'de>>(
            &mut self,
            seed: K,
        ) -> Result<Option<K::Value>, A::Error> {
            while let Some(key) = self.inner.next_key::<String>()? {
                if let Some(name) = LEGACY_BACKEND_KEY_NAMES
                    .into_iter()
                    .find(|name| *name == key)
                {
                    let value = self.inner.next_value::<bool>()?;
                    self.legacy.push((name, value));
                } else if FIELDS_WITHOUT_A_LEGACY_KEY.contains(&key.as_str()) {
                    self.inner.next_value::<IgnoredAny>()?;
                } else {
                    return seed.deserialize(StringDeserializer::new(key)).map(Some);
                }
            }
            Ok(None)
        }

        fn next_value_seed<V: DeserializeSeed<'de>>(
            &mut self,
            seed: V,
        ) -> Result<V::Value, A::Error> {
            self.inner.next_value_seed(seed)
        }
    }

    /// The top-level array's elements, in the legacy form's positions. The
    /// derived `Config` deserializer asks for one element per field in
    /// declaration order; at [`Config::backend`] this reads up to fifteen
    /// elements as the legacy backend keys, and neither the field it asks for
    /// there nor [`Config::record_host_inputs`],
    /// [`Config::guest_may_inherit_a_terminal`],
    /// [`Config::in_guest_detlog_forward_policy`],
    /// [`Config::in_guest_site_patching_off`],
    /// [`Config::in_guest_reserved_signals`],
    /// [`Config::in_guest_refuses_host_timed_producers`],
    /// [`Config::in_guest_signal_delivery`], [`Config::launch_image`],
    /// [`Config::target_timeslice_syscalls_only`],
    /// [`Config::futex_wake_yields`], [`Config::replaying`],
    /// [`Config::scheduler_turn_cost`], [`Config::cap_preemption_charge`],
    /// [`Config::virtual_timeout`], [`Config::inherited_seccomp`] or [`Config::replay_edited_preemptions`] takes
    /// an element. Each gets a
    /// placeholder; [`super::from_legacy_backend_json`] replaces the first.
    struct LegacyPositions<'f, 'l, A> {
        inner: A,
        fields: std::slice::Iter<'f, String>,
        legacy: &'l mut Vec<(&'static str, bool)>,
    }

    impl<'de, A: SeqAccess<'de>> SeqAccess<'de> for LegacyPositions<'_, '_, A> {
        type Error = A::Error;

        fn next_element_seed<T: DeserializeSeed<'de>>(
            &mut self,
            seed: T,
        ) -> Result<Option<T::Value>, A::Error> {
            match self.fields.next().map(String::as_str) {
                Some("backend") => {
                    for name in LEGACY_BACKEND_KEY_NAMES {
                        match self.inner.next_element::<bool>()? {
                            Some(value) => self.legacy.push((name, value)),
                            // A short array; the rest take their defaults.
                            None => break,
                        }
                    }
                    let placeholder = serde_json::to_value(BackendCapabilities::PTRACE)
                        .map_err(<A::Error as de::Error>::custom)?;
                    seed.deserialize(placeholder)
                        .map(Some)
                        .map_err(<A::Error as de::Error>::custom)
                }
                Some(
                    "strict_schedule_boundaries"
                    | "record_host_inputs"
                    | "guest_may_inherit_a_terminal"
                    | "in_guest_site_patching_off"
                    | "in_guest_refuses_host_timed_producers"
                    | "in_guest_signal_delivery"
                    | "annotated_priority_replay"
                    | "target_timeslice_syscalls_only"
                    | "futex_wake_yields"
                    | "cap_preemption_charge"
                    | "replaying"
                    | "signal_delivery_check_fatal"
                    | "container_procfs"
                    | "notice_host_network_abstract_binds"
                    | "replay_edited_preemptions"
                    | "lenient_replay"
                    | "eager_lone_preemptions"
                    | "replay_continue_fairly"
                    | "strict_binder"
                    | "rewrite_exclusives_in_memory"
                    | "rewrite_exclusives_overlay_requested"
                    | "count_store_exclusives"
                    | "stacktrace_at_schedule_positions"
                    | "untrap_passthrough_syscalls",
                ) => seed.deserialize(BoolDeserializer::new(false)).map(Some),
                // Empty, as its serde default is.
                Some("rewrite_exclusives_overlay" | "rewrite_exclusives_unmarked") => seed
                    .deserialize(serde_json::Value::Array(Vec::new()))
                    .map(Some)
                    .map_err(<A::Error as de::Error>::custom),
                // Empty, as its serde default is; and 0, the default.
                Some(
                    "in_guest_reserved_signals"
                    | "access_trace_from"
                    | "watch_from"
                    | "chaos_instruction_offset",
                ) => seed.deserialize(U64Deserializer::new(0)).map(Some),
                // None, as its serde default is.
                Some("watch") => seed
                    .deserialize(serde_json::Value::Array(Vec::new()))
                    .map(Some)
                    .map_err(<A::Error as de::Error>::custom),
                // On, as its serde default is.
                Some("signal_state_copy") => {
                    seed.deserialize(BoolDeserializer::new(true)).map(Some)
                }
                // Unset, as its serde default is.
                Some(
                    "in_guest_detlog_forward_policy"
                    | "launch_image"
                    | "detlog_anon"
                    | "scheduler_turn_cost"
                    | "virtual_timeout"
                    | "pipe_wake_latency"
                    | "expired_wait_charge"
                    | "sleep_registration"
                    | "container_mount_table"
                    | "mountinfo_mount_namespace"
                    | "directory_placeholder"
                    | "work_span_report"
                    | "signal_at"
                    | "virtual_cpus"
                    | "chaos_timeslice_range"
                    | "chaos_timeslice"
                    | "access_trace"
                    | "access_trace_max_records"
                    | "access_trace_max_bytes"
                    | "standard_stream_aliases"
                    | "post_crash_timeslice"
                    | "property_snapshot",
                ) => seed
                    .deserialize(serde_json::Value::Null)
                    .map(Some)
                    .map_err(<A::Error as de::Error>::custom),
                // Per-slice, as its serde default is.
                Some("chaos_timeslice_draw") => {
                    let slice = serde_json::to_value(super::ChaosTimesliceDraw::Slice)
                        .map_err(<A::Error as de::Error>::custom)?;
                    seed.deserialize(slice)
                        .map(Some)
                        .map_err(<A::Error as de::Error>::custom)
                }
                // x86-64-v2, as their serde defaults are.
                Some("cpu_profile") => {
                    let v2 = serde_json::to_value(crate::cpu_profile::CpuProfile::X86_64V2)
                        .map_err(<A::Error as de::Error>::custom)?;
                    seed.deserialize(v2)
                        .map(Some)
                        .map_err(<A::Error as de::Error>::custom)
                }
                Some("cpu_profile_host" | "cpu_profile_source") => seed
                    .deserialize(serde_json::Value::Null)
                    .map(Some)
                    .map_err(<A::Error as de::Error>::custom),
                // Numbered by a PID namespace, as its serde default is.
                Some("guest_ids") => {
                    let namespace = serde_json::to_value(crate::pid::GuestIds::Namespace)
                        .map_err(<A::Error as de::Error>::custom)?;
                    seed.deserialize(namespace)
                        .map(Some)
                        .map_err(<A::Error as de::Error>::custom)
                }
                // Unknown, as its serde default is.
                Some("inherited_seccomp") => {
                    let unknown = serde_json::to_value(super::InheritedSeccomp::Unknown)
                        .map_err(<A::Error as de::Error>::custom)?;
                    seed.deserialize(unknown)
                        .map(Some)
                        .map_err(<A::Error as de::Error>::custom)
                }
                _ => self.inner.next_element_seed(seed),
            }
        }
    }

    /// The names of `Config`'s serialized fields in declaration order, which
    /// is the order its derived deserializer reads an array in: no field is
    /// serialized without being deserialized or the reverse.
    ///
    /// The configuration serialized to learn them is built by Clap from no
    /// arguments, with every environment binding removed before parsing.
    /// Building `Config::command()` still reads the bound variables (Clap's
    /// `Arg::env` reads a variable's value when the binding is made), but
    /// removing the bindings discards those values, so no ambient value
    /// reaches the parse. `Config::default()` would instead parse
    /// HERMIT_EPOCH, HERMIT_PRNG, HERMIT_SCHED_SEED and HERMIT_CPU_PROFILE, and exit the process
    /// when one of them does not parse; the legacy decoder was not affected
    /// by any of them.
    fn config_field_order() -> serde_json::Result<Vec<String>> {
        let config = environment_free_config().map_err(<serde_json::Error as de::Error>::custom)?;
        let json = serde_json::to_string(&config)?;
        Ok(serde_json::from_str::<KeyOrder>(&json)?.0)
    }

    /// `Config` as Clap builds it from no arguments and no environment.
    fn environment_free_config() -> Result<Config, clap::Error> {
        use clap::CommandFactory;
        use clap::FromArgMatches;

        let matches = Config::command()
            .mut_args(|arg| arg.env(None::<&'static str>))
            .try_get_matches_from(["config-field-order"])?;
        Config::from_arg_matches(&matches)
    }

    /// The keys of a JSON object, in the order they appear.
    pub(super) struct KeyOrder(pub(super) Vec<String>);

    impl<'de> Deserialize<'de> for KeyOrder {
        fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            struct KeyOrderVisitor;

            impl<'de> Visitor<'de> for KeyOrderVisitor {
                type Value = KeyOrder;

                fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                    formatter.write_str("a JSON object")
                }

                fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<KeyOrder, A::Error> {
                    let mut keys = Vec::new();
                    while let Some(key) = map.next_key::<String>()? {
                        map.next_value::<IgnoredAny>()?;
                        keys.push(key);
                    }
                    Ok(KeyOrder(keys))
                }
            }

            deserializer.deserialize_map(KeyOrderVisitor)
        }
    }

    pub(super) struct ConfigSerializer<'c, S> {
        pub(super) inner: S,
        pub(super) config: &'c Config,
    }

    pub(super) struct ConfigFields<'c, S> {
        inner: S,
        config: &'c Config,
        wrote_backend_keys: bool,
    }

    fn not_a_config<E: serde::ser::Error>() -> E {
        E::custom("the legacy backend JSON encoding applies only to a Detcore Config")
    }

    impl<'c, S: Serializer> Serializer for ConfigSerializer<'c, S> {
        type Ok = S::Ok;
        type Error = S::Error;
        type SerializeSeq = Impossible<S::Ok, S::Error>;
        type SerializeTuple = Impossible<S::Ok, S::Error>;
        type SerializeTupleStruct = Impossible<S::Ok, S::Error>;
        type SerializeTupleVariant = Impossible<S::Ok, S::Error>;
        type SerializeMap = Impossible<S::Ok, S::Error>;
        type SerializeStruct = ConfigFields<'c, S::SerializeStruct>;
        type SerializeStructVariant = Impossible<S::Ok, S::Error>;

        fn is_human_readable(&self) -> bool {
            self.inner.is_human_readable()
        }

        fn serialize_struct(
            self,
            name: &'static str,
            len: usize,
        ) -> Result<Self::SerializeStruct, S::Error> {
            // Two fields are replaced by fifteen keys.
            let len = len + legacy_backend_keys(self.config).len() - 2;
            Ok(ConfigFields {
                inner: self.inner.serialize_struct(name, len)?,
                config: self.config,
                wrote_backend_keys: false,
            })
        }

        fn serialize_bool(self, _: bool) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_i8(self, _: i8) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_i16(self, _: i16) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_i32(self, _: i32) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_i64(self, _: i64) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_u8(self, _: u8) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_u16(self, _: u16) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_u32(self, _: u32) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_u64(self, _: u64) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_f32(self, _: f32) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_f64(self, _: f64) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_char(self, _: char) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_str(self, _: &str) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_bytes(self, _: &[u8]) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_none(self) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_some<T: ?Sized + Serialize>(self, _: &T) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_unit(self) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_unit_struct(self, _: &'static str) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_unit_variant(
            self,
            _: &'static str,
            _: u32,
            _: &'static str,
        ) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_newtype_struct<T: ?Sized + Serialize>(
            self,
            _: &'static str,
            _: &T,
        ) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_newtype_variant<T: ?Sized + Serialize>(
            self,
            _: &'static str,
            _: u32,
            _: &'static str,
            _: &T,
        ) -> Result<S::Ok, S::Error> {
            Err(not_a_config())
        }
        fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, S::Error> {
            Err(not_a_config())
        }
        fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, S::Error> {
            Err(not_a_config())
        }
        fn serialize_tuple_struct(
            self,
            _: &'static str,
            _: usize,
        ) -> Result<Self::SerializeTupleStruct, S::Error> {
            Err(not_a_config())
        }
        fn serialize_tuple_variant(
            self,
            _: &'static str,
            _: u32,
            _: &'static str,
            _: usize,
        ) -> Result<Self::SerializeTupleVariant, S::Error> {
            Err(not_a_config())
        }
        fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, S::Error> {
            Err(not_a_config())
        }
        fn serialize_struct_variant(
            self,
            _: &'static str,
            _: u32,
            _: &'static str,
            _: usize,
        ) -> Result<Self::SerializeStructVariant, S::Error> {
            Err(not_a_config())
        }
    }

    impl<S: SerializeStruct> SerializeStruct for ConfigFields<'_, S> {
        type Ok = S::Ok;
        type Error = S::Error;

        fn serialize_field<T: ?Sized + Serialize>(
            &mut self,
            key: &'static str,
            value: &T,
        ) -> Result<(), S::Error> {
            match key {
                "backend" => {
                    for (legacy_key, legacy_value) in legacy_backend_keys(self.config) {
                        self.inner.serialize_field(legacy_key, &legacy_value)?;
                    }
                    self.wrote_backend_keys = true;
                    Ok(())
                }
                // No legacy key; see FIELDS_WITHOUT_A_LEGACY_KEY.
                "strict_schedule_boundaries"
                | "record_host_inputs"
                | "guest_may_inherit_a_terminal"
                | "in_guest_detlog_forward_policy"
                | "in_guest_site_patching_off"
                | "in_guest_reserved_signals"
                | "in_guest_refuses_host_timed_producers"
                | "launch_image"
                | "target_timeslice_syscalls_only"
                | "futex_wake_yields"
                | "pipe_wake_latency"
                | "expired_wait_charge"
                | "sleep_registration"
                | "replaying"
                | "scheduler_turn_cost"
                | "cap_preemption_charge"
                | "virtual_timeout"
                | "inherited_seccomp"
                | "cpu_profile"
                | "cpu_profile_host"
                | "cpu_profile_source"
                | "signal_state_copy"
                | "container_mount_table"
                | "mountinfo_mount_namespace"
                | "directory_placeholder"
                | "container_procfs"
                | "signal_delivery_check_fatal"
                | "strict_binder"
                | "work_span_report"
                | "signal_at"
                | "post_crash_timeslice"
                | "in_guest_signal_delivery"
                | "annotated_priority_replay"
                | "virtual_cpus"
                | "notice_host_network_abstract_binds"
                | "replay_edited_preemptions"
                | "lenient_replay"
                | "eager_lone_preemptions"
                | "chaos_timeslice_range"
                | "chaos_timeslice_draw"
                | "chaos_timeslice"
                | "guest_ids"
                | "detlog_anon"
                | "standard_stream_aliases"
                | "rewrite_exclusives_unmarked" => Ok(()),
                "access_trace"
                | "access_trace_from"
                | "access_trace_max_records"
                | "access_trace_max_bytes"
                | "rewrite_exclusives_in_memory"
                | "rewrite_exclusives_overlay"
                | "rewrite_exclusives_overlay_requested"
                | "count_store_exclusives"
                | "stacktrace_at_schedule_positions"
                | "property_snapshot"
                | "watch"
                | "watch_from"
                | "chaos_instruction_offset"
                | "untrap_passthrough_syscalls" => Ok(()),
                "replay_continue_fairly" => Ok(()),
                _ => self.inner.serialize_field(key, value),
            }
        }

        fn skip_field(&mut self, key: &'static str) -> Result<(), S::Error> {
            self.inner.skip_field(key)
        }

        fn end(self) -> Result<S::Ok, S::Error> {
            if !self.wrote_backend_keys {
                return Err(S::Error::custom(
                    "Config serialized no `backend` field to replace with the legacy backend keys",
                ));
            }
            self.inner.end()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::parse_post_crash_timeslice;

    /// A set bound is refused, with the reason, wherever it would not be in
    /// effect: not above the run's bound, no bound, unsequentialized threads.
    #[test]
    fn a_post_crash_bound_is_refused_where_it_would_not_take_effect() {
        let nz = std::num::NonZeroU64::new;
        let config = |bound: u64, max: Option<u64>, sequentialize: bool| super::Config {
            post_crash_timeslice: nz(bound),
            max_timeslice: max.and_then(nz),
            sequentialize_threads: sequentialize,
            ..Default::default()
        };
        assert_eq!(
            config(200, Some(100), true).post_crash_timeslice_refusal(),
            None
        );
        assert_eq!(
            super::Config::default().post_crash_timeslice_refusal(),
            None,
            "not set"
        );
        for (max, sequentialize, why) in [
            (Some(200), true, "not above --max-timeslice=200"),
            (Some(300), true, "not above --max-timeslice=300"),
            (None, true, "needs a --max-timeslice"),
            (Some(100), false, "needs sequentialized threads"),
        ] {
            let refusal = config(200, max, sequentialize)
                .post_crash_timeslice_refusal()
                .unwrap();
            assert!(refusal.contains(why), "{refusal}");
        }
    }

    /// `--post-crash-timeslice` takes off, default (the default
    /// `--max-timeslice`) or a bound of at least one RCB.
    #[test]
    fn post_crash_timeslice_parses_off_default_and_bounds() {
        assert_eq!(parse_post_crash_timeslice("off").unwrap(), None);
        assert_eq!(
            parse_post_crash_timeslice("default").unwrap(),
            std::num::NonZeroU64::new(super::DEFAULT_MAX_TIMESLICE)
        );
        assert_eq!(
            parse_post_crash_timeslice("5000000").unwrap(),
            std::num::NonZeroU64::new(5_000_000)
        );
        for bad in ["0", "5", "disabled", "fast"] {
            assert!(parse_post_crash_timeslice(bad).is_err(), "{bad}");
        }
    }

    use super::*;

    /// `--chaos-timeslice-range` takes MIN..MAX virtual nanoseconds, both at
    /// least one RCB and MIN no larger than MAX, prints back as given, needs
    /// `--chaos`, and maps uniform draws log-uniformly onto the range.
    fn range_max(config: &Config) -> NonZeroU64 {
        config.chaos_timeslice_range.unwrap().max
    }

    #[test]
    fn a_chaos_timeslice_range_is_min_to_max_nanoseconds_drawn_log_uniformly() {
        let range: TimesliceRange = "1000..1000000000".parse().unwrap();
        assert_eq!((range.min.get(), range.max.get()), (1_000, 1_000_000_000));
        assert_eq!(range.to_string(), "1000..1000000000");
        assert_eq!("10..10".parse::<TimesliceRange>().unwrap().max.get(), 10);
        for refused in ["1000", "9..1000", "2000..1000", "a..b", "1000..", "..1000"] {
            assert!(refused.parse::<TimesliceRange>().is_err(), "{refused}");
        }
        assert!(
            Config::try_parse_from(["config", "--chaos-timeslice-range=1000..2000"]).is_err(),
            "a range without --chaos was accepted"
        );
        let config =
            Config::try_parse_from(["config", "--chaos", "--chaos-timeslice-range=1000..2000"])
                .unwrap();
        assert_eq!(
            config.chaos_timeslice_range,
            Some("1000..2000".parse().unwrap())
        );
        assert!(
            Config::try_parse_from([
                "config",
                "--chaos",
                "--chaos-timeslice-range=1000..2000",
                "--target-timeslice=1000",
            ])
            .is_err(),
            "a range beside --target-timeslice was accepted"
        );
        // The rendered command line carries the range, not the maximum it
        // sets, and parses back to the same range.
        let mut resolved = config.clone();
        resolved.max_timeslice = Some(range_max(&config));
        let rendered = resolved.to_string();
        assert!(
            rendered.contains(" --chaos-timeslice-range=1000..2000"),
            "{rendered}"
        );
        assert!(!rendered.contains("--max-timeslice"), "{rendered}");
        let reparsed = Config::parse_from(
            std::iter::once("detcore".to_string())
                .chain(rendered.split_whitespace().map(String::from)),
        );
        assert_eq!(reparsed.chaos_timeslice_range, config.chaos_timeslice_range);
        // A saved configuration is checked as the command line is.
        assert_eq!(
            serde_json::to_value(config.chaos_timeslice_range).unwrap(),
            "1000..2000"
        );
        assert!(serde_json::from_str::<TimesliceRange>("\"2000..1000\"").is_err());
        assert_eq!(config.chaos_timeslice_draw, ChaosTimesliceDraw::Slice);
        assert_eq!(config.chaos_timeslice, None);
        let config = Config::try_parse_from([
            "config",
            "--chaos",
            "--chaos-timeslice-range=1000..2000",
            "--chaos-timeslice-draw=run",
        ])
        .unwrap();
        assert_eq!(config.chaos_timeslice_draw, ChaosTimesliceDraw::Run);
        let bare =
            Config::try_parse_from(["config", "--chaos", "--chaos-timeslice-draw=run"]).unwrap();
        assert_eq!(bare.chaos_timeslice_draw, ChaosTimesliceDraw::Run);
        assert_eq!(
            bare.chaos_timeslice_range, None,
            "the CLI resolves the range"
        );
        assert_eq!(
            Config::default().chaos_timeslice_draw,
            ChaosTimesliceDraw::Slice
        );
        for args in [
            vec!["config", "--chaos-timeslice-draw=run"],
            vec!["config", "--chaos", "--chaos-timeslice-draw=slice"],
            vec!["config", "--chaos", "--chaos-timeslice-draw=fixed"],
            vec![
                "config",
                "--chaos",
                "--chaos-timeslice-draw=run",
                "--target-timeslice=1000",
            ],
        ] {
            assert!(Config::try_parse_from(&args).is_err(), "{args:?}");
        }
        let rendered = bare.to_string();
        assert!(
            rendered.contains(" --chaos-timeslice-draw=run"),
            "{rendered}"
        );
        assert!(!rendered.contains("--chaos-timeslice-range"), "{rendered}");
        let reparsed =
            Config::parse_from(std::iter::once("detcore").chain(rendered.split_whitespace()));
        assert_eq!(reparsed.chaos_timeslice_draw, ChaosTimesliceDraw::Run);
        assert_eq!(reparsed.chaos_timeslice_range, None);
        assert_eq!(
            RECOMMENDED_CHAOS_TIMESLICE_RANGE,
            "1000..200000000".parse().unwrap()
        );
        let command = <Config as clap::CommandFactory>::command();
        for id in ["chaos", "chaos_timeslice_draw"] {
            let text = command
                .get_arguments()
                .find(|arg| arg.get_id() == id)
                .unwrap()
                .get_long_help()
                .unwrap()
                .to_string();
            assert!(
                text.contains(&RECOMMENDED_CHAOS_TIMESLICE_RANGE.to_string()),
                "{text}"
            );
            assert!(text.contains("time limits"), "{text}");
        }
        // A drawn maximum renders as the range and the draw, which parse
        // back to the same request.
        let mut drawn = config.clone();
        drawn.max_timeslice = NonZeroU64::new(1234);
        drawn.chaos_timeslice = NonZeroU64::new(1234);
        let rendered = drawn.to_string();
        assert!(
            rendered.contains(" --chaos-timeslice-range=1000..2000 --chaos-timeslice-draw=run"),
            "{rendered}"
        );
        assert!(!rendered.contains("--max-timeslice"), "{rendered}");
        let reparsed = Config::parse_from(
            std::iter::once("detcore".to_string())
                .chain(rendered.split_whitespace().map(String::from)),
        );
        assert_eq!(reparsed.chaos_timeslice_range, config.chaos_timeslice_range);
        assert_eq!(reparsed.chaos_timeslice_draw, ChaosTimesliceDraw::Run);

        // Six decades, an equal share each: u = k/6 lands on 10^(3+k).
        for k in 0..=6 {
            let expected = 10f64.powi(3 + k);
            let point = range.log_uniform(f64::from(k) / 6.0);
            assert!((point / expected - 1.0).abs() < 1e-9, "{k}: {point}");
        }
        let point: TimesliceRange = "4321..4321".parse().unwrap();
        assert_eq!(point.log_uniform(0.7), 4321.0);
    }

    /// `--chaos-instruction-offset` needs a real stop at every chaos slice
    /// end; nothing else does, and the option is inert without `--chaos`.
    #[test]
    fn only_a_chaos_instruction_offset_needs_a_stop_at_every_slice_end() {
        let mut config = Config::default();
        assert!(!config.slice_ends_need_a_stop());
        config.chaos_instruction_offset = 8;
        assert!(!config.slice_ends_need_a_stop());
        config.chaos = true;
        assert!(config.slice_ends_need_a_stop());
        config.chaos_instruction_offset = 0;
        assert!(!config.slice_ends_need_a_stop());
    }

    /// The four capabilities split out of
    /// `supports_blocked_wait_signal_interruption` in A5 step 3c.0 equal it
    /// on every backend, as the readings they replace did, so no backend's
    /// behaviour moves; and `models_signal_targets` is serialized threads and
    /// `kernel_signal_targets_are_predictable`, under both thread modes.
    #[test]
    fn split_signal_facts_equal_their_former_bundle_on_every_backend() {
        // Only ptrace and e9patch set the bundle today.
        let backends = [
            ("ptrace", BackendCapabilities::PTRACE, true),
            ("e9patch", BackendCapabilities::E9PATCH, true),
            ("liteinst", BackendCapabilities::LITEINST_IN_GUEST, false),
            ("sabre", BackendCapabilities::SABRE, false),
            ("dbt", BackendCapabilities::DBT, false),
            ("kvm", BackendCapabilities::KVM, false),
            ("unprepared", DEFAULT_BACKEND_CAPABILITIES, false),
        ];
        for (name, backend, expected) in backends {
            assert_eq!(
                backend.supports_blocked_wait_signal_interruption, expected,
                "{name}"
            );
            assert_eq!(backend.kernel_signal_state_is_guests, expected, "{name}");
            assert_eq!(
                backend.kernel_signal_targets_are_predictable, expected,
                "{name}"
            );
            assert_eq!(
                backend.host_task_reads_reach_guest_threads, expected,
                "{name}"
            );
            assert_eq!(backend.host_timed_signals_are_physical, expected, "{name}");
            // It sets exactly those five: the whole struct after the call
            // equals `backend` with only them flipped by hand.
            let mut flipped = backend;
            flipped.supports_blocked_wait_signal_interruption = !expected;
            flipped.kernel_signal_state_is_guests = !expected;
            flipped.kernel_signal_targets_are_predictable = !expected;
            flipped.host_task_reads_reach_guest_threads = !expected;
            flipped.host_timed_signals_are_physical = !expected;
            let mut toggled = backend;
            set_kernel_signal_facts(&mut toggled, !expected);
            assert_eq!(toggled, flipped, "{name}");
            set_kernel_signal_facts(&mut toggled, expected);
            assert_eq!(toggled, backend, "{name}");
            for sequentialize_threads in [false, true] {
                let cfg = Config {
                    sequentialize_threads,
                    backend,
                    ..Config::default()
                };
                assert_eq!(
                    cfg.models_signal_targets(),
                    sequentialize_threads && expected,
                    "{name}, sequentialize_threads={sequentialize_threads}"
                );
            }
        }
    }

    #[test]
    fn default_epoch_is_2026() {
        assert_eq!(DEFAULT_EPOCH_STR, "2026-01-01T00:00:00Z");
        let epoch = DEFAULT_EPOCH_STR.parse::<DateTime<Utc>>().unwrap();
        assert_eq!(epoch.timestamp(), 1_767_225_600);
    }

    #[test]
    fn resolved_epoch_is_serialized_as_an_exact_config_input() {
        let epoch = "2000-12-31T23:59:59.123456789Z"
            .parse::<DateTime<Utc>>()
            .unwrap();
        let config = Config {
            epoch,
            ..Config::default()
        };
        let encoded = serde_json::to_value(&config).unwrap();
        assert_eq!(
            encoded["epoch"],
            serde_json::json!("2000-12-31T23:59:59.123456789Z")
        );
        let decoded: Config = serde_json::from_value(encoded).unwrap();
        assert_eq!(decoded.epoch, epoch);
    }

    #[test]
    fn default_backend_capabilities_match_instrumented_backends() {
        let config = Config::default();
        assert_eq!(config.backend, DEFAULT_BACKEND_CAPABILITIES);
        assert!(!config.backend.reports_physical_process_exits);
        assert!(config.backend.tracks_process_children);
        assert!(config.backend.runs_exit_robust_list);
        assert!(!config.backend.requires_thread_directed_process_signals);
        assert!(config.backend.supports_parked_write_signal_interruption);
        assert!(!config.sequentialize_threads);
        assert!(
            legacy_backend_keys(&config).contains(&("kvm_shared_dequeue_timers", false)),
            "the default configuration must not write a controlled-run legacy key"
        );
        assert!(!config.backend.supports_blocked_wait_signal_interruption);
        assert!(!config.backend.kernel_signal_state_is_guests);
        assert!(!config.backend.kernel_signal_targets_are_predictable);
        assert!(!config.backend.host_task_reads_reach_guest_threads);
        assert!(!config.backend.host_timed_signals_are_physical);
        assert!(!config.backend.delivers_preemption_timer_events);
        assert!(!config.backend.runs_guest_code_between_injections);
        assert!(config.backend.rcb_clock_contract.is_none());
        assert_eq!(config.rcb_clock_claim, RcbClockClaim::Unset);
        assert!(!config.guest_may_inherit_a_terminal);
        assert!(config.in_guest_detlog_forward_policy.is_none());
        assert!(!config.in_guest_site_patching_off);
        assert_eq!(config.in_guest_reserved_signals, 0);
        assert!(!config.reserves_in_guest_signal(libc::SIGSYS.into()));
        assert!(!config.in_guest_refuses_host_timed_producers);
        assert!(!config.in_guest_signal_delivery);
        assert!(config.launch_image.is_none());
    }

    #[test]
    fn a_config_without_backend_capabilities_deserializes_as_ptrace() {
        let mut encoded = serde_json::to_value(Config::default()).unwrap();
        let fields = encoded.as_object_mut().unwrap();
        assert!(fields.remove("backend").is_some());
        assert!(!fields.contains_key("shared_dequeue_timers"));
        let decoded: Config = serde_json::from_value(encoded).unwrap();
        assert_eq!(decoded.backend, DEFAULT_BACKEND_CAPABILITIES);
    }

    /// `Config`'s own derived deserializer ignores a key it does not name, and
    /// `shared_dequeue_timers` is no longer one it names, so JSON saved while
    /// it was a field still reads and the key decides nothing, whatever it
    /// holds and however often it appears. This is the whole compatibility
    /// policy for the named form, and it is safe because no production code
    /// reads a `Config` by field name: the standalone DBT form is read by
    /// [`from_legacy_backend_json`], which checks the key, and the bincode form
    /// that crosses process boundaries is positional. Removing the field
    /// changed that positional form and [`config_wire_fingerprint`]. SaBRe's
    /// plugin compares the fingerprint and refuses a coordinator from another
    /// build; LiteInst's in-guest runtime does not, so a runtime library built
    /// before the removal and loaded by a newer coordinator can misdecode the
    /// Config and fail its initialization (exit 127). That mixed-build hazard
    /// is not new: every change to the positional form has it.
    #[test]
    fn a_saved_shared_dequeue_timers_field_is_ignored() {
        let baseline = serde_json::to_string(&Config::default()).unwrap();
        for extra in [
            r#""shared_dequeue_timers":false"#,
            r#""shared_dequeue_timers":true"#,
            r#""shared_dequeue_timers":"yes""#,
            r#""shared_dequeue_timers":true,"shared_dequeue_timers":false"#,
        ] {
            let edited = baseline.replacen('{', &format!("{{{extra},"), 1);
            let decoded: Config = serde_json::from_str(&edited).unwrap();
            assert_eq!(
                serde_json::to_string(&decoded).unwrap(),
                baseline,
                "{edited}"
            );
        }
    }

    /// Ptrace's capabilities with the robust-list transition and parked-write
    /// signal interruption both left to Detcore: the value the retired
    /// ptrace-hosted LiteInst runtime reported. No backend reports it now, but
    /// the legacy keys can still express it, so the round trips keep it.
    fn ptrace_without_exit_transitions() -> BackendCapabilities {
        let mut backend = BackendCapabilities::PTRACE;
        backend.runs_exit_robust_list = false;
        backend.supports_parked_write_signal_interruption = false;
        backend
    }

    /// `backend` as the legacy form can carry it: capabilities added after the
    /// form froze never enter it and read back as their default (see
    /// [`process_exits_complete_asynchronously_never_enters_the_legacy_form`]).
    fn legacy_expressible(mut backend: BackendCapabilities) -> BackendCapabilities {
        backend.process_exits_complete_asynchronously = false;
        backend.virtualizes_guest_sigalrm = false;
        set_kernel_signal_facts(&mut backend, false);
        backend.delivers_preemption_timer_events = false;
        backend.runs_guest_code_between_injections = false;
        backend.reports_sigreturns = false;
        // Added after the legacy form froze: it reads back as false (see
        // `LegacyBackendKeys::capabilities`).
        backend.kernel_never_walks_robust_lists = false;
        // Added after the legacy form froze: it reads back as true (see
        // `LegacyBackendKeys::capabilities`).
        backend.provides_guest_backtraces = true;
        // Added after the legacy form froze: they read back as false and the
        // empty set (see `LegacyBackendKeys::capabilities`).
        backend.places_orphans = false;
        backend.publishable_parent_death_signals = 0;
        backend.reports_kernel_child_exit_signals = backend.reports_child_exit_publication;
        // The legacy discover_live_file_metadata bit couples three facts.
        backend.rediscovers_descriptors_after_exec = backend.tool_shares_guest_descriptor_table;
        // The legacy form derives promotion from the thread-directed signal
        // bit, which only DBT, the form's only reader, sets.
        backend.promotes_open_files_before_native_lifecycle =
            backend.requires_thread_directed_process_signals;
        backend.loopback_pollers_yield_to_peers = backend.tool_shares_guest_descriptor_table;
        // The legacy form carries no width for `hardware_watchpoints`: it
        // reads back as 0, the minimal width (see `LegacyBackendKeys::capabilities`).
        backend.hardware_watchpoints = 0;
        // Added after the legacy form froze: it reads back as `None` (see
        // `LegacyBackendKeys::capabilities`).
        backend.rcb_clock_contract = None;
        backend
    }

    #[test]
    fn legacy_backend_json_round_trips_every_backend_capabilities_constant() {
        for (name, backend) in [
            ("PTRACE", BackendCapabilities::PTRACE),
            ("E9PATCH", BackendCapabilities::E9PATCH),
            (
                "ptrace without exit transitions",
                ptrace_without_exit_transitions(),
            ),
            ("LITEINST_IN_GUEST", BackendCapabilities::LITEINST_IN_GUEST),
            ("SABRE", BackendCapabilities::SABRE),
            ("DBT", BackendCapabilities::DBT),
            ("KVM", BackendCapabilities::KVM),
        ] {
            for sequentialize_threads in [false, true] {
                let sent = Config {
                    backend,
                    sequentialize_threads,
                    // A launch sends a resolved profile: the legacy form has
                    // no profile field and reads back as v2, so an unresolved
                    // auto would not round-trip.
                    cpu_profile: crate::cpu_profile::CpuProfile::X86_64V2,
                    ..Config::default()
                };
                let json = to_legacy_backend_json(&sent).unwrap();
                let received = from_legacy_backend_json(&json).unwrap();
                assert_eq!(received.backend, legacy_expressible(backend), "{name}");
                assert_eq!(
                    received.sequentialize_threads, sequentialize_threads,
                    "{name}"
                );
                assert_controlled_key(&json, &backend, sequentialize_threads, name);
                let expressible = Config {
                    backend: legacy_expressible(backend),
                    ..sent.clone()
                };
                assert_eq!(
                    serde_json::to_string(&received).unwrap(),
                    serde_json::to_string(&expressible).unwrap(),
                    "{name}"
                );
                assert_eq!(to_legacy_backend_json(&received).unwrap(), json, "{name}");
                assert_controlled_key_is_checked(&json, &sent, name);
            }
        }
    }

    #[test]
    fn backend_config_json_preserves_every_backend_and_representable_bytes() {
        for (name, backend, needs_envelope) in [
            ("PTRACE", BackendCapabilities::PTRACE, true),
            ("E9PATCH", BackendCapabilities::E9PATCH, true),
            (
                "LITEINST_IN_GUEST",
                BackendCapabilities::LITEINST_IN_GUEST,
                true,
            ),
            ("SABRE", BackendCapabilities::SABRE, true),
            ("DBT", BackendCapabilities::DBT, true),
            // KVM's `provides_guest_backtraces` is false, which the legacy
            // form cannot express (it reads back true), so KVM needs the
            // envelope like the others.
            ("KVM", BackendCapabilities::KVM, true),
        ] {
            for sequentialize_threads in [false, true] {
                let sent = Config {
                    backend,
                    sequentialize_threads,
                    // A launch sends a resolved profile: the legacy form has
                    // no profile field and reads back as v2, so an unresolved
                    // auto would not round-trip.
                    cpu_profile: crate::cpu_profile::CpuProfile::X86_64V2,
                    ..Config::default()
                };
                let legacy = to_legacy_backend_json(&sent).unwrap();
                let encoded = encode_backend_config_json(legacy.clone(), backend).unwrap();
                assert_eq!(encoded != legacy, needs_envelope, "{name}");
                assert_eq!(
                    from_legacy_backend_json(&encoded).is_err(),
                    needs_envelope,
                    "{name}: an envelope must not be accepted as a legacy config"
                );
                let received = decode_backend_config_json(&encoded).unwrap();
                assert_eq!(received.backend, backend, "{name}");
                assert_eq!(
                    serde_json::to_string(&received).unwrap(),
                    serde_json::to_string(&sent).unwrap(),
                    "{name}"
                );
            }
        }
    }

    #[test]
    fn backend_config_json_round_trips_each_independent_capability() {
        let baseline = serde_json::to_value(BackendCapabilities::DBT).unwrap();
        for (name, value) in baseline.as_object().unwrap() {
            let mut edited = baseline.clone();
            // Every capability is a bool except `hardware_watchpoints`, a
            // `u8` width, `publishable_parent_death_signals`, a `u64` set of
            // signal bits, and `rcb_clock_contract`, an optional contract:
            // flip a bool, and move a number to another value (DBT's is 0
            // for both: width 1, and KVM's set), and DBT's absent contract
            // to ptrace's.
            edited[name] = match name.as_str() {
                "hardware_watchpoints" => serde_json::Value::from(1u8),
                "publishable_parent_death_signals" => serde_json::Value::from(
                    BackendCapabilities::KVM.publishable_parent_death_signals,
                ),
                "rcb_clock_contract" => {
                    assert!(value.is_null(), "DBT states no RCB clock contract");
                    serde_json::to_value(BackendCapabilities::PTRACE.rcb_clock_contract).unwrap()
                }
                _ => serde_json::Value::Bool(!value.as_bool().unwrap()),
            };
            let backend = serde_json::from_value(edited).unwrap();
            let sent = Config {
                backend,
                ..Config::default()
            };
            let legacy = to_legacy_backend_json(&sent).unwrap();
            let encoded = encode_backend_config_json(legacy, backend).unwrap();
            let received = decode_backend_config_json(&encoded).unwrap();
            assert_eq!(received.backend, backend, "{name}");
        }
    }

    /// Every backend constant states a listed RCB clock contract (Reverie's
    /// enum is non-exhaustive, so a match cannot catch a new variant), and a
    /// contract no Reverie names is refused, not read as none. Ported from
    /// the pinned guard's capability adaptation; the envelope missing and
    /// duplicate cases are covered by the next test.
    #[test]
    fn every_backend_constant_states_a_listed_rcb_clock_contract() {
        use reverie::RcbClockContract;

        const RCB_CLOCK_CONTRACTS: [Option<RcbClockContract>; 2] = [
            None,
            Some(RcbClockContract::PerfCounterOnTargetThreadUserOnly),
        ];
        for backend in [
            BackendCapabilities::PTRACE,
            BackendCapabilities::E9PATCH,
            BackendCapabilities::LITEINST_IN_GUEST,
            BackendCapabilities::SABRE,
            BackendCapabilities::DBT,
            BackendCapabilities::KVM,
        ] {
            assert!(
                RCB_CLOCK_CONTRACTS.contains(&backend.rcb_clock_contract),
                "{backend:?}"
            );
        }
        assert_eq!(
            BackendCapabilities::PTRACE.rcb_clock_contract,
            Some(RcbClockContract::PerfCounterOnTargetThreadUserOnly)
        );
        let sent = Config {
            backend: BackendCapabilities::LITEINST_IN_GUEST,
            ..Config::default()
        };
        let encoded =
            encode_backend_config_json(to_legacy_backend_json(&sent).unwrap(), sent.backend)
                .unwrap();
        let mut unnamed: serde_json::Value = serde_json::from_str(&encoded).unwrap();
        unnamed["backend"]["rcb_clock_contract"] = "PerfCounterOnSomeOtherThread".into();
        assert!(decode_backend_config_json(&unnamed.to_string()).is_err());
    }

    #[test]
    fn backend_config_json_requires_each_typed_capability_once() {
        let sent = Config {
            backend: BackendCapabilities::LITEINST_IN_GUEST,
            ..Config::default()
        };
        let encoded =
            encode_backend_config_json(to_legacy_backend_json(&sent).unwrap(), sent.backend)
                .unwrap();
        let envelope: serde_json::Value = serde_json::from_str(&encoded).unwrap();
        for (name, value) in envelope["backend"].as_object().unwrap() {
            let mut missing = envelope.clone();
            missing["backend"].as_object_mut().unwrap().remove(name);
            assert!(
                decode_backend_config_json(&missing.to_string()).is_err(),
                "missing {name}"
            );
            let backend = serde_json::to_string(&sent.backend).unwrap();
            let duplicate = backend.replacen('{', &format!("{{\"{name}\":{value},"), 1);
            let duplicate = encoded.replace(&backend, &duplicate);
            assert_ne!(duplicate, encoded);
            assert!(
                decode_backend_config_json(&duplicate).is_err(),
                "duplicate {name}"
            );
        }
    }

    #[test]
    fn backend_config_json_keeps_legacy_object_and_array_semantics() {
        let legacy = to_legacy_backend_json(&Config::default()).unwrap();
        let unknown_duplicates = legacy.replacen(
            '{',
            r#"{"unrecognized":1,"unrecognized":null,"backend":null,"backend":true,"dbt_config_version":null,"dbt_config_version":2,"#,
            1,
        );
        let known_duplicate = legacy.replacen('{', r#"{"seed":1,"#, 1);
        let mistyped = legacy.replacen("\"backend_is_kvm\":false", "\"backend_is_kvm\":null", 1);
        assert_ne!(mistyped, legacy);
        for json in [
            legacy.clone(),
            positional(&legacy),
            unknown_duplicates,
            known_duplicate,
            mistyped,
            format!("{legacy} trailing"),
            "[]".to_owned(),
        ] {
            let old = from_legacy_backend_json(&json);
            let new = decode_backend_config_json(&json);
            match (old, new) {
                (Ok(old), Ok(new)) => assert_eq!(
                    serde_json::to_string(&old).unwrap(),
                    serde_json::to_string(&new).unwrap()
                ),
                (Err(_), Err(_)) => {}
                _ => panic!("legacy parse outcome changed for {json}"),
            }
        }
    }

    /// `kvm_shared_dequeue_timers` is read independently of the other keys,
    /// absent, false or true, and the parse succeeds exactly when the value
    /// read (false when absent) equals `backend_is_kvm &&
    /// sequentialize_threads`. Under that condition the accepted value is the
    /// one the decoded capabilities and `sequentialize_threads` imply, so the
    /// decoded configuration is the one sent; otherwise the error names the
    /// key.
    fn assert_controlled_key_is_checked(json: &str, sent: &Config, name: &str) {
        let controlled = sent.backend.provides_process_signal_control && sent.sequentialize_threads;
        let written = format!("\"kvm_shared_dequeue_timers\":{controlled}");
        assert_eq!(json.matches(&written).count(), 1, "{name}");
        for value in [None, Some(false), Some(true)] {
            let edited = match value {
                None => json.replacen(&format!(",{written}"), "", 1),
                Some(value) => json.replacen(
                    &written,
                    &format!("\"kvm_shared_dequeue_timers\":{value}"),
                    1,
                ),
            };
            assert_eq!(
                edited.contains("kvm_shared_dequeue_timers"),
                value.is_some(),
                "{name} {value:?}"
            );
            let case = format!(
                "{name} sequentialize_threads={} key={value:?}",
                sent.sequentialize_threads
            );
            match from_legacy_backend_json(&edited) {
                Ok(received) => {
                    assert_eq!(value.unwrap_or(false), controlled, "{case} was accepted");
                    // Compared with what the legacy form can carry; see
                    // `legacy_expressible`.
                    let expressible = Config {
                        backend: legacy_expressible(sent.backend),
                        ..sent.clone()
                    };
                    assert_eq!(
                        serde_json::to_string(&received).unwrap(),
                        serde_json::to_string(&expressible).unwrap(),
                        "{case}"
                    );
                }
                Err(error) => {
                    assert_ne!(value.unwrap_or(false), controlled, "{case}: {error}");
                    assert!(
                        error.to_string().contains("kvm_shared_dequeue_timers"),
                        "{case}: {error}"
                    );
                }
            }
        }
    }

    /// The four controlled-key outcomes that differ from simply ignoring the
    /// key, spelled out: each would otherwise run a configuration on a path
    /// other than the one the same JSON selected before.
    #[test]
    fn a_controlled_key_that_contradicts_the_capabilities_fails_the_parse() {
        let kvm = Config {
            backend: BackendCapabilities::KVM,
            sequentialize_threads: true,
            ..Config::default()
        };
        let kvm_json = to_legacy_backend_json(&kvm).unwrap();
        assert!(from_legacy_backend_json(&kvm_json).is_ok());
        let dbt = Config {
            backend: BackendCapabilities::DBT,
            sequentialize_threads: true,
            ..Config::default()
        };
        let dbt_json = to_legacy_backend_json(&dbt).unwrap();
        assert!(from_legacy_backend_json(&dbt_json).is_ok());
        for edited in [
            // KVM, sequentialized, key false: the control was left uninstalled.
            kvm_json.replacen(
                r#""kvm_shared_dequeue_timers":true"#,
                r#""kvm_shared_dequeue_timers":false"#,
                1,
            ),
            // KVM, sequentialized, key absent: read as false, as before.
            kvm_json.replacen(r#","kvm_shared_dequeue_timers":true"#, "", 1),
            // No control capability, key true: the controlled loop was chosen.
            dbt_json.replacen(
                r#""kvm_shared_dequeue_timers":false"#,
                r#""kvm_shared_dequeue_timers":true"#,
                1,
            ),
            // KVM without sequentialized threads, key true.
            to_legacy_backend_json(&Config {
                sequentialize_threads: false,
                ..kvm.clone()
            })
            .unwrap()
            .replacen(
                r#""kvm_shared_dequeue_timers":false"#,
                r#""kvm_shared_dequeue_timers":true"#,
                1,
            ),
        ] {
            assert_ne!(edited, kvm_json);
            assert_ne!(edited, dbt_json);
            let error = from_legacy_backend_json(&edited).unwrap_err();
            assert!(
                error.to_string().contains("kvm_shared_dequeue_timers"),
                "{error}"
            );
        }
    }

    /// Each legacy key decodes as the `Config` field it was: absent, it takes
    /// that field's default, and set alone, it gives exactly the capabilities
    /// that replaced Detcore's reads of that field.
    #[test]
    fn legacy_backend_keys_decode_as_the_fields_they_were() {
        let ptrace_json = to_legacy_backend_json(&Config::default()).unwrap();
        let ptrace: serde_json::Map<String, serde_json::Value> =
            serde_json::from_str(&ptrace_json).unwrap();
        let keys = legacy_backend_keys(&Config::default());

        let mut absent = ptrace.clone();
        for (key, _) in keys {
            assert!(absent.remove(key).is_some(), "{key}");
        }
        let decoded = from_legacy_backend_json(&serde_json::to_string(&absent).unwrap()).unwrap();
        // The capabilities added after the legacy form froze read back as
        // false (`legacy_expressible`), whatever ptrace reports.
        let legacy_ptrace = legacy_expressible(BackendCapabilities::PTRACE);
        assert_eq!(decoded.backend, legacy_ptrace);
        let unflipped =
            serde_json::to_string(&from_legacy_backend_json(&ptrace_json).unwrap()).unwrap();
        assert_eq!(serde_json::to_string(&decoded).unwrap(), unflipped);

        for (key, ptrace_value) in keys {
            let mut expected = legacy_ptrace;
            match key {
                "cpuid_virtualized_by_backend" => {
                    // No backend installs a CPUID policy of its own, so `true`
                    // fails the parse.
                    let mut flipped = ptrace.clone();
                    flipped.insert(key.to_owned(), serde_json::Value::Bool(!ptrace_value));
                    let error = from_legacy_backend_json(&serde_json::to_string(&flipped).unwrap())
                        .unwrap_err();
                    assert!(error.to_string().contains(key), "{error}");
                    continue;
                }
                "syscall_clobbers_virtualized_by_backend"
                | "backend_supports_madvise"
                | "backend_defers_vfork_child_registration" => {
                    // None supplies a capability: either value reads back the
                    // same configuration.
                    let mut flipped = ptrace.clone();
                    flipped.insert(key.to_owned(), serde_json::Value::Bool(!ptrace_value));
                    let decoded =
                        from_legacy_backend_json(&serde_json::to_string(&flipped).unwrap())
                            .unwrap();
                    assert_eq!(serde_json::to_string(&decoded).unwrap(), unflipped, "{key}");
                    continue;
                }
                "discover_live_file_metadata" => {
                    expected.tool_shares_guest_descriptor_table = true;
                    expected.rediscovers_descriptors_after_exec = true;
                    expected.loopback_pollers_yield_to_peers = true;
                }
                "detect_host_clock_futex_timeouts" => {
                    expected.guest_clock_reads_bypass_backend = true
                }
                "cancel_killed_thread_rpcs" => {
                    expected.needs_killed_thread_rpc_cancellation = true;
                    expected.reports_child_exit_publication = false;
                    expected.reports_kernel_child_exit_signals = false;
                }
                "backend_reports_physical_process_exits" => {
                    expected.reports_physical_process_exits = true
                }
                "backend_tracks_process_children" => expected.tracks_process_children = false,
                "backend_runs_exit_robust_list" => expected.runs_exit_robust_list = false,
                "backend_requires_thread_directed_process_signals" => {
                    expected.requires_thread_directed_process_signals = true;
                    expected.promotes_open_files_before_native_lifecycle = true;
                }
                "backend_is_kvm" => {
                    expected.provides_process_signal_control = true;
                    expected.emulates_child_waits = true;
                }
                "kvm_shared_dequeue_timers" => {
                    // Set alone it contradicts `backend_is_kvm`, so the parse
                    // fails; see
                    // `a_controlled_key_that_contradicts_the_capabilities_fails_the_parse`.
                    let mut flipped = ptrace.clone();
                    flipped.insert(key.to_owned(), serde_json::Value::Bool(!ptrace_value));
                    assert!(
                        from_legacy_backend_json(&serde_json::to_string(&flipped).unwrap())
                            .is_err(),
                        "{key}"
                    );
                    continue;
                }
                "backend_supports_parked_write_signal_interruption" => {
                    expected.supports_parked_write_signal_interruption = false
                }
                "backend_virtualizes_capability_prctls" => {
                    // No backend answers the capability prctls itself, so
                    // `true` fails the parse.
                    let mut flipped = ptrace.clone();
                    flipped.insert(key.to_owned(), serde_json::Value::Bool(!ptrace_value));
                    let error = from_legacy_backend_json(&serde_json::to_string(&flipped).unwrap())
                        .unwrap_err();
                    assert!(error.to_string().contains(key), "{error}");
                    continue;
                }
                other => panic!("no expectation for legacy key {other}"),
            }
            let mut flipped = ptrace.clone();
            flipped.insert(key.to_owned(), serde_json::Value::Bool(!ptrace_value));
            let decoded =
                from_legacy_backend_json(&serde_json::to_string(&flipped).unwrap()).unwrap();
            assert_eq!(decoded.backend, expected, "{key}");
            assert_ne!(serde_json::to_string(&decoded).unwrap(), unflipped, "{key}");
        }
    }

    /// The legacy `kvm_shared_dequeue_timers` key holds exactly when the
    /// backend provides process signal control and threads are sequentialized,
    /// the condition under which Detcore installs that control.
    fn assert_controlled_key(
        json: &str,
        backend: &BackendCapabilities,
        sequentialize_threads: bool,
        name: &str,
    ) {
        let object: serde_json::Map<String, serde_json::Value> =
            serde_json::from_str(json).unwrap();
        assert_eq!(
            object["kvm_shared_dequeue_timers"],
            serde_json::Value::Bool(
                backend.provides_process_signal_control && sequentialize_threads
            ),
            "{name} sequentialize_threads={sequentialize_threads}"
        );
    }

    /// A legacy key failed the parse when it held the wrong type, or appeared
    /// twice, while it was a `Config` field; it still does, so a reader falls
    /// back as it did.
    #[test]
    fn a_legacy_backend_key_of_the_wrong_type_fails_the_parse() {
        let json = to_legacy_backend_json(&Config::default()).unwrap();
        assert!(from_legacy_backend_json(&json).is_ok());
        for (key, value) in legacy_backend_keys(&Config::default()) {
            let field = format!("\"{key}\":{value}");
            assert_eq!(json.matches(&field).count(), 1, "{field}");
            let mistyped = json.replacen(&field, &format!("\"{key}\":0"), 1);
            assert!(from_legacy_backend_json(&mistyped).is_err(), "{mistyped}");
            let repeated = json.replacen(&field, &format!("{field},{field}"), 1);
            assert!(from_legacy_backend_json(&repeated).is_err(), "{repeated}");
        }
    }

    /// The entries of a JSON object, in the order they appear.
    fn ordered_entries(json: &str) -> Vec<(String, serde_json::Value)> {
        struct Entries(Vec<(String, serde_json::Value)>);

        impl<'de> Deserialize<'de> for Entries {
            fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                struct EntriesVisitor;

                impl<'de> serde::de::Visitor<'de> for EntriesVisitor {
                    type Value = Entries;

                    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                        formatter.write_str("a JSON object")
                    }

                    fn visit_map<A: serde::de::MapAccess<'de>>(
                        self,
                        mut map: A,
                    ) -> Result<Entries, A::Error> {
                        let mut entries = Vec::new();
                        while let Some(entry) = map.next_entry()? {
                            entries.push(entry);
                        }
                        Ok(Entries(entries))
                    }
                }

                deserializer.deserialize_map(EntriesVisitor)
            }
        }

        serde_json::from_str::<Entries>(json).unwrap().0
    }

    /// The JSON array of `json`'s values in its key order: the same
    /// configuration, listed by position.
    fn positional(json: &str) -> String {
        let values: Vec<serde_json::Value> = ordered_entries(json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        serde_json::to_string(&values).unwrap()
    }

    /// The legacy form's positions are `Config`'s fields in declaration order
    /// with the fifteen legacy keys where `backend` stands and no
    /// `record_host_inputs`, `guest_may_inherit_a_terminal`,
    /// `in_guest_detlog_forward_policy`, `in_guest_site_patching_off`,
    /// `in_guest_reserved_signals`, `in_guest_refuses_host_timed_producers`,
    /// `in_guest_signal_delivery`,
    /// `launch_image`, `target_timeslice_syscalls_only`, `futex_wake_yields`,
    /// `replaying`,
    /// `scheduler_turn_cost`, `cap_preemption_charge`,
    /// `inherited_seccomp`, `replay_edited_preemptions`, `lenient_replay` or `detlog_anon`,
    /// which is the key order the encoder writes.
    #[test]
    fn legacy_positions_are_the_encoded_key_order() {
        let names = legacy_backend_keys(&Config::default()).map(|(name, _)| name);
        assert_eq!(names, LEGACY_BACKEND_KEY_NAMES);

        let fields = serde_json::from_str::<legacy_backend_json::KeyOrder>(
            &serde_json::to_string(&Config::default()).unwrap(),
        )
        .unwrap()
        .0;
        let mut expected = Vec::new();
        for field in &fields {
            match field.as_str() {
                "backend" => expected.extend(names.map(str::to_owned)),
                "strict_schedule_boundaries"
                | "record_host_inputs"
                | "guest_may_inherit_a_terminal"
                | "in_guest_detlog_forward_policy"
                | "in_guest_site_patching_off"
                | "in_guest_reserved_signals"
                | "in_guest_refuses_host_timed_producers"
                | "launch_image"
                | "target_timeslice_syscalls_only"
                | "futex_wake_yields"
                | "pipe_wake_latency"
                | "expired_wait_charge"
                | "sleep_registration"
                | "replaying"
                | "scheduler_turn_cost"
                | "cap_preemption_charge"
                | "virtual_timeout"
                | "inherited_seccomp"
                | "cpu_profile"
                | "cpu_profile_host"
                | "cpu_profile_source"
                | "signal_state_copy"
                | "container_mount_table"
                | "mountinfo_mount_namespace"
                | "directory_placeholder"
                | "container_procfs"
                | "signal_delivery_check_fatal"
                | "strict_binder"
                | "work_span_report"
                | "signal_at"
                | "post_crash_timeslice"
                | "in_guest_signal_delivery"
                | "annotated_priority_replay"
                | "virtual_cpus"
                | "notice_host_network_abstract_binds"
                | "replay_edited_preemptions"
                | "lenient_replay"
                | "eager_lone_preemptions"
                | "chaos_timeslice_range"
                | "chaos_timeslice_draw"
                | "chaos_timeslice"
                | "guest_ids"
                | "detlog_anon"
                | "standard_stream_aliases"
                | "rewrite_exclusives_unmarked" => {}
                "access_trace"
                | "access_trace_from"
                | "access_trace_max_records"
                | "access_trace_max_bytes"
                | "rewrite_exclusives_in_memory"
                | "rewrite_exclusives_overlay"
                | "rewrite_exclusives_overlay_requested"
                | "count_store_exclusives"
                | "stacktrace_at_schedule_positions"
                | "property_snapshot"
                | "watch"
                | "watch_from"
                | "chaos_instruction_offset"
                | "untrap_passthrough_syscalls" => {}
                "replay_continue_fairly" => {}
                other => expected.push(other.to_owned()),
            }
        }
        let encoded = to_legacy_backend_json(&Config::default()).unwrap();
        let keys: Vec<String> = ordered_entries(&encoded)
            .into_iter()
            .map(|(key, _)| key)
            .collect();
        assert_eq!(keys, expected);
        // `backend` becomes fifteen keys; `record_host_inputs`,
        // `guest_may_inherit_a_terminal`,
        // `in_guest_detlog_forward_policy`, `in_guest_site_patching_off`,
        // `in_guest_reserved_signals`, `in_guest_refuses_host_timed_producers`,
        // `launch_image`, `target_timeslice_syscalls_only`, `futex_wake_yields`,
        // `pipe_wake_latency`, `expired_wait_charge`, `sleep_registration`, `replaying`,
        // `scheduler_turn_cost`, `cap_preemption_charge`, `virtual_timeout`,
        // `inherited_seccomp`, `cpu_profile`,
        // `cpu_profile_host`, `cpu_profile_source`,
        // `signal_state_copy`, `container_mount_table`,
        // `guest_may_inherit_a_terminal`, `in_guest_detlog_forward_policy`,
        // `in_guest_site_patching_off`, `in_guest_reserved_signals`,
        // `in_guest_refuses_host_timed_producers`, `launch_image`,
        // `target_timeslice_syscalls_only`, `futex_wake_yields`,
        // `pipe_wake_latency`, `replaying`, `scheduler_turn_cost`,
        // `inherited_seccomp`, `cpu_profile`, `cpu_profile_host`,
        // `cpu_profile_source`, `signal_state_copy`, `container_mount_table`,
        // `mountinfo_mount_namespace`, `directory_placeholder`,
        // `container_procfs`, `signal_delivery_check_fatal`,
        // `work_span_report`, `signal_at`, `in_guest_signal_delivery`,
        // `annotated_priority_replay`, `virtual_cpus`, `replay_edited_preemptions`,
        // `notice_host_network_abstract_binds`, `eager_lone_preemptions`,
        // `chaos_timeslice_range`, `chaos_timeslice_draw`, `chaos_timeslice`,
        // `guest_ids`, `virtual_timeout`, `detlog_anon`,
        // `access_trace`, `access_trace_from`, `access_trace_max_records`,
        // `access_trace_max_bytes`, `replay_continue_fairly`,
        // `standard_stream_aliases`, `sleep_registration`, `strict_binder`,
        // `post_crash_timeslice`, `rewrite_exclusives_in_memory`,
        // `stacktrace_at_schedule_positions`, `count_store_exclusives`,
        // `property_snapshot`, `watch`, `watch_from`, `rewrite_exclusives_overlay`,
        // `chaos_instruction_offset`, `rewrite_exclusives_overlay_requested`,
        // `rewrite_exclusives_unmarked`, `untrap_passthrough_syscalls`
        // and `strict_schedule_boundaries` none: fourteen more keys and
        // sixty fewer (lenient_replay joined them in dh-mvz7y), so forty-six keys fewer than fields.
        assert!(!fields.iter().any(|field| field == "shared_dequeue_timers"));
        assert_eq!(fields.len(), keys.len() + 46);
    }

    /// `record_host_inputs` never enters the legacy form, whatever its value:
    /// the legacy string keeps its bytes, and it reads back as
    /// false from both the object and the array form.
    #[test]
    fn record_host_inputs_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            record_host_inputs: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("record_host_inputs"), "{json}");
        assert!(!from_legacy_backend_json(&json).unwrap().record_host_inputs);
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(!from_legacy_backend_json(&array).unwrap().record_host_inputs);
    }

    /// The files the exclusive rewrite left unchanged never enter the legacy
    /// form, whatever they are: the legacy string keeps its bytes, and they
    /// read back as empty from both the object and the array form.
    #[test]
    fn the_unmarked_rewrite_files_never_enter_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            rewrite_exclusives_unmarked: vec!["/lib64/libstdc++.so.6".into()],
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("rewrite_exclusives"), "{json}");
        let back = from_legacy_backend_json(&json).unwrap();
        assert!(back.rewrite_exclusives_unmarked.is_empty());
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        let back = from_legacy_backend_json(&array).unwrap();
        assert!(back.rewrite_exclusives_unmarked.is_empty());
    }

    /// `process_exits_complete_asynchronously`, added after the legacy form
    /// froze, never enters it, whatever its value, exactly as
    /// `record_host_inputs` does not: the legacy string keeps its bytes, and
    /// it reads back as false from the object and the array form.
    #[test]
    fn process_exits_complete_asynchronously_never_enters_the_legacy_form() {
        let on = Config {
            backend: BackendCapabilities::LITEINST_IN_GUEST,
            ..Config::default()
        };
        assert!(on.backend.process_exits_complete_asynchronously);
        assert!(on.backend.virtualizes_guest_sigalrm);
        let off = Config {
            backend: legacy_expressible(on.backend),
            ..on.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(
            !json.contains("process_exits_complete_asynchronously"),
            "{json}"
        );
        assert!(!json.contains("virtualizes_guest_sigalrm"), "{json}");
        let decoded = from_legacy_backend_json(&json).unwrap();
        assert!(!decoded.backend.process_exits_complete_asynchronously);
        assert!(!decoded.backend.virtualizes_guest_sigalrm);
        let array = positional(&json);
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .backend
                .process_exits_complete_asynchronously
        );
    }

    /// The capabilities added after the legacy form froze that a legacy
    /// configuration reads back as false
    /// (`supports_blocked_wait_signal_interruption` and the four split out of
    /// it, `delivers_preemption_timer_events`,
    /// `runs_guest_code_between_injections`, `reports_sigreturns`,
    /// `kernel_never_walks_robust_lists`, `places_orphans`,
    /// `publishable_parent_death_signals`, which reads back as the empty set,
    /// and `rcb_clock_contract`, which reads back as `None`)
    /// never enter it, whatever their
    /// value: the legacy string keeps its bytes, and they read
    /// back as false from both the object and the array form.
    #[test]
    fn post_freeze_capabilities_never_enter_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let mut on = off.clone();
        set_kernel_signal_facts(&mut on.backend, true);
        on.backend.delivers_preemption_timer_events = true;
        on.backend.runs_guest_code_between_injections = true;
        on.backend.reports_sigreturns = true;
        on.backend.kernel_never_walks_robust_lists = true;
        on.backend.places_orphans = true;
        on.backend.publishable_parent_death_signals =
            BackendCapabilities::KVM.publishable_parent_death_signals;
        on.backend.rcb_clock_contract = BackendCapabilities::PTRACE.rcb_clock_contract;
        assert!(on.backend.rcb_clock_contract.is_some());
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        for key in [
            "supports_blocked_wait_signal_interruption",
            "kernel_signal_state_is_guests",
            "kernel_signal_targets_are_predictable",
            "host_task_reads_reach_guest_threads",
            "host_timed_signals_are_physical",
            "delivers_preemption_timer_events",
            "runs_guest_code_between_injections",
            "reports_sigreturns",
            "kernel_never_walks_robust_lists",
            "places_orphans",
            "publishable_parent_death_signals",
            "rcb_clock_contract",
        ] {
            assert!(!json.contains(key), "{json}");
        }
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        for decoded in [&json, &array] {
            let backend = from_legacy_backend_json(decoded).unwrap().backend;
            assert!(!backend.supports_blocked_wait_signal_interruption);
            assert!(!backend.kernel_signal_state_is_guests);
            assert!(!backend.kernel_signal_targets_are_predictable);
            assert!(!backend.host_task_reads_reach_guest_threads);
            assert!(!backend.host_timed_signals_are_physical);
            assert!(!backend.delivers_preemption_timer_events);
            assert!(!backend.runs_guest_code_between_injections);
            assert!(!backend.reports_sigreturns);
            assert!(!backend.kernel_never_walks_robust_lists);
            assert!(!backend.places_orphans);
            assert_eq!(backend.publishable_parent_death_signals, 0);
            assert_eq!(backend.rcb_clock_contract, None);
            assert_eq!(backend, legacy_expressible(BackendCapabilities::DBT));
        }
    }

    /// `guest_may_inherit_a_terminal` never enters the legacy form, whatever
    /// its value: the legacy string keeps its bytes, and it reads
    /// back as false from both the object and the array form.
    /// `in_guest_detlog_forward_policy` is unset for DBT, so the legacy form
    /// never carries it, and it reads back as unset.
    #[test]
    fn in_guest_detlog_forward_policy_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            in_guest_detlog_forward_policy: Some("1".to_owned()),
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("in_guest_detlog_forward_policy"), "{json}");
        assert!(
            from_legacy_backend_json(&json)
                .unwrap()
                .in_guest_detlog_forward_policy
                .is_none()
        );
    }

    /// `launch_namespace_mounts` is never serialized: neither DBT's legacy
    /// form nor the configuration's own (which recordings keep) carries it,
    /// both read back with an empty table, and the run-start log line shows
    /// only its size.
    #[test]
    fn launch_namespace_mounts_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            launch_namespace_mounts: crate::host_input::LaunchNamespaceMounts(vec![
                (29, "ext4".to_owned()),
                (31, "devtmpfs".to_owned()),
            ]),
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("launch_namespace_mounts"), "{json}");
        assert!(
            from_legacy_backend_json(&json)
                .unwrap()
                .launch_namespace_mounts
                .0
                .is_empty()
        );
        let full = serde_json::to_string(&on).unwrap();
        assert!(!full.contains("launch_namespace_mounts"), "{full}");
        assert!(
            serde_json::from_str::<Config>(&full)
                .unwrap()
                .launch_namespace_mounts
                .0
                .is_empty()
        );
        assert_eq!(
            format!("{:?}", on.launch_namespace_mounts),
            "LaunchNamespaceMounts { mounts: 2 }"
        );
    }

    /// `launch_image` has no legacy key, so the legacy form never carries
    /// it, and it reads back as unset from both the object and the array
    /// form.
    #[test]
    fn launch_image_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            launch_image: Some(LaunchImage {
                exe: Some(PathBuf::from("/usr/bin/true")),
                launcher: Some(PathBuf::from("/usr/bin/env")),
            }),
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("launch_image"), "{json}");
        assert!(
            from_legacy_backend_json(&json)
                .unwrap()
                .launch_image
                .is_none()
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            from_legacy_backend_json(&array)
                .unwrap()
                .launch_image
                .is_none()
        );
    }

    /// `in_guest_site_patching_off` is false for DBT, so the legacy form never
    /// carries it, and it reads back as false from both the object and the
    /// array form.
    #[test]
    fn in_guest_site_patching_off_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            in_guest_site_patching_off: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("in_guest_site_patching_off"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .in_guest_site_patching_off
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .in_guest_site_patching_off
        );
    }

    #[test]
    fn guest_may_inherit_a_terminal_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            guest_may_inherit_a_terminal: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("guest_may_inherit_a_terminal"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .guest_may_inherit_a_terminal
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .guest_may_inherit_a_terminal
        );
    }

    /// `guest_ids` never enters the legacy form, whatever its value: the
    /// legacy string keeps its bytes, and it reads back as numbered by a PID
    /// namespace from both the object and the array form.
    #[test]
    fn the_guest_numbering_never_enters_the_legacy_form() {
        let namespace = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let chosen = Config {
            guest_ids: crate::pid::GuestIds::Chosen {
                base: 5000,
                refuse_host_ids: true,
            },
            ..namespace.clone()
        };
        let json = to_legacy_backend_json(&chosen).unwrap();
        assert_eq!(json, to_legacy_backend_json(&namespace).unwrap());
        assert!(!json.contains("guest_ids"), "{json}");
        assert_eq!(
            from_legacy_backend_json(&json).unwrap().guest_ids,
            crate::pid::GuestIds::Namespace
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert_eq!(
            from_legacy_backend_json(&array).unwrap().guest_ids,
            crate::pid::GuestIds::Namespace
        );
    }

    /// `chaos_instruction_offset` never enters the legacy form, whatever its
    /// value: the legacy string keeps its bytes, and it reads back as 0 from
    /// both the object and the array form.
    #[test]
    fn chaos_instruction_offset_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            chaos: true,
            chaos_instruction_offset: 16,
            ..off.clone()
        };
        let chaos = Config {
            chaos: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&chaos).unwrap());
        assert!(!json.contains("chaos_instruction_offset"), "{json}");
        assert_eq!(
            from_legacy_backend_json(&json)
                .unwrap()
                .chaos_instruction_offset,
            0
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert_eq!(
            from_legacy_backend_json(&array)
                .unwrap()
                .chaos_instruction_offset,
            0
        );
        assert!(
            !to_legacy_backend_json(&off)
                .unwrap()
                .contains("chaos_instruction_offset")
        );
    }

    /// `inherited_seccomp` never enters the legacy form, whatever its value:
    /// the legacy string keeps its bytes, and it reads back as
    /// unknown from both the object and the array form.
    #[test]
    fn the_cpu_profile_never_enters_the_legacy_form() {
        let v2 = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let v3 = Config {
            cpu_profile: crate::cpu_profile::CpuProfile::X86_64V3,
            cpu_profile_source: Some(17),
            ..v2.clone()
        };
        let json = to_legacy_backend_json(&v3).unwrap();
        assert_eq!(json, to_legacy_backend_json(&v2).unwrap());
        assert!(!json.contains("cpu_profile"), "{json}");
        assert_eq!(
            from_legacy_backend_json(&json).unwrap().cpu_profile,
            crate::cpu_profile::CpuProfile::X86_64V2
        );
        assert_eq!(
            from_legacy_backend_json(&json).unwrap().cpu_profile_source,
            None
        );
        // The bincode form the tool reads carries all profile fields.
        let wire = bincode::serde::encode_to_vec(&v3, bincode::config::legacy()).unwrap();
        let (decoded, _): (Config, usize) =
            bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
        assert_eq!(
            decoded.cpu_profile,
            crate::cpu_profile::CpuProfile::X86_64V3
        );
        assert_eq!(decoded.cpu_profile_source, Some(17));
    }

    #[test]
    fn inherited_seccomp_never_enters_the_legacy_form() {
        use crate::config::InheritedSeccomp;
        let unknown = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        assert_eq!(unknown.inherited_seccomp, InheritedSeccomp::Unknown);
        let measured = Config {
            inherited_seccomp: InheritedSeccomp::Filter(Some(2)),
            ..unknown.clone()
        };
        let json = to_legacy_backend_json(&measured).unwrap();
        assert_eq!(json, to_legacy_backend_json(&unknown).unwrap());
        assert!(!json.contains("inherited_seccomp"), "{json}");
        assert_eq!(
            from_legacy_backend_json(&json).unwrap().inherited_seccomp,
            InheritedSeccomp::Unknown
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert_eq!(
            from_legacy_backend_json(&array).unwrap().inherited_seccomp,
            InheritedSeccomp::Unknown
        );
    }

    /// `Config` crosses Reverie RPC as legacy bincode, which is positional, so
    /// `inherited_seccomp` must be encoded whatever its value, the number of
    /// filters included.
    #[test]
    fn inherited_seccomp_round_trips_through_reverie_bincode() {
        use crate::config::InheritedSeccomp;
        for inherited_seccomp in [
            InheritedSeccomp::Unknown,
            InheritedSeccomp::Disabled,
            InheritedSeccomp::Strict,
            InheritedSeccomp::Filter(None),
            InheritedSeccomp::Filter(Some(3)),
        ] {
            let config = Config {
                inherited_seccomp,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.inherited_seccomp, inherited_seccomp);
            assert_eq!(
                bincode::serde::encode_to_vec(&decoded, bincode::config::legacy()).unwrap(),
                wire
            );
        }
    }

    /// `Seccomp` gives the mode, and `Seccomp_filters` the number of filters in
    /// filter mode.
    #[test]
    fn inherited_seccomp_is_read_from_a_status_file() {
        use crate::config::InheritedSeccomp;
        let status = |lines: &str| {
            InheritedSeccomp::parse(&format!(
                "Name:\tguest\nNoNewPrivs:\t1\n{lines}Speculation_Store_Bypass:\tthread vulnerable\n"
            ))
        };
        assert_eq!(
            status("Seccomp:\t0\nSeccomp_filters:\t0\n"),
            InheritedSeccomp::Disabled
        );
        assert_eq!(
            status("Seccomp:\t1\nSeccomp_filters:\t0\n"),
            InheritedSeccomp::Strict
        );
        assert_eq!(
            status("Seccomp:\t2\nSeccomp_filters:\t3\n"),
            InheritedSeccomp::Filter(Some(3))
        );
        assert_eq!(status("Seccomp:\t2\n"), InheritedSeccomp::Filter(None));
        assert_eq!(status(""), InheritedSeccomp::Unknown);
        assert_eq!(status("Seccomp:\t7\n"), InheritedSeccomp::Unknown);
        // This thread's own state is readable.
        assert_ne!(InheritedSeccomp::measure(), InheritedSeccomp::Unknown);
    }

    /// `target_timeslice_syscalls_only` never enters the legacy form, whatever
    /// its value: the legacy string keeps its bytes, and it reads
    /// back as false from both the object and the array form.
    #[test]
    fn target_timeslice_syscalls_only_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            target_timeslice: NonZeroU64::new(1_000_000),
            ..Config::default()
        };
        let on = Config {
            target_timeslice_syscalls_only: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("target_timeslice_syscalls_only"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .target_timeslice_syscalls_only
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .target_timeslice_syscalls_only
        );
    }

    #[test]
    fn a_futex_wake_yields_only_when_it_woke_a_waiter() {
        let config = Config {
            futex_wake_yields: true,
            ..Config::default()
        };
        assert!(config.yields_after_futex_wake(1));
        assert!(config.yields_after_futex_wake(3));
        // A wake that found no waiter hands nothing off and keeps the slice.
        assert!(!config.yields_after_futex_wake(0));
        // Off by default.
        assert!(!Config::default().futex_wake_yields);
        assert!(!Config::default().yields_after_futex_wake(1));
    }

    #[test]
    fn a_futex_wake_yields_under_replay_too() {
        // Neither record holds the queue order the policy creates, so replay
        // must make the same decision as the recorded run.
        let config = Config {
            futex_wake_yields: true,
            ..Config::default()
        };
        let preemption_replay = Config {
            replay_preemptions_from: Some(PathBuf::from("/nonexistent/preemptions.json")),
            ..config.clone()
        };
        assert!(preemption_replay.yields_after_futex_wake(1));
        assert!(!preemption_replay.yields_after_futex_wake(0));
        let schedule_replay = Config {
            replay_schedule_from: Some(PathBuf::from("/nonexistent/schedule.json")),
            ..config
        };
        assert!(schedule_replay.yields_after_futex_wake(1));
    }

    #[test]
    fn scheduler_turn_charge_refuses_a_scaled_charge_below_one_nanosecond() {
        let default = Config::default();
        assert_eq!(default.scheduler_turn_charge(), Ok(500_000));
        let scaled = Config {
            clock_multiplier: Some(0.1),
            ..Config::default()
        };
        assert_eq!(scaled.scheduler_turn_charge(), Ok(50_000));
        let cost = |cost: u64, multiplier: f64| Config {
            scheduler_turn_cost: NonZeroU64::new(cost),
            clock_multiplier: Some(multiplier),
            ..Config::default()
        };
        assert_eq!(cost(10_000, 0.1).scheduler_turn_charge(), Ok(1_000));
        assert_eq!(cost(10, 0.1).scheduler_turn_charge(), Ok(1));
        // Truncation would make these zero: a finite poll would never expire.
        assert!(cost(9, 0.1).scheduler_turn_charge().is_err());
        assert!(cost(1, 0.1).scheduler_turn_charge().is_err());
        assert_eq!(
            cost(MAX_SCHEDULER_TURN_COST, 1.0).scheduler_turn_charge(),
            Ok(MAX_SCHEDULER_TURN_COST)
        );
        assert!(
            cost(MAX_SCHEDULER_TURN_COST + 1, 1.0)
                .scheduler_turn_charge()
                .is_err()
        );
        assert!(cost(u64::MAX, 1.0).scheduler_turn_charge().is_err());
        assert!(
            cost(MAX_SCHEDULER_TURN_COST, 1e12)
                .scheduler_turn_charge()
                .is_err()
        );
        // The scaled charge has the same upper bound as the cost: one charge
        // of 9e18 ns would exhaust the clock in two turns.
        assert!(
            cost(MAX_SCHEDULER_TURN_COST, 9e9)
                .scheduler_turn_charge()
                .is_err()
        );
        assert!(cost(1_000_000, 1_001.0).scheduler_turn_charge().is_err());
        assert_eq!(
            cost(1_000_000, 1_000.0).scheduler_turn_charge(),
            Ok(MAX_SCHEDULER_TURN_COST)
        );
        let message = cost(MAX_SCHEDULER_TURN_COST, 9e9)
            .scheduler_turn_charge()
            .unwrap_err();
        assert!(message.contains("at least 1"), "{message}");
        assert!(message.contains("at most 1000000000"), "{message}");
    }

    /// The in-memory rewrite admission every launch path applies: a host
    /// without LSE and a run with concurrent threads are each refused,
    /// and the serialized run on an LSE host is admitted.
    #[test]
    fn rewrite_exclusives_in_memory_admission_refuses_what_the_cli_refuses() {
        let on = Config {
            rewrite_exclusives_in_memory: true,
            sequentialize_threads: true,
            ..Config::default()
        };
        assert_eq!(
            on.rewrite_exclusives_in_memory_refusal(false),
            Some(RewriteExclusivesRefusal::HostLacksLseAtomics)
        );
        let concurrent = Config {
            sequentialize_threads: false,
            ..on.clone()
        };
        assert_eq!(
            concurrent.rewrite_exclusives_in_memory_refusal(true),
            Some(RewriteExclusivesRefusal::ThreadsNotSequentialized)
        );
        assert_eq!(on.rewrite_exclusives_in_memory_refusal(true), None);
        assert_eq!(
            Config::default().rewrite_exclusives_in_memory_refusal(false),
            None
        );
    }

    /// `rewrite_exclusives_in_memory` never enters the legacy form, whatever its value: the
    /// legacy string keeps its bytes, and it reads back as false from both
    /// the object and the array form.
    #[test]
    fn rewrite_exclusives_in_memory_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            rewrite_exclusives_in_memory: true,
            rewrite_exclusives_overlay: vec!["/bin/guest".into()],
            rewrite_exclusives_overlay_requested: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("rewrite_exclusives"), "{json}");
        let back = from_legacy_backend_json(&json).unwrap();
        assert!(!back.rewrite_exclusives_in_memory);
        assert!(back.rewrite_exclusives_overlay.is_empty());
        assert!(!back.rewrite_exclusives_overlay_requested);
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        let back = from_legacy_backend_json(&array).unwrap();
        assert!(!back.rewrite_exclusives_in_memory);
        assert!(back.rewrite_exclusives_overlay.is_empty());
        assert!(!back.rewrite_exclusives_overlay_requested);
    }

    /// `rewrite_exclusives_overlay` never enters the legacy form, whatever
    /// its value: the legacy string keeps its bytes, and it reads back as
    /// empty from both the object and the array form.
    #[test]
    fn rewrite_exclusives_overlay_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            rewrite_exclusives_overlay: vec!["/bin/guest".into(), "/lib/libatomic.so.1".into()],
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("rewrite_exclusives_overlay"), "{json}");
        assert!(
            from_legacy_backend_json(&json)
                .unwrap()
                .rewrite_exclusives_overlay
                .is_empty()
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            from_legacy_backend_json(&array)
                .unwrap()
                .rewrite_exclusives_overlay
                .is_empty()
        );
    }

    /// `count_store_exclusives` never enters the legacy form, whatever its
    /// value: the legacy string keeps its bytes, and it reads back as false
    /// from both the object and the array form.
    #[test]
    fn count_store_exclusives_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            count_store_exclusives: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("count_store_exclusives"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .count_store_exclusives
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .count_store_exclusives
        );
    }

    /// `untrap_passthrough_syscalls` never enters the legacy form, whatever its
    /// value: the legacy string keeps its bytes, and it reads
    /// back as false from both the object and the array form.
    #[test]
    fn untrap_passthrough_syscalls_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            untrap_passthrough_syscalls: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("untrap_passthrough_syscalls"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .untrap_passthrough_syscalls
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .untrap_passthrough_syscalls
        );
    }

    /// `futex_wake_yields` never enters the legacy form, whatever its
    /// value: the legacy string keeps its bytes, and it reads
    /// back as false from both the object and the array form.
    #[test]
    fn futex_wake_yields_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            futex_wake_yields: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("futex_wake_yields"), "{json}");
        assert!(!from_legacy_backend_json(&json).unwrap().futex_wake_yields);
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(!from_legacy_backend_json(&array).unwrap().futex_wake_yields);
    }

    /// `scheduler_turn_cost` never enters the legacy form, whatever its value:
    /// the legacy string keeps its bytes, and it reads back as
    /// unset from both the object and the array form.
    #[test]
    fn scheduler_turn_cost_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            scheduler_turn_cost: NonZeroU64::new(10_000),
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("scheduler_turn_cost"), "{json}");
        assert_eq!(
            from_legacy_backend_json(&json).unwrap().scheduler_turn_cost,
            None
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert_eq!(
            from_legacy_backend_json(&array)
                .unwrap()
                .scheduler_turn_cost,
            None
        );
    }

    /// `virtual_timeout` crosses Reverie RPC in the positional bincode form,
    /// set or not, and is rendered into the reproduce command; it never enters
    /// DBT's legacy form, whose runtime `hermit run` refuses it for.
    #[test]
    fn virtual_timeout_round_trips_but_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            virtual_timeout: NonZeroU64::new(3_500_000_000),
            ..off.clone()
        };
        assert!(
            on.to_string().contains(" --virtual-timeout=3500000000"),
            "{on}"
        );
        assert!(!off.to_string().contains("--virtual-timeout"), "{off}");
        for config in [&off, &on] {
            let wire = bincode::serde::encode_to_vec(config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.virtual_timeout, config.virtual_timeout);
        }
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("virtual_timeout"), "{json}");
        assert_eq!(
            from_legacy_backend_json(&json).unwrap().virtual_timeout,
            None
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert_eq!(
            from_legacy_backend_json(&array).unwrap().virtual_timeout,
            None
        );
    }

    /// `cap_preemption_charge` never enters the legacy form, whatever its
    /// value: the legacy string keeps its bytes, and it reads
    /// back as false from both the object and the array form.
    #[test]
    fn cap_preemption_charge_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            cap_preemption_charge: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("cap_preemption_charge"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .cap_preemption_charge
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .cap_preemption_charge
        );
    }

    /// `--cap-preemption-charge` is off by default, parses, and survives the
    /// rendered command line and serde, so a recorded configuration replays
    /// with the rule it ran with.
    #[test]
    fn cap_preemption_charge_is_opt_in_and_round_trips() {
        let default = Config::default();
        assert!(!default.cap_preemption_charge);
        assert!(!default.to_string().contains("--cap-preemption-charge"));
        let config = Config::parse_from(["detcore", "--cap-preemption-charge"]);
        assert!(config.cap_preemption_charge);
        let rendered = config.to_string();
        assert!(rendered.contains(" --cap-preemption-charge"), "{rendered}");
        let reparsed =
            Config::parse_from(std::iter::once("detcore").chain(rendered.split_whitespace()));
        assert!(reparsed.cap_preemption_charge);
        let json = serde_json::to_string(&config).unwrap();
        let back: Config = serde_json::from_str(&json).unwrap();
        assert!(back.cap_preemption_charge);
        let mut legacy: serde_json::Value = serde_json::to_value(&default).unwrap();
        legacy
            .as_object_mut()
            .unwrap()
            .remove("cap_preemption_charge");
        let old: Config = serde_json::from_value(legacy).unwrap();
        assert!(
            !old.cap_preemption_charge,
            "a configuration from before the option reads as off"
        );
    }

    /// `replaying` never enters the legacy form, whatever its value: the
    /// legacy string keeps its bytes, and it reads back as false
    /// from both the object and the array form.
    #[test]
    fn replaying_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            replaying: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("replaying"), "{json}");
        assert!(!from_legacy_backend_json(&json).unwrap().replaying);
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(!from_legacy_backend_json(&array).unwrap().replaying);
    }

    /// `Config` crosses Reverie RPC as legacy bincode, which is positional, so
    /// `replaying` must be encoded whatever its value: a field left out when
    /// false would shift every later field for the decoder.
    #[test]
    fn replaying_round_trips_through_reverie_bincode() {
        for replaying in [false, true] {
            let config = Config {
                replaying,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.replaying, replaying);
            assert_eq!(
                bincode::serde::encode_to_vec(&decoded, bincode::config::legacy()).unwrap(),
                wire
            );
        }
    }

    /// `signal_at` came after the legacy form froze, so it never enters it,
    /// and both the object and the array form read it back as unset.
    #[test]
    fn signal_at_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            signal_at: Some(SignalAt {
                turn: 500,
                signal: libc::SIGUSR1,
            }),
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("signal_at"), "{json}");
        assert_eq!(from_legacy_backend_json(&json).unwrap().signal_at, None);
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert_eq!(from_legacy_backend_json(&array).unwrap().signal_at, None);
    }

    /// `virtual_cpus` came after the legacy form froze, so it never enters
    /// it: the DBT runtime's environment keeps its bytes, and the option
    /// reads back as unset (`hermit run` refuses it with DBT).
    #[test]
    fn virtual_cpus_never_enter_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            virtual_cpus: NonZeroU32::new(4),
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("virtual_cpus"), "{json}");
        assert_eq!(from_legacy_backend_json(&json).unwrap().virtual_cpus, None);
    }

    /// `--virtual-cpus` takes a count from 1 to the 128 CPUs Detcore's mask
    /// holds, and names the range when refused; it reaches the reproducer
    /// command line.
    #[test]
    fn virtual_cpus_parse_from_1_to_128() {
        let parse = |count: &str| {
            Config::try_parse_from(["detcore".to_owned(), format!("--virtual-cpus={count}")])
        };
        assert_eq!(parse("1").unwrap().virtual_cpus, NonZeroU32::new(1));
        assert_eq!(parse("128").unwrap().virtual_cpus, NonZeroU32::new(128));
        for refused in ["0", "129", "-1", "four"] {
            let error = parse(refused).unwrap_err().to_string();
            assert!(error.contains("from 1 to 128"), "{refused}: {error}");
        }
        let config = parse("8").unwrap();
        assert_eq!(config.virtual_cpu_count(), 8);
        assert!(config.to_string().contains(" --virtual-cpus=8"));
        assert_eq!(Config::default().virtual_cpus, None);
        assert_eq!(Config::default().virtual_cpu_count(), 1);
        assert!(!Config::default().to_string().contains("virtual-cpus"));
    }

    /// `eager_lone_preemptions` came after the legacy form froze, so it never
    /// enters it (DBT has no PMU timer, where it would act), and both the
    /// object and the array form read it back as false.
    #[test]
    fn eager_lone_preemptions_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            eager_lone_preemptions: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("eager_lone_preemptions"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .eager_lone_preemptions
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .eager_lone_preemptions
        );
    }

    /// `stacktrace_at_schedule_positions` came after the legacy form froze, so
    /// it never enters it, and both the object and the array form read it
    /// back as false.
    #[test]
    fn stacktrace_at_schedule_positions_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            stacktrace_at_schedule_positions: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("stacktrace_at_schedule_positions"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .stacktrace_at_schedule_positions
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .stacktrace_at_schedule_positions
        );
    }

    /// `--stacktrace-at-schedule-positions` needs a replayed schedule, is
    /// refused with `--strict-schedule-boundaries` (whose indices are
    /// boundary-stream positions already), and round-trips through the
    /// configuration's command line.
    #[test]
    fn stacktrace_at_schedule_positions_needs_a_replayed_schedule_and_round_trips() {
        assert!(Config::try_parse_from(["detcore", "--stacktrace-at-schedule-positions"]).is_err());
        assert!(
            Config::try_parse_from([
                "detcore",
                "--replay-schedule-from=schedule.json",
                "--strict-schedule-boundaries",
                "--stacktrace-at-schedule-positions",
            ])
            .is_err()
        );
        let config = Config::try_parse_from([
            "detcore",
            "--replay-schedule-from=schedule.json",
            "--stacktrace-event=7",
            "--stacktrace-at-schedule-positions",
        ])
        .unwrap();
        assert!(config.stacktrace_at_schedule_positions);
        let rendered = config.to_string();
        assert!(
            rendered.contains(" --stacktrace-at-schedule-positions"),
            "{rendered}"
        );
        let words: Vec<String> = shell_words::split(&rendered)
            .unwrap()
            .into_iter()
            .filter(|word| word.starts_with("--replay-") || word.starts_with("--stacktrace-"))
            .collect();
        let reparsed =
            Config::try_parse_from(std::iter::once("detcore".to_owned()).chain(words)).unwrap();
        assert!(reparsed.stacktrace_at_schedule_positions, "{rendered}");
        assert_eq!(
            reparsed.stacktrace_event, config.stacktrace_event,
            "{rendered}"
        );
    }

    /// `Config` crosses Reverie RPC as positional legacy bincode, so
    /// `signal_at` is encoded whether set or not.
    #[test]
    fn signal_at_round_trips_through_reverie_bincode() {
        for signal_at in [
            None,
            Some(SignalAt {
                turn: 3000,
                signal: libc::SIGUSR1,
            }),
        ] {
            let config = Config {
                signal_at,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.signal_at, signal_at);
        }
    }

    /// `notice_host_network_abstract_binds` came after the legacy form froze, so it never
    /// enters it, and both the object and the array form read it back as
    /// false.
    #[test]
    fn notice_host_network_abstract_binds_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            notice_host_network_abstract_binds: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(
            !json.contains("notice_host_network_abstract_binds"),
            "{json}"
        );
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .notice_host_network_abstract_binds
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .notice_host_network_abstract_binds
        );
    }

    /// `Config` crosses Reverie RPC as positional legacy bincode, so
    /// `notice_host_network_abstract_binds` is encoded whether set or not.
    #[test]
    fn notice_host_network_abstract_binds_round_trips_through_reverie_bincode() {
        for notice_host_network_abstract_binds in [false, true] {
            let config = Config {
                notice_host_network_abstract_binds,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(
                decoded.notice_host_network_abstract_binds,
                notice_host_network_abstract_binds
            );
        }
    }

    /// The access trace's four fields came after the legacy form froze, so
    /// they never enter it, and both the object and the array form read them
    /// back as unset, 0, unset and unset. Set or not, they cross Reverie RPC's positional
    /// bincode and the CLI spelling (`Display`) unchanged.
    #[test]
    fn access_trace_never_enters_the_legacy_form_and_round_trips() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            access_trace: Some(PathBuf::from("/var/run 1.access")),
            access_trace_from: 7586,
            access_trace_max_records: Some(40),
            access_trace_max_bytes: Some(4096),
            watch: vec!["0x404010:4:rw".parse().unwrap()],
            watch_from: 12,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("access_trace"), "{json}");
        let decoded = from_legacy_backend_json(&json).unwrap();
        assert_eq!((decoded.access_trace, decoded.access_trace_from), (None, 0));
        assert_eq!(decoded.access_trace_max_records, None);
        assert_eq!(decoded.access_trace_max_bytes, None);
        assert_eq!((decoded.watch, decoded.watch_from), (vec![], 0));
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        let decoded = from_legacy_backend_json(&array).unwrap();
        assert_eq!((decoded.access_trace, decoded.access_trace_from), (None, 0));
        assert_eq!(
            (
                decoded.access_trace_max_records,
                decoded.access_trace_max_bytes
            ),
            (None, None)
        );

        for config in [Config::default(), on.clone()] {
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.access_trace, config.access_trace);
            assert_eq!(decoded.access_trace_from, config.access_trace_from);
            assert_eq!(
                decoded.access_trace_max_records,
                config.access_trace_max_records
            );
            assert_eq!(
                decoded.access_trace_max_bytes,
                config.access_trace_max_bytes
            );
            assert_eq!(
                (decoded.watch, decoded.watch_from),
                (config.watch.clone(), config.watch_from)
            );
        }

        let spelled = on.to_string();
        assert!(
            spelled.contains(
                " --record-access-trace-to='/var/run 1.access' --access-trace-from=7586 \
                 --access-trace-max-records=40 --access-trace-max-bytes=4096 \
                 --watch=0x404010:4:rw --watch-from=12"
            ),
            "{spelled}"
        );
        assert!(!Config::default().to_string().contains("access-trace"));
        let reparsed = Config::parse_from([
            "hermit",
            "--record-access-trace-to=/var/run 1.access",
            "--access-trace-from=7586",
            "--access-trace-max-records=40",
            "--access-trace-max-bytes=4096",
            "--watch=0x404010:4:rw",
            "--watch-from=12",
        ]);
        assert_eq!(reparsed.access_trace, on.access_trace);
        assert_eq!(reparsed.access_trace_from, 7586);
        assert_eq!(reparsed.access_trace_max_records, Some(40));
        assert_eq!(reparsed.access_trace_max_bytes, Some(4096));
        assert_eq!((reparsed.watch, reparsed.watch_from), (on.watch, 12));
    }

    /// Anonymous-memory logging is newer than the frozen DBT configuration.
    /// Omit every mode from its object and positional forms while preserving
    /// the exact mode in the current serde and bincode encodings.
    #[test]
    fn anonymous_mapping_logs_never_enter_the_legacy_form() {
        let off = Config {
            detlog_stack: true,
            detlog_regs: true,
            detlog_io_buffers: false,
            detlog_regs_cadence: 7,
            ..Config::default()
        };
        let legacy = to_legacy_backend_json(&off).unwrap();
        assert!(!legacy.contains("detlog_anon"), "{legacy}");
        for mode in [None, Some(DetlogAnon::Mappings), Some(DetlogAnon::Pages)] {
            let config = Config {
                detlog_anon: mode,
                ..off.clone()
            };
            let object = to_legacy_backend_json(&config).unwrap();
            assert_eq!(object, legacy);
            let values: Vec<serde_json::Value> = ordered_entries(&object)
                .into_iter()
                .map(|(_, value)| value)
                .collect();
            let array = serde_json::to_string(&values).unwrap();
            for encoded in [object, array] {
                let decoded = from_legacy_backend_json(&encoded).unwrap();
                assert_eq!(decoded.detlog_anon, None);
                assert_eq!(to_legacy_backend_json(&decoded).unwrap(), legacy);
            }

            let current = serde_json::to_value(&config).unwrap();
            assert_eq!(current["detlog_anon"], serde_json::to_value(mode).unwrap());
            let decoded: Config = serde_json::from_value(current).unwrap();
            assert_eq!(decoded.detlog_anon, mode);
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.detlog_anon, mode);
        }
    }

    /// The in-guest reserved-signal set as an in-guest default-route run
    /// carries it: SIGSYS, SIGSEGV and SIGTRAP.
    fn in_guest_reserved_set() -> u64 {
        [libc::SIGSYS, libc::SIGSEGV, libc::SIGTRAP]
            .into_iter()
            .fold(0, |set, signal| set | (1_u64 << (signal - 1)))
    }

    /// `in_guest_reserved_signals` is empty for DBT, so the legacy form never
    /// carries it, and it reads back as empty from both the object and the
    /// array form.
    #[test]
    fn in_guest_reserved_signals_never_enters_the_legacy_form() {
        let empty = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let reserved = Config {
            in_guest_reserved_signals: in_guest_reserved_set(),
            ..empty.clone()
        };
        let json = to_legacy_backend_json(&reserved).unwrap();
        assert_eq!(json, to_legacy_backend_json(&empty).unwrap());
        assert!(!json.contains("in_guest_reserved_signals"), "{json}");
        assert_eq!(
            from_legacy_backend_json(&json)
                .unwrap()
                .in_guest_reserved_signals,
            0
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert_eq!(
            from_legacy_backend_json(&array)
                .unwrap()
                .in_guest_reserved_signals,
            0
        );
    }

    /// The set reaches every Tool image through Reverie's bincode handshake
    /// unchanged, empty or not, and a JSON configuration without the key reads
    /// as empty (its serde default).
    #[test]
    fn in_guest_reserved_signals_round_trips_and_defaults_to_empty() {
        for set in [0, in_guest_reserved_set()] {
            let config = Config {
                in_guest_reserved_signals: set,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.in_guest_reserved_signals, set);
            let json: Config =
                serde_json::from_str(&serde_json::to_string(&config).unwrap()).unwrap();
            assert_eq!(json.in_guest_reserved_signals, set);
        }
        let mut encoded = serde_json::to_value(Config::default()).unwrap();
        encoded
            .as_object_mut()
            .unwrap()
            .remove("in_guest_reserved_signals")
            .expect("the field is serialized");
        let decoded: Config = serde_json::from_value(encoded).unwrap();
        assert_eq!(decoded.in_guest_reserved_signals, 0);
    }

    /// `reserves_in_guest_signal` answers from the set alone: each reserved
    /// signal and no other, never signal 0 or a number outside 1 to 64, and
    /// nothing at all while the set is empty.
    #[test]
    fn reserves_in_guest_signal_reads_only_the_set() {
        let empty = Config::default();
        let reserved = Config {
            in_guest_reserved_signals: in_guest_reserved_set(),
            ..Config::default()
        };
        for signal in -1..=66 {
            assert!(!empty.reserves_in_guest_signal(signal), "{signal}");
            let expected = [libc::SIGSYS, libc::SIGSEGV, libc::SIGTRAP]
                .into_iter()
                .any(|reserved| i64::from(reserved) == signal);
            assert_eq!(
                reserved.reserves_in_guest_signal(signal),
                expected,
                "{signal}"
            );
        }
        // The top bit is signal 64, the last one Linux accepts.
        let top = Config {
            in_guest_reserved_signals: 1 << 63,
            ..Config::default()
        };
        assert!(top.reserves_in_guest_signal(64));
        assert!(!top.reserves_in_guest_signal(65));
        assert!(!top.reserves_in_guest_signal(0));
    }

    /// `in_guest_refuses_host_timed_producers` is false for DBT, so the legacy
    /// form never carries it, and it reads back as false from both the object
    /// and the array form.
    #[test]
    fn in_guest_refuses_host_timed_producers_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            in_guest_refuses_host_timed_producers: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(
            !json.contains("in_guest_refuses_host_timed_producers"),
            "{json}"
        );
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .in_guest_refuses_host_timed_producers
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .in_guest_refuses_host_timed_producers
        );
    }

    /// The setting reaches every Tool image through Reverie's bincode
    /// handshake unchanged, on or off, and a JSON configuration without the
    /// key reads as off (its serde default).
    #[test]
    fn in_guest_refuses_host_timed_producers_round_trips_and_defaults_to_off() {
        for refuses in [false, true] {
            let config = Config {
                in_guest_refuses_host_timed_producers: refuses,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.in_guest_refuses_host_timed_producers, refuses);
            let json: Config =
                serde_json::from_str(&serde_json::to_string(&config).unwrap()).unwrap();
            assert_eq!(json.in_guest_refuses_host_timed_producers, refuses);
        }
        let mut encoded = serde_json::to_value(Config::default()).unwrap();
        encoded
            .as_object_mut()
            .unwrap()
            .remove("in_guest_refuses_host_timed_producers")
            .expect("the field is serialized");
        let decoded: Config = serde_json::from_value(encoded).unwrap();
        assert!(!decoded.in_guest_refuses_host_timed_producers);
    }

    /// `in_guest_signal_delivery` is false for DBT, so the legacy form never
    /// carries it, and it reads back as false from both the object and the
    /// array form.
    #[test]
    fn in_guest_signal_delivery_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            in_guest_signal_delivery: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("in_guest_signal_delivery"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .in_guest_signal_delivery
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .in_guest_signal_delivery
        );
    }

    /// `--replay-edited-preemptions` stays out of the legacy (DBT) form, and
    /// reads as false from it.
    #[test]
    fn replay_edited_preemptions_never_enters_the_legacy_form() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            replay_preemptions_from: Some("record.preempts".into()),
            ..Config::default()
        };
        let on = Config {
            replay_edited_preemptions: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("replay_edited_preemptions"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .replay_edited_preemptions
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .replay_edited_preemptions
        );
    }

    /// `--replay-edited-preemptions` describes the record
    /// `--replay-preemptions-from` reads, so it needs one. It round-trips
    /// through the rendered options and through Reverie's bincode handshake,
    /// and a JSON configuration without the key reads as off.
    #[test]
    fn replay_edited_preemptions_needs_a_replayed_record_and_round_trips() {
        assert!(Config::try_parse_from(["detcore", "--replay-edited-preemptions"]).is_err());
        let config = Config::try_parse_from([
            "detcore",
            "--replay-preemptions-from=record.preempts",
            "--replay-edited-preemptions",
        ])
        .unwrap();
        assert!(config.replay_edited_preemptions);
        // The rendered options parse back to the same replay settings.
        let rendered = config.to_string();
        let words: Vec<String> = shell_words::split(&rendered)
            .unwrap()
            .into_iter()
            .filter(|word| word.starts_with("--replay-"))
            .collect();
        let reparsed =
            Config::try_parse_from(std::iter::once("detcore".to_owned()).chain(words)).unwrap();
        assert!(reparsed.replay_edited_preemptions, "{rendered}");
        assert_eq!(
            reparsed.replay_preemptions_from, config.replay_preemptions_from,
            "{rendered}"
        );
        let strict =
            Config::try_parse_from(["detcore", "--replay-preemptions-from=record.preempts"])
                .unwrap();
        assert!(!strict.to_string().contains("--replay-edited-preemptions"));

        for config in [&config, &strict] {
            let wire = bincode::serde::encode_to_vec(config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(
                decoded.replay_edited_preemptions,
                config.replay_edited_preemptions
            );
        }
        let mut encoded = serde_json::to_value(&config).unwrap();
        encoded
            .as_object_mut()
            .unwrap()
            .remove("replay_edited_preemptions")
            .expect("the field is serialized");
        let decoded: Config = serde_json::from_value(encoded).unwrap();
        assert!(!decoded.replay_edited_preemptions);
    }

    /// The gate reaches every Tool image through Reverie's bincode handshake
    /// unchanged, on or off, and a JSON configuration without the key reads
    /// as off (its serde default).
    #[test]
    fn in_guest_signal_delivery_round_trips_and_defaults_to_off() {
        for on in [false, true] {
            let config = Config {
                in_guest_signal_delivery: on,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.in_guest_signal_delivery, on);
            let json: Config =
                serde_json::from_str(&serde_json::to_string(&config).unwrap()).unwrap();
            assert_eq!(json.in_guest_signal_delivery, on);
        }
        let mut encoded = serde_json::to_value(Config::default()).unwrap();
        encoded
            .as_object_mut()
            .unwrap()
            .remove("in_guest_signal_delivery")
            .expect("the field is serialized");
        let decoded: Config = serde_json::from_value(encoded).unwrap();
        assert!(!decoded.in_guest_signal_delivery);
    }

    /// `annotated_priority_replay` never enters the legacy form, and reads
    /// back as false from both its object and array forms; it crosses
    /// Reverie's positional bincode and JSON unchanged, and a JSON
    /// configuration without the key reads as off.
    #[test]
    fn annotated_priority_replay_never_enters_the_legacy_form_and_round_trips() {
        let off = Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        };
        let on = Config {
            annotated_priority_replay: true,
            ..off.clone()
        };
        let json = to_legacy_backend_json(&on).unwrap();
        assert_eq!(json, to_legacy_backend_json(&off).unwrap());
        assert!(!json.contains("annotated_priority_replay"), "{json}");
        assert!(
            !from_legacy_backend_json(&json)
                .unwrap()
                .annotated_priority_replay
        );
        let values: Vec<serde_json::Value> = ordered_entries(&json)
            .into_iter()
            .map(|(_, value)| value)
            .collect();
        let array = serde_json::to_string(&values).unwrap();
        assert!(
            !from_legacy_backend_json(&array)
                .unwrap()
                .annotated_priority_replay
        );
        for annotated in [false, true] {
            let config = Config {
                annotated_priority_replay: annotated,
                ..Config::default()
            };
            let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
            let (decoded, read): (Config, usize) =
                bincode::serde::decode_from_slice(&wire, bincode::config::legacy()).unwrap();
            assert_eq!(read, wire.len());
            assert_eq!(decoded.annotated_priority_replay, annotated);
            let json: Config =
                serde_json::from_str(&serde_json::to_string(&config).unwrap()).unwrap();
            assert_eq!(json.annotated_priority_replay, annotated);
        }
        let mut encoded = serde_json::to_value(Config::default()).unwrap();
        encoded
            .as_object_mut()
            .unwrap()
            .remove("annotated_priority_replay")
            .expect("the field is serialized");
        let decoded: Config = serde_json::from_value(encoded).unwrap();
        assert!(!decoded.annotated_priority_replay);
    }

    /// Schedule events are observed exactly where they are traced, and also
    /// in replay of a callback-annotated priority history, whose recording
    /// traced them, whether or not that replay records.
    #[test]
    fn annotated_priority_replay_observes_schedule_events() {
        let plain = Config::default();
        assert!(!plain.should_trace_schedevent() && !plain.observes_schedevents());
        let recording = Config {
            record_preemptions: true,
            ..Config::default()
        };
        assert!(recording.should_trace_schedevent() && recording.observes_schedevents());
        let replay = Config {
            replay_preemptions_from: Some("history.json".into()),
            ..Config::default()
        };
        assert!(!replay.observes_schedevents(), "an unannotated history");
        let annotated = Config {
            annotated_priority_replay: true,
            ..replay
        };
        assert!(!annotated.should_trace_schedevent());
        assert!(annotated.observes_schedevents());
    }

    /// An annotated priority history replays only on an effective
    /// configuration with the backend's own preemption timer events,
    /// sequentialized threads, an active maximum, precise timers and RCB time.
    /// A configuration nobody prepared for a backend has no timer events and
    /// is refused; each other unmet requirement is named in turn, and the
    /// answer never changes the configuration.
    #[test]
    fn annotated_priority_replay_requires_a_qualified_effective_configuration() {
        let unprepared = Config {
            annotated_priority_replay: true,
            replay_preemptions_from: Some("history.json".into()),
            ..Config::default()
        };
        assert_eq!(
            unprepared.annotated_priority_replay_unqualified(),
            Some("cannot deliver Detcore preemption timer events")
        );
        let qualified = Config {
            backend: BackendCapabilities::PTRACE,
            sequentialize_threads: true,
            max_timeslice: NonZeroU64::new(1_000_000),
            imprecise_timers: false,
            no_rcb_time: false,
            ..unprepared.clone()
        };
        assert_eq!(qualified.annotated_priority_replay_unqualified(), None);
        let before = serde_json::to_value(&qualified).unwrap();
        for (config, setting) in [
            (
                Config {
                    sequentialize_threads: false,
                    ..qualified.clone()
                },
                "does not sequentialize threads (--no-sequentialize-threads)",
            ),
            (
                Config {
                    max_timeslice: None,
                    ..qualified.clone()
                },
                "has its preemption timer disabled (--max-timeslice=disabled)",
            ),
            (
                Config {
                    imprecise_timers: true,
                    ..qualified.clone()
                },
                "requests imprecise timers (--imprecise-timers)",
            ),
            (
                Config {
                    no_rcb_time: true,
                    ..qualified.clone()
                },
                "requests logical time without RCBs (--no-rcb-time)",
            ),
        ] {
            assert_eq!(
                config.annotated_priority_replay_unqualified(),
                Some(setting)
            );
        }
        assert_eq!(serde_json::to_value(&qualified).unwrap(), before);
    }

    /// `--signal-at` takes `turn=N:SIGNAL` with SIGNAL a name or a number.
    /// SIGKILL and SIGSTOP are refused, by name or number: the kernel acts on
    /// them at once, outside the scheduler's protocols for them.
    #[test]
    fn signal_at_parses_a_turn_and_a_catchable_signal() {
        let usr1 = SignalAt {
            turn: 500,
            signal: libc::SIGUSR1,
        };
        assert_eq!(parse_signal_at("turn=500:SIGUSR1"), Ok(usr1));
        assert_eq!(parse_signal_at("turn=500:10"), Ok(usr1));
        for refused in [
            "turn=500:SIGKILL",
            "turn=500:9",
            "turn=500:SIGSTOP",
            "turn=500:19",
        ] {
            let error = parse_signal_at(refused).unwrap_err();
            assert!(
                error.contains("cannot be caught or blocked"),
                "{refused}: {error}"
            );
        }
        for malformed in [
            "turn=500",
            "500:SIGUSR1",
            "turn=-1:SIGUSR1",
            "turn=5:SIGNOPE",
        ] {
            assert!(parse_signal_at(malformed).is_err(), "{malformed}");
        }
    }

    /// Serde reads a derived struct from a JSON array by position, so the
    /// legacy form could be given as one. Each array reads as the object
    /// whose values it lists in order, every backend's capabilities included.
    #[test]
    fn a_legacy_positional_array_reads_as_the_object_it_lists() {
        for (name, backend) in [
            ("PTRACE", BackendCapabilities::PTRACE),
            ("E9PATCH", BackendCapabilities::E9PATCH),
            (
                "ptrace without exit transitions",
                ptrace_without_exit_transitions(),
            ),
            ("LITEINST_IN_GUEST", BackendCapabilities::LITEINST_IN_GUEST),
            ("SABRE", BackendCapabilities::SABRE),
            ("DBT", BackendCapabilities::DBT),
            ("KVM", BackendCapabilities::KVM),
        ] {
            for sequentialize_threads in [false, true] {
                let sent = Config {
                    backend,
                    sequentialize_threads,
                    seed: 7,
                    chaos: true,
                    ..Config::default()
                };
                let json = to_legacy_backend_json(&sent).unwrap();
                assert_controlled_key(&json, &backend, sequentialize_threads, name);
                let array = positional(&json);
                assert!(array.starts_with('['), "{array}");
                let received = from_legacy_backend_json(&array).unwrap();
                assert_eq!(received.backend, legacy_expressible(backend), "{name}");
                assert_eq!(
                    received.sequentialize_threads, sequentialize_threads,
                    "{name}"
                );
                assert_eq!(received.seed, 7, "{name}");
                assert!(received.chaos, "{name}");
                assert_eq!(to_legacy_backend_json(&received).unwrap(), json, "{name}");
            }
        }
    }

    /// An array fails the parse where the legacy form's array did: with an
    /// element past the last position, with an element of the wrong type, or
    /// short of a field that has no default. The last position,
    /// `interrupt_at`, has none, so every shorter array fails.
    #[test]
    fn a_malformed_legacy_positional_array_fails_the_parse() {
        let json = to_legacy_backend_json(&Config {
            backend: BackendCapabilities::DBT,
            ..Config::default()
        })
        .unwrap();
        let entries = ordered_entries(&json);
        let values: Vec<serde_json::Value> =
            entries.iter().map(|(_, value)| value.clone()).collect();
        let parse = |values: &[serde_json::Value]| {
            from_legacy_backend_json(&serde_json::to_string(values).unwrap())
        };
        assert!(parse(&values).is_ok());

        let mut longer = values.clone();
        longer.push(serde_json::Value::Bool(false));
        assert!(parse(&longer).is_err());

        assert_eq!(entries.last().unwrap().0, "interrupt_at");
        for len in 0..values.len() {
            assert!(parse(&values[..len]).is_err(), "{len} elements");
        }

        for (position, (key, _)) in entries.iter().enumerate() {
            if LEGACY_BACKEND_KEY_NAMES.contains(&key.as_str()) || key == "seed" {
                let mut mistyped = values.clone();
                mistyped[position] = serde_json::json!("0");
                assert!(parse(&mistyped).is_err(), "{key}");
            }
        }
    }

    /// Selects the child half of
    /// [`a_legacy_configuration_decodes_whatever_the_ambient_hermit_settings`] and
    /// carries the input it decodes.
    const LEGACY_INPUT_IN_CHILD: &str = "DETCORE_MODEL_TEST_LEGACY_INPUT";
    /// The legacy object the child's decoded configuration must re-encode as.
    const LEGACY_EXPECTED_IN_CHILD: &str = "DETCORE_MODEL_TEST_LEGACY_EXPECTED";
    /// Printed by a child that decoded its input, so that a parent can tell
    /// that run from one that selected no test.
    const LEGACY_DECODED_IN_CHILD: &str = "legacy configuration decoded under the ambient settings";

    /// Clap reads HERMIT_EPOCH, HERMIT_PRNG, HERMIT_SCHED_SEED and HERMIT_CPU_PROFILE when it
    /// parses a `Config`, and exits the process when one of them does not
    /// parse. The legacy decoder read none of them, so a valid legacy object
    /// or array decodes to the configuration it lists whatever they hold. Each
    /// decode runs in a child process given those variables unset, set to
    /// text that does not parse, or set to bytes that are not UTF-8. The child
    /// builds no `Config` before decoding, which would read them itself.
    #[test]
    fn a_legacy_configuration_decodes_whatever_the_ambient_hermit_settings() {
        use std::ffi::OsStr;
        use std::os::unix::ffi::OsStrExt;

        if let Some(input) = std::env::var_os(LEGACY_INPUT_IN_CHILD) {
            let expected = std::env::var(LEGACY_EXPECTED_IN_CHILD).unwrap();
            let received = from_legacy_backend_json(input.to_str().unwrap()).unwrap();
            assert_eq!(
                received.backend,
                legacy_expressible(BackendCapabilities::DBT)
            );
            assert_eq!(received.seed, 7);
            assert_eq!(received.sched_seed, Some(11));
            assert_eq!(to_legacy_backend_json(&received).unwrap(), expected);
            println!("{LEGACY_DECODED_IN_CHILD}");
            return;
        }

        const VARIABLES: [&str; 4] = [
            "HERMIT_EPOCH",
            "HERMIT_PRNG",
            "HERMIT_SCHED_SEED",
            "HERMIT_CPU_PROFILE",
        ];
        let text = OsStr::new("not-a-value");
        let bytes = OsStr::from_bytes(b"\xff");
        let mut settings: Vec<Vec<(&str, &OsStr)>> = vec![Vec::new()];
        for variable in VARIABLES {
            settings.push(vec![(variable, text)]);
            settings.push(vec![(variable, bytes)]);
        }
        settings.push(VARIABLES.map(|variable| (variable, text)).to_vec());
        settings.push(VARIABLES.map(|variable| (variable, bytes)).to_vec());

        let object = to_legacy_backend_json(&Config {
            backend: BackendCapabilities::DBT,
            seed: 7,
            sched_seed: Some(11),
            ..Config::default()
        })
        .unwrap();
        let array = positional(&object);
        assert!(array.starts_with('['), "{array}");
        let test = format!(
            "{}::a_legacy_configuration_decodes_whatever_the_ambient_hermit_settings",
            module_path!().split_once("::").unwrap().1
        );
        for input in [&object, &array] {
            for setting in &settings {
                let mut child = std::process::Command::new(std::env::current_exe().unwrap());
                child
                    .args(["--exact", &test, "--nocapture"])
                    .env(LEGACY_INPUT_IN_CHILD, input)
                    .env(LEGACY_EXPECTED_IN_CHILD, &object);
                for variable in VARIABLES {
                    child.env_remove(variable);
                }
                for (variable, value) in setting {
                    child.env(variable, value);
                }
                let output = child.output().unwrap();
                let stdout = String::from_utf8_lossy(&output.stdout);
                let stderr = String::from_utf8_lossy(&output.stderr);
                assert!(
                    output.status.success()
                        && stdout.contains(LEGACY_DECODED_IN_CHILD)
                        && stdout.contains("test result: ok. 1 passed; 0 failed;"),
                    "decoding {} under {setting:?} did not succeed exactly once: {}\n{stdout}\n{stderr}",
                    if input.starts_with('[') {
                        "the array"
                    } else {
                        "the object"
                    },
                    output.status,
                );
            }
        }
    }

    /// Set in the child process of
    /// [`hermit_cpu_profile_supplies_the_profile_only_when_the_flag_is_absent`]:
    /// the arguments it parses, one per line.
    const CPU_PROFILE_ARGS_IN_CHILD: &str = "DETCORE_MODEL_TEST_CPU_PROFILE_ARGS";

    /// HERMIT_CPU_PROFILE gives `--cpu-profile` its value when the flag is
    /// absent: unset is x86-64-v2, an explicit flag wins over the variable,
    /// and a value that names no profile is refused. The wire default stays
    /// x86-64-v2 whatever the variable holds. Each parse runs in a child
    /// process given exactly the variable under test, because Clap reads the
    /// process environment.
    #[test]
    fn hermit_cpu_profile_supplies_the_profile_only_when_the_flag_is_absent() {
        use clap::Parser;

        if let Some(args) = std::env::var_os(CPU_PROFILE_ARGS_IN_CHILD) {
            let args = args.into_string().unwrap();
            let argv = std::iter::once("config").chain(args.lines());
            match Config::try_parse_from(argv) {
                Ok(config) => println!(
                    "cpu-profile={} wire={} default={}",
                    config.cpu_profile.name(),
                    config_wire_default().cpu_profile.name(),
                    // `Config::default` itself, where the environment it
                    // reads parses (it exits on an invalid value).
                    match Config::try_parse_from(["config"]) {
                        Ok(_) => Config::default().cpu_profile.name(),
                        Err(_) => "refused",
                    }
                ),
                Err(error) => println!("refused: {:?}", error.kind()),
            }
            return;
        }

        let test = format!(
            "{}::hermit_cpu_profile_supplies_the_profile_only_when_the_flag_is_absent",
            module_path!().split_once("::").unwrap().1
        );
        let parse = |variable: Option<&str>, args: &[&str]| -> String {
            let mut child = std::process::Command::new(std::env::current_exe().unwrap());
            child
                .args(["--exact", &test, "--nocapture"])
                .env(CPU_PROFILE_ARGS_IN_CHILD, args.join("\n"));
            for name in [
                "HERMIT_EPOCH",
                "HERMIT_PRNG",
                "HERMIT_SCHED_SEED",
                "HERMIT_CPU_PROFILE",
            ] {
                child.env_remove(name);
            }
            if let Some(value) = variable {
                child.env("HERMIT_CPU_PROFILE", value);
            }
            let output = child.output().unwrap();
            let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
            assert!(
                output.status.success() && stdout.contains("test result: ok. 1 passed; 0 failed;"),
                "the child for {variable:?} {args:?} did not run exactly once: {}\n{stdout}",
                output.status,
            );
            stdout
                .lines()
                .find(|line| line.starts_with("cpu-profile=") || line.starts_with("refused: "))
                .unwrap_or_else(|| panic!("the child printed no verdict:\n{stdout}"))
                .to_owned()
        };

        // Unset on the command line is auto; the wire default stays v2.
        assert_eq!(
            parse(None, &[]),
            "cpu-profile=auto wire=x86-64-v2 default=auto"
        );
        assert_eq!(
            parse(Some("auto"), &[]),
            "cpu-profile=auto wire=x86-64-v2 default=auto"
        );
        assert_eq!(
            parse(Some("auto"), &["--cpu-profile=x86-64-v2"]),
            "cpu-profile=x86-64-v2 wire=x86-64-v2 default=auto"
        );
        assert_eq!(
            parse(Some("x86-64-v3"), &[]),
            "cpu-profile=x86-64-v3 wire=x86-64-v2 default=x86-64-v3"
        );
        assert_eq!(
            parse(Some("host-no-rdrand"), &[]),
            "cpu-profile=host-no-rdrand wire=x86-64-v2 default=host-no-rdrand"
        );
        assert_eq!(
            parse(None, &["--cpu-profile=x86-64-v3"]),
            "cpu-profile=x86-64-v3 wire=x86-64-v2 default=auto"
        );
        assert_eq!(
            parse(Some("x86-64-v3"), &["--cpu-profile=x86-64-v2"]),
            "cpu-profile=x86-64-v2 wire=x86-64-v2 default=x86-64-v3"
        );
        assert_eq!(parse(Some("x86-64-v4"), &[]), "refused: InvalidValue");
        assert_eq!(
            parse(Some("x86-64-v4"), &["--cpu-profile=x86-64-v2"]),
            "cpu-profile=x86-64-v2 wire=x86-64-v2 default=refused"
        );
    }

    /// `backend` and `shared_dequeue_timers` were not keys of the legacy form,
    /// which ignored every key it did not name. They are still ignored,
    /// whatever they hold, however often they appear and wherever they stand,
    /// so the legacy keys alone decide the capabilities. A key the legacy form
    /// named still fails the parse when it appears twice.
    #[test]
    fn keys_the_legacy_form_did_not_name_are_ignored() {
        let sent = Config {
            backend: legacy_expressible(BackendCapabilities::DBT),
            seed: 7,
            // A launch sends a resolved profile: the legacy form has no
            // profile field and reads back as v2.
            cpu_profile: crate::cpu_profile::CpuProfile::X86_64V2,
            ..Config::default()
        };
        let json = to_legacy_backend_json(&sent).unwrap();
        let kvm = serde_json::to_string(&BackendCapabilities::KVM).unwrap();
        for extra in [
            r#""backend":null"#.to_owned(),
            r#""shared_dequeue_timers":null"#.to_owned(),
            r#""backend":null,"shared_dequeue_timers":null"#.to_owned(),
            format!(r#""backend":{kvm},"shared_dequeue_timers":true"#),
            r#""backend":7,"backend":{"tracks_process_children":false}"#.to_owned(),
            r#""shared_dequeue_timers":"yes","shared_dequeue_timers":true"#.to_owned(),
            r#""no_such_field":[1,{"x":null}]"#.to_owned(),
        ] {
            for edited in [
                json.replacen('{', &format!("{{{extra},"), 1),
                format!("{},{extra}}}", &json[..json.len() - 1]),
            ] {
                let received = from_legacy_backend_json(&edited).unwrap();
                assert_eq!(received.backend, sent.backend, "{edited}");
                assert_eq!(
                    serde_json::to_string(&received).unwrap(),
                    serde_json::to_string(&sent).unwrap(),
                    "{edited}"
                );
                assert_eq!(to_legacy_backend_json(&received).unwrap(), json, "{edited}");
            }
        }

        let repeated = format!("{},\"seed\":7}}", &json[..json.len() - 1]);
        assert!(from_legacy_backend_json(&repeated).is_err());
    }

    #[test]
    fn network_perturbation_seed_never_falls_back_to_scheduler_seeds() {
        let mut config = Config {
            seed: 41,
            sched_seed: Some(42),
            ..Config::default()
        };
        assert_eq!(config.network_trace.network_perturb_seed, None);

        config.network_trace = NetworkTraceConfig {
            mode: crate::network_trace::NetworkTraceMode::Replay,
            path: Some("network.trace".into()),
            network_perturb_seed: Some(43),
        };
        assert_eq!(config.seed, 41);
        assert_eq!(config.sched_seed(), 42);
        assert_eq!(config.network_trace.network_perturb_seed, Some(43));
    }

    #[test]
    fn missing_mountinfo_provenance_deserializes_as_empty() {
        let mut value = serde_json::to_value(Config::default()).unwrap();
        value
            .as_object_mut()
            .unwrap()
            .remove("mountinfo_root_rewrites");
        value
            .as_object_mut()
            .unwrap()
            .remove("mountinfo_device_rewrites");
        value.as_object_mut().unwrap().remove("mountinfo_mount_ids");
        value
            .as_object_mut()
            .unwrap()
            .remove("mountinfo_mount_ids_captured");
        value
            .as_object_mut()
            .unwrap()
            .remove("mount_id_assignment_order");
        value
            .as_object_mut()
            .unwrap()
            .remove("container_mount_table");
        value
            .as_object_mut()
            .unwrap()
            .remove("mountinfo_mount_namespace");
        value
            .as_object_mut()
            .unwrap()
            .remove("directory_placeholder");
        value.as_object_mut().unwrap().remove("container_procfs");
        let restored: Config = serde_json::from_value(value).unwrap();
        assert!(restored.mountinfo_root_rewrites.is_empty());
        assert!(restored.mountinfo_device_rewrites.is_empty());
        assert!(restored.mountinfo_mount_ids.is_empty());
        assert!(!restored.mountinfo_mount_ids_captured);
        assert!(restored.mount_id_assignment_order.is_empty());
        assert!(restored.container_mount_table.is_none());
        assert!(restored.mountinfo_mount_namespace.is_none());
        assert!(restored.directory_placeholder.is_none());
        assert!(!restored.container_procfs);
    }

    /// Lone slice ends are processed without stops only where a slice end
    /// reads nothing from the guest but its branch count: each listed option
    /// turns it off.
    #[test]
    fn lone_preemptions_are_replayed_only_where_a_slice_end_reads_nothing_else() {
        let base = Config {
            sequentialize_threads: true,
            backend: reverie::BackendCapabilities::PTRACE,
            ..Config::parse_from(["detcore"])
        };
        assert!(base.replays_lone_preemptions());
        let off: Vec<(&str, Config)> = vec![
            (
                "--eager-lone-preemptions",
                Config {
                    eager_lone_preemptions: true,
                    ..base.clone()
                },
            ),
            (
                "no sequentialization",
                Config {
                    sequentialize_threads: false,
                    ..base.clone()
                },
            ),
            (
                "--max-timeslice=disabled",
                Config {
                    max_timeslice: None,
                    ..base.clone()
                },
            ),
            (
                "--no-rcb-time",
                Config {
                    no_rcb_time: true,
                    ..base.clone()
                },
            ),
            (
                "--imprecise-timers",
                Config {
                    imprecise_timers: true,
                    ..base.clone()
                },
            ),
            (
                "--chaos-per-thread-slowdown",
                Config {
                    chaos_per_thread_slowdown: true,
                    ..base.clone()
                },
            ),
            (
                "--strict-schedule-boundaries",
                Config {
                    strict_schedule_boundaries: true,
                    ..base.clone()
                },
            ),
            (
                "--target-timeslice",
                Config {
                    target_timeslice: NonZeroU64::new(1_000),
                    ..base.clone()
                },
            ),
            (
                "--preemption-stacktrace",
                Config {
                    preemption_stacktrace: true,
                    ..base.clone()
                },
            ),
            (
                "--stacktrace-event",
                Config {
                    stacktrace_event: vec![(1, None)],
                    ..base.clone()
                },
            ),
            (
                "--stop-after-turn",
                Config {
                    stop_after_turn: Some(1),
                    ..base.clone()
                },
            ),
            (
                "--stop-after-iter",
                Config {
                    stop_after_iter: Some(1),
                    ..base.clone()
                },
            ),
            (
                "--interrupt-at",
                Config {
                    interrupt_at: vec![(DetTid::from_raw(3), 1)],
                    ..base.clone()
                },
            ),
            (
                "--signal-at",
                Config {
                    signal_at: Some(parse_signal_at("turn=1:SIGUSR1").unwrap()),
                    ..base.clone()
                },
            ),
            (
                "--happens-before",
                Config {
                    happens_before: Some(crate::happens_before::HappensBeforeProgram {
                        version: 1,
                        anchors: Default::default(),
                        edges: Vec::new(),
                        max_hold_ns: None,
                    }),
                    ..base.clone()
                },
            ),
            (
                "--chaos --chaos-instruction-offset=8",
                Config {
                    chaos: true,
                    chaos_instruction_offset: 8,
                    ..base.clone()
                },
            ),
            (
                "--gdbserver",
                Config {
                    gdbserver: true,
                    ..base.clone()
                },
            ),
        ];
        for (option, config) in off {
            assert!(!config.replays_lone_preemptions(), "{option}");
        }
        // Recording and both replays keep them: a recording writes a lone
        // end's event without a RIP, and a replay takes without a stop only
        // the ends it may (`Detcore::replay_allows_stopless_end`).
        let on: Vec<(&str, Config)> = vec![
            (
                "--record-preemptions",
                Config {
                    record_preemptions: true,
                    ..base.clone()
                },
            ),
            (
                "--replay-preemptions-from",
                Config {
                    replay_preemptions_from: Some("p".into()),
                    ..base.clone()
                },
            ),
            (
                "--replay-schedule-from",
                Config {
                    replay_schedule_from: Some("s".into()),
                    record_preemptions: true,
                    ..base.clone()
                },
            ),
        ];
        for (option, config) in on {
            assert!(config.replays_lone_preemptions(), "{option}");
        }
        // Chaos alone draws no offsets: its slice ends need no stop.
        assert!(
            Config {
                chaos: true,
                ..base.clone()
            }
            .replays_lone_preemptions()
        );
    }

    #[test]
    fn runs_post_fork_parses_all_modes_and_defaults_to_child() {
        assert_eq!(Config::default().runs_post_fork, RunsPostFork::Child);
        assert_eq!(
            Config::parse_from(["detcore", "--runs-post-fork=parent"]).runs_post_fork,
            RunsPostFork::Parent
        );
        assert_eq!(
            Config::parse_from(["detcore", "--runs-post-fork=random"]).runs_post_fork,
            RunsPostFork::Random
        );
        assert!(Config::try_parse_from(["detcore", "--runs-post-fork=invalid"]).is_err());
    }

    #[test]
    fn proc_level_parses_both_levels_defaults_to_minimal_and_round_trips() {
        assert_eq!(Config::default().proc_level, ProcLevel::Minimal);
        assert!(!Config::default().to_string().contains("--proc"));
        let standard = Config::parse_from(["detcore", "--proc=standard"]);
        assert_eq!(standard.proc_level, ProcLevel::Standard);
        assert!(standard.to_string().contains(" --proc=standard"));
        assert_eq!(
            Config::parse_from(["detcore", "--proc=minimal"]).proc_level,
            ProcLevel::Minimal
        );
        assert!(Config::try_parse_from(["detcore", "--proc=host"]).is_err());
        assert!(ProcLevel::Minimal < ProcLevel::Standard);
    }

    #[test]
    fn untrap_passthrough_syscalls_is_opt_in_and_round_trips() {
        assert!(!Config::default().untrap_passthrough_syscalls);
        let config = Config::parse_from(["detcore", "--untrap-passthrough-syscalls"]);
        assert!(config.untrap_passthrough_syscalls);
        assert!(
            config
                .to_string()
                .contains(" --untrap-passthrough-syscalls")
        );
        assert!(!Config::default().to_string().contains("untrap"));
    }

    #[test]
    fn panic_on_rcb_overshoot_is_opt_in_and_round_trips() {
        assert!(!Config::default().panic_on_rcb_overshoot);

        let config = Config::parse_from(["detcore", "--panic-on-rbc-overshoot"]);
        assert!(config.panic_on_rcb_overshoot);
        assert!(config.to_string().contains(" --panic-on-rbc-overshoot"));

        let alias = Config::parse_from(["detcore", "--panic-on-rcb-overshoot"]);
        assert!(alias.panic_on_rcb_overshoot);
    }

    #[test]
    fn config_display_preserves_nondefault_post_fork_modes() {
        let mut config = Config {
            runs_post_fork: RunsPostFork::Parent,
            ..Config::default()
        };
        assert!(config.to_string().contains(" --runs-post-fork=parent"));

        config.runs_post_fork = RunsPostFork::Random;
        assert!(config.to_string().contains(" --runs-post-fork=random"));
    }

    /// `--expired-wait-charge` is unset by default, parses both values, is
    /// printed by `Display` (reproducer commands are built from it) only when
    /// set, round-trips through serde, and never enters the legacy form.
    #[test]
    fn the_expired_wait_charge_is_printed_when_set_and_never_enters_the_legacy_form() {
        assert_eq!(Config::default().expired_wait_charge, None);
        assert!(
            !Config::default()
                .to_string()
                .contains("--expired-wait-charge")
        );
        for (value, charge) in [
            ("each", ExpiredWaitCharge::Each),
            ("once", ExpiredWaitCharge::Once),
        ] {
            let set =
                Config::try_parse_from(["detcore", &format!("--expired-wait-charge={value}")])
                    .unwrap();
            assert_eq!(set.expired_wait_charge, Some(charge));
            assert!(
                set.to_string()
                    .contains(&format!(" --expired-wait-charge={value}")),
                "{set}"
            );
            let reparsed: Config =
                serde_json::from_str(&serde_json::to_string(&set).unwrap()).unwrap();
            assert_eq!(reparsed.expired_wait_charge, Some(charge));
            let legacy = to_legacy_backend_json(&set).unwrap();
            assert!(!legacy.contains("expired_wait_charge"), "{legacy}");
            assert_eq!(
                from_legacy_backend_json(&legacy)
                    .unwrap()
                    .expired_wait_charge,
                None
            );
        }
    }

    /// `--sleep-registration` is unset by default, parses both values, is
    /// printed by `Display` (reproducer commands are built from it) only when
    /// set, round-trips through serde, and never enters the legacy form.
    #[test]
    fn the_sleep_registration_is_printed_when_set_and_never_enters_the_legacy_form() {
        assert_eq!(Config::default().sleep_registration, None);
        assert!(
            !Config::default()
                .to_string()
                .contains("--sleep-registration")
        );
        for (value, registration) in [
            ("first-pass", SleepRegistration::FirstPass),
            ("when-chosen", SleepRegistration::WhenChosen),
        ] {
            let set = Config::try_parse_from(["detcore", &format!("--sleep-registration={value}")])
                .unwrap();
            assert_eq!(set.sleep_registration, Some(registration));
            assert!(
                set.to_string()
                    .contains(&format!(" --sleep-registration={value}")),
                "{set}"
            );
            let reparsed: Config =
                serde_json::from_str(&serde_json::to_string(&set).unwrap()).unwrap();
            assert_eq!(reparsed.sleep_registration, Some(registration));
            let legacy = to_legacy_backend_json(&set).unwrap();
            assert!(!legacy.contains("sleep_registration"), "{legacy}");
            assert_eq!(
                from_legacy_backend_json(&legacy)
                    .unwrap()
                    .sleep_registration,
                None
            );
        }
    }

    /// By default a queued sleep registers at the first pass only where the scheduler reads a
    /// sleeper's signal state: a backend whose kernel signal targets it predicts (ptrace), or any
    /// backend under the in-guest delivery gate. DBT, SaBRe, LiteInst and KVM register when
    /// chosen by default. An explicit value decides here; a `first-pass` the backend cannot honour
    /// is refused at launch. Race search and `hermit record`/`replay` keep their rule.
    #[test]
    fn first_pass_sleep_registration_is_the_default_only_where_signal_state_is_read() {
        let config = |backend: BackendCapabilities,
                      gate: bool,
                      sleep_registration: Option<SleepRegistration>| Config {
            backend,
            in_guest_signal_delivery: gate,
            sleep_registration,
            sequentialize_threads: true,
            ..Config::default()
        };
        for (name, backend) in [
            ("PTRACE", BackendCapabilities::PTRACE),
            ("E9PATCH", BackendCapabilities::E9PATCH),
        ] {
            let default = config(backend, false, None);
            assert!(default.reads_sleeper_signal_state(), "{name}");
            assert!(default.queued_sleeps_registered_at_first_pass(), "{name}");
        }
        for (name, backend) in [
            ("DBT", BackendCapabilities::DBT),
            ("SABRE", BackendCapabilities::SABRE),
            ("LITEINST_IN_GUEST", BackendCapabilities::LITEINST_IN_GUEST),
            ("KVM", BackendCapabilities::KVM),
        ] {
            let default = config(backend, false, None);
            assert!(!default.reads_sleeper_signal_state(), "{name}");
            assert!(!default.queued_sleeps_registered_at_first_pass(), "{name}");
            let gated = config(backend, true, None);
            assert!(gated.reads_sleeper_signal_state(), "{name} under the gate");
            assert!(
                gated.queued_sleeps_registered_at_first_pass(),
                "{name} under the gate"
            );
            assert!(
                config(backend, false, Some(SleepRegistration::FirstPass))
                    .queued_sleeps_registered_at_first_pass(),
                "{name}, asked for"
            );
        }
        let unserialized = Config {
            sequentialize_threads: false,
            ..config(BackendCapabilities::PTRACE, false, None)
        };
        assert!(!unserialized.reads_sleeper_signal_state());
        assert!(!unserialized.queued_sleeps_registered_at_first_pass());
        let chaos = Config {
            chaos: true,
            ..config(BackendCapabilities::PTRACE, false, None)
        };
        assert!(!chaos.queued_sleeps_registered_at_first_pass());
        let recorded = Config {
            recordreplay_modes: true,
            ..config(
                BackendCapabilities::PTRACE,
                false,
                Some(SleepRegistration::FirstPass),
            )
        };
        assert!(!recorded.queued_sleeps_registered_at_first_pass());
    }

    #[test]
    fn the_work_span_report_is_off_by_default_and_never_enters_the_legacy_form() {
        assert_eq!(Config::default().work_span_report, None);
        let on = Config::try_parse_from(["detcore", "--work-span-report=/tmp/ws.json"]).unwrap();
        assert_eq!(
            on.work_span_report.as_deref(),
            Some(std::path::Path::new("/tmp/ws.json"))
        );
        assert!(on.to_string().contains(" --work-span-report=/tmp/ws.json"));
        let reparsed: Config = serde_json::from_str(&serde_json::to_string(&on).unwrap()).unwrap();
        assert_eq!(reparsed.work_span_report, on.work_span_report);
        let mut old = serde_json::to_value(Config::default()).unwrap();
        old.as_object_mut().unwrap().remove("work_span_report");
        let old: Config = serde_json::from_value(old).unwrap();
        assert_eq!(old.work_span_report, None);
        let legacy = to_legacy_backend_json(&on).unwrap();
        assert!(!legacy.contains("work_span_report"), "{legacy}");
        assert_eq!(
            from_legacy_backend_json(&legacy).unwrap().work_span_report,
            None
        );
    }

    #[test]
    fn a_fatal_delivery_check_is_off_by_default_and_never_enters_the_legacy_form() {
        assert!(!Config::default().signal_delivery_check_fatal);
        let fatal = Config {
            signal_delivery_check_fatal: true,
            ..Config::default()
        };
        let json = serde_json::to_string(&fatal).unwrap();
        let reparsed: Config = serde_json::from_str(&json).unwrap();
        assert!(reparsed.signal_delivery_check_fatal);
        let mut old = serde_json::to_value(Config::default()).unwrap();
        old.as_object_mut()
            .unwrap()
            .remove("signal_delivery_check_fatal");
        let old: Config = serde_json::from_value(old).unwrap();
        assert!(!old.signal_delivery_check_fatal);
        let legacy = to_legacy_backend_json(&fatal).unwrap();
        assert!(!legacy.contains("signal_delivery_check_fatal"), "{legacy}");
        assert!(
            !from_legacy_backend_json(&legacy)
                .unwrap()
                .signal_delivery_check_fatal
        );
    }

    #[test]
    fn the_signal_state_copy_is_on_by_default_and_its_off_switch_round_trips() {
        let dflt = Config::default();
        assert!(dflt.signal_state_copy);
        assert!(!dflt.to_string().contains("signal-state-copy"));
        // A configuration serialized before the field existed reads as on.
        let mut json = serde_json::to_value(&dflt).unwrap();
        json.as_object_mut().unwrap().remove("signal_state_copy");
        let old: Config = serde_json::from_value(json).unwrap();
        assert!(old.signal_state_copy);
        // Off renders as the flag, so that a command rebuilt from a
        // configuration (analyze's reproduction commands) keeps it off.
        let off = Config::parse_from(["detcore", "--no-signal-state-copy"]);
        assert!(!off.signal_state_copy);
        let rendered = off.to_string();
        assert!(rendered.contains(" --no-signal-state-copy"), "{rendered}");
        let reparsed = Config::parse_from(
            std::iter::once("detcore".to_string())
                .chain(rendered.split_whitespace().map(String::from)),
        );
        assert!(!reparsed.signal_state_copy);
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1149)
    #[test]
    fn chaos_per_thread_slowdown_is_opt_in_and_round_trips() {
        // Off by default; the factor default is present but inert.
        let dflt = Config::default();
        assert!(!dflt.chaos_per_thread_slowdown);
        assert_eq!(dflt.chaos_slowdown_max_factor, 10.0);
        // Default (disabled) config does not emit the flags.
        assert!(!dflt.to_string().contains("--chaos-per-thread-slowdown"));

        let config = Config::parse_from([
            "detcore",
            "--chaos",
            "--chaos-per-thread-slowdown",
            "--chaos-slowdown-max-factor=4.5",
        ]);
        assert!(config.chaos_per_thread_slowdown);
        assert_eq!(config.chaos_slowdown_max_factor, 4.5);

        // The Display round-trips both flags into the recorded schedule artifact.
        let rendered = config.to_string();
        assert!(rendered.contains(" --chaos-per-thread-slowdown"));
        assert!(rendered.contains(" --chaos-slowdown-max-factor=4.5"));
        let reparsed = Config::parse_from(
            std::iter::once("detcore".to_string())
                .chain(rendered.split_whitespace().map(String::from)),
        );
        assert!(reparsed.chaos_per_thread_slowdown);
        assert_eq!(reparsed.chaos_slowdown_max_factor, 4.5);
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1151)
    #[test]
    fn chaos_epoch_length_is_opt_in_and_round_trips() {
        // Off by default (single stable factor == plain per-thread-slowdown).
        let dflt = Config::default();
        assert_eq!(dflt.chaos_epoch_length_ns, 0);
        assert!(!dflt.to_string().contains("--chaos-epoch-length-ns"));

        // Epochs are only emitted alongside per-thread-slowdown.
        let config = Config::parse_from([
            "detcore",
            "--chaos",
            "--chaos-per-thread-slowdown",
            "--chaos-epoch-length-ns=100000",
        ]);
        assert_eq!(config.chaos_epoch_length_ns, 100000);

        let rendered = config.to_string();
        assert!(rendered.contains(" --chaos-epoch-length-ns=100000"));
        let reparsed = Config::parse_from(
            std::iter::once("detcore".to_string())
                .chain(rendered.split_whitespace().map(String::from)),
        );
        assert_eq!(reparsed.chaos_epoch_length_ns, 100000);

        // Without per-thread-slowdown the epoch flag is inert and not rendered.
        let no_slowdown = Config::parse_from(["detcore", "--chaos", "--chaos-epoch-length-ns=100"]);
        assert_eq!(no_slowdown.chaos_epoch_length_ns, 100);
        assert!(!no_slowdown.to_string().contains("--chaos-epoch-length-ns"));
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1149)
    #[test]
    #[should_panic(expected = "chaos_slowdown_max_factor must be finite and in")]
    fn validate_rejects_chaos_slowdown_max_factor_below_one() {
        let mut config = Config {
            chaos_slowdown_max_factor: 0.5,
            ..Default::default()
        };
        config.validate();
    }

    #[test]
    #[should_panic(expected = "max_timeslice must be at least one RCB")]
    fn validate_rejects_max_timeslice_below_one_rcb() {
        let mut config = Config {
            max_timeslice: NonZeroU64::new(NANOS_PER_RCB as u64 - 1),
            ..Default::default()
        };

        config.validate();
    }

    #[test]
    fn validate_accepts_one_rcb_max_timeslice() {
        let mut config = Config {
            max_timeslice: NonZeroU64::new(NANOS_PER_RCB as u64),
            ..Default::default()
        };

        config.validate();
    }

    #[test]
    #[should_panic(expected = "clock_multiplier must be finite and positive")]
    fn validate_rejects_invalid_clock_multiplier() {
        let mut config = Config {
            clock_multiplier: Some(0.0),
            ..Default::default()
        };
        config.validate();
    }

    #[test]
    #[should_panic(expected = "max_timeslice must be at least one RCB")]
    fn validate_scales_one_rcb_minimum_with_clock_multiplier() {
        let mut config = Config {
            max_timeslice: NonZeroU64::new(10),
            clock_multiplier: Some(2.0),
            ..Default::default()
        };
        config.validate();
    }

    #[test]
    fn config_fingerprint_sees_the_width_of_every_backend_capability() {
        // `BackendCapabilities` lives in Reverie, outside the fingerprinted
        // sources, so only its encoding can reveal a change to it. Every
        // field encodes at a fixed width whatever its value (a bool, or the
        // `u8` width of `hardware_watchpoints`, in one byte; the `u64`
        // signal set of `publishable_parent_death_signals` in eight), so the
        // encoded default carries every field, and adding, removing or
        // widening one changes the bytes. A field that is optional would have
        // to be present in the default, or a change to its width would encode
        // as the same bare `None`; this fails first. The one optional field,
        // `rcb_clock_contract`, is present: `config_wire_default` sets
        // ptrace's contract.
        let config = config_wire_default();
        let fields = serde_json::to_value(config.backend).unwrap();
        let fields = fields.as_object().unwrap();
        // The true widths from the capability table: every field is a bool
        // except `hardware_watchpoints`, a `u8` count,
        // `publishable_parent_death_signals`, a `u64` set of signal bits, and
        // `rcb_clock_contract`, an optional unit-variant enum, present here.
        for (name, value) in fields {
            match name.as_str() {
                "hardware_watchpoints" => {
                    let width = value.as_u64().expect("a u8 count encodes as a number");
                    assert!(width <= u64::from(u8::MAX), "{name}={width} exceeds a u8");
                }
                "publishable_parent_death_signals" => {
                    value
                        .as_u64()
                        .expect("a u64 signal set encodes as a number");
                }
                "rcb_clock_contract" => {
                    value
                        .as_str()
                        .expect("the default's contract is present, a unit variant's name");
                }
                _ => assert!(value.is_boolean(), "{name}={value} is not a bool"),
            }
        }
        let encode = |config: &Config| {
            bincode::serde::encode_to_vec(config, bincode::config::legacy()).unwrap()
        };
        // One byte per field (the option's tag for `rcb_clock_contract`),
        // seven more for the `u64` signal set, and four for the contract's
        // `u32` variant index.
        assert_eq!(
            bincode::serde::encode_to_vec(config.backend, bincode::config::legacy())
                .unwrap()
                .len(),
            fields.len() + 7 + 4
        );
        // The backend's values are part of the encoded default.
        let flipped = config.clone().with_backend(|backend| {
            backend.tracks_process_children = !backend.tracks_process_children
        });
        assert_eq!(encode(&flipped).len(), encode(&config).len());
        assert_ne!(encode(&flipped), encode(&config));
        // The one numeric capability: another width within a `u8` changes
        // the bytes at the same length; a wider type would change the length
        // asserted above.
        let widened = config.clone().with_backend(|backend| {
            backend.hardware_watchpoints = backend.hardware_watchpoints.wrapping_add(1)
        });
        assert_eq!(encode(&widened).len(), encode(&config).len());
        assert_ne!(encode(&widened), encode(&config));
        // The signal set: another set changes the bytes at the same length.
        let resigned = config
            .clone()
            .with_backend(|backend| backend.publishable_parent_death_signals ^= 1 << 63);
        assert_eq!(encode(&resigned).len(), encode(&config).len());
        assert_ne!(encode(&resigned), encode(&config));
        // The contract: its absence drops the variant index.
        let uncontracted = config
            .clone()
            .with_backend(|backend| backend.rcb_clock_contract = None);
        assert_eq!(encode(&uncontracted).len() + 4, encode(&config).len());
    }

    #[test]
    fn config_fingerprint_includes_clock_rpc_definitions() {
        let config = config_wire_default();
        let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
        let named_shape = serde_json::to_string(&config).unwrap();
        let current = config_wire_fingerprint();
        let clock_source = include_bytes!("time.rs").as_slice();

        // The previous guard covered these same Config bytes and definitions,
        // but omitted DetTime. Adding a positional clock field could therefore
        // pass the handshake guard and corrupt the following request on decode.
        let without_clock: Vec<_> = CONFIG_DEFINITION_SOURCES
            .iter()
            .copied()
            .filter(|source| *source != clock_source)
            .collect();
        assert_ne!(
            fingerprint_of_config_material(&wire, &named_shape, &without_clock),
            current,
            "the published fingerprint must reject source inputs that omit the RPC clock"
        );

        // Hold all Config material fixed and remove only the added serialized
        // clock field from its definition. A future clock-only change must also
        // invalidate the existing artifact guard, independently of config.rs.
        let changed_clock =
            include_str!("time.rs").replacen("    inherited_nanos: LogicalDuration,", "", 1);
        assert_ne!(changed_clock.as_bytes(), clock_source);
        let changed_sources: Vec<_> = CONFIG_DEFINITION_SOURCES
            .iter()
            .map(|source| {
                if *source == clock_source {
                    changed_clock.as_bytes()
                } else {
                    *source
                }
            })
            .collect();
        assert_ne!(
            fingerprint_of_config_material(&wire, &named_shape, &changed_sources),
            current,
            "a clock-only serialized field change must invalidate the fingerprint"
        );
    }

    /// The inode RPCs carry `RawInode`, whose layout neither encoding of
    /// `Config` shows. A plugin built before `RawInode` gained its device
    /// sends one `u64` where the coordinator decodes two, so a change to its
    /// definition must invalidate the fingerprint.
    #[test]
    fn config_fingerprint_includes_the_inode_rpc_identity() {
        let config = config_wire_default();
        let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
        let named_shape = serde_json::to_string(&config).unwrap();
        let current = config_wire_fingerprint();
        let inode_source = include_bytes!("fd.rs").as_slice();
        assert!(CONFIG_DEFINITION_SOURCES.contains(&inode_source));
        let without_device = include_str!("fd.rs").replacen("    pub dev: u64,\n", "", 1);
        assert_ne!(without_device.as_bytes(), inode_source);
        let changed_sources: Vec<_> = CONFIG_DEFINITION_SOURCES
            .iter()
            .map(|source| {
                if *source == inode_source {
                    without_device.as_bytes()
                } else {
                    *source
                }
            })
            .collect();
        assert_ne!(
            fingerprint_of_config_material(&wire, &named_shape, &changed_sources),
            current,
            "an inode-identity layout change must invalidate the fingerprint"
        );
    }

    /// `Config::watch` is empty in the encoded default, so neither encoding
    /// shows the layout of its element, `WatchArg`: a plugin built before a
    /// change to it would decode a run's watches against another layout. A
    /// change to its definition must invalidate the fingerprint.
    #[test]
    fn config_fingerprint_includes_the_watch_element_layout() {
        let config = config_wire_default();
        assert!(config.watch.is_empty(), "the default hides the element");
        let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
        let named_shape = serde_json::to_string(&config).unwrap();
        let current = config_wire_fingerprint();
        let watch_source = include_bytes!("watch.rs").as_slice();
        assert!(CONFIG_DEFINITION_SOURCES.contains(&watch_source));
        let without_len = include_str!("watch.rs").replacen("    pub len: u8,\n", "", 1);
        assert_ne!(without_len.as_bytes(), watch_source);
        let changed_sources: Vec<_> = CONFIG_DEFINITION_SOURCES
            .iter()
            .map(|source| {
                if *source == watch_source {
                    without_len.as_bytes()
                } else {
                    *source
                }
            })
            .collect();
        assert_ne!(
            fingerprint_of_config_material(&wire, &named_shape, &changed_sources),
            current,
            "a watch element layout change must invalidate the fingerprint"
        );
    }

    #[test]
    fn a_config_fingerprint_mismatch_names_both_fingerprints() {
        let ours = config_wire_fingerprint();
        assert_eq!(
            config_fingerprint_mismatch(std::ffi::OsStr::new(&ours)),
            None
        );
        let other = config_fingerprint_mismatch(std::ffi::OsStr::new("0123456789abcdef"))
            .expect("another fingerprint is a mismatch");
        assert!(other.contains(&ours), "{other}");
        assert!(other.contains("0123456789abcdef"), "{other}");
        use std::os::unix::ffi::OsStrExt;
        assert!(config_fingerprint_mismatch(std::ffi::OsStr::from_bytes(b"\xff")).is_some());
    }

    #[test]
    fn config_fingerprint_is_stable_and_shape_sensitive() {
        // STABLE: a build must agree with itself, or the guard would reject a
        // MATCHED pair -- which would be worse than having no guard at all.
        assert_eq!(config_wire_fingerprint(), config_wire_fingerprint());
        assert_eq!(config_wire_fingerprint().len(), 16);

        let config = config_wire_default();
        let base = serde_json::to_string(&config).unwrap();
        let wire = bincode::serde::encode_to_vec(&config, bincode::config::legacy()).unwrap();
        assert_eq!(
            fingerprint_of_config_material(&wire, &base, CONFIG_DEFINITION_SOURCES),
            config_wire_fingerprint()
        );

        // SHAPE-SENSITIVE, checked on the same mechanism the real function uses.
        // One added field is exactly the change that caused the outage.
        let with_extra_field = format!("{},\"a_new_flag\":false}}", &base[..base.len() - 1]);
        assert_ne!(
            fingerprint_of_config_material(&wire, &with_extra_field, CONFIG_DEFINITION_SOURCES),
            config_wire_fingerprint()
        );
        // A removed field.
        let removed = base.replacen("\"virtualize_time\":true,", "", 1);
        assert_ne!(
            fingerprint_of_config_material(&wire, &removed, CONFIG_DEFINITION_SOURCES),
            config_wire_fingerprint()
        );
        // A pure rename, which bincode would tolerate but which we still refuse.
        let renamed = base.replacen("\"virtualize_time\"", "\"virtualise_time\"", 1);
        assert_ne!(
            fingerprint_of_config_material(&wire, &renamed, CONFIG_DEFINITION_SOURCES),
            config_wire_fingerprint()
        );

        // The counterexample the JSON-only fingerprint missed: serde_json emits
        // the same text for integer zero regardless of width, but legacy bincode
        // changes the payload width. A stale peer would decode every following
        // field at the wrong offset.
        #[derive(Serialize)]
        struct U32Field {
            field: u32,
        }
        #[derive(Serialize)]
        struct U64Field {
            field: u64,
        }
        let u32_value = U32Field { field: 0 };
        let u64_value = U64Field { field: 0 };
        let u32_json = serde_json::to_string(&u32_value).unwrap();
        let u64_json = serde_json::to_string(&u64_value).unwrap();
        assert_eq!(
            u32_json, u64_json,
            "the planted JSON collision must be real"
        );
        let u32_wire =
            bincode::serde::encode_to_vec(&u32_value, bincode::config::legacy()).unwrap();
        let u64_wire =
            bincode::serde::encode_to_vec(&u64_value, bincode::config::legacy()).unwrap();
        assert_ne!(u32_wire, u64_wire, "the planted wire retype must be real");
        assert_ne!(
            fingerprint_of_config_material(&u32_wire, &u32_json, &[b"struct S { field: u32 }"]),
            fingerprint_of_config_material(&u64_wire, &u64_json, &[b"struct S { field: u64 }"]),
            "a wire-incompatible integer retype must change the fingerprint"
        );

        // Defaults can hide an incompatible inner type in BOTH value encodings.
        // The definition source is therefore load-bearing, not decorative.
        #[derive(Serialize)]
        struct OptionalU32 {
            field: Option<u32>,
        }
        #[derive(Serialize)]
        struct OptionalU64 {
            field: Option<u64>,
        }
        let optional_u32 = OptionalU32 { field: None };
        let optional_u64 = OptionalU64 { field: None };
        let optional_u32_json = serde_json::to_string(&optional_u32).unwrap();
        let optional_u64_json = serde_json::to_string(&optional_u64).unwrap();
        assert_eq!(optional_u32_json, optional_u64_json);
        let optional_u32_wire =
            bincode::serde::encode_to_vec(&optional_u32, bincode::config::legacy()).unwrap();
        let optional_u64_wire =
            bincode::serde::encode_to_vec(&optional_u64, bincode::config::legacy()).unwrap();
        assert_eq!(
            optional_u32_wire, optional_u64_wire,
            "the planted default must be invisible in both value encodings"
        );
        assert_ne!(
            fingerprint_of_config_material(
                &optional_u32_wire,
                &optional_u32_json,
                &[b"struct S { field: Option<u32> }"]
            ),
            fingerprint_of_config_material(
                &optional_u64_wire,
                &optional_u64_json,
                &[b"struct S { field: Option<u64> }"]
            ),
            "a hidden wire-incompatible inner-type change must alter the fingerprint"
        );
    }

    #[test]
    fn config_fingerprint_uses_environment_free_wire_defaults() {
        let environment_derived = Config {
            epoch: "2042-03-04T05:06:07.890123456Z".parse().unwrap(),
            seed: 41,
            sched_seed: Some(42),
            cpu_profile: crate::cpu_profile::CpuProfile::X86_64V3,
            ..Config::default()
        };

        let canonical = config_wire_default();

        assert_ne!(
            serde_json::to_string(&environment_derived).unwrap(),
            serde_json::to_string(&canonical).unwrap()
        );
        assert_eq!(
            canonical.epoch,
            DEFAULT_EPOCH_STR.parse::<DateTime<Utc>>().unwrap()
        );
        assert_eq!(canonical.seed, 0);
        assert_eq!(canonical.sched_seed, None);
        assert_eq!(
            canonical.cpu_profile,
            crate::cpu_profile::CpuProfile::X86_64V2
        );
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1151)
    #[test]
    #[should_panic(expected = "max_timeslice must be at least one RCB")]
    fn validate_scales_one_rcb_minimum_with_chaos_slowdown() {
        let mut config = Config {
            chaos: true,
            chaos_per_thread_slowdown: true,
            chaos_slowdown_max_factor: 4.0,
            max_timeslice: NonZeroU64::new(39),
            ..Default::default()
        };
        config.validate();
    }

    /// The one-RCB minimum multiplies like the PMU epsilon of Detcore's
    /// post-hook, `NANOS_PER_RCB * (clock_multiplier * factor)` in
    /// `detcore/src/lib.rs`, bit for bit. Every pair below rounds differently
    /// when multiplied left to right; in the first three that moves the
    /// minimum: at `--clock-multiplier=2.5` and
    /// `--chaos-slowdown-max-factor=4.4` the left-to-right product refused
    /// `--max-timeslice=110` as below "at least 111".
    #[test]
    fn one_rcb_minimum_multiplies_like_detcores_pmu_epsilon() {
        let slowed = |clock_multiplier: f64, factor: f64, max_timeslice: u64| Config {
            chaos: true,
            chaos_per_thread_slowdown: true,
            chaos_slowdown_max_factor: factor,
            clock_multiplier: Some(clock_multiplier),
            max_timeslice: NonZeroU64::new(max_timeslice),
            ..Default::default()
        };

        let mut config = slowed(2.5, 4.4, 110);
        assert_eq!(config.minimum_max_timeslice_nanos(), 110);
        config.validate();

        for (clock_multiplier, factor, minimum) in [
            (2.5, 4.4, 110),
            (2.5, 2.2, 55),
            (7.0, 1.1, 78),
            (0.7, 3.0, 21),
            (1.1, 5.5, 61),
        ] {
            let detcore = NANOS_PER_RCB * (clock_multiplier * factor);
            assert_ne!(
                (NANOS_PER_RCB * clock_multiplier * factor).to_bits(),
                detcore.to_bits(),
                "{clock_multiplier} x {factor} does not tell the orders apart"
            );
            let config = slowed(clock_multiplier, factor, 1_000_000);
            assert_eq!(
                config.largest_nanos_per_rcb().to_bits(),
                detcore.to_bits(),
                "{clock_multiplier} x {factor}"
            );
            assert_eq!(
                config.minimum_max_timeslice_nanos(),
                minimum,
                "{clock_multiplier} x {factor}"
            );
        }
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1151)
    #[test]
    #[should_panic(expected = "chaos_slowdown_max_factor must be finite and in")]
    fn validate_rejects_unrepresentable_chaos_slowdown_factor() {
        let mut config = Config {
            chaos_slowdown_max_factor: RcbTimeMultiplier::MAX * 2.0,
            ..Default::default()
        };
        config.validate();
    }
}

#[cfg(test)]
mod strict_boundary_mode_tests {
    use super::Config;

    #[test]
    fn strict_boundary_policy_is_explicit_and_round_trips_the_actual_wire() {
        let ordinary = Config::default();
        assert!(!ordinary.strict_schedule_boundaries);
        let mut old_shape = serde_json::to_value(&ordinary).unwrap();
        assert_eq!(
            old_shape
                .as_object_mut()
                .unwrap()
                .remove("strict_schedule_boundaries"),
            Some(false.into())
        );
        assert!(
            !serde_json::from_value::<Config>(old_shape)
                .unwrap()
                .strict_schedule_boundaries
        );
        let explicit = Config {
            strict_schedule_boundaries: true,
            ..ordinary
        };
        let encoded = serde_json::to_value(&explicit).unwrap();
        assert_eq!(encoded["strict_schedule_boundaries"], true);
        assert!(
            serde_json::from_value::<Config>(encoded)
                .unwrap()
                .strict_schedule_boundaries
        );
        assert!(
            explicit
                .to_string()
                .contains("--strict-schedule-boundaries")
        );
        for config in [&Config::default(), &explicit] {
            let bytes = bincode::serde::encode_to_vec(config, bincode::config::legacy()).unwrap();
            let (decoded, consumed): (Config, usize) =
                bincode::serde::decode_from_slice(&bytes, bincode::config::legacy()).unwrap();
            assert_eq!(consumed, bytes.len());
            assert_eq!(
                decoded.strict_schedule_boundaries,
                config.strict_schedule_boundaries
            );
            assert_eq!(
                bincode::serde::encode_to_vec(&decoded, bincode::config::legacy()).unwrap(),
                bytes
            );
        }
        let legacy = super::to_legacy_backend_json(&Config::default()).unwrap();
        assert!(!legacy.contains("strict_schedule_boundaries"));
        assert!(
            !super::from_legacy_backend_json(&legacy)
                .unwrap()
                .strict_schedule_boundaries
        );
        assert!(super::to_legacy_backend_json(&explicit).is_err());
    }
}