stegobench-cli 1.0.0

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

use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};

use clap::{CommandFactory, Parser};
use stegobench_cli::cli::{Cli, Command, ReportFormat};
use stegobench_cli::examine;
use stegobench_cli::fetch;
use stegobench_cli::fixtures;
use stegobench_cli::help_topics;
use stegobench_cli::metrics;
use stegobench_cli::needs;
// One rendering of an IO error for the whole binary, shared with `report`,
// whose own error type used to print the operating system's number.
use stegobench_cli::plain_io as plain;
use stegobench_cli::registry;
use stegobench_cli::registry::Resolved;
use stegobench_cli::report;
use stegobench_cli::score;
use stegobench_cli::STARTER_ID;
use stegobench_core::registry::{Kind, Registry};
use stegobench_core::{exit, ManifestV1, Result1, RunV1};
use stegobench_plugin::availability::Presence;
use stegobench_plugin::{availability, selftest, Verified};

/// What a subcommand produced: a JSON value for stdout, and human text for stderr.
#[cfg_attr(test, derive(Debug))]
struct Output {
    json: serde_json::Value,
    human: String,
    code: i32,
    /// Whether the human text IS the payload and belongs on stdout even
    /// though the command did not exit zero.
    ///
    /// One command needs this. `report` prints a table AND exits non-zero
    /// when a file could not be read, because the table is worth having and
    /// the code says it is short. Without this flag the table would be
    /// diverted to stderr in exactly the case a reader most needs to see it.
    payload_on_stdout: bool,
}

impl Output {
    fn ok(json: serde_json::Value, human: impl Into<String>) -> Self {
        Output {
            json,
            human: human.into(),
            code: exit::OK,
            payload_on_stdout: false,
        }
    }

    /// A refusal or a failure. `code` says which, and the distinction is the
    /// whole reason the codes are enumerated.
    fn err(code: i32, human: impl Into<String>) -> Self {
        let human = human.into();
        Output {
            json: serde_json::json!({ "ok": false, "error": human }),
            human,
            code,
            payload_on_stdout: false,
        }
    }

    /// The same, carrying a stable word a script can branch on.
    ///
    /// The exit code says how bad it was and the message says what happened in
    /// English, and neither is a thing to branch on: there are more failures
    /// than codes, and the prose is written to be read by a person and will be
    /// reworded when a person is confused by it. `metrics` has published one of
    /// these since it shipped and `score` did not, so automating against
    /// `score` meant matching English that nothing promised to keep.
    fn err_because(code: i32, reason: &str, human: impl Into<String>) -> Self {
        let mut out = Output::err(code, human);
        out.json["reason"] = serde_json::json!(reason);
        out
    }
}

/// A [`score::ScoreError`] as the command's answer: the stable word, the exit
/// code, and the prose with the flag that caused it named where one did.
fn score_refusal(e: &score::ScoreError, limit: Option<u64>) -> Output {
    let why = e.to_string();
    // `--limit` TAKES A PREFIX, AND A CORPUS PUTS ITS COVERS FIRST
    //
    // So the flag advertised for a smoke test is the one that reliably
    // produces an all-clean set and a refusal about the corpus. The refusal is
    // true and it names the wrong culprit: nothing is wrong with the corpus,
    // the command asked for a slice of it that cannot be measured. Naming the
    // flag here rather than widening the message in `score.rs` keeps the
    // corpus's own refusal about the corpus.
    let human = match (e, limit) {
        (score::ScoreError::OneSided { .. }, Some(n)) => format!(
            "{why}.\n--limit {n} takes the FIRST {n} item(s) in corpus order, \
             and a corpus lists its covers before its stego arms, so a small \
             limit reaches clean images only. Raise it past the covers, drop \
             it and score the whole corpus, or use --split test for a smaller \
             whole."
        ),
        _ => why,
    };
    Output::err_because(e.exit_code(), e.reason(), human)
}

const KNOWN_SCHEMAS: &[&str] = &["result-v1", "run-v1", "manifest-v1"];

fn schema_value(name: &str) -> Option<serde_json::Value> {
    let schema = match name {
        "result-v1" => serde_json::to_value(schemars::schema_for!(Result1)),
        "run-v1" => serde_json::to_value(schemars::schema_for!(RunV1)),
        "manifest-v1" => serde_json::to_value(schemars::schema_for!(ManifestV1)),
        _ => return None,
    };
    Some(schema.expect("a generated schema serialises"))
}

fn cmd_schema(name: &str) -> Output {
    if name == "all" {
        let map: serde_json::Map<String, serde_json::Value> = KNOWN_SCHEMAS
            .iter()
            .map(|n| (n.to_string(), schema_value(n).expect("known schema")))
            .collect();
        return Output::ok(
            serde_json::Value::Object(map),
            "all schemas written to stdout",
        );
    }
    match schema_value(name) {
        Some(v) => Output::ok(v, format!("{name} schema written to stdout")),
        None => Output::err(
            exit::USAGE,
            format!(
                "unknown schema {name:?}. Known schemas: {}, or `all`",
                KNOWN_SCHEMAS.join(", ")
            ),
        ),
    }
}

/// Finds the registry once, and hands it to the command that asked for it.
///
/// Every failure to find one is the same shape and the same exit code, so the
/// five commands that read a registry report it identically rather than each
/// inventing its own wording.
fn with_registry(cli: &Cli, f: impl FnOnce(&Resolved) -> Output) -> Output {
    match registry::resolve(cli.registry.as_deref()) {
        Ok(resolved) => f(&resolved),
        Err(e) => Output::err(e.exit_code(), e.to_string()),
    }
}

/// The human block for the registered corpora.
///
/// An empty registry prints a sentence saying so rather than nothing at all.
/// A blank listing under an exit code of zero reads as "checked, all fine",
/// and the honest statement is that nothing has been declared, which is a
/// different fact from there being no corpus in the world. `list` is a listing
/// rather than a check, so an empty one is not a failure: it is the count that
/// has to be visible, and it is, in both the text and the JSON.
fn corpora_block(reg: &Registry, where_from: &str) -> String {
    if reg.corpora.is_empty() {
        return format!("no corpora registered in {where_from}");
    }
    // Aligned over the rows actually being printed. A fixed pad here silently
    // stopped lining up the day a corpus with a longer id than the literal was
    // registered, and nothing failed to say so.
    let rows = reg.corpora.values().map(|c| c.cells()).collect::<Vec<_>>();
    let mut text = stegobench_core::table::align(&rows).join("\n");
    let publishable = reg
        .corpora
        .values()
        .filter(|c| c.licence.redistribution.allows_publishing())
        .count();
    // The use/publish split stays: it is the distinction the `redistribution`
    // field exists for, and a reader who misses it republishes something they
    // may only measure against.
    text.push_str(&format!(
        "\n\n{} corpora, {publishable} republishable. Using one is not \
         publishing it; `describe <id>` prints the terms.",
        reg.corpora.len()
    ));
    text
}

fn cmd_list(resolved: &Resolved, kind: &str) -> Output {
    let reg = &resolved.registry;
    // Said on every listing, not only in `doctor`. `list` is the first command
    // a person runs, and a machine with an installed registry and a checkout
    // has two answers to "what can this run"; which one answered is part of
    // the answer.
    let where_from = match resolved.source.path() {
        Some(p) => p.display().to_string(),
        None => "built in".to_string(),
    };
    let corpora_from = match resolved.source.path() {
        Some(p) => format!("{}/corpora", p.display()),
        None => where_from.clone(),
    };
    if kind == "corpora" {
        return Output::ok(
            serde_json::json!({
                "corpora": reg.corpora.values().collect::<Vec<_>>(),
                "count": reg.corpora.len(),
                "registry": resolved.to_json(),
            }),
            format!(
                "{}\n\n{}",
                corpora_block(reg, &corpora_from),
                resolved.source.line()
            ),
        );
    }
    let wanted: Vec<_> = match kind {
        "detectors" => reg.of_kind(Kind::Detector),
        "embedders" => reg.of_kind(Kind::Embedder),
        "all" => reg.entries.values().collect(),
        other => {
            return Output::err(
                exit::USAGE,
                format!("unknown kind {other:?}. Known: detectors, embedders, corpora, all"),
            )
        }
    };
    // Same alignment rule as `corpora_block`, and for the same reason: the
    // widest name in the listing sets the column, not a literal in the source.
    //
    // Over the whole listing rather than per group, so the two tables under
    // `list all` share one set of columns and read as one table with headings
    // rather than two tables that happen to be adjacent.
    let rows = wanted.iter().map(|e| e.cells()).collect::<Vec<_>>();
    let aligned = stegobench_core::table::align(&rows);
    let mut human = if kind == "all" {
        // HEADINGS, BECAUSE A BARE `stegobench list` HAD NONE.
        //
        // It printed thirteen tool rows, a footer, then five corpus rows,
        // with nothing saying that the first block was tools and the second
        // was data, or that six of the thirteen were embedders rather than
        // the detectors `list detectors` had just counted at seven.
        let mut out: Vec<String> = Vec::new();
        for (k, heading) in [(Kind::Detector, "DETECTORS"), (Kind::Embedder, "EMBEDDERS")] {
            let group: Vec<&String> = wanted
                .iter()
                .zip(&aligned)
                .filter(|(e, _)| e.kind == k)
                .map(|(_, line)| line)
                .collect();
            if group.is_empty() {
                continue;
            }
            if !out.is_empty() {
                out.push(String::new());
            }
            out.push(format!("{heading} ({})", group.len()));
            out.extend(group.into_iter().cloned());
        }
        out.join("\n")
    } else {
        aligned.join("\n")
    };
    if human.is_empty() {
        // Same standard as `corpora_block`: a bare line under an exit code of
        // zero reads as "checked, all fine". Say where it looked, and say
        // whether the registry is empty or merely has nothing of this kind,
        // because those two call for different actions.
        human = if reg.entries.is_empty() {
            format!("no tools registered in {where_from}")
        } else {
            format!(
                "none of the {} tool(s) in {where_from} is a {}. `list all` \
                 prints every one.",
                reg.entries.len(),
                kind.trim_end_matches('s')
            )
        };
    } else {
        // Said once, under the listing, rather than in every entry's
        // description. TWO FACTS, NOT ONE: what pins the tool, and what it can
        // reach. The legend used to carry them as a single word and was wrong
        // about the third shape, where the image names the subject and an
        // adapter here does the running. The vocabulary matches
        // `provenance.plugins[].pinned_by` and `.isolation` in a result, so
        // the listing and the document say the same words.
        human.push_str(
            "\n\ncontainer  pinned by image digest, sandboxed, no network. \
             Needs a container runtime.\nlocal      a program you installed, \
             pinned by the hash of the file that ran. No sandbox, your \
             network.",
        );
        // Counted rather than asserted. A sentence saying "one of these is a
        // service" would be a claim about a registry that changes, and the
        // point of this listing is that it cannot go stale.
        let services = wanted
            .iter()
            .filter(|e| e.invoke.as_ref().is_some_and(|i| i.host))
            .map(|e| e.name.as_str())
            .collect::<Vec<_>>();
        if !services.is_empty() {
            human.push_str(&format!(
                "\n\nSERVICE, not sandboxed, and needs the network: {}. The \
                 image names the subject; an adapter here reaches an instance \
                 you started. Nothing checks that instance was built from that \
                 image, so a result of it reads `unpinned`.",
                services.join(", ")
            ));
        }
        human.push_str("\n\n`stegobench doctor` says what each one still needs.");
        // WHICH ONE SHOULD I USE, WHICH THIS LISTING DID NOT ANSWER.
        //
        // A journey on 2026-10-02 nearly stopped here: seven names, a licence
        // column that "means nothing to me", and nothing saying which is any
        // good for the purpose or that one would do. This does not rank them,
        // because this listing cannot say which detector is better and saying
        // so would be the exact overclaim the project exists to correct. It
        // names the cheapest place to start, which is the question actually
        // being asked.
        if wanted.iter().any(|e| e.kind == Kind::Detector) {
            if let Some(way) = stegobench_cli::smallest_way_in(&resolved.registry) {
                human.push_str(&format!("\n\n{way}"));
            }
        }
        // Over what was listed, not over the registry. Printed under seven
        // detectors, a thirteen tool total reads as the cost of the seven.
        let f = stegobench_core::registry::Registry::footprint_of(wanted.iter().copied());
        human.push_str(&format!(
            // "small enough to bundle", not "in the image": the flag behind
            // this sum is derived from size, and a tool can be small and still
            // be out of the image for a reason size knows nothing about.
            // Reading it as the image's size over-counts.
            "\n\n{} tools in {} images. {} MB small enough to bundle, {:.1} GB \
             larger and fetched on demand.",
            f.tools,
            f.unique_images,
            f.bundled_mb,
            f.on_demand_mb as f64 / 1024.0
        ));
    }
    let mut json = serde_json::json!({
        "tools": wanted,
        "footprint": stegobench_core::registry::Registry::footprint_of(wanted.iter().copied()),
        "registry": resolved.to_json(),
    });
    if kind == "all" {
        human.push_str(&format!(
            "\n\nCORPORA ({})\n{}",
            reg.corpora.len(),
            corpora_block(reg, &corpora_from)
        ));
        json["corpora"] = serde_json::json!(reg.corpora.values().collect::<Vec<_>>());
    }
    human.push_str(&format!("\n\n{}", resolved.source.line()));
    Output::ok(json, human)
}

/// Hide one payload in one image with a registered embedder.
///
/// Deliberately one image. The pairing rule says a cover and its stego twin
/// must be written from the same source array through the same code path, and
/// a loop around this command satisfies neither: it reads the cover back off
/// disk each time and gives no way to state that the pair belongs together.
/// So this makes a demonstration or a fixture, and `generators/` makes a
/// corpus, and the help says so where somebody would otherwise find out by
/// publishing a number.
fn cmd_embed(
    resolved: &Resolved,
    embedder: &str,
    cover: &Path,
    payload: &Path,
    out: &Path,
    passphrase: Option<&str>,
    no_verify: bool,
) -> Output {
    let reg = &resolved.registry;
    let Some(entry) = reg.entries.get(embedder) else {
        return Output::err(
            exit::USAGE,
            format!(
                "no embedder called {embedder} is registered. `stegobench \
                 list embedders` shows the ones that are"
            ),
        );
    };
    if entry.kind != Kind::Embedder {
        return Output::err(
            exit::USAGE,
            format!(
                "{embedder} is registered as a detector, which answers \
                 questions about an image rather than hiding anything in \
                 one. `stegobench list embedders` shows what can embed"
            ),
        );
    }
    for (what, path) in [("cover", cover), ("payload", payload)] {
        if !path.is_file() {
            return Output::err(
                exit::PREFLIGHT_REFUSED,
                format!("the {what} {} is not a file here", path.display()),
            );
        }
    }

    match stegobench_plugin::embed::run(entry, cover, payload, out, passphrase, !no_verify) {
        Err(e) if e.contains("declares no way to embed") => Output::err(exit::PREFLIGHT_REFUSED, e),
        Err(e) if e.starts_with("could not stage") || e.starts_with("no scratch") => {
            Output::err(exit::PREFLIGHT_REFUSED, e)
        }
        Err(e) => Output::err(exit::PLUGIN_FAILED, e),
        Ok(done) => {
            let checked = match &done.recovered {
                Some(Ok(())) => "the payload was extracted again and matches byte for byte",
                Some(Err(_)) => "the payload did NOT survive",
                None => "the payload was NOT checked, so nothing here says it is really in there",
            };
            let json = serde_json::json!({
                "embedder": entry.name,
                "cover": cover.display().to_string(),
                "stego": done.stego.display().to_string(),
                "bytes": done.bytes,
                "verified": match &done.recovered {
                    Some(Ok(())) => serde_json::json!(true),
                    Some(Err(e)) => serde_json::json!({ "ok": false, "reason": e }),
                    None => serde_json::Value::Null,
                },
            });
            if let Some(Err(e)) = &done.recovered {
                let mut bad = Output::err(
                    exit::VERIFY_MISMATCH,
                    format!(
                        "{} wrote {} ({} bytes) and {e}.\nThe file is there \
                         and what is in it is not what you gave it, so it is \
                         not a stego image of that payload.",
                        entry.name,
                        done.stego.display(),
                        done.bytes
                    ),
                );
                bad.json = json;
                return bad;
            }
            Output::ok(
                json,
                format!(
                    "{} wrote {} ({} bytes), and {checked}.\nOne image is a \
                     demonstration. A corpus needs every pair written from \
                     one source through one code path: see `stegobench help \
                     pairing`.",
                    entry.name,
                    done.stego.display(),
                    done.bytes
                ),
            )
        }
    }
}

fn cmd_describe(resolved: &Resolved, name: &str, toml_only: bool) -> Output {
    let reg = &resolved.registry;
    // The needs block goes FIRST, above the facts, because it is the question
    // somebody typing `describe` is usually asking. One shape for every
    // subject: a container, a binary, a service and a corpus all answer here,
    // so a reader never has to know which of the four they are holding.
    if let Some(e) = reg.entries.get(name) {
        let needs = needs::of_tool(
            e,
            &availability::check(e, resolved.adapter_roots()),
            resolved.adapter_roots(),
        );
        let mut json = serde_json::to_value(e).unwrap_or(serde_json::Value::Null);
        if let Some(map) = json.as_object_mut() {
            map.insert("needs".into(), needs.to_json());
        }
        if toml_only {
            return raw_toml(e, json, &e.name);
        }
        // Only the roots that will still be there tomorrow. The built-in
        // registry unpacks its adapters into a scratch directory this process
        // owns, and a command naming that path is a command that cannot be
        // pasted. `adapter_roots()` is the right answer for running something
        // now and the wrong one for printing something to keep.
        let durable: &[PathBuf] = match resolved.source.path() {
            Some(_) => resolved.adapter_roots(),
            None => &[],
        };
        return Output::ok(
            json,
            describe_block(&needs, e.name.as_str(), "tool", &tool_facts(e, durable)),
        );
    }
    // One vocabulary: a corpus id is looked up in the same breath as a tool
    // name, because a user should not have to know which of the two a thing is
    // before they can ask about it.
    if let Some(c) = reg.corpora.get(name) {
        let needs = needs::of_corpus(c);
        let mut json = serde_json::to_value(c).unwrap_or(serde_json::Value::Null);
        if let Some(map) = json.as_object_mut() {
            map.insert("needs".into(), needs.to_json());
        }
        if toml_only {
            return raw_toml(c, json, &c.id);
        }
        return Output::ok(
            json,
            describe_block(&needs, c.id.as_str(), "corpus", &corpus_facts(c)),
        );
    }
    let known: Vec<_> = reg
        .entries
        .keys()
        .chain(reg.corpora.keys())
        .cloned()
        .collect();
    // A path here is the commonest way to reach this refusal with something
    // that is not a typo, because `score` and `verify` both take a directory
    // under a flag spelled almost the same way. Saying which of the two
    // vocabularies this command speaks costs one line and saves the reader
    // working it out from a list of names that does not contain theirs.
    let mut human = if Path::new(name).exists() {
        format!(
            "{name:?} is a path on this machine, and `describe` takes a \
             registered name or corpus id rather than a path. There is nothing \
             registered under that name.\nTo measure a directory of samples:\n  \
             stegobench score --corpus {name} --detector <name>\n"
        )
    } else {
        let close = near_registered_names(reg, name);
        let mut head = format!("nothing registered as {name:?}.");
        if !close.is_empty() {
            head.push_str(&format!(" Did you mean: {}?", close.join(", ")));
        }
        head.push('\n');
        head
    };
    human.push_str(&format!("Known tools and corpora: {}", known.join(", ")));
    Output::err(exit::USAGE, human)
}

/// The registered entry verbatim, and nothing else.
///
/// A serialisation failure is reported rather than papered over with the
/// `Debug` rendering the summary path once fell back to: this output exists to
/// be piped into a TOML parser, and handing that parser Rust's struct dump
/// under exit zero is the silent wrong answer the flag is least able to afford.
///
/// VERBATIM MEANS WHAT SOMEBODY WROTE, NOT WHAT THE TYPE DERIVES.
///
/// `may_be_served` is computed at the serialisation boundary so that the rule
/// deciding it lives in exactly one place, which is what a publishing script
/// reads out of `--json`. It is an answer about the entry rather than a line
/// in it, and this output is the line somebody wrote. Printing it here would
/// hand back a document with a key the author never typed, which anybody
/// round-tripping an entry would then commit.
fn raw_toml<T: serde::Serialize>(entry: &T, json: serde_json::Value, name: &str) -> Output {
    let rendered = toml::Value::try_from(entry).map(|mut value| {
        if let Some(table) = value.as_table_mut() {
            table.remove(stegobench_core::registry::MAY_BE_SERVED_KEY);
        }
        value
    });
    match rendered.and_then(|v| toml::to_string_pretty(&v)) {
        Ok(text) => {
            let mut out = Output::ok(json, text);
            out.payload_on_stdout = true;
            out
        }
        Err(e) => Output::err(
            exit::FAILURE,
            format!(
                "the registered entry for {name} could not be written back as \
                 TOML: {e}. `stegobench describe {name}` prints the summary, \
                 and `--json` prints the same entry as JSON."
            ),
        ),
    }
}

/// What `describe` prints: the readiness line, the facts, then what is needed.
///
/// One renderer for a tool and for a corpus, on purpose. They are different
/// types with different fields, and the thing a reader wants first is the same
/// for both: whether they can use it, and what to type if not.
///
/// The whole entry USED TO BE PRINTED UNDERNEATH, as TOML. That put forty
/// lines of registry between the reader and the next command, most of it
/// fields only the harness reads, and a first-time user reported reading past
/// the answer looking for it. The entry is still one flag away, under `--toml`,
/// which prints it alone so a pipe gets a document rather than a document with
/// prose around it.
/// The word in the brackets beside the name.
///
/// `needs::Readiness` answers one question, "can the harness drive this", and
/// its bare words answered a different one for the reader. A first-time user
/// read `[ready]` on a containerised detector as "ready for me to run", went
/// looking for `zsteg` on their PATH, and found nothing; and read the honest
/// `[unknown]` on the corpus that ships inside the binary as a fault.
///
/// So a tool says who it is ready FOR, and a corpus says whose copy is being
/// talked about, which for the starter corpus is this binary's own.
fn readiness_word(needs: &needs::Needs, kind: &str, name: &str) -> String {
    if kind == "corpus" && needs.readiness == needs::Readiness::Unknown {
        return if name == STARTER_ID {
            "included in this binary".to_string()
        } else {
            "bring your own copy".to_string()
        };
    }
    if needs.readiness == needs::Readiness::Ready {
        return "stegobench can run it".to_string();
    }
    if needs.readiness == needs::Readiness::CannotBeDriven {
        return "stegobench cannot drive it".to_string();
    }
    needs.readiness.word().to_string()
}

fn describe_block(
    needs: &needs::Needs,
    name: &str,
    kind: &str,
    facts: &[(&str, String)],
) -> String {
    let width = facts.iter().map(|(k, _)| k.len()).max().unwrap_or(0);
    let mut text = format!("{name}  [{}]\n", readiness_word(needs, kind, name));
    for (label, value) in facts {
        // A value of several lines keeps the column: the second line of a
        // command that wraps anyway must not read as the next field's value.
        let mut lines = value.lines();
        let first = lines.next().unwrap_or("");
        text.push_str(&format!("{label:<width$}  {first}\n", width = width));
        for line in lines {
            text.push_str(&format!("{:<width$}  {line}\n", "", width = width));
        }
    }
    let steps = needs.block();
    if steps.is_empty() {
        text.push_str(&format!(
            "\nNothing needed. `stegobench doctor` says whether this {kind} \
             works.\n"
        ));
    } else {
        // Two of the five states are properties of the entry or the machine
        // rather than a list of jobs, and "Needs from you:" over "nothing you
        // install will change that" asks the reader to resolve a
        // contradiction the heading created.
        let heading = match needs.readiness {
            needs::Readiness::CannotRunHere | needs::Readiness::CannotBeDriven => {
                "Why you can't use it here:"
            }
            _ => "Needs from you:",
        };
        text.push_str(&format!("\n{heading}\n{steps}\n"));
    }
    text.push_str(&format!(
        "\nThe registered entry in full: stegobench describe {name} --toml"
    ));
    text
}

/// One shell word, quoted if the shell would otherwise take it apart.
///
/// A registry lives wherever the reader installed it, and a path with a space
/// in it pasted unquoted runs a different command rather than failing.
fn shell_word(word: &str) -> String {
    let safe = |c: char| c.is_ascii_alphanumeric() || "_@%+=:,./-{}".contains(c);
    if !word.is_empty() && word.chars().all(safe) {
        return word.to_string();
    }
    format!("'{}'", word.replace('\'', r"'\''"))
}

/// The line that runs this tool on one image of the reader's own, exactly as
/// the harness would run it, with `{file}` left for them to fill in.
///
/// Built from the same pieces `stegobench_plugin::selftest` assembles, so the
/// sandbox flags, the mount, the entrypoint and the environment are the ones
/// a measured run really uses rather than a plausible set invented here. A
/// container reference carries its digest, so what this pastes is pinned.
///
/// `None` where no honest line exists: an entry with neither an image nor a
/// binary cannot be run at all, and an adapter that is not where the registry
/// says it is would paste a mount of a path that does not exist.
fn runnable_command(
    e: &stegobench_core::registry::Entry,
    adapter_roots: &[PathBuf],
) -> Option<String> {
    let invoke = e.invoke.as_ref();
    let argv = invoke.map(|i| i.argv.as_slice()).unwrap_or(&[]);

    // A service is reached by an adapter that runs here, so the image names
    // the subject and is not the thing to start.
    let host = invoke.is_some_and(|i| i.host);
    if host || e.image.is_none() {
        if let Some(bin) = e.binary.as_ref().filter(|b| !b.command.is_empty()) {
            let mut words: Vec<String> = bin.command.iter().map(|w| shell_word(w)).collect();
            words.extend(argv.iter().map(|a| shell_word(a)));
            return Some(words.join(" "));
        }
        let (Some(i), Some(rel)) = (invoke, invoke.and_then(|i| i.adapter.as_ref())) else {
            return None;
        };
        let adapter = stegobench_plugin::adapter::resolve(rel, adapter_roots).ok()?;
        let program = i
            .entrypoint
            .clone()
            .unwrap_or_else(|| stegobench_plugin::DEFAULT_HOST_ENTRYPOINT.to_string());
        let mut words = vec![shell_word(&program)];
        for arg in argv {
            words.push(shell_word(
                &arg.replace("{adapter}", &adapter.display().to_string()),
            ));
        }
        return Some(words.join(" "));
    }

    let image = e.image.as_ref()?;
    let i = invoke?;
    // The whole directory, writable, where the tool writes beside its input;
    // the one file, read only, otherwise. Same choice `selftest::run_one`
    // makes from the same flag.
    let mut words: Vec<String> = ["docker", "run", "--rm"]
        .iter()
        .map(|s| s.to_string())
        .collect();
    if i.writable_workdir {
        words.push("--user".into());
        words.push("$(id -u):$(id -g)".into());
    }
    // The harness's own list, not a copy of it. What makes this command worth
    // printing is that it is the command the harness runs.
    for flag in stegobench_plugin::selftest::SANDBOX {
        words.push((*flag).into());
    }
    words.push("-v".into());
    words.push(if i.writable_workdir {
        "\"$PWD\":/work".into()
    } else {
        "\"$PWD/{file}\":/work/{file}:ro".into()
    });

    let mut adapter_inner = String::new();
    if let Some(rel) = &i.adapter {
        let abs = stegobench_plugin::adapter::resolve(rel, adapter_roots).ok()?;
        let base = abs.file_name()?.to_str()?;
        adapter_inner = format!("/adapter/{base}");
        words.push("-v".into());
        words.push(shell_word(&format!("{}:{adapter_inner}:ro", abs.display())));
    }
    for kv in &i.env {
        words.push("-e".into());
        words.push(shell_word(kv));
    }
    if let Some(ep) = &i.entrypoint {
        words.push("--entrypoint".into());
        words.push(shell_word(ep));
    }
    words.push(shell_word(&image.reference));
    for arg in argv {
        words.push(shell_word(
            &arg.replace("{file}", "/work/{file}")
                .replace("{adapter}", &adapter_inner),
        ));
    }
    Some(words.join(" "))
}

/// The fields of a tool entry a person reads, in the order they ask for them.
///
/// A subset rather than everything: the rest is one `--toml` away, and a
/// summary that reprints every field is the dump it replaced.
fn tool_facts(
    e: &stegobench_core::registry::Entry,
    adapter_roots: &[PathBuf],
) -> Vec<(&'static str, String)> {
    let mut facts = vec![(
        "Kind",
        match e.kind {
            Kind::Detector => "detector".to_string(),
            Kind::Embedder => "embedder".to_string(),
        },
    )];
    facts.push(("Licence", e.licence.clone()));
    // SAID HERE BECAUSE THIS IS WHERE SOMEBODY LOOKS BEFORE PUBLISHING ONE.
    //
    // The licence answers whether you may run it; this answers whether we may
    // serve a copy, and the two are different questions. Printed only where the
    // entry says something, because a tool that is nothing but a local binary
    // has nothing to mirror and a blank line about it would be noise. The
    // reason is printed WITH the verdict rather than behind a flag: a verdict
    // whose reason you have to go and find is one people quote without it, and
    // the one entry here that says `mirrored by decision` is defensible only
    // because of what the reason says.
    if let Some(r) = e.redistribution {
        facts.push((
            "Republish",
            format!(
                "{}{}",
                stegobench_core::corpus::redistribution_label(r),
                match e.redistribution_reason.as_deref().map(str::trim) {
                    Some(why) if !why.is_empty() => format!(". {why}"),
                    _ => String::new(),
                }
            ),
        ));
    }
    let runs = match (&e.image, &e.binary) {
        (Some(i), _) => {
            let sandbox = if e.invoke.as_ref().is_some_and(|i| i.host) {
                "a service this only talks to, so its isolation is not ours to state"
            } else if i.needs_network {
                "a container, with network"
            } else {
                "a container, sandboxed with no network"
            };
            format!("{} ({sandbox})", i.reference)
        }
        (_, Some(b)) => format!(
            "{} (a program you installed, no sandbox, your network)",
            b.command.first().map(String::as_str).unwrap_or("a binary")
        ),
        _ => "nothing declared, so it cannot be run".to_string(),
    };
    facts.push(("Runs as", runs));
    // The command is in the summary rather than only in the entry because
    // `stegobench help scope` sends the reader who wants to examine their own
    // images here, to find the command that runs the tool directly. That
    // reader is the one least able to go looking for it under another flag.
    //
    // It used to be the raw `invoke.argv`, which for a container was an argv
    // that only means anything INSIDE the image (`zsteg -a {file}`, where
    // `zsteg` is on no host's PATH) and for a binary dropped the program name
    // the entry keeps in `binary.command` (`{file}`, alone). Both read as
    // something to paste and neither was.
    match runnable_command(e, adapter_roots) {
        Some(line) => {
            let containerised = e.image.is_some() && !e.invoke.as_ref().is_some_and(|i| i.host);
            let note = if !line.contains("{file}") {
                "It reads every image in the directory you run it from."
            } else if containerised {
                "{file} is your image's filename, and this runs from the directory it is in."
            } else {
                "{file} is the path to your image."
            };
            facts.push(("Run it", format!("{line}\n{note}")));
        }
        // The adapter is real and the command would be too, but the built-in
        // registry unpacks its adapters into a scratch directory that is
        // wiped when the command ends. A line naming that path pastes as a
        // file-not-found a minute later, which is worse than no line.
        None => {
            if let Some(adapter) = e.invoke.as_ref().and_then(|i| i.adapter.as_ref()) {
                facts.push((
                    "Run it",
                    format!(
                        "no command to paste: this one needs {adapter}, and \
                         the copy inside this binary lasts only as long as a \
                         run.\nWith a checkout of the repository: stegobench \
                         describe {} --registry plugins/registry",
                        e.name
                    ),
                ));
            }
        }
    }
    // An entry with no [emits] block gets the honest line rather than the
    // assumed one. A detector the host can drive is refused at load without
    // one, so this only reaches an entry nothing here launches.
    facts.push((
        "Answers",
        if !e.emits.declared() {
            "not declared: this entry has no [emits] block".to_string()
        } else {
            match e.emits.output {
                stegobench_core::registry::Output::Score => {
                    if e.emits.higher_means_stego {
                        "a score, higher means more like stego".to_string()
                    } else {
                        "a score, lower means more like stego".to_string()
                    }
                }
                stegobench_core::registry::Output::Verdict => {
                    "a verdict, yes or no, so it gives one point and not a curve".to_string()
                }
            }
        },
    ));
    if !e.accepts.formats.is_empty() {
        facts.push(("Reads", e.accepts.formats.join(", ")));
    }
    if let Some(s) = e.cost.seconds_per_image {
        facts.push(("Costs", format!("about {s} seconds an image")));
    }
    if let Some(u) = &e.upstream {
        facts.push(("Upstream", u.clone()));
    }
    if let Some(n) = &e.notes {
        facts.push(("Notes", n.clone()));
    }
    facts
}

/// The fields of a corpus entry a person reads, in the order they ask for them.
///
/// The licence block is not abbreviated to its identifier. Whether the terms
/// were READ, and on what date, and whether a stego image derived from them may
/// be published, are the three questions the registry exists to answer, and
/// each one has a field precisely so a reader never has to infer it.
fn corpus_facts(c: &stegobench_core::corpus::CorpusEntry) -> Vec<(&'static str, String)> {
    use stegobench_core::corpus::{LicenceStatus, Redistribution};
    let mut facts = vec![("Name", c.name.clone()), ("What", c.description.clone())];
    if let Some(t) = &c.tier {
        facts.push(("Tier", t.clone()));
    }
    let licence = match c.licence.status {
        LicenceStatus::Verified => {
            let named = c.licence.spdx.clone().unwrap_or_else(|| "verified".into());
            let inferred = if c.licence.spdx_version_inferred {
                ", version inferred rather than stated"
            } else {
                ""
            };
            match &c.licence.verified_on {
                Some(day) => format!("{named}, verified_on {day}{inferred}"),
                None => format!("{named}, verified{inferred}"),
            }
        }
        LicenceStatus::Unverified => {
            "UNVERIFIED: nobody has established the terms, so treat it as granting nothing"
                .to_string()
        }
        LicenceStatus::NoneGranted => {
            "NO LICENCE: there is no grant, which is not the same as none being found".to_string()
        }
    };
    facts.push(("Licence", licence));
    if let Some(src) = &c.licence.source {
        facts.push(("Read from", src.clone()));
    }
    facts.push((
        "Republish",
        format!(
            "{}: {}",
            match c.licence.redistribution {
                Redistribution::Permitted => "permitted",
                Redistribution::Forbidden => "forbidden",
                Redistribution::Unknown => "unknown, which any gate here treats as no",
                // Worded so a reader cannot take it for a grant. The reason
                // beside it is what says who decided and on what condition it
                // stops, which is the only thing that makes it defensible.
                Redistribution::MirroredByDecision =>
                    "mirrored by decision, with no grant behind it",
            },
            c.licence.redistribution_reason
        ),
    ));
    let size = match (c.properties.base_images, c.properties.total_images) {
        // IMAGES, NOT FILES. `total_images` is what the field holds and
        // "files" is what it used to print, so `describe` said 18 over a
        // corpus `fetch` correctly reports as 39 files: the same eighteen
        // images, their eighteen records, and three more beside them.
        (Some(b), Some(t)) => format!("{b} covers, {t} images in all"),
        (Some(b), None) => format!("{b} covers"),
        (None, Some(t)) => format!("{t} images"),
        (None, None) => match (&c.properties.size_mb, &c.properties.size_note) {
            (Some(mb), _) => format!("{mb} MB"),
            (None, Some(note)) => note.clone(),
            (None, None) => "size unestablished".to_string(),
        },
    };
    facts.push(("Size", size));
    if let Some(paired) = c.properties.paired {
        facts.push((
            "Paired",
            if paired {
                "yes, every stego image has its clean twin".to_string()
            } else {
                "NO, so it cannot answer the question this benchmark asks".to_string()
            },
        ));
    }
    if c.demonstration {
        facts.push((
            "Demonstration",
            "yes: enough to prove a detector runs, nowhere near enough to measure one".to_string(),
        ));
    }
    if let Some(cite) = &c.citation {
        facts.push(("Cite as", cite.clone()));
    }
    if let Some(n) = &c.notes {
        facts.push(("Notes", n.clone()));
    }
    facts
}

/// `fixtures` is `None` when the self-tests are not being run, which is the
/// only state in which no fixtures are resolved at all. Carrying the resolved
/// fixtures rather than just their directory is what lets the report say WHICH
/// ones answered: a stale `./fixtures` beside a checkout and the copy compiled
/// into the binary are different bytes, and a self-test result is about the
/// ones it actually read.
/// A sha256 digest written as lowercase hexadecimal.
fn is_sha256_hex(text: &str) -> bool {
    text.len() == 64 && text.bytes().all(|b| b.is_ascii_hexdigit())
}

/// The longest a version string is allowed to be before it stops being one.
const VERSION_MAX_CHARS: usize = 40;

/// What a tool printed when asked for its version, or the fact that it did not.
///
/// The probe runs whatever `version_args` says and keeps the first line of the
/// output, so a tool that does not understand those arguments answers with its
/// own error. A Python tool answered `Traceback (most recent call last):` and
/// that sat in the column every other row uses for a digest, under the word
/// `present`, with nothing saying the probe had failed. A version is short, on
/// one line, printable, and carries a digit; anything else is wreckage.
fn version_column(version: &str) -> String {
    let v = version.trim();
    let plausible = !v.is_empty()
        && v.chars().count() <= VERSION_MAX_CHARS
        && !v.chars().any(char::is_control)
        && v.chars().any(|c| c.is_ascii_digit());
    if plausible {
        v.to_string()
    } else {
        "version probe failed".to_string()
    }
}

/// The pin a presence check carries, rendered as the facts it actually holds.
///
/// Two different things arrive here. A container's pin is the image id, which
/// is a digest already. A binary's is the version the tool printed followed by
/// the sha256 of the bytes on disk, and those are a claim and a measurement
/// respectively. The `sha256:` prefix is what tells a reader which is which.
fn pin_columns(pin: &str) -> String {
    if let Some((version, hash)) = pin.rsplit_once(' ') {
        if let Some(short) = short_sha256(hash) {
            return format!("{}  {short}", version_column(version));
        }
    }
    short_sha256(pin).unwrap_or_else(|| pin.chars().take(23).collect())
}

/// A `sha256:...` digest cut to a length a person can compare by eye.
fn short_sha256(text: &str) -> Option<String> {
    let hex = text.strip_prefix("sha256:")?;
    is_sha256_hex(hex).then(|| format!("sha256:{}", &hex[..12]))
}

/// One `doctor` row, with the name padded to the widest in the table.
///
/// Rendered here rather than by `Availability::summary`, whose pad is a
/// constant sixteen: a name of exactly sixteen characters ate the gap and ran
/// into the word beside it, and no single row can know how wide the table is.
fn doctor_row(check: &availability::Availability, drivable: bool, width: usize) -> String {
    let state = match &check.presence {
        // PRESENT IS NOT THE SAME AS USABLE, AND THE STATE COLUMN IS WHERE A
        // READER STOPS. A forensic analyst read `present` beside an entry
        // nothing here can drive and had to work out from a parenthetical
        // further along the line that it was not available to them at all.
        // `describe` and `list` were corrected for this the same day and
        // `doctor` was missed.
        Presence::Present { .. } if !drivable => {
            "undrivable  nothing in its entry says what to launch".to_string()
        }
        Presence::Present { pin } => format!("present   {}", pin_columns(pin)),
        Presence::Absent { reason } => format!("MISSING   {reason}"),
        Presence::Unknown { reason } => format!("unknown   {reason}"),
        Presence::Unsupported { reason } => format!("n/a       {reason}"),
    };
    let verified = match check.verified {
        Some(true) => "  verified",
        Some(false) => "  SELF TEST FAILED",
        None => "  not verified",
    };
    let secrets = if check.missing_secrets.is_empty() {
        String::new()
    } else {
        format!("  needs {}", check.missing_secrets.join(", "))
    };
    format!(
        "{:<width$} {state}{verified}{secrets}",
        check.name,
        width = width
    )
}

fn cmd_doctor(
    resolved: &Resolved,
    fixtures: Option<&fixtures::Fixtures>,
    strict: bool,
    registry_reach: bool,
) -> Output {
    let reg = &resolved.registry;
    let no_selftest = fixtures.is_none();

    let mut rows = Vec::new();
    let (mut missing, mut broken, mut passed, mut skipped, mut answered) = (0, 0, 0, 0, 0);
    // Counted apart from `missing` on purpose. A tool that cannot run on this
    // operating system is not a thing the reader failed to install, and a
    // count that lumps the two together reads as a job of work waiting.
    let mut unsupported = 0;
    // Present, absent and "nobody can say" are three answers, and the third
    // one is a real state rather than a soft no. See the match below.
    let mut undetermined = 0;
    let mut present = 0;
    // Only ever non-zero with --registry-reach, and counted apart because a
    // registry that refused and a registry nobody asked are different states.
    let (mut unfetchable, mut unasked) = (0, 0);
    // The whole table's widest name, plus one, so the column after it always
    // has a gap. A constant pad closed up entirely on a name that reached it.
    let width = reg
        .entries
        .values()
        .map(|e| e.name.chars().count())
        .max()
        .unwrap_or(0)
        + 1;

    for entry in reg.entries.values() {
        let mut check = availability::check(entry, resolved.adapter_roots());
        // Only ask a tool to prove itself if its code is actually here. Running
        // a self-test against a missing image produces a failure that says
        // "broken" when the truth is "absent", and those need different fixes.
        let verdict = match fixtures {
            Some(f) if check.presence.is_present() => {
                selftest::run(entry, f.dir(), resolved.adapter_roots())
            }
            _ => Verified::Skipped("not attempted".into()),
        };
        check.verified = match &verdict {
            Verified::Passed => Some(true),
            Verified::Failed(_) => Some(false),
            Verified::Skipped(_) | Verified::Answered(_) => None,
        };
        match &check.presence {
            Presence::Unsupported { .. } => unsupported += 1,
            // Counted apart from `missing`, because it is not one. A service
            // whose address nobody has supplied is not a thing the reader
            // failed to install, and the summary line used to say "13 not
            // installed" over a registry where one of the thirteen needed an
            // `export` rather than a `docker pull`. That sends somebody
            // looking for a package that does not exist.
            Presence::Unknown { .. } => undetermined += 1,
            p if !p.is_present() => missing += 1,
            // Counted rather than left implicit, so the presence line adds up
            // to the total in front of it.
            _ => present += 1,
        }
        match &verdict {
            Verified::Passed => passed += 1,
            Verified::Failed(_) => broken += 1,
            Verified::Answered(_) => answered += 1,
            Verified::Skipped(_) => skipped += 1,
        }
        let row = doctor_row(&check, entry.can_be_driven(), width);
        let detail = match &verdict {
            Verified::Failed(why) => format!("{row}  ({why})"),
            Verified::Answered(why) => {
                format!("{}  ({why})", row.replace("not verified", "responded "))
            }
            // An undrivable entry's state column already says so, and
            // repeating the skip reason beside it printed the same fact
            // twice in one line.
            Verified::Skipped(_) if !entry.can_be_driven() => row,
            Verified::Skipped(why) if !no_selftest && check.presence.is_present() => {
                format!("{row}  ({why})")
            }
            _ => row,
        };
        // Off by default because it crosses a network, and a registry with
        // thirteen images to ask about is thirteen round trips. Only asked
        // where there is an image to ask about: a locally installed binary
        // has no registry and reporting "not asked" against it would be noise
        // about a question that does not apply.
        // Only where the image names the bytes that RAN. An entry with
        // `invoke.host` names a third party's public image and scores against
        // an instance somebody else started, so "fetchable" would be true and
        // would still invite the wrong inference: that fetching it gets you
        // what answered. `pinning` already calls that one unpinned.
        let detail = match (registry_reach, &entry.image) {
            (true, Some(image)) if needs::runs_in_container(entry) => {
                let said = match stegobench_plugin::registry_reach(&image.reference) {
                    stegobench_plugin::Reach::Fetchable => "fetchable".to_string(),
                    // Loud, because this is the one a reader is about to rely
                    // on without knowing. The whole check exists because the
                    // document said the digest could be pulled and it could
                    // not.
                    stegobench_plugin::Reach::Absent { reason } => {
                        unfetchable += 1;
                        format!("NOT FETCHABLE: {reason}")
                    }
                    stegobench_plugin::Reach::NotRun { reason } => {
                        unasked += 1;
                        format!("not asked: {reason}")
                    }
                };
                format!("{detail}\n    registry  {said}")
            }
            _ => detail,
        };
        // Computed from the availability answer already paid for, so nothing
        // here asks the container runtime a second time.
        let needs = needs::of_tool(entry, &check, resolved.adapter_roots());
        rows.push((check, verdict, detail, needs, entry.kind));
    }

    // One line per tool, and under the ones that need something, the lines to
    // type. Indented under their own tool rather than gathered at the bottom,
    // so a reader scanning thirteen rows finds the instruction beside the
    // problem rather than having to match names up afterwards.
    let mut human: Vec<String> = Vec::new();
    // First line, before any tool. Every row below is a fact about the
    // registry that answered, and a machine can easily hold two: a checkout,
    // an installed copy, and the one compiled into the binary. A reader
    // looking at a row they did not expect needs to know which one it came
    // from before they start looking for the tool.
    human.push(resolved.source.line());
    // And the same question about the fixtures, for the same reason: a
    // self-test verdict is about the bytes it read, and there is more than one
    // place those can come from.
    human.push(match fixtures {
        Some(f) => f.line(),
        None => "fixtures  not read, because the self-tests were not run".to_string(),
    });
    // Grouped under a heading naming the kind, because this screen and
    // `stegobench list detectors` are read one after the other and used to
    // disagree without explaining themselves: thirteen rows here, seven
    // there, and the word "embedder" never printed. A reader with no way to
    // tell which six were the difference reads it as something broken.
    let detectors = rows.iter().filter(|r| r.4 == Kind::Detector).count();
    let embedders = rows.len() - detectors;
    for (kind, heading, count) in [
        (Kind::Detector, "DETECTORS", detectors),
        (Kind::Embedder, "EMBEDDERS", embedders),
    ] {
        if count == 0 {
            continue;
        }
        human.push(String::new());
        human.push(format!("{heading} ({count})"));
        for (_, _, detail, needs, _) in rows.iter().filter(|r| r.4 == kind) {
            human.push(detail.clone());
            let block = needs.block();
            if !block.is_empty() {
                for line in block.lines() {
                    human.push(format!("  {line}"));
                }
            }
        }
    }
    let needing = rows
        .iter()
        .filter(|(_, _, _, n, _)| n.readiness == needs::Readiness::NeedsYou)
        .count();
    human.push(String::new());
    // TWO AXES, TWO LINES, AND EACH ONE SUMS TO THE TOTAL.
    //
    // These used to be one comma list: "13 tool(s): 6 verified, 0 answering,
    // 0 broken, 2 not installed, 1 undetermined, 7 not checked", which adds
    // up to 16. It was never wrong, it was two independent questions printed
    // as one list, and a reader adds a comma list up against the number in
    // front of it. Every bucket is printed even at zero, so both lines can
    // still be summed on a machine where one of them is empty.
    human.push(format!(
        "{} tool(s): {detectors} detector(s), {embedders} embedder(s).",
        rows.len()
    ));
    human.push(format!(
        "On this machine: {present} installed, {missing} not installed, \
         {undetermined} undetermined, {unsupported} cannot run here."
    ));
    human.push(format!(
        "Stegobench's own self-test: {passed} passed, {answered} responded, \
         {broken} failed, {skipped} not run. Passing says stegobench can \
         drive it, not that you can run it yourself."
    ));
    if undetermined > 0 {
        human.push(format!(
            "{undetermined} undetermined: nothing to install would settle it. \
             Its line says what it needs."
        ));
    }
    if unsupported > 0 {
        human.push(format!(
            "{unsupported} cannot run on {} at all, so nothing to install would change it.",
            std::env::consts::OS
        ));
    }
    if answered > 0 {
        human.push(
            "responded: it answered without settling either fixture, so nothing is proved.".into(),
        );
    }
    if skipped > 0 {
        // Never let "we did not look" read as "it is fine".
        human.push("not run is not the same as working; each line says why.".into());
    }
    if needing > 0 {
        human.push(format!(
            "{needing} need something from you, indented under each."
        ));
    }

    let json = serde_json::json!({
        "registry": resolved.to_json(),
        "fixtures": fixtures.map(|f| serde_json::json!({
            "source": f.source().tag(),
            "path": f.dir().display().to_string(),
        })),
        "checked": rows.len(),
        "detectors": detectors,
        "embedders": embedders,
        "needing_action": needing,
        "present": present,
        "verified": passed,
        "broken": broken,
        "missing": missing,
        "unsupported_here": unsupported,
        "undetermined": undetermined,
        "not_checked": skipped,
        "responded": answered,
        "tools": rows.iter().map(|(c, v, d, n, k)| serde_json::json!({
            "name": c.name,
            "kind": match k {
                Kind::Detector => "detector",
                Kind::Embedder => "embedder",
            },
            "present": c.presence.is_present(),
            "verified": c.verified,
            "status": match v {
                Verified::Passed => "passed",
                Verified::Failed(_) => "failed",
                Verified::Answered(_) => "responded",
                Verified::Skipped(_) => "not_checked",
            },
            "detail": d,
            "missing_secrets": c.missing_secrets,
            // The same field name and the same shape as `describe`'s, so a
            // script reads one structure whichever command it called.
            "needs": n.to_json(),
        })).collect::<Vec<_>>(),
    });

    let mut out = Output::ok(json, human.join("\n"));
    // The report IS the output, and it is the same report whether the verdict
    // is fit or unfit, so it goes to stdout under either exit code. Routing it
    // on the code instead sent `stegobench doctor > report.txt` to an empty
    // file on exactly the machine whose report was worth keeping.
    out.payload_on_stdout = true;
    // WHAT "UNFIT" MEANS, AND WHAT IT USED TO MEAN
    //
    // It used to mean any registered tool being absent, which no machine
    // will ever satisfy: thirteen tools are registered, most of them
    // containers, and a reader who has installed one detector and can
    // measure with it was told their machine was unfit and handed exit 8.
    // The front page recommends running this, so the first thing a newcomer
    // saw was a failure code for a working install, and anybody wiring it
    // into CI had to special case it.
    //
    // So the default now answers the question the command asks in its own
    // summary line, "can this machine run what it claims to": nothing usable
    // is unfit, and so is a tool that is installed and fails its own
    // self-test, because that one is not absent, it is lying. A tool you
    // simply have not installed is reported and is not a failure.
    //
    // `--strict` keeps the old meaning for the caller that wants it, which is
    // a release gate rather than a person at a terminal.
    // WITH `--no-selftest`, THE VERDICT IS ABOUT WHAT IS INSTALLED
    //
    // Fitness was read off the self-test results alone, and `--no-selftest`
    // runs none, so every machine came out with nothing usable: a box where
    // plain `doctor` exited 0 and four detectors had just scored was told it
    // was unfit and handed exit 8. The flag promises a faster report, not a
    // different answer, so what it cannot prove it does not judge, and the
    // question falls back to the one it can still answer.
    let strictly_short = strict && (missing > 0 || undetermined > 0);
    let usable = passed + answered;
    let unfit = if no_selftest {
        present == 0 || strictly_short
    } else {
        broken > 0 || usable == 0 || strictly_short
    };
    if unfit {
        out.code = exit::ENVIRONMENT_UNFIT;
    }
    if out.code == exit::ENVIRONMENT_UNFIT {
        // NOTHING INSTALLED YET IS NOT THE SAME STATE AS NOTHING WORKING, AND
        // ONE WORD WAS BEING USED FOR BOTH.
        //
        // A journey on 2026-10-02 put a reader who had installed the tool
        // five minutes earlier in front of "UNFIT: no tool here is usable",
        // in capitals, with exit 8. They read it as a verdict on their
        // computer. It is not: the machine is fine and has simply not fetched
        // an image yet, which is the expected state of every fresh install
        // and the one the front page sends people to this command from.
        //
        // The exit code stays 8, because nothing can be measured and a script
        // needs to know that. What changes is that the sentence says which
        // situation this is, and the one that is a normal first run carries
        // the way out of it rather than a verdict.
        let fresh = present == 0 && broken == 0 && !strictly_short;
        let why = if !no_selftest && broken > 0 {
            format!("{broken} installed tool(s) failed their own self-test")
        } else if fresh {
            "nothing is installed here yet, so there is nothing to measure \
             with. That is the normal state of a fresh install rather than a \
             fault on this machine"
                .to_string()
        } else if no_selftest && present == 0 {
            "no tool here is installed, so nothing could be measured".to_string()
        } else if !no_selftest && usable == 0 {
            "no tool here is usable, so nothing could be measured".to_string()
        } else {
            "--strict was given and something is missing or undetermined".to_string()
        };
        out.human.push_str(&format!("\n\nUNFIT: {why}."));
        if fresh {
            if let Some(way) = stegobench_cli::smallest_way_in(&resolved.registry) {
                out.human.push_str(&format!("\n\n{way}"));
            }
        }
    } else if no_selftest {
        out.human.push_str(&format!(
            "\n\nFIT: {present} tool(s) installed. The self-tests were \
             skipped, so none of them is proved to work."
        ));
    } else if missing > 0 || undetermined > 0 {
        out.human.push_str(
            "\n\nFIT: what is not installed is listed, not counted against \
             you. `--strict` counts it.",
        );
    }
    // After the fitness verdict, because it is a different question. A tool
    // can be installed, pass its self-test and produce a perfectly good
    // number that nobody else can ever reproduce, and that is not a fault in
    // this machine.
    if registry_reach && (unfetchable > 0 || unasked > 0) {
        if unfetchable > 0 {
            out.human.push_str(&format!(
                "\n\nNOT REPRODUCIBLE ELSEWHERE: {unfetchable} pinned image(s) \
                 are not in their registry. A result naming one pins the bytes \
                 that ran here and gives a reader no way to obtain them."
            ));
        }
        if unasked > 0 {
            out.human.push_str(&format!(
                "\n\n{unasked} image(s) could not be asked about. That is not \
                 the same as their being absent, and each line says why."
            ));
        }
    }
    out
}

/// Which of the three published schemas a document is judged against.
///
/// Dispatched on the document's own `schema` field rather than guessed from
/// its shape, so a document that claims to be a `result-v1` but is missing a
/// required field fails with a reason instead of being silently tried against
/// the wrong type.
fn cmd_validate(file: &Path) -> Output {
    let text = match std::fs::read_to_string(file) {
        Ok(t) => t,
        Err(e) => {
            return Output::err(
                exit::FAILURE,
                format!("cannot read {}: {e}", file.display()),
            )
        }
    };
    let sniff: serde_json::Value = match serde_json::from_str(&text) {
        Ok(v) => v,
        Err(e) => {
            return Output::err(
                exit::SCHEMA_INVALID,
                format!("{} is not valid JSON: {e}", file.display()),
            )
        }
    };
    let schema_field = sniff.get("schema").and_then(|v| v.as_str());

    macro_rules! validate_as {
        ($ty:ty, $label:literal) => {
            validate_as!($ty, $label, |_| None)
        };
        ($ty:ty, $label:literal, $seal:expr) => {{
            let parsed: $ty = match serde_json::from_str(&text) {
                Ok(v) => v,
                Err(e) => {
                    return Output::err(
                        exit::SCHEMA_INVALID,
                        format!("{} is not a {} document: {e}", file.display(), $label),
                    )
                }
            };
            match parsed.validate() {
                Ok(()) => {
                    let mut json = serde_json::json!({ "ok": true, "schema": parsed.schema });
                    let mut human = format!("{} is a valid {} document", file.display(), $label);
                    let seal: Option<(&'static str, serde_json::Value)> = ($seal)(&parsed);
                    if let Some((line, detail)) = seal {
                        json["seal"] = detail;
                        human.push('\n');
                        human.push_str(line);
                    }
                    Output::ok(json, human)
                }
                Err(problems) => {
                    let mut out = Output::err(
                        exit::SCHEMA_INVALID,
                        format!(
                            "{} is not valid:\n  {}",
                            file.display(),
                            problems.join("\n  ")
                        ),
                    );
                    out.json = serde_json::json!({ "ok": false, "problems": problems });
                    out
                }
            }
        }};
    }

    // A result's seal deliberately leaves the two timing fields out, so that
    // two runs differing only in when they happened seal identically. The
    // consequence is that an unqualified "valid" would be read as vouching for
    // a date this command never checked, which is the reading a forensic
    // examiner acts on. So the success line names the gap instead.
    let seal_note = |r: &Result1| -> Option<(&'static str, serde_json::Value)> {
        Some(if r.content_digest.is_some() {
            (
                "Its seal leaves out started_utc and elapsed_seconds, so a \
                 changed timestamp wouldn't show up here.",
                serde_json::json!({
                    "sealed": true,
                    "excluded": ["provenance.started_utc", "provenance.elapsed_seconds"],
                }),
            )
        } else {
            (
                "It carries no content digest, so nothing here checked whether \
                 it changed after it was written.",
                serde_json::json!({ "sealed": false }),
            )
        })
    };

    match schema_field {
        Some(s) if s.starts_with("stegobench/result-v") => {
            validate_as!(Result1, "result-v1", seal_note)
        }
        Some(s) if s.starts_with("stegobench/run-v") => validate_as!(RunV1, "run-v1"),
        Some(s) if s.starts_with("stegobench/manifest-v") => {
            validate_as!(ManifestV1, "manifest-v1")
        }
        Some(other) => Output::err(
            exit::SCHEMA_INVALID,
            format!(
                "{} declares schema {other:?}, which this version does not \
                 know. Known: {}",
                file.display(),
                KNOWN_SCHEMAS.join(", ")
            ),
        ),
        None => Output::err(
            exit::SCHEMA_INVALID,
            format!(
                "{} has no \"schema\" field, so it cannot be dispatched to a \
                 validator. Known: {}",
                file.display(),
                KNOWN_SCHEMAS.join(", ")
            ),
        ),
    }
}

/// Re-checks a result against the corpus it claims to have measured.
///
/// The claim a result makes about its corpus is the one thing a reader cannot
/// check by reading the document: every other field describes the run, and this
/// one describes bytes somewhere else. So it is recomputed here rather than
/// compared with itself.
///
/// Two checks, and the second is the one with teeth. The digest names what
/// the corpus's own records declare about their images, so a match proves the
/// document and the corpus describe the same manifest and nothing more: swap
/// a stego image for an easier one and leave its record alone, and every
/// digest still agrees while the number is now about different bytes. So
/// unless the caller passes `shallow`, every image is re-read and checked
/// against the digest its record states before this says the bytes are the
/// bytes.
fn cmd_verify(file: &Path, corpus: &Path, records: Option<&Path>, shallow: bool) -> Output {
    let text = match std::fs::read_to_string(file) {
        Ok(t) => t,
        Err(e) => {
            return Output::err(
                exit::FAILURE,
                format!("cannot read {}: {e}", file.display()),
            )
        }
    };
    let result: Result1 = match serde_json::from_str(&text) {
        Ok(v) => v,
        Err(e) => {
            return Output::err(
                exit::SCHEMA_INVALID,
                format!(
                    "{} is not a result-v1 document: {e}. `stegobench validate \
                     {}` says what is wrong with it",
                    file.display(),
                    file.display()
                ),
            )
        }
    };

    // Before anything about the corpus, because this asks whether the document
    // is the one that was written rather than whether the bytes it names are
    // the ones that were measured. A document edited after the fact cannot be
    // trusted to say which corpus to go and look at.
    //
    // What it computed is deliberately NOT printed. See the same check in
    // `result-v1`'s own validation: printing it turns the refusal into an
    // oracle that hands a forger the one thing they lack.
    if let Some(claimed) = result.content_digest.as_deref() {
        if claimed != result.compute_content_digest() {
            return Output::err(
                exit::VERIFY_MISMATCH,
                format!(
                    "{} does not match its own content digest, which it \
                     declares as\n  {claimed}\nEvery field except the two that \
                     record WHEN the run happened is covered, so something in \
                     this document changed after it was written. Re-run the \
                     measurement rather than trusting the number in it",
                    file.display()
                ),
            );
        }
    }

    // Also about the document rather than the corpus, so it sits with the
    // seal check and before any image is read. Only when asked for: a reader
    // who was not given the per-item scores still gets the corpus checks, and
    // silently skipping a file they did pass would be the worse failure.
    if let Some(path) = records {
        let Some(claimed) = result.provenance.records.as_ref() else {
            return Output::err_because(
                exit::VERIFY_MISMATCH,
                "records-unrecorded",
                format!(
                    "{} records no digest for its per-item scores, so {} \
                     cannot be checked against it. The document was written \
                     before this was recorded, or by a run that wrote no \
                     records file",
                    file.display(),
                    path.display()
                ),
            );
        };
        match stegobench_core::result::records_digest(path) {
            Err(e) => {
                return Output::err(
                    exit::FAILURE,
                    format!("cannot read {}: {e}", path.display()),
                )
            }
            Ok(found) if found.digest != claimed.digest => {
                return Output::err_because(
                    exit::VERIFY_MISMATCH,
                    "records-mismatch",
                    format!(
                        "{} is not the scores {} was computed from.\n  the \
                         document names {} over {} record(s)\n  that file is \
                         {} over {}\nThe metrics in the document \
                         were computed from a different file, so re-deriving \
                         them from this one gives different numbers",
                        path.display(),
                        file.display(),
                        claimed.digest,
                        claimed.count,
                        found.digest,
                        found.count
                    ),
                );
            }
            Ok(_) => {}
        }
    }

    if result.corpus.digest.is_empty() {
        return Output::err(
            exit::VERIFY_MISMATCH,
            format!(
                "{} names no corpus digest, so there is nothing to check it \
                 against. A corpus whose records state no digests produces a \
                 result that cannot be verified this way",
                file.display()
            ),
        );
    }

    let found = match score::corpus_digest(corpus) {
        Ok(Some(d)) => d,
        Ok(None) => {
            return Output::err(
                exit::VERIFY_MISMATCH,
                format!(
                    "the corpus at {} cannot be named: at least one record \
                     states no digest for its own image",
                    corpus.display()
                ),
            )
        }
        // The exit code is deliberately unchanged: `verify` has always
        // answered a corpus it could not read with a generic failure, and the
        // stable word is an addition beside it rather than a reclassification.
        Err(e) => return Output::err_because(exit::FAILURE, e.reason(), e.to_string()),
    };

    let claimed = &result.corpus.digest;
    let mut json = serde_json::json!({
        "ok": &found == claimed,
        "claimed": claimed,
        "found": found,
        "corpus": corpus.display().to_string(),
        "checked": if shallow { "records" } else { "bytes" },
    });
    if &found == claimed {
        // The records agree. That is necessary and it is not sufficient: the
        // digest is over what the records state, so images can be swapped
        // under records that still agree with each other. Unless the caller
        // asked for the cheap check, re-read the bytes before saying they are
        // the bytes.
        if !shallow {
            let (bad, read) = match score::rehash_corpus(corpus, |line| {
                eprintln!("verify: {line}");
            }) {
                Ok(v) => v,
                Err(e) => return Output::err_because(exit::FAILURE, e.reason(), e.to_string()),
            };
            if !bad.is_empty() {
                json["ok"] = serde_json::Value::Bool(false);
                json["mismatched"] = serde_json::json!(bad
                    .iter()
                    .map(|m| serde_json::json!({
                        "id": m.id,
                        "claimed": m.claimed,
                        "found": match &m.found {
                            Ok(f) => serde_json::json!(f),
                            Err(e) => serde_json::json!({ "error": e }),
                        },
                    }))
                    .collect::<Vec<_>>());
                let mut lines = String::new();
                for m in &bad {
                    let got = match &m.found {
                        Ok(f) => f.clone(),
                        Err(e) => format!("could not be read: {e}"),
                    };
                    lines.push_str(&format!(
                        "\n  {} claims {}\n    and is    {}",
                        m.id, m.claimed, got
                    ));
                }
                let mut out = Output::err(
                    exit::VERIFY_MISMATCH,
                    format!(
                        "the records at {} match the document, and the images \
                         do not match the records.{lines}\nThe corpus digest \
                         is computed from what the records state, so it still \
                         agrees; the bytes that were scored are not the bytes \
                         here now. That number cannot be attributed to this \
                         corpus.",
                        corpus.display()
                    ),
                );
                out.json = json;
                return out;
            }
            // A TRUNCATED RUN IS NOT A MEASUREMENT OF THIS CORPUS
            //
            // The digest covers the corpus manifest, so a run that scored
            // eight images of eighteen produces a digest identical to the
            // full run's and reaches this line. Saying "was measured on the
            // corpus ... all 18 image(s)" about it is an affirmative false
            // statement from the one command whose job is refusing those, and
            // exit 0 is what a reviewer's script reads.
            //
            // `--limit` would otherwise be a free dial: sweep it, keep the
            // value that flatters, and the benchmark certifies the answer.
            if let Some(total) = result.corpus.of_items {
                let scored = result.metrics.n_clean + result.metrics.n_stego;
                let mut out = Output::err_because(
                    exit::VERIFY_MISMATCH,
                    "partial-run",
                    format!(
                        "{} scored {scored} of the {total} item(s) this corpus \
                         holds, so it is not a measurement of it. The digests \
                         agree because a digest names the corpus and not the \
                         part of it that was read. Score the whole corpus \
                         before quoting a number for it.",
                        file.display()
                    ),
                );
                out.json = json;
                return out;
            }
            // WHAT THIS SENTENCE MAY CLAIM.
            //
            // It said the document "was measured on" the corpus, which is
            // more than anything here checked. Every check above is about the
            // CORPUS: that the document names these bytes, and that the bytes
            // are what their records say. Nothing reads the scores, and
            // nothing could: they are a text file on the machine that ran it,
            // and whoever ran it could write anything into it and re-run.
            //
            // A reviewer fabricated a perfect AUC by editing that file and
            // this command exited 0 over it, correctly by its own rules and
            // under a sentence that read as an endorsement of the number.
            // `declarations.self_reported` is what actually speaks to who
            // produced the scores, and the submission path is what clears it.
            let mut out = Output::ok(
                json,
                format!(
                    "{} names the corpus at {}. Both name {claimed}, and all \
                     {read} image(s) hash to what their records state.\nThat \
                     is a check on the corpus, not on the scores: nothing here \
                     read them, and `declarations.self_reported` says who \
                     produced them",
                    file.display(),
                    corpus.display()
                ),
            );
            out.code = exit::OK;
            return out;
        }
        // The same refusal as the deep path: --shallow says the bytes were
        // not re-read, not that a partial run counts as a whole one.
        if let Some(total) = result.corpus.of_items {
            let scored = result.metrics.n_clean + result.metrics.n_stego;
            let mut out = Output::err_because(
                exit::VERIFY_MISMATCH,
                "partial-run",
                format!(
                    "{} scored {scored} of the {total} item(s) this corpus \
                     holds, so it is not a measurement of it. Score the whole \
                     corpus before quoting a number for it.",
                    file.display()
                ),
            );
            out.json = json;
            return out;
        }
        let mut out = Output::ok(
            json,
            format!(
                "{} names the same records as the corpus at {}. Both name \
                 {claimed}.\nThe images were NOT re-read, because --shallow \
                 was given, so this says the two describe the same list of \
                 records rather than the same bytes. Nothing here read the \
                 scores either",
                file.display(),
                corpus.display()
            ),
        );
        out.code = exit::OK;
        out
    } else {
        let mut out = Output::err(
            exit::VERIFY_MISMATCH,
            format!(
                "{} and the corpus at {} are not about each other.\n  \
                 the document claims {claimed}\n  the corpus is    {found}\n\
                 That number was not measured on these images.",
                file.display(),
                corpus.display()
            ),
        );
        out.json = json;
        out
    }
}

/// The metrics over one set of scores and labels.
///
/// The refusal carries a stable `reason` word beside the message. A caller
/// driving this from another language has to tell "one class only", which is a
/// fact about its corpus, from "that file is not JSON", which is its own bug,
/// and matching on English prose to do it is how a caller stops noticing the
/// difference the first time the wording improves.
fn cmd_metrics(file: Option<&Path>, at: &[String]) -> Output {
    // `-` is the conventional spelling for standard input, and a file actually
    // called `-` is not worth the ambiguity.
    let file = file.filter(|p| p.as_os_str() != "-");
    match metrics::run(file, at) {
        Ok(report) => Output::ok(report.to_json(), report.human()),
        Err(e) => {
            let mut out = Output::err(e.exit_code(), e.to_string());
            out.json = serde_json::json!({
                "ok": false,
                "reason": e.reason(),
                "error": e.to_string(),
            });
            out
        }
    }
}

fn cmd_completions(shell: clap_complete::Shell) -> Output {
    let mut cmd = Cli::command();
    let name = cmd.get_name().to_string();
    let mut buf = Vec::new();
    clap_complete::generate(shell, &mut cmd, name, &mut buf);
    // Not `unwrap_or_default()`: an empty string is a completion script that
    // silently does nothing, written to a shell's completion directory with an
    // exit code of zero. A generator that produced bytes we cannot read has to
    // say so.
    let script = match String::from_utf8(buf) {
        Ok(s) => s,
        Err(e) => {
            return Output::err(
                exit::FAILURE,
                format!(
                    "the {shell} completion generator produced {} bytes that \
                     are not valid UTF-8. This is a bug in stegobench; please \
                     report it with this message: {e}",
                    e.as_bytes().len()
                ),
            )
        }
    };
    if script.trim().is_empty() {
        return Output::err(
            exit::FAILURE,
            format!(
                "the {shell} completion generator produced an empty script, \
                 which would complete nothing. Refused rather than written"
            ),
        );
    }
    Output::ok(
        serde_json::json!({ "shell": shell.to_string(), "script": script }),
        script,
    )
}

fn cmd_help(topic: Option<&str>) -> Output {
    match topic {
        None => {
            let human = format!(
                "Known topics: {}\n\nExample:\n  stegobench help pairing",
                help_topics::TOPICS.join(", ")
            );
            Output::ok(serde_json::json!({ "topics": help_topics::TOPICS }), human)
        }
        Some(t) => match help_topics::text(t) {
            Some(text) => Output::ok(
                serde_json::json!({ "topic": t, "text": text }),
                text.to_string(),
            ),
            None => Output::err(
                exit::USAGE,
                format!(
                    "no help topic {t:?}. Known: {}",
                    help_topics::TOPICS.join(", ")
                ),
            ),
        },
    }
}

/// What a run would cost, without running it.
///
/// It takes the command as you would type it, rather than its own flags, so
/// there is no second set of arguments to keep in step with `score`. The same
/// parser reads both, which means a plan cannot silently describe a different
/// run from the one that would happen.
fn cmd_plan(resolved: &Resolved, command: &[String]) -> Output {
    if command.is_empty() {
        return Output::err(
            exit::USAGE,
            "plan takes the command you would run, for example:\n  \
             stegobench plan score --corpus ./pentimento-nano --detector zsteg"
                .to_string(),
        );
    }
    let argv = std::iter::once("stegobench".to_string()).chain(command.iter().cloned());
    let parsed = match Cli::try_parse_from(argv) {
        Ok(c) => c,
        Err(e) => {
            return Output::err(
                exit::USAGE,
                format!("that is not a command this can plan:\n{e}"),
            )
        }
    };
    let Some(Command::Score {
        corpus,
        detector,
        corpus_id,
        limit,
        timeout,
        out,
        records,
        split,
        trained_on,
        ..
    }) = &parsed.command
    else {
        return Output::err(
            exit::USAGE,
            "only `score` can be planned today.".to_string(),
        );
    };

    let reg = &resolved.registry;
    let entries = match resolve_detectors(reg, detector) {
        Ok(e) => e,
        Err(o) => return o,
    };

    // The same refusals `score` gives, in the same order, because a plan that
    // accepts a command `score` would reject has answered a question nobody
    // can act on.
    if let Some(refused) = corpus_argument_problem(Some(reg), corpus) {
        return refused;
    }
    for (flag, path) in [("--out", out.as_deref()), ("--records", records.as_deref())] {
        if let Some(why) = output_inside_corpus(corpus, flag, path) {
            return Output::err_because(exit::USAGE, "output-inside-corpus", why);
        }
    }

    // The same teaching refusal `score` gives, for the same reason: somebody
    // asking what a run would cost over their own photographs is on the wrong
    // side of what this measures, and "no record beside DSC_0001" does not
    // tell them so.
    if let Some(why) = unlabelled_corpus(corpus) {
        return Output::err_because(exit::PREFLIGHT_REFUSED, "corpus-unlabelled", why);
    }

    // Asked before the corpus is walked, in the same position `score` asks
    // it, so a plan over nothing that can run refuses with the same word and
    // the same code rather than printing a table of zeroes.
    let blocked: Vec<Option<String>> = entries
        .iter()
        .map(|e| stegobench_cli::unavailable_reason(e, resolved.adapter_roots()))
        .collect();
    if blocked.iter().all(Option::is_some) {
        return Output::err_because(
            exit::PREFLIGHT_REFUSED,
            "nothing-available",
            nothing_available(&entries, &blocked),
        );
    }

    // THE SAME PRE-FLIGHT `score` RUNS, RATHER THAN A COUNT OF ITS OWN
    //
    // This used to walk the corpus itself and count scorable samples, which
    // answered "how many" and nothing else. Everything the walk could also
    // have said was left for `score` to say later, which is backwards for a
    // command whose whole job is telling you things before you spend the time:
    // a corpus of 120 dimension-mismatched pairs planned silently, and `score`
    // then found it instantly and statically from the same records.
    //
    // `registered` is deliberately `None`, so this does the record pass and
    // not the digest-and-rehash pass. Naming the corpus is `plan_configuration`'s
    // question and it answers it from the registry alone, which is what keeps
    // a plan cheaper than the run it describes.
    let mut notes: Vec<String> = Vec::new();
    let prepared = match score::prepare(
        corpus,
        None,
        *limit,
        trained_on.as_deref(),
        split.as_deref(),
        |line: &str| notes.push(line.to_string()),
    ) {
        Ok(p) => p,
        Err(e) => return score_refusal(&e, *limit),
    };
    let items = prepared.items();

    // What the run would be WORTH, beside what it would cost. A plan that
    // reports six hours and omits that the result will be `custom` has
    // answered half the question somebody asks before committing six hours.
    let (configuration, why) = plan_configuration(reg, corpus_id.as_deref(), limit.is_some());

    // WHAT `score` WOULD ACTUALLY RUN, NOT WHAT WAS ASKED FOR
    //
    // `plan ... --detector all` estimated seven detectors and `score` then
    // ran four, because this never asked the availability question `score`
    // asks. The three it could not run were not merely absent from the
    // total: they were absent from the screen, so the one command whose job
    // is to say what a run will cost overstated it and hid three blockers a
    // reader could have fixed before starting.
    let mut per_detector = Vec::new();
    let mut lines = Vec::new();
    // Summed only over the detectors that declare a rate. A total that
    // silently treated an unmeasured tool as free would be the plan lying
    // about the one thing it is for, so the count of unestimated ones is
    // carried beside the total rather than folded into it.
    let mut total_seconds = 0.0f64;
    let mut unestimated = 0usize;
    let mut unavailable = 0usize;
    let mut runnable = 0usize;
    for (entry, blocked) in entries.iter().zip(&blocked) {
        let per_image = entry.cost.seconds_per_image;
        // A rate over a detector that will not start is arithmetic about
        // nothing, so it is neither summed nor counted as unestimated.
        let seconds = match blocked {
            Some(_) => None,
            None => per_image.map(|s| s * items as f64),
        };
        match blocked {
            Some(_) => unavailable += 1,
            None => {
                runnable += 1;
                match seconds {
                    Some(s) => total_seconds += s,
                    None => unestimated += 1,
                }
            }
        }
        let duration = match (blocked, seconds) {
            (Some(why), _) => format!("NOT AVAILABLE: {why}"),
            (None, Some(s)) => format!("at least {}", stegobench_cli::human_duration(s)),
            (None, None) => "unknown: it declares no seconds_per_image".to_string(),
        };
        lines.push(format!("{:<16} {duration}", entry.name));
        per_detector.push(serde_json::json!({
            "detector": entry.name,
            "available": blocked.is_none(),
            "unavailable_reason": blocked,
            "seconds_per_image": per_image,
            "estimated_seconds": seconds,
        }));
    }

    // One JSON line per answer, measured at roughly sixty bytes on the real
    // records this writes, and one records file per detector that will run.
    let records_mb = (items as f64 * 60.0 * runnable as f64) / 1_048_576.0;
    let worst_case_seconds = items * timeout * runnable as u64;

    let mut value = serde_json::Map::new();
    value.insert("items".into(), serde_json::json!(items));
    value.insert(
        "detectors".into(),
        serde_json::json!(entries.iter().map(|e| &e.name).collect::<Vec<_>>()),
    );
    value.insert("per_detector".into(), serde_json::json!(per_detector));
    value.insert("runnable_detectors".into(), serde_json::json!(runnable));
    value.insert(
        "unavailable_detectors".into(),
        serde_json::json!(unavailable),
    );
    value.insert("estimated_seconds".into(), serde_json::json!(total_seconds));
    value.insert(
        "unestimated_detectors".into(),
        serde_json::json!(unestimated),
    );
    value.insert("records_mb".into(), serde_json::json!(records_mb));
    value.insert("configuration".into(), serde_json::json!(configuration));
    value.insert(
        "worst_case_seconds".into(),
        serde_json::json!(worst_case_seconds),
    );
    // So a caller cannot mistake the total for something this machine
    // measured. Nothing here times a detector, and nothing yet replaces a
    // declared rate with a measured one.
    value.insert("rate_source".into(), serde_json::json!("declared"));
    value.insert("estimate_is_a_lower_bound".into(), serde_json::json!(true));
    value.insert("corpus_notes".into(), serde_json::json!(notes));

    let mut human = format!("{items} item(s) to score with each of {runnable} detector(s):\n");
    human.push_str(&lines.join("\n"));
    let estimated = runnable - unestimated;
    if estimated == 0 {
        // "Total: about 0 seconds" over a set where nothing could be estimated
        // is a number that reads as free and means nothing was measured. No
        // total is the honest output.
        human.push_str(
            "\n\nNo total: none of these declares a seconds_per_image. The \
             worst case below is the only bound there is.",
        );
    } else {
        human.push_str(&format!(
            "\n\nTotal          at least {}, over the {estimated} that declare \
             a rate",
            stegobench_cli::human_duration(total_seconds),
        ));
        if unestimated > 0 {
            // A total that reads as the whole job when it covers five of seven
            // is worse than no total.
            human.push_str(&format!(
                "\n               {unestimated} declare none, so the real \
                 total is larger"
            ));
        }
    }
    human.push_str(&format!(
        "\nRecords        about {records_mb:.1} MB\nWorst case     \
         {}\nConfiguration  {configuration}: {why}",
        stegobench_cli::human_duration(worst_case_seconds as f64)
    ));
    if unavailable > 0 {
        human.push_str(&format!(
            "\n\n{unavailable} of {} asked for cannot run here and are left \
             out of every figure above. `stegobench doctor` says what each \
             one needs.",
            entries.len()
        ));
    }

    // One line, because the reasoning behind it is on `plan --help` and a
    // reader who wants it can ask. What cannot be left out is the claim: a
    // total presented without the word "declared" reads as a measurement,
    // and saying "about" in front of a figure that is systematically low is
    // the plan being confidently wrong.
    if estimated > 0 {
        human.push_str(
            "\n\nRates are declared by each registry entry, not measured, and \
             exclude per-image start-up. Read the total as a floor. \
             `stegobench plan --help` says why.",
        );
    }
    if !notes.is_empty() {
        human.push_str("\n\nAbout this corpus:\n  ");
        human.push_str(&notes.join("\n  "));
    }

    Output::ok(serde_json::Value::Object(value), human)
}

/// Whether the run being planned would earn a `named` result, and why.
///
/// Answered from the registry alone, without hashing the corpus. A plan is
/// meant to be cheap, and the expensive half of the real check is comparing a
/// digest this has deliberately not computed. So a run this calls `named` is
/// one that COULD be named, and `score` still has to agree.
fn plan_configuration(
    reg: &Registry,
    corpus_id: Option<&str>,
    limited: bool,
) -> (&'static str, String) {
    let Some(id) = corpus_id else {
        return (
            "custom",
            "no --corpus-id, so nothing to check this directory against".into(),
        );
    };
    let Some(entry) = reg.corpora.get(id) else {
        return (
            "custom",
            format!("no corpus with id {id:?} is registered, so `score` would refuse this"),
        );
    };
    if entry
        .integrity
        .as_ref()
        .and_then(|i| i.records_sha256.as_deref())
        .is_none()
    {
        return (
            "custom",
            format!("{id} declares no records digest, so there is nothing to check against"),
        );
    }
    if limited {
        return (
            "custom",
            "--limit scores part of the corpus, and a prefix of a tier is not the tier".into(),
        );
    }
    (
        "named",
        format!(
            "{id} declares a records digest. `score` checks this directory \
             against it and refuses if it disagrees"
        ),
    )
}

/// What happened to one detector of a `score` command.
///
/// Three outcomes, kept apart because they call for different things from the
/// reader and because two of them must never be read as the third. A skipped
/// detector is not a measured one that scored badly; it is a number that does
/// not exist.
enum Outcome {
    Measured {
        result: Box<Result1>,
        tally: stegobench_plugin::runner::Tally,
        written: Option<PathBuf>,
    },
    /// Not available on this machine, so nothing ran. Not fatal to the others.
    Skipped { why: String },
    /// It was available and the run did not produce a usable result.
    ///
    /// `reason` is the stable word, carried beside the prose so a single
    /// detector's failure can publish it the way `metrics` does.
    Failed {
        why: String,
        code: i32,
        reason: &'static str,
    },
}

/// Which detectors a `--detector` list names, in a deterministic order.
///
/// `all` is the whole registry's detectors rather than every entry: an
/// embedder cannot be asked to tell two images apart, and quietly including
/// one so it could be refused a moment later would be a worse answer than not
/// including it. Named explicitly, an embedder is still refused, because
/// somebody who typed its name has asked a question and is owed the answer.
fn resolve_detectors<'a>(
    reg: &'a Registry,
    asked: &[String],
) -> Result<Vec<&'a stegobench_core::registry::Entry>, Output> {
    if asked.iter().any(|d| d == "all") {
        if asked.len() > 1 {
            return Err(Output::err_because(
                exit::USAGE,
                "all-with-others",
                "`--detector all` already means every registered detector. Use \
                 `all` on its own, or list the ones you want."
                    .to_string(),
            ));
        }
        let mut all = reg.of_kind(Kind::Detector);
        // Sorted by name, so two runs of the same command write the same files
        // in the same order however the registry happened to be read.
        all.sort_by(|a, b| a.name.cmp(&b.name));
        if all.is_empty() {
            return Err(Output::err_because(
                exit::USAGE,
                "no-detectors-registered",
                "no detectors are registered, so `--detector all` has nothing \
                 to score with. `stegobench list detectors` says where it \
                 looked."
                    .to_string(),
            ));
        }
        return Ok(all);
    }

    let mut seen = std::collections::BTreeSet::new();
    let mut out = Vec::new();
    for name in asked {
        let Some(entry) = reg.entries.get(name) else {
            return Err(Output::err_because(
                exit::USAGE,
                "unknown-detector",
                format!(
                    "no tool named {name:?} is registered. \
                     `stegobench list detectors` shows what is."
                ),
            ));
        };
        // Refused here rather than after the corpus is walked. An embedder
        // hides a payload; it cannot be asked to tell two images apart, and
        // somebody who typed its name has asked for something impossible
        // rather than something unavailable.
        if entry.kind == Kind::Embedder {
            return Err(Output::err_because(
                exit::USAGE,
                "not-a-detector",
                format!(
                    "{name} is an embedder: it hides payloads, it cannot tell \
                     two images apart. `stegobench list detectors` shows what \
                     can."
                ),
            ));
        }
        // A name given twice is one measurement, not two. Silently scoring it
        // twice would write the second run over the first and report two.
        if seen.insert(name.clone()) {
            out.push(entry);
        }
    }
    Ok(out)
}

/// How many directory entries the shape check is allowed to look at.
///
/// It runs before a command that may take hours, so it has to be cheap and
/// bounded rather than correct on a pathological tree. Enough to be sure about
/// a folder of holiday photographs, and it gives up quietly on anything
/// larger, which then meets the ordinary corpus errors as before.
const SHAPE_CHECK_ENTRIES: usize = 4096;

/// Why this path cannot be a corpus, in the words a user can act on.
///
/// `None` means the path is a directory this process can list, which is all
/// that is checked here: whether what is inside it is a corpus is the loader's
/// question and it answers it far better than a pre-flight could.
fn corpus_path_problem(corpus: &Path) -> Option<String> {
    let shown = corpus.display();
    match std::fs::metadata(corpus) {
        Ok(meta) if meta.is_dir() => match std::fs::read_dir(corpus) {
            Ok(_) => None,
            Err(e) => Some(format!(
                "{shown} cannot be listed: {}.\n\
                 Check the permissions on it, or point at a copy you can read.",
                plain(&e)
            )),
        },
        Ok(_) => Some(format!(
            "{shown} is a file, and a corpus is a directory.\n\
             Point at an unpacked corpus rather than at a shard or a file \
             beside one. `stegobench help scope` says what a corpus has to \
             hold."
        )),
        Err(e) => Some(format!(
            "there is no corpus at {shown}: {}.\n\
             Check the path. `stegobench list corpora` names the ones this \
             registry knows, and `stegobench help scope` says what a corpus \
             has to hold.",
            plain(&e)
        )),
    }
}

/// What `--corpus` was given, after BOTH of its plausible meanings were tried.
///
/// WHY ONE FLAG HAS TWO PLAUSIBLE MEANINGS
/// ---------------------------------------
/// This tool says the word "corpus" in two vocabularies. `fetch` and
/// `--corpus-id` take a registered ID, a name for a dataset somebody
/// publishes. `score --corpus` and `verify --corpus` take a PATH, the unpacked
/// bytes on this machine, because both of them read the bytes. A reader who
/// learns one of those first types it at the other, and the answer they used
/// to get was "there is no corpus at pentimento-core: nothing is there", which
/// is true, unhelpful, and says nothing about the vocabulary they are in.
///
/// So a value that is not a directory is looked up as an id before it is
/// refused, and the refusal says what was tried.
///
/// WHY EVERY ANSWER HERE IS EXIT 3 AND NOT EXIT 2
/// ----------------------------------------------
/// The two codes divide on a line this project has already drawn: 2 means the
/// command line was malformed, and 3 means what you named is not there or not
/// fit to use. `--corpus somewhere-that-is-not-there` is a well-formed command
/// naming a thing that does not exist, which is the second. Only the WORDING
/// changed here; the code is the one the contract test has asserted since
/// before the two vocabularies were noticed.
fn corpus_argument_problem(reg: Option<&Registry>, corpus: &Path) -> Option<Output> {
    if matches!(std::fs::metadata(corpus), Err(ref e) if e.kind() == std::io::ErrorKind::NotFound) {
        return Some(unresolved_corpus(reg, corpus));
    }
    corpus_path_problem(corpus)
        .map(|why| Output::err_because(exit::PREFLIGHT_REFUSED, "corpus-unusable", why))
}

/// A `--corpus` value that is not a directory, answered by what it might be.
fn unresolved_corpus(reg: Option<&Registry>, corpus: &Path) -> Output {
    let typed = corpus.to_string_lossy().into_owned();
    if reg.is_some_and(|r| r.corpora.contains_key(typed.as_str())) {
        return Output::err_because(
            exit::PREFLIGHT_REFUSED,
            "corpus-missing",
            format!(
                "there is no corpus at {typed}, and {typed} is a registered \
                 corpus id rather than a path: --corpus takes a directory of \
                 unpacked samples on this machine.\nGet the bytes first:\n  \
                 stegobench fetch {typed} \
                 --tier <tier>\nthen unpack them, point --corpus at the \
                 directory, and name the id under --corpus-id {typed} so the \
                 run is checked against the digest the registry declares.\n\
                 `stegobench describe {typed}` says how it is obtained when it \
                 declares no download route."
            ),
        );
    }
    let as_id = match reg {
        Some(r) => {
            let close = near_corpus_ids(r, &typed);
            if close.is_empty() {
                "none is registered under that name".to_string()
            } else {
                format!(
                    "none is registered under that name; close: {}",
                    close.join(", ")
                )
            }
        }
        // Fail loud rather than report an absence nobody established: with no
        // registry the id half of the question was not asked, and saying it
        // was would send the reader to check a spelling that may be right.
        None => "the registry could not be read, so this was not checked".to_string(),
    };
    Output::err_because(
        exit::PREFLIGHT_REFUSED,
        "corpus-missing",
        format!(
            "there is no corpus at {typed}, and it is not a registered corpus \
             id either. Both were tried:\n  as a directory  nothing is there\n  \
             as a corpus id  {as_id}\n`stegobench list corpora` names every \
             registered id. --corpus wants a directory of samples, an image \
             with a record beside it; `stegobench help scope` says what one \
             has to hold."
        ),
    )
}

/// A corpus id that is not in the registry, answered the same way everywhere.
///
/// `next` is the sentence that closes it, because the commands that take an id
/// fail for one reason and want different things done about it.
/// The per-arm breakdown, indented under the detector it belongs to.
///
/// Empty for a corpus of one arm, because `metrics.per_arm` is empty there
/// and a breakdown of one row is the headline printed twice.
///
/// Every arm is shown rather than a best-and-worst summary. A Core tier is 39
/// arms, which is about a screen, and the reader who is scoring one is not
/// doing it casually; the arm they care about is as likely to be in the
/// middle as at either end.
fn per_arm_lines(arms: &[stegobench_core::result::ArmMetrics]) -> Vec<String> {
    if arms.is_empty() {
        return Vec::new();
    }
    // Widest name, so the numbers line up in a column a reader can scan down
    // rather than hunting along ragged rows.
    let width = arms.iter().map(|a| a.arm.len()).max().unwrap_or(0);
    let mut out = vec![format!(
        "{:<16} by arm, each against the same {} clean image(s):",
        "",
        arms.first().map(|a| a.n_clean).unwrap_or(0)
    )];
    for a in arms {
        out.push(format!(
            "{:<18}{:<width$}  AUC {:.4}{}  {} stego",
            "",
            a.arm,
            a.auc,
            match a.auc_ci95 {
                Some([lo, hi]) => format!(" [{lo:.4}, {hi:.4}]"),
                None => String::new(),
            },
            a.n_stego,
            width = width
        ));
    }
    out
}

fn unregistered_corpus_id(resolved: &Resolved, id: &str, next: &str) -> Output {
    let reg = &resolved.registry;
    let mut human = format!("no corpus with id {id:?} is registered.");
    if Path::new(id).is_dir() {
        // The mirror image of `unresolved_corpus`, and the other half of the
        // same confusion: a directory handed to the flag that wants a name.
        human.push_str(&format!(
            " It is a directory on this machine, and an id names a dataset \
             rather than a path.\nThose bytes are already here, so score them \
             directly:\n  stegobench score --corpus {id} --detector <name>"
        ));
    } else {
        let close = near_corpus_ids(reg, id);
        if !close.is_empty() {
            human.push_str(&format!(" Did you mean: {}?", close.join(", ")));
        }
    }
    human.push('\n');
    human.push_str(next);
    // Which registry was searched, because "not registered" is a statement
    // about one directory and there is more than one it could have been. A
    // reader whose STEGOBENCH_REGISTRY points somewhere with no corpora at all
    // is looking at a true sentence and the wrong conclusion.
    human.push('\n');
    human.push_str(&resolved.source.line());
    // PREFLIGHT_REFUSED rather than USAGE, matching `--corpus` on a directory
    // that is not there. The two codes divide on whether the command line was
    // malformed or whether what it named is absent, and an id nobody has
    // registered is the second: the flag took the kind of value it asked for.
    // These two call sites answered USAGE until 2026-09-30 purely because
    // nothing had made them agree with the path case.
    Output::err_because(exit::PREFLIGHT_REFUSED, "unregistered-corpus-id", human)
}

/// Registered corpus ids close enough to what was typed to be worth naming.
///
/// Substring matches count however far apart the two strings are: `pentimento`
/// is fifteen edits from `pentimento-core` and is obviously the thing the
/// reader meant, which is the case an edit distance alone answers worst.
/// Sorted and capped so two runs print the same suggestions in the same order.
fn near_corpus_ids(reg: &Registry, typed: &str) -> Vec<String> {
    near_names(reg.corpora.keys().map(String::as_str), typed)
}

/// The same, over everything `describe` can look up.
fn near_registered_names(reg: &Registry, typed: &str) -> Vec<String> {
    near_names(
        reg.entries
            .keys()
            .chain(reg.corpora.keys())
            .map(String::as_str),
        typed,
    )
}

fn near_names<'a>(names: impl Iterator<Item = &'a str>, typed: &str) -> Vec<String> {
    let typed = typed.to_lowercase();
    let mut scored: Vec<(usize, String)> = names
        .filter_map(|name| {
            let lower = name.to_lowercase();
            let d = edit_distance(&typed, &lower);
            let close = d <= 3 || lower.contains(&typed) || typed.contains(&lower);
            close.then(|| (d, name.to_string()))
        })
        .collect();
    scored.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
    scored.into_iter().take(3).map(|(_, name)| name).collect()
}

/// A path resolved far enough to be compared with another one, existing or not.
///
/// [`Path::canonicalize`] answers only for a path that is already there, and
/// the path this has to judge is one nothing has written yet. So the deepest
/// ancestor that DOES exist is canonicalised, which resolves every symbolic
/// link and every `..` in that half, and the components below it are rejoined
/// on the end.
///
/// `None` means the question could not be answered, which happens for a
/// relative path with no working directory and for one whose unwritten half
/// ends in `..`. A caller treats that as "cannot prove containment" and lets
/// the write fail on its own terms rather than refusing on a guess.
fn resolved_for_comparison(path: &Path) -> Option<PathBuf> {
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir().ok()?.join(path)
    };
    let mut tail: Vec<std::ffi::OsString> = Vec::new();
    let mut head = absolute.as_path();
    loop {
        if let Ok(base) = head.canonicalize() {
            let mut resolved = base;
            for component in tail.iter().rev() {
                resolved.push(component);
            }
            return Some(resolved);
        }
        let name = head.file_name()?;
        tail.push(name.to_os_string());
        head = head.parent()?;
    }
}

/// Whether an output path would be written INTO the corpus being scored.
///
/// WHY THIS IS A REFUSAL AND NOT A WARNING
/// ---------------------------------------
/// The corpus digest is computed over what the corpus holds. A result document
/// or a records file written inside it becomes part of it, so the next run
/// reads a different set, computes a different digest, and reports that the
/// corpus changed when nothing about the images did. The measurement before it
/// cannot be reproduced and nothing says why. `--out ./mycorpus/result.json`
/// is a natural thing to type and it quietly destroys the one property this
/// tool exists to protect, so it is refused at the boundary rather than
/// explained afterwards.
///
/// Both paths are resolved before they are compared, so a corpus reached
/// through a symbolic link and an `--out` reached through the real directory
/// are still recognised as the same place.
///
/// The caller raises this as a usage error, exit 2, deliberately: both paths
/// are well formed and both destinations exist or could be created, and it is
/// the COMBINATION of the two that is wrong. That is the line exit 2 draws.
/// Exit 3 is for a thing that is named and is not there, which is a different
/// fault and needs a different answer from a script.
fn output_inside_corpus(corpus: &Path, flag: &str, out: Option<&Path>) -> Option<String> {
    let out = out?;
    let corpus_real = resolved_for_comparison(corpus)?;
    let out_real = resolved_for_comparison(out)?;
    if !out_real.starts_with(&corpus_real) {
        return None;
    }
    let what = if out_real == corpus_real {
        format!("the corpus directory {} itself", corpus.display())
    } else {
        format!("inside the corpus at {}", corpus.display())
    };
    let mut why = format!(
        "{flag} {} is {what}.\nWriting there adds a file to the corpus, so the \
         next run reads a different set of bytes, computes a different corpus \
         digest, and reports that the corpus has changed when nothing about \
         the images did. The number measured now could not be reproduced \
         afterwards.\nPut it anywhere outside the corpus.",
        out.display()
    );
    if let Some(parent) = corpus_real.parent() {
        let name = out_real
            .file_name()
            .unwrap_or_else(|| std::ffi::OsStr::new("results"));
        why.push_str(&format!(
            " Beside it works: {flag} {}",
            parent.join(name).display()
        ));
    }
    Some(why)
}

/// Whether this looks like a folder of unlabelled images rather than a corpus.
///
/// WHY THE REFUSAL TEACHES RATHER THAN REPORTS
/// -------------------------------------------
/// Most people who reach for a steganalysis tool want to know whether THEIR
/// images are hiding something. That is the opposite direction from what this
/// measures, and the moment somebody points `score` at a folder of their own
/// files is the moment that distinction is worth the most: they have already
/// installed the thing and typed a real command, so a message that only says
/// "no records found" costs them another twenty minutes before they work out
/// they are in the wrong place.
///
/// Returns `None` whenever it cannot be sure, which includes an empty
/// directory, a directory it could not read, and a tree wider than the bound.
/// Those cases meet the ordinary corpus errors, which is the safe direction to
/// be wrong in: a corpus wrongly accused of being a photo album would be a
/// refusal nobody could work around.
fn unlabelled_corpus(corpus: &Path) -> Option<String> {
    let mut images = 0usize;
    let mut seen = 0usize;
    let mut stack = vec![(corpus.to_path_buf(), 0usize)];
    while let Some((dir, depth)) = stack.pop() {
        let read = std::fs::read_dir(&dir).ok()?;
        for item in read.flatten() {
            seen += 1;
            if seen > SHAPE_CHECK_ENTRIES {
                return None;
            }
            let path = item.path();
            let Ok(meta) = std::fs::metadata(&path) else {
                return None;
            };
            if meta.is_dir() {
                if depth < 2 {
                    stack.push((path, depth + 1));
                }
                continue;
            }
            match path
                .extension()
                .map(|e| e.to_string_lossy().to_lowercase())
                .as_deref()
            {
                // One record anywhere is enough to stop guessing. Whether the
                // records are the RIGHT ones is the corpus loader's job, and
                // it says so far better than a heuristic could.
                Some("json" | "jsonl") => return None,
                Some(ext) if stegobench_cli::LOOKS_LIKE_AN_IMAGE.contains(&ext) => images += 1,
                _ => {}
            }
        }
    }
    if images == 0 {
        return None;
    }
    Some(format!(
        "{} holds {images} image(s) and no records saying which of them hides \
         anything, so there is nothing to be right or wrong about.\n\n\
         `score` measures a detector, which needs the answers in advance. \
         Asking the detectors what they make of these files is `examine`, and \
         what comes back is an answer rather than a measurement.\n\
         `stegobench examine {} --detector <name>`\n\
         `stegobench help scope`      the difference between the two\n\
         `stegobench list detectors`  what is registered here\n\
         `stegobench help pairing`    what a corpus has to carry first",
        corpus.display(),
        corpus.display()
    ))
}

/// Everything `score` was asked for, past the registry it resolves against.
///
/// Bundled rather than passed one at a time because the list outgrew what a
/// reader can hold in order: `records` and `out` are both `Option<&Path>` and
/// sit next to each other, so a transposition at a call site would have written
/// the per-image scores where the result documents go and compiled cleanly.
struct ScoreRequest<'a> {
    corpus: &'a Path,
    detectors: &'a [String],
    corpus_id: Option<&'a str>,
    /// The corpus the detector was trained on, as the user declared it.
    trained_on: Option<&'a str>,
    /// Which half of the train and test split to score, if only one.
    split: Option<&'a str>,
    records: Option<&'a Path>,
    out: Option<&'a Path>,
    timeout: u64,
    /// How many images to score at once. 1 is one at a time.
    jobs: usize,
    /// Keep what the detector printed for every image.
    keep_raw: bool,
    /// The seed the caller declared their plugin was run with.
    seed: Option<u64>,
    /// Where the self-test fixtures live.
    fixtures: Option<&'a Path>,
    limit: Option<u64>,
}

fn cmd_score(resolved: &Resolved, req: ScoreRequest<'_>) -> Output {
    let ScoreRequest {
        corpus,
        detectors,
        corpus_id,
        trained_on,
        split,
        records,
        out,
        timeout,
        jobs,
        keep_raw,
        seed,
        fixtures,
        limit,
    } = req;
    let reg = &resolved.registry;
    let entries = match resolve_detectors(reg, detectors) {
        Ok(e) => e,
        Err(o) => return o,
    };
    let many = entries.len() > 1;

    // Asked before the shape check, because that check reads the directory and
    // answers "nothing to say" for a path it cannot open, so a corpus that is
    // not there fell through to the loader and arrived as
    // "cannot read /nope: No such file or directory (os error 2)" under exit 1.
    // Three faults: a raw operating system error in front of a user, FAILURE
    // where the contract has a refusal, and a message that does not say what to
    // do. A path that is not a readable directory is a refusal, and it is the
    // cheapest one there is.
    if let Some(refused) = corpus_argument_problem(Some(reg), corpus) {
        return refused;
    }

    // Before the corpus is walked and before any detector is probed, because
    // it costs two `canonicalize` calls and the alternative is discovering it
    // by way of a corpus that no longer matches its own digest.
    for (flag, path) in [("--out", out), ("--records", records)] {
        if let Some(why) = output_inside_corpus(corpus, flag, path) {
            return Output::err_because(exit::USAGE, "output-inside-corpus", why);
        }
    }

    // Asked BEFORE availability, because it is the more useful refusal and
    // because it does not depend on any detector. Somebody who points `score`
    // at their own folder of photographs is on the wrong side of the thing
    // this tool does, and telling them "docker pull ..." sends them to install
    // a container that will not answer their question either.
    if let Some(why) = unlabelled_corpus(corpus) {
        return Output::err_because(exit::PREFLIGHT_REFUSED, "corpus-unlabelled", why);
    }

    // Resolved before anything runs, so a typo costs a usage error rather than
    // a corpus walk followed by one.
    let registered = match corpus_id {
        None => None,
        Some(id) => match reg.corpora.get(id) {
            Some(entry) => Some(entry),
            None => {
                return unregistered_corpus_id(
                    resolved,
                    id,
                    "`stegobench list corpora` shows what is. Leaving \
                     --corpus-id out scores the directory anyway, and marks \
                     the result `custom`.",
                )
            }
        },
    };

    // A destination that cannot hold several documents is a usage error, and
    // it is raised before anything runs rather than after an hour. The
    // DIRECTORY itself is not created until at least one detector can run, so
    // a command that refuses leaves nothing behind.
    if many {
        for (flag, path) in [("--out", out), ("--records", records)] {
            if path.is_some_and(|p| p.is_file()) {
                return Output::err_because(
                    exit::USAGE,
                    "output-not-a-directory",
                    format!(
                        "{} is a file. Scoring {} detectors writes one {} \
                         each, so {flag} has to name a directory.",
                        path.expect("checked").display(),
                        entries.len(),
                        if flag == "--out" {
                            "result document"
                        } else {
                            "records file"
                        }
                    ),
                );
            }
        }
    }

    // Progress goes to stderr, so `--json` on stdout stays machine readable
    // while a person can still watch a run that takes hours.
    let say = |line: &str| eprintln!("  {line}");

    // Availability is asked for EVERY detector before the corpus is touched,
    // and the answers are kept. A seven-detector command should say at the
    // start which of the seven it can actually run, not discover the fourth is
    // missing after three hours. Presence only: the self-test is `doctor`'s
    // job and costs a container pull.
    let blocked: Vec<Option<String>> = entries
        .iter()
        .map(|e| stegobench_cli::unavailable_reason(e, resolved.adapter_roots()))
        .collect();
    let mut runnable = Vec::new();
    let mut outcomes: Vec<(String, Outcome)> = Vec::new();
    for (entry, why) in entries.iter().zip(&blocked) {
        match why {
            Some(why) => outcomes.push((entry.name.clone(), Outcome::Skipped { why: why.clone() })),
            None => runnable.push(*entry),
        }
    }

    // With one detector asked for and that one unavailable, nothing was
    // measured and the corpus is not worth walking. Kept as the refusal it has
    // always been, with the same exit code, rather than becoming a zero-result
    // "run" that happens to have skipped everything.
    if runnable.is_empty() {
        return Output::err_because(
            exit::PREFLIGHT_REFUSED,
            "nothing-available",
            format!(
                "nothing was measured: {}",
                nothing_available(&entries, &blocked)
            ),
        );
    }
    for (name, outcome) in &outcomes {
        if let Outcome::Skipped { why } = outcome {
            say(&format!("SKIPPING {name}: {why}"));
        }
    }

    // Created only now that something is going to be written into it. A
    // refusal above leaves no empty directory beside the corpus.
    let out_dir = match resolve_out(corpus, out, many) {
        Ok(d) => d,
        Err(o) => return o,
    };

    // The expensive half, once. Every refusal it can raise is a fact about the
    // corpus, so it applies to all the detectors equally and there is nothing
    // partial to report: the whole command stops.
    say(&format!(
        "establishing what the corpus is, once, for {} detector(s)",
        runnable.len()
    ));
    let prepared = match score::prepare(corpus, registered, limit, trained_on, split, say) {
        Ok(p) => p,
        Err(e) => return score_refusal(&e, limit),
    };
    say(&format!(
        "corpus established in {:.1}s: {} item(s) to score with each detector",
        prepared.preflight_seconds(),
        prepared.items()
    ));

    for (i, entry) in runnable.iter().enumerate() {
        let records_path = records_for(corpus, records, &out_dir, &entry.name, many, prepared.side);
        // Counted against what CAN run rather than against what was asked for,
        // so "1 of 2" beside a third skipped detector does not read as a
        // miscount. The ratio of asked to measured is the summary's job and it
        // says it in full at the end.
        say(&format!(
            "scoring with {} ({} of {} that can run here)",
            entry.name,
            i + 1,
            runnable.len()
        ));
        let outcome = run_one(
            entry,
            &prepared,
            &records_path,
            timeout,
            jobs,
            keep_raw,
            seed,
            fixtures,
            out,
            out_dir.as_deref(),
            resolved.adapter_roots(),
            say,
        );
        outcomes.push((entry.name.clone(), outcome));
    }

    summarise(
        &outcomes,
        &out_dir,
        many,
        offer_for_run(&resolved.registry, corpus, corpus_id).as_ref(),
    )
}

/// The corpus offer, for the runs it is actually an offer to.
///
/// It is for somebody whose run was too small to quote and who has no real
/// corpus yet. Somebody who just scored that corpus has one. And a directory
/// that happens to carry its name made the line read as recognition of the
/// bytes it was pointed at, which nothing here does: a path is not an id.
fn offer_for_run(reg: &Registry, corpus: &Path, corpus_id: Option<&str>) -> Option<fetch::Offer> {
    let named_it = |name: &str| name == fetch::OFFERED_CORPUS;
    if corpus_id.is_some_and(named_it) {
        return None;
    }
    if corpus
        .file_name()
        .and_then(|n| n.to_str())
        .is_some_and(named_it)
    {
        return None;
    }
    fetch::offer(reg)
}

/// The refusal `score` and `plan` share when not one detector can run.
///
/// `blocked` is positional against `entries`, and every entry in it has a
/// reason by the time this is called.
fn nothing_available(
    entries: &[&stegobench_core::registry::Entry],
    blocked: &[Option<String>],
) -> String {
    let why = entries
        .iter()
        .zip(blocked)
        .filter_map(|(e, b)| b.as_ref().map(|w| format!("{} {w}", e.name)))
        .collect::<Vec<_>>()
        .join("\n  ");
    let subject = if entries.len() == 1 {
        "the one detector you asked for is not available here".to_string()
    } else {
        format!(
            "not one of the {} detectors you asked for is available here",
            entries.len()
        )
    };
    format!(
        "{subject}.\n  \
         {why}\n`stegobench doctor` checks every registered tool at once and \
         says what each one needs."
    )
}

/// Where the documents go, decided before any work happens.
///
/// `Ok(None)` means one detector writing to stdout or to the single file the
/// caller named. `Ok(Some(dir))` means a directory holding one document per
/// detector, created here so a failure to create it is a usage error rather
/// than something discovered after the first hour of scoring.
fn resolve_out(corpus: &Path, out: Option<&Path>, many: bool) -> Result<Option<PathBuf>, Output> {
    // `--out` USED TO MEAN DIFFERENT THINGS ON DIFFERENT DAYS
    //
    // It was read as a directory when several detectors were named and as a
    // file when one was, so the same path worked and then did not depending on
    // how many detectors the caller asked for. Measured 2026-09-29: one run
    // wrote /tmp/multi/zsteg.json and /tmp/multi/stegexpose.json happily, and
    // the next, differing only in naming a single detector, scored the whole
    // corpus and then died with
    //
    //     could not write the result to /tmp/multi: Is a directory (os error 21)
    //
    // Three faults in one line: a raw operating system error in front of a
    // user, a generic exit code where the contract has a specific one, and all
    // of it discovered AFTER the work rather than before it.
    //
    // A path that is already a directory is now treated as one whatever the
    // detector count, which is the reading that never surprises, and the check
    // happens here, before a single image is scored. The other half of the
    // question, several detectors aimed at an existing FILE, is refused by
    // `cmd_score` before any detector is even probed for, which is earlier
    // than this and therefore where it belongs.
    if let Some(dir) = out.filter(|p| p.is_dir()) {
        return Ok(Some(dir.to_path_buf()));
    }
    // NAMING NO DESTINATION USED TO MEAN THROWING THE DOCUMENT AWAY
    //
    // This returned `Ok(None)` for a single detector whatever `--out` said,
    // and the caller reads `None` as "write to the file the caller named".
    // With no file named there was nothing to write to, so the run printed
    // its AUC, wrote its records, exited 0 and discarded the versioned
    // document the whole tool exists to produce. A ten hour run over Core
    // lost the only artefact anybody could check, silently.
    //
    // So the default destination is decided by whether a path was given, not
    // by how many detectors were asked for. One detector and no `--out` now
    // lands in the same directory several detectors would.
    let dir = match out {
        // A path was named and it is not an existing directory, so it is the
        // file this one detector writes to. Still `None`; the caller owns it.
        //
        // ITS PARENT IS CHECKED HERE, which is the whole point of this
        // function running before anything is scored. `--records` creates its
        // parent and `--out` did not, so a single misspelt directory was
        // discovered by the write AFTER the last image, with the same
        // `No such file or directory (os error 2)` in front of the user that
        // the comment above this function was written about. Eighteen images
        // cost six seconds; a Core tier run is 344,357 pairs and twenty two
        // hours.
        Some(p) if !many => {
            if let Some(parent) = p.parent().filter(|d| !d.as_os_str().is_empty()) {
                if !parent.is_dir() {
                    return Err(Output::err_because(
                        exit::PREFLIGHT_REFUSED,
                        "out-parent-missing",
                        format!(
                            "there is no directory at {} to write {} into. \
                             Create it, or name a path whose parent exists. \
                             Checked now rather than after the scoring, which \
                             is where it used to be found",
                            parent.display(),
                            p.display()
                        ),
                    ));
                }
            }
            return Ok(None);
        }
        Some(p) => p.to_path_buf(),
        // Beside the corpus and named after it, so two corpora scored on one
        // machine cannot write over each other's documents.
        None => {
            let mut name = corpus.file_name().unwrap_or_default().to_os_string();
            name.push(".results");
            corpus.with_file_name(name)
        }
    };
    if let Err(e) = std::fs::create_dir_all(&dir) {
        return Err(Output::err_because(
            exit::FAILURE,
            "results-unwritable",
            format!("could not create {} for the results: {e}", dir.display()),
        ));
    }
    Ok(Some(dir))
}

/// Where one detector's per-item answers are kept.
///
/// THE NAME CARRIES THE DETECTOR, AND IT DID NOT USED TO
///
/// The default was `<corpus>.records.jsonl`, keyed on the corpus alone. Two
/// detectors over one corpus therefore shared a records file, and the second
/// one RESUMED from the first one's answers: it reported the first detector's
/// scores under its own name, with a resumed count that looked like a feature.
/// A records file is a record of what one subject said about one corpus, so
/// the name says both.
///
/// The split is the third part of that key, for the same reason. `--split
/// test` scores a subset in a different order, so a records file written for
/// the whole corpus does not line up with it; the resume check catches the
/// mismatch and refuses, which is correct but reads as "the corpus changed"
/// when nothing changed except which half was asked for. Keying the name on
/// the split means the two runs never meet in the first place.
fn records_for(
    corpus: &Path,
    records: Option<&Path>,
    out_dir: &Option<PathBuf>,
    detector: &str,
    many: bool,
    split: Option<score::Side>,
) -> PathBuf {
    let file = match split {
        Some(side) => format!("{detector}.{}.records.jsonl", side.as_str()),
        None => format!("{detector}.records.jsonl"),
    };
    match (records, many, out_dir) {
        // One detector and an explicit path: exactly what the caller asked
        // for, because they named one run's file and there is one run.
        (Some(p), false, _) => p.to_path_buf(),
        // Several detectors: the caller named a directory to keep them in.
        (Some(p), true, _) => p.join(file),
        // Whatever the detector count, records go where the documents go, so
        // everything one run produced is in the place the caller named.
        (None, _, Some(dir)) => dir.join(file),
        (None, true, None) => corpus.with_file_name(file),
        (None, false, None) => {
            let mut name = corpus.file_name().unwrap_or_default().to_os_string();
            name.push(format!(".{file}"));
            corpus.with_file_name(name)
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn run_one<P>(
    entry: &stegobench_core::registry::Entry,
    prepared: &score::Prepared,
    records: &Path,
    timeout: u64,
    jobs: usize,
    keep_raw: bool,
    seed: Option<u64>,
    fixtures: Option<&Path>,
    out: Option<&Path>,
    out_dir: Option<&Path>,
    adapter_roots: &[PathBuf],
    say: P,
) -> Outcome
where
    P: Fn(&str),
{
    if let Some(parent) = records.parent() {
        if !parent.as_os_str().is_empty() && !parent.exists() {
            if let Err(e) = std::fs::create_dir_all(parent) {
                return Outcome::Failed {
                    why: format!("could not create {} for the records: {e}", parent.display()),
                    code: exit::FAILURE,
                    reason: "records-unwritable",
                };
            }
        }
    }

    let (result, tally) = match score::score_one(
        entry,
        prepared,
        records,
        score::How {
            timeout: std::time::Duration::from_secs(timeout),
            jobs,
            keep_raw,
            seed,
            fixtures,
            adapter_roots,
        },
        |line: &str| say(line),
    ) {
        Ok(pair) => pair,
        Err(e) => {
            return Outcome::Failed {
                why: e.to_string(),
                code: e.exit_code(),
                reason: e.reason(),
            }
        }
    };

    // Validated before it is written, not after. A document this refuses is
    // one no reader should have been handed in the first place.
    if let Err(problems) = result.validate() {
        return Outcome::Failed {
            why: format!(
                "the run finished but produced a result that does not \
                 validate, which is a bug in this harness rather than in the \
                 detector:\n  {}",
                problems.join("\n  ")
            ),
            code: exit::SCHEMA_INVALID,
            reason: "result-invalid",
        };
    }

    let body = match serde_json::to_string_pretty(&result) {
        Ok(b) => b,
        Err(e) => {
            return Outcome::Failed {
                why: format!("could not write the result: {e}"),
                code: exit::FAILURE,
                reason: "result-unwritable",
            }
        }
    };

    // Written as this detector completes rather than at the end of the
    // command, so an interrupted seven-detector run keeps what it already
    // measured. That is the whole reason the loop is shaped this way.
    // A directory wins over the detector count: `resolve_out` has already
    // decided, once, before anything ran, so this is not the place that guesses.
    let destination = match (out_dir, out) {
        (Some(dir), _) => Some(dir.join(format!("{}.json", entry.name))),
        (None, Some(path)) => Some(path.to_path_buf()),
        _ => None,
    };
    if let Some(path) = &destination {
        if let Err(e) = std::fs::write(path, format!("{body}\n")) {
            return Outcome::Failed {
                why: format!("could not write the result to {}: {e}", path.display()),
                code: exit::FAILURE,
                reason: "result-unwritable",
            };
        }
    }

    Outcome::Measured {
        result: Box::new(result),
        tally,
        written: destination,
    }
}

/// One line per detector and one line saying how many of them are real.
///
/// WHY THE COUNT IS ALWAYS PRINTED, EVEN WHEN IT IS `7 of 7`
///
/// The reader of a benchmark summary is deciding whether they have a baseline.
/// "AUC 0.83, 0.61, 0.55" over a registry of seven says nothing about the four
/// that are missing, and a reader who has to count the rows to find out is a
/// reader who will one day not bother. So the ratio leads, the skipped
/// detectors are named with their reasons, and the exit code carries the same
/// fact for anything that is not a person.
fn summarise(
    outcomes: &[(String, Outcome)],
    out_dir: &Option<PathBuf>,
    many: bool,
    offer: Option<&fetch::Offer>,
) -> Output {
    let measured = outcomes
        .iter()
        .filter(|(_, o)| matches!(o, Outcome::Measured { .. }))
        .count();
    let skipped = outcomes
        .iter()
        .filter(|(_, o)| matches!(o, Outcome::Skipped { .. }))
        .count();
    let failed = outcomes
        .iter()
        .filter(|(_, o)| matches!(o, Outcome::Failed { .. }))
        .count();

    let mut lines = Vec::new();
    for (name, outcome) in outcomes {
        match outcome {
            Outcome::Measured {
                result,
                tally,
                written,
            } => {
                lines.push(format!(
                    "{name:<16} AUC {:.4}{}  {} clean / {} stego / {} unanswered{}{}",
                    result.metrics.auc,
                    // Printed beside the figure rather than under the table,
                    // because the interval is what stops two AUCs differing in
                    // the third decimal from being read as two different
                    // detectors, and a caveat a line away is a caveat nobody
                    // carries when they copy the number out.
                    match result.metrics.auc_ci95 {
                        Some([lo, hi]) => format!(" [{lo:.4}, {hi:.4}]"),
                        None => String::new(),
                    },
                    result.metrics.n_clean,
                    result.metrics.n_stego,
                    result.metrics.n_error,
                    if tally.resumed > 0 {
                        format!("  {} resumed", tally.resumed)
                    } else {
                        String::new()
                    },
                    match written {
                        Some(p) => format!("  {}", p.display()),
                        None => String::new(),
                    }
                ));
                // The pooled figure is the misleading one whenever a corpus
                // holds several arms: it lands between them and describes
                // none. Printed rather than left in the document, because a
                // number a reader has to open a file to qualify is a number
                // they will quote unqualified.
                lines.extend(per_arm_lines(&result.metrics.per_arm));
            }
            Outcome::Skipped { why } => {
                lines.push(format!("{name:<16} NOT MEASURED, skipped: {why}"))
            }
            Outcome::Failed { why, .. } => {
                lines.push(format!("{name:<16} NOT MEASURED, failed: {why}"))
            }
        }
    }

    // Adjacent to the figures rather than in the footer. A definition one
    // screen away from the number it defines is a definition nobody reads,
    // and neither "AUC" nor the bracketed pair beside it is expanded here.
    if measured > 0 {
        lines.push("AUC and the bracketed pair: `stegobench help results`.".into());
    }

    lines.push(String::new());
    lines.push(format!(
        "{measured} of {} detector(s) measured.",
        outcomes.len()
    ));
    if skipped > 0 || failed > 0 {
        lines.push(format!(
            "{} produced NO number ({skipped} skipped, {failed} failed). A \
             report over these documents covers only the {measured} that ran.",
            skipped + failed
        ));
    }

    // A DIRECTORY IS NOT A RUN, AND A READER WILL TREAT IT AS ONE.
    //
    // A detector measured last week leaves its document behind. If it is
    // skipped this week, `stegobench report <dir>` still finds that document
    // and puts its number in the table, beside numbers from today, with
    // nothing in either the table or this summary saying so. The count above
    // would read "2 of 3" while the directory holds three. Nothing is deleted,
    // because a measurement is not this command's to throw away; it is named
    // instead, which is the half a reader cannot work out for themselves.
    if let Some(dir) = out_dir {
        // Everything in the directory that this run did not write, not only
        // the detectors it asked for and missed. A document from a detector
        // nobody named today is exactly as invisible in the resulting table,
        // and a run of five detectors into a directory that already held
        // seven produces a seven-row table under a "5 of 5 measured" summary.
        let written: std::collections::BTreeSet<&Path> = outcomes
            .iter()
            .filter_map(|(_, o)| match o {
                Outcome::Measured { written, .. } => written.as_deref(),
                _ => None,
            })
            .collect();
        let mut stale: Vec<String> = Vec::new();
        // A directory that cannot be read is not reported as clean: the
        // warning is skipped and the reason is carried instead, because
        // "nothing stale here" and "could not look" are different facts.
        match std::fs::read_dir(dir) {
            Ok(entries) => {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.extension().is_some_and(|e| e == "json") && !written.contains(&*path) {
                        stale.push(path.display().to_string());
                    }
                }
                stale.sort();
            }
            Err(e) => lines.push(format!(
                "could not list {} to check for older documents: {e}. A report \
                 over it may cover more than this run did.",
                dir.display()
            )),
        }
        if !stale.is_empty() {
            lines.push(format!(
                "WARNING: {} document(s) here are from an earlier run, not \
                 this one: {}. `stegobench report {}` will include them \
                 beside today's numbers. Move them aside if this run is meant \
                 to be the whole table.",
                stale.len(),
                stale.join(", "),
                dir.display()
            ));
        }
    }
    // SAID BECAUSE THE PATH ALONE DOES NOT SAY WHICH KIND IT IS.
    //
    // `--out results` makes a directory of documents when several detectors are
    // named and a single document called `results` when one is, and the summary
    // printed the same bare path either way. A reader who ran the two-detector
    // form first goes looking inside the file. The contract is not worth
    // changing for it, because one detector's document landing exactly where
    // the caller pointed is the reading that wins when somebody writes
    // `--out run.json`; what was missing was the sentence saying which happened.
    //
    // Read off the outcome rather than from `--out`, because `out_dir` is None
    // for exactly one case: a path was named and one detector was asked for.
    if !many && out_dir.is_none() {
        if let Some(named) = outcomes.iter().find_map(|(_, o)| match o {
            Outcome::Measured { written, .. } => written.as_deref(),
            _ => None,
        }) {
            lines.push(format!(
                "{} is the result document itself, because one detector was \
                 asked for. More than one would have made it a directory with \
                 a document per detector inside.",
                named.display()
            ));
        }
    }
    // TWO "Next:" LINES ARE NONE.
    //
    // A run of the shipped starter corpus into a directory printed both
    // "Next: stegobench report ..." and "Next: stegobench describe
    // pentimento-core", one under the other, and a reader with two next steps
    // has no next step. They are gathered here and printed as an ordered
    // pair instead, nearest first.
    let mut next: Vec<String> = Vec::new();
    if many {
        if let Some(dir) = out_dir {
            next.push(format!(
                "stegobench report {} --format markdown",
                dir.display()
            ));
        }
    }

    // WHY THIS DOES NOT PRINT ON EVERY RUN.
    //
    // An offer a reader sees after every measurement is an advertisement, and
    // an advertisement is something people learn to skip. This fires only where
    // it is the useful next line: a run small enough that the number cannot be
    // quoted, which is what scoring the shipped starter corpus looks like. A
    // real tier is thousands of images and never reaches it.
    let largest = outcomes
        .iter()
        .filter_map(|(_, o)| match o {
            Outcome::Measured { result, .. } => {
                Some(result.metrics.n_clean + result.metrics.n_stego)
            }
            _ => None,
        })
        .max()
        .unwrap_or(0);
    if let Some(offer) = offer {
        if largest > 0 && largest < fetch::SMALL_RUN_IMAGES {
            lines.push(format!(
                "{largest} images is a demonstration, not a measurement."
            ));
            next.push(offer.command().to_string());
        }
    }
    for (label, command) in ["Next: ", "Then: "].iter().zip(&next) {
        lines.push(format!("{label}{command}"));
    }

    // The code says the same thing the count says, for a caller that is not a
    // person. A failure outranks a skip because the two ask for different
    // things: a skip is something to install, a failure is something to
    // investigate, and reporting the louder one first is the honest order.
    let code = if failed > 0 {
        exit::PLUGIN_FAILED
    } else if skipped > 0 {
        exit::PREFLIGHT_REFUSED
    } else {
        exit::OK
    };

    let json = serde_json::json!({
        "ok": code == exit::OK,
        "requested": outcomes.len(),
        "measured": measured,
        "skipped": skipped,
        "failed": failed,
        "results_dir": out_dir.as_ref().map(|d| d.display().to_string()),
        "detectors": outcomes.iter().map(|(name, o)| match o {
            Outcome::Measured { result, tally, written } => serde_json::json!({
                "name": name,
                "status": "measured",
                "written": written.as_ref().map(|p| p.display().to_string()),
                "scored": tally.scored,
                "resumed": tally.resumed,
                "result": serde_json::to_value(result.as_ref())
                    .unwrap_or(serde_json::Value::Null),
            }),
            Outcome::Skipped { why } => serde_json::json!({
                "name": name, "status": "skipped", "reason": why,
            }),
            Outcome::Failed { why, code, .. } => serde_json::json!({
                "name": name, "status": "failed", "reason": why, "exit_code": code,
            }),
        }).collect::<Vec<_>>(),
    });

    // ONE DETECTOR STILL BEHAVES EXACTLY AS IT ALWAYS DID.
    //
    // The whole multi-detector apparatus is the wrong shape for a command that
    // asked one question. A single run that worked puts the result document on
    // stdout, so `--json | jq` and `--json | stegobench validate -` keep
    // working, and a single run that failed returns its own exit code with its
    // own message rather than the summary's coarser one: an embedder refused
    // is a usage error and a one-sided corpus is a pre-flight refusal, and
    // flattening both to "a detector failed" would lose the distinction the
    // codes exist to carry.
    if !many {
        match outcomes.first() {
            Some((_, Outcome::Measured { result, .. })) => {
                let mut output = Output::ok(
                    serde_json::to_value(result.as_ref()).unwrap_or(serde_json::Value::Null),
                    lines.join("\n"),
                );
                output.code = code;
                return output;
            }
            Some((_, Outcome::Failed { why, code, reason })) => {
                return Output::err_because(*code, reason, why.clone())
            }
            _ => {}
        }
    }

    let mut output = Output::ok(json, lines.join("\n"));
    output.code = code;
    output
}

/// Turns result documents into a table, and refuses to turn them into a
/// misleading one.
///
/// WHY A SKIPPED FILE STILL PRINTS A TABLE, AND STILL EXITS NON-ZERO
///
/// Two failures were available here and both are real. Refusing the whole
/// report over one bad file makes the command useless against a directory
/// somebody else assembled; dropping the file quietly produces a table that
/// looks complete and is not, which is the worse of the two by a distance.
/// So it does both halves of the honest thing: the table prints, the skipped
/// files are named at the TOP of it with their reasons, and the exit code
/// says the table is short, so a script cannot treat it as whole.
fn cmd_report(paths: &[PathBuf], format: ReportFormat, out: Option<&Path>) -> Output {
    let report = match report::build(paths) {
        Ok(r) => r,
        Err(e) => return Output::err(e.exit_code(), e.to_string()),
    };
    let body = report::render(&report, format);
    let json = report::to_json(&report);

    let human = match out {
        None => body,
        Some(path) => {
            if let Err(e) = report::write_atomically(path, &body) {
                return Output::err(e.exit_code(), e.to_string());
            }
            format!(
                "{} result(s) in {} table(s) written to {}. {}",
                report.read,
                report.groups.len(),
                path.display(),
                if report.skipped.is_empty() {
                    "Every file found was readable.".to_string()
                } else {
                    format!(
                        "{} file(s) could not be read and are named at the top \
                         of it, so the tables are incomplete.",
                        report.skipped.len()
                    )
                }
            )
        }
    };

    let mut output = Output::ok(json, human);
    output.code = report.exit_code();
    // The table goes to stdout when it IS the output. When `--out` took it,
    // the human text is a confirmation line and belongs on stderr with every
    // other progress message.
    output.payload_on_stdout = out.is_none();
    output
}

/// Where a fetch lands: the flag, then the environment, then the default.
///
/// Split out from `cmd_fetch` with the environment's answer handed in so the
/// precedence, and the treatment of an empty variable, are testable without
/// `set_var`. Setting a variable is process-global and this suite runs its
/// tests on several threads at once.
fn fetch_dest(named: Option<&Path>, from_env: Option<&str>) -> PathBuf {
    named
        .map(Path::to_path_buf)
        .or_else(|| from_env.filter(|v| !v.is_empty()).map(PathBuf::from))
        .unwrap_or_else(fetch::default_dest)
}

include!(concat!(env!("OUT_DIR"), "/embedded_starter.rs"));

/// Write the compiled-in starter corpus to `dest`, returning how many files.
///
/// Refuses a destination that already holds files rather than merging into
/// it: a half-overwritten corpus whose records describe images from two
/// different copies is worse than no corpus, and it is the kind of thing
/// nobody notices until a number comes out wrong.
fn write_embedded_starter(dest: &Path) -> Result<usize, String> {
    if let Ok(mut entries) = std::fs::read_dir(dest) {
        if entries.next().is_some() {
            return Err(format!(
                "{} already has something in it. Name an empty directory \
                 with --dest, or move that one aside: writing over half of a \
                 corpus leaves records describing images from two different \
                 copies, and nothing downstream can tell.",
                dest.display()
            ));
        }
    }
    for (rel, bytes) in EMBEDDED_STARTER {
        let path = dest.join(rel);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| format!("could not make {}: {e}", parent.display()))?;
        }
        std::fs::write(&path, bytes)
            .map_err(|e| format!("could not write {}: {e}", path.display()))?;
    }
    Ok(EMBEDDED_STARTER.len())
}

/// Download one tier of one corpus.
///
/// THE ORDER HERE IS THE POINT.
///
/// The corpus is resolved, then the refusal is decided, and only then is a
/// transport looked for. A corpus this tool may not fetch is refused
/// identically on a machine with `curl` and on a machine without one, and
/// neither answer is ever "install curl" to somebody whose real answer is that
/// the terms say no.
fn cmd_fetch(
    resolved: &Resolved,
    id: &str,
    tier: &str,
    dest: Option<&Path>,
    max_bytes: Option<u64>,
    budget_minutes: u64,
) -> Output {
    let Some(entry) = resolved.registry.corpora.get(id) else {
        return unregistered_corpus_id(
            resolved,
            id,
            "`stegobench list corpora` shows what is registered.",
        );
    };
    // A corpus carried inside this binary is obtained by writing it out, not
    // by downloading it, and this is the verb somebody reaches for either
    // way. Before the refusal, because the refusal is about a download route
    // and this corpus needs none: `describe` used to tell the reader to point
    // `score` at a path "in a checkout of this repository", which is false
    // for anybody who installed the binary, and that is everybody the starter
    // corpus exists for.
    if entry.id == STARTER_ID && !EMBEDDED_STARTER.is_empty() {
        let dest = dest
            .map(Path::to_path_buf)
            .unwrap_or_else(|| PathBuf::from(STARTER_ID));
        return match write_embedded_starter(&dest) {
            Err(e) => Output::err(exit::FAILURE, e),
            Ok(n) => Output::ok(
                serde_json::json!({
                    "corpus": entry.id,
                    "dest": dest.display().to_string(),
                    "files": n,
                    "source": "compiled into this binary",
                }),
                format!(
                    "wrote {n} file(s) to {}, from the copy compiled into \
                     this binary rather than from a download.\n\nScore it \
                     with:\n  stegobench score --corpus {} --detector \
                     <name>\n\nEighteen images is a demonstration that the \
                     machinery works, not a measurement of anything.",
                    dest.display(),
                    dest.display()
                ),
            ),
        };
    }
    if let Some(refused) = fetch::refusal(entry) {
        return Output::err(refused.exit_code(), refused.to_string());
    }

    // Asked before a directory is created, so a mistyped tier leaves nothing
    // behind. The message is the core's own rather than a second copy of it.
    if entry.route(tier).is_none() {
        let e = fetch::Error::Failed(stegobench_core::fetch::FetchError::UnknownTier {
            id: entry.id.clone(),
            tier: tier.to_string(),
            known: entry.download.iter().map(|r| r.tier.clone()).collect(),
        });
        return Output::err(e.exit_code(), e.to_string());
    }

    let limits = fetch::limits(max_bytes, budget_minutes);
    let dest = fetch_dest(
        dest,
        registry::non_empty("STEGOBENCH_CORPUS_DIR").as_deref(),
    );
    // A pre-flight rather than a discovery an hour in: an unwritable
    // destination is the same failure whether it is found now or after 48 GB.
    if let Err(e) = std::fs::create_dir_all(&dest) {
        return Output::err(
            exit::FAILURE,
            format!(
                "cannot use {} to keep the download: {e}. Name a writable \
                 directory with --dest.",
                dest.display()
            ),
        );
    }
    let transport = match fetch::Curl::find(limits) {
        Ok(t) => t,
        Err(e) => return Output::err(e.exit_code(), e.to_string()),
    };
    let store = stegobench_core::fetch::FileStore::new(&dest);

    let mut stderr = std::io::stderr();
    let interactive = stderr.is_terminal();
    let mut renderer = fetch::Renderer::new(&mut stderr, interactive);
    let outcome = fetch::run(entry, tier, &transport, &store, limits, &mut renderer);
    let progress_error = renderer.write_error().map(|e| e.to_string());
    drop(renderer);

    match outcome {
        Ok(fetched) => {
            let (json, human) = fetch::describe_success(entry, tier, &fetched, progress_error);
            Output::ok(json, human)
        }
        Err(e) => Output::err(e.exit_code(), e.to_string()),
    }
}

/// What a bare `stegobench` prints.
///
/// It used to print the whole help, which is three screens answering a
/// question nobody typing one word has asked yet. A person who types the bare
/// name wants to know what this is and what to type next, and both fit in a
/// few lines. Everything else is one `--help` away and nothing was removed.
fn cmd_orientation(offer: Option<fetch::Offer>) -> Output {
    let mut human = "stegobench measures how good a steganography detector is, by \
         running it over images whose answers are already known.\n\
         It can also run those detectors over images of your own, which is an \
         answer rather than a measurement (`stegobench help scope`).\n\n  \
         stegobench list detectors    what this installation can run\n  \
         stegobench examine <image>   what the detectors say about a file\n  \
         stegobench doctor            what is installed, and what it needs\n  \
         stegobench help              the reasoning, one topic at a time\n  \
         stegobench --help            every command and flag"
        .to_string();
    let mut next = vec![
        "stegobench list detectors".to_string(),
        "stegobench examine <image> --detector <name>".to_string(),
        "stegobench doctor".to_string(),
        "stegobench help scope".to_string(),
    ];
    // ONE line, and only the one that runs. A reader with nothing to score
    // needs somewhere to get images, and the offer is generated from the
    // registry entry so it can never name a route that entry has not declared.
    if let Some(offer) = &offer {
        human.push_str(&format!("\n\n  {}", offer.line()));
        next.push(offer.run.clone());
    }
    let mut out = Output::ok(
        serde_json::json!({
            "ok": false,
            "error": "no command given",
            "next": next,
        }),
        human,
    );
    // Nothing was done, so this is not a success. Exit 2 is what the contract
    // calls a usage error and what clap returns for the same case, and the
    // text still goes to stdout because it IS the answer to what was asked.
    out.code = exit::USAGE;
    out.payload_on_stdout = true;
    out
}

/// The words somebody arriving with "is there something hidden in my photo"
/// types before they read anything.
///
/// This list used to hold fifteen words and all fifteen got the same refusal,
/// which was the honest answer while the tool could not be pointed at a file
/// of yours at all. `examine` changed that, so the list split in two by what
/// is still true. These are the words that still describe something absent:
/// every one of them is asking for the payload itself, and Stegobench asks
/// detectors questions rather than pulling data out of pictures.
const NO_EXTRACTION: &[&str] = &["decode", "extract", "reveal", "unhide"];

/// Is this one of the words above, whatever case it was typed in?
fn no_extraction(typed: &str) -> bool {
    let typed = typed.to_lowercase();
    NO_EXTRACTION.contains(&typed.as_str())
}

/// The words that now mean `examine`.
///
/// `check`, `scan`, `inspect`, `detect` and `analyse` are clap aliases on the
/// command itself, so they never reach here; they are listed anyway because a
/// word belongs in exactly one of these two sets and leaving them out would
/// make this one read as the complete answer to "what did they guess". The
/// rest are vaguer guesses that cannot be aliases: `run` and `test` would
/// both be misleading as names for this, and `find` and `search` sound like
/// they take a needle.
const MEANT_EXAMINE: &[&str] = &[
    "analyse", "analyze", "check", "detect", "examine", "find", "inspect", "run", "scan", "search",
    "test",
];

fn meant_examine(typed: &str) -> bool {
    let typed = typed.to_lowercase();
    MEANT_EXAMINE.contains(&typed.as_str())
}

/// Words that mean the right thing and are not the verb.
///
/// These are not the wrong direction: somebody typing them has understood
/// what the tool measures and has only guessed the wrong word for it. They
/// used to reach the generic "there is no `stegobench benchmark`" line, which
/// says what is absent and nothing about what is present, and "benchmark" is
/// the word in the product's own name and the likeliest first guess there is.
const RIGHT_IDEA: &[&str] = &[
    "assess",
    "bench",
    "benchmark",
    "benchmarks",
    "compare",
    "eval",
    "evaluate",
    "grade",
    "measure",
    "rank",
];

fn right_idea(typed: &str) -> bool {
    let typed = typed.to_lowercase();
    RIGHT_IDEA.contains(&typed.as_str())
}

/// The redirect those words get: the verb they meant, and the two commands.
fn cmd_right_idea(word: &str) -> Output {
    Output::err(
        exit::USAGE,
        format!(
            "there is no `stegobench {word}`. Measuring a detector against a \
             labelled corpus is `score`.\n\n\
             `stegobench plan score --corpus <dir> --detector <name>`  what \
             it would cost\n\
             `stegobench score --corpus <dir> --detector <name>`       the \
             measurement\n\
             `stegobench list detectors`                               what \
             is registered here"
        ),
    )
}

/// The refusal the extraction words get.
///
/// It has to say two things rather than one, and in this order: the thing
/// they asked for is absent, and the nearby thing they probably want is not.
/// Leading with `examine` would read as though it extracts.
fn cmd_no_extraction(word: &str) -> Output {
    Output::err(
        exit::USAGE,
        format!(
            "there is no `stegobench {word}`. Stegobench asks detectors \
             questions about images; it does not pull a hidden payload back \
             out of one.\n\n\
             `stegobench examine <image> --detector <name>`  what the \
             detectors say about it\n\
             `stegobench list detectors`                     what is \
             registered here\n\
             `stegobench help scope`                         what an answer \
             about one image is worth"
        ),
    )
}

/// The redirect the examiner words get.
fn cmd_meant_examine(word: &str) -> Output {
    Output::err(
        exit::USAGE,
        format!(
            "there is no `stegobench {word}`. Asking detectors about images \
             of your own is `examine`.\n\n\
             `stegobench examine <image>... --detector <name>`  ask one, or \
             several at once\n\
             `stegobench list detectors`                        what is \
             registered here\n\
             `stegobench help scope`                            why that \
             answer is not a measurement"
        ),
    )
}

/// `examine`: what the named detectors say about the images the user brought.
///
/// The table is the payload even when the exit code is not zero, for the same
/// reason `report`'s is: a column that failed and three that answered is
/// worth seeing, and diverting it to stderr hides it in exactly the case
/// somebody most needs to read it.
fn cmd_examine(
    resolved: &Resolved,
    images: &[PathBuf],
    detectors: &[String],
    timeout: u64,
    jobs: usize,
    raw: Option<&Path>,
) -> Output {
    // Progress goes to stderr, so `--json` on stdout stays machine readable
    // while a person can still watch a container start up.
    let say = |line: &str| eprintln!("  {line}");

    let found = match examine::run(
        &resolved.registry,
        examine::Request {
            detectors,
            images,
            timeout: std::time::Duration::from_secs(timeout),
            jobs,
            adapter_roots: resolved.adapter_roots(),
            raw,
        },
        say,
    ) {
        Ok(found) => found,
        Err(e) => {
            let mut human = e.to_string();
            // Naming the flag and not one value that would satisfy it is what
            // clap did, and it is what a journey called unhelpful.
            if matches!(e, examine::ExamineError::NoDetectorNamed) {
                if let Some(way) = stegobench_cli::smallest_way_in(&resolved.registry) {
                    human.push_str(&format!("\n\n{way}"));
                }
            }
            return Output::err_because(e.exit_code(), e.reason(), human);
        }
    };

    let mut human = found.table();
    for note in found.footnotes() {
        human.push('\n');
        human.push_str(&note);
        human.push('\n');
    }
    // A table of nothing is where a first run most needs the way out of it,
    // and it is the same sentence `doctor` gives for the same state.
    if !found.answered_anything() {
        if let Some(way) = stegobench_cli::smallest_way_in(&resolved.registry) {
            human.push('\n');
            human.push_str(&way);
            human.push('\n');
        }
    }

    let mut out = Output::ok(found.to_json(), human);
    out.code = found.exit_code();
    out.payload_on_stdout = true;
    out
}

fn run(cli: &Cli) -> Output {
    let Some(command) = &cli.command else {
        // Fail-open: a registry that will not load is a problem for the
        // commands that need one, and refusing to print three lines of
        // orientation over it would answer the wrong question.
        let offer = registry::resolve(cli.registry.as_deref())
            .ok()
            .and_then(|r| fetch::offer(&r.registry));
        return cmd_orientation(offer);
    };
    match command {
        Command::Schema { name } => cmd_schema(name),
        Command::Validate { file } => cmd_validate(file),
        Command::Verify {
            file,
            corpus,
            records,
            shallow,
        } => {
            // The registry is resolved leniently here and nowhere else in this
            // arm: `verify` compares a document against bytes and needs no
            // registry to do it, so a machine without one must still be able
            // to run it. It is read only so that a `--corpus` value naming a
            // registered id rather than a directory can be told apart from a
            // typo, and a registry that cannot be read costs that one sentence
            // rather than the command.
            let reg = registry::resolve(cli.registry.as_deref()).ok();
            match corpus_argument_problem(reg.as_ref().map(|r| &r.registry), corpus) {
                Some(refused) => refused,
                None => cmd_verify(file, corpus, records.as_deref(), *shallow),
            }
        }
        Command::List { kind } => with_registry(cli, |r| cmd_list(r, kind)),
        Command::Describe { name, toml } => with_registry(cli, |r| cmd_describe(r, name, *toml)),
        Command::Embed {
            embedder,
            cover,
            payload,
            out,
            passphrase,
            no_verify,
        } => with_registry(cli, |r| {
            cmd_embed(
                r,
                embedder,
                cover,
                payload,
                out,
                passphrase.as_deref(),
                *no_verify,
            )
        }),
        Command::Plan { command } => with_registry(cli, |r| cmd_plan(r, command)),
        Command::Doctor {
            fixtures,
            no_selftest,
            strict,
            registry_reach,
        } => with_registry(cli, |r| {
            if *no_selftest {
                // Nothing will read the fixtures, so nothing looks for them.
                // Unpacking the built-in copy here would be work done to
                // satisfy a parameter rather than a question.
                return cmd_doctor(r, None, *strict, *registry_reach);
            }
            match fixtures::resolve(fixtures.as_deref()) {
                // Held for the whole call: for the built-in copy this owns the
                // scratch directory the images were unpacked into.
                Ok(found) => cmd_doctor(r, Some(&found), *strict, *registry_reach),
                Err(e) => Output::err(exit::PREFLIGHT_REFUSED, e.to_string()),
            }
        }),
        Command::Examine {
            images,
            detectors,
            timeout,
            jobs,
            raw,
        } => with_registry(cli, |r| {
            cmd_examine(r, images, detectors, *timeout, *jobs, raw.as_deref())
        }),
        Command::Score {
            corpus,
            detector,
            corpus_id,
            trained_on,
            split,
            records,
            out,
            timeout,
            jobs,
            keep_raw,
            seed,
            limit,
        } => with_registry(cli, |r| {
            // Resolved here and held for the whole call, the same way
            // `doctor` holds it, because for the built-in copy this owns the
            // scratch directory the fixture images were unpacked into. A
            // machine with no fixtures records no self-test rather than
            // refusing the run: the measurement is still worth having, and
            // the document says the check was not made.
            let found = fixtures::resolve(None).ok();
            cmd_score(
                r,
                ScoreRequest {
                    corpus,
                    detectors: detector.as_slice(),
                    corpus_id: corpus_id.as_deref(),
                    trained_on: trained_on.as_deref(),
                    split: split.as_deref(),
                    records: records.as_deref(),
                    out: out.as_deref(),
                    timeout: *timeout,
                    jobs: *jobs,
                    keep_raw: *keep_raw,
                    seed: *seed,
                    fixtures: found.as_ref().map(|f| f.dir()),
                    limit: *limit,
                },
            )
        }),
        Command::Fetch {
            corpus,
            tier,
            dest,
            max_bytes,
            budget_minutes,
        } => with_registry(cli, |r| {
            cmd_fetch(
                r,
                corpus,
                tier,
                dest.as_deref(),
                *max_bytes,
                *budget_minutes,
            )
        }),
        Command::Metrics { file, at } => cmd_metrics(file.as_deref(), at),
        Command::Report { paths, format, out } => cmd_report(paths, *format, out.as_deref()),
        Command::Completions { shell } => cmd_completions(*shell),
        Command::Help { topic } => cmd_help(topic.as_deref()),
    }
}

/// Parses, and on an unknown subcommand names the closest real one.
///
/// clap's own did-you-mean only fires when its threshold is met, so `scan`
/// produced a usage error naming nothing at all. This always names the nearest
/// command and always names where the full list is, and falls back to clap's
/// own message untouched when the offending word cannot be recovered from the
/// error, because a worse message is better than no message.
fn parse_or_explain() -> Cli {
    match Cli::try_parse() {
        Ok(cli) => cli,
        Err(e) if e.kind() == clap::error::ErrorKind::InvalidSubcommand => {
            let typed = e
                .get(clap::error::ContextKind::InvalidSubcommand)
                .map(|v| v.to_string());
            let Some(typed) = typed else {
                e.exit();
            };
            if no_extraction(&typed) {
                eprintln!("{}", cmd_no_extraction(&typed).human);
                std::process::exit(exit::USAGE);
            }
            if meant_examine(&typed) {
                eprintln!("{}", cmd_meant_examine(&typed).human);
                std::process::exit(exit::USAGE);
            }
            if right_idea(&typed) {
                eprintln!("{}", cmd_right_idea(&typed).human);
                std::process::exit(exit::USAGE);
            }
            let nearest = nearest_command(&typed);
            eprintln!("there is no `stegobench {typed}`.");
            if let Some(nearest) = nearest {
                eprintln!("The closest command is `{nearest}`.");
            }
            eprintln!(
                "`stegobench` on its own says what the tool is for, and \
                 `stegobench --help` lists every command."
            );
            std::process::exit(exit::USAGE);
        }
        // `stegobench plan --corpus ...` is the natural first attempt at a
        // command that wraps another one, and clap answers it with "to pass
        // '--corpus' as a value, use '-- --corpus'", which would quote the
        // flag as a literal word and plan nothing at all.
        Err(ref e) if plan_without_its_verb(e).is_some() => {
            let rest = plan_without_its_verb(e).expect("matched");
            eprintln!(
                "`plan` takes the command you would run, whole, so it has to \
                 start with that command's own name."
            );
            eprintln!("\nYou typed:\n  stegobench plan {rest}");
            eprintln!("You probably meant:\n  stegobench plan score {rest}");
            eprintln!("\n`score` is the only command that can be planned today.");
            std::process::exit(exit::USAGE);
        }
        // `stegobench score ~/Pictures` is the commonest wrong first command
        // there is, because it is what the tool sounds like it does. clap
        // answers it with the required flags it did not get, which is true
        // and tells the one person who most needs telling nothing at all.
        // The splash screen is three lines about this exact
        // misunderstanding, so the refusal says the same thing.
        Err(e) if looks_like_a_folder_of_photos(&e) => {
            eprintln!(
                "a path was given to `score` without a flag, and this does \
                 not take a folder of images that way."
            );
            eprintln!(
                "\nIf you have a labelled corpus, name it with --corpus, and \
                 a detector with --detector."
            );
            eprintln!(
                "If you meant your own pictures, this is not the tool: it \
                 measures how good a detector is, using images whose answers \
                 are already known, and it cannot tell you whether something \
                 is hidden in yours."
            );
            eprintln!("\n`stegobench help scope` is the whole of why.");
            std::process::exit(exit::USAGE);
        }
        Err(ref e) if misleading_suggestion(e).is_some() => {
            let typed = misleading_suggestion(e).expect("matched");
            let command = std::env::args().nth(1).unwrap_or_default();
            eprintln!("there is no `{typed}` here.");
            if command.is_empty() || command.starts_with('-') {
                eprintln!("`stegobench --help` lists every flag.");
            } else {
                eprintln!("`stegobench {command} --help` lists every flag it takes.");
            }
            std::process::exit(exit::USAGE);
        }
        Err(e) => e.exit(),
    }
}

/// Did clap offer a flag that has nothing to do with what was typed?
///
/// `Some(typed)` is the unknown flag, and the caller answers without the
/// suggestion. clap's did-you-mean is looser than this tool's: `--threads`
/// drew `--records`, five edits away and about writing files rather than
/// about concurrency, which sends a reader down a road that does not go
/// where they were headed. There is no concurrency flag, and no suggestion at
/// all is a better answer than a confident wrong one.
///
/// A genuine near miss still gets clap's own message, which is the better one
/// for it: `--corpu` is one edit from `--corpus` and passes.
fn misleading_suggestion(e: &clap::Error) -> Option<String> {
    if e.kind() != clap::error::ErrorKind::UnknownArgument {
        return None;
    }
    let typed = e.get(clap::error::ContextKind::InvalidArg)?.to_string();
    let suggested = e.get(clap::error::ContextKind::SuggestedArg)?.to_string();
    // Every suggestion clap made has to be a poor one before this takes over,
    // so a list holding one good match is still clap's to answer.
    let bare = |s: &str| s.trim_matches(|c: char| !c.is_alphanumeric()).to_string();
    let typed_bare = bare(&typed);
    let any_close = suggested
        .split(|c: char| c == ',' || c.is_whitespace())
        .map(bare)
        .filter(|s| !s.is_empty())
        .any(|s| near_enough(&typed_bare, &s));
    if any_close {
        return None;
    }
    Some(typed)
}

/// The same rule [`nearest_command`] applies: at most two edits, and no more
/// than half the longer word.
fn near_enough(typed: &str, candidate: &str) -> bool {
    let d = edit_distance(typed, candidate);
    d <= 2 && d * 2 <= candidate.len().max(typed.len())
}

/// Did somebody type `stegobench plan --corpus ...` and mean
/// `stegobench plan score --corpus ...`?
///
/// `Some(rest)` is the arguments they gave after `plan`, quoted so the
/// suggestion can be pasted back. Matched on the arguments rather than on
/// clap's message, for the same reason as [`looks_like_a_folder_of_photos`].
///
/// Only a leading flag counts. A leading word is either a verb `plan` can
/// read or one it refuses by name, and both of those are better answers than
/// this one.
fn plan_without_its_verb(e: &clap::Error) -> Option<String> {
    let argv: Vec<String> = std::env::args().skip(1).collect();
    plan_verb_hint(e.kind(), &argv)
}

/// The decision [`plan_without_its_verb`] makes, with the argv handed in so a
/// test can drive it without a process of its own.
fn plan_verb_hint(kind: clap::error::ErrorKind, argv: &[String]) -> Option<String> {
    if kind != clap::error::ErrorKind::UnknownArgument {
        return None;
    }
    if argv.first().map(String::as_str) != Some("plan") {
        return None;
    }
    let rest = &argv[1..];
    if !rest.first().is_some_and(|a| a.starts_with('-')) {
        return None;
    }
    Some(
        rest.iter()
            .map(|w| shell_word(w))
            .collect::<Vec<_>>()
            .join(" "),
    )
}

/// Did somebody type `stegobench score <path>` and mean "check these"?
///
/// Matched on the arguments rather than on clap's message, because the
/// message is prose and this has to keep working when it is reworded.
fn looks_like_a_folder_of_photos(e: &clap::Error) -> bool {
    let argv: Vec<String> = std::env::args().skip(1).collect();
    let Some(first) = argv.first() else {
        return false;
    };
    if first != "score" && first != "plan" {
        return false;
    }
    // A bare word that is not a flag and not the value of one. Only the
    // missing-argument and unexpected-argument failures, so a genuine typo in
    // a flag still gets clap's own answer, which is the better one for it.
    let kind = e.kind();
    if kind != clap::error::ErrorKind::MissingRequiredArgument
        && kind != clap::error::ErrorKind::UnknownArgument
    {
        return false;
    }
    let mut expecting_value = false;
    for arg in argv.iter().skip(1) {
        if expecting_value {
            expecting_value = false;
            continue;
        }
        if arg.starts_with('-') {
            expecting_value = !arg.contains('=');
            continue;
        }
        return true;
    }
    false
}

/// The visible command whose name is closest to what was typed.
///
/// Hidden commands are skipped deliberately: `check` and `scan` are signposts
/// rather than features, and suggesting one as the nearest match to some third
/// word would send a reader to a refusal.
fn nearest_command(typed: &str) -> Option<String> {
    let mut cmd = Cli::command();
    cmd.build();
    let typed = typed.to_lowercase();
    cmd.get_subcommands()
        .filter(|s| !s.is_hide_set())
        .map(|s| s.get_name().to_string())
        .map(|name| {
            let d = edit_distance(&typed, &name);
            (d, name)
        })
        // Half the word's length, so a genuinely unrelated word gets no
        // suggestion rather than a confident wrong one. Two edits at most, AND
        // no more than half the word: `chek` is three edits from `schema`,
        // which clap was happy to offer and which is a worse answer than
        // silence, because it sends somebody to a command about JSON schemas.
        .filter(|(_, name)| near_enough(&typed, name))
        .min_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)))
        .map(|(_, name)| name)
}

/// Levenshtein distance, two rows at a time.
///
/// Bounded by the product of two command names, both of which are at most a
/// dozen characters, so there is nothing here to cap.
fn edit_distance(a: &str, b: &str) -> usize {
    let a: Vec<char> = a.chars().collect();
    let b: Vec<char> = b.chars().collect();
    let mut prev: Vec<usize> = (0..=b.len()).collect();
    let mut cur = vec![0usize; b.len() + 1];
    for (i, ca) in a.iter().enumerate() {
        cur[0] = i + 1;
        for (j, cb) in b.iter().enumerate() {
            let cost = usize::from(ca != cb);
            cur[j + 1] = (prev[j] + cost).min(prev[j + 1] + 1).min(cur[j] + 1);
        }
        std::mem::swap(&mut prev, &mut cur);
    }
    prev[b.len()]
}

/// Whether a succeeding command's human text is the thing the caller asked
/// for, rather than a note about producing it.
///
/// A listing is content. `stegobench list detectors | grep zsteg` found
/// nothing and `stegobench describe zsteg > notes.txt` wrote an empty file,
/// because every one of these wrote its whole answer to stderr while
/// `--help` promised stdout carried the content.
///
/// `doctor` is not here and reaches stdout by its own route: it prints the
/// same report whether it ends in 0 or 8, so the decision cannot be made from
/// the exit code the way it is for everything else.
fn human_is_content(command: &Command) -> bool {
    match command {
        // An estimate and a set of figures are both the answer somebody ran
        // the command to get, so `plan score ... > estimate.txt` and
        // `metrics < scores.json | tee` work. Only reached on exit 0, so a
        // refusal from either still goes to stderr.
        Command::List { .. }
        | Command::Describe { .. }
        | Command::Help { .. }
        | Command::Plan { .. }
        | Command::Metrics { .. }
        | Command::Completions { .. } => true,
        // Named rather than caught by a wildcard, so adding a command is a
        // decision about which stream it writes to instead of a default.
        Command::Schema { .. }
        | Command::Validate { .. }
        | Command::Verify { .. }
        | Command::Embed { .. }
        | Command::Doctor { .. }
        | Command::Score { .. }
        | Command::Fetch { .. }
        | Command::Report { .. }
        | Command::Examine { .. } => false,
    }
}

fn main() {
    let cli = parse_or_explain();
    let out = run(&cli);

    if cli.json {
        let mut stdout = std::io::stdout().lock();
        let _ = serde_json::to_writer_pretty(&mut stdout, &out.json);
        let _ = writeln!(stdout);
    } else if out.payload_on_stdout {
        let mut stdout = std::io::stdout().lock();
        let _ = writeln!(stdout, "{}", out.human);
    } else if out.code == exit::OK {
        // Human mode still puts the payload on stdout when the payload IS the
        // point, so redirecting to a file works without remembering a flag.
        // `schema` writes structured JSON even in human mode, because the
        // schema itself is JSON; `completions` and `help` write the plain
        // text a shell or a reader wants, not a JSON wrapper around it.
        if matches!(cli.command, Some(Command::Schema { .. })) {
            let mut stdout = std::io::stdout().lock();
            let _ = serde_json::to_writer_pretty(&mut stdout, &out.json);
            let _ = writeln!(stdout);
        } else if cli.command.as_ref().is_some_and(human_is_content) {
            let mut stdout = std::io::stdout().lock();
            let _ = writeln!(stdout, "{}", out.human);
        } else {
            eprintln!("{}", out.human);
        }
    } else {
        eprintln!("{}", out.human);
    }

    std::process::exit(out.code);
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    /// Naming no destination is not the same as asking for the document to
    /// be thrown away.
    ///
    /// One detector and no `--out` used to resolve to "nowhere", and the
    /// caller reads that as "write to the file the caller named", so with no
    /// file named the document was silently discarded under exit 0.
    #[test]
    fn a_run_that_names_no_destination_still_keeps_its_document() {
        let tmp = tempfile::tempdir().expect("a temporary directory");
        let corpus = tmp.path().join("starter");
        let expected = tmp.path().join("starter.results");

        for many in [false, true] {
            assert_eq!(
                resolve_out(&corpus, None, many).expect("a destination"),
                Some(expected.clone()),
                "a run of {} detector(s) with no --out lost its document",
                if many { "several" } else { "one" }
            );
        }

        // And naming a file for one detector still means that file, which is
        // the case the old early return existed to serve.
        let file = tmp.path().join("one.json");
        assert_eq!(
            resolve_out(&corpus, Some(&file), false).expect("a file"),
            None
        );
    }

    #[test]
    fn a_half_corpus_run_keeps_its_own_records_file() {
        // The same collision the detector name already fixed, one dimension
        // along. `--split test` scores a subset in a different order, so a
        // records file written for the whole corpus does not line up with it.
        // Sharing the name makes the resume check refuse with "the corpus has
        // changed", which is true of nothing: the corpus is identical and only
        // the question was narrowed.
        let corpus = PathBuf::from("/data/pentimento-core");
        let whole = records_for(&corpus, None, &None, "stegcore", false, None);
        let test = records_for(
            &corpus,
            None,
            &None,
            "stegcore",
            false,
            Some(score::Side::Test),
        );
        let train = records_for(
            &corpus,
            None,
            &None,
            "stegcore",
            false,
            Some(score::Side::Train),
        );
        assert_ne!(whole, test);
        assert_ne!(test, train);
        assert!(
            test.to_string_lossy()
                .ends_with("stegcore.test.records.jsonl"),
            "{}",
            test.display()
        );
    }

    #[test]
    fn a_records_path_the_caller_named_is_used_whatever_the_split() {
        // An explicit path is an instruction, not a suggestion. Decorating it
        // with the split would write somewhere the caller did not ask for and
        // leave them looking at an empty file.
        let corpus = PathBuf::from("/data/corpus");
        let asked = PathBuf::from("/tmp/mine.jsonl");
        let got = records_for(
            &corpus,
            Some(&asked),
            &None,
            "stegcore",
            false,
            Some(score::Side::Test),
        );
        assert_eq!(got, asked);
    }

    /// Every code `stegobench_core::exit` publishes, which is what the help
    /// text, `llms.txt` and the man page all print as the contract.
    const CONTRACT_EXIT_CODES: &[i32] = &[
        exit::OK,
        exit::FAILURE,
        exit::USAGE,
        exit::PREFLIGHT_REFUSED,
        exit::PLUGIN_FAILED,
        exit::VERIFY_MISMATCH,
        exit::SCHEMA_INVALID,
        exit::LICENCE_REFUSED,
        exit::ENVIRONMENT_UNFIT,
        exit::INTERRUPTED,
    ];

    #[test]
    fn the_command_tree_is_well_formed() {
        Cli::command().debug_assert();
    }

    /// `build.rs` renders one man page per subcommand from this same tree, and
    /// a subcommand's page can only name `--json` and `--registry` once clap
    /// has propagated the globals into it. Pinned here because the failure is
    /// silent: the pages still generate, they just quietly stop describing two
    /// flags the binary accepts, which is the drift the generation exists to
    /// prevent.
    #[test]
    fn the_globals_reach_every_subcommand_so_the_man_pages_can_name_them() {
        let mut root = Cli::command();
        root.build();
        let mut seen = 0;
        for sub in root.get_subcommands() {
            let longs: Vec<&str> = sub.get_arguments().filter_map(|a| a.get_long()).collect();
            for global in ["json", "registry"] {
                assert!(
                    longs.contains(&global),
                    "`{}` does not carry --{global}, so its man page cannot \
                     name a flag the binary accepts",
                    sub.get_name()
                );
            }
            seen += 1;
        }
        assert!(seen >= 9, "checked only {seen} subcommand(s)");
    }

    #[test]
    fn json_is_accepted_by_every_subcommand() {
        // The rule in the module docstring, asserted rather than trusted.
        // A subcommand added later without --json fails here.
        for args in [
            vec!["stegobench", "--json", "schema", "result-v1"],
            vec!["stegobench", "--json", "validate", "x.json"],
            vec!["stegobench", "--json", "list", "detectors"],
            vec!["stegobench", "--json", "describe", "steghide"],
            vec!["stegobench", "--json", "plan", "score"],
            vec!["stegobench", "--json", "doctor"],
            vec!["stegobench", "--json", "doctor", "--no-selftest"],
            vec![
                "stegobench",
                "--json",
                "score",
                "--corpus",
                "x",
                "--detector",
                "y",
            ],
            vec!["stegobench", "--json", "completions", "bash"],
            vec!["stegobench", "--json", "help", "pairing"],
            vec!["stegobench", "--json", "help"],
        ] {
            assert!(Cli::try_parse_from(&args).is_ok(), "rejected: {args:?}");
        }
    }

    /// Measured 2026-09-29, before the pre-flight existed: `score --corpus
    /// /nope/not-here` walked past the shape check, reached the loader, and
    /// printed "cannot read /nope/not-here: No such file or directory (os
    /// error 2)" with exit 1. A mistyped path is the commonest mistake there
    /// is and it deserves a sentence, not an errno under the code that means
    /// "something went wrong, try again".
    #[test]
    fn a_corpus_path_that_is_not_a_readable_directory_is_refused_in_plain_words() {
        let missing = corpus_path_problem(Path::new("/nope/not-here-either"))
            .expect("a path that is not there is a problem");
        assert!(
            missing.contains("no corpus at"),
            "says what is wrong: {missing}"
        );
        assert!(
            !missing.contains("os error"),
            "an operating system error number reached the user: {missing}"
        );

        let tmp = tempfile::tempdir().expect("tmp");
        let file = tmp.path().join("one.png");
        std::fs::write(&file, b"not really a png").expect("written");
        let is_file = corpus_path_problem(&file).expect("a file is a problem");
        assert!(
            is_file.contains("is a file"),
            "says what is wrong: {is_file}"
        );

        assert!(
            corpus_path_problem(tmp.path()).is_none(),
            "a readable directory is not a problem, and if it were this test \
             would be passing for the wrong reason"
        );
    }

    /// Measured 2026-09-30, in a first-time walkthrough: `score --corpus
    /// pentimento-core` answered "there is no corpus at pentimento-core:
    /// nothing is there". True, and it says nothing about the thing the reader
    /// got wrong, which is that the word "corpus" is spoken in two
    /// vocabularies here and they typed the other one.
    #[test]
    fn a_corpus_value_is_tried_as_an_id_before_it_is_refused_as_a_path() {
        let reg = Registry::load(&shipped_registry()).expect("the real registry loads");

        // Exit 3 throughout, not 2: the command line is well formed and the
        // thing it names is not there, which is the line this project's exit
        // codes already divide on.
        let as_id = corpus_argument_problem(Some(&reg), Path::new("pentimento-core"))
            .expect("a registered id is not a directory");
        assert_eq!(as_id.code, exit::PREFLIGHT_REFUSED);
        assert!(
            as_id.human.contains("registered corpus id")
                && as_id.human.contains("stegobench fetch pentimento-core"),
            "a registered id must be recognised as one: {}",
            as_id.human
        );

        let neither = corpus_argument_problem(Some(&reg), Path::new("pentimento"))
            .expect("a value that is nothing is a problem");
        assert_eq!(neither.code, exit::PREFLIGHT_REFUSED);
        assert!(
            neither.human.contains("as a directory") && neither.human.contains("as a corpus id"),
            "the refusal must say both meanings were tried: {}",
            neither.human
        );
        // The sentence the exit-code contract test reads is still here: the
        // wording grew, it did not move.
        assert!(neither.human.contains("no corpus at"), "{}", neither.human);
        assert!(
            neither.human.contains("pentimento-core"),
            "a near miss must be named: {}",
            neither.human
        );

        // A registry that could not be read says so rather than reporting an
        // absence it never established.
        let blind = corpus_argument_problem(None, Path::new("pentimento-core"))
            .expect("still not a directory");
        assert_eq!(blind.code, exit::PREFLIGHT_REFUSED);
        assert!(
            blind.human.contains("registry could not be read"),
            "{}",
            blind.human
        );

        // An existing directory is what the flag wants, and a path that is
        // there and the wrong shape stays the pre-flight refusal it was.
        let tmp = tempfile::tempdir().expect("tmp");
        assert!(corpus_argument_problem(Some(&reg), tmp.path()).is_none());
        let file = tmp.path().join("shard.tar");
        std::fs::write(&file, b"bytes").expect("written");
        let wrong_shape =
            corpus_argument_problem(Some(&reg), &file).expect("a file is not a corpus");
        assert_eq!(wrong_shape.code, exit::PREFLIGHT_REFUSED);
        assert!(
            wrong_shape.human.contains("is a file"),
            "{}",
            wrong_shape.human
        );
    }

    /// The mirror image: a directory handed to `fetch`, which takes an id.
    ///
    /// PREFLIGHT_REFUSED, not USAGE, and the same code `--corpus` gives for a
    /// directory that is not there. The two divide on whether the command
    /// line was malformed or whether what it named is absent, and an
    /// unregistered id is the second. These call sites disagreed with the
    /// path case until 2026-09-30 for no reason anybody had chosen.
    #[test]
    fn an_unregistered_corpus_id_names_the_close_ones_and_spots_a_directory() {
        let dir = shipped_registry();
        let resolved = resolved_at(&dir);

        let typo = unregistered_corpus_id(
            &resolved,
            "bossbas",
            "`stegobench list corpora` shows what is.",
        );
        assert_eq!(typo.code, exit::PREFLIGHT_REFUSED);
        assert!(
            typo.human.contains("Did you mean") && typo.human.contains("bossbase"),
            "{}",
            typo.human
        );
        // The registry it searched, named the way every other command names
        // it. "Not registered" is a claim about one directory.
        assert!(
            typo.human.lines().last() == Some(resolved.source.line().as_str()),
            "the refusal does not end by naming the registry:\n{}",
            typo.human
        );

        let tmp = tempfile::tempdir().expect("tmp");
        let as_path = unregistered_corpus_id(
            &resolved,
            &tmp.path().display().to_string(),
            "`stegobench list corpora` shows what is.",
        );
        assert_eq!(as_path.code, exit::PREFLIGHT_REFUSED);
        assert!(
            as_path.human.contains("an id names a dataset")
                && as_path.human.contains("score --corpus"),
            "a directory handed to an id flag must be recognised: {}",
            as_path.human
        );
    }

    /// A result document or a records file written inside the corpus JOINS the
    /// corpus: the next run reads a different set of bytes, computes a
    /// different digest, and reports that the corpus changed when nothing
    /// about the images did. `--out ./mycorpus/result.json` is a natural thing
    /// to type, so it is refused at the boundary.
    #[test]
    fn an_output_path_inside_the_corpus_is_refused_and_one_outside_it_is_not() {
        let tmp = tempfile::tempdir().expect("tmp");
        let corpus = tmp.path().join("mycorpus");
        std::fs::create_dir_all(&corpus).expect("made");

        let inside = output_inside_corpus(&corpus, "--out", Some(&corpus.join("result.json")))
            .expect("a file in the corpus is refused");
        assert!(
            inside.contains("inside the corpus") && inside.contains("digest"),
            "the refusal must say what would have happened: {inside}"
        );
        assert!(
            inside.contains("Beside it works"),
            "the refusal must say where to put it instead: {inside}"
        );

        // The corpus directory itself, which is what `--out <dir>` means when
        // several detectors are named.
        let itself =
            output_inside_corpus(&corpus, "--out", Some(&corpus)).expect("the corpus itself");
        assert!(itself.contains("itself"), "{itself}");

        // A path that walks out and back in again resolves to the same place.
        let round_trip = corpus.join("..").join("mycorpus").join("result.json");
        assert!(
            output_inside_corpus(&corpus, "--out", Some(&round_trip)).is_some(),
            "a path that leaves the corpus and returns was not recognised"
        );

        // Directories below the corpus that nothing has created yet are still
        // inside it, and a missing parent must not panic on the way to saying
        // so.
        assert!(
            output_inside_corpus(
                &corpus,
                "--records",
                Some(&corpus.join("not-made-yet").join("deeper").join("r.jsonl"))
            )
            .is_some(),
            "an unwritten directory inside the corpus was not recognised"
        );

        // Outside is allowed, whether or not it exists yet.
        assert!(
            output_inside_corpus(&corpus, "--out", Some(&tmp.path().join("result.json"))).is_none()
        );
        assert!(output_inside_corpus(
            &corpus,
            "--out",
            Some(&tmp.path().join("nowhere").join("yet").join("result.json"))
        )
        .is_none());
        // A sibling whose name merely starts with the corpus's is a different
        // directory, and a textual prefix test would have called it inside.
        assert!(output_inside_corpus(
            &corpus,
            "--out",
            Some(&tmp.path().join("mycorpus-2/r.json"))
        )
        .is_none());
        assert!(output_inside_corpus(&corpus, "--out", None).is_none());
    }

    /// A corpus reached through a symbolic link and an `--out` reached through
    /// the real directory are the same place, and a comparison of the strings
    /// would not have said so.
    #[cfg(unix)]
    #[test]
    fn a_symbolic_link_does_not_hide_an_output_path_inside_the_corpus() {
        let tmp = tempfile::tempdir().expect("tmp");
        let real = tmp.path().join("real-corpus");
        std::fs::create_dir_all(&real).expect("made");
        let link = tmp.path().join("linked");
        std::os::unix::fs::symlink(&real, &link).expect("linked");

        assert!(
            output_inside_corpus(&link, "--out", Some(&real.join("result.json"))).is_some(),
            "the link hid an output path inside the corpus"
        );
        assert!(
            output_inside_corpus(&real, "--out", Some(&link.join("result.json"))).is_some(),
            "the link hid an output path inside the corpus"
        );
    }

    /// The two refusals above, reaching a user through the commands that can
    /// raise them, and raised before anything is scored rather than after.
    #[test]
    fn score_and_plan_both_refuse_before_they_touch_the_corpus() {
        let dir = tempfile::tempdir().expect("tmp");
        let reg = dir.path().join("registry");
        std::fs::create_dir_all(&reg).expect("made");
        std::fs::write(
            reg.join("ghost.toml"),
            "name = \"ghost\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [binary]\ncommand = [\"definitely-not-installed-xyzzy\"]\n\
             version_args = [\"--version\"]\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nargv = [\"{file}\"]\nparser = \"number\"\n\
             [selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
        )
        .expect("written");
        let corpus = dir.path().join("mycorpus");
        std::fs::create_dir_all(&corpus).expect("made");
        let inside = corpus.join("result.json");

        let scored = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["ghost".to_string()],
                corpus_id: None,
                trained_on: None,
                split: None,
                records: None,
                out: Some(&inside),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
            },
        );
        assert_eq!(scored.code, exit::USAGE, "{}", scored.human);
        assert!(
            scored.human.contains("inside the corpus"),
            "{}",
            scored.human
        );
        assert!(
            !inside.exists(),
            "the refusal wrote into the corpus it was refusing to write into"
        );

        // `plan` is the command whose whole job is to answer before the run,
        // so a plan that approves a run `score` would refuse is the one
        // answer it must never give.
        let planned = cmd_plan(
            &resolved_at(&reg),
            &[
                "score".to_string(),
                "--corpus".to_string(),
                corpus.display().to_string(),
                "--detector".to_string(),
                "ghost".to_string(),
                "--records".to_string(),
                corpus.join("r.jsonl").display().to_string(),
            ],
        );
        assert_eq!(planned.code, exit::USAGE, "{}", planned.human);
        assert!(
            planned.human.contains("inside the corpus"),
            "{}",
            planned.human
        );
    }

    /// `score --corpus <a registered id>` is the mistake the two vocabularies
    /// invite, and it reaches the user through the command rather than only
    /// through the helper.
    #[test]
    fn score_answers_a_registered_id_in_the_path_flag_with_the_way_out() {
        let refused = cmd_score(
            &resolved_at(shipped_registry()),
            ScoreRequest {
                corpus: Path::new("pentimento-core"),
                detectors: &["zsteg".to_string()],
                corpus_id: None,
                trained_on: None,
                split: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
            },
        );
        assert_eq!(refused.code, exit::PREFLIGHT_REFUSED, "{}", refused.human);
        assert!(
            refused.human.contains("stegobench fetch pentimento-core"),
            "{}",
            refused.human
        );
    }

    /// An environment variable bound with clap's `env` is read before any of
    /// our code runs, and clap treats a variable that is set but empty as a
    /// flag supplied without its value. `STEGOBENCH_REGISTRY=` in a shell
    /// profile therefore made EVERY command refuse with "a value is required
    /// for '--registry <DIR>'", including `--help`. Every variable is now
    /// read by the resolver that wants it, through `non_empty`, which treats
    /// empty as unset. This walks the real tree so a later argument cannot
    /// reintroduce the binding quietly.
    /// The third of the three variables, and the only one whose resolver is
    /// here rather than in a module of its own. Asserted through the seam,
    /// because setting a variable is process-global and this suite runs its
    /// tests on several threads at once.
    #[test]
    fn the_corpus_directory_comes_from_the_flag_then_the_environment_then_the_default() {
        let named = Path::new("/tmp/named-by-the-flag");
        assert_eq!(
            fetch_dest(Some(named), Some("/tmp/named-by-the-variable")),
            named,
            "the environment overrode an explicit --dest"
        );
        assert_eq!(
            fetch_dest(None, Some("/tmp/named-by-the-variable")),
            Path::new("/tmp/named-by-the-variable"),
            "the variable was not read"
        );
        // An empty value is not an answer, so the default answers instead.
        assert_eq!(fetch_dest(None, Some("")), fetch::default_dest());
        assert_eq!(fetch_dest(None, None), fetch::default_dest());
    }

    #[test]
    fn no_argument_reads_its_environment_variable_through_clap() {
        fn walk(cmd: &clap::Command, found: &mut Vec<String>) {
            for arg in cmd.get_arguments() {
                if let Some(var) = arg.get_env() {
                    found.push(format!(
                        "{} --{} reads {}",
                        cmd.get_name(),
                        arg.get_id(),
                        var.to_string_lossy()
                    ));
                }
            }
            for sub in cmd.get_subcommands() {
                walk(sub, found);
            }
        }
        let root = Cli::command();
        let mut found = Vec::new();
        walk(&root, &mut found);
        assert!(
            found.is_empty(),
            "an empty value in one of these variables would refuse every \
             command: {}",
            found.join(", ")
        );
    }

    /// L: walks the actual command tree from `Cli::command()` rather than a
    /// hand-kept list, so a subcommand added later without `--json` fails
    /// here even if nobody remembered to update a fixture list by hand. Each
    /// subcommand's required positionals are filled with a placeholder value
    /// picked from what the argument declares (or `bash` for the one enum
    /// value, `completions`' shell), since the parse succeeding is what is
    /// under test, not what the placeholder does downstream.
    //
    // The vocabulary gate immediately below walks the same tree.
    /// `--help` is for the reader, and this project's own process vocabulary
    /// is not for the reader.
    ///
    /// RAISED BY A JOURNEY, 2026-10-02. A forensic analyst reading
    /// `examine --help` found the sentence "a journey watched a reader meet
    /// that message", and listed it as internal language showing through. It
    /// was a `///` comment on a flag, which clap renders into the help, so
    /// rationale written for maintainers was being printed to users.
    ///
    /// Walked over the rendered help of every subcommand rather than over the
    /// source, because the defect is what clap PRINTS: the same words in a
    /// `//` comment are correct and invisible, and a check on the source
    /// could not tell the two apart.
    #[test]
    fn no_help_text_carries_this_project_s_internal_vocabulary() {
        /// Words that mean something inside this project and nothing to a
        /// reader. Not a spell checker: each of these is a term of art here
        /// with an ordinary meaning elsewhere, so the match is deliberately
        /// narrow and a false positive is rewritten rather than exempted.
        const INTERNAL: &[&str] = &[
            "a journey",
            "journeys",
            "persona",
            "rung",
            "the panel",
            "subagent",
            "DEFERRED",
            "the baseline",
            "the operator decided",
        ];
        let root = Cli::command();
        let mut checked = 0;
        let mut found: Vec<String> = Vec::new();
        for sub in root.get_subcommands() {
            let name = sub.get_name().to_string();
            let rendered = sub.clone().render_long_help().to_string();
            checked += 1;
            for word in INTERNAL {
                if rendered.to_lowercase().contains(&word.to_lowercase()) {
                    let line = rendered
                        .lines()
                        .find(|l| l.to_lowercase().contains(&word.to_lowercase()))
                        .unwrap_or("")
                        .trim()
                        .to_string();
                    found.push(format!("{name} --help says {word:?}: {line}"));
                }
            }
        }
        assert!(
            checked >= 10,
            "rendered help for {checked} subcommand(s), so this looked at \
             almost nothing"
        );
        assert!(
            found.is_empty(),
            "internal vocabulary reached the help text. A `///` comment on a \
             flag is printed to users; move the rationale to a `//` comment:\n{}",
            found.join("\n")
        );
    }

    #[test]
    fn every_subcommand_in_the_tree_accepts_json_and_the_run_output_is_parseable() {
        let root = Cli::command();
        // A walker over an empty tree passes every assertion inside the loop
        // by never reaching one, which is the same fault as a check that
        // reports clean because it could not look. The tree is therefore
        // required to hold at least the commands the docs name.
        let walked = root.get_subcommands().count();
        assert!(
            walked >= 10,
            "walked {walked} subcommand(s); the command tree carries schema, \
             validate, verify, list, describe, doctor, plan, score, \
             completions and help, so this walk looked at almost nothing"
        );
        for sub in root.get_subcommands() {
            let name = sub.get_name().to_string();
            let mut argv = vec!["stegobench".to_string(), "--json".to_string(), name.clone()];
            for arg in sub.get_arguments() {
                if !arg.is_required_set() {
                    continue;
                }
                let placeholder = if name == "completions" {
                    "bash".to_string()
                } else {
                    "x".to_string()
                };
                if let Some(long) = arg.get_long() {
                    argv.push(format!("--{long}"));
                }
                argv.push(placeholder);
            }
            // `metrics` READS STANDARD INPUT WHEN GIVEN NO FILE, AND THIS
            // TEST RUNS IN A PROCESS WHOSE STDIN IT DOES NOT CONTROL.
            //
            // Under `cargo test` stdin is inherited, and an inherited pipe
            // that stays open and never delivers a byte made this test wait
            // for ever: twelve minutes in a read on fd 0 before it was
            // stopped by hand, with nothing in the output to say why. So it
            // is handed a file it will fail to open, which exercises the same
            // envelope this test is about and touches no stream.
            if name == "metrics" {
                argv.push("no-such-scores-file.json".to_string());
            }
            let parsed = Cli::try_parse_from(&argv);
            assert!(parsed.is_ok(), "{name:?} rejected --json: {argv:?}");

            // And the command actually runs to a well-formed, parseable JSON
            // payload rather than merely parsing its flags. `score`, `plan`
            // and any future not-yet-built command still emit a JSON error
            // object, which is exactly the point: --json is honoured on the
            // refusal path too.
            //
            // Asserting `to_string(&out.json).is_ok()` would be a check that
            // cannot fail: `out.json` is already a `serde_json::Value`, whose
            // object keys are `String`s, so serialising one always succeeds.
            // The bytes are therefore rendered and re-parsed, and the result
            // is required to be the documented envelope (an object) carrying
            // a code the exit contract actually names.
            let cli = parsed.unwrap();
            let out = run(&cli);
            let rendered =
                serde_json::to_string(&out.json).expect("a Value always renders to text");
            let reparsed: serde_json::Value = serde_json::from_str(&rendered)
                .unwrap_or_else(|e| panic!("{name:?} produced JSON that will not re-parse: {e}"));
            assert!(
                reparsed.is_object(),
                "{name:?} emitted {reparsed} on --json, but every subcommand's \
                 machine output is a JSON object"
            );
            assert!(
                CONTRACT_EXIT_CODES.contains(&out.code),
                "{name:?} exited {} with --json, which is not in the exit code \
                 contract the help text and the man page publish",
                out.code
            );
            assert!(
                !out.human.trim().is_empty(),
                "{name:?} produced no human text, so a failure would print \
                 nothing to stderr"
            );
        }
    }

    /// The registry this repository actually ships, not a fixture.
    fn shipped_registry() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../plugins/registry")
    }

    /// A registry loaded from exactly the directory a test names.
    ///
    /// Deliberately the explicit route rather than `registry::resolve`, so a
    /// test never picks up whichever registry happens to be installed on the
    /// machine running it.
    fn resolved_at(dir: impl AsRef<Path>) -> Resolved {
        Resolved::from_dir(dir.as_ref()).expect("the registry under test loads")
    }

    /// Every corpus file in the repository is loaded and validated.
    ///
    /// The count is asserted first and deliberately. A loop over an empty
    /// directory passes every assertion inside it by never reaching one, which
    /// is the same fault as a check that reports clean because it could not
    /// look: this file would go green on a registry with the corpora deleted.
    #[test]
    fn every_shipped_corpus_validates_and_there_is_at_least_one_to_validate() {
        let reg = Registry::load(&shipped_registry()).expect("the real registry loads");
        assert!(
            !reg.corpora.is_empty(),
            "no corpora were loaded from {}, so this test verified nothing. A \
             count of zero here is a failure, not a pass",
            shipped_registry().display()
        );
        for (id, corpus) in &reg.corpora {
            assert_eq!(corpus.validate(), Ok(()), "{id} is registered but invalid");
            assert_eq!(id, &corpus.id, "keyed under a name that is not its id");
        }
    }

    /// Three commands read one registry, and they have to agree about whether
    /// an entry is usable.
    ///
    /// RAISED BY A JOURNEY, 2026-10-02. With `aletheia-rich`'s image on the
    /// machine, `describe` said "stegobench can run it" and `list detectors`
    /// showed it beside the six that work, while `plan` said it "declares no
    /// invoke block, so nothing in its registry entry says what command to
    /// launch". The reader was left to work out which of the three was wrong,
    /// and the honest answer was the two that sounded encouraging.
    ///
    /// Written over the registry this repository ships rather than a fixture,
    /// because the contradiction needed a real entry to appear at all: every
    /// fixture in `needs` modelled a detector that was present and drivable,
    /// and three of them modelled one that was present and could never have
    /// been driven while asserting it was ready.
    ///
    /// Presence is supplied here rather than probed, so the answer is the same
    /// on a machine with every image pulled and on one with no container
    /// runtime, and so the test cannot pass by finding nothing present.
    #[test]
    fn no_surface_calls_an_entry_usable_that_nothing_here_can_drive() {
        let reg = Registry::load(&shipped_registry()).expect("the real registry loads");
        assert!(
            !reg.entries.is_empty(),
            "no tools were loaded from {}, so this test verified nothing",
            shipped_registry().display()
        );
        let mut undrivable = 0;
        for entry in reg.entries.values() {
            let present = stegobench_plugin::availability::Availability {
                name: entry.name.clone(),
                presence: stegobench_plugin::availability::Presence::Present {
                    pin: "supplied by the test".into(),
                },
                verified: None,
                missing_secrets: Vec::new(),
            };
            let needs = needs::of_tool(entry, &present, &[]);
            let word = readiness_word(&needs, "tool", &entry.name);
            let row = entry.summary();
            if entry.can_be_driven() {
                assert_eq!(
                    needs.readiness,
                    needs::Readiness::Ready,
                    "{} is drivable and present and was not reported ready",
                    entry.name
                );
                assert!(
                    !row.contains("can't drive it"),
                    "{} is drivable and its listing row says otherwise: {row}",
                    entry.name
                );
                continue;
            }
            undrivable += 1;
            assert_eq!(
                needs.readiness,
                needs::Readiness::CannotBeDriven,
                "{} declares nothing to drive and was reported otherwise",
                entry.name
            );
            assert_ne!(
                word, "stegobench can run it",
                "describe says it can run {}, which no command here can",
                entry.name
            );
            assert!(
                row.contains("can't drive it"),
                "{} sits in the listing unmarked: {row}",
                entry.name
            );
            // The adapter roots are empty and no probe should be needed: the
            // answer is a property of the entry, so it is reached without
            // asking this machine anything.
            let why = stegobench_cli::unavailable_reason(entry, &[])
                .unwrap_or_else(|| panic!("{} would be run by score and plan", entry.name));
            assert!(
                why.contains("block"),
                "{} is skipped for a reason that does not name the missing \
                 block: {why}",
                entry.name
            );
        }
        // Not a claim about how many; a claim that the undrivable branch above
        // was entered at all. Zero would mean every assertion in it was
        // skipped and this test measured only the easy half.
        assert!(
            undrivable > 0,
            "no registered entry is undrivable, so the branch that this test \
             exists for was never reached. If that is now true of the \
             registry, delete this assertion and say so."
        );
    }

    /// Every result document this repository ships must satisfy this
    /// repository's own validator.
    ///
    /// It did not, and nothing noticed. Two fields were added to `result-v1`
    /// in earlier work and every document under `results/` was left
    /// behind, so a reader following the README's own instruction to
    /// validate a document would have been told the project's own published
    /// measurements are not valid documents. A benchmark whose sample output
    /// its own tool refuses has undermined the point of publishing it.
    ///
    /// The same failure will happen again on the next schema change; the
    /// difference is that it will happen here rather than in front of a
    /// reader.
    #[test]
    fn every_result_this_repository_publishes_validates() {
        let dir = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../..")
            .join("results/v1");
        let mut checked = 0;
        for entry in std::fs::read_dir(&dir).expect("results/v1 exists") {
            let path = entry.expect("readable").path();
            if path.extension().is_none_or(|e| e != "json") {
                continue;
            }
            let out = cmd_validate(&path);
            assert_eq!(
                out.code,
                exit::OK,
                "{} does not validate:\n{}",
                path.display(),
                out.human
            );
            checked += 1;
        }
        // A walk over an empty directory passes every assertion by never
        // reaching one, which is the same fault as a check reporting clean
        // because it could not look.
        assert!(
            checked >= 20,
            "checked {checked} document(s) in {}; this repository publishes \
             two dozen, so the walk found almost nothing",
            dir.display()
        );
    }

    /// One registry to a user: the same two verbs reach a corpus and a tool.
    #[test]
    fn list_corpora_and_describe_reach_a_corpus_the_way_they_reach_a_tool() {
        let dir = shipped_registry();
        let listed = cmd_list(&resolved_at(&dir), "corpora");
        assert_eq!(listed.code, exit::OK);
        assert!(
            listed.human.contains("reveal") && listed.human.contains("republish:"),
            "got: {}",
            listed.human
        );
        assert!(
            listed.json["count"].as_u64().is_some_and(|n| n > 0),
            "the machine output must carry the count, not only the rows"
        );

        let described = cmd_describe(&resolved_at(&dir), "reveal", false);
        assert_eq!(described.code, exit::OK);
        assert_eq!(described.json["licence"]["redistribution"], "permitted");
        // The summary carries the date the terms were read, in the summary's
        // own words rather than as the field name the TOML dump used to show.
        assert!(
            described.human.contains("verified_on"),
            "the summary drops the verification date: {}",
            described.human
        );
        assert!(
            described.human.contains("Republish"),
            "the summary drops the redistribution answer: {}",
            described.human
        );

        // And a tool still answers the same verb.
        assert_eq!(
            cmd_describe(&resolved_at(&dir), "steghide", false).code,
            exit::OK
        );
        // An unknown name is a usage error that names both kinds.
        let missing = cmd_describe(&resolved_at(&dir), "not-registered", false);
        assert_eq!(missing.code, exit::USAGE);
        assert!(missing.human.contains("reveal") && missing.human.contains("steghide"));
    }

    /// `describe` used to print the whole registry entry as TOML under its
    /// summary, which is forty lines of fields the harness reads and the
    /// reader does not, between them and the next command they have to type.
    ///
    /// The entry is still one flag away, and under that flag it is ALONE:
    /// anything else on the stream makes the output unpipeable, which is the
    /// only reason a raw form exists.
    #[test]
    fn describe_summarises_by_default_and_prints_the_entry_only_under_toml() {
        let dir = shipped_registry();
        for name in ["steghide", "reveal"] {
            let summary = cmd_describe(&resolved_at(&dir), name, false);
            assert_eq!(summary.code, exit::OK);
            assert!(
                !summary.human.contains("[licence]") && !summary.human.contains("[properties]"),
                "{name}'s summary still carries the raw TOML: {}",
                summary.human
            );
            assert!(
                summary.human.contains("--toml"),
                "{name}'s summary does not say where the whole entry went: {}",
                summary.human
            );

            let raw = cmd_describe(&resolved_at(&dir), name, true);
            assert_eq!(raw.code, exit::OK);
            assert!(
                raw.payload_on_stdout,
                "{name} --toml was diverted to stderr, so it cannot be piped"
            );
            let parsed: toml::Value = toml::from_str(&raw.human).unwrap_or_else(|e| {
                panic!("{name} --toml is not parseable TOML: {e}\n{}", raw.human)
            });
            assert!(
                parsed.get("name").is_some() || parsed.get("id").is_some(),
                "{name} --toml is missing the entry itself: {}",
                raw.human
            );
            assert!(
                !raw.human.contains("Needs from you") && !raw.human.contains("Nothing needed"),
                "{name} --toml carries the summary too: {}",
                raw.human
            );
        }

        // `stegobench help scope` promises that `describe` says where a tool
        // lives, what it costs and the command that runs it. That promise used
        // to be kept by the TOML dump, so the summary has to keep it now.
        let zsteg = cmd_describe(&resolved_at(&dir), "zsteg", false);
        assert_eq!(zsteg.code, exit::OK);
        // The arguments, not the program name. zsteg's image already has
        // `zsteg` as its entrypoint, so naming it again in the registry ran
        // it twice; asserting on the doubled form would hold that bug in
        // place.
        for expected in ["Runs as", "Costs", "Run it", "-a /work/{file}"] {
            assert!(
                zsteg.human.contains(expected),
                "the summary drops {expected:?}: {}",
                zsteg.human
            );
        }
    }

    /// The commonest way to reach `describe`'s refusal with something that is
    /// not a typo is to hand it the path that `score --corpus` wants. A list
    /// of registered names that does not contain theirs does not tell them so.
    #[test]
    fn describe_says_it_takes_a_name_when_it_is_handed_a_path() {
        let dir = shipped_registry();
        let tmp = tempfile::tempdir().expect("tmp");
        let refused = cmd_describe(&resolved_at(&dir), &tmp.path().display().to_string(), false);
        assert_eq!(refused.code, exit::USAGE);
        assert!(
            refused.human.contains("is a path on this machine")
                && refused.human.contains("score --corpus"),
            "{}",
            refused.human
        );

        // A near miss is still a near miss, and gets the suggestion instead.
        let typo = cmd_describe(&resolved_at(&dir), "steghid", false);
        assert_eq!(typo.code, exit::USAGE);
        assert!(
            typo.human.contains("Did you mean") && typo.human.contains("steghide"),
            "{}",
            typo.human
        );
    }

    /// Every listed row's columns start in the same place, including the
    /// longest name's.
    ///
    /// This is the regression the fixed `{:<16}` pad could not survive:
    /// `stegobench-starter` is eighteen characters, so its row overflowed the
    /// column and every cell after it on that line sat two places right of
    /// everybody else's. A test that only looked for the text somewhere in the
    /// output passed throughout.
    #[test]
    fn a_listing_lines_its_columns_up_however_long_the_longest_name_is() {
        let dir = shipped_registry();
        for kind in ["corpora", "detectors", "embedders"] {
            let out = cmd_list(&resolved_at(&dir), kind);
            assert_eq!(out.code, exit::OK, "{kind}");
            // The rows, which are the lines before the first blank one: the
            // legend and the footnotes below it are prose, not table.
            let rows: Vec<&str> = out
                .human
                .lines()
                .take_while(|l| !l.trim().is_empty())
                .collect();
            assert!(rows.len() > 1, "{kind}: nothing to align");
            // Byte offsets throughout, which is the same as character offsets
            // here because every registered name is ASCII. `table::align` is
            // the place that has to care about the difference.
            let longest = rows
                .iter()
                .map(|r| r.split_whitespace().next().unwrap_or("").len())
                .max()
                .expect("rows");
            for row in &rows {
                let name = row.split_whitespace().next().expect("a name");
                // Where the second column begins: past the name, past its
                // padding, at the first character that is not a space.
                let second = row[name.len()..]
                    .find(|c: char| c != ' ')
                    .map(|i| i + name.len());
                assert_eq!(
                    second,
                    Some(longest + 1),
                    "{kind}: {row:?} does not start its second column where the others do"
                );
            }
            assert!(
                rows.iter().any(|r| r.len() > longest + 1),
                "{kind}: every row is a bare name, so this proved nothing"
            );
        }
    }

    /// `list all` is the whole registry, so leaving corpora out of it would
    /// make the two halves reachable only by knowing which is which.
    #[test]
    fn list_all_carries_the_corpora_as_well_as_the_tools() {
        let out = cmd_list(&resolved_at(shipped_registry()), "all");
        assert_eq!(out.code, exit::OK);
        assert!(out.json["tools"].as_array().is_some_and(|a| !a.is_empty()));
        assert!(out.json["corpora"]
            .as_array()
            .is_some_and(|a| !a.is_empty()));
        assert!(out.human.contains("republishable"), "got: {}", out.human);
    }

    /// The tools half had the fault the corpora half was written to avoid: it
    /// printed `nothing registered under "detectors"` and exited zero, which
    /// says neither where it looked nor whether anything is registered at all.
    #[test]
    fn an_empty_tools_listing_says_where_it_looked() {
        let dir = tempfile::tempdir().unwrap();
        let out = cmd_list(&resolved_at(dir.path()), "detectors");
        assert_eq!(out.code, exit::OK);
        assert!(
            out.human.contains("no tools registered")
                && out.human.contains(&dir.path().display().to_string()),
            "got: {}",
            out.human
        );
    }

    /// A registry with tools but none of this kind is a different fact from an
    /// empty registry, and calls for a different next step.
    #[test]
    fn a_kind_with_no_tools_is_distinguished_from_an_empty_registry() {
        // Built here rather than read from the shipped registry, which has
        // both kinds: a test that only checks this when the registry happens
        // to be one-sided is a test that can pass without looking.
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("only-a-detector.toml"),
            format!(
                "name = \"solo\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
                 [image]\nreference = \"ghcr.io/x/y@sha256:{}\"\n\
                 [selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
                "5".repeat(64)
            ),
        )
        .unwrap();

        let out = cmd_list(&resolved_at(dir.path()), "embedders");
        assert_eq!(out.code, exit::OK);
        assert!(out.json["tools"].as_array().is_some_and(|a| a.is_empty()));
        assert!(
            out.human.contains("none of the 1 tool(s)") && out.human.contains("is a embedder"),
            "got: {}",
            out.human
        );
        assert!(
            !out.human.contains("no tools registered"),
            "a registry with a tool in it was reported as empty: {}",
            out.human
        );
    }

    /// An empty listing under exit zero reads as "checked, all fine". It has
    /// to say that nothing is declared, which is a different fact.
    #[test]
    fn an_empty_corpora_listing_says_so_rather_than_printing_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let out = cmd_list(&resolved_at(dir.path()), "corpora");
        assert_eq!(out.code, exit::OK);
        assert_eq!(out.json["count"], 0);
        assert!(
            out.human.contains("no corpora registered"),
            "an empty registry printed: {:?}",
            out.human
        );
    }

    /// The defect the analysis called the worst one: the first command a
    /// person types must work from anywhere. Pinned at the parser, because
    /// the fix was removing a default value and a default value is one line to
    /// put back.
    #[test]
    fn the_registry_flag_carries_no_relative_default_value() {
        let mut cmd = Cli::command();
        cmd.build();
        let arg = cmd
            .get_arguments()
            .find(|a| a.get_long() == Some("registry"))
            .expect("--registry exists");
        assert!(
            arg.get_default_values().is_empty(),
            "--registry has a default value again, so an installed binary run \
             outside a checkout resolves it against the current directory and \
             every command that reads a registry fails"
        );
    }

    /// A bare invocation used to print three screens. Somebody who types one
    /// word is asking what this is, and that answer is short.
    #[test]
    fn a_bare_invocation_points_somewhere_rather_than_printing_everything() {
        let out = cmd_orientation(None);
        assert_eq!(out.code, exit::USAGE);
        assert!(
            out.payload_on_stdout,
            "the orientation text is the answer to what was asked, so it \
             belongs on stdout"
        );
        assert!(
            out.human.contains("stegobench list detectors"),
            "no obvious next command: {}",
            out.human
        );
        assert!(
            out.human.contains("help scope"),
            "the bare invocation does not say what the tool is not for: {}",
            out.human
        );
        assert!(
            out.human.lines().count() <= 16,
            "the orientation is {} lines, which is the whole help again",
            out.human.lines().count()
        );
    }

    /// `check` and `scan` are the two words rungs 2 and 3 guess, and clap
    /// suggested `schema` for one and nothing for the other.
    ///
    /// They used to be signposts that refused and explained the tool could
    /// not examine your own images. `examine` made that explanation false, so
    /// they are aliases on it now, and this test changed direction with them:
    /// what it protects is that the guessed word still lands somewhere
    /// deliberate rather than on clap's did-you-mean.
    #[test]
    fn the_guessed_words_run_examine_rather_than_reporting_a_typo() {
        for word in ["check", "scan", "inspect", "detect", "analyse", "analyze"] {
            let parsed = Cli::try_parse_from(["stegobench", word, "./photo.png", "-d", "zsteg"])
                .unwrap_or_else(|e| panic!("`{word}` is rejected rather than routed: {e}"));
            assert!(
                matches!(parsed.command, Some(Command::Examine { .. })),
                "`{word}` parses as something other than examine"
            );
        }
    }

    /// And the alias refuses before it starts anything, on the same path a
    /// real name would.
    ///
    /// Deliberately a file that does not exist: this calls `run`, and a test
    /// that reaches a container would spend the per image timeout waiting for
    /// a detector nobody asked it to measure anything with.
    #[test]
    fn an_alias_refuses_a_missing_file_without_starting_a_detector() {
        let parsed = Cli::try_parse_from([
            "stegobench",
            "check",
            "./no-such-image-exists-here.png",
            "-d",
            "zsteg",
        ])
        .expect("the alias parses");
        let out = run(&parsed);
        assert_eq!(out.code, exit::PREFLIGHT_REFUSED, "{}", out.human);
        assert_eq!(out.json["reason"], "not-a-file", "{}", out.json);
    }

    /// EVERY word on both lists.
    ///
    /// `detect` is the likeliest first guess of all, and it once fell through
    /// to the generic "there is no `stegobench detect`" while the tailored
    /// answer sat one match arm away. Both lists are walked here so that
    /// adding a word and forgetting to wire it up fails rather than ships.
    #[test]
    fn every_extraction_word_is_told_that_extraction_is_absent() {
        assert!(
            NO_EXTRACTION.windows(2).all(|w| w[0] < w[1]),
            "the list is meant to stay sorted so a reader can find a word in it"
        );
        for word in NO_EXTRACTION {
            assert!(no_extraction(word), "`{word}` is on the list and unmatched");
            assert!(
                no_extraction(&word.to_uppercase()),
                "`{word}` typed in capitals is the same misunderstanding"
            );
            // And it is on exactly one list. A word that is on both would get
            // whichever answer the dispatch happens to try first, and the two
            // answers say opposite things about whether the thing exists.
            assert!(
                !meant_examine(word),
                "`{word}` is on both lists, so its answer depends on arm order"
            );
            let out = cmd_no_extraction(word);
            assert_eq!(out.code, exit::USAGE, "`{word}` should be a usage error");
            for expected in [
                "does not pull a hidden payload",
                "examine",
                "list detectors",
            ] {
                assert!(
                    out.human.contains(expected),
                    "`{word}` does not say {expected:?}: {}",
                    out.human
                );
            }
            // Clap must reject it as an unknown subcommand, which is what
            // `parse_or_explain` catches it on. Anything else means the word
            // never reaches this answer at runtime.
            match Cli::try_parse_from(["stegobench", word, "./photo.png"]) {
                Ok(_) => panic!("`{word}` parsed as a command, and it is not one"),
                Err(e) => assert_eq!(
                    e.kind(),
                    clap::error::ErrorKind::InvalidSubcommand,
                    "`{word}` fails in a way `parse_or_explain` does not catch"
                ),
            }
        }
    }

    /// The words that now mean `examine`, each reaching it or reaching the
    /// redirect that names it.
    #[test]
    fn every_examiner_word_reaches_examine() {
        assert!(
            MEANT_EXAMINE.windows(2).all(|w| w[0] < w[1]),
            "the list is meant to stay sorted so a reader can find a word in it"
        );
        for word in MEANT_EXAMINE {
            assert!(meant_examine(word), "`{word}` is on the list and unmatched");
            assert!(
                meant_examine(&word.to_uppercase()),
                "`{word}` typed in capitals means the same thing"
            );
            // Either it is an alias clap parses straight into the command, or
            // clap rejects it and the redirect names `examine`.
            match Cli::try_parse_from(["stegobench", word, "./photo.png", "-d", "zsteg"]) {
                Ok(parsed) => assert!(
                    matches!(parsed.command, Some(Command::Examine { .. })),
                    "`{word}` parses as something other than examine"
                ),
                Err(e) => {
                    assert_eq!(
                        e.kind(),
                        clap::error::ErrorKind::InvalidSubcommand,
                        "`{word}` fails in a way `parse_or_explain` does not catch"
                    );
                    let out = cmd_meant_examine(word);
                    assert_eq!(out.code, exit::USAGE);
                    assert!(
                        out.human.contains("is `examine`"),
                        "`{word}` does not name the command: {}",
                        out.human
                    );
                }
            }
        }
    }

    /// Neither list may swallow a real command or a plain typo.
    #[test]
    fn a_real_command_is_never_read_as_a_guess() {
        for word in [
            "list",
            "score",
            "help",
            "doctor",
            "verify",
            "validate",
            "report",
            "frobnicate",
            "",
        ] {
            assert!(!no_extraction(word), "`{word}` was diverted");
            assert!(!meant_examine(word), "`{word}` was diverted");
        }
    }

    #[test]
    fn a_shell_word_is_quoted_only_when_the_shell_would_take_it_apart() {
        assert_eq!(shell_word("zsteg"), "zsteg");
        assert_eq!(shell_word("/work/{file}"), "/work/{file}");
        assert_eq!(
            shell_word("PYTHONPATH=/opt/aletheia"),
            "PYTHONPATH=/opt/aletheia"
        );
        assert_eq!(shell_word("/my pictures/a.png"), "'/my pictures/a.png'");
        assert_eq!(shell_word("it's.png"), r"'it'\''s.png'");
        assert_eq!(shell_word(""), "''");
    }

    fn parsed_entry(text: &str) -> stegobench_core::registry::Entry {
        toml::from_str(text).expect("the entry under test parses")
    }

    /// A containerised tool has no command anywhere a reader can paste, which
    /// is what sent a first-time user looking for `zsteg` on their PATH and
    /// then hand-writing a `docker run` out of `--toml`.
    #[test]
    fn a_containerised_tool_pastes_as_a_pinned_sandboxed_docker_run() {
        let e = parsed_entry(
            "name = \"zsteg\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [image]\nreference = \"stegobench/zsteg@sha256:abc\"\n\
             [emits]\noutput = \"verdict\"\n\
             [invoke]\nargv = [\"zsteg\", \"-a\", \"{file}\"]\nparser = \"zsteg\"\n",
        );
        let line = runnable_command(&e, &[]).expect("a container has a command");
        // The digest, because an unpinned paste measures whatever the tag
        // points at today.
        assert!(line.contains("stegobench/zsteg@sha256:abc"), "{line}");
        // The same sandbox the harness uses, not a friendlier one.
        for flag in [
            "--rm",
            "--network=none",
            "--cap-drop=ALL",
            "--security-opt no-new-privileges",
            "--read-only",
            "--memory=2g",
        ] {
            assert!(line.contains(flag), "{flag} is missing from {line}");
        }
        assert!(
            line.contains("-v \"$PWD/{file}\":/work/{file}:ro"),
            "{line}"
        );
        assert!(line.ends_with("zsteg -a /work/{file}"), "{line}");
        assert!(
            !line.contains("--user"),
            "a read-only run needs no uid: {line}"
        );
    }

    /// StegExpose scans a directory and writes beside its input, so the
    /// harness gives it the whole directory and a uid. A paste that mounted
    /// one read-only file would print nothing and exit zero.
    #[test]
    fn a_tool_that_writes_beside_its_input_pastes_with_the_directory_mounted() {
        let e = parsed_entry(
            "name = \"stegexpose\"\nkind = \"detector\"\nlicence = \"GPL-3.0\"\n\
             [image]\nreference = \"stegobench/stegexpose@sha256:def\"\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nargv = [\"/work\", \"default\", \"0.2\"]\n\
             writable_workdir = true\nparser = \"stegexpose\"\n",
        );
        let line = runnable_command(&e, &[]).expect("a container has a command");
        assert!(line.contains("-v \"$PWD\":/work"), "{line}");
        assert!(line.contains("--user $(id -u):$(id -g)"), "{line}");
        assert!(
            !line.contains("{file}"),
            "nothing to substitute here: {line}"
        );
    }

    /// The old line printed `invoke.argv` alone, which for a binary entry is
    /// the arguments without the program: `{file}`, by itself.
    #[test]
    fn a_binary_tool_pastes_with_the_program_name_it_needs() {
        let e = parsed_entry(
            "name = \"stegcore\"\nkind = \"detector\"\nlicence = \"AGPL-3.0-or-later\"\n\
             [binary]\ncommand = [\"stegcore\", \"analyse\", \"--json\"]\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nargv = [\"{file}\"]\nparser = \"stegcore\"\n",
        );
        assert_eq!(
            runnable_command(&e, &[]).as_deref(),
            Some("stegcore analyse --json {file}")
        );
    }

    /// An adapter that is not where the registry says it is would paste a
    /// mount of a path that does not exist, which fails at the runtime rather
    /// than where the reader can see why.
    #[test]
    fn an_entry_with_nothing_runnable_pastes_no_command_at_all() {
        let missing_adapter = parsed_entry(
            "name = \"aletheia-rs\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [image]\nreference = \"stegobench/aletheia@sha256:abc\"\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nadapter = \"nowhere/at/all.py\"\nentrypoint = \"python3\"\n\
             argv = [\"{adapter}\", \"{file}\"]\nparser = \"number\"\n",
        );
        assert_eq!(runnable_command(&missing_adapter, &[]), None);

        let nothing_declared = parsed_entry(
            "name = \"aletheia-rich\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [image]\nreference = \"stegobench/aletheia-rich@sha256:abc\"\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n",
        );
        assert_eq!(runnable_command(&nothing_declared, &[]), None);
    }

    /// An adapter that IS there is mounted read only and named by the path it
    /// has inside the container, not the one it has here.
    #[test]
    fn an_adapter_is_mounted_and_then_referred_to_by_its_inside_path() {
        let tmp = tempfile::tempdir().unwrap();
        let adapters = tmp.path().join("plugins/adapters");
        std::fs::create_dir_all(&adapters).unwrap();
        std::fs::write(adapters.join("one.py"), "print(0)\n").unwrap();
        let e = parsed_entry(
            "name = \"aletheia-rs\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [image]\nreference = \"stegobench/aletheia@sha256:abc\"\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nadapter = \"plugins/adapters/one.py\"\nentrypoint = \"python3\"\n\
             env = [\"PYTHONPATH=/opt/aletheia\"]\n\
             argv = [\"{adapter}\", \"{file}\", \"rs\"]\nparser = \"number\"\n",
        );
        let line = runnable_command(&e, &[tmp.path().to_path_buf()]).expect("resolvable");
        assert!(line.contains(":/adapter/one.py:ro"), "{line}");
        assert!(line.contains("-e PYTHONPATH=/opt/aletheia"), "{line}");
        assert!(line.contains("--entrypoint python3"), "{line}");
        assert!(line.ends_with("/adapter/one.py /work/{file} rs"), "{line}");
        assert!(
            !line.contains("{adapter}"),
            "the placeholder survived: {line}"
        );
    }

    /// The built-in registry unpacks its adapters into a scratch directory
    /// that is gone when the command ends, so `describe` must not print a
    /// command naming a path that will not be there when it is pasted.
    #[test]
    fn a_command_is_never_printed_against_an_adapter_that_will_not_survive() {
        let built_in = registry::Resolved::built_in().expect("the built-in registry loads");
        let out = cmd_describe(&built_in, "aletheia-rs", false);
        assert_eq!(out.code, exit::OK, "{}", out.human);
        let temp = std::env::temp_dir().display().to_string();
        assert!(
            !out.human.contains(&temp),
            "a scratch path was offered to be pasted: {}",
            out.human
        );
        assert!(
            out.human.contains("no command to paste"),
            "the reader is left with nothing and no reason: {}",
            out.human
        );
        assert!(
            out.human.contains("--registry plugins/registry"),
            "nothing says how to get the command: {}",
            out.human
        );
        // The same tool off a registry on disk does print one, so this is a
        // property of where the adapter lives rather than of the entry.
        let on_disk = cmd_describe(&resolved_at(shipped_registry()), "aletheia-rs", false);
        assert!(
            on_disk.human.contains("docker run --rm"),
            "a registry with its adapters beside it still cannot paste: {}",
            on_disk.human
        );
    }

    /// A service is reached by an adapter running HERE, so the image is the
    /// subject rather than the thing to start.
    #[test]
    fn a_service_pastes_the_adapter_that_runs_on_this_machine() {
        let tmp = tempfile::tempdir().unwrap();
        let adapters = tmp.path().join("plugins/adapters");
        std::fs::create_dir_all(&adapters).unwrap();
        std::fs::write(adapters.join("svc.py"), "print(0)\n").unwrap();
        let e = parsed_entry(
            "name = \"stegashield\"\nkind = \"detector\"\nlicence = \"proprietary\"\n\
             [image]\nreference = \"5iprojects/stegashield@sha256:abc\"\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nhost = true\nadapter = \"plugins/adapters/svc.py\"\n\
             entrypoint = \"python3\"\nargv = [\"{adapter}\", \"{file}\"]\nparser = \"number\"\n",
        );
        let line = runnable_command(&e, &[tmp.path().to_path_buf()]).expect("resolvable");
        assert!(line.starts_with("python3 "), "{line}");
        assert!(
            !line.contains("docker"),
            "a service is not started here: {line}"
        );
        assert!(line.ends_with("{file}"), "{line}");
    }

    /// Both status words were true about the harness and read as claims about
    /// the reader: `[ready]` sent one looking for a binary that only exists
    /// inside an image, and `[unknown]` on the corpus that ships in the binary
    /// read as a fault beside it.
    #[test]
    fn the_status_word_says_who_it_is_about() {
        let ready = needs::Needs {
            readiness: needs::Readiness::Ready,
            steps: Vec::new(),
        };
        assert_eq!(
            readiness_word(&ready, "tool", "zsteg"),
            "stegobench can run it"
        );

        let unknown = needs::Needs {
            readiness: needs::Readiness::Unknown,
            steps: Vec::new(),
        };
        assert_eq!(
            readiness_word(&unknown, "corpus", STARTER_ID),
            "included in this binary"
        );
        assert_eq!(
            readiness_word(&unknown, "corpus", "bossbase"),
            "bring your own copy"
        );
        // A tool whose readiness genuinely cannot be established keeps the
        // word that says so.
        assert_eq!(readiness_word(&unknown, "tool", "stegashield"), "unknown");

        let needs_you = needs::Needs {
            readiness: needs::Readiness::NeedsYou,
            steps: Vec::new(),
        };
        assert_eq!(readiness_word(&needs_you, "tool", "stegcore"), "NEEDS YOU");
    }

    /// Every whole number in a line, in the order it is printed.
    fn numbers_in(line: &str) -> Vec<u64> {
        line.split(|c: char| !c.is_ascii_digit())
            .filter(|s| !s.is_empty())
            .filter_map(|s| s.parse().ok())
            .collect()
    }

    /// The summary used to be one comma list over two independent questions:
    /// "13 tool(s): 6 verified, 0 answering, 0 broken, 2 not installed, 1
    /// undetermined, 7 not checked", which sums to 16 against the 13 in front
    /// of it. A reader adds a comma list up against the total beside it, so
    /// every list that can be summed has to sum.
    #[test]
    fn every_count_doctor_prints_adds_up_to_the_total_beside_it() {
        let out = cmd_doctor(&resolved_at(shipped_registry()), None, false, false);
        let total = out.json["checked"].as_u64().expect("a total");
        assert!(total > 1, "a registry of one proves nothing here");

        let mut summed = 0;
        for (head, from_second) in [
            ("tool(s):", true),
            ("On this machine:", false),
            ("Stegobench's own self-test:", false),
        ] {
            let line = out
                .human
                .lines()
                .find(|l| l.contains(head))
                .unwrap_or_else(|| panic!("no line for {head:?}: {}", out.human));
            let found = numbers_in(line);
            let (stated, parts) = if from_second {
                (found[0], &found[1..])
            } else {
                (total, &found[..])
            };
            assert_eq!(stated, total, "{head:?} states a different total: {line}");
            assert_eq!(
                parts.iter().sum::<u64>(),
                total,
                "{head:?} does not add up to {total}: {line}"
            );
            summed += 1;
        }
        assert_eq!(summed, 3, "a line went unchecked");
    }

    /// `doctor` listed thirteen tools, `list detectors` listed seven, and the
    /// word "embedder" appeared on neither screen. A reader with no way to
    /// tell which six were the difference reads it as something broken.
    #[test]
    fn doctor_and_the_listing_both_say_which_kind_each_tool_is() {
        let dir = shipped_registry();
        let doctor = cmd_doctor(&resolved_at(&dir), None, false, false);
        let listing = cmd_list(&resolved_at(&dir), "all");
        let detectors = doctor.json["detectors"].as_u64().expect("a count");
        let embedders = doctor.json["embedders"].as_u64().expect("a count");
        assert!(detectors > 0 && embedders > 0, "one kind is missing here");
        for screen in [&doctor.human, &listing.human] {
            for heading in [
                format!("DETECTORS ({detectors})"),
                format!("EMBEDDERS ({embedders})"),
            ] {
                assert!(screen.contains(&heading), "no {heading:?} in:\n{screen}");
            }
        }
        // The count under each heading is the count `list <kind>` gives, so
        // the two screens cannot drift apart.
        for (kind, count) in [("detectors", detectors), ("embedders", embedders)] {
            let one = cmd_list(&resolved_at(&dir), kind);
            assert_eq!(
                one.json["tools"].as_array().map(Vec::len),
                Some(count as usize),
                "`list {kind}` and the grouped screens disagree"
            );
        }
    }

    /// A bare `stegobench list` printed thirteen tool rows, a footer, then
    /// five corpus rows, with nothing saying which was which.
    #[test]
    fn a_bare_listing_says_where_the_tools_end_and_the_corpora_begin() {
        let dir = shipped_registry();
        let out = cmd_list(&resolved_at(&dir), "all");
        let corpora = out.json["corpora"].as_array().expect("corpora").len();
        assert!(
            out.human.contains(&format!("CORPORA ({corpora})")),
            "{}",
            out.human
        );
        let heading = out.human.find("CORPORA (").expect("the corpora heading");
        let tools = out.human.find("DETECTORS (").expect("the tools heading");
        assert!(tools < heading, "the corpora are listed before the tools");
    }

    /// The pasteable command is long and carries a note under it. A second
    /// line that started in column zero would read as the next field.
    #[test]
    fn a_fact_of_several_lines_keeps_the_column() {
        let block = describe_block(
            &needs::Needs {
                readiness: needs::Readiness::Ready,
                steps: Vec::new(),
            },
            "thing",
            "tool",
            &[
                ("Short", "one".to_string()),
                ("Much longer", "first\nsecond".to_string()),
            ],
        );
        let lines: Vec<&str> = block.lines().collect();
        let continuation = lines
            .iter()
            .find(|l| l.trim() == "second")
            .expect("the second line survived");
        let head = lines
            .iter()
            .find(|l| l.starts_with("Much longer"))
            .expect("the labelled line");
        assert_eq!(
            continuation.find("second"),
            head.find("first"),
            "the continuation does not line up: {continuation:?} against {head:?}"
        );
    }

    /// Neither of the two signposts may be offered as the nearest match to
    /// some third word: that would send a reader to a refusal.
    #[test]
    fn the_nearest_command_is_named_for_a_near_miss_and_withheld_for_a_stranger() {
        assert_eq!(nearest_command("lst").as_deref(), Some("list"));
        assert_eq!(nearest_command("doctr").as_deref(), Some("doctor"));
        assert_eq!(nearest_command("REPORT").as_deref(), Some("report"));
        assert_eq!(nearest_command("frobnicate"), None);
        for word in ["chek", "scn", "sca"] {
            let got = nearest_command(word);
            assert!(
                !matches!(got.as_deref(), Some("check" | "scan")),
                "{word} was pointed at a hidden signpost: {got:?}"
            );
        }
        assert_eq!(
            nearest_command("chek"),
            None,
            "`chek` was pointed at some third command, which is what clap did \
             when it answered it with `schema`"
        );
    }

    #[test]
    fn the_edit_distance_is_the_ordinary_one() {
        assert_eq!(edit_distance("", ""), 0);
        assert_eq!(edit_distance("", "list"), 4);
        assert_eq!(edit_distance("list", ""), 4);
        assert_eq!(edit_distance("list", "list"), 0);
        assert_eq!(edit_distance("lst", "list"), 1);
        assert_eq!(edit_distance("kitten", "sitting"), 3);
    }

    /// The reorder the ladder asked for, pinned so it cannot quietly revert:
    /// the commands come before the machine contract, and the machine contract
    /// is still there.
    #[test]
    fn the_long_help_names_the_commands_before_the_exit_codes_and_keeps_both() {
        let mut cmd = Cli::command();
        let mut buf: Vec<u8> = Vec::new();
        cmd.write_long_help(&mut buf).expect("the help renders");
        let help = String::from_utf8(buf).expect("help is text");
        let commands = help.find("Commands:").expect("the command list is there");
        let codes = help
            .find("EXIT CODES")
            .expect("the exit codes are still there");
        assert!(
            commands < codes,
            "the exit codes come before the list of commands again"
        );
        for code in CONTRACT_EXIT_CODES {
            assert!(
                help.contains(&format!("{code}")),
                "exit code {code} is no longer documented anywhere in the help"
            );
        }
        assert!(
            help.contains("stdout") && help.contains("stderr"),
            "the stream contract was dropped rather than moved"
        );
        // The long help has to keep BOTH halves of the distinction, and the
        // assertion changed shape when `examine` landed: it used to check for
        // a sentence saying the tool cannot examine your own images, which
        // stopped being true. What it protects now is that the help still
        // separates the two, because a reader who misses the distinction
        // quotes an examination as a benchmark figure.
        let lower = help.to_lowercase();
        assert!(
            lower.contains("images of your own"),
            "the long help no longer says the tool can be pointed at your own images"
        );
        assert!(
            lower.contains("not a measurement"),
            "the long help no longer says an examination is not a measurement"
        );
    }

    #[test]
    fn scope_is_a_help_topic_and_says_what_this_is_not_for() {
        let out = cmd_help(Some("scope"));
        assert_eq!(out.code, exit::OK);
        assert!(out.human.contains("IS SOMETHING HIDDEN IN THESE PICTURES"));
        assert!(out.human.contains("stegobench list detectors"));
        let listed = cmd_help(None);
        assert!(
            listed.human.contains("scope"),
            "the topic list does not offer scope: {}",
            listed.human
        );
    }

    /// The heuristic behind the teaching refusal, including both directions it
    /// must not fire in.
    #[test]
    fn a_folder_of_photographs_is_recognised_and_a_corpus_is_not() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(
            unlabelled_corpus(dir.path()),
            None,
            "an empty directory is not a photo album"
        );
        std::fs::write(dir.path().join("DSC_0001.JPG"), b"x").unwrap();
        std::fs::write(dir.path().join("DSC_0002.png"), b"x").unwrap();
        let why = unlabelled_corpus(dir.path()).expect("two images and no records");
        assert!(why.contains("help scope"), "got: {why}");
        assert!(why.contains("2 image(s)"), "got: {why}");

        std::fs::write(dir.path().join("DSC_0001.json"), b"{}").unwrap();
        assert_eq!(
            unlabelled_corpus(dir.path()),
            None,
            "one record is enough to stop guessing and let the corpus loader speak"
        );

        assert_eq!(
            unlabelled_corpus(&dir.path().join("not-there")),
            None,
            "a missing directory is the corpus loader's error to report, not this one's"
        );
    }

    /// The refusal has to reach the person BEFORE the availability check,
    /// because "docker pull ..." sends them to install something that will not
    /// answer their question either.
    #[test]
    fn score_refuses_a_folder_of_unlabelled_images_before_it_talks_about_detectors() {
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("holiday-photos");
        std::fs::create_dir_all(&corpus).unwrap();
        std::fs::write(corpus.join("a.png"), b"x").unwrap();
        let out = cmd_score(
            &resolved_at(shipped_registry()),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["all".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(out.code, exit::PREFLIGHT_REFUSED);
        assert!(out.human.contains("help scope"), "got: {}", out.human);
        assert!(
            !out.human.contains("docker pull"),
            "the refusal sent somebody to install a container: {}",
            out.human
        );
    }

    /// `plan` is the other command somebody on the wrong side of this points
    /// at their own pictures, and it used to answer "DSC_0001 has an image but
    /// no record beside it", which does not tell them what is wrong.
    #[test]
    fn plan_refuses_a_folder_of_unlabelled_images_the_same_way_score_does() {
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("holiday-photos");
        std::fs::create_dir_all(&corpus).unwrap();
        std::fs::write(corpus.join("a.png"), b"x").unwrap();
        let out = cmd_plan(
            &resolved_at(shipped_registry()),
            &[
                "score".to_string(),
                "--corpus".to_string(),
                corpus.display().to_string(),
                "--detector".to_string(),
                "zsteg".to_string(),
            ],
        );
        assert_eq!(out.code, exit::PREFLIGHT_REFUSED);
        assert!(out.human.contains("help scope"), "got: {}", out.human);
    }

    /// A machine can hold a checkout, an installed copy and the built-in one.
    /// Which answered is part of the answer.
    #[test]
    fn list_and_doctor_both_name_the_registry_that_answered() {
        let resolved = resolved_at(shipped_registry());
        let listed = cmd_list(&resolved, "detectors");
        assert!(
            listed.human.contains("registry  "),
            "list does not name the registry: {}",
            listed.human
        );
        assert_eq!(listed.json["registry"]["source"], "explicit");
        assert_eq!(
            listed.json["registry"]["path"],
            shipped_registry().display().to_string()
        );

        let doctor = cmd_doctor(&resolved, None, false, false);
        assert!(
            doctor.human.starts_with("registry  "),
            "doctor does not open by naming the registry: {}",
            doctor.human.lines().next().unwrap_or_default()
        );
        assert_eq!(doctor.json["registry"]["source"], "explicit");
        assert!(doctor.json["registry"]["tools"].as_u64().unwrap() > 0);
    }

    #[test]
    fn an_unknown_list_kind_names_corpora_among_the_known_ones() {
        let out = cmd_list(&resolved_at(shipped_registry()), "corpuses");
        assert_eq!(out.code, exit::USAGE);
        assert!(out.human.contains("corpora"), "got: {}", out.human);
    }

    #[test]
    fn every_shell_gets_a_script_with_the_command_names_in_it() {
        for shell in [
            clap_complete::Shell::Bash,
            clap_complete::Shell::Zsh,
            clap_complete::Shell::Fish,
            clap_complete::Shell::PowerShell,
            clap_complete::Shell::Elvish,
        ] {
            let out = cmd_completions(shell);
            assert_eq!(out.code, exit::OK, "{shell} completions failed");
            assert!(
                out.human.contains("doctor") && out.human.contains("validate"),
                "{shell} script does not mention the subcommands it completes"
            );
        }
    }

    #[test]
    fn schema_generates_and_names_the_format() {
        let out = cmd_schema("result-v1");
        assert_eq!(out.code, exit::OK);
        let text = serde_json::to_string(&out.json).unwrap();
        assert!(
            text.contains("Result1"),
            "schema should describe the result type"
        );
        assert!(
            text.contains("n_error"),
            "the required honesty field must be in the schema"
        );
    }

    #[test]
    fn an_unknown_schema_name_is_a_usage_error_not_a_crash() {
        assert_eq!(cmd_schema("result-v9").code, exit::USAGE);
    }

    #[test]
    fn validating_a_missing_file_fails_without_panicking() {
        let out = cmd_validate(&PathBuf::from("/definitely/not/here.json"));
        assert_eq!(out.code, exit::FAILURE);
    }

    #[test]
    fn a_non_result_document_exits_schema_invalid() {
        let dir = tempfile::tempdir().unwrap();
        let p = dir.path().join("x.json");
        std::fs::write(&p, r#"{"hello":"world"}"#).unwrap();
        assert_eq!(cmd_validate(&p).code, exit::SCHEMA_INVALID);
    }

    /// A corpus of `n` samples whose records state the digests given.
    fn corpus_named(root: &Path, digests: &[&str]) {
        std::fs::create_dir_all(root).unwrap();
        for (i, d) in digests.iter().enumerate() {
            let role = if i == 0 { "clean" } else { "stego" };
            std::fs::write(root.join(format!("i{i}.png")), b"\x89PNG\r\n\x1a\n").unwrap();
            std::fs::write(
                root.join(format!("i{i}.json")),
                format!(r#"{{"role":"{role}","sha256":"{d}"}}"#),
            )
            .unwrap();
        }
    }

    #[test]
    fn the_starter_corpus_is_carried_in_the_binary_and_can_be_written_out() {
        // The first run for anybody who installed a binary. `describe` used
        // to tell them to look in a checkout they do not have.
        assert!(
            !EMBEDDED_STARTER.is_empty(),
            "no starter corpus was compiled in, so a first run needs a checkout"
        );
        let dir = tempfile::tempdir().unwrap();
        let dest = dir.path().join("out");
        let n = write_embedded_starter(&dest).expect("written");
        assert_eq!(n, EMBEDDED_STARTER.len());

        // It has to be a corpus, not just files: every image needs the record
        // beside it or `score` will refuse what we just produced.
        let digest = score::corpus_digest(&dest)
            .expect("readable")
            .expect("every record states a digest");
        assert!(digest.starts_with("sha256:"), "got {digest}");
    }

    #[test]
    fn writing_the_starter_corpus_over_something_is_refused() {
        // Merging into a directory that already holds a corpus leaves records
        // describing images from two copies, and nothing downstream can tell.
        let dir = tempfile::tempdir().unwrap();
        let dest = dir.path().join("out");
        std::fs::create_dir_all(&dest).unwrap();
        std::fs::write(dest.join("something.txt"), b"mine").unwrap();
        let err = write_embedded_starter(&dest).expect_err("should have refused");
        assert!(err.contains("already has something in it"), "got {err}");
    }

    /// A corpus whose records state the digest their image actually has.
    ///
    /// `corpus_named` fabricates digests, which is what the record-level
    /// comparison needs and is exactly what the byte check exists to catch,
    /// so a test of that check cannot be built on it.
    fn corpus_honest(root: &Path, bodies: &[&[u8]]) {
        use sha2::{Digest, Sha256};
        std::fs::create_dir_all(root).unwrap();
        for (i, body) in bodies.iter().enumerate() {
            let role = if i == 0 { "clean" } else { "stego" };
            std::fs::write(root.join(format!("i{i}.png")), body).unwrap();
            let d = format!("{:x}", Sha256::digest(body));
            std::fs::write(
                root.join(format!("i{i}.json")),
                format!(r#"{{"role":"{role}","sha256":"{d}"}}"#),
            )
            .unwrap();
        }
    }

    #[test]
    fn a_document_edited_after_it_was_written_is_refused() {
        // The whole reason `content_digest` exists. Every field except the
        // two recording WHEN the run happened is covered, so a changed
        // number cannot survive the check, and a run edited to flatter a
        // detector stops being something a reader has to spot by eye.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        let mut sealed: Result1 =
            serde_json::from_str(&std::fs::read_to_string(&doc).unwrap()).unwrap();
        sealed.seal();
        // Improve the number after sealing, which is what a tamper looks like.
        sealed.metrics.auc = 0.99;
        std::fs::write(&doc, serde_json::to_string(&sealed).unwrap()).unwrap();

        let out = cmd_verify(&doc, &corpus, None, false);
        assert_eq!(out.code, exit::VERIFY_MISMATCH, "{}", out.human);
        assert!(
            out.human.contains("does not match its own content digest"),
            "{}",
            out.human
        );
    }

    #[test]
    fn a_document_whose_digest_matches_passes_the_check_it_used_to_skip() {
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        let mut sealed: Result1 =
            serde_json::from_str(&std::fs::read_to_string(&doc).unwrap()).unwrap();
        sealed.seal();
        std::fs::write(&doc, serde_json::to_string(&sealed).unwrap()).unwrap();

        let out = cmd_verify(&doc, &corpus, None, false);
        assert_eq!(out.code, exit::OK, "{}", out.human);
    }

    #[test]
    fn an_edited_records_file_is_not_the_one_the_document_names() {
        // THE REVIEWER'S ATTACK, and it is the cheap one. The per-item scores
        // are a plain text file with no digest. They rewrote every score to
        // produce a perfect AUC, re-ran `score`, which resumed from the file
        // without re-checking it, and `verify` exited 0 over the result. Every
        // check `verify` ran was about the corpus, which was untouched.
        //
        // What this closes is the disclosure half: a document and a records
        // file can now be shown to be about each other. It does NOT make the
        // scores the detector's, because whoever ran the measurement wrote
        // that file; `declarations.self_reported` is the field for that.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        let records = dir.path().join("r.records.jsonl");
        std::fs::write(
            &records,
            "{\"id\":\"covers/000000\",\"score\":0.01}\n\
             {\"id\":\"stego/000000\",\"score\":0.02}\n",
        )
        .unwrap();
        let honest = stegobench_core::result::records_digest(&records).unwrap();
        assert_eq!(honest.count, 2);

        let mut sealed: Result1 =
            serde_json::from_str(&std::fs::read_to_string(&doc).unwrap()).unwrap();
        sealed.provenance.records = Some(honest);
        sealed.seal();
        std::fs::write(&doc, serde_json::to_string(&sealed).unwrap()).unwrap();

        let out = cmd_verify(&doc, &corpus, Some(&records), false);
        assert_eq!(out.code, exit::OK, "the honest pair: {}", out.human);

        // One score rewritten, which is all it took.
        std::fs::write(
            &records,
            "{\"id\":\"covers/000000\",\"score\":0.01}\n\
             {\"id\":\"stego/000000\",\"score\":0.99}\n",
        )
        .unwrap();
        let out = cmd_verify(&doc, &corpus, Some(&records), false);
        assert_eq!(out.code, exit::VERIFY_MISMATCH, "{}", out.human);
        assert!(
            out.human.contains("not the scores"),
            "the refusal should name what disagreed: {}",
            out.human
        );
    }

    #[test]
    fn a_records_file_offered_to_a_document_that_names_none_is_refused() {
        // Not silently skipped. A reader who passed the file is asking a
        // question, and answering a different one with exit 0 is how a check
        // that never ran reads as a check that passed.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);
        let records = dir.path().join("r.records.jsonl");
        std::fs::write(&records, "{\"id\":\"covers/000000\",\"score\":0.01}\n").unwrap();

        let out = cmd_verify(&doc, &corpus, Some(&records), false);
        assert_eq!(out.code, exit::VERIFY_MISMATCH, "{}", out.human);
        assert!(out.human.contains("records no digest"), "{}", out.human);
    }

    #[test]
    fn a_trailing_newline_is_not_counted_as_a_record() {
        // The count sits beside the digest so a truncated file is obvious by
        // eye. A file that ends properly would otherwise be reported with one
        // record more than it has, every time.
        let dir = tempfile::tempdir().unwrap();
        let records = dir.path().join("r.jsonl");
        std::fs::write(&records, "{\"id\":\"a\"}\n{\"id\":\"b\"}\n").unwrap();
        assert_eq!(
            stegobench_core::result::records_digest(&records)
                .unwrap()
                .count,
            2
        );
        std::fs::write(&records, "{\"id\":\"a\"}\n{\"id\":\"b\"}").unwrap();
        assert_eq!(
            stegobench_core::result::records_digest(&records)
                .unwrap()
                .count,
            2
        );
    }

    #[test]
    fn a_document_carrying_no_content_digest_is_not_accused_of_a_mismatch() {
        // Absent means nobody offered one. A submitted or hand-written
        // document is allowed to say nothing here, and refusing it would
        // make the field mandatory by the back door.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);
        assert!(!std::fs::read_to_string(&doc)
            .unwrap()
            .contains("content_digest"));

        let out = cmd_verify(&doc, &corpus, None, false);
        assert_eq!(out.code, exit::OK, "{}", out.human);
    }

    #[test]
    fn a_changed_timestamp_is_not_silently_blessed() {
        // A reviewer moved a sealed document's date back five years and
        // `validate` answered an unqualified "is a valid result-v1 document".
        // The exclusion is right: two runs differing only in when they ran
        // must seal identically. The sentence was wrong, because for casework
        // the date is read as inside the envelope unless the output says
        // otherwise.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        let mut sealed: Result1 =
            serde_json::from_str(&std::fs::read_to_string(&doc).unwrap()).unwrap();
        sealed.seal();
        sealed.provenance.started_utc = "2021-01-01T00:00:00Z".into();
        std::fs::write(&doc, serde_json::to_string(&sealed).unwrap()).unwrap();

        let out = cmd_validate(&doc);
        // Still valid, because the seal genuinely does not cover this field.
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert!(out.human.contains("started_utc"), "{}", out.human);
        assert!(out.human.contains("elapsed_seconds"), "{}", out.human);
        assert_eq!(out.json["seal"]["sealed"], serde_json::json!(true));
        assert_eq!(
            out.json["seal"]["excluded"],
            serde_json::json!(["provenance.started_utc", "provenance.elapsed_seconds"])
        );
        // The continuation-stripped literal must not leave runs of spaces.
        assert!(!out.human.contains("   "), "{}", out.human);
    }

    #[test]
    fn an_unsealed_result_says_so_rather_than_claiming_a_timing_gap() {
        // Without a digest nothing was checked for tampering at all, so the
        // sealed document's caveat would understate it.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        let out = cmd_validate(&doc);
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert!(out.human.contains("no content digest"), "{}", out.human);
        assert_eq!(out.json["seal"]["sealed"], serde_json::json!(false));
    }

    #[test]
    fn the_per_arm_block_is_absent_for_a_corpus_of_one_arm() {
        assert!(per_arm_lines(&[]).is_empty());
    }

    #[test]
    fn the_per_arm_block_names_every_arm_and_omits_an_interval_it_does_not_have() {
        use stegobench_core::result::ArmMetrics;
        let arms = vec![
            ArmMetrics {
                arm: "lsb-0100".into(),
                auc: 0.5,
                auc_ci95: Some([0.1135, 0.8865]),
                n_clean: 6,
                n_stego: 6,
            },
            ArmMetrics {
                arm: "a-much-longer-arm".into(),
                auc: 0.6944,
                auc_ci95: None,
                n_clean: 6,
                n_stego: 6,
            },
        ];
        let text = per_arm_lines(&arms).join("\n");
        assert!(text.contains("lsb-0100"), "{text}");
        assert!(text.contains("a-much-longer-arm"), "{text}");
        assert!(text.contains("0.5000"), "{text}");
        assert!(text.contains("[0.1135, 0.8865]"), "{text}");
        assert!(
            !text.contains("0.6944 ["),
            "an interval was fabricated for the arm that had none: {text}"
        );
        assert!(
            text.contains("same 6 clean image(s)"),
            "the shared clean set should be stated once: {text}"
        );
    }

    #[test]
    fn verify_reads_the_images_and_says_so_when_they_match() {
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus)
            .expect("readable")
            .expect("named");
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        let out = cmd_verify(&doc, &corpus, None, false);
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert_eq!(out.json["checked"], serde_json::json!("bytes"));
        assert!(
            out.human.contains("hash to what their records state"),
            "the answer does not say the bytes were read: {}",
            out.human
        );
    }

    #[test]
    fn verify_catches_an_image_swapped_under_a_record_that_still_agrees() {
        // The attack the byte check exists for, and the one the corpus digest
        // cannot see: replace a stego image with an easier one, leave its
        // record alone, and every digest in the corpus still agrees with
        // every other. Scored again it gives a better number, and before this
        // check `verify` called that number attributable to this corpus.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus)
            .expect("readable")
            .expect("named");
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        std::fs::write(corpus.join("i1.png"), b"a much easier image").unwrap();

        // The records are untouched, so the corpus still names itself the
        // same way. That is the point.
        let after = score::corpus_digest(&corpus)
            .expect("readable")
            .expect("named");
        assert_eq!(after, digest, "the swap changed the record level digest");

        let out = cmd_verify(&doc, &corpus, None, false);
        assert_eq!(out.code, exit::VERIFY_MISMATCH, "{}", out.human);
        assert_eq!(out.json["ok"], serde_json::json!(false));
        assert!(
            out.human.contains("the images do not match the records"),
            "the refusal does not name the fault: {}",
            out.human
        );

        // And the cheap check still passes, which is why it says what it
        // checked rather than claiming more.
        let shallow = cmd_verify(&doc, &corpus, None, true);
        assert_eq!(shallow.code, exit::OK, "{}", shallow.human);
        assert!(
            shallow.human.contains("NOT re-read"),
            "the shallow answer overclaims: {}",
            shallow.human
        );
    }

    /// A result-v1 document claiming it was measured on `digest`.
    fn result_claiming(path: &Path, digest: &str) {
        let doc = serde_json::json!({
            "schema": stegobench_core::result::RESULT_SCHEMA_ID,
            "subject": {"name": "x", "version": "sha256:a", "kind": "detector"},
            "corpus": {"name": "c", "source": "supplied", "digest": digest, "pairs": 2},
            "arm": {"embedder": "wow", "domain": "spatial", "format": "png"},
            "metrics": {"auc": 0.9, "tpr_at_fpr": {}, "n_clean": 1, "n_stego": 1, "n_error": 0},
            "provenance": {
                "plugins": [{"name": "x", "image": "sha256:a", "determinism": "nondeterministic", "pinned_by": "executable-hash", "isolation": "host"}],
                "harness_version": "0.1.0",
                "started_utc": "2026-09-25T00:00:00Z",
                "elapsed_seconds": 1.0,
                "network_reachable": true
            },
            "declarations": {
                "split_discipline": "not-applicable",
                "pairing": "unverified",
                "configuration": "custom",
                "self_reported": false
            }
        });
        std::fs::write(path, serde_json::to_string(&doc).unwrap()).unwrap();
    }

    #[test]
    fn verify_agrees_when_the_document_and_the_corpus_are_the_same_corpus() {
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_named(&corpus, &["aa", "bb"]);
        let digest = score::corpus_digest(&corpus)
            .expect("readable")
            .expect("named");
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);
        let out = cmd_verify(&doc, &corpus, None, true);
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert_eq!(out.json["ok"], serde_json::json!(true));
    }

    #[test]
    fn verify_refuses_a_corpus_the_document_was_not_measured_on() {
        // The whole job. Two directories can have the same name, the same file
        // count and different contents, and the number in a result is about
        // exactly one of them.
        let dir = tempfile::tempdir().unwrap();
        let measured = dir.path().join("measured");
        let other = dir.path().join("other");
        corpus_named(&measured, &["aa", "bb"]);
        corpus_named(&other, &["aa", "bc"]);
        let digest = score::corpus_digest(&measured)
            .expect("readable")
            .expect("named");
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);

        let out = cmd_verify(&doc, &other, None, true);
        assert_eq!(out.code, exit::VERIFY_MISMATCH);
        assert!(out.human.contains("not about each other"), "{}", out.human);
        assert_eq!(out.json["ok"], serde_json::json!(false));
    }

    #[test]
    fn verify_says_so_when_there_is_nothing_to_check() {
        // A result over a corpus that cannot be named carries an empty digest,
        // and comparing an empty string with an empty string would pass while
        // proving nothing at all.
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_named(&corpus, &["aa", "bb"]);
        let doc = dir.path().join("r.json");
        result_claiming(&doc, "");
        let out = cmd_verify(&doc, &corpus, None, true);
        assert_eq!(out.code, exit::VERIFY_MISMATCH);
        assert!(
            out.human.contains("names no corpus digest"),
            "{}",
            out.human
        );
    }

    #[test]
    fn verify_refuses_a_corpus_that_cannot_be_named() {
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        std::fs::create_dir_all(&corpus).unwrap();
        std::fs::write(corpus.join("a.png"), b"\x89PNG\r\n\x1a\n").unwrap();
        std::fs::write(corpus.join("a.json"), r#"{"role":"clean"}"#).unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, "sha256:whatever");
        let out = cmd_verify(&doc, &corpus, None, true);
        assert_eq!(out.code, exit::VERIFY_MISMATCH);
        assert!(out.human.contains("cannot be named"), "{}", out.human);
    }

    #[test]
    fn verify_sends_a_document_that_is_not_a_result_to_validate() {
        let dir = tempfile::tempdir().unwrap();
        let doc = dir.path().join("x.json");
        std::fs::write(&doc, r#"{"hello":"world"}"#).unwrap();
        let out = cmd_verify(&doc, dir.path(), None, true);
        assert_eq!(out.code, exit::SCHEMA_INVALID);
        assert!(out.human.contains("stegobench validate"), "{}", out.human);
    }

    /// The table is the payload, so it has to reach stdout even when the
    /// command exits non-zero. It exits non-zero precisely when a file was
    /// dropped, which is the case a reader most needs the table in front of
    /// them for.
    #[test]
    fn a_report_with_a_dropped_file_still_puts_the_table_on_stdout() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::copy(
            Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../../results/v1/round3-q95-structural-0000-aletheia-rs.json"),
            dir.path().join("good.json"),
        )
        .unwrap();
        std::fs::write(dir.path().join("bad.json"), "{nope").unwrap();

        let out = cmd_report(&[dir.path().to_path_buf()], ReportFormat::Text, None);
        assert_eq!(out.code, exit::SCHEMA_INVALID);
        assert!(out.payload_on_stdout, "the table was diverted to stderr");
        assert!(out.human.contains("THIS REPORT IS INCOMPLETE"));
        assert!(out.human.contains("bad.json"));
        assert!(out.human.contains("round3-q95"));
        assert_eq!(out.json["complete"], serde_json::json!(false));
    }

    /// With --out the payload is the file, so the human text is a
    /// confirmation and belongs on stderr with the rest of the progress.
    #[test]
    fn writing_a_report_to_a_file_confirms_on_stderr_and_says_if_it_is_short() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::copy(
            Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../../results/v1/round3-q95-structural-0000-aletheia-rs.json"),
            dir.path().join("good.json"),
        )
        .unwrap();
        let target = dir.path().join("out").join("report.md");
        std::fs::create_dir_all(target.parent().unwrap()).unwrap();

        let out = cmd_report(
            &[dir.path().join("good.json")],
            ReportFormat::Markdown,
            Some(&target),
        );
        assert_eq!(out.code, exit::OK);
        assert!(!out.payload_on_stdout);
        assert!(out.human.contains("Every file found was readable"));
        let written = std::fs::read_to_string(&target).unwrap();
        assert!(written.starts_with("# Steganalysis results"));
        assert!(written.contains("round3-q95"));

        // And the short case says so in the confirmation, so a person
        // watching stderr does not have to open the file to find out.
        std::fs::write(dir.path().join("bad.json"), "{").unwrap();
        let out = cmd_report(
            &[dir.path().to_path_buf()],
            ReportFormat::Markdown,
            Some(&target),
        );
        assert_eq!(out.code, exit::SCHEMA_INVALID);
        assert!(out.human.contains("tables are incomplete"), "{}", out.human);
    }

    #[test]
    fn a_report_over_nothing_is_a_usage_error_rather_than_an_empty_table() {
        let dir = tempfile::tempdir().unwrap();
        let out = cmd_report(&[dir.path().to_path_buf()], ReportFormat::Text, None);
        assert_eq!(out.code, exit::USAGE);
        assert!(out.human.contains("no table to print"), "{}", out.human);
    }

    #[test]
    fn a_report_to_an_unwritable_destination_fails_rather_than_reporting_success() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::copy(
            Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../../results/v1/round3-q95-structural-0000-aletheia-rs.json"),
            dir.path().join("good.json"),
        )
        .unwrap();
        let out = cmd_report(
            &[dir.path().to_path_buf()],
            ReportFormat::Csv,
            Some(&dir.path().join("no-such-dir").join("r.csv")),
        );
        assert_eq!(out.code, exit::FAILURE);
        assert!(out.human.contains("could not write the report"));
    }

    /// A registry holding one detector that works and one that is not here.
    ///
    /// Both are needed in the same registry, because the behaviour under test
    /// is what happens to the good one when the other is missing, and a test
    /// with only one of them cannot see it.
    #[cfg(unix)]
    fn registry_with_one_present_and_one_missing(dir: &Path) -> PathBuf {
        use std::os::unix::fs::PermissionsExt;
        let reg = dir.join("registry");
        std::fs::create_dir_all(&reg).unwrap();

        let script = dir.join("size.sh");
        std::fs::write(
            &script,
            "#!/bin/sh\n[ \"$1\" = \"--version\" ] && { echo v1; exit 0; }\n\
             wc -c < \"$1\" | tr -d ' ' | awk '{print $1/1000}'\n",
        )
        .unwrap();
        let mut perms = std::fs::metadata(&script).unwrap().permissions();
        perms.set_mode(0o755);
        std::fs::set_permissions(&script, perms).unwrap();

        std::fs::write(
            reg.join("sizer.toml"),
            format!(
                "name = \"sizer\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
                 [binary]\ncommand = [{:?}]\nversion_args = [\"--version\"]\n\
                 [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
                 [invoke]\nargv = [\"{{file}}\"]\nparser = \"number\"\n\
                 [selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
                script.display().to_string()
            ),
        )
        .unwrap();
        std::fs::write(
            reg.join("ghost.toml"),
            "name = \"ghost\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [binary]\ncommand = [\"definitely-not-installed-xyzzy\"]\n\
             version_args = [\"--version\"]\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nargv = [\"{file}\"]\nparser = \"number\"\n\
             [selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
        )
        .unwrap();
        reg
    }

    /// A PNG header with `padding` bytes after it, so a stego image is the
    /// same shape as its cover and only larger.
    #[cfg(unix)]
    fn test_png(padding: usize) -> Vec<u8> {
        let mut v = b"\x89PNG\r\n\x1a\n".to_vec();
        v.extend_from_slice(&13u32.to_be_bytes());
        v.extend_from_slice(b"IHDR");
        v.extend_from_slice(&32u32.to_be_bytes());
        v.extend_from_slice(&32u32.to_be_bytes());
        v.extend_from_slice(&[8, 2, 0, 0, 0]);
        v.extend_from_slice(&[0, 0, 0, 0]);
        v.extend_from_slice(&vec![0u8; padding]);
        v
    }

    #[cfg(unix)]
    fn scratch_corpus(root: &Path) {
        std::fs::create_dir_all(root).unwrap();
        for i in 0..3 {
            std::fs::write(root.join(format!("c{i}.png")), test_png(0)).unwrap();
            std::fs::write(
                root.join(format!("c{i}.json")),
                r#"{"role":"clean","sha256":"0"}"#,
            )
            .unwrap();
            std::fs::write(root.join(format!("s{i}.png")), test_png(64)).unwrap();
            std::fs::write(
                root.join(format!("s{i}.json")),
                format!(r#"{{"role":"stego","source_png":"c{i}.png","sha256":"0"}}"#),
            )
            .unwrap();
        }
    }

    /// THE FAILURE THIS WHOLE COMMAND HAS TO AVOID.
    ///
    /// A baseline over several detectors where one is missing must not exit
    /// zero, because a zero is read as "every detector I asked for was
    /// measured" and a script will read it that way forever. The other
    /// detector's work still has to survive.
    #[cfg(unix)]
    #[test]
    fn one_detector_writing_into_a_directory_puts_its_document_inside_it() {
        // `--out` used to mean a directory when several detectors were named
        // and a file when one was, so the same path worked and then did not
        // depending on how many detectors were asked for. Measured 2026-09-29:
        // a run wrote /tmp/multi/zsteg.json and /tmp/multi/stegexpose.json,
        // and the next run, differing only in naming one detector, scored the
        // whole corpus and then died with "Is a directory (os error 21)".
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");
        std::fs::create_dir_all(&out_dir).unwrap();

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );

        assert_eq!(out.code, exit::OK, "{}", out.human);
        let written = out_dir.join("sizer.json");
        assert!(
            written.exists(),
            "one detector did not write into the directory it was given: {}",
            out.human
        );
        assert_eq!(cmd_validate(&written).code, exit::OK);
        // The per-item answers keep it company rather than landing beside the
        // corpus, so one run's output is in one place.
        assert!(
            out_dir.join("sizer.records.jsonl").exists(),
            "the records did not follow the document into the directory"
        );
    }

    /// "AUC" is never expanded on this screen and the bracketed pair beside
    /// it is never explained, so the screen has to say where both are
    /// defined. It used to point only at a report of a run that had just
    /// happened, which is a next step rather than an answer.
    #[cfg(unix)]
    #[test]
    fn the_score_summary_says_where_its_two_unexplained_figures_are_defined() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");
        std::fs::create_dir_all(&out_dir).unwrap();

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert!(
            out.human.contains("AUC "),
            "nothing to explain: {}",
            out.human
        );
        assert!(
            out.human.contains("`stegobench help results`"),
            "the summary explains neither figure and points nowhere: {}",
            out.human
        );
        // Beside the figures, not at the bottom of the screen: a definition a
        // screen away from the number is one nobody reads.
        let figure = out.human.find("AUC ").expect("an AUC");
        let pointer = out
            .human
            .find("`stegobench help results`")
            .expect("a pointer");
        assert!(pointer > figure, "the pointer comes before the figure");
        assert!(
            out.human[figure..pointer].lines().count() <= 6,
            "the pointer is not beside the figures: {}",
            out.human
        );
    }

    #[cfg(unix)]
    #[test]
    fn several_detectors_aimed_at_a_file_are_refused_before_anything_is_scored() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let target = tmp.path().join("one-file.json");
        std::fs::write(&target, "{}").unwrap();

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["ghost".to_string(), "sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&target),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );

        assert_eq!(out.code, exit::USAGE, "{}", out.human);
        // The exact refusal, so this cannot pass on some other message that
        // happens to carry the word "directory".
        let expected = "is a file. Scoring 2 detectors writes one result \
                        document each, so --out has to name a directory.";
        assert!(out.human.contains(expected), "{}", out.human);
        // Refused BEFORE the work: the file it was pointed at is untouched.
        assert_eq!(
            std::fs::read_to_string(&target).unwrap(),
            "{}",
            "the run wrote over the file it should have refused"
        );
    }

    #[cfg(unix)]
    #[test]
    fn one_missing_detector_does_not_lose_the_others_and_never_exits_zero() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["ghost".to_string(), "sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );

        assert_eq!(
            out.code,
            exit::PREFLIGHT_REFUSED,
            "a run that measured one of two detectors exited {}: {}",
            out.code,
            out.human
        );
        assert_ne!(out.code, exit::OK, "{}", out.human);

        // The good one's work survived and is on disk.
        let written = out_dir.join("sizer.json");
        assert!(written.exists(), "{}", out.human);
        assert_eq!(cmd_validate(&written).code, exit::OK);

        // And the summary says so in words a person reads, not only in a code.
        assert!(
            out.human.contains("1 of 2 detector(s) measured"),
            "{}",
            out.human
        );
        assert!(out.human.contains("NOT MEASURED"), "{}", out.human);
        assert_eq!(out.json["measured"], serde_json::json!(1));
        assert_eq!(out.json["skipped"], serde_json::json!(1));
        assert_eq!(out.json["requested"], serde_json::json!(2));
        assert_eq!(out.json["ok"], serde_json::json!(false));
    }

    /// A directory is not a run, and a reader will treat it as one.
    ///
    /// A detector measured yesterday leaves its document behind. Skipped
    /// today, that document is still in the directory and `report` will put
    /// its number in the table. Nothing is deleted, because a measurement is
    /// not this command's to throw away, but the summary has to say it.
    #[cfg(unix)]
    #[test]
    fn a_document_left_from_an_earlier_run_of_a_now_skipped_detector_is_named() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");
        std::fs::create_dir_all(&out_dir).unwrap();
        // Stand in for last week's run of a detector that is gone today.
        std::fs::write(out_dir.join("ghost.json"), "{}").unwrap();

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["ghost".to_string(), "sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert!(
            out.human.contains("are from an earlier run"),
            "a stale document went unmentioned: {}",
            out.human
        );
        assert!(out.human.contains("ghost.json"), "{}", out.human);
        assert!(
            out_dir.join("ghost.json").exists(),
            "the stale document was deleted rather than named"
        );
    }

    /// The same hazard from the other direction, which the narrow version of
    /// the check missed entirely: a document from a detector nobody named
    /// today is exactly as invisible in the resulting table.
    #[cfg(unix)]
    #[test]
    fn a_document_from_a_detector_this_run_never_asked_for_is_named_too() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");
        std::fs::create_dir_all(&out_dir).unwrap();
        std::fs::write(out_dir.join("somebody-elses-tool.json"), "{}").unwrap();

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string(), "ghost".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert!(
            out.human.contains("somebody-elses-tool.json"),
            "a document nobody asked about today went unmentioned: {}",
            out.human
        );
    }

    /// And a clean directory says nothing, so the warning stays worth reading.
    #[cfg(unix)]
    #[test]
    fn a_run_that_wrote_every_document_in_its_directory_warns_about_nothing() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        std::fs::copy(reg.join("sizer.toml"), reg.join("sizer2.toml")).unwrap();
        let text = std::fs::read_to_string(reg.join("sizer2.toml"))
            .unwrap()
            .replace("name = \"sizer\"", "name = \"sizer2\"");
        std::fs::write(reg.join("sizer2.toml"), text).unwrap();
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string(), "sizer2".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert!(
            !out.human.contains("NOT measured by this run"),
            "warned about documents it had just written itself: {}",
            out.human
        );
    }

    /// Two detectors sharing one records file was a live bug: the second
    /// resumed from the first one's answers and reported them as its own.
    #[cfg(unix)]
    #[test]
    fn two_detectors_over_one_corpus_do_not_resume_from_each_others_answers() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        // A second working detector, so two of them actually run.
        std::fs::copy(reg.join("sizer.toml"), reg.join("sizer2.toml")).unwrap();
        let text = std::fs::read_to_string(reg.join("sizer2.toml"))
            .unwrap()
            .replace("name = \"sizer\"", "name = \"sizer2\"");
        std::fs::write(reg.join("sizer2.toml"), text).unwrap();

        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string(), "sizer2".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(out.code, exit::OK, "{}", out.human);

        // Each scored all six items itself. A resumed count above zero here
        // would mean one of them read the other's file.
        for d in out.json["detectors"].as_array().expect("rows") {
            assert_eq!(d["scored"], serde_json::json!(6), "{d}");
            assert_eq!(
                d["resumed"],
                serde_json::json!(0),
                "{} resumed from another detector's records: {d}",
                d["name"]
            );
        }
        assert!(out_dir.join("sizer.records.jsonl").exists());
        assert!(out_dir.join("sizer2.records.jsonl").exists());
    }

    /// A resumed multi-detector run picks up per detector, so an interrupted
    /// baseline does not re-score what it already measured.
    #[cfg(unix)]
    #[test]
    fn a_second_run_of_the_same_command_resumes_each_detector_separately() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");
        let args = ["sizer".to_string()];

        let first = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &args,
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(first.code, exit::OK, "{}", first.human);

        let second = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &args,
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(second.code, exit::OK, "{}", second.human);
        assert!(
            second.human.contains("6 resumed"),
            "the second run re-scored instead of resuming: {}",
            second.human
        );
    }

    /// With no `--out` and several detectors the documents still land
    /// somewhere findable, and the command says where rather than leaving the
    /// reader to guess.
    #[cfg(unix)]
    #[test]
    fn several_detectors_with_no_out_write_beside_the_corpus_and_say_where() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string(), "ghost".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        let expected = tmp.path().join("corpus.results");
        assert!(expected.join("sizer.json").exists(), "{}", out.human);
        assert!(
            out.human.contains(&expected.display().to_string()),
            "the command did not say where it wrote: {}",
            out.human
        );
        assert!(out.human.contains("stegobench report"), "{}", out.human);
    }

    /// One detector still behaves exactly as it always did: the document on
    /// stdout, nothing invented around it.
    #[cfg(unix)]
    #[test]
    fn a_single_detector_still_puts_the_result_document_on_stdout() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert_eq!(
            out.json["schema"],
            serde_json::json!(stegobench_core::result::RESULT_SCHEMA_ID),
            "a single run stopped emitting the result document itself"
        );
        assert!(out.json["metrics"]["auc"].is_number());
    }

    /// Rung 3 of the journey round: `--out results-test` with one detector
    /// created a FILE, where the same flag with two had created a directory,
    /// and nothing said which had happened.
    #[cfg(unix)]
    #[test]
    fn one_detector_is_told_its_out_path_is_the_document_and_not_a_directory() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let named = tmp.path().join("results-test");

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&named),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert!(named.is_file(), "the document is not at the named path");
        assert!(
            out.human.contains("is the result document itself"),
            "nothing said the path was a file rather than a directory: {}",
            out.human
        );
    }

    /// And the notice does not fire for the case it would be wrong about.
    #[cfg(unix)]
    #[test]
    fn several_detectors_are_not_told_their_out_path_is_a_document() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let named = tmp.path().join("results-dir");

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string(), "ghost".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&named),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert!(
            !out.human.contains("is the result document itself"),
            "a directory run was told its path was a document: {}",
            out.human
        );
        assert!(named.is_dir(), "{}", out.human);
    }

    /// Rung 5 of the journey round claimed `plan` exits 0 on a corpus `score`
    /// refuses with exit 3, having printed the refusal verbatim. Written to find
    /// out rather than to assume: a plan whose exit code disagrees with the run
    /// it describes is useless to the script that gates on it.
    #[cfg(unix)]
    #[test]
    fn a_plan_over_a_leaking_corpus_refuses_with_the_code_the_run_would() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        std::fs::create_dir_all(&corpus).unwrap();
        // Every cover in train, every stego image in test, which puts each twin
        // on the opposite side of the boundary from its own cover.
        for i in 0..3 {
            std::fs::write(corpus.join(format!("c{i}.png")), test_png(0)).unwrap();
            std::fs::write(
                corpus.join(format!("c{i}.json")),
                format!(r#"{{"role":"clean","sha256":"{i}","split":"train"}}"#),
            )
            .unwrap();
            std::fs::write(corpus.join(format!("s{i}.png")), test_png(64)).unwrap();
            std::fs::write(
                corpus.join(format!("s{i}.json")),
                format!(
                    r#"{{"role":"stego","source_png":"c{i}.png","sha256":"{i}","split":"test"}}"#
                ),
            )
            .unwrap();
        }

        let out = cmd_plan(
            &resolved_at(&reg),
            &[
                "score".into(),
                "--corpus".into(),
                corpus.display().to_string(),
                "--detector".into(),
                "sizer".into(),
            ],
        );
        assert_eq!(
            out.code,
            exit::PREFLIGHT_REFUSED,
            "a plan over a corpus score refuses returned {}: {}",
            out.code,
            out.human
        );
        assert!(
            out.human
                .contains("different side of the train and test split"),
            "{}",
            out.human
        );
    }

    /// Asking for every detector when none of them is here is not a run that
    /// measured nothing; it is a refusal, and it keeps the exit code it has
    /// always had.
    #[cfg(unix)]
    #[test]
    fn a_command_where_no_detector_is_available_refuses_rather_than_reporting_zero() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = tmp.path().join("registry");
        std::fs::create_dir_all(&reg).unwrap();
        std::fs::write(
            reg.join("ghost.toml"),
            "name = \"ghost\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [binary]\ncommand = [\"definitely-not-installed-xyzzy\"]\n\
             version_args = [\"--version\"]\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [invoke]\nargv = [\"{file}\"]\nparser = \"number\"\n\
             [selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
        )
        .unwrap();
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["all".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );
        assert_eq!(out.code, exit::PREFLIGHT_REFUSED, "{}", out.human);
        assert!(out.human.contains("nothing was measured"), "{}", out.human);
        assert!(!corpus.with_file_name("corpus.results").exists());
    }

    #[test]
    fn all_beside_a_named_detector_is_a_usage_error_rather_than_a_guess() {
        let reg = Registry::load(&shipped_registry()).expect("loads");
        let err = resolve_detectors(&reg, &["all".into(), "zsteg".into()]).expect_err("refused");
        assert_eq!(err.code, exit::USAGE);
        assert!(err
            .human
            .contains("already means every registered detector"));
    }

    #[test]
    fn all_is_every_detector_and_never_an_embedder() {
        let reg = Registry::load(&shipped_registry()).expect("loads");
        let picked = resolve_detectors(&reg, &["all".into()]).expect("resolved");
        assert!(picked.len() >= 5, "resolved only {}", picked.len());
        assert!(picked.iter().all(|e| e.kind == Kind::Detector));
        // Sorted, so two runs of the same command write the same files in the
        // same order.
        let names: Vec<_> = picked.iter().map(|e| e.name.clone()).collect();
        let mut sorted = names.clone();
        sorted.sort();
        assert_eq!(names, sorted);
    }

    #[test]
    fn naming_an_embedder_is_refused_before_the_corpus_is_touched() {
        let reg = Registry::load(&shipped_registry()).expect("loads");
        let embedder = reg
            .of_kind(Kind::Embedder)
            .first()
            .map(|e| e.name.clone())
            .expect("the shipped registry has embedders");
        let err = resolve_detectors(&reg, std::slice::from_ref(&embedder)).expect_err("refused");
        assert_eq!(err.code, exit::USAGE);
        assert!(err.human.contains("it cannot tell two images apart"));
    }

    #[test]
    fn the_same_detector_named_twice_is_one_measurement() {
        let reg = Registry::load(&shipped_registry()).expect("loads");
        let picked = resolve_detectors(&reg, &["zsteg".into(), "zsteg".into()]).expect("resolved");
        assert_eq!(picked.len(), 1);
    }

    /// `doctor` and `describe` answer the same question the same way,
    /// whichever kind of thing is asked about. The service is the case that
    /// forces this: it is the one a user could only discover by running it.
    #[test]
    fn doctor_and_describe_both_say_what_a_tool_needs_and_agree() {
        let dir = shipped_registry();
        let doctor = cmd_doctor(&resolved_at(&dir), None, false, false);
        assert!(
            doctor.json["needing_action"].is_number(),
            "doctor does not report how many tools need something"
        );
        for tool in doctor.json["tools"].as_array().expect("rows") {
            assert!(
                tool["needs"]["readiness"].is_string(),
                "{} has no readiness: {tool}",
                tool["name"]
            );
            assert!(tool["needs"]["steps"].is_array());
        }

        // And `describe` carries the identical structure for the same tool.
        let described = cmd_describe(&resolved_at(&dir), "stegashield", false);
        assert_eq!(described.code, exit::OK);
        assert!(described.json["needs"]["readiness"].is_string());
        assert!(
            described.human.contains("Needs from you")
                || described.human.contains("Nothing needed"),
            "{}",
            described.human
        );

        // A corpus answers in the same shape, which is the whole point: a
        // reader should not have to know which kind of thing they typed.
        let corpus = cmd_describe(&resolved_at(&dir), "reveal", false);
        assert_eq!(corpus.code, exit::OK);
        assert!(corpus.json["needs"]["steps"].is_array());
        assert!(corpus.human.contains("Needs from you"), "{}", corpus.human);
    }

    /// The legend under `list` called every image entry a sandbox with no
    /// network, which is false for a service and false in the direction that
    /// matters most.
    #[test]
    fn the_list_legend_does_not_call_a_service_a_sandbox() {
        let out = cmd_list(&resolved_at(shipped_registry()), "detectors");
        assert_eq!(out.code, exit::OK);
        assert!(
            out.human.contains("SERVICE, not sandboxed"),
            "the legend still presents every container entry as sandboxed: {}",
            out.human
        );
        assert!(out.human.contains("stegashield"), "{}", out.human);
    }

    /// Drives the binary's own code to every exit code the contract in
    /// `stegobench_core::exit` names, or records plainly why a given code
    /// cannot be reached yet.
    ///
    /// The man page is only as good as the binary's agreement with it: a
    /// documented code the binary never actually returns is worse than no
    /// table at all, because a script or an agent trusts the table.
    ///
    /// Code 3 (pre-flight refusal) IS reachable and this test drives it,
    /// which it did not used to be. This comment claimed otherwise for
    /// longer than it was true, which is the dangerous way round: a reader
    /// trusts the prose and concludes a code they can see in the body is
    /// dead. Code 7 (licence refusal) is the one still unreachable, because
    /// no corpus-licence gate is built.
    ///
    /// Asserting the gap here means the day code 7 becomes reachable without
    /// a test acknowledging it, this test starts failing and says so, rather
    /// than staying quiet about a path nobody covered.
    #[test]
    fn the_exit_code_contract_is_driven_or_explicitly_not_yet_reachable() {
        // 0: success.
        assert_eq!(cmd_schema("result-v1").code, exit::OK);

        // 1: generic failure.
        assert_eq!(
            cmd_validate(&PathBuf::from("/definitely/not/here.json")).code,
            exit::FAILURE
        );

        // 2: usage error.
        assert_eq!(cmd_schema("not-a-real-schema").code, exit::USAGE);
        assert_eq!(cmd_help(Some("not-a-real-topic")).code, exit::USAGE);

        // 3: pre-flight refusal, which `score` reaches two ways. A corpus
        // path that is not a readable directory is answered first, because a
        // stat costs nothing and a detector probe can pull a container. Then,
        // for a corpus that is there, whether the detector is on this machine,
        // refused rather than discovered after the walk.
        {
            let dir = tempfile::tempdir().unwrap();
            let reg = dir.path().join("detectors");
            std::fs::create_dir_all(&reg).unwrap();
            std::fs::write(
                reg.join("ghost.toml"),
                "name = \"ghost\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
                 [binary]\ncommand = [\"definitely-not-installed-xyzzy\"]\n\
                 version_args = [\"--version\"]\n\
                 [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
                 [invoke]\nargv = [\"{file}\"]\nparser = \"number\"\n\
                 [selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
            )
            .unwrap();
            let ask = |corpus: &Path| {
                cmd_score(
                    &resolved_at(dir.path()),
                    ScoreRequest {
                        corpus,
                        detectors: &["ghost".to_string()],
                        corpus_id: None,
                        trained_on: None,
                        records: None,
                        out: None,
                        timeout: 5,
                        jobs: 1,
                        keep_raw: false,
                        seed: None,
                        fixtures: None,
                        limit: None,
                        split: None,
                    },
                )
            };

            let missing = ask(&dir.path().join("no-such-corpus"));
            assert_eq!(missing.code, exit::PREFLIGHT_REFUSED, "{}", missing.human);
            assert!(
                missing.human.contains("no corpus at"),
                "a path that is not there should be answered as itself: {}",
                missing.human
            );

            // The same request over a corpus that IS there, so the tool check
            // is what answers and this arm cannot pass on the path alone.
            let empty = dir.path().join("empty-corpus");
            std::fs::create_dir_all(&empty).unwrap();
            let absent = ask(&empty);
            assert_eq!(absent.code, exit::PREFLIGHT_REFUSED, "{}", absent.human);
            assert!(
                absent.human.contains("not on this machine"),
                "{}",
                absent.human
            );
        }

        // 4: plugin failure. Reachable now that `score` runs: a detector that
        // broke, or a run whose answers could not be turned into an AUC, is
        // reported through this code. It is driven in the score module's own
        // tests, which can build a corpus and a registry entry without this
        // test constructing both.

        // 5: verify mismatch. Reachable: `verify` recomputes a corpus digest
        // and compares it with the one a result claims. Driven by the
        // `verify_refuses_*` tests above, against a corpus the document was
        // not measured on and against a document naming no digest at all.

        // 6: schema invalid.
        {
            let dir = tempfile::tempdir().unwrap();
            let p = dir.path().join("x.json");
            std::fs::write(&p, r#"{"hello":"world"}"#).unwrap();
            assert_eq!(cmd_validate(&p).code, exit::SCHEMA_INVALID);
        }

        // 7: licence refusal. Reachable: `fetch` refuses a corpus that may
        // not be redistributed before it looks for a transport at all. Driven
        // in the fetch module's own tests, which hand it a refusing transport
        // and prove the licence answered first.

        // 8: environment unfit. NO LONGER REACHABLE FROM A STUB: every
        // command in the tree is built, so the `not_yet` helper that used to
        // return this code is gone rather than kept as scaffolding nothing
        // stands on. Two real paths reach it: `fetch` when no transport is
        // installed, and `cmd_doctor` when a registered tool is missing or
        // broken. Doctor's wants its own test with a real registry directory
        // rather than a shortcut here.

        // 130: interrupted. NOT YET REACHABLE from a unit test: this is a
        // signal-handler exit path (SIGINT/SIGTERM), which needs a real
        // process and a real signal to drive, not a function call. No
        // signal handling exists in this binary yet to test in the first
        // place.
    }

    /// A registry entry that is installed and that nothing here can drive.
    ///
    /// `aletheia-rich` is the shipped one: a real, registered detector with a
    /// self-test and no invoke block.
    #[cfg(unix)]
    fn registry_with_an_undrivable_detector(dir: &Path) -> PathBuf {
        let reg = registry_with_one_present_and_one_missing(dir);
        std::fs::write(
            reg.join("paperwork.toml"),
            "name = \"paperwork\"\nkind = \"detector\"\nlicence = \"MIT\"\n\
             [binary]\ncommand = [\"sh\"]\nversion_args = [\"-c\", \"echo v1\"]\n\
             [emits]\noutput = \"score\"\nhigher_means_stego = true\n\
             [selftest]\nmust_detect = \"a.png\"\nmust_clear = \"b.png\"\n",
        )
        .unwrap();
        reg
    }

    /// THE FAULT THIS EXISTS TO STOP: OUR REGISTRY, THEIR CORPUS, THEIR FAULT.
    ///
    /// An entry with no invoke block cannot be driven, `doctor` has always
    /// said so, and `score` did not ask. So it announced "1 of 1 that can run
    /// here", started the tool once per image, wrote "entry declares no invoke
    /// block" against every one, and refused with "the corpus holds 0 clean
    /// and 0 stego image(s)" over a corpus holding six and twelve.
    #[cfg(unix)]
    #[test]
    fn a_detector_with_no_invoke_block_is_skipped_before_the_corpus_is_walked() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_an_undrivable_detector(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["paperwork".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );

        assert_eq!(out.code, exit::PREFLIGHT_REFUSED, "{}", out.human);
        assert!(
            out.human.contains("invoke block"),
            "the refusal has to name the real reason: {}",
            out.human
        );
        assert!(
            !out.human.contains("0 clean"),
            "our registry's gap was reported as a fault in their corpus: {}",
            out.human
        );
        assert!(
            !corpus.join("paperwork.records.jsonl").exists(),
            "an entry that cannot be driven was still run over the corpus"
        );
    }

    /// The same entry, classified the same way whether it is asked for alone
    /// or beside a detector that works. It used to be a pre-flight refusal in
    /// one case and a plugin failure in the other, which is one fact answered
    /// with two exit codes.
    #[cfg(unix)]
    #[test]
    fn an_undrivable_detector_is_a_skip_beside_a_working_one_too() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_an_undrivable_detector(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let out_dir = tmp.path().join("results");

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["paperwork".to_string(), "sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: Some(&out_dir),
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );

        assert_eq!(out.code, exit::PREFLIGHT_REFUSED, "{}", out.human);
        assert!(
            out.human.contains("skipped: declares no invoke block"),
            "{}",
            out.human
        );
        assert_eq!(out.json["skipped"], serde_json::json!(1), "{}", out.json);
        assert_eq!(out.json["failed"], serde_json::json!(0), "{}", out.json);
        assert!(
            out_dir.join("sizer.json").exists(),
            "the detector that could run lost its work: {}",
            out.human
        );
    }

    /// `--limit` TAKES A PREFIX AND A CORPUS PUTS ITS COVERS FIRST.
    ///
    /// So the flag advertised for a smoke test reliably produces an all-clean
    /// set, and the refusal that followed talked only about the corpus. The
    /// corpus is fine; the slice asked for is not, and the message has to say
    /// which of the two it is.
    #[cfg(unix)]
    #[test]
    fn a_limit_that_reaches_only_covers_names_the_flag_rather_than_the_corpus() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: Some(2),
                split: None,
            },
        );

        assert_eq!(out.code, exit::PREFLIGHT_REFUSED, "{}", out.human);
        assert!(out.human.contains("--limit 2"), "{}", out.human);
        assert!(
            out.human.contains("covers before its stego arms"),
            "{}",
            out.human
        );
        assert_eq!(out.json["reason"], serde_json::json!("one-sided"));
    }

    /// The same refusal without the flag says nothing about the flag, because
    /// then the corpus genuinely is the one-sided thing.
    #[cfg(unix)]
    #[test]
    fn a_one_sided_corpus_is_not_blamed_on_a_limit_nobody_gave() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("covers-only");
        std::fs::create_dir_all(&corpus).unwrap();
        for i in 0..3 {
            std::fs::write(corpus.join(format!("c{i}.png")), test_png(0)).unwrap();
            std::fs::write(
                corpus.join(format!("c{i}.json")),
                r#"{"role":"clean","sha256":"0"}"#,
            )
            .unwrap();
        }

        let out = cmd_score(
            &resolved_at(&reg),
            ScoreRequest {
                corpus: &corpus,
                detectors: &["sizer".to_string()],
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 5,
                jobs: 1,
                keep_raw: false,
                seed: None,
                fixtures: None,
                limit: None,
                split: None,
            },
        );

        assert_eq!(out.code, exit::PREFLIGHT_REFUSED, "{}", out.human);
        assert!(!out.human.contains("--limit"), "{}", out.human);
        assert_eq!(out.json["reason"], serde_json::json!("one-sided"));
    }

    /// PLAN'S WHOLE JOB IS TELLING YOU THINGS BEFORE YOU SPEND THE TIME.
    ///
    /// A corpus whose stego images differ from their covers in more than the
    /// payload planned silently, and `score` then found it instantly and
    /// statically from the same records.
    #[cfg(unix)]
    #[test]
    fn plan_warns_about_a_confounded_corpus_before_anything_is_scored() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("confounded");
        std::fs::create_dir_all(&corpus).unwrap();
        // Wider than its cover, which is a second variable a measurement over
        // this corpus would pick up along with the payload.
        let mut wider = b"\x89PNG\r\n\x1a\n".to_vec();
        wider.extend_from_slice(&13u32.to_be_bytes());
        wider.extend_from_slice(b"IHDR");
        wider.extend_from_slice(&64u32.to_be_bytes());
        wider.extend_from_slice(&64u32.to_be_bytes());
        wider.extend_from_slice(&[8, 2, 0, 0, 0]);
        wider.extend_from_slice(&[0, 0, 0, 0]);
        for i in 0..3 {
            std::fs::write(corpus.join(format!("c{i}.png")), test_png(0)).unwrap();
            std::fs::write(
                corpus.join(format!("c{i}.json")),
                r#"{"role":"clean","sha256":"0"}"#,
            )
            .unwrap();
            std::fs::write(corpus.join(format!("s{i}.png")), &wider).unwrap();
            std::fs::write(
                corpus.join(format!("s{i}.json")),
                format!(r#"{{"role":"stego","source_png":"c{i}.png","sha256":"0"}}"#),
            )
            .unwrap();
        }

        let out = cmd_plan(
            &resolved_at(&reg),
            &[
                "score".into(),
                "--corpus".into(),
                corpus.display().to_string(),
                "--detector".into(),
                "sizer".into(),
            ],
        );

        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert!(
            out.human.contains("differ from their cover in more than"),
            "plan said nothing about a corpus score would warn about: {}",
            out.human
        );
        assert!(
            out.human.contains("different sizes"),
            "the warning has to carry an example: {}",
            out.human
        );
        assert!(
            !out.json["corpus_notes"].as_array().unwrap().is_empty(),
            "{}",
            out.json
        );
    }

    /// A PLAN THAT SAYS "ABOUT" IN FRONT OF A GUESS IS CONFIDENTLY WRONG.
    ///
    /// Every total is arithmetic over a `seconds_per_image` somebody wrote
    /// into a registry entry, covering the work on an image and not the cost
    /// of starting the tool once per image. Measured 2026-09-30: an estimate
    /// of about 3 minutes against roughly 11 minutes on the same machine.
    #[cfg(unix)]
    #[test]
    fn plan_says_its_rate_is_declared_rather_than_measured() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let sizer = reg.join("sizer.toml");
        let with_cost = format!(
            "{}\n[cost]\nseconds_per_image = 0.5\n",
            std::fs::read_to_string(&sizer).unwrap()
        );
        std::fs::write(&sizer, with_cost).unwrap();
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_plan(
            &resolved_at(&reg),
            &[
                "score".into(),
                "--corpus".into(),
                corpus.display().to_string(),
                "--detector".into(),
                "sizer".into(),
            ],
        );

        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert!(
            out.human.contains("Total          at least"),
            "{}",
            out.human
        );
        // The estimate still says it is declared rather than measured, and
        // still says the total is a floor. What moved into `plan --help` is
        // the REASONING for both; what stays here is the claim itself, which
        // is the part a reader has to see without asking for it.
        assert!(out.human.contains("declared"), "{}", out.human);
        assert!(
            out.human.contains("Read the total as a floor"),
            "{}",
            out.human
        );
        // And the worst-case figure is now bare, because the parenthetical
        // explaining it was longer than the line it qualified.
        assert!(out.human.contains("Worst case"), "{}", out.human);
        assert!(
            !out.human.contains("a ceiling, not a forecast"),
            "the explanation belongs in `plan --help`, not in every run: {}",
            out.human
        );
        assert_eq!(out.json["rate_source"], serde_json::json!("declared"));
        assert_eq!(
            out.json["estimate_is_a_lower_bound"],
            serde_json::json!(true)
        );
    }

    /// `plan` wraps a whole command, so the natural first attempt puts
    /// `score`'s flags straight after `plan`. clap answered that with "to
    /// pass '--corpus' as a value, use '-- --corpus'", which quotes the flag
    /// as a literal word and plans nothing at all.
    #[test]
    fn a_plan_command_starting_with_a_flag_is_answered_with_the_verb_it_needs() {
        let argv = |words: &[&str]| words.iter().map(|w| w.to_string()).collect::<Vec<_>>();
        let unknown = clap::error::ErrorKind::UnknownArgument;

        let hint = plan_verb_hint(unknown, &argv(&["plan", "--corpus", "./mine"]))
            .expect("the natural first attempt is not recognised");
        assert_eq!(hint, "--corpus ./mine");

        // A path with a space survives being pasted back.
        let quoted = plan_verb_hint(unknown, &argv(&["plan", "--corpus", "my corpus"]))
            .expect("a quoted value is not recognised");
        assert_eq!(quoted, "--corpus 'my corpus'");

        // A leading word is either a verb `plan` reads or one it refuses by
        // name, and both are better answers than this one.
        assert!(plan_verb_hint(unknown, &argv(&["plan", "score", "--corpus", "x"])).is_none());
        // Another command's unknown flag is not this.
        assert!(plan_verb_hint(unknown, &argv(&["score", "--nope"])).is_none());
        assert!(plan_verb_hint(unknown, &argv(&["plan"])).is_none());
        assert!(plan_verb_hint(unknown, &[]).is_none());
        // A different failure keeps clap's own answer, which is better for it.
        assert!(plan_verb_hint(
            clap::error::ErrorKind::MissingRequiredArgument,
            &argv(&["plan", "--corpus", "x"])
        )
        .is_none());
    }

    /// `metrics` has published a stable word beside every refusal since it
    /// shipped and `score` published prose. A caller branching on the second
    /// is matching English that nothing promises to keep.
    #[cfg(unix)]
    #[test]
    fn every_score_refusal_carries_a_word_a_script_can_branch_on() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);
        let ask = |corpus: &Path, detectors: &[String]| {
            cmd_score(
                &resolved_at(&reg),
                ScoreRequest {
                    corpus,
                    detectors,
                    corpus_id: None,
                    trained_on: None,
                    records: None,
                    out: None,
                    timeout: 5,
                    jobs: 1,
                    keep_raw: false,
                    seed: None,
                    fixtures: None,
                    limit: None,
                    split: None,
                },
            )
        };

        let cases = [
            (
                ask(&tmp.path().join("nope"), &["sizer".to_string()]),
                "corpus-missing",
            ),
            (ask(&corpus, &["ghost".to_string()]), "nothing-available"),
            (ask(&corpus, &["nobody".to_string()]), "unknown-detector"),
            (
                ask(&corpus, &["all".to_string(), "sizer".to_string()]),
                "all-with-others",
            ),
        ];
        for (out, want) in cases {
            assert_ne!(out.code, exit::OK, "{}", out.human);
            assert_eq!(out.json["reason"], serde_json::json!(want), "{}", out.human);
            // The prose is still there beside the word, because a person
            // reads one and a script reads the other.
            assert!(out.json["error"].is_string(), "{}", out.json);
        }
    }

    /// The flagship integrity message, which rendered with runs of a dozen
    /// spaces inside it because the format string had been collapsed onto one
    /// line with its continuations baked in as literal whitespace.
    #[test]
    fn the_content_digest_refusal_wraps_like_every_other_message() {
        let dir = tempfile::tempdir().unwrap();
        let corpus = dir.path().join("corpus");
        corpus_honest(&corpus, &[b"cover bytes", b"stego bytes"]);
        let digest = score::corpus_digest(&corpus).unwrap().unwrap();
        let doc = dir.path().join("r.json");
        result_claiming(&doc, &digest);
        let mut sealed: Result1 =
            serde_json::from_str(&std::fs::read_to_string(&doc).unwrap()).unwrap();
        sealed.seal();
        sealed.metrics.auc = 0.99;
        std::fs::write(&doc, serde_json::to_string(&sealed).unwrap()).unwrap();

        let out = cmd_verify(&doc, &corpus, None, true);
        assert_eq!(out.code, exit::VERIFY_MISMATCH, "{}", out.human);
        assert!(
            !out.human.contains("   "),
            "the message still carries runs of whitespace:\n{}",
            out.human
        );
        assert!(
            out.human
                .contains("Every field except the two that record WHEN the run happened"),
            "the sentence did not survive rewrapping:\n{}",
            out.human
        );
    }

    /// `stegobench list detectors | grep zsteg` found nothing, and
    /// `stegobench doctor > report.txt` wrote an empty file, because a
    /// listing is content and every one of them was going to stderr.
    #[test]
    fn a_listing_is_content_and_a_refusal_is_not() {
        for command in [
            Command::List { kind: "all".into() },
            Command::Describe {
                name: "zsteg".into(),
                toml: false,
            },
            Command::Help { topic: None },
            Command::Completions {
                shell: clap_complete::Shell::Bash,
            },
            Command::Plan {
                command: vec!["score".into()],
            },
            Command::Metrics {
                file: None,
                at: vec![],
            },
        ] {
            assert!(human_is_content(&command), "not routed to stdout");
        }
        for command in [
            Command::Score {
                corpus: PathBuf::from("c"),
                detector: vec!["d".into()],
                split: None,
                corpus_id: None,
                trained_on: None,
                records: None,
                out: None,
                timeout: 60,
                jobs: 1,
                keep_raw: false,
                seed: None,
                limit: None,
            },
            Command::Validate {
                file: PathBuf::from("f"),
            },
            Command::Fetch {
                corpus: "c".into(),
                tier: "nano".into(),
                dest: None,
                max_bytes: None,
                budget_minutes: 1,
            },
        ] {
            assert!(!human_is_content(&command), "progress reached stdout");
        }
    }

    /// `doctor` prints the same report whether it ends in 0 or 8, so the
    /// report is not routed on the exit code: redirecting it on the machine
    /// whose report was worth keeping wrote an empty file.
    #[test]
    fn doctors_report_goes_to_stdout_under_either_verdict() {
        let out = cmd_doctor(&resolved_at(shipped_registry()), None, false, false);
        assert!(out.payload_on_stdout, "{}", out.human);
    }

    /// `--no-selftest` promises a faster report, not a different verdict. It
    /// used to hand exit 8 to a machine where plain `doctor` exited 0, because
    /// fitness was read off self-test results the flag had just skipped.
    #[test]
    fn skipping_the_self_tests_judges_what_is_installed() {
        let out = cmd_doctor(&resolved_at(shipped_registry()), None, false, false);
        assert_eq!(
            out.json["verified"].as_u64(),
            Some(0),
            "this test only means something when nothing was self-tested"
        );
        let present = out.json["present"].as_u64().expect("a count");
        let expected = if present == 0 {
            exit::ENVIRONMENT_UNFIT
        } else {
            exit::OK
        };
        assert_eq!(out.code, expected, "{present} installed:\n{}", out.human);
        assert!(
            !out.human.contains("no tool here is usable"),
            "judged on a self-test it did not run:\n{}",
            out.human
        );
        if present > 0 {
            assert!(
                out.human.contains("The self-tests were skipped"),
                "the report claims more than it checked:\n{}",
                out.human
            );
        }
    }

    /// `--toml` prints the line somebody wrote, not the answer the type
    /// derives from it.
    ///
    /// `may_be_served` is computed at the serialisation boundary so the rule
    /// behind it has one home, and it reaches every surface that serialises
    /// an entry. `--json` should carry it and this should not: a reader
    /// round-tripping an entry through this flag would otherwise commit a key
    /// nobody typed.
    #[test]
    fn the_toml_output_carries_no_key_the_author_never_wrote() {
        let resolved = resolved_at(shipped_registry());
        let out = cmd_describe(&resolved, "zsteg", true);
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert!(
            !out.human
                .contains(stegobench_core::registry::MAY_BE_SERVED_KEY),
            "the derived answer reached the verbatim entry:\n{}",
            out.human
        );
        // And it is still a document a parser accepts, which is what the flag
        // is for.
        let parsed: toml::Value =
            toml::from_str(&out.human).expect("the entry parses back as TOML");
        assert!(parsed.get("redistribution").is_some(), "{parsed:?}");

        // The JSON surface is the one that must carry it, because that is
        // what the publishing script reads.
        let json = cmd_describe(&resolved, "zsteg", false).json;
        assert_eq!(
            json.get(stegobench_core::registry::MAY_BE_SERVED_KEY),
            Some(&serde_json::json!(true)),
            "{json}"
        );
    }

    /// A machine that has downloaded nothing is a fresh install, not a
    /// broken one, and it used to be told it was "not usable".
    ///
    /// Raised by the rung 1 journey on 2026-10-02: the reader had installed
    /// the tool five minutes earlier, read "UNFIT: no tool here is usable" in
    /// capitals with exit 8, and took it as a verdict on their computer. The
    /// code stays 8, because nothing can be measured; the sentence has to say
    /// which situation it is and carry the way out.
    #[test]
    fn a_machine_with_nothing_installed_is_told_it_is_fresh_rather_than_broken() {
        let out = cmd_doctor(&resolved_at(shipped_registry()), None, false, false);
        let present = out.json["present"].as_u64().expect("a count");
        if present > 0 {
            // This machine has a tool installed, so the state under test is
            // not reachable here. Said out loud rather than passing quietly,
            // because a test that silently checks nothing is worse than none.
            eprintln!("skipped: {present} tool(s) installed, so not a fresh machine");
            return;
        }
        assert_eq!(out.code, exit::ENVIRONMENT_UNFIT, "{}", out.human);
        assert!(
            out.human.contains("nothing is installed here yet"),
            "does not say which situation this is:\n{}",
            out.human
        );
        assert!(
            out.human.contains("rather than a fault on this machine"),
            "still reads as a verdict on the machine:\n{}",
            out.human
        );
        assert!(
            !out.human.contains("no tool here is usable"),
            "a fresh install is still called unusable:\n{}",
            out.human
        );
        // And it names a way out, which is the finding the journey actually
        // raised: they had to guess which of seven to install.
        assert!(
            out.human.contains("you need ONE detector"),
            "no way out of the state it just reported:\n{}",
            out.human
        );
        assert!(
            out.human.contains("docker pull"),
            "names no command to run:\n{}",
            out.human
        );
    }

    /// The listing is where the same reader nearly stopped, with seven names
    /// and no way to choose between them.
    #[test]
    fn listing_the_detectors_names_the_cheapest_place_to_start() {
        let resolved = resolved_at(shipped_registry());
        let out = cmd_list(&resolved, "detectors");
        assert!(
            out.human.contains("you need ONE detector"),
            "the listing still answers seven names and no guidance:\n{}",
            out.human
        );
        // It must not rank them. Saying which detector is BETTER is the
        // overclaim this project exists to correct, and this listing has no
        // evidence for it.
        for word in ["best", "recommended", "most accurate", "strongest"] {
            assert!(
                !out.human.to_lowercase().contains(word),
                "the listing ranks detectors by {word:?}:\n{}",
                out.human
            );
        }
    }

    /// A human reading "FIT" does not need the number and a script reads the
    /// status. The documented contract stays under `--help`.
    #[test]
    fn the_verdict_line_carries_no_exit_code() {
        for strict in [false, true] {
            let out = cmd_doctor(&resolved_at(shipped_registry()), None, strict, false);
            assert!(
                out.human.contains("\nFIT:") || out.human.contains("\nUNFIT:"),
                "no verdict at all:\n{}",
                out.human
            );
            assert!(
                !out.human.contains("(exit "),
                "the verdict quotes its own exit code:\n{}",
                out.human
            );
        }
    }

    /// "0 answering" was undefined anywhere a reader would meet it.
    #[test]
    fn the_self_test_summary_uses_a_word_the_help_defines() {
        let out = cmd_doctor(&resolved_at(shipped_registry()), None, false, false);
        let line = out
            .human
            .lines()
            .find(|l| l.contains("Stegobench's own self-test:"))
            .expect("a summary line");
        assert!(line.contains("responded"), "{line}");
        assert!(!line.contains("answering"), "{line}");

        let mut doctor = Cli::command();
        doctor.build();
        let help = doctor
            .get_subcommands()
            .find(|s| s.get_name() == "doctor")
            .expect("doctor is a command")
            .get_long_about()
            .expect("doctor explains itself")
            .to_string();
        assert!(help.contains("responded"), "the word is defined nowhere");
        // The screen and the JSON say the same word, so a reader moving
        // between them is not looking at two vocabularies.
        assert_eq!(out.json["responded"], serde_json::json!(0));
    }

    /// `registry  plugins/registry` and `fixtures  fixtures`: the second is a
    /// value repeating its own key, which reads as a bug rather than as an
    /// answer to "which images were these".
    #[test]
    fn doctor_names_where_it_read_from_in_full() {
        let out = cmd_doctor(&resolved_at(shipped_registry()), None, false, false);
        let first = out.human.lines().next().expect("a registry line");
        let path = first
            .strip_prefix("registry  ")
            .expect("the registry line comes first");
        assert!(
            Path::new(path).is_absolute(),
            "a relative path is not somewhere a reader can go: {first}"
        );
    }

    /// `plan score --detector all` estimated seven detectors and `score` then
    /// ran four, because the plan never asked the availability question the
    /// run asks. It overstated the job and hid three blockers.
    #[cfg(unix)]
    #[test]
    fn a_plan_leaves_out_what_score_would_skip() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_plan(
            &resolved_at(&reg),
            &[
                "score".into(),
                "--corpus".into(),
                corpus.display().to_string(),
                "--detector".into(),
                "all".into(),
            ],
        );
        assert_eq!(out.code, exit::OK, "{}", out.human);
        assert_eq!(out.json["runnable_detectors"], serde_json::json!(1));
        assert_eq!(out.json["unavailable_detectors"], serde_json::json!(1));
        assert!(
            out.human.contains("to score with each of 1 detector(s)"),
            "the header counts what cannot run:\n{}",
            out.human
        );
        assert!(
            out.human.contains("ghost") && out.human.contains("NOT AVAILABLE"),
            "the blocker is not on the screen:\n{}",
            out.human
        );
        assert!(
            out.human.contains("cannot run here and are left out"),
            "nothing says the figures exclude it:\n{}",
            out.human
        );
        let ghost = out.json["per_detector"]
            .as_array()
            .expect("per detector")
            .iter()
            .find(|d| d["detector"] == "ghost")
            .expect("ghost is listed");
        assert_eq!(ghost["available"], serde_json::json!(false));
        assert!(ghost["estimated_seconds"].is_null(), "{ghost}");
    }

    /// A plan over nothing that can run refuses with the word and the code
    /// `score` refuses with, rather than printing a table of zeroes.
    #[cfg(unix)]
    #[test]
    fn a_plan_over_nothing_available_refuses_the_way_score_does() {
        let tmp = tempfile::tempdir().unwrap();
        let reg = registry_with_one_present_and_one_missing(tmp.path());
        let corpus = tmp.path().join("corpus");
        scratch_corpus(&corpus);

        let out = cmd_plan(
            &resolved_at(&reg),
            &[
                "score".into(),
                "--corpus".into(),
                corpus.display().to_string(),
                "--detector".into(),
                "ghost".into(),
            ],
        );
        assert_eq!(out.code, exit::PREFLIGHT_REFUSED, "{}", out.human);
        assert_eq!(out.json["reason"], serde_json::json!("nothing-available"));
    }

    /// Zero parsed, and meant "kill it before it can answer": every item
    /// recorded a timeout under a document that exited zero.
    #[test]
    fn a_timeout_of_zero_is_refused_rather_than_given_a_meaning() {
        let Err(e) = Cli::try_parse_from([
            "stegobench",
            "score",
            "--corpus",
            "c",
            "--detector",
            "d",
            "--timeout",
            "0",
        ]) else {
            panic!("0 has no honest meaning here")
        };
        let said = e.to_string();
        assert!(said.contains("kill the detector"), "{said}");
        assert!(said.contains("no value meaning no timeout"), "{said}");

        let ok = Cli::try_parse_from([
            "stegobench",
            "score",
            "--corpus",
            "c",
            "--detector",
            "d",
            "--timeout",
            "1",
        ]);
        assert!(ok.is_ok(), "one second is a real deadline");
    }

    /// clap offered `--records` for `--threads`: five edits apart, and about
    /// writing files rather than about concurrency.
    #[test]
    fn an_unrelated_flag_is_not_offered_as_a_correction() {
        let Err(e) = Cli::try_parse_from([
            "stegobench",
            "score",
            "--corpus",
            "c",
            "--detector",
            "d",
            "--threads",
            "4",
        ]) else {
            panic!("there is no --threads")
        };
        assert_eq!(
            misleading_suggestion(&e).as_deref(),
            Some("--threads"),
            "clap's suggestion was let through: {e}"
        );

        // A genuine near miss still gets clap's own answer, which is better.
        let Err(e) =
            Cli::try_parse_from(["stegobench", "score", "--corpu", "c", "--detector", "d"])
        else {
            panic!("--corpu is a typo")
        };
        assert_eq!(misleading_suggestion(&e), None, "{e}");
    }

    /// "benchmark" is the word in the product's own name and the likeliest
    /// first guess there is. It used to reach the generic unknown-command
    /// line, which says what is absent and nothing about what is present.
    #[test]
    fn the_words_that_mean_score_are_redirected_to_score() {
        for word in ["benchmark", "eval", "evaluate", "Benchmark"] {
            assert!(right_idea(word), "{word} gets the generic message");
            assert!(
                !no_extraction(word),
                "{word} is not asking for a payload, only using the wrong verb"
            );
            assert!(
                !meant_examine(word),
                "{word} means the measurement, not an examination"
            );
        }
        let out = cmd_right_idea("benchmark");
        assert_eq!(out.code, exit::USAGE);
        assert!(
            out.human.contains("stegobench score --corpus"),
            "{}",
            out.human
        );
        assert!(
            !out.human.contains("does not pull a hidden payload"),
            "a reader who typed benchmark has understood the tool:\n{}",
            out.human
        );
    }

    /// `describe` said "18 files in all" over a corpus `fetch` correctly
    /// reports as 39 files. The field holds images.
    #[test]
    fn the_starter_corpus_counts_the_same_thing_on_every_screen() {
        let reg = Registry::load(&shipped_registry()).expect("the real registry loads");
        let starter = reg.corpora.get(STARTER_ID).expect("the starter is listed");
        let facts = corpus_facts(starter);
        let size = facts
            .iter()
            .find(|(k, _)| *k == "Size")
            .map(|(_, v)| v.clone())
            .expect("a size fact");
        assert_eq!(size, "6 covers, 18 images in all", "{size}");
        assert_eq!(
            EMBEDDED_STARTER.len(),
            39,
            "the count the quickstart quotes for `fetch` has moved"
        );
    }

    /// TWO "Next:" LINES ARE NONE.
    ///
    /// A run of the shipped starter corpus into a directory closed with both
    /// "Next: stegobench report ..." and "Next: stegobench describe
    /// pentimento-core", one under the other.
    #[test]
    fn a_run_closes_with_one_next_step() {
        let doc: Result1 = serde_json::from_value(serde_json::json!({
            "schema": stegobench_core::result::RESULT_SCHEMA_ID,
            "subject": {"name": "x", "version": "sha256:a", "kind": "detector"},
            "corpus": {"name": "c", "source": "supplied", "digest": "sha256:a", "pairs": 2},
            "arm": {"embedder": "wow", "domain": "spatial", "format": "png"},
            "metrics": {"auc": 0.9, "tpr_at_fpr": {}, "n_clean": 1, "n_stego": 1, "n_error": 0},
            "provenance": {
                "plugins": [{"name": "x", "image": "sha256:a", "determinism": "nondeterministic", "pinned_by": "executable-hash", "isolation": "host"}],
                "harness_version": "0.1.0",
                "started_utc": "2026-09-25T00:00:00Z",
                "elapsed_seconds": 1.0,
                "network_reachable": true
            },
            "declarations": {
                "split_discipline": "not-applicable",
                "pairing": "unverified",
                "configuration": "custom",
                "self_reported": false
            }
        }))
        .expect("the fixture document parses");

        let dir = tempfile::tempdir().unwrap();
        let reg = Registry::load(&shipped_registry()).expect("the real registry loads");
        let offer = fetch::offer(&reg).expect("the registry offers a real corpus");
        let outcomes = vec![
            (
                "one".to_string(),
                Outcome::Measured {
                    result: Box::new(doc.clone()),
                    tally: stegobench_plugin::runner::Tally::default(),
                    written: Some(dir.path().join("one.json")),
                },
            ),
            (
                "two".to_string(),
                Outcome::Measured {
                    result: Box::new(doc),
                    tally: stegobench_plugin::runner::Tally::default(),
                    written: Some(dir.path().join("two.json")),
                },
            ),
        ];
        let out = summarise(
            &outcomes,
            &Some(dir.path().to_path_buf()),
            true,
            Some(&offer),
        );
        let nexts = out.human.lines().filter(|l| l.starts_with("Next:")).count();
        assert_eq!(nexts, 1, "two next steps is none:\n{}", out.human);
        assert!(
            out.human.contains("Then: "),
            "the second suggestion was dropped rather than ordered:\n{}",
            out.human
        );
        assert!(
            out.human.contains("is a demonstration, not a measurement."),
            "{}",
            out.human
        );
    }

    /// A version probe answers with whatever the tool prints, and a tool that
    /// does not understand `--version` prints something else entirely.
    ///
    /// A Python stand-in answered `Traceback (most recent call last):`, which
    /// landed in the column every other row fills with a digest, under the word
    /// `present`, with nothing saying the probe had failed.
    #[test]
    fn a_version_probe_that_printed_wreckage_is_reported_as_a_failed_probe() {
        let hash = "a".repeat(64);
        let digest = format!("sha256:{hash}");
        let short = format!("sha256:{}", &hash[..12]);

        let wreck = pin_columns(&format!("Traceback (most recent call last): {digest}"));
        assert_eq!(wreck, format!("version probe failed  {short}"));

        let good = pin_columns(&format!("stegcore 0.9.1 {digest}"));
        assert_eq!(good, format!("stegcore 0.9.1  {short}"));

        // A container's pin is a digest already, and keeps its marker.
        assert_eq!(pin_columns(&digest), short);

        // A probe that could not run at all is the same answer, not a version.
        assert_eq!(
            pin_columns(&format!("unknown version {digest}")),
            format!("version probe failed  {short}")
        );
    }

    /// A name exactly as wide as the pad ate the gap and ran into the word
    /// beside it: `stegashield-edge present`, against `stegcore         MISSING`.
    #[test]
    fn a_doctor_row_keeps_its_gap_however_long_the_longest_name_is() {
        let names = ["stegcore", "stegashield-edge"];
        let width = names.iter().map(|n| n.len()).max().expect("names") + 1;
        let rows: Vec<String> = names
            .iter()
            .map(|name| {
                doctor_row(
                    &availability::Availability {
                        name: (*name).to_string(),
                        presence: Presence::Absent {
                            reason: "not on PATH".into(),
                        },
                        verified: None,
                        missing_secrets: Vec::new(),
                    },
                    true,
                    width,
                )
            })
            .collect();
        let columns: Vec<usize> = rows
            .iter()
            .map(|r| r.find("MISSING").expect("a state column"))
            .collect();
        assert_eq!(columns[0], columns[1], "ragged columns: {rows:?}");
        for (name, row) in names.iter().zip(&rows) {
            assert!(
                row[name.len()..].starts_with("  "),
                "no gap after the name: {row:?}"
            );
        }
    }

    /// One detector is one detector, not `the 1 detector(s) asked for`.
    #[test]
    fn the_nothing_available_refusal_reads_as_english_for_one_detector() {
        let reg = Registry::load(&shipped_registry()).expect("the real registry loads");
        let all: Vec<&stegobench_core::registry::Entry> = reg.entries.values().collect();

        let one = nothing_available(&all[..1], &[Some("not pulled".to_string())]);
        assert!(
            one.starts_with("the one detector you asked for is not available here."),
            "{one}"
        );

        let two = nothing_available(
            &all[..2],
            &[
                Some("not pulled".to_string()),
                Some("not pulled".to_string()),
            ],
        );
        assert!(
            two.starts_with("not one of the 2 detectors you asked for is available here."),
            "{two}"
        );
    }

    /// The offer is for somebody who has no real corpus, so it is dropped for
    /// the run that just scored one, and for the directory whose name collides
    /// with its id: a path is not an id and the line read as recognition.
    #[test]
    fn the_corpus_offer_is_dropped_for_a_run_it_does_not_apply_to() {
        let reg = Registry::load(&shipped_registry()).expect("the real registry loads");
        let elsewhere = Path::new("/tmp/my-samples");
        assert!(
            offer_for_run(&reg, elsewhere, None).is_some(),
            "the offer never fires at all"
        );
        assert!(offer_for_run(&reg, elsewhere, Some(fetch::OFFERED_CORPUS)).is_none());
        assert!(offer_for_run(&reg, Path::new("/tmp/pentimento-core"), None).is_none());
    }
}