hermit-run 0.5.0

Hermit: a deterministic Linux execution engine for reproducible runs, record and replay, and concurrency-race search.
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
/*
 * 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.
 */

// AUTONOMOUS-BOT-IMPLEMENTED

//! Execution-backend dispatch for `hermit run`.
//!
//! The DBT path launches the real guest through DynamoRIO and links the native
//! client against Hermit's `detcore-dbt` runtime. That runtime instantiates the
//! production [`detcore::Detcore`] Tool over [`reverie_dbt::DbtGuest`].
//!
//! Generic SaBRe runs are coordinated by `libhermit` with the real Detcore
//! plugin. This module retains the separate
//! `hermit --backend sabre strace` diagnostic path.

#[cfg(feature = "dbt")]
use std::collections::BTreeMap;
#[cfg(feature = "dbt")]
use std::collections::BTreeSet;
#[cfg(feature = "dbt")]
use std::env;
#[cfg(feature = "dbt")]
use std::ffi::OsStr;
#[cfg(any(feature = "sabre", feature = "dbt"))]
use std::ffi::OsString;
#[cfg(any(feature = "sabre", feature = "dbt"))]
use std::fs;
#[cfg(feature = "dbt")]
use std::io::IsTerminal as _;
#[cfg(feature = "dbt")]
use std::io::Read;
#[cfg(feature = "dbt")]
use std::io::Seek as _;
#[cfg(feature = "dbt")]
use std::io::SeekFrom;
#[cfg(feature = "dbt")]
use std::io::Write;
#[cfg(feature = "dbt")]
use std::os::fd::AsFd;
#[cfg(feature = "dbt")]
use std::os::fd::AsRawFd;
#[cfg(feature = "dbt")]
use std::os::fd::FromRawFd;
#[cfg(any(feature = "sabre", feature = "dbt"))]
use std::os::unix::fs::PermissionsExt;
#[cfg(any(feature = "dbt", test))]
use std::os::unix::process::ExitStatusExt as _;
use std::path::Path;
#[cfg(feature = "dbt")]
use std::path::PathBuf;
#[cfg(any(feature = "sabre", feature = "dbt"))]
use std::process::Command as StdCommand;
#[cfg(any(feature = "dbt", test))]
use std::process::Output;
#[cfg(feature = "dbt")]
use std::sync::Arc;

#[cfg(feature = "dbt")]
use detcore::Config;
#[cfg(feature = "dbt")]
use detcore_model::backend_engagement::BackendEngagement;
#[cfg(feature = "dbt")]
use detcore_model::backend_engagement::BackendEngagementReport;
use hermit::Error;
use hermit::ExitStatus;
#[cfg(feature = "dbt")]
use reverie::process::Bind;
#[cfg(feature = "dbt")]
use reverie_dbt::DbtEvidence;
#[cfg(feature = "dbt")]
use reverie_dbt::DbtEvidenceLogLevel;
#[cfg(feature = "dbt")]
use reverie_dbt::DbtRunner;
#[cfg(feature = "dbt")]
use reverie_dbt::DbtStatsSink;
#[cfg(feature = "dbt")]
use reverie_dbt::backend_stats::DbtBackendStatsSnapshot;
#[cfg(feature = "dbt")]
use reverie_dbt::backend_stats::DbtStatsCapture;
#[cfg(any(feature = "sabre", feature = "dbt"))]
use tracing::metadata::LevelFilter;

#[cfg(feature = "dbt")]
use super::record_envelope::RecordEnvelope;
#[cfg(feature = "dbt")]
use super::verify::ComparedRun;
#[cfg(feature = "dbt")]
use super::verify::ComparisonOptions;
#[cfg(feature = "dbt")]
use super::verify::DbtCountedBranchComparison;
#[cfg(feature = "dbt")]
use super::verify::LogCompareStrictness;
#[cfg(feature = "dbt")]
use super::verify::SecondRun;
#[cfg(feature = "dbt")]
use super::verify::Verdict;
#[cfg(feature = "dbt")]
use super::verify::VerificationOutcome;
#[cfg(feature = "dbt")]
use super::verify::VerificationRun;
#[cfg(feature = "dbt")]
use super::verify::announce_verification_outcome;
#[cfg(feature = "dbt")]
use super::verify::compare_two_runs;
#[cfg(feature = "dbt")]
use super::verify::record_terminal_deadlock;
#[cfg(feature = "dbt")]
use super::verify::verification_log_level;
#[cfg(feature = "dbt")]
use super::verify::write_verification_json;

#[cfg(feature = "dbt")]
fn same_dbt_observable_behavior(
    first: &DbtBackendStatsSnapshot,
    second: &DbtBackendStatsSnapshot,
) -> bool {
    // `counted_branches` is deliberately absent here because the branch clock
    // is checked separately, with its own typed evidence. It is NOT excluded
    // because a difference is tolerable.
    first.intercepted_syscalls() == second.intercepted_syscalls()
        && first.rewritten_syscalls() == second.rewritten_syscalls()
        && first.stdin_reads() == second.stdin_reads()
        && first.memory_hash_fold() == second.memory_hash_fold()
}

/// Describe a counted-branch-clock divergence without conflating it with the
/// other native summary fields.
#[cfg(feature = "dbt")]
fn dbt_branch_clock_mismatch(first: u64, second: u64) -> Option<String> {
    (first != second).then(|| {
        format!(
            "DBT verification failed: counted-branch clocks differed between runs ({first} != {second}); \
             the clock is a deterministic function of the executed instruction stream"
        )
    })
}

/// Compare the authenticated number of DBT process images as a typed dimension.
///
/// Each process-image initialization record is also compared in the log, at its
/// stream position, so a differing count diverges there too. This check names
/// that cause directly instead of leaving it to be inferred from a record diff.
#[cfg(feature = "dbt")]
fn dbt_initialization_record_mismatch(first: usize, second: usize) -> Option<String> {
    (first != second).then(|| {
        format!(
            "DBT verification failed: authenticated process-image initialization counts differed between runs ({first} != {second})"
        )
    })
}

#[cfg(feature = "dbt")]
#[derive(Debug)]
struct DbtSummaryComparison {
    failure: Option<String>,
}

#[cfg(feature = "dbt")]
impl DbtSummaryComparison {
    fn compare(
        first_stats: &DbtBackendStatsSnapshot,
        second_stats: &DbtBackendStatsSnapshot,
        first_initialization_records: usize,
        second_initialization_records: usize,
    ) -> Self {
        let mut failures = Vec::new();
        if !same_dbt_observable_behavior(first_stats, second_stats) {
            failures.push(format!(
                "DBT verification failed: typed native Detcore statistics differed \
                 ({first_stats:?} != {second_stats:?})"
            ));
        }
        if let Some(failure) = dbt_initialization_record_mismatch(
            first_initialization_records,
            second_initialization_records,
        ) {
            failures.push(failure);
        }
        Self {
            failure: (!failures.is_empty()).then(|| failures.join("; ")),
        }
    }

    fn requires_log_retention(&self) -> bool {
        self.failure.is_some()
    }

    fn apply(self, outcome: &mut VerificationOutcome) -> Option<String> {
        if self.failure.is_some() {
            outcome.verdict = Verdict::Diverged;
            outcome.no_result_reason = None;
        }
        self.failure
    }
}

/// Add a backend-observed divergence to the typed verification verdict.
///
/// The canonical comparator may have matched its stdout, stderr, status, and
/// INFO records, or it may have refused a truncated log. Neither can erase a
/// separately observed difference in the deterministic counted-branch clock.
/// Mutating the typed outcome before it is serialized ensures `--verify-json`
/// says `diverged` with `verified=false` and `bitwise_parity=false`, rather than
/// leaving a false match or the invocation's pending `no_result` record.
#[cfg(feature = "dbt")]
fn record_dbt_branch_clock_comparison(
    outcome: &mut VerificationOutcome,
    comparison: DbtCountedBranchComparison,
) -> Option<String> {
    match dbt_branch_clock_mismatch(comparison.left, comparison.right) {
        Some(message) => {
            outcome.dbt_counted_branches = Some(comparison);
            outcome.verdict = Verdict::Diverged;
            outcome.no_result_reason = None;
            Some(message)
        }
        None if outcome.verdict == Verdict::NoResult => {
            // Equal clocks do not turn a refused common comparison into a
            // verdict. Keep the backend field absent so `no_result` does not
            // claim that this one successful dimension authorized anything.
            outcome.dbt_counted_branches = None;
            None
        }
        None => {
            outcome.dbt_counted_branches = Some(comparison);
            None
        }
    }
}

/// Apply Hermit's execution-evidence policy to the shared physical capture.
/// Capture/reading/decoding belong to Reverie; this refusal policy also applies
/// to caller-retained saved runs and remains after evidence/latch checks.
#[cfg(feature = "dbt")]
pub(super) fn require_dbt_stats(
    capture: &DbtStatsCapture,
) -> Result<DbtBackendStatsSnapshot, Error> {
    match capture {
        DbtStatsCapture::Missing => {
            return Err(Error::msg(
                "DBT verification did not reach a verdict: typed whole-process statistics were missing",
            ));
        }
        DbtStatsCapture::Unreadable { message, .. } => {
            return Err(Error::msg(format!(
                "DBT verification did not reach a verdict: failed to read typed whole-process statistics: {message}"
            )));
        }
        DbtStatsCapture::Records(bytes) if bytes.is_empty() => {
            return Err(Error::msg(
                "DBT verification did not reach a verdict: typed whole-process statistics were empty",
            ));
        }
        DbtStatsCapture::Records(_) => {}
    }
    let snapshot = capture.snapshot().map_err(|error| Error::msg(format!(
        "DBT verification did not reach a verdict: typed whole-process statistics were unreadable: {error}"
    )))?;
    if snapshot.process_images() == 0 {
        return Err(Error::msg(
            "DBT verification did not reach a verdict: typed whole-process statistics contained no process records",
        ));
    }
    if snapshot.counted_branches() == 0
        || snapshot.intercepted_syscalls() == 0
        || snapshot.rewritten_syscalls() > snapshot.intercepted_syscalls()
    {
        return Err(Error::msg(
            "DBT typed native callback counters are inconsistent",
        ));
    }
    Ok(snapshot)
}

/// Render the native DBT counters as a labeled `--summary` block.
///
/// These are the counters the DynamoRIO client already emits at exit; hermit
/// simply surfaces them on the normal run path. Labels are deliberately honest
/// about what each number is: `branches` is Detcore's deterministic
/// counted-branch clock (cbr/ubr/call/return retired), **not** a count of
/// translated basic blocks, and `memory hash` is the client's observed
/// guest-memory digest, not a Detcore RunSummary field.
#[cfg(feature = "dbt")]
fn format_dbt_stats(summary: &DbtBackendStatsSnapshot) -> String {
    format!(
        "=== DBT backend stats (native DynamoRIO client) ===\n\
         counted branches (deterministic branch clock): {}\n\
         syscalls intercepted:                          {}\n\
         syscall instructions rewritten:                {}\n\
         stdin (fd 0) reads:                            {}\n\
         observed guest-memory hash:                    {}\n",
        summary.counted_branches(),
        summary.intercepted_syscalls(),
        summary.rewritten_syscalls(),
        summary.stdin_reads(),
        format_args!("{:016x}", summary.memory_hash_fold()),
    )
}

#[cfg(feature = "dbt")]
fn write_dbt_engagement(path: &Path, snapshot: &DbtBackendStatsSnapshot) -> Result<(), Error> {
    let report = BackendEngagementReport::new(BackendEngagement::Dbt {
        counted_branches: snapshot.counted_branches(),
    });
    report.validate().map_err(Error::msg)?;
    let mut bytes = serde_json::to_vec(&report)?;
    bytes.push(b'\n');
    fs::write(path, bytes).map_err(|error| {
        Error::msg(format!(
            "writing DBT engagement {}: {error}",
            path.display()
        ))
    })
}

/// The exit code with which the DBT native client ends a process on a backend
/// or Tool failure (`exit_runtime_tree(101)` in reverie-dbt's client).
#[cfg(feature = "dbt")]
const DBT_BACKEND_FAILURE_EXIT_CODE: i32 = 101;

/// Prints and records a single run's statistics as requested, and returns them
/// when they could be read. A run always captures them: they are also how a
/// backend failure in a guest child is found (see [`fail_on_dbt_backend_failure`]).
#[cfg(feature = "dbt")]
fn finish_single_run_dbt_stats(
    capture: &DbtStatsCapture,
    summary: bool,
    engagement_json: Option<&Path>,
) -> Result<Option<DbtBackendStatsSnapshot>, Error> {
    match require_dbt_stats(capture) {
        Ok(stats) => {
            if summary {
                eprint!("{}", format_dbt_stats(&stats));
            }
            if let Some(path) = engagement_json {
                write_dbt_engagement(path, &stats)?;
            }
            Ok(Some(stats))
        }
        Err(error) if engagement_json.is_some() => Err(error),
        Err(error) => {
            if summary {
                eprintln!(":: DBT summary unavailable: {error}");
            }
            eprintln!(":: WARNING: DBT execution statistics are unavailable: {error}");
            Ok(None)
        }
    }
}

/// `status`, or a failed status when any process image of the run ended on a
/// backend or Tool failure.
///
/// The native client ends such a process with exit code 101. When the guest
/// tree has no isolated process group (`--allow-unsupported-syscalls` without
/// `--verify`), that ends only the failing process, and a parent that ignores
/// its child's status can still finish the run with any status, 0 included.
/// The failing image flags its stats record, so the run fails here instead.
#[cfg(feature = "dbt")]
fn fail_on_dbt_backend_failure(
    status: ExitStatus,
    capture: Option<&DbtStatsCapture>,
) -> ExitStatus {
    // A complete failure-only record remains authoritative even if the root
    // was killed before it could publish execution counters. It cannot prove
    // engagement or verification completeness; require_dbt_stats owns those.
    let failures = capture
        .and_then(|capture| capture.snapshot().ok())
        .map_or(0, |stats| stats.backend_failures());
    if failures == 0 || status == ExitStatus::Exited(DBT_BACKEND_FAILURE_EXIT_CODE) {
        return status;
    }
    eprintln!(
        "hermit: [dbt backend] {failures} guest process(es) ended on a DBT backend or Tool \
         failure (exit {DBT_BACKEND_FAILURE_EXIT_CODE}), but the guest tree finished with \
         {status:?}; failing the run with exit {DBT_BACKEND_FAILURE_EXIT_CODE}. The failing \
         process's diagnostic is above."
    );
    ExitStatus::Exited(DBT_BACKEND_FAILURE_EXIT_CODE)
}

#[derive(Debug, Eq, PartialEq)]
#[cfg(feature = "dbt")]
struct DbtGuestCommand {
    program: PathBuf,
    args: Vec<OsString>,
}

#[cfg(feature = "dbt")]
fn executable_on_path(program: &OsStr, path: &OsStr, cwd: &Path) -> Option<PathBuf> {
    executable_path_candidate(program, path, cwd).map(|candidate| cwd.join(candidate))
}

#[cfg(feature = "dbt")]
fn executable_path_candidate(program: &OsStr, path: &OsStr, cwd: &Path) -> Option<PathBuf> {
    env::split_paths(path)
        .map(|directory| directory.join(program))
        .find(|candidate| {
            cwd.join(candidate).metadata().is_ok_and(|metadata| {
                metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
            })
        })
}

/// Resolve the simple `#!/usr/bin/env PROGRAM` form before DynamoRIO starts.
///
/// DynamoRIO follows an absolute exec target correctly, but its copied exec path
/// can wait indefinitely when `env` later resolves a bare target through PATH.
/// Keep `env` in the process chain and replace only its single plain program
/// token with the equivalent absolute PATH match. More complex `env` forms are
/// left unchanged for the normal launcher rather than partially interpreting
/// options or assignments here.
#[cfg(feature = "dbt")]
fn prepare_dbt_guest_command(
    program: &Path,
    args: &[OsString],
    path: Option<&OsStr>,
    cwd: &Path,
) -> DbtGuestCommand {
    let unchanged = || DbtGuestCommand {
        program: program.to_path_buf(),
        args: args.to_vec(),
    };
    // Command resolves relative paths and PATH entries after changing cwd.
    // Inspect that same file in the physical run's already-established mount
    // namespace; the caller's /test may have been hidden by its fresh tmpfs.
    let script_path =
        if program.is_absolute() || program.as_os_str().as_encoded_bytes().contains(&b'/') {
            program.to_path_buf()
        } else if let Some(found) =
            path.and_then(|path| executable_path_candidate(program.as_os_str(), path, cwd))
        {
            found
        } else {
            return unchanged();
        };
    let Some(shebang) = hermit::Shebang::new(cwd.join(&script_path)) else {
        return unchanged();
    };
    let (interpreter, interpreter_args) = shebang.into_parts();
    if interpreter.file_name() != Some(OsStr::new("env")) || interpreter_args.len() != 1 {
        return unchanged();
    }

    let target = &interpreter_args[0];
    let target_bytes = std::os::unix::ffi::OsStrExt::as_bytes(target.as_os_str());
    if target_bytes.starts_with(b"-")
        || target_bytes.contains(&b'=')
        || target_bytes.contains(&b'/')
    {
        return unchanged();
    }
    let Some(target) = path.and_then(|path| executable_on_path(target, path, cwd)) else {
        return unchanged();
    };

    let mut resolved_args = Vec::with_capacity(args.len() + 2);
    resolved_args.push(target.into_os_string());
    // execvp supplies the selected PATH candidate to a script interpreter.
    // Explicit paths retain their spelling; a bare name must not discard the
    // directory that actually selected its script.
    resolved_args.push(script_path.into_os_string());
    resolved_args.extend_from_slice(args);
    DbtGuestCommand {
        program: interpreter,
        args: resolved_args,
    }
}

// run_dbt supplies program/args, an exact environment represented by values
// and explicit removals, and cwd.
// Reconstruct those fields inside the physical run, before DbtRunner performs
// its own command reconstruction. Stdin and evidence descriptors remain owned
// by the existing runner adapters.
#[cfg(feature = "dbt")]
fn prepare_dbt_physical_command(guest: &StdCommand) -> Result<StdCommand, Error> {
    let environment = guest
        .get_envs()
        .filter_map(|(key, value)| value.map(|value| (key.to_owned(), value.to_owned())))
        .collect::<BTreeMap<_, _>>();
    let args = guest.get_args().map(OsStr::to_owned).collect::<Vec<_>>();
    let cwd = env::current_dir()?;
    let cwd = guest
        .get_current_dir()
        .map_or(cwd.clone(), |workdir| cwd.join(workdir));
    let prepared = prepare_dbt_guest_command(
        Path::new(guest.get_program()),
        &args,
        environment.get(OsStr::new("PATH")).map(OsString::as_os_str),
        &cwd,
    );
    let mut command = StdCommand::new(prepared.program);
    command.args(prepared.args);
    // Do not call env_clear here: std::Command then erases removal entries,
    // so DbtRunner cannot see them when it reconstructs from get_envs().
    // DbtRunner copies explicit removals as well as values. Preserve the raw
    // command's removal entries instead of re-enumerating ambient variables.
    for (name, value) in guest.get_envs() {
        if let Some(value) = value {
            command.env(name, value);
        } else {
            command.env_remove(name);
        }
    }
    if let Some(workdir) = guest.get_current_dir() {
        command.current_dir(workdir);
    }
    Ok(command)
}

#[cfg(feature = "dbt")]
fn apply_exact_environment(command: &mut StdCommand, environment: &BTreeMap<OsString, OsString>) {
    // DbtRunner reconstructs its launcher command from Command::get_envs(),
    // which cannot expose env_clear(). Make removals explicit so --base-env
    // does not accidentally inherit the Hermit launcher's environment.
    for (key, _) in env::vars_os() {
        if !environment.contains_key(&key) {
            command.env_remove(key);
        }
    }
    command.envs(environment);
}

#[cfg(feature = "dbt")]
fn apply_dbt_workdir(command: &mut StdCommand, workdir: Option<&Path>) {
    if let Some(workdir) = workdir {
        command.current_dir(workdir);
    }
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-644): Review inherited DBT policy descriptors and bounded reports.
#[cfg(feature = "dbt")]
struct InstalledFd {
    target: i32,
    backup: Option<i32>,
    original_flags: Option<i32>,
}

#[cfg(feature = "dbt")]
impl InstalledFd {
    fn install(source: i32, target: i32) -> std::io::Result<Self> {
        // Keep the backup above the reserved transport descriptor so installing the target
        // cannot overwrite its backup.
        let backup = unsafe {
            libc::fcntl(
                target,
                libc::F_DUPFD_CLOEXEC,
                detcore_dbt::UNSUPPORTED_SYSCALL_REPORT_FD + 1,
            )
        };
        let backup = if backup == -1 {
            let error = std::io::Error::last_os_error();
            if error.raw_os_error() == Some(libc::EBADF) {
                None
            } else {
                return Err(error);
            }
        } else {
            Some(backup)
        };
        let original_flags = if let Some(backup_fd) = backup {
            let flags = unsafe { libc::fcntl(target, libc::F_GETFD) };
            if flags == -1 {
                let error = std::io::Error::last_os_error();
                let _ = unsafe { libc::close(backup_fd) };
                return Err(error);
            }
            Some(flags)
        } else {
            None
        };
        let installed = Self {
            target,
            backup,
            original_flags,
        };
        if unsafe { libc::dup2(source, target) } == -1 {
            return Err(std::io::Error::last_os_error());
        }
        if unsafe { libc::fcntl(target, libc::F_SETFD, 0) } == -1 {
            let error = std::io::Error::last_os_error();
            drop(installed);
            return Err(error);
        }
        Ok(installed)
    }
}

#[cfg(feature = "dbt")]
impl Drop for InstalledFd {
    fn drop(&mut self) {
        if let Some(backup) = self.backup {
            let _ = unsafe { libc::dup2(backup, self.target) };
            if let Some(flags) = self.original_flags {
                let _ = unsafe { libc::fcntl(self.target, libc::F_SETFD, flags) };
            }
            let _ = unsafe { libc::close(backup) };
        } else {
            let _ = unsafe { libc::close(self.target) };
        }
    }
}

#[cfg(feature = "dbt")]
struct DbtUnsupportedSyscallReport {
    reader: std::fs::File,
    writer: Arc<std::fs::File>,
    _report_fd: InstalledFd,
}

#[cfg(feature = "dbt")]
impl DbtUnsupportedSyscallReport {
    fn new() -> std::io::Result<Self> {
        let mut descriptors = [-1; 2];
        let result =
            unsafe { libc::pipe2(descriptors.as_mut_ptr(), libc::O_CLOEXEC | libc::O_NONBLOCK) };
        if result == -1 {
            return Err(std::io::Error::last_os_error());
        }
        // SAFETY: pipe2 initialized both descriptors, transferring their ownership here.
        let reader = unsafe { std::fs::File::from_raw_fd(descriptors[0]) };
        let writer = unsafe { std::fs::File::from_raw_fd(descriptors[1]) };
        let report_fd = InstalledFd::install(
            writer.as_raw_fd(),
            detcore_dbt::UNSUPPORTED_SYSCALL_REPORT_FD,
        )?;
        Ok(Self {
            reader,
            writer: Arc::new(writer),
            _report_fd: report_fd,
        })
    }

    fn emit(&mut self) -> std::io::Result<()> {
        const MAX_REPORT_BYTES: usize = 1024 * 1024;
        let mut contents = Vec::new();
        let mut buffer = [0_u8; 4096];
        loop {
            match self.reader.read(&mut buffer) {
                Ok(0) => break,
                Ok(read) => {
                    if contents.len() + read > MAX_REPORT_BYTES {
                        return Err(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            "DBT unsupported-syscall report exceeded 1 MiB",
                        ));
                    }
                    contents.extend_from_slice(&buffer[..read]);
                }
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => break,
                Err(error) => return Err(error),
            }
        }
        let contents = String::from_utf8_lossy(&contents);
        let syscalls = contents
            .lines()
            .filter_map(|line| {
                if let Some(raw) = line.strip_prefix("@") {
                    let sysno = raw
                        .parse::<i32>()
                        .ok()
                        .map(reverie::syscalls::Sysno::from)?;
                    detcore::is_unsupported_syscall(sysno).then(|| sysno.to_string())
                } else if !line.is_empty()
                    && line.len() <= 64
                    && line
                        .bytes()
                        .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
                {
                    Some(line.to_owned())
                } else {
                    None
                }
            })
            .take(512)
            .collect::<BTreeSet<_>>();
        if let Some(message) = detcore::format_unsupported_syscall_warning(&syscalls) {
            eprintln!("WARNING: {message}");
        }
        Ok(())
    }
}

#[cfg(feature = "dbt")]
impl Drop for DbtUnsupportedSyscallReport {
    fn drop(&mut self) {
        if let Err(error) = self.emit() {
            eprintln!("WARNING: failed to read DBT unsupported-syscall report: {error}");
        }
    }
}

#[cfg(feature = "dbt")]
struct TeeReader<R, W> {
    input: R,
    replay: W,
}

#[cfg(feature = "dbt")]
impl<R: Read, W: Write> Read for TeeReader<R, W> {
    fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
        let read = self.input.read(buffer)?;
        self.replay.write_all(&buffer[..read])?;
        Ok(read)
    }
}

#[cfg(feature = "dbt")]
fn dbt_evidence_log_level(
    requested: Option<LevelFilter>,
    diagnostic_full_trace: bool,
) -> DbtEvidenceLogLevel {
    let level = verification_log_level(
        requested,
        LogCompareStrictness::Canonical,
        diagnostic_full_trace,
    );
    if level >= LevelFilter::TRACE {
        DbtEvidenceLogLevel::Trace
    } else if level >= LevelFilter::DEBUG {
        DbtEvidenceLogLevel::Debug
    } else {
        DbtEvidenceLogLevel::Info
    }
}

#[cfg(feature = "dbt")]
fn decode_dbt_evidence(file: &mut std::fs::File) -> Result<DbtEvidence, Error> {
    file.seek(SeekFrom::Start(0))?;
    let mut encoded = Vec::new();
    file.read_to_end(&mut encoded)?;
    if encoded.is_empty() {
        return Err(Error::msg("DBT canonical evidence was empty"));
    }
    reverie_dbt::decode_evidence(&encoded).map_err(|error| {
        Error::msg(format!(
            "DBT canonical evidence was malformed or truncated: {error}"
        ))
    })
}

#[cfg(feature = "dbt")]
/// The latches of one DBT run. Detcore runs in the guest's DynamoRIO client,
/// so its determinism-loss latch is read from the run's evidence: the client
/// writes a record with target [`detcore::detlog::DETERMINISM_LOSS_RECORD_TARGET`]
/// at process exit when its latch is set. The reason is the record's text after
/// the target, escaped as every evidence record is.
fn dbt_run_latches(evidence: &DbtEvidence) -> super::run::RunLatches {
    let marker = format!(
        " ERROR {}: ",
        detcore::detlog::DETERMINISM_LOSS_RECORD_TARGET
    );
    let determinism_loss = evidence.all_records().iter().find_map(|record| {
        let text = String::from_utf8_lossy(record);
        text.split_once(&marker)
            .map(|(_, reason)| reason.trim_end_matches('\n').to_owned())
    });
    super::run::RunLatches {
        determinism_loss,
        log_sink_failure: None,
    }
}

/// Write the comparison log for one DBT run from its decoded evidence.
///
/// The log holds `DbtEvidence::all_records()`: every authenticated record in
/// stream order, with each process-image initialization record where the
/// Reverie collector placed it, immediately before that image's first comparable
/// record (or at the end of the stream if the image wrote none). That is exactly
/// the stream the `all_records_v1` envelope
/// compares and counts. The refusal guards count `DbtEvidence::records()`
/// instead, as described on `materialize_dbt_comparison_log`.
#[cfg(feature = "dbt")]
fn materialize_dbt_evidence_log(
    evidence: &DbtEvidence,
    log: std::fs::File,
    path: &Path,
) -> Result<usize, Error> {
    let published = evidence.all_records();
    materialize_dbt_comparison_log(evidence.records(), &published, log, path)
}

/// Refuse evidence without guest records, then write `published` to the log.
///
/// `guest_records` are the decoded records other than initialization records,
/// and `published` is the complete stream that is compared. Every stream holds
/// at least one initialization record, which is an INFO record, so a guard over
/// `published` could never fire: a run whose guest produced no evidence would
/// reach the verdict with nonzero compared INFO counts. That is the gap
/// https://github.com/rrnewton/hermit/pull/2356 closed, so both guards here
/// count `guest_records`. Returns the number of INFO records in `published`.
#[cfg(feature = "dbt")]
fn materialize_dbt_comparison_log(
    guest_records: &[Vec<u8>],
    published: &[&[u8]],
    mut log: std::fs::File,
    path: &Path,
) -> Result<usize, Error> {
    if guest_records.is_empty() {
        return Err(Error::msg("DBT canonical evidence contained no records"));
    }
    // Detcore counts INFO records by parsing a log file, so the guest records
    // are written and counted on their own first. The published stream then
    // replaces them and is counted by the same parser.
    let mut write_and_count = |records: &[&[u8]]| -> Result<usize, Error> {
        write_dbt_comparison_records(&mut log, path, records)?;
        detcore::logdiff::write_canonical_info(path, &mut std::io::sink()).map_err(|error| {
            Error::msg(format!(
                "DBT canonical evidence did not contain a valid log stream: {error}"
            ))
        })
    };
    let guest_records: Vec<&[u8]> = guest_records.iter().map(Vec::as_slice).collect();
    if write_and_count(guest_records.as_slice())? == 0 {
        return Err(Error::msg(
            "DBT canonical evidence contained no INFO records",
        ));
    }
    write_and_count(published)
}

/// Replace the contents of the DBT comparison log with `records`, one record
/// per line, and check that none was lost or split on the way.
#[cfg(feature = "dbt")]
fn write_dbt_comparison_records(
    log: &mut std::fs::File,
    path: &Path,
    records: &[&[u8]],
) -> Result<(), Error> {
    log.set_len(0)?;
    log.seek(SeekFrom::Start(0))?;
    for record in records {
        let Some(payload) = record.strip_suffix(b"\n") else {
            return Err(Error::msg(
                "DBT canonical evidence record was missing its terminal newline",
            ));
        };
        if payload.contains(&b'\n') || payload.contains(&b'\r') {
            return Err(Error::msg(
                "DBT canonical evidence record contained an embedded line boundary",
            ));
        }
        log.write_all(record)?;
    }
    log.flush()?;

    // The verdict publishes `all_records_v1`, so nothing between Reverie's
    // authenticated decoder and this log may select records: every record,
    // initialization records included, must reach the log in stream order.
    // Filtering here would be an undisclosed second selection. Each record was
    // checked above to hold no embedded line boundary, so one record is one line
    // and this count is exact.
    let materialized = std::fs::read(path)
        .map_err(|error| Error::msg(format!("DBT canonical evidence log unreadable: {error}")))?
        .iter()
        .filter(|byte| **byte == b'\n')
        .count();
    if materialized != records.len() {
        return Err(Error::msg(format!(
            "DBT canonical evidence log holds {materialized} records but {} were decoded; the \
             comparison publishes the all_records_v1 envelope and must not drop any",
            records.len()
        )));
    }
    Ok(())
}

#[cfg(feature = "dbt")]
fn dbt_verification_output(output: Output) -> reverie::process::Output {
    reverie::process::Output {
        status: process_status(output.status),
        stdout: output.stdout,
        stderr: output.stderr,
    }
}

/// The DBT `--log-file` refusal message, named so a test can assert the exact text
/// rather than a paraphrase of it.
#[cfg(any(feature = "dbt", test))]
pub(super) const DBT_LOG_FILE_REFUSAL: &str =
    "DBT --log-file is unavailable on the ordinary single-run adapter";

/// Is `--log-file` refused for this DBT invocation?
///
/// ⚠️ EXTRACTED SO THE POLICY IS DEFENDED. Before this, `git grep 'log-file is
/// unavailable'` returned exactly ONE hit tree-wide -- the error string itself --
/// with no test asserting when it fires. An undefended refusal can be reversed by
/// accident, and there is a live proposal to reverse this one: hermit#1689's first
/// claim implements DBT `--log-file`, and main adopted the opposite policy in
/// `f0584c1aac` about four minutes after that branch forked, inside a commit about
/// verification verdicts rather than as the subject of a decision.
///
/// This does NOT take a side on which policy is right -- that is an owner ruling.
/// It pins what main does TODAY, so reversing it means deleting a test, which is
/// visible in a diff, instead of editing one condition, which is not.
///
/// The rule: a requested log file is refused on the ordinary single-run adapter,
/// and permitted under `--verify`, where the verification adapter owns the sink.
#[cfg(any(feature = "dbt", test))]
pub(super) fn dbt_log_file_is_refused(log_file_requested: bool, verify: bool) -> bool {
    log_file_requested && !verify
}

/// The prepared physical DBT invocation, shared by ordinary runs and the
/// common verifier. Each execution still owns its container and coordinator.
#[cfg(feature = "dbt")]
pub(super) struct DbtInvocation {
    execution: DbtExecution,
    runner: DbtRunner,
    guest: StdCommand,
    drrun: PathBuf,
    config: Config,
    _unsupported_report: DbtUnsupportedSyscallReport,
}

#[cfg(feature = "dbt")]
impl DbtInvocation {
    #[allow(clippy::too_many_arguments)]
    pub(super) fn new(
        program: &Path,
        args: &[String],
        config: &Config,
        mut environment: BTreeMap<OsString, OsString>,
        workdir: Option<&Path>,
        binds: Vec<(PathBuf, PathBuf)>,
        options: &super::run::RunOpts,
    ) -> Result<Self, Error> {
        // The DBT backend drives a single Detcore external scheduler, so it cannot
        // honor a request to relax thread sequentialization. Fail loudly rather
        // than silently ignoring the flag.
        if !config.sequentialize_threads {
            return Err(Error::msg(
                "the dbt backend requires sequentialized threads; \
             remove --no-sequentialize-threads (or --strace-only) to run under --backend dbt",
            ));
        }
        // The guest sees this string in its environment, so it keeps the encoding
        // it had before the backend facts moved into `Config::backend` and before
        // three constant switches were deleted; see `hermit::dbt_detconfig_json`.
        // The DBT runtime decodes the backend facts back out of it (here DBT's
        // own); when the string is absent or does not parse, it uses the strict
        // default's facts, which are ptrace's.
        let (drrun, client) = detcore_dbt::prepare_native_client().map_err(|error| {
            Error::msg(format!(
                "failed to prepare the Detcore DynamoRIO client: {error}"
            ))
        })?;
        let mut config = config.clone();
        hermit::declare_dbt_launcher(&mut config, &drrun)
            .map_err(|error| Error::msg(format!("{error:#}")))?;
        let config = &config;
        let config_json = hermit::dbt_detconfig_json(config).map_err(|error| {
            Error::msg(format!(
                "failed to serialize the Detcore config for the DBT backend: {error}"
            ))
        })?;
        // The full DetConfig now reaches the DBT runtime via the serialized env
        // above; the fail-closed policy (PR #644) still drives process-group
        // isolation and the client flag here.
        let panic_on_unsupported_syscalls = config.panic_on_unsupported_syscalls;

        let marker = std::env::var_os(hermit_test_workdir::REQUEST_ENV);
        let isolated_workdir = hermit_test_workdir::requested_workdir(marker.as_deref())?;
        let binds = binds
            .into_iter()
            .map(|(source, target)| hermit_test_workdir::BindMount { source, target })
            .collect::<Vec<_>>();
        // A bound workdir is as fresh as the bind source the caller made for this
        // run, which is how the other backends get theirs.
        let workdir_is_bound = workdir
            .is_some_and(|workdir| binds.iter().any(|bind| workdir.starts_with(&bind.target)));
        if isolated_workdir.is_some() && workdir != isolated_workdir && !workdir_is_bound {
            return Err(Error::msg(
                "HERMIT_E2E_EMPTY_WORKDIR=/test requires --workdir=/test, or a --workdir inside a \
             --bind target, for DBT",
            ));
        }
        // The container applies the binds; keep the existing target admission.
        for bind in &binds {
            hermit_test_workdir::check_bind_target(&bind.target)?;
        }
        let unsupported_report = DbtUnsupportedSyscallReport::new()?;
        let execution = DbtExecution::new(
            options,
            isolated_workdir.is_some() && workdir == isolated_workdir,
        )
        .reporting_unsupported_to(unsupported_report.writer.clone())
        .launching(
            program,
            workdir,
            environment.get(OsStr::new("PATH")).map(OsString::as_os_str),
        );

        // Retain the controller sink before the runner replaces the guest's
        // fd 2 with a capture pipe. Native diagnostics must not enter that pipe.
        let diagnostics =
            std::fs::File::from(std::io::stderr().as_fd().try_clone_to_owned().map_err(
                |error| Error::msg(format!("failed to retain DBT diagnostic stderr: {error}")),
            )?);
        let mut runner = DbtRunner::new(&drrun, &client)
            .map_err(|error| {
                Error::msg(format!(
                    "failed to configure the DynamoRIO DBT runner (drrun={}, client={}): {error}",
                    drrun.display(),
                    client.display()
                ))
            })?
            .summary(options.summary)
            .isolated_process_group(panic_on_unsupported_syscalls)
            .tool_answers_brk(true)
            .diagnostic_file(&diagnostics)
            .map_err(|error| {
                Error::msg(format!(
                    "failed to configure DBT diagnostic stderr: {error}"
                ))
            })?;
        drop(diagnostics);
        if panic_on_unsupported_syscalls {
            runner = runner.client_argument("-panic-on-unsupported-syscalls");
        }

        eprintln!(
            "hermit: [dbt backend] Detcore Tool active; running {program:?} under DynamoRIO ({})",
            drrun.display()
        );

        let mut guest = StdCommand::new(program);
        environment.remove(OsStr::new("HERMIT_LOG_FILE"));
        environment.insert(detcore_dbt::DETCONFIG_ENV.into(), config_json.into());
        apply_exact_environment(&mut guest, &environment);
        guest.args(args);
        apply_dbt_workdir(&mut guest, workdir);

        Ok(Self {
            execution,
            runner,
            guest,
            drrun,
            config: config.clone(),
            _unsupported_report: unsupported_report,
        })
    }

    pub(super) fn run(
        mut self,
        summary: bool,
        engagement_json: Option<&Path>,
        log: Option<LevelFilter>,
        log_file: Option<&Path>,
    ) -> Result<ExitStatus, Error> {
        if dbt_log_file_is_refused(log_file.is_some(), false) {
            return Err(Error::msg(DBT_LOG_FILE_REFUSAL));
        }
        if let Some(level) = log {
            self.guest.env("HERMIT_LOG", level.to_string());
        }
        let stats = match self.execution.stats_capture() {
            Ok(capture) => Some(capture),
            Err(error) if summary || engagement_json.is_some() => return Err(error),
            Err(error) => {
                eprintln!(
                    ":: WARNING: the DBT process records cannot be set up, so a backend failure in a guest child cannot be ruled out: {error}"
                );
                None
            }
        };
        if let Some(capture) = &stats {
            self.runner = self.execution.configure_stats(capture, self.runner)?;
        }
        let status = if std::io::stdin().is_terminal() {
            process_status(run_status(
                &self.execution,
                &self.runner,
                &self.guest,
                &self.drrun,
                &self.config,
            )?)
        } else {
            let output = run_once_with_inherited_input(
                &self.execution,
                &self.runner,
                &self.guest,
                &self.drrun,
                &self.config,
                None,
            )?;
            write_output(&output)?;
            output_status(&output)
        };
        let stats = stats.map(|sink| sink.capture());
        if let Some(capture) = &stats {
            finish_single_run_dbt_stats(capture, summary, engagement_json)?;
        }
        if summary {
            // Report the supplied configuration separately from timer delivery.
            // DBT's runtime still clears its maximum internally; this does not
            // change that execution model or claim a preemption mechanism.
            eprintln!(
                "{}",
                detcore_model::summary::PreemptionSummary {
                    max_timeslice_ns: self.config.max_timeslice.map(|maximum| maximum.get()),
                    timer_enabled: Some(false),
                }
            );
        }
        Ok(fail_on_dbt_backend_failure(status, stats.as_ref()))
    }

    pub(super) fn verify_run(
        self,
        directory: &Path,
        log: Option<LevelFilter>,
        verbose: bool,
        stdin: Option<std::fs::File>,
        options: &super::run::RunOpts,
    ) -> Result<ExitStatus, Error> {
        dbt_verify_run(
            directory,
            &self.execution,
            self.runner,
            &self.guest,
            &self.drrun,
            &self.config,
            dbt_evidence_log_level(log, verbose),
            verbose,
            stdin,
            options,
        )
    }

    pub(super) fn verification(
        self,
        stdin: Option<std::fs::File>,
    ) -> Result<DbtVerification, Error> {
        let terminal = stdin
            .as_ref()
            .is_some_and(|input| unsafe { libc::isatty(input.as_raw_fd()) } == 1);
        Ok(DbtVerification {
            invocation: self,
            stdin,
            terminal,
            replay: tempfile::tempfile()?,
        })
    }
}

/// Input replay belongs to one common verify invocation. Reading begins inside
/// run 1; an open, idle producer never has to reach EOF before the guest starts.
#[cfg(feature = "dbt")]
pub(super) struct DbtVerification {
    invocation: DbtInvocation,
    stdin: Option<std::fs::File>,
    terminal: bool,
    replay: std::fs::File,
}

#[cfg(feature = "dbt")]
impl DbtVerification {
    pub(super) fn run(
        &mut self,
        log: std::fs::File,
        log_path: &Path,
        run: VerificationRun,
        requested_log: Option<LevelFilter>,
        verbose: bool,
        verify_json: Option<&Path>,
    ) -> Result<super::run::VerificationRunOutput, Error> {
        let invocation = &self.invocation;
        let stats = invocation.execution.stats_capture()?;
        let runner = invocation
            .execution
            .configure_stats(&stats, invocation.runner.clone())?;
        let mut evidence_file = tempfile::tempfile()?;
        let evidence = Some(DbtRunEvidence {
            file: &evidence_file,
            log_level: dbt_evidence_log_level(requested_log, verbose),
            label: match run {
                VerificationRun::Run1 => "run-1",
                VerificationRun::Run2 => "run-2",
            },
        });
        let deadlock_recording = record_terminal_deadlock(verify_json, run);
        let output = if self.terminal {
            run_once_with_inherited_input(
                &invocation.execution,
                &runner,
                &invocation.guest,
                &invocation.drrun,
                &invocation.config,
                evidence,
            )
        } else {
            let input: Box<dyn Read + Send> = match run {
                VerificationRun::Run1 => match self.stdin.take() {
                    Some(input) => Box::new(TeeReader {
                        input,
                        replay: self.replay.try_clone()?,
                    }),
                    None => Box::new(std::io::empty()),
                },
                VerificationRun::Run2 => {
                    self.replay.seek(SeekFrom::Start(0))?;
                    Box::new(self.replay.try_clone()?)
                }
            };
            run_once(
                &invocation.execution,
                &runner,
                &invocation.guest,
                &invocation.drrun,
                &invocation.config,
                evidence,
                input,
            )
        };
        drop(deadlock_recording);
        let output = output?;
        // These failures occurred after a physical run, unlike preparation
        // errors. Keep the cause and let the common owner publish its run label.
        let evidence = decode_dbt_evidence(&mut evidence_file)
            .map_err(|error| error.context(hermit::BackendRunFailure))?;
        materialize_dbt_evidence_log(&evidence, log, log_path)
            .map_err(|error| error.context(hermit::BackendRunFailure))?;
        Ok(super::run::VerificationRunOutput {
            output: dbt_verification_output(output),
            skid_overshoots: 0,
            latches: dbt_run_latches(&evidence),
            dbt: Some(DbtRunCompletion {
                stats,
                initialization_records: evidence.initialization_records(),
                terminal_stdin: self.terminal,
            }),
        })
    }
}

/// Stats are consumed only after latches and first-run exit policy, preserving
/// their diagnostic precedence even when the stats artifact is missing.
#[cfg(feature = "dbt")]
pub(super) struct DbtRunCompletion {
    stats: DbtStatsSink,
    initialization_records: usize,
    terminal_stdin: bool,
}

#[cfg(feature = "dbt")]
impl DbtRunCompletion {
    pub(super) fn finish(
        self,
        output: &reverie::process::Output,
        run: VerificationRun,
    ) -> Result<DbtRunFacts, Error> {
        let stats = require_dbt_stats(&self.stats.capture())
            .map_err(|error| error.context(hermit::BackendRunFailure))?;
        if run == VerificationRun::Run1 && self.terminal_stdin && stats.stdin_reads() != 0 {
            std::io::stdout().write_all(&output.stdout)?;
            std::io::stderr().write_all(&output.stderr)?;
            return Err(Error::msg(format!(
                "DBT verification cannot replay terminal stdin: guest attempted {} fd-0 read syscall(s)",
                stats.stdin_reads()
            )));
        }
        Ok(DbtRunFacts {
            stats,
            initialization_records: self.initialization_records,
        })
    }
}

#[cfg(feature = "dbt")]
pub(super) struct DbtRunFacts {
    stats: DbtBackendStatsSnapshot,
    initialization_records: usize,
}

/// Backend facts supplement the common comparison, never replace it. Decide
/// retention before the comparator consumes logs; apply facts before reporting.
#[cfg(feature = "dbt")]
pub(super) struct DbtVerificationComparison {
    branches: DbtCountedBranchComparison,
    summary: DbtSummaryComparison,
}

#[cfg(feature = "dbt")]
impl DbtVerificationComparison {
    pub(super) fn new(first: &DbtRunFacts, second: &DbtRunFacts) -> Self {
        Self {
            branches: DbtCountedBranchComparison {
                left: first.stats.counted_branches(),
                right: second.stats.counted_branches(),
            },
            summary: DbtSummaryComparison::compare(
                &first.stats,
                &second.stats,
                first.initialization_records,
                second.initialization_records,
            ),
        }
    }

    pub(super) fn configure(&self, options: &mut ComparisonOptions) {
        // DBT already compared canonically even for plain --verify. Joining
        // the common owner must not lower that existing observation contract.
        options.strictness = LogCompareStrictness::Canonical;
        let mismatch = !self.branches.matched() || self.summary.requires_log_retention();
        options.keep_logs |= mismatch;
        options.match_overridden |= mismatch;
    }

    pub(super) fn apply(self, outcome: &mut VerificationOutcome) -> Vec<String> {
        let summary = self.summary.apply(outcome);
        let branches = record_dbt_branch_clock_comparison(outcome, self.branches);
        branches.into_iter().chain(summary).collect()
    }
}

/// `run --verify-run DIR` on DBT: one physical verification run,
/// run through the same adapter as live verification, with what its comparison reads written to DIR
/// (`verify_run`): the log materialized from the run's evidence, the guest's
/// output and status, the typed whole-process statistics, and the count of
/// initialization records. `hermit log-diff --verify-json` compares two such
/// directories with [`conclude_dbt_verification`].
#[allow(clippy::too_many_arguments)]
#[cfg(feature = "dbt")]
fn dbt_verify_run(
    directory: &Path,
    execution: &DbtExecution,
    runner: DbtRunner,
    guest: &StdCommand,
    drrun: &Path,
    config: &Config,
    evidence_level: DbtEvidenceLogLevel,
    verify_verbose: bool,
    verification_stdin: Option<std::fs::File>,
    options: &super::run::RunOpts,
) -> Result<ExitStatus, Error> {
    use super::verify_run;

    let writer = verify_run::VerifyRunWriter::create(directory)?;
    let log_path = writer.path(verify_run::LOG);
    let log_file = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&log_path)
        .map_err(|error| {
            Error::msg(format!(
                "creating the verification log {}: {error}",
                log_path.display()
            ))
        })?;
    let comparison = verify_run::RecordedComparison {
        strictness: LogCompareStrictness::Canonical,
        diagnostic_full_trace: verify_verbose,
        compare_io_buffers: config.detlog_io_buffers,
        virtualize_time: config.virtualize_time,
    };
    let mut evidence = tempfile::tempfile()?;
    let stats = execution.stats_capture()?;
    let runner = execution.configure_stats(&stats, runner)?;
    let run_evidence = Some(DbtRunEvidence {
        file: &evidence,
        log_level: evidence_level,
        label: "run",
    });
    let terminal_stdin = verification_stdin.as_ref().is_some_and(|file| {
        // SAFETY: `as_raw_fd` borrows a live descriptor for this check.
        (unsafe { libc::isatty(file.as_raw_fd()) }) == 1
    });

    eprintln!(":: DBT run...");
    let raw = if terminal_stdin {
        run_once_with_inherited_input(execution, &runner, guest, drrun, config, run_evidence)
    } else {
        match verification_stdin {
            Some(input) => run_once(
                execution,
                &runner,
                guest,
                drrun,
                config,
                run_evidence,
                input,
            ),
            None => run_once(
                execution,
                &runner,
                guest,
                drrun,
                config,
                run_evidence,
                std::io::empty(),
            ),
        }
    };
    // Each step in the order live verification takes for each of its runs, so the
    // comparison meets a failure where `run --verify` meets it.
    let completed = raw.and_then(|raw| {
        let evidence = decode_dbt_evidence(&mut evidence)
            .map_err(|error| error.context(hermit::BackendRunFailure))?;
        materialize_dbt_evidence_log(&evidence, log_file, &log_path)
            .map_err(|error| error.context(hermit::BackendRunFailure))?;
        dbt_run_latches(&evidence).refuse_comparison("run")?;
        let records = stats.capture();
        let snapshot = require_dbt_stats(&records)
            .map_err(|error| error.context(hermit::BackendRunFailure))?;
        if terminal_stdin && snapshot.stdin_reads() != 0 {
            return Err(Error::msg(format!(
                "DBT verification cannot replay terminal stdin: guest attempted {} fd-0 read \
                 syscall(s)",
                snapshot.stdin_reads()
            )));
        }
        Ok((dbt_verification_output(raw), evidence, records))
    });
    let (outcome, dbt, result) = match completed {
        Ok((output, evidence, records)) => {
            writer.write(verify_run::STDOUT, &output.stdout)?;
            writer.write(verify_run::STDERR, &output.stderr)?;
            writer.write(verify_run::DBT_STATS, records.records()?)?;
            (
                verify_run::VerifyRunOutcome::Completed {
                    status: verify_run::RecordedStatus::of(output.status),
                    skid_overshoots: 0,
                    forwarded_syscalls: None,
                },
                Some(verify_run::DbtRunRecord {
                    initialization_records: evidence.initialization_records() as u64,
                }),
                Ok(output.status),
            )
        }
        Err(error) => (super::run::verify_run_outcome_of(&error), None, Err(error)),
    };
    writer.commit(&options.verify_run_record(comparison, None, outcome, dbt))?;
    result
}

/// One completed DBT verification run: what [`conclude_dbt_verification`]
/// compares.
#[cfg(feature = "dbt")]
pub(super) struct DbtCompletedRun<'a> {
    pub(super) output: &'a reverie::process::Output,
    /// The run's comparison log (`materialize_dbt_evidence_log`).
    pub(super) log: tempfile::TempPath,
    pub(super) stats: DbtBackendStatsSnapshot,
    pub(super) initialization_records: usize,
}

/// How a DBT verification is concluded once both runs completed.
#[cfg(feature = "dbt")]
pub(super) struct DbtConclusion<'a> {
    pub(super) verify_json: Option<&'a Path>,
    pub(super) verify_verbose: bool,
    pub(super) compare_io_buffers: bool,
    pub(super) virtualize_time: bool,
    pub(super) keep_logs: bool,
    /// The logs are links to the caller's (`ComparisonOptions::caller_owns_logs`).
    pub(super) caller_owns_logs: bool,
}

/// Compare two completed DBT verification runs: the common comparison, then
/// the counted-branch clocks and the typed native statistics, publish the
/// report, and announce it. `hermit log-diff --verify-json` reaches this
/// offline conclusion after reading two `--verify-run` directories. Live
/// verification uses the same typed facts in the common two-run owner.
#[cfg(feature = "dbt")]
pub(super) fn conclude_dbt_verification(
    first: DbtCompletedRun<'_>,
    second: DbtCompletedRun<'_>,
    conclusion: DbtConclusion<'_>,
) -> Result<VerificationOutcome, Error> {
    let DbtConclusion {
        verify_json,
        verify_verbose,
        compare_io_buffers,
        virtualize_time,
        keep_logs,
        caller_owns_logs,
    } = conclusion;
    let comparison = DbtVerificationComparison::new(
        &DbtRunFacts {
            stats: first.stats,
            initialization_records: first.initialization_records,
        },
        &DbtRunFacts {
            stats: second.stats,
            initialization_records: second.initialization_records,
        },
    );
    let mut options = ComparisonOptions {
        verbose: verify_verbose,
        strictness: LogCompareStrictness::Canonical,
        compare_logs: true,
        diagnostic_full_trace: verify_verbose,
        compare_io_buffers,
        virtualize_time,
        keep_logs,
        match_overridden: false,
        failed_log_retention: (!keep_logs).then(super::verify::default_failed_verify_log_retention),
        // Every process-image initialization remains at its observed stream
        // position, and typed initialization counts also enter the facts below.
        record_envelope: RecordEnvelope::all_records_v1(),
        caller_owns_logs,
    };
    comparison.configure(&mut options);
    let mut outcome = compare_two_runs(
        ComparedRun {
            output: first.output,
            log: first.log,
            label: "run 1",
        },
        ComparedRun {
            output: second.output,
            log: second.log,
            label: "run 2",
        },
        options,
    )?;
    let messages = comparison.apply(&mut outcome);
    if let Some(path) = verify_json {
        write_verification_json(path, &outcome)?;
    }
    // Publish before diagnostics, just as the common live owner does.
    for message in messages {
        eprintln!(":: {message}");
    }
    eprintln!(":: DBT path confirmed: DynamoRIO client reported tool=Detcore");
    let success_message = if compare_io_buffers {
        "Success: deterministic. Determinism verified."
    } else {
        "Success: deterministic. Determinism verified. NOTE: syscall \
         output-buffer CONTENT was not compared because --no-detlog-io-buffers \
         was given, so a divergence confined to a buffer whose length is stable \
         would not have been seen; drop that flag to include it."
    };
    announce_verification_outcome(
        &outcome,
        SecondRun::Rerun,
        success_message,
        "Failure: nondeterministic.",
    );
    Ok(outcome)
}

/// Where DynamoRIO keeps its configuration files when set; otherwise `HOME`.
#[cfg(feature = "dbt")]
const DYNAMORIO_CONFIG_DIR_ENV: &str = "DYNAMORIO_CONFIGDIR";

/// Each physical DBT run is the init of its own container, the one every
/// backend uses (`RunOpts::launch_in_container`): PID, UTS, mount and
/// network namespaces, a private /tmp and the identity mounts. Under
/// `--no-namespace` it is a plain child. The coordinator runtime is created
/// inside, so its workers and the guest share those namespaces.
#[cfg(feature = "dbt")]
struct DbtExecution {
    options: super::run::RunOpts,
    /// The current physical run's unique host sink at its stable guest address.
    statistics: std::cell::RefCell<Option<Bind>>,
    /// Existing invocation writer; Arc cloning does not create a descriptor.
    unsupported_report: Option<Arc<std::fs::File>>,
    /// DynamoRIO's configuration directory inside the container; see
    /// `contained_command`.
    config_dir: PathBuf,
    /// The guest program, working directory and `PATH`, from which the
    /// container records the guest's first image
    /// (`hermit::record_dbt_launched_image`).
    launched: Option<(PathBuf, Option<PathBuf>, Option<OsString>)>,
}

#[cfg(feature = "dbt")]
impl DbtExecution {
    fn new(options: &super::run::RunOpts, test_workdir: bool) -> Self {
        // A marked workdir (the validate harness's HERMIT_E2E_EMPTY_WORKDIR)
        // gets a fresh tmpfs, unless the caller already mounts a tmpfs there.
        // It is the guest's working directory, part of the guest's machine,
        // so it is a mount of the caller's kind, built exactly as the
        // harness's `--mount=type=tmpfs,target=/test` is for every other
        // backend (DBT refuses --mount): the guest's mount table then shows
        // the same row on every backend
        // (https://github.com/rrnewton/hermit/issues/4213).
        let test_workdir =
            test_workdir && !options.mounts_tmpfs_at(Path::new(hermit_test_workdir::WORKDIR));
        let options = if test_workdir {
            let workdir = format!("type=tmpfs,target={}", hermit_test_workdir::WORKDIR)
                .parse::<reverie::process::Mount>()
                .expect("a tmpfs mount at the test workdir parses");
            options.with_guest_mount(workdir)
        } else {
            options.clone()
        };
        Self {
            options,
            statistics: Default::default(),
            unsupported_report: None,
            config_dir: dynamorio_config_dir(),
            launched: None,
        }
    }

    fn reporting_unsupported_to(mut self, writer: Arc<std::fs::File>) -> Self {
        self.unsupported_report = Some(writer);
        self
    }

    /// Records `program`, run from `workdir` and found through `path`, as
    /// the guest's first image in the container
    /// (`hermit::record_dbt_launched_image`).
    fn launching(mut self, program: &Path, workdir: Option<&Path>, path: Option<&OsStr>) -> Self {
        self.launched = Some((
            program.to_owned(),
            workdir.map(Path::to_owned),
            path.map(OsStr::to_owned),
        ));
        self
    }

    fn stats_capture(&self) -> Result<DbtStatsSink, Error> {
        // Also clear the preceding mapping when optional allocation fails.
        self.statistics.replace(None);
        DbtStatsSink::with_prefix("hermit-dbt-verify-stats-").map_err(|error| {
            Error::msg(format!(
                "failed to create DBT whole-process statistics sink: {error}"
            ))
        })
    }

    fn configure_stats(
        &self,
        capture: &DbtStatsSink,
        runner: DbtRunner,
    ) -> Result<DbtRunner, Error> {
        if !self.options.uses_container() {
            return Ok(capture.configure(runner));
        }
        let binds = self.options.dbt_binds();
        // Admission accepts only normal descendants of /tmp. Each declared
        // target can overlap at most one of these direct sibling directories.
        // Do not consult host contents: those differ between physical runs.
        let directory = (0..=binds.len())
            .map(|slot| PathBuf::from(format!("/tmp/.hermit-dbt-stats-{slot}")))
            .find(|candidate| {
                binds.iter().all(|(_, target)| {
                    !candidate.starts_with(target) && !target.starts_with(candidate)
                })
            })
            .ok_or_else(|| Error::msg("DBT statistics directory conflicts with declared binds"))?;
        self.statistics
            .replace(Some(Bind::new(capture.directory(), &directory)));
        Ok(capture.configure_at(runner, &directory))
    }

    /// `guest`, copied, with DynamoRIO's configuration directory moved to a
    /// directory of this invocation's own under the container's /tmp.
    ///
    /// drrun hands its client options to the launched process in a one-time
    /// file, `<dir>/.dynamorio/<program>.<pid>.1config64`, where `<dir>` is
    /// `DYNAMORIO_CONFIGDIR` or else `HOME`. In the container the PID is the
    /// namespace's own, so concurrent runs that share `<dir>` agree on the
    /// file name: one guest then reads and deletes the other's file, runs with
    /// that run's evidence channel, and neither run receives its evidence
    /// ("DBT evidence received no image START"). The container's /tmp alone
    /// is not private enough: `--tmp=DIR` binds a shared directory there, as
    /// the robust_futex_owner_death tests do with `--tmp=/tmp`, and those
    /// collided when nextest ran them eight at a time. So `<dir>` is a
    /// directory named for this invocation, made inside the container before
    /// each physical run and removed after it.
    ///
    /// DynamoRIO removes every `DYNAMORIO_` variable from the guest's
    /// environment. The value is the same on every physical run of one
    /// invocation (both runs of a verification), and its length is the same
    /// on every invocation, so the guest's initial stack does not change
    /// between runs.
    fn contained_command(&self, guest: &StdCommand) -> StdCommand {
        let mut command = copy_dbt_command(guest);
        if self.options.uses_container() {
            command.env(DYNAMORIO_CONFIG_DIR_ENV, &self.config_dir);
        }
        command
    }

    fn new_runtime() -> Result<tokio::runtime::Runtime, Error> {
        // RPC and scheduler waits yield asynchronously. The launcher moves
        // blocking child waits and I/O to spawn_blocking after guest creation.
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(|error| Error::msg(format!("failed to start the DBT coordinator: {error}")))
    }

    /// Run `run` with a fresh coordinator runtime and `config` completed for
    /// the container. The guest's environment keeps the configuration
    /// encoded before the container exists: the completed one carries this
    /// run's host mount IDs, and its length would move the guest's stack.
    fn run<F, T>(&self, config: &Config, mut run: F) -> Result<T, Error>
    where
        F: FnMut(&tokio::runtime::Runtime, &Config) -> Result<T, Error> + 'static,
        T: serde::Serialize + serde::de::DeserializeOwned + 'static,
    {
        // Keep the mapping in the ephemeral launch options, not the original
        // guest arguments or config. Common bind provenance presents its target
        // as both mountinfo root and mountpoint, without exposing the host name.
        let options = self
            .options
            .with_dbt_mounts(self.statistics.borrow().as_slice(), Vec::new());
        let config = config.clone();
        let launched = self.launched.clone();
        let launch_workdir = options.launch_workdir().map(std::path::Path::to_path_buf);
        let config_dir = self
            .options
            .uses_container()
            .then(|| self.config_dir.clone());
        options
            .launch_in_container(
                (),
                "the DBT coordinator",
                options.run_timeout(),
                move |_, identity, native| {
                    if native.is_some() {
                        return Err(Error::msg(
                            "native exec container state cannot be used by the DBT coordinator",
                        ));
                    }
                    // The detached stdin reader may stay blocked after the
                    // guest exits.
                    super::container::exit_init_after_publication();
                    // This physical-run path does not initialize tracing. Use
                    // its common namespace baseline exactly once, before any
                    // coordinator task starts, just as the tracing paths do.
                    super::tracing::align_tracing_pid_baseline(hermit::Backend::Dbt);
                    let mut config = super::run::complete_container_config(
                        config.clone(),
                        identity,
                        launch_workdir.as_deref(),
                    )?;
                    if let Some((program, workdir, path)) = &launched {
                        hermit::record_dbt_launched_image(
                            &mut config,
                            program,
                            workdir.as_deref(),
                            path.as_deref(),
                        );
                    }
                    let _config_dir = config_dir
                        .as_deref()
                        .map(DynamoRioConfigDir::create)
                        .transpose()?;
                    let runtime = Self::new_runtime()?;
                    // The operation consumes GlobalState cleanup; runtime Drop
                    // joins its workers before the result is published.
                    run(&runtime, &config)
                },
            )
            .map(|(value, ())| value)
    }
}

/// A run's wait status and output, in the form the container init publishes.
#[cfg(feature = "dbt")]
#[derive(serde::Serialize, serde::Deserialize)]
struct DbtPhysicalOutput {
    status: i32,
    stdout: Vec<u8>,
    stderr: Vec<u8>,
}

#[cfg(feature = "dbt")]
impl From<Output> for DbtPhysicalOutput {
    fn from(output: Output) -> Self {
        Self {
            status: output.status.into_raw(),
            stdout: output.stdout,
            stderr: output.stderr,
        }
    }
}

#[cfg(feature = "dbt")]
impl From<DbtPhysicalOutput> for Output {
    fn from(output: DbtPhysicalOutput) -> Self {
        Self {
            status: std::process::ExitStatus::from_raw(output.status),
            stdout: output.stdout,
            stderr: output.stderr,
        }
    }
}

/// The container init's copy-on-write view of the caller's input. The init
/// exits without unwinding, which closes its descriptors; dropping the reader
/// would free heap the parent still owns.
#[cfg(feature = "dbt")]
struct ChildInput<R>(std::mem::ManuallyDrop<R>);

#[cfg(feature = "dbt")]
impl<R: Read> Read for ChildInput<R> {
    fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
        self.0.read(buffer)
    }
}

/// [`DbtRunEvidence`] with its own descriptor, for the container init.
#[cfg(feature = "dbt")]
struct OwnedDbtRunEvidence {
    file: std::fs::File,
    log_level: DbtEvidenceLogLevel,
    label: &'static str,
}

#[cfg(feature = "dbt")]
impl OwnedDbtRunEvidence {
    fn from_borrowed(evidence: Option<DbtRunEvidence<'_>>) -> Result<Option<Self>, Error> {
        evidence
            .map(|evidence| {
                Ok(Self {
                    file: evidence.file.try_clone()?,
                    log_level: evidence.log_level,
                    label: evidence.label,
                })
            })
            .transpose()
    }

    fn borrowed(&self) -> DbtRunEvidence<'_> {
        DbtRunEvidence {
            file: &self.file,
            log_level: self.log_level,
            label: self.label,
        }
    }
}

/// A copy of `guest`, which `std::process::Command` cannot clone.
/// A DynamoRIO configuration directory under the container's /tmp, named
/// for one Hermit invocation: `/tmp/.hermit-dynamorio-` and 16 hex digits
/// from a randomly keyed hash, so concurrent invocations sharing /tmp never
/// share it and every name has the same length.
#[cfg(feature = "dbt")]
fn dynamorio_config_dir() -> PathBuf {
    use std::hash::BuildHasher;
    let key = std::collections::hash_map::RandomState::new().hash_one((
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |elapsed| elapsed.as_nanos()),
    ));
    PathBuf::from(format!("/tmp/.hermit-dynamorio-{key:016x}"))
}

/// The configuration directory of one physical DBT run, made inside the
/// container before drrun starts and removed, with the `.dynamorio` files
/// drrun leaves in it, when the run ends.
#[cfg(feature = "dbt")]
struct DynamoRioConfigDir(PathBuf);

#[cfg(feature = "dbt")]
impl DynamoRioConfigDir {
    fn create(path: &Path) -> Result<Self, Error> {
        use std::os::unix::fs::DirBuilderExt;
        std::fs::DirBuilder::new()
            .mode(0o700)
            .create(path)
            .map_err(|error| {
                Error::msg(format!(
                    "failed to create the DynamoRIO configuration directory {}: {error}",
                    path.display()
                ))
            })?;
        Ok(Self(path.to_path_buf()))
    }
}

#[cfg(feature = "dbt")]
impl Drop for DynamoRioConfigDir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

#[cfg(feature = "dbt")]
fn copy_dbt_command(guest: &StdCommand) -> StdCommand {
    let mut command = StdCommand::new(guest.get_program());
    command.args(guest.get_args());
    for (name, value) in guest.get_envs() {
        match value {
            Some(value) => {
                command.env(name, value);
            }
            None => {
                command.env_remove(name);
            }
        }
    }
    if let Some(directory) = guest.get_current_dir() {
        command.current_dir(directory);
    }
    command
}

/// Create the abstract Unix evidence listener only after entering this physical
/// run's network namespace. A listener created by the caller would be invisible
/// to the guest. The completed artifact is an ordinary file read by the caller.
#[cfg(feature = "dbt")]
struct DbtRunEvidence<'a> {
    file: &'a std::fs::File,
    log_level: DbtEvidenceLogLevel,
    label: &'static str,
}

#[cfg(feature = "dbt")]
fn configure_dbt_run_evidence(
    runner: &DbtRunner,
    evidence: Option<DbtRunEvidence<'_>>,
) -> Result<DbtRunner, Error> {
    let runner = runner.clone();
    match evidence {
        Some(evidence) => runner
            .evidence_file(evidence.file)
            .map(|runner| runner.evidence_log_level(evidence.log_level))
            .map_err(|error| {
                Error::msg(format!(
                    "failed to configure protected DBT {} evidence: {error}",
                    evidence.label
                ))
            }),
        None => Ok(runner),
    }
}

#[cfg(feature = "dbt")]
fn run_once<R: Read + Send + 'static>(
    execution: &DbtExecution,
    runner: &DbtRunner,
    guest: &StdCommand,
    drrun: &Path,
    config: &Config,
    evidence: Option<DbtRunEvidence<'_>>,
    input: R,
) -> Result<Output, Error> {
    let runner = runner.clone();
    let guest = execution.contained_command(guest);
    let drrun = drrun.to_path_buf();
    let report = execution.unsupported_report.clone();
    let evidence = OwnedDbtRunEvidence::from_borrowed(evidence)?;
    let mut input = Some(input);
    execution
        .run(config, move |runtime, config| {
            let runner = configure_dbt_run_evidence(
                &runner,
                evidence.as_ref().map(OwnedDbtRunEvidence::borrowed),
            )?;
            let guest = prepare_dbt_physical_command(&guest)?;
            let input =
                ChildInput(std::mem::ManuallyDrop::new(input.take().ok_or_else(
                    || Error::msg("DBT physical input already consumed"),
                )?));
            let (output, global) = runtime
                .block_on(
                    runner.output_with_detached_reader_and_global::<detcore::GlobalState, _>(
                        &guest,
                        input,
                        config.clone(),
                    ),
                )
                .map_err(|error| dbt_run_error(&drrun, error))?;
            runtime.block_on(clean_up_dbt_global(
                &output.status,
                global,
                report.as_deref(),
            ))?;
            Ok(DbtPhysicalOutput::from(output))
        })
        .map(Output::from)
}

#[cfg(feature = "dbt")]
fn run_once_with_inherited_input(
    execution: &DbtExecution,
    runner: &DbtRunner,
    guest: &StdCommand,
    drrun: &Path,
    config: &Config,
    evidence: Option<DbtRunEvidence<'_>>,
) -> Result<Output, Error> {
    let runner = runner.clone();
    let guest = execution.contained_command(guest);
    let drrun = drrun.to_path_buf();
    let report = execution.unsupported_report.clone();
    let evidence = OwnedDbtRunEvidence::from_borrowed(evidence)?;
    execution
        .run(config, move |runtime, config| {
            let runner = configure_dbt_run_evidence(
                &runner,
                evidence.as_ref().map(OwnedDbtRunEvidence::borrowed),
            )?;
            let guest = prepare_dbt_physical_command(&guest)?;
            let (output, global) = runtime
                .block_on(
                    runner.output_with_inherited_stdin_and_global::<detcore::GlobalState>(
                        &guest,
                        config.clone(),
                    ),
                )
                .map_err(|error| dbt_run_error(&drrun, error))?;
            runtime.block_on(clean_up_dbt_global(
                &output.status,
                global,
                report.as_deref(),
            ))?;
            Ok(DbtPhysicalOutput::from(output))
        })
        .map(Output::from)
}

#[cfg(feature = "dbt")]
fn run_status(
    execution: &DbtExecution,
    runner: &DbtRunner,
    guest: &StdCommand,
    drrun: &Path,
    config: &Config,
) -> Result<std::process::ExitStatus, Error> {
    let runner = runner.clone();
    let guest = execution.contained_command(guest);
    let drrun = drrun.to_path_buf();
    let report = execution.unsupported_report.clone();
    execution
        .run(config, move |runtime, config| {
            let guest = prepare_dbt_physical_command(&guest)?;
            let (status, global) = runtime
                .block_on(runner.status_with_global::<detcore::GlobalState>(&guest, config.clone()))
                .map_err(|error| dbt_run_error(&drrun, error))?;
            runtime.block_on(clean_up_dbt_global(&status, global, report.as_deref()))?;
            Ok(status.into_raw())
        })
        .map(std::process::ExitStatus::from_raw)
}

#[cfg(feature = "dbt")]
async fn clean_up_dbt_global(
    status: &std::process::ExitStatus,
    mut global: detcore::GlobalState,
    report: Option<&std::fs::File>,
) -> std::io::Result<()> {
    // The runner has drained RPC owners before returning exclusive GlobalState.
    // Forward its aggregate through the existing invocation pipe used by
    // copied-child records, without opening or duplicating a descriptor.
    let exported = if let Some(mut report) = report {
        global
            .take_unsupported_syscalls()
            .into_iter()
            .try_for_each(|name| writeln!(report, "{name}"))
    } else {
        Ok(())
    };
    if !status.success() {
        global.force_shutdown_with_error();
        // The physical supervisor and RPC owner drain have finished. A client
        // that failed before registration cannot start the owned scheduler.
        global.cancel_internal_scheduler().await;
    }
    global.clean_up(false, &None).await;
    // Export failure must never bypass scheduler cleanup.
    exported
}

/// Name the stage that actually failed.
///
/// This used to be `launch_error`, and it announced EVERY `io::Error` from a
/// DBT run as "failed to launch drrun ({path})". But the calls it wraps --
/// `output_with_detached_reader` and `output_with_inherited_stdin` -- run the
/// whole lifecycle: spawn, wait, collect output, and finalize the protected
/// evidence session. A failure in any later stage was reported as a failure of
/// the first one.
///
/// Measured cost of that: a campaign agent hit
/// `"failed to launch drrun (target/install_pkg/rsrcs/dynamorio/bin64/drrun):
/// DBT guest exited with status S..."`, correctly observed that drrun was a
/// real 737 KB ELF matching the build cache, and could not proceed. The inner
/// text is the giveaway -- it comes from `reverie-dbt`'s
/// `(Ok(status), Err(error))` arm, so the guest HAD RUN AND EXITED and the
/// failure was in evidence finalization. A binary that failed to launch cannot
/// produce an exit status. The wrapper had renamed a post-run failure into a
/// missing-binary hunt.
///
/// The discriminator is `ErrorKind`. A real spawn failure surfaces the OS error
/// (`NotFound` when drrun is absent, `PermissionDenied` when it is not
/// executable, `ExecutableFileBusy`). Where the kind does not prove a spawn
/// failure this deliberately does NOT claim one.
///
/// The pinned Reverie revision preserves this distinction: once it has an exit
/// status, an evidence-finalization failure is returned as `Other`, even when
/// the underlying error was `NotFound` or `PermissionDenied`. Spawn failures
/// bypass that combination and retain their original kind. Do not replace this
/// typed boundary with matching on display text.
#[cfg(feature = "dbt")]
fn dbt_run_error(drrun: &Path, error: std::io::Error) -> Error {
    use std::io::ErrorKind;
    // A run that failed after its tree was reaped (protected evidence reporting
    // a backend failure, for one) still carries what it captured. Its stderr
    // usually holds the line that says why, so show it before the error.
    if let Some(output) = reverie_dbt::failed_run_output(&error)
        && !output.stderr.is_empty()
    {
        eprintln!(":: stderr captured from the failed DBT run:");
        let _ = std::io::stderr().write_all(&output.stderr);
    }
    match error.kind() {
        ErrorKind::NotFound | ErrorKind::PermissionDenied | ErrorKind::ExecutableFileBusy => {
            Error::msg(format!(
                "failed to launch drrun ({}): {error}",
                drrun.display()
            ))
        }
        // Typed under the message: `run --verify` records it as a backend
        // failure of the run, not as a run that never started.
        _ => Error::new(hermit::BackendRunFailure).context(format!(
            "drrun started ({}) and the DBT run then failed: {error} \
             -- this is NOT a launch failure; the drrun binary is not implicated. \
             Read the text after the last ':' for the stage that actually failed.",
            drrun.display()
        )),
    }
}

// ⚠️ `test` AS WELL AS `dbt`, AND THE REASON IS THAT THE BRACKETS BELOW MUST RUN.
// `dbt` is not in `default` (hermit-cli/Cargo.toml: `default = []`), so under
// `#[cfg(feature = "dbt")]` alone this function and any test of it compile only in
// a build validation does not perform. Measured on main before this change:
//
//     $ cargo test -p hermit --bin hermit dbt_status
//     running 0 tests
//
// Zero -- so the only check that a signalled death is not reported as a normal exit
// contributed nothing to any receipt. Adding `test` keeps the symbol out of a
// feature-off PRODUCTION build, where `clippy -D warnings` would call the import
// dead, while letting the brackets execute wherever tests do.
#[cfg(any(feature = "dbt", test))]
fn process_status(status: std::process::ExitStatus) -> ExitStatus {
    ExitStatus::from_raw(status.into_raw())
}

#[cfg(feature = "dbt")]
fn write_output(output: &Output) -> Result<(), Error> {
    std::io::stdout().write_all(&output.stdout)?;
    std::io::stderr().write_all(&output.stderr)?;
    Ok(())
}

// `test` as well as `dbt`, matching process_status above. This is the site where
// the signal-losing conversion was MISSED -- `code().unwrap_or(1)` reported every
// signalled death as Exited(1) -- so it is the one that most needs a bracket that
// actually runs, and the bracket for it ran ZERO times on the default build.
#[cfg(any(feature = "dbt", test))]
fn output_status(output: &Output) -> ExitStatus {
    // ⚠️ SAME SIGNAL-LOSING CONVERSION `process_status` ALREADY FIXED, MISSED HERE.
    // `std::process::ExitStatus::code()` is `None` for a process killed by a
    // signal, so `code().unwrap_or(1)` reported every signalled death as a
    // normal `Exited(1)`: a guest killed by SIGSEGV came back as "exited
    // normally with status 1", and WIFSIGNALED/WTERMSIG/WCOREDUMP were all lost.
    // `ExitStatus::from_raw` decodes exited-versus-signalled and the core-dump
    // flag the same way the ptrace backend does.
    ExitStatus::from_raw(output.status.into_raw())
}

#[cfg(feature = "sabre")]
fn sabre_artifact(variable: &str, description: &str, executable: bool) -> Result<OsString, Error> {
    let value = std::env::var_os(variable).ok_or_else(|| {
        Error::msg(format!(
            "the sabre backend needs {variable}=<path-to-{description}>"
        ))
    })?;
    validate_sabre_artifact(Path::new(&value), variable, executable)
}

#[cfg(feature = "sabre")]
fn validate_sabre_artifact(
    requested_path: &Path,
    variable: &str,
    executable: bool,
) -> Result<OsString, Error> {
    let path = fs::canonicalize(requested_path).map_err(|error| {
        Error::msg(format!(
            "the sabre backend cannot access {variable}={}: {error}",
            requested_path.display()
        ))
    })?;
    let metadata = fs::metadata(&path).map_err(|error| {
        Error::msg(format!(
            "the sabre backend cannot inspect {variable}={}: {error}",
            path.display()
        ))
    })?;
    if !metadata.is_file() {
        return Err(Error::msg(format!(
            "the sabre backend needs {variable}={} to be a regular file",
            path.display()
        )));
    }
    if executable && metadata.permissions().mode() & 0o111 == 0 {
        return Err(Error::msg(format!(
            "the sabre backend needs {variable}={} to be executable",
            path.display()
        )));
    }
    Ok(path.into_os_string())
}

#[cfg(feature = "sabre")]
const SABRE_QUIET_ENV: &str = "REVERIE_SABRE_STRACE_QUIET";

#[cfg(feature = "sabre")]
fn sabre_command(
    runner: &OsString,
    sabre: &OsString,
    plugin: &OsString,
    program: &Path,
    args: &[String],
    quiet: bool,
    log: Option<LevelFilter>,
) -> StdCommand {
    let mut command = StdCommand::new(runner);
    command
        .arg("--sabre")
        .arg(sabre)
        .arg("--plugin")
        .arg(plugin)
        .arg("--")
        .arg(program)
        .args(args);
    if quiet {
        command.env(SABRE_QUIET_ENV, "1");
    }
    if let Some(level) = log {
        command.env("HERMIT_LOG", level.to_string());
    }
    command
}

#[cfg(feature = "sabre")]
fn sabre_artifacts() -> Result<(OsString, OsString, OsString), Error> {
    Ok((
        sabre_artifact("HERMIT_SABRE_RUNNER", "reverie-sabre-strace", true)?,
        sabre_artifact("HERMIT_SABRE_BINARY", "sabre", true)?,
        sabre_artifact(
            "HERMIT_SABRE_PLUGIN",
            "libreverie_sabre_strace_plugin.so",
            false,
        )?,
    ))
}

/// Runs program through the shared Reverie strace tool over SaBRe.
///
/// The SaBRe host and plugin live in the coordinated Reverie checkout, so
/// Hermit uses explicit artifact paths rather than taking an unreleased Cargo
/// dependency:
///
/// * HERMIT_SABRE_RUNNER: reverie-sabre-strace executable.
/// * HERMIT_SABRE_BINARY: pinned SaBRe executable.
/// * HERMIT_SABRE_PLUGIN: libreverie_sabre_strace_plugin.so.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#589): Review SaBRe CLI backend dispatch.
#[cfg(feature = "sabre")]
pub fn run_sabre_strace(program: &Path, args: &[String]) -> Result<ExitStatus, Error> {
    let (runner, sabre, plugin) = sabre_artifacts()?;

    eprintln!("hermit: [sabre backend] tracing {program:?} with the shared Reverie tool");

    let status = sabre_command(&runner, &sabre, &plugin, program, args, false, None)
        .status()
        .map_err(|error| {
            Error::msg(format!(
                "failed to launch the SaBRe runner {}: {error}",
                Path::new(&runner).display()
            ))
        })?;

    Ok(status.into())
}

#[cfg(not(feature = "sabre"))]
pub fn run_sabre_strace(_program: &Path, _args: &[String]) -> Result<ExitStatus, Error> {
    hermit::Backend::Sabre.ensure_available()?;
    unreachable!("SaBRe availability must fail when the feature is disabled");
}

#[cfg(test)]
mod tests {
    #[cfg(feature = "dbt")]
    #[test]
    fn each_invocation_names_its_own_dynamorio_config_dir_of_one_length() {
        let first = super::dynamorio_config_dir();
        let second = super::dynamorio_config_dir();
        for dir in [&first, &second] {
            let name = dir.to_str().unwrap();
            let suffix = name.strip_prefix("/tmp/.hermit-dynamorio-").unwrap();
            assert_eq!(suffix.len(), 16, "{name}");
            assert!(
                suffix.bytes().all(|byte| byte.is_ascii_hexdigit()),
                "{name}"
            );
        }
        assert_ne!(first, second);
    }

    #[cfg(feature = "dbt")]
    #[test]
    fn a_dynamorio_config_dir_is_private_and_removed_with_its_files() {
        use std::os::unix::fs::PermissionsExt;
        let parent = tempfile::tempdir().unwrap();
        let path = parent.path().join("config");
        let dir = super::DynamoRioConfigDir::create(&path).unwrap();
        assert_eq!(
            std::fs::metadata(&path).unwrap().permissions().mode() & 0o777,
            0o700
        );
        // A second run cannot take over a directory that already exists.
        assert!(super::DynamoRioConfigDir::create(&path).is_err());
        std::fs::create_dir(path.join(".dynamorio")).unwrap();
        std::fs::write(path.join(".dynamorio/true.2.1config64"), b"-client").unwrap();
        drop(dir);
        assert!(!path.exists());
    }

    #[cfg(not(feature = "sabre"))]
    #[test]
    fn disabled_sabre_strace_refuses_before_resolving_artifacts() {
        let error = super::run_sabre_strace(
            std::path::Path::new("/hermit-missing-disabled-sabre-program"),
            &[],
        )
        .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("the `sabre` feature is not enabled")
        );
    }

    #[cfg(feature = "dbt")]
    #[tokio::test]
    async fn dbt_abnormal_cleanup_ends_an_unregistered_scheduler() {
        use std::os::unix::process::ExitStatusExt;

        use reverie::GlobalTool;

        let config = detcore::Config {
            sequentialize_threads: true,
            ..detcore::Config::default()
        };
        let mut outcomes = Vec::new();
        for (name, raw_status) in [("exit255", 255 << 8), ("SIGKILL", libc::SIGKILL)] {
            let global = detcore::GlobalState::init_global_state(&config).await;
            let status = std::process::ExitStatus::from_raw(raw_status);
            let completed = tokio::time::timeout(
                std::time::Duration::from_millis(100),
                super::clean_up_dbt_global(&status, global, None),
            )
            .await
            .is_ok();
            assert_eq!(status.into_raw(), raw_status);
            outcomes.push((name, completed));
        }
        assert_eq!(
            outcomes,
            [("exit255", true), ("SIGKILL", true)],
            "DBT cleanup waited for a scheduler whose physical guest had already failed"
        );
    }

    #[cfg(feature = "dbt")]
    #[tokio::test]
    async fn dbt_successful_cleanup_preserves_normal_scheduler_completion() {
        use std::os::unix::process::ExitStatusExt;

        use reverie::GlobalTool;

        let status = std::process::ExitStatus::from_raw(0);
        let config = detcore::Config {
            sequentialize_threads: false,
            ..detcore::Config::default()
        };
        let global = detcore::GlobalState::init_global_state(&config).await;
        assert!(
            tokio::time::timeout(
                std::time::Duration::from_millis(100),
                super::clean_up_dbt_global(&status, global, None),
            )
            .await
            .is_ok(),
            "cleanup without an owned scheduler did not complete"
        );
        let config = detcore::Config {
            sequentialize_threads: true,
            ..detcore::Config::default()
        };
        let global = detcore::GlobalState::init_global_state(&config).await;
        assert!(
            tokio::time::timeout(
                std::time::Duration::from_millis(100),
                super::clean_up_dbt_global(&status, global, None),
            )
            .await
            .is_err(),
            "successful DBT cleanup silently cancelled a scheduler that had not completed"
        );
        assert!(status.success());
    }
    #[cfg(feature = "dbt")]
    #[test]
    fn dbt_workdir_reaches_the_dynamorio_guest_command() {
        let mut command = std::process::Command::new("/bin/true");
        super::apply_dbt_workdir(&mut command, Some(std::path::Path::new("/test")));
        assert_eq!(
            command.get_current_dir(),
            Some(std::path::Path::new("/test"))
        );
    }

    #[cfg(feature = "dbt")]
    /// A behavioural pin, not a text match: require every record of the
    /// published stream, initialization records included, to reach the
    /// comparison log unchanged and in order. The guest records are passed
    /// separately because only they are counted by the refusal guards.
    #[test]
    fn materialization_keeps_every_decoded_comparable_record() {
        let directory = tempfile::tempdir().expect("tempdir");
        let path = directory.path().join("evidence.log");
        let file = std::fs::File::create(&path).expect("create log");
        let records: Vec<Vec<u8>> = vec![
            b"1970-01-01T00:00:00.000000Z  INFO detcore: DETLOG first\n".to_vec(),
            b"1970-01-01T00:00:00.000000Z  INFO detcore: DETLOG second\n".to_vec(),
        ];
        let published = [
            DBT_INITIALIZATION_RECORD,
            records[0].as_slice(),
            records[1].as_slice(),
        ];

        super::materialize_dbt_comparison_log(&records, &published, file, &path)
            .expect("materialization must accept comparable records");

        assert_eq!(std::fs::read(&path).expect("read back"), published.concat());
    }

    /// Initialization records decide the terminal outcome in two ways: their
    /// typed count is compared as a separate dimension, and under
    /// `all_records_v1` their positions are compared with the guest records.
    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_initialization_record_counts_control_the_terminal_outcome() {
        use super::super::record_envelope::RecordEnvelopePolicy;
        use super::super::verify::ComparedLogCounts;
        use super::super::verify::verification_report;

        fn dbt_verdict(directory: &Path, left: &[&[u8]], right: &[&[u8]]) -> VerificationOutcome {
            let output = reverie::process::Output {
                status: ExitStatus::Exited(0),
                stdout: Vec::new(),
                stderr: Vec::new(),
            };
            let log = |records: &[&[u8]]| {
                let (file, path) = tempfile::NamedTempFile::new_in(directory)
                    .unwrap()
                    .into_parts();
                materialize_dbt_evidence_log(&decoded_dbt_evidence(records), file, &path).unwrap();
                path
            };
            compare_two_runs(
                ComparedRun {
                    output: &output,
                    log: log(left),
                    label: "run 1",
                },
                ComparedRun {
                    output: &output,
                    log: log(right),
                    label: "run 2",
                },
                ComparisonOptions {
                    verbose: false,
                    strictness: LogCompareStrictness::Canonical,
                    compare_logs: true,
                    diagnostic_full_trace: false,
                    compare_io_buffers: true,
                    keep_logs: false,
                    match_overridden: false,
                    failed_log_retention: None,
                    record_envelope: RecordEnvelope::all_records_v1(),
                    virtualize_time: true,
                    caller_owns_logs: false,
                },
            )
            .unwrap()
        }

        let stats = dbt_stats(10, 20, 5, 2, 99);
        let equal = DbtSummaryComparison::compare(&stats, &stats, 2, 2);
        assert!(!equal.requires_log_retention());
        let mut matched = typed_outcome(Verdict::Matched, 0);
        assert!(equal.apply(&mut matched).is_none());
        assert_eq!(matched.verdict, Verdict::Matched);

        let unequal = DbtSummaryComparison::compare(&stats, &stats, 2, 3);
        assert!(
            unequal.requires_log_retention(),
            "an initialization mismatch must retain both evidence logs"
        );
        let mut outcome = typed_outcome(Verdict::Matched, 0);
        let failure = unequal.apply(&mut outcome).unwrap();
        assert_eq!(outcome.verdict, Verdict::Diverged);
        assert!(!outcome.verified());
        assert!(
            !outcome
                .into_exit_status()
                .expect("divergence has a terminal status")
                .success()
        );
        assert!(
            failure.contains("initialization counts differed"),
            "{failure}"
        );
        assert!(failure.contains("2 != 3"), "{failure}");

        // End to end through the verdict: logs materialized by the production
        // path are compared with the options the DBT adapter uses. The
        // comparison counts five INFO records per side, where `records()` alone
        // holds three, and the report names `all_records_v1`. Moving the child's
        // initialization record away from the position the collector gives it,
        // immediately before the child's first record, diverges the verdict
        // although the guest records and the initialization counts are
        // unchanged.
        let parent_first: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 1\n";
        let parent_second: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 2\n";
        let child: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: child record 1\n";
        let canonical = [
            DBT_INITIALIZATION_RECORD,
            parent_first,
            parent_second,
            DBT_INITIALIZATION_RECORD,
            child,
        ];
        let moved = [
            DBT_INITIALIZATION_RECORD,
            parent_first,
            DBT_INITIALIZATION_RECORD,
            parent_second,
            child,
        ];
        // A diverged comparison keeps its logs, so they must live in a
        // directory this test removes.
        let directory = tempfile::tempdir().unwrap();

        let matched = dbt_verdict(directory.path(), &canonical, &canonical);
        assert_eq!(matched.verdict, Verdict::Matched);
        assert_eq!(
            matched.compared_log_messages,
            Some(ComparedLogCounts { left: 5, right: 5 }),
            "the initialization records must be compared with the guest records"
        );
        assert_eq!(
            matched.comparison.record_envelope,
            RecordEnvelopePolicy::AllRecordsV1
        );
        let report = serde_json::to_value(verification_report(&matched)).unwrap();
        assert_eq!(report["comparison"]["record_envelope"], "all_records_v1");
        assert_eq!(report["bitwise_parity"], true);

        let diverged = dbt_verdict(directory.path(), &canonical, &moved);
        assert_eq!(diverged.verdict, Verdict::Diverged);
        assert_eq!(
            diverged.compared_log_messages,
            Some(ComparedLogCounts { left: 5, right: 5 })
        );
    }
    use super::*;

    #[cfg(feature = "dbt")]
    fn write_executable(path: &Path, contents: &[u8]) {
        fs::write(path, contents).unwrap();
        let mut permissions = fs::metadata(path).unwrap().permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(path, permissions).unwrap();
    }

    #[cfg(feature = "dbt")]
    fn dbt_stats(
        branches: u64,
        syscalls: u64,
        rewritten: u64,
        stdin_reads: u64,
        memory_hash: u64,
    ) -> DbtBackendStatsSnapshot {
        use reverie_dbt::backend_stats::DbtBackendStatsAggregator;
        use reverie_dbt::backend_stats::DbtProcessRecord;

        let record = DbtProcessRecord {
            branches,
            syscalls,
            rewritten,
            stdin_reads,
            memory_hash,
            ..Default::default()
        };
        let mut aggregator = DbtBackendStatsAggregator::new();
        aggregator.record(&record);
        aggregator.snapshot()
    }

    #[cfg(feature = "dbt")]
    fn typed_outcome(verdict: Verdict, guest_exit: i32) -> VerificationOutcome {
        use super::super::verify::ComparedLogCounts;
        use super::super::verify::ComparisonSpec;

        VerificationOutcome {
            verdict,
            no_result_reason: (verdict == Verdict::NoResult).then(|| {
                super::super::verify::NoResultReason::ComparisonRefused {
                    detail: "fixture comparator refusal".into(),
                }
            }),
            refused_log_comparison: (verdict == Verdict::NoResult)
                .then(|| "fixture comparator refusal".into()),
            guest_status: ExitStatus::Exited(guest_exit),
            comparison: ComparisonSpec::new(
                LogCompareStrictness::Canonical,
                true,
                false,
                true,
                RecordEnvelope::all_records_v1().policy(),
                true,
            ),
            compared_log_messages: Some(ComparedLogCounts { left: 4, right: 4 }),
            compared_outputs: {
                let output = hermit::canonical_verdict::ComparedOutput {
                    exit_code: Some(guest_exit),
                    signal: None,
                    stdout_sha256: "a".repeat(64),
                    stdout_bytes: 0,
                    stderr_sha256: "b".repeat(64),
                    stderr_bytes: 0,
                };
                hermit::canonical_verdict::ComparedOutputs {
                    left: output.clone(),
                    right: output,
                }
            },
            dbt_counted_branches: None,
            runtime: None,
            first_divergent_scheduler_turn: None,
            first_divergent_record_positions: None,
            first_divergent_virtual_nanoseconds: None,
            first_divergent_record: None,
            first_divergent_syscall: None,
            first_divergent_left_message: None,
            first_divergent_right_message: None,
        }
    }

    /// The common owner must consume DBT facts before it publishes a verdict,
    /// while a rejected run must never reach run 2 or a stats-error override.
    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_verification_run_path_binds_terminal_verdict_to_branch_stats() {
        let common = include_str!("run.rs")
            .split_once("    fn verify(")
            .unwrap()
            .1
            .split_once("    /// The `--bind`s")
            .unwrap()
            .0;
        let physical = include_str!("backends.rs")
            .split_once("impl DbtVerification {")
            .unwrap()
            .1
            .split_once("/// Stats are consumed")
            .unwrap()
            .0;
        assert!(!physical.contains("if verify_json.is_none()"));
        assert!(
            physical.find("decode_dbt_evidence").unwrap()
                < physical.find("materialize_dbt_evidence_log").unwrap()
        );
        assert!(
            physical.find("materialize_dbt_evidence_log").unwrap()
                < physical
                    .find("Ok(super::run::VerificationRunOutput")
                    .unwrap()
        );
        let at = |needle| {
            common
                .find(needle)
                .unwrap_or_else(|| panic!("missing common owner boundary: {needle}"))
        };
        let run1 = at("&run1_options,");
        let signal1 = at("signal_reports.prepare(\"run 1\"");
        let latch1 = at("latches1.refuse_comparison");
        let rejected = at("if let Some(error) = reject_first_run(");
        let stats1 = at("facts.finish(&out1, VerificationRun::Run1)");
        let run2 = at("&run2_options,");
        let signal2 = at("signal_reports.prepare(\"run 2\"");
        let latch2 = at("latches2.refuse_comparison");
        let stats2 = at("facts.finish(&out2, VerificationRun::Run2)");
        let comparison = at("DbtVerificationComparison::new(first, second)");
        let conclude = at("let outcome = conclude_verification(");
        let exit = at("return outcome.into_exit_status()");
        assert!(
            run1 < signal1
                && signal1 < latch1
                && latch1 < rejected
                && rejected < stats1
                && stats1 < run2
        );
        assert!(
            run2 < signal2
                && signal2 < latch2
                && latch2 < stats2
                && stats2 < comparison
                && comparison < conclude
                && conclude < exit
        );
        let conclusion = include_str!("run.rs")
            .split_once("pub(super) fn conclude_verification(")
            .unwrap()
            .1;
        let boundary = |needle| conclusion.find(needle).unwrap();
        let retention = boundary("comparison.configure(&mut comparison_options)");
        let compare = boundary("let mut outcome = compare_two_runs(");
        let apply = boundary("comparison.apply(&mut outcome)");
        let publish = boundary("write_verification_json(path, &outcome)");
        let announce = boundary("if let Some(messages) = dbt_messages");
        assert!(retention < compare && compare < apply && apply < publish && publish < announce);
        let offline = include_str!("backends.rs")
            .split_once("pub(super) fn conclude_dbt_verification(")
            .unwrap()
            .1
            .split_once("/// Where DynamoRIO")
            .unwrap()
            .0;
        assert!(
            offline.find("comparison.configure(&mut options)").unwrap()
                < offline.find("compare_two_runs(").unwrap()
        );
        assert!(
            offline.find("comparison.apply(&mut outcome)").unwrap()
                < offline
                    .find("write_verification_json(path, &outcome)")
                    .unwrap()
        );
        assert!(offline.contains("caller_owns_logs,"));
        assert!(exit < at("signal_reports.verified()"));
        let rejection = &common[rejected..stats1];
        assert!(rejection.contains("drop(signal_reports)"));
        let reject = include_str!("run.rs")
            .split_once("pub(super) fn reject_first_run(")
            .unwrap()
            .1
            .split_once("pub(super) fn verify_run_outcome_of(")
            .unwrap()
            .0;
        assert!(reject.contains("first_run_rejected_report("));
        assert!(reject.contains("write_report_json(path, &report)"));
        for (begin, end) in [(stats1, run2), (stats2, comparison)] {
            let refusal = &common[begin..end];
            assert!(refusal.contains("publish_run_failure("));
            assert!(refusal.contains("retain_logs_after_verification_error("));
            assert!(refusal.contains("return Err(error)"));
        }
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn equal_branch_clocks_do_not_authorize_a_no_result() {
        let outcome = typed_outcome(Verdict::NoResult, 23);
        let comparison = DbtCountedBranchComparison {
            left: 563_145,
            right: 563_145,
        };
        let verdict_file = tempfile::NamedTempFile::new().unwrap();

        let mut outcome = outcome;
        let failure = record_dbt_branch_clock_comparison(&mut outcome, comparison);
        write_verification_json(verdict_file.path(), &outcome).unwrap();
        let json: serde_json::Value =
            serde_json::from_slice(&fs::read(verdict_file.path()).unwrap()).unwrap();

        assert!(failure.is_none());
        assert_eq!(outcome.verdict, Verdict::NoResult);
        assert!(outcome.dbt_counted_branches.is_none());
        assert_eq!(json["verdict"], "no_result");
        assert_eq!(json["verified"], false);
        assert!(json.get("dbt_counted_branches").is_none());
        let error = outcome.into_exit_status().unwrap_err().to_string();
        assert!(error.contains("did not reach a verdict"), "{error}");
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn branch_clock_divergence_is_retained_in_the_serialized_typed_verdict() {
        use super::super::verify::ComparedLogCounts;

        for initial_verdict in [Verdict::Matched, Verdict::NoResult] {
            let outcome = typed_outcome(initial_verdict, 0);
            let comparison = DbtCountedBranchComparison {
                left: 563_145,
                right: 563_103,
            };
            let verdict_file = tempfile::NamedTempFile::new().unwrap();
            let mut outcome = outcome;
            let message = record_dbt_branch_clock_comparison(&mut outcome, comparison);
            write_verification_json(verdict_file.path(), &outcome).unwrap();
            let message = message.unwrap();
            let json: serde_json::Value =
                serde_json::from_slice(&fs::read(verdict_file.path()).unwrap()).unwrap();

            assert!(message.contains("563145 != 563103"), "{message}");
            assert_eq!(outcome.verdict, Verdict::Diverged);
            assert_eq!(outcome.dbt_counted_branches, Some(comparison));
            assert_eq!(json["verdict"], "diverged");
            assert_eq!(json["verified"], false);
            assert_eq!(json["bitwise_parity"], false);
            assert_eq!(json["dbt_counted_branches"]["left"], 563_145);
            assert_eq!(json["dbt_counted_branches"]["right"], 563_103);
            assert_eq!(
                outcome.compared_log_messages,
                Some(ComparedLogCounts { left: 4, right: 4 })
            );
            assert_eq!(
                outcome.into_exit_status().unwrap(),
                ExitStatus::Exited(hermit::HERMIT_VERIFICATION_DIVERGENCE_EXIT)
            );
        }
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn equal_branch_clocks_preserve_match_and_nonzero_guest_status() {
        let outcome = typed_outcome(Verdict::Matched, 23);
        let comparison = DbtCountedBranchComparison {
            left: 563_145,
            right: 563_145,
        };

        let mut outcome = outcome;
        let failure = record_dbt_branch_clock_comparison(&mut outcome, comparison);
        assert!(failure.is_none());
        let report = super::super::verify::verification_report(&outcome);

        assert_eq!(outcome.verdict, Verdict::Matched);
        assert_eq!(report.dbt_counted_branches, Some(comparison));
        assert!(report.verified);
        assert!(report.bitwise_parity);
        assert_eq!(outcome.into_exit_status().unwrap(), ExitStatus::Exited(23));
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_facts_preserve_canonical_comparison_and_force_mismatch_retention() {
        let first = DbtRunFacts {
            stats: dbt_stats(100, 7, 7, 0, 42),
            initialization_records: 2,
        };
        for (branches, images, mismatch, caller_owns_logs, initial_override) in [
            (100, 2, false, false, false),
            (101, 2, true, false, false),
            (100, 3, true, false, false),
            (100, 2, false, true, false),
            (101, 2, true, true, false),
            (100, 3, true, true, false),
            (100, 2, false, false, true),
            (101, 2, true, false, true),
            (100, 3, true, false, true),
            (100, 2, false, true, true),
            (101, 2, true, true, true),
            (100, 3, true, true, true),
        ] {
            let second = DbtRunFacts {
                stats: dbt_stats(branches, 7, 7, 0, 42),
                initialization_records: images,
            };
            let comparison = DbtVerificationComparison::new(&first, &second);
            let mut options = ComparisonOptions {
                verbose: false,
                strictness: LogCompareStrictness::Stripped,
                compare_logs: true,
                diagnostic_full_trace: false,
                compare_io_buffers: true,
                keep_logs: false,
                match_overridden: initial_override,
                failed_log_retention: None,
                record_envelope: RecordEnvelope::all_records_v1(),
                virtualize_time: true,
                caller_owns_logs,
            };
            comparison.configure(&mut options);
            assert_eq!(options.caller_owns_logs, caller_owns_logs);
            assert_eq!(options.strictness, LogCompareStrictness::Canonical);
            assert!(options.compare_logs && options.compare_io_buffers && options.virtualize_time);
            assert_eq!(options.keep_logs, mismatch);
            assert_eq!(options.match_overridden, initial_override || mismatch);
            let mut outcome = typed_outcome(Verdict::Matched, 23);
            let failures = comparison.apply(&mut outcome);
            assert_eq!(!failures.is_empty(), mismatch);
            assert_eq!(outcome.verified(), !mismatch);
            assert_eq!(outcome.guest_status, ExitStatus::Exited(23));
        }
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn missing_dbt_completion_stats_are_attributed_to_the_completed_run() {
        let output = reverie::process::Output {
            status: ExitStatus::Exited(0),
            stdout: Vec::new(),
            stderr: Vec::new(),
        };
        for (run, expected) in [
            (VerificationRun::Run1, "run1"),
            (VerificationRun::Run2, "run2"),
        ] {
            let completion = DbtRunCompletion {
                stats: DbtStatsSink::new().unwrap(),
                initialization_records: 1,
                terminal_stdin: false,
            };
            let error = completion
                .finish(&output, run)
                .err()
                .expect("missing stats must refuse");
            assert!(error.downcast_ref::<hermit::BackendRunFailure>().is_some());
            let saved =
                serde_json::to_value(super::super::run::verify_run_outcome_of(&error)).unwrap();
            assert_eq!(saved["kind"], "run_failed");
            assert_eq!(saved["failure"], "backend_failure");
            assert!(
                saved["detail"]
                    .as_str()
                    .unwrap()
                    .contains("typed whole-process statistics were missing")
            );
            let report = tempfile::NamedTempFile::new().unwrap();
            let error = super::super::verify::publish_run_failure(Some(report.path()), run, error);
            assert!(format!("{error:#}").contains("typed whole-process statistics were missing"));
            let report: serde_json::Value =
                serde_json::from_slice(&fs::read(report.path()).unwrap()).unwrap();
            assert_eq!(report["verdict"], "no_result");
            assert_eq!(report["no_result_reason"]["kind"], "run_failed");
            assert_eq!(report["no_result_reason"]["run"], expected);
            assert_eq!(report["no_result_reason"]["failure"], "backend_failure");
        }
    }

    #[cfg(feature = "dbt")]
    fn common_dbt_verify_options(
        directory: &Path,
        allow: &str,
    ) -> (super::super::run::RunOpts, PathBuf) {
        use clap::Parser;

        let report = directory.join("verdict.json");
        let logs = directory.join("logs");
        let allow = format!("--verify-allow={allow}");
        let mut options = super::super::run::RunOpts::parse_from([
            "run",
            "--verify",
            &allow,
            "--keep-logs",
            "--verify-log-dir",
            logs.to_str().unwrap(),
            "--verify-json",
            report.to_str().unwrap(),
            "--",
            "/bin/true",
        ]);
        options.set_backend(Some(hermit::Backend::Dbt));
        super::super::verify::write_pending_verification_json(&report).unwrap();
        (options, report)
    }

    #[cfg(feature = "dbt")]
    #[test]
    fn dbt_stats_mapping_avoids_declared_targets_and_replaces_each_run() {
        let directory = tempfile::tempdir().unwrap();
        let (mut options, _) = common_dbt_verify_options(directory.path(), "success");
        for target in [
            "/tmp/.hermit-dbt-stats-0/child",
            "/tmp/.hermit-dbt-stats-1",
            "/tmp/.hermit-dbt-stats-2-other",
            "/ignored/.hermit-dbt-stats-2",
        ] {
            options.bind.push(Bind::new(directory.path(), target));
        }
        let original = format!("{options:?}");
        let execution = DbtExecution::new(&options, false);
        let runner = DbtRunner::new("/bin/true", "/bin/true").unwrap();
        let first = execution.stats_capture().unwrap();
        let configured = execution.configure_stats(&first, runner.clone()).unwrap();
        let target = Path::new("/tmp/.hermit-dbt-stats-2");
        assert_eq!(
            *execution.statistics.borrow(),
            Some(Bind::new(first.directory(), target))
        );
        assert_eq!(
            format!("{configured:?}"),
            format!("{:?}", first.configure_at(runner.clone(), target))
        );
        // The prior sink can still exist; slot choice and replacement depend
        // on declarations, not which random backing directories remain alive.
        let second = execution.stats_capture().unwrap();
        assert!(execution.statistics.borrow().is_none());
        assert_ne!(first.directory(), second.directory());
        execution.configure_stats(&second, runner).unwrap();
        assert_eq!(
            *execution.statistics.borrow(),
            Some(Bind::new(second.directory(), target))
        );
        assert_eq!(format!("{options:?}"), original);
        assert_eq!(format!("{:?}", execution.options), original);
    }

    #[cfg(feature = "dbt")]
    #[test]
    fn dbt_stats_without_namespace_keep_the_host_address_and_no_bind() {
        use clap::Parser;

        let options =
            super::super::run::RunOpts::parse_from(["run", "--no-namespace", "--", "/bin/true"]);
        let execution = DbtExecution::new(&options, false);
        let capture = execution.stats_capture().unwrap();
        let runner = DbtRunner::new("/bin/true", "/bin/true").unwrap();
        let expected = capture.configure(runner.clone());
        let actual = execution.configure_stats(&capture, runner).unwrap();
        assert_eq!(format!("{actual:?}"), format!("{expected:?}"));
        assert!(execution.statistics.borrow().is_none());
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn common_verifier_checks_first_run_disposition_before_dbt_stats() {
        for (case, status, loss) in [
            ("latch", 0, Some("injected run-1 loss")),
            ("rejected", 23, None),
            ("stats", 0, None),
        ] {
            let directory = tempfile::tempdir().unwrap();
            let (options, report_path) = common_dbt_verify_options(directory.path(), "success");
            let mut calls = Vec::new();
            let mut log_path = None;
            let error = options
                .verify_with_execution(|_, mut log, path, forwarded, run| {
                    calls.push(run);
                    assert_eq!(run, VerificationRun::Run1, "{case}: run 2 must not start");
                    assert!(forwarded.is_none());
                    log.write_all(b"retained first-run evidence\n")?;
                    log_path = Some(path.to_path_buf());
                    Ok(super::super::run::VerificationRunOutput {
                        output: reverie::process::Output {
                            status: ExitStatus::Exited(status),
                            stdout: Vec::new(),
                            stderr: Vec::new(),
                        },
                        skid_overshoots: 0,
                        latches: super::super::run::RunLatches {
                            determinism_loss: loss.map(str::to_owned),
                            log_sink_failure: None,
                        },
                        dbt: Some(DbtRunCompletion {
                            stats: DbtStatsSink::new()?,
                            initialization_records: 1,
                            terminal_stdin: false,
                        }),
                    })
                })
                .unwrap_err();
            assert_eq!(calls, [VerificationRun::Run1]);
            assert_eq!(
                fs::read(log_path.unwrap()).unwrap(),
                b"retained first-run evidence\n"
            );
            let report: serde_json::Value =
                serde_json::from_slice(&fs::read(report_path).unwrap()).unwrap();
            assert_eq!(report["verdict"], "no_result");
            assert_eq!(report["verified"], false);
            let reason = &report["no_result_reason"];
            let detail = format!("{error:#}");
            match case {
                "latch" => {
                    assert_eq!(reason["failure"], "determinism_loss");
                    assert_eq!(reason["run"], "run1");
                    assert!(detail.contains("injected run-1 loss"), "{detail}");
                    assert!(!detail.contains("statistics were missing"), "{detail}");
                }
                "rejected" => {
                    assert_eq!(reason["kind"], "first_run_rejected");
                    assert_eq!(report["guest_exit_code"], 23);
                    assert!(detail.contains("First run during --verify"), "{detail}");
                    assert!(!detail.contains("statistics were missing"), "{detail}");
                }
                "stats" => {
                    assert_eq!(reason["kind"], "run_failed");
                    assert_eq!(reason["failure"], "backend_failure");
                    assert_eq!(reason["run"], "run1");
                    assert!(detail.contains("statistics were missing"), "{detail}");
                }
                _ => unreachable!(),
            }
        }
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn common_verifier_retains_run1_when_second_dbt_completion_fails() {
        let directory = tempfile::tempdir().unwrap();
        let (options, report_path) = common_dbt_verify_options(directory.path(), "both");
        let mut runs = Vec::new();
        let error = options
            .verify_with_execution(|_, mut log, path, forwarded, run| {
                assert!(forwarded.is_none());
                let bytes = format!("completed {run:?}: guest exit 23\n");
                log.write_all(bytes.as_bytes())?;
                runs.push((run, path.to_path_buf(), bytes));
                let stats = DbtStatsSink::new()?;
                if run == VerificationRun::Run1 {
                    let record = reverie_dbt::backend_stats::DbtProcessRecord {
                        branches: 1,
                        syscalls: 1,
                        rewritten: 1,
                        ..Default::default()
                    };
                    fs::write(
                        stats.directory().join("records.bin"),
                        reverie_dbt::backend_stats::encode_process_record(&record),
                    )?;
                }
                Ok(super::super::run::VerificationRunOutput {
                    output: reverie::process::Output {
                        status: ExitStatus::Exited(23),
                        stdout: Vec::new(),
                        stderr: Vec::new(),
                    },
                    skid_overshoots: 0,
                    latches: super::super::run::RunLatches::default(),
                    dbt: Some(DbtRunCompletion {
                        stats,
                        initialization_records: 1,
                        terminal_stdin: false,
                    }),
                })
            })
            .unwrap_err();
        assert_eq!(
            runs.iter().map(|(run, _, _)| *run).collect::<Vec<_>>(),
            [VerificationRun::Run1, VerificationRun::Run2]
        );
        for (_, path, bytes) in runs {
            assert_eq!(fs::read_to_string(path).unwrap(), bytes);
        }
        assert!(format!("{error:#}").contains("statistics were missing"));
        let report: serde_json::Value =
            serde_json::from_slice(&fs::read(report_path).unwrap()).unwrap();
        assert_eq!(report["verdict"], "no_result");
        assert_eq!(report["no_result_reason"]["kind"], "run_failed");
        assert_eq!(report["no_result_reason"]["failure"], "backend_failure");
        assert_eq!(report["no_result_reason"]["run"], "run2");
        // The existing failure schema does not invent a guest status or
        // comparison from incomplete facts. Both real per-run logs survive.
        assert!(report["guest_exit_code"].is_null());
        assert!(report["comparison"].is_null());
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn typed_stats_capture_aggregates_the_whole_process_tree() {
        use reverie_dbt::backend_stats::DbtProcessRecord;
        use reverie_dbt::backend_stats::encode_process_record;

        use super::super::verify_run;

        let capture = DbtStatsSink::new().unwrap();
        let root = DbtProcessRecord {
            branches: 400,
            syscalls: 7,
            memory_hash: 0xff00_0a0d_001a_0000,
            ..Default::default()
        };
        let child = DbtProcessRecord {
            branches: 23,
            syscalls: 2,
            ..Default::default()
        };
        let bytes = [encode_process_record(&root), encode_process_record(&child)].concat();
        fs::write(capture.directory().join("records.bin"), &bytes).unwrap();

        let captured = capture.capture();
        let snapshot = require_dbt_stats(&captured).unwrap();

        assert_eq!(snapshot.process_images(), 2);
        assert_eq!(snapshot.counted_branches(), 423);
        assert_eq!(snapshot.intercepted_syscalls(), 9);
        // The actual saved-run writer must preserve unequal binary records,
        // not regenerate an aggregate or reorder images. Offline strict policy
        // reads that same artifact through the shared capture/codec owner.
        let directory = tempfile::tempdir().unwrap();
        let writer = verify_run::VerifyRunWriter::create(&directory.path().join("saved")).unwrap();
        writer
            .write(verify_run::DBT_STATS, captured.records().unwrap())
            .unwrap();
        let saved = writer.path(verify_run::DBT_STATS);
        assert_eq!(fs::read(&saved).unwrap(), bytes);
        assert_eq!(
            require_dbt_stats(&DbtStatsCapture::read(&saved)).unwrap(),
            snapshot
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_engagement_record_follows_the_typed_branch_count() {
        fn record(branches: u64) -> serde_json::Value {
            let capture = DbtStatsSink::new().unwrap();
            let row = reverie_dbt::backend_stats::DbtProcessRecord {
                branches,
                syscalls: 1,
                rewritten: 1,
                ..Default::default()
            };
            fs::write(
                capture.directory().join("records.bin"),
                reverie_dbt::backend_stats::encode_process_record(&row),
            )
            .unwrap();
            let output = tempfile::NamedTempFile::new().unwrap();
            finish_single_run_dbt_stats(&capture.capture(), false, Some(output.path())).unwrap();
            serde_json::from_slice(&fs::read(output.path()).unwrap()).unwrap()
        }

        let first = record(18);
        let second = record(19);
        assert_eq!(first["engagement"]["backend"], "dbt");
        assert_eq!(first["engagement"]["counted_branches"], 18);
        assert_eq!(second["engagement"]["counted_branches"], 19);
        assert_ne!(
            first, second,
            "mutating the producer count must move the record"
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn terminal_failure_records_do_not_establish_execution_completeness() {
        use reverie_dbt::backend_stats::DbtProcessRecord;
        use reverie_dbt::backend_stats::encode_process_record;

        let capture = DbtStatsCapture::Records(
            encode_process_record(&DbtProcessRecord {
                backend_failure: true,
                process_identity: 7,
                parent_identity: 3,
                ..Default::default()
            })
            .to_vec(),
        );
        let killed = ExitStatus::Signaled(reverie::Signal::SIGKILL, false);
        assert_eq!(
            fail_on_dbt_backend_failure(killed, Some(&capture)),
            ExitStatus::Exited(101)
        );
        assert!(require_dbt_stats(&capture).is_err());
        assert!(
            finish_single_run_dbt_stats(&capture, false, None)
                .unwrap()
                .is_none()
        );
        let output = tempfile::NamedTempFile::new().unwrap();
        assert!(finish_single_run_dbt_stats(&capture, false, Some(output.path())).is_err());
        assert_eq!(output.as_file().metadata().unwrap().len(), 0);
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn terminal_failure_status_requires_a_complete_flagged_record() {
        use reverie_dbt::backend_stats::DbtProcessRecord;
        use reverie_dbt::backend_stats::encode_process_record;

        let flagged = encode_process_record(&DbtProcessRecord {
            backend_failure: true,
            ..Default::default()
        });
        let mut bad_magic = flagged.to_vec();
        bad_magic[0] ^= 1;
        let malformed_tail = [flagged.as_slice(), &flagged[..13]].concat();
        for capture in [
            DbtStatsCapture::Missing,
            DbtStatsCapture::Unreadable {
                message: "unreadable".into(),
                raw_os_error: None,
            },
            DbtStatsCapture::Records(Vec::new()),
            DbtStatsCapture::Records(bad_magic),
            DbtStatsCapture::Records(malformed_tail),
            DbtStatsCapture::Records(encode_process_record(&DbtProcessRecord::default()).to_vec()),
        ] {
            for status in [
                ExitStatus::Signaled(reverie::Signal::SIGKILL, false),
                ExitStatus::Exited(0),
                ExitStatus::Exited(101),
            ] {
                assert_eq!(fail_on_dbt_backend_failure(status, Some(&capture)), status);
            }
        }
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn typed_stats_capture_refuses_missing_empty_or_truncated_evidence() {
        let missing = DbtStatsSink::new().unwrap();
        let error = require_dbt_stats(&missing.capture()).unwrap_err();
        assert!(
            error.to_string().contains("statistics were missing"),
            "{error}"
        );

        let empty = DbtStatsSink::new().unwrap();
        fs::write(empty.directory().join("records.bin"), b"").unwrap();
        let error = require_dbt_stats(&empty.capture()).unwrap_err();
        assert!(
            error.to_string().contains("statistics were empty"),
            "{error}"
        );

        let truncated = DbtStatsSink::new().unwrap();
        fs::write(truncated.directory().join("records.bin"), b"truncated").unwrap();
        let error = require_dbt_stats(&truncated.capture()).unwrap_err();
        assert!(
            error.to_string().contains("statistics were unreadable"),
            "{error}"
        );
        assert!(error.to_string().contains("truncated"), "{error}");

        let inconsistent = DbtStatsSink::new().unwrap();
        let record = reverie_dbt::backend_stats::DbtProcessRecord {
            branches: 18,
            syscalls: 1,
            rewritten: 2,
            ..Default::default()
        };
        fs::write(
            inconsistent.directory().join("records.bin"),
            reverie_dbt::backend_stats::encode_process_record(&record),
        )
        .unwrap();
        let error = require_dbt_stats(&inconsistent.capture()).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("typed native callback counters are inconsistent"),
            "{error}"
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn typed_stats_failure_leaves_the_pre_stamped_no_result() {
        let verdict_file = tempfile::NamedTempFile::new().unwrap();
        super::super::verify::write_pending_verification_json(verdict_file.path()).unwrap();

        let empty = DbtStatsSink::new().unwrap();
        fs::write(empty.directory().join("records.bin"), b"").unwrap();
        assert!(require_dbt_stats(&empty.capture()).is_err());

        let json: serde_json::Value =
            serde_json::from_slice(&fs::read(verdict_file.path()).unwrap()).unwrap();
        assert_eq!(json["verdict"], "no_result");
        assert_eq!(json["verified"], false);
        assert_eq!(json["bitwise_parity"], false);
        assert!(json.get("dbt_counted_branches").is_none());
    }

    /// The published stream reaches the comparison log unchanged and in
    /// stream order, initialization records included.
    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_canonical_evidence_materializes_records_unchanged() {
        let log = tempfile::NamedTempFile::new().unwrap();
        let (file, path) = log.into_parts();
        let records = vec![
            b"1970-01-01T00:00:00.000000Z INFO detcore: DETLOG first\n".to_vec(),
            b"1970-01-01T00:00:00.000000Z INFO detcore::scheduler: second\n".to_vec(),
        ];
        let published = [
            DBT_INITIALIZATION_RECORD,
            records[0].as_slice(),
            records[1].as_slice(),
        ];

        let compared = materialize_dbt_comparison_log(&records, &published, file, &path).unwrap();

        // The initialization record is an INFO record of the published stream,
        // so it is counted with the guest records.
        assert_eq!(compared, 3);
        assert_eq!(fs::read(&path).unwrap(), published.concat());

        // Materialization publishes an initialization record where the decoded
        // stream holds it, neither dropped nor moved. This stream is built by
        // hand: the Reverie collector would place the second initialization
        // record immediately before "child record 1", but the artifact carries
        // no image identity, so the decoder cannot enforce that placement and
        // accepts this layout too. Two streams with the same guest records and
        // the same initialization count, differing only in where an
        // initialization record sits, publish different logs; the typed count
        // alone could not tell them apart.
        let parent_first: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 1\n";
        let parent_second: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: parent record 2\n";
        let child: &[u8] = b"1970-01-01T00:00:00.000000Z INFO detcore: child record 1\n";
        let stream = [
            DBT_INITIALIZATION_RECORD,
            parent_first,
            DBT_INITIALIZATION_RECORD,
            parent_second,
            child,
        ];
        let evidence = decoded_dbt_evidence(&stream);
        assert_eq!(evidence.all_records(), stream);
        let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
        let compared = materialize_dbt_evidence_log(&evidence, file, &path).unwrap();
        assert_eq!(compared, 5, "both initialization records are INFO records");
        assert_eq!(fs::read(&path).unwrap(), stream.concat());

        let front_loaded = decoded_dbt_evidence(&[
            DBT_INITIALIZATION_RECORD,
            DBT_INITIALIZATION_RECORD,
            parent_first,
            parent_second,
            child,
        ]);
        assert_eq!(front_loaded.records(), evidence.records());
        assert_eq!(
            front_loaded.initialization_records(),
            evidence.initialization_records()
        );
        let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
        materialize_dbt_evidence_log(&front_loaded, file, &path).unwrap();
        assert_ne!(fs::read(&path).unwrap(), stream.concat());
    }

    /// Evidence without guest records, or with an unframed record, is refused
    /// before anything is compared.
    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_canonical_evidence_fails_closed_on_empty_or_unframed_records() {
        let empty = tempfile::NamedTempFile::new().unwrap();
        let (file, path) = empty.into_parts();
        assert!(materialize_dbt_comparison_log(&[], &[], file, &path).is_err());

        let unframed_record = b"1970-01-01T00:00:00.000000Z INFO detcore: missing newline";
        let unframed = tempfile::NamedTempFile::new().unwrap();
        let (file, path) = unframed.into_parts();
        assert!(
            materialize_dbt_comparison_log(
                &[unframed_record.to_vec()],
                &[DBT_INITIALIZATION_RECORD, unframed_record],
                file,
                &path,
            )
            .is_err()
        );

        // Valid guest records do not excuse an unframed record in the stream
        // that is actually published.
        let framed_record = b"1970-01-01T00:00:00.000000Z INFO detcore: framed\n";
        let unframed_published = tempfile::NamedTempFile::new().unwrap();
        let (file, path) = unframed_published.into_parts();
        assert!(
            materialize_dbt_comparison_log(
                &[framed_record.to_vec()],
                &[framed_record, unframed_record],
                file,
                &path,
            )
            .is_err()
        );

        // The refusal https://github.com/rrnewton/hermit/pull/2356 added must
        // survive the publication of initialization records. Every decoded
        // stream holds at least one initialization record, and it is an INFO
        // record, so a guard that counted `all_records()` would pass both
        // streams below and the verdict would report nonzero compared INFO
        // counts for a guest that left no evidence. Each refusal is asserted by
        // its exact message: counting the published stream in either guard
        // makes this test fail, either with a different message or because
        // materialization succeeds.
        let startup_only = decoded_dbt_evidence(&[DBT_INITIALIZATION_RECORD]);
        assert!(startup_only.records().is_empty());
        assert_eq!(startup_only.all_records(), [DBT_INITIALIZATION_RECORD]);
        let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
        let error = materialize_dbt_evidence_log(&startup_only, file, &path).unwrap_err();
        assert_eq!(
            error.to_string(),
            "DBT canonical evidence contained no records"
        );

        // A guest record outside the INFO comparison cannot stand in for guest
        // evidence, even beside two INFO initialization records.
        let no_guest_info = decoded_dbt_evidence(&[
            DBT_INITIALIZATION_RECORD,
            b"1970-01-01T00:00:00.000000Z DEBUG detcore: guest diagnostic\n",
            DBT_INITIALIZATION_RECORD,
        ]);
        assert_eq!(no_guest_info.records().len(), 1);
        assert_eq!(no_guest_info.initialization_records(), 2);
        let (file, path) = tempfile::NamedTempFile::new().unwrap().into_parts();
        let error = materialize_dbt_evidence_log(&no_guest_info, file, &path).unwrap_err();
        assert_eq!(
            error.to_string(),
            "DBT canonical evidence contained no INFO records"
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_canonical_evidence_fails_closed_on_empty_or_malformed_artifact() {
        let mut empty = tempfile::tempfile().unwrap();
        assert!(decode_dbt_evidence(&mut empty).is_err());

        let mut malformed = tempfile::tempfile().unwrap();
        malformed.write_all(b"not framed evidence").unwrap();
        assert!(decode_dbt_evidence(&mut malformed).is_err());
    }

    /// The process-image initialization record, byte for byte the constant
    /// Reverie's decoder admits. Every decoded stream holds at least one.
    #[cfg(feature = "dbt")]
    const DBT_INITIALIZATION_RECORD: &[u8] =
        b"1970-01-01T00:00:00.000000Z INFO reverie_dbt::evidence: protected evidence initialized\n";

    /// Encode `records` in the version 1 evidence file format that
    /// `reverie_dbt::decode_evidence` authenticates. Reverie exports no
    /// encoder, so the layout is written out here: a 40-byte header holding the
    /// magic, version, header length, a zero reserved word, the record count,
    /// the payload byte count and an FNV-1a digest of the body, followed by the
    /// body, in which each record is preceded by its little-endian `u32` length.
    #[cfg(feature = "dbt")]
    fn encode_dbt_evidence(records: &[&[u8]]) -> Vec<u8> {
        let mut body = Vec::new();
        for record in records {
            body.extend_from_slice(&u32::try_from(record.len()).unwrap().to_le_bytes());
            body.extend_from_slice(record);
        }
        let payload_bytes: usize = records.iter().map(|record| record.len()).sum();
        let digest = body.iter().fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
            (hash ^ u64::from(*byte)).wrapping_mul(0x0000_0100_0000_01b3)
        });
        let mut encoded = Vec::with_capacity(40 + body.len());
        encoded.extend_from_slice(b"RVDBTEF1");
        encoded.extend_from_slice(&1_u16.to_le_bytes());
        encoded.extend_from_slice(&40_u16.to_le_bytes());
        encoded.extend_from_slice(&0_u32.to_le_bytes());
        encoded.extend_from_slice(&u64::try_from(records.len()).unwrap().to_le_bytes());
        encoded.extend_from_slice(&u64::try_from(payload_bytes).unwrap().to_le_bytes());
        encoded.extend_from_slice(&digest.to_le_bytes());
        encoded.extend_from_slice(&body);
        encoded
    }

    /// Decode `records` through the production decoder, as a DBT run's
    /// evidence file is decoded.
    #[cfg(feature = "dbt")]
    fn decoded_dbt_evidence(records: &[&[u8]]) -> DbtEvidence {
        let mut file = tempfile::tempfile().unwrap();
        file.write_all(&encode_dbt_evidence(records)).unwrap();
        decode_dbt_evidence(&mut file).expect("fixture evidence must decode")
    }

    // Under DBT the determinism-loss latch is in the guest; the client
    // reports it as an evidence record, and the run is refused by the same
    // check the generic verifier uses, never compared.
    #[test]
    #[cfg(feature = "dbt")]
    fn a_dbt_run_whose_evidence_records_a_determinism_loss_is_not_compared() {
        let record: &[u8] =
            b"1970-01-01T00:00:00.000000Z INFO detcore: [dtid 3] DETLOG [syscall] getpid() = 3\n";
        let clean = decoded_dbt_evidence(&[DBT_INITIALIZATION_RECORD, record]);
        assert_eq!(dbt_run_latches(&clean).determinism_loss, None);
        assert!(dbt_run_latches(&clean).refuse_comparison("run 1").is_ok());

        let loss = format!(
            "1970-01-01T00:00:00.000000Z ERROR {}: process 3 exited without deregistering\n",
            detcore::detlog::DETERMINISM_LOSS_RECORD_TARGET
        );
        let lost = decoded_dbt_evidence(&[DBT_INITIALIZATION_RECORD, record, loss.as_bytes()]);
        let refusal = dbt_run_latches(&lost)
            .refuse_comparison("run 2")
            .unwrap_err()
            .to_string();
        assert_eq!(
            refusal,
            "run 2: determinism loss recorded: process 3 exited without deregistering"
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_stats_block_labels_counters_honestly() {
        let rendered = format_dbt_stats(&dbt_stats(563_145, 169, 168, 0, 0x4b5e0e70f3050157));
        // The branch counter must be labeled as a branch clock, never as
        // "basic blocks translated" — the client counts retired branches.
        assert!(rendered.contains("counted branches (deterministic branch clock): 563145"));
        assert!(rendered.contains("syscalls intercepted:                          169"));
        assert!(rendered.contains("syscall instructions rewritten:                168"));
        assert!(rendered.contains("stdin (fd 0) reads:                            0"));
        assert!(
            rendered.contains("observed guest-memory hash:                    4b5e0e70f3050157")
        );
        assert!(!rendered.to_lowercase().contains("basic block"));
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_stats_block_renders_the_typed_wire_snapshot() {
        let rendered = format_dbt_stats(&dbt_stats(42, 7, 6, 0, 0xcbf29ce484222325));
        assert!(rendered.contains("counted branches (deterministic branch clock): 42"));
        assert!(rendered.contains("syscalls intercepted:                          7"));
        assert!(
            rendered.contains("observed guest-memory hash:                    cbf29ce484222325")
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_summary_compares_every_non_branch_observation() {
        let expected = dbt_stats(100, 169, 168, 0, 0x4b5e0e70f3050157);

        assert!(!same_dbt_observable_behavior(
            &expected,
            &dbt_stats(100, 170, 168, 0, 0x4b5e0e70f3050157)
        ));
        assert!(!same_dbt_observable_behavior(
            &expected,
            &dbt_stats(100, 169, 167, 0, 0x4b5e0e70f3050157)
        ));
        assert!(!same_dbt_observable_behavior(
            &expected,
            &dbt_stats(100, 169, 168, 1, 0x4b5e0e70f3050157)
        ));
        assert!(!same_dbt_observable_behavior(
            &expected,
            &dbt_stats(100, 169, 168, 0, 0)
        ));

        let different_branch_clock = dbt_stats(101, 169, 168, 0, 0x4b5e0e70f3050157);
        assert!(
            same_dbt_observable_behavior(&expected, &different_branch_clock),
            "the counted-branch clock has its own typed comparison and must not be folded into \
             the opaque summary mismatch"
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_physical_command_preserves_explicit_environment_removals() {
        let inherited = env::vars_os()
            .map(|(key, _)| key)
            .find(|key| key != "DBT_PHYSICAL_KEPT")
            .expect("native fixture requires an inherited environment entry");
        let environment = BTreeMap::from([(
            OsString::from("DBT_PHYSICAL_KEPT"),
            OsString::from("literal guest value"),
        )]);
        let mut command = StdCommand::new("/usr/bin/env");
        apply_exact_environment(&mut command, &environment);
        command.env_remove("DBT_PHYSICAL_EXPLICIT_REMOVAL");
        let original = command
            .get_envs()
            .map(|(key, value)| (key.to_owned(), value.map(OsStr::to_owned)))
            .collect::<BTreeMap<_, _>>();
        assert_eq!(original.get(&inherited), Some(&None));
        assert_eq!(
            original.get(OsStr::new("DBT_PHYSICAL_EXPLICIT_REMOVAL")),
            Some(&None)
        );
        let prepared = prepare_dbt_physical_command(&command).unwrap();
        let transported = prepared
            .get_envs()
            .map(|(key, value)| (key.to_owned(), value.map(OsStr::to_owned)))
            .collect::<BTreeMap<_, _>>();
        assert_eq!(
            transported, original,
            "physical command preparation must preserve each removal for DbtRunner"
        );
        assert_eq!(prepared.get_program(), command.get_program());
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_resolves_simple_env_shebang_target_to_absolute_path() {
        let root = tempfile::tempdir().unwrap();
        let bin = root.path().join("bin");
        fs::create_dir(&bin).unwrap();
        let python = bin.join("python3");
        write_executable(&python, b"\x7fELFplaceholder");
        let script = root.path().join("guest.py");
        write_executable(&script, b"#!/usr/bin/env python3\n");

        let prepared = prepare_dbt_guest_command(
            &script,
            &[OsString::from("argument")],
            Some(bin.as_os_str()),
            root.path(),
        );

        assert_eq!(prepared.program, Path::new("/usr/bin/env"));
        assert_eq!(
            prepared.args,
            [
                python.into_os_string(),
                script.into_os_string(),
                OsString::from("argument"),
            ]
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_leaves_complex_env_shebang_for_launcher() {
        let root = tempfile::tempdir().unwrap();
        let script = root.path().join("guest.py");
        write_executable(&script, b"#!/usr/bin/env -S python3 -u\n");

        let prepared =
            prepare_dbt_guest_command(&script, &[], Some(OsStr::new("/usr/bin")), root.path());

        assert_eq!(prepared.program, script);
        assert!(prepared.args.is_empty());
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_relative_shebang_uses_guest_cwd_and_preserves_script_arguments() {
        let root = tempfile::Builder::new()
            .prefix("dbt-workdir-")
            .tempdir_in(".")
            .unwrap();
        let relative = PathBuf::from(root.path().file_name().unwrap()).join("guest.py");
        let guest_cwd = fs::canonicalize(root.path()).unwrap().join("guest-cwd");
        let guest_script = guest_cwd.join(&relative);
        fs::create_dir_all(guest_script.parent().unwrap()).unwrap();
        write_executable(&relative, b"#!/usr/bin/env caller-only\n");
        write_executable(&guest_script, b"#!/usr/bin/env guest-only\n");
        let relative_bin = PathBuf::from(root.path().file_name().unwrap()).join("bin");
        let guest_bin = guest_cwd.join(&relative_bin);
        fs::create_dir_all(&relative_bin).unwrap();
        fs::create_dir_all(&guest_bin).unwrap();
        write_executable(&relative_bin.join("caller-only"), b"\x7fELFcaller");
        write_executable(&guest_bin.join("guest-only"), b"\x7fELFguest");
        let mut command = StdCommand::new(&relative);
        command
            .current_dir(&guest_cwd)
            .env_clear()
            .env("PATH", &relative_bin)
            .env("KEPT", "literal value")
            .env_remove("EXPLICIT_REMOVAL")
            .args(["literal argument", ""]);
        let prepared = prepare_dbt_physical_command(&command).unwrap();
        assert_eq!(prepared.get_program(), OsStr::new("/usr/bin/env"));
        assert_eq!(
            prepared.get_args().map(OsStr::to_owned).collect::<Vec<_>>(),
            [
                guest_bin.join("guest-only").into_os_string(),
                relative.into_os_string(),
                OsString::from("literal argument"),
                OsString::new()
            ]
        );
        assert_eq!(prepared.get_current_dir(), Some(guest_cwd.as_path()));
        assert_eq!(
            prepared.get_envs().collect::<Vec<_>>(),
            command.get_envs().collect::<Vec<_>>()
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_path_selected_script_keeps_the_selected_script_path() {
        let root = tempfile::tempdir().unwrap();
        let bin = root.path().join("bin");
        fs::create_dir(&bin).unwrap();
        write_executable(
            &bin.join("guest.py"),
            b"#!/usr/bin/env chosen-interpreter\n",
        );
        let interpreter = bin.join("chosen-interpreter");
        write_executable(&interpreter, b"\x7fELFguest");
        for (cwd, path, script, target) in [
            (
                root.path(),
                OsStr::new("bin"),
                PathBuf::from("bin/guest.py"),
                interpreter.clone(),
            ),
            (
                root.path(),
                bin.as_os_str(),
                bin.join("guest.py"),
                interpreter.clone(),
            ),
            (
                root.path(),
                OsStr::new(":bin"),
                PathBuf::from("bin/guest.py"),
                interpreter.clone(),
            ),
            (
                bin.as_path(),
                OsStr::new(""),
                PathBuf::from("guest.py"),
                interpreter.clone(),
            ),
            (
                bin.as_path(),
                OsStr::new("."),
                PathBuf::from("./guest.py"),
                bin.join("./chosen-interpreter"),
            ),
        ] {
            let mut command = StdCommand::new("guest.py");
            command
                .current_dir(cwd)
                .env_clear()
                .env("PATH", path)
                .args(["", "two words"]);
            let prepared = prepare_dbt_physical_command(&command).unwrap();
            assert_eq!(prepared.get_program(), OsStr::new("/usr/bin/env"));
            assert_eq!(
                prepared.get_args().map(OsStr::to_owned).collect::<Vec<_>>(),
                [
                    target.into_os_string(),
                    script.into_os_string(),
                    OsString::new(),
                    OsString::from("two words"),
                ],
                "PATH {path:?} in {cwd:?} must identify the selected script"
            );
            assert_eq!(prepared.get_current_dir(), command.get_current_dir());
            assert_eq!(
                prepared.get_envs().collect::<Vec<_>>(),
                command.get_envs().collect::<Vec<_>>()
            );
        }
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_empty_path_component_and_hidden_script_use_physical_cwd() {
        let root = tempfile::Builder::new()
            .prefix("dbt-physical-cwd-")
            .tempdir_in(".")
            .unwrap();
        let relative = PathBuf::from(root.path().file_name().unwrap()).join("guest.py");
        let guest_cwd = fs::canonicalize(root.path()).unwrap().join("empty-workdir");
        fs::create_dir(&guest_cwd).unwrap();
        write_executable(&relative, b"#!/usr/bin/env chosen-interpreter\n");
        let caller_bin = root.path().join("bin");
        fs::create_dir(&caller_bin).unwrap();
        write_executable(&caller_bin.join("chosen-interpreter"), b"\x7fELFcaller");
        let mut command = StdCommand::new(&relative);
        command
            .current_dir(&guest_cwd)
            .env_clear()
            .env("PATH", &caller_bin)
            .arg("argument");
        // The caller can see this relative script; the physical guest cannot.
        let absent = prepare_dbt_physical_command(&command).unwrap();
        assert_eq!(absent.get_program(), relative.as_os_str());
        assert_eq!(
            absent.get_args().collect::<Vec<_>>(),
            [OsStr::new("argument")]
        );
        // An empty PATH component denotes the physical guest's cwd.
        let script = guest_cwd.join("script");
        write_executable(&script, b"#!/usr/bin/env chosen-interpreter\n");
        let interpreter = guest_cwd.join("chosen-interpreter");
        write_executable(&interpreter, b"\x7fELFguest");
        let mut command = StdCommand::new(&script);
        command.current_dir(&guest_cwd).env_clear().env("PATH", "");
        let prepared = prepare_dbt_physical_command(&command).unwrap();
        assert_eq!(prepared.get_program(), OsStr::new("/usr/bin/env"));
        assert_eq!(
            prepared.get_args().collect::<Vec<_>>(),
            [interpreter.as_os_str(), script.as_os_str()]
        );
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_artifact_returns_the_validated_absolute_path() {
        let file = tempfile::NamedTempFile::new_in(".").unwrap();
        let relative_path = file.path().file_name().unwrap();

        let resolved = validate_sabre_artifact(Path::new(relative_path), "test-artifact", false)
            .map(std::path::PathBuf::from)
            .unwrap();

        assert!(resolved.is_absolute());
        assert_eq!(resolved, fs::canonicalize(file.path()).unwrap());
    }

    /// A guest killed by a signal must report as SIGNALLED, not as a normal exit.
    ///
    /// ⚠️ THIS IS THE TEST THAT WAS MISSING, WHICH IS WHY ONE SITE GOT FIXED AND
    /// ITS SIBLING DID NOT. `process_status` was converted to `from_raw`;
    /// `output_status` kept `code().unwrap_or(1)` and nothing noticed, because no
    /// test asserted the signalled case for either. Asserting it here binds BOTH.
    #[test]
    fn a_signalled_guest_is_not_reported_as_a_normal_exit() {
        use std::os::unix::process::ExitStatusExt as _;

        // raw wait status for "killed by SIGSEGV" (11), no core dump.
        let raw = 11i32;
        let native = std::process::ExitStatus::from_raw(raw);
        assert!(native.code().is_none(), "SIGSEGV death has no exit code");

        let converted = process_status(native);
        assert!(
            !matches!(converted, ExitStatus::Exited(_)),
            "a signalled guest must not read as a normal exit, got {converted:?}"
        );

        // ⚠️ CALL `output_status` ITSELF, not an expression that looks like it.
        // My first version compared `process_status(..)` against an inline
        // `ExitStatus::from_raw(..)` and never invoked `output_status` at all --
        // so reverting `output_status` to the buggy form left this test GREEN.
        // Caught by mutating BOTH sites instead of one.
        let output = Output {
            status: native,
            stdout: Vec::new(),
            stderr: Vec::new(),
        };
        let from_output = output_status(&output);
        assert!(
            !matches!(from_output, ExitStatus::Exited(_)),
            "output_status must not report a signalled guest as a normal exit, got {from_output:?}"
        );
        assert_eq!(
            converted, from_output,
            "output_status and process_status must agree; they diverged once and \
             only one of them was fixed"
        );
    }

    /// A real spawn failure -- drrun absent or not executable -- must still say
    /// so, because that is when the operator SHOULD go and look at the binary.
    #[cfg(feature = "dbt")]
    #[test]
    fn a_spawn_failure_still_names_the_launch() {
        let drrun = Path::new("/nonexistent/dynamorio/bin64/drrun");
        for kind in [
            std::io::ErrorKind::NotFound,
            std::io::ErrorKind::PermissionDenied,
        ] {
            let rendered = dbt_run_error(drrun, std::io::Error::new(kind, "boom")).to_string();
            assert!(
                rendered.contains("failed to launch drrun"),
                "a {kind:?} spawn failure must be reported as a launch failure: {rendered}"
            );
        }
    }

    /// The regression this function exists for. A post-launch failure -- the
    /// evidence-finalization error is the real-world one -- must NOT be
    /// reported as a launch failure. It sent a campaign agent hunting a 737 KB
    /// drrun that was correct and working, and the inner text proves the guest
    /// had already exited.
    #[cfg(feature = "dbt")]
    #[test]
    fn a_post_launch_failure_does_not_blame_the_binary() {
        let drrun = Path::new("/real/dynamorio/bin64/drrun");
        let inner = "DBT guest exited with status Some(1) while protected evidence failed: \
                     DBT evidence collector thread panicked";
        let rendered = dbt_run_error(drrun, std::io::Error::other(inner)).to_string();
        assert!(
            !rendered.contains("failed to launch drrun"),
            "a post-launch failure must not be reported as a launch failure: {rendered}"
        );
        assert!(
            rendered.contains("drrun started"),
            "the message must say the launch succeeded: {rendered}"
        );
        assert!(
            rendered.contains("NOT a launch failure"),
            "the message must rule the binary out explicitly: {rendered}"
        );
        // The cause must survive verbatim. Truncating it here is what cost the
        // original investigation its answer.
        assert!(
            rendered.contains("protected evidence failed"),
            "the underlying error must be preserved in full: {rendered}"
        );
    }

    /// A signalled death must not be reported as a normal exit.
    ///
    /// Ported from hermit#1689's fifth commit as the one free-standing piece of
    /// that head: the behaviour it brackets (`ExitStatus::from_raw` preserving
    /// exited-versus-signalled) is ALREADY on main -- that head's claim 2 landed
    /// via `b441950f72` -- while its bracket never did. Landed code with no
    /// coverage, which is the pairing worth closing first.
    #[test]
    fn dbt_status_preserves_normal_exit_codes() {
        for code in [0, 1, 42, 255] {
            let raw = std::process::ExitStatus::from_raw(code << 8);
            assert_eq!(
                process_status(raw),
                ExitStatus::Exited(code),
                "a normal exit with code {code} must round-trip"
            );
        }
    }

    #[test]
    fn dbt_status_preserves_death_by_signal() {
        for (signum, signal) in [
            (libc::SIGABRT, reverie::Signal::SIGABRT),
            (libc::SIGSEGV, reverie::Signal::SIGSEGV),
            (libc::SIGFPE, reverie::Signal::SIGFPE),
            (libc::SIGILL, reverie::Signal::SIGILL),
            (libc::SIGTERM, reverie::Signal::SIGTERM),
            (libc::SIGKILL, reverie::Signal::SIGKILL),
            (libc::SIGTRAP, reverie::Signal::SIGTRAP),
        ] {
            // without a core dump
            let raw = std::process::ExitStatus::from_raw(signum);
            assert_eq!(
                process_status(raw),
                ExitStatus::Signaled(signal, false),
                "death by signal {signum} must be reported as Signaled, not Exited"
            );
            // with the core-dump flag set (bit 0x80 of the wait status)
            let raw = std::process::ExitStatus::from_raw(signum | 0x80);
            assert_eq!(
                process_status(raw),
                ExitStatus::Signaled(signal, true),
                "the core-dump flag for signal {signum} must survive the conversion"
            );
        }
    }

    /// Pins main's CURRENT DBT `--log-file` policy, which was undefended.
    ///
    /// hermit#1689 claim 1 would reverse this. That reversal may well be correct --
    /// it is an owner ruling, and this test takes no side on it. What it removes is
    /// the possibility of reversing it SILENTLY: before this, the refusal existed
    /// only as an error string with no assertion, so flipping the condition changed
    /// behaviour without changing any test.
    #[test]
    fn dbt_log_file_is_refused_on_the_single_run_adapter_and_permitted_under_verify() {
        // the refusal: a log file was asked for, and this is not a verification run
        assert!(
            dbt_log_file_is_refused(true, false),
            "a requested --log-file must be refused on the ordinary single-run adapter"
        );
        // --verify owns its own sink, so the request is honoured there
        assert!(
            !dbt_log_file_is_refused(true, true),
            "--verify must keep accepting --log-file; the verification adapter owns the sink"
        );
        // no request, nothing to refuse, either way
        assert!(!dbt_log_file_is_refused(false, false));
        assert!(!dbt_log_file_is_refused(false, true));
    }

    /// The refusal text is what a caller greps for, so pin it exactly.
    #[test]
    fn dbt_log_file_refusal_names_the_adapter_it_is_about() {
        assert_eq!(
            DBT_LOG_FILE_REFUSAL,
            "DBT --log-file is unavailable on the ordinary single-run adapter"
        );
    }
}