cargo-judge 0.7.0

Deterministic post-refactoring analysis for Rust workspaces
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
use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use clap::{Args, Parser, ValueEnum};
use judge::AnalysisTier;
#[cfg(test)]
use judge::baseline::TriVerdict;
use judge::baseline::Verdict;
use judge::finding::{Finding, Report};
use judge::rules::duplication::DupeMode;
use serde::Serialize;
use serde::ser::SerializeMap;

mod commands;

use commands::advisory_commands::{
    run_explain_pattern, run_explain_principle, run_explain_rule, run_fix_preview, run_patterns,
    run_principles,
};
use commands::analysis_commands::{
    run_api_surface, run_boundaries, run_coverage, run_deps, run_dupes, run_errors,
    run_module_graph, run_slop, run_tests, run_unsafe,
};
use commands::baseline_output::{
    BaselineInput, BaselineOptions, BaselineRequest, analysis_errors, append_analysis_errors,
    handle_baseline_with_trend, print_pattern_delta_tty, write_json,
};
use commands::cli::{BaselineArgs, Command};
#[cfg(test)]
use commands::combined_analysis::collect_findings;
use commands::combined_analysis::collect_findings_with_progress;
use commands::deep_commands::{run_dead_code, run_explain};
use commands::health_command::run as run_health;
use commands::workspace_commands::{
    run_complexity, run_impact, run_inspect, run_map, run_refactor, run_structure,
};

const DEFAULT_BASELINE_HEALTH: &str = ".judge/baseline-health.json";
const DEFAULT_BASELINE_DUPES: &str = ".judge/baseline-dupes.json";
const DEFAULT_BASELINE_DEPS: &str = ".judge/baseline-deps.json";
const DEFAULT_BASELINE_BOUNDARIES: &str = ".judge/baseline-boundaries.json";
const DEFAULT_BASELINE_ALL: &str = ".judge/baseline.json";
const DEFAULT_BASELINE_COVERAGE: &str = ".judge/baseline-coverage.json";
const DEFAULT_BASELINE_API_SURFACE: &str = ".judge/baseline-api-surface.json";
const DEFAULT_BASELINE_MODULE_GRAPH: &str = ".judge/baseline-module-graph.json";
const DEFAULT_PATTERN_BASELINE: &str = ".judge/baseline-patterns.json";
#[cfg(feature = "deep")]
const DEFAULT_BASELINE_DEAD_CODE: &str = ".judge/baseline-dead-code.json";

/// `cargo judge dupes`'s TTY view prints at most this many clone families
/// (GitHub issue #7: on a large workspace the unindicated truncation reads
/// as a complete list — grepping the TTY output for a just-touched file and
/// finding nothing looked like "no duplication" when the full graph, only
/// visible via `--format json`, told a different story). The header's
/// `clone families: N` line already gives the true total; the loop below
/// additionally prints an explicit "... and N more" trailer whenever the
/// list is actually truncated, so the TTY view can't be mistaken for the
/// full picture on its own.
const DUPE_FAMILY_TTY_LIMIT: usize = 15;

#[derive(Debug, Parser)]
#[command(
    name = "cargo judge",
    version,
    about = "Deterministic post-refactoring analysis for Rust workspaces",
    long_about = "Deterministic post-refactoring analysis for Rust workspaces. Analyses the current source tree, workspace structure, public APIs, and dependency graph without Git history or code provenance."
)]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
    /// Output format (bare `cargo judge` only — a combined run across every
    /// detector; see todo.md §4 "Decision Surface", §8).
    #[command(flatten)]
    baseline_args: BaselineArgs,
    /// Write versioned JSON Lines progress events for a bare combined run.
    /// The final report remains on stdout; this separate file is flushed
    /// after every phase event so agents can observe long-running analysis.
    #[arg(long, value_name = "PATH")]
    progress: Option<PathBuf>,
    /// Show every finding in the bare combined TTY report instead of the
    /// default rule-grouped decision summary.
    #[arg(long)]
    details: bool,
    /// Terminal color policy for the bare combined report. `auto` uses color
    /// only on a terminal and honours `NO_COLOR`; use `always` to force it.
    #[arg(long, value_enum, default_value_t = ColorChoice::Auto)]
    color: ColorChoice,
    /// Write the artifact to this path. With `--format json`, the default is
    /// `.judge/<command>.json`; with the bare `cargo judge --format
    /// markdown`, the default is `.judge/judge.md`. This option selects a
    /// different artifact path.
    #[arg(long, global = true, value_name = "PATH")]
    output: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct DupesOptions {
    /// How aggressively token spans must match to count as duplicates.
    #[arg(long, value_enum, default_value = "mild")]
    mode: DupeModeArg,
    /// Minimum span length, in tokens — spans shorter than this are
    /// ignored so trivial one-liners don't dominate every family.
    #[arg(long, default_value_t = judge::rules::duplication::DEFAULT_MIN_TOKENS)]
    min_tokens: usize,
    #[command(flatten)]
    baseline_args: BaselineArgs,
    /// Analyze generated files too (see todo.md §3.A). Off by default —
    /// duplication in generated code isn't actionable the way it is in
    /// authored code.
    #[arg(long)]
    include_generated: bool,
    /// Include test-only functions and integration-test targets. Off by
    /// default so fixture duplication does not dominate refactoring signals.
    #[arg(long)]
    include_tests: bool,
}

#[derive(Debug, Args)]
struct HealthOptions {
    /// Include the numeric health score.
    #[arg(long)]
    score: bool,
    /// Show findings caused by another finding, not just root findings.
    #[arg(long)]
    show_cascades: bool,
    #[command(flatten)]
    baseline_args: BaselineArgs,
    /// Analyze generated files too (see todo.md §3.A).
    #[arg(long)]
    include_generated: bool,
}

#[derive(Debug, Args)]
struct DepsOptions {
    #[command(flatten)]
    baseline_args: BaselineArgs,
    /// Opt-in: also check declared dependencies against the real
    /// crates.io sparse index and REST API (`phantom-crate`,
    /// `phantom-version`, `fresh-low-reputation-dep`). Off by default —
    /// judge makes no network calls unless explicitly asked to (see
    /// todo.md §1 "kein SaaS, keine Telemetrie, lokal deterministisch").
    /// `name-collision-risk` always runs; it's fully local.
    #[arg(long)]
    check_crates_io: bool,
    /// Opt-in: also run a full `cargo check --workspace --all-targets` with
    /// rustc's stable `unused_crate_dependencies` lint enabled and import
    /// its result as `unused-dependency` findings. Off by default — unlike
    /// this command's other detectors, a full compile is a different order
    /// of cost (see `judge::rules::deps` module docs "Importing rustc's
    /// `unused_crate_dependencies` lint").
    #[arg(long)]
    check_rustc_lints: bool,
    /// Opt-in: cross-reference an already-generated `cargo audit --json`
    /// report against the resolved dependency graph (`known-vulnerability`).
    /// judge never runs `cargo-audit` itself — generate the report with
    /// `cargo audit --json > PATH` first (see `judge::advisory::advisories` module
    /// docs).
    #[arg(long, value_name = "PATH")]
    audit_json: Option<PathBuf>,
    /// Explain one dependency instead of running the checks above: every
    /// workspace crate's declaration of it (kind, features, `default-features
    /// = true`?, and every source location its code identifier is referenced
    /// from), its resolved dependency-graph path(s) from this workspace's own
    /// crate(s), and an explicit statement of what judge cannot determine
    /// about its public API exposure (see `judge::rules::deps` module docs
    /// "`cargo judge deps --why <crate>`", GitHub issue #22). Ignores every
    /// other flag above and does not run any of the checks they gate; prints
    /// and exits. Only `tty`/`json` are supported.
    #[arg(long, value_name = "CRATE")]
    why: Option<String>,
}

#[derive(Debug, Args)]
struct BoundariesOptions {
    /// Path to the boundary config. Defaults to `judge.toml` in the
    /// workspace root.
    #[arg(long, value_name = "PATH")]
    config: Option<PathBuf>,
    #[command(flatten)]
    baseline_args: BaselineArgs,
    /// Print the workspace's crate dependency graph in this format instead
    /// of checking boundary rules, and exit — a pure projection of the
    /// existing architecture graph (todo.md §H), not a new rule engine.
    /// Ignores every other flag above; does not require `judge.toml`.
    #[arg(long, value_enum)]
    graph: Option<GraphFormat>,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum GraphFormat {
    /// Graphviz DOT (see `judge::rules::boundaries::CrateGraph::to_dot`).
    Dot,
    /// Mermaid `flowchart` (see `judge::rules::boundaries::CrateGraph::to_mermaid`).
    Mermaid,
}

#[derive(Debug, Args)]
struct DeadCodeOptions {
    /// Count a `#[test]`-only reference as usage. Off by default: a
    /// `pub` item only reachable from tests is still dead in production
    /// (see todo.md §3.A "Reachability-Modi").
    #[arg(long)]
    include_tests: bool,
    #[command(flatten)]
    baseline_args: BaselineArgs,
}

#[derive(Debug, Args)]
struct CoverageOptions {
    /// Path to a `cargo-llvm-cov` LCOV report (see todo.md §J). judge never
    /// measures coverage itself, only imports an already-generated snapshot.
    #[arg(long, value_name = "PATH")]
    lcov: PathBuf,
    /// Opt-in: also import an externally generated `cargo-mutants`
    /// `outcomes.json` report and flag `mutation-survivor` findings (see
    /// `judge::advisory::mutants` module docs). judge never runs `cargo-mutants`
    /// itself — generate the report with `cargo mutants` first (writes
    /// `mutants.out/outcomes.json`), then pass that path here.
    #[arg(long, value_name = "PATH")]
    mutants_json: Option<PathBuf>,
    #[command(flatten)]
    baseline_args: BaselineArgs,
}

#[derive(Debug, Args)]
struct ExplainOptions {
    /// The qualified item path (e.g. `core::retry::backoff`) to explain.
    item_path: String,
    /// Show the shortest evidenced call path from a recognized entry
    /// point. Needs the Deep Tier — build with `--features deep`.
    #[arg(long)]
    why_live: bool,
    /// Count a `#[test]`-only call as reaching the item.
    #[arg(long)]
    include_tests: bool,
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct CompareOptions {
    /// Baseline artifact produced by `cargo judge --save-baseline`.
    baseline: PathBuf,
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct PatternsOptions {
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
    /// Opt-in: cross-reference an already-generated `cargo clippy
    /// --message-format=json` report, strengthening `boolean-state-cluster`
    /// candidates with a third, cross-call-site corroborating evidence
    /// signal (`clippy::fn_params_excessive_bools`) when clippy
    /// independently flags the same function. judge never runs `cargo
    /// clippy` itself — generate the report with `cargo clippy
    /// --message-format=json > PATH` first (see `judge::advisory::clippy_import`
    /// module docs).
    #[arg(long, value_name = "PATH")]
    clippy_json: Option<PathBuf>,
    /// Save the current pattern candidates as a pattern baseline — a
    /// separate, simpler mechanism from `Finding` baselines (see
    /// `judge::pattern_baseline`: pattern candidates never gate, so their
    /// baseline carries no rule revisions, LOC, or score context).
    #[arg(long)]
    save_pattern_baseline: bool,
    /// Compare pattern candidates against a previously saved pattern
    /// baseline.
    #[arg(long, value_name = "PATH")]
    pattern_baseline: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct PrinciplesOptions {
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct ExplainPatternOptions {
    /// The pattern candidate id (see `cargo judge patterns`).
    id: String,
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct ExplainPrincipleOptions {
    /// The principle heuristic id (see `cargo judge principles`).
    id: String,
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct FixPreviewOptions {
    /// The pattern candidate id (see `cargo judge patterns`).
    id: String,
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct ExplainRuleOptions {
    /// The rule id (e.g. `catch-all-error`) — see `judge::rule_registry`.
    id: String,
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct ApiSurfaceOptions {
    #[command(flatten)]
    baseline_args: BaselineArgs,
    /// Analyze generated files too (see todo.md §3.A). Off by default —
    /// documentation completeness on generated code isn't actionable the way
    /// it is on authored code.
    #[arg(long)]
    include_generated: bool,
}

#[derive(Debug, Args)]
struct ModuleGraphOptions {
    #[command(flatten)]
    baseline_args: BaselineArgs,
    /// Analyze generated files too (see todo.md §3.A). Off by default — an
    /// unlinked/orphaned generated file isn't actionable the way it is in
    /// authored code.
    #[arg(long)]
    include_generated: bool,
}

#[derive(Debug, Args)]
struct MapOptions {
    /// Output format. JSON is intended for tooling; TTY shows the highest
    /// complexity-ranked authored files with their measured facts.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
    /// Include test-only functions in the complexity attention ranking.
    /// Production and test metrics remain separate in every output format.
    #[arg(long)]
    include_tests: bool,
}

#[derive(Debug, Args)]
struct ImpactOptions {
    /// Workspace-relative or absolute path to a discovered Rust source file.
    target: PathBuf,
    /// Output format. JSON is intended for deterministic agent tooling.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct StructureOptions {
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct ComplexityOptions {
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
    /// Include test-only functions in the ranking while keeping their metrics separate.
    #[arg(long)]
    include_tests: bool,
}

#[derive(Debug, Args)]
struct RefactorOptions {
    /// Optional workspace-relative source file to focus the queue on.
    target: Option<PathBuf>,
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct FocusedAnalysisOptions {
    /// Output format.
    #[arg(long, value_enum, default_value = "tty")]
    format: OutputFormat,
    /// Include generated source files in the analysis.
    #[arg(long)]
    include_generated: bool,
}

/// Output format shared by commands that emit findings (see todo.md §7).
/// Not every command supports every format: SARIF exists for the
/// report-producing commands; Markdown exists for the baseline delta
/// and the bare `cargo judge` review report (the PR-comment use cases) —
/// anything else is rejected as a config error instead of producing
/// half-baked output.
#[derive(Debug, Clone, Copy, ValueEnum)]
enum OutputFormat {
    /// Human-readable, reduced to root findings by default.
    Tty,
    /// Versioned JSON, always the full finding graph.
    Json,
    /// SARIF 2.1.0 (report-producing commands only — see `judge::sarif`).
    Sarif,
    /// Markdown: a delta table for baseline comparisons
    /// (see `judge::markdown`), or the shared-model review report for the
    /// bare `cargo judge` combined run (see `judge::report`).
    Markdown,
}

/// Color policy for the bare human-readable combined report.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum ColorChoice {
    Auto,
    Always,
    Never,
}

impl ColorChoice {
    fn is_enabled(self) -> bool {
        match self {
            Self::Auto => std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none(),
            Self::Always => true,
            Self::Never => false,
        }
    }
}

impl OutputFormat {
    fn label(self) -> &'static str {
        match self {
            Self::Tty => "tty",
            Self::Json => "json",
            Self::Sarif => "sarif",
            Self::Markdown => "markdown",
        }
    }
}

/// The config error (exit 2) for a format a command has no meaningful
/// rendering for (see todo.md §7: no half-baked outputs).
fn unsupported_format(context: &str, format: OutputFormat, supported: &str) -> CliError {
    CliError::Config(format!(
        "--format {} is not supported for {context}; supported formats: {supported}",
        format.label()
    ))
}

/// Renders `findings` as a SARIF 2.1.0 log (see `judge::sarif`). Findings
/// are relativized to the workspace root first — SARIF artifact URIs are
/// relative, forward-slash paths.
fn write_sarif(
    out: &mut dyn Write,
    workspace_root: &Path,
    mut findings: Vec<Finding>,
    analysis_errors: Vec<String>,
    universe: Option<judge::finding::AnalysisUniverse>,
) -> Result<(), CliError> {
    judge::finding::relativize_paths(&mut findings, workspace_root);
    let mut report = Report::with_errors(findings, analysis_errors);
    if let Some(universe) = universe {
        report = report.with_universe(universe);
    }
    writeln!(
        out,
        "{}",
        serde_json::to_string_pretty(&judge::sarif::render(&report)).unwrap()
    )?;
    Ok(())
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum DupeModeArg {
    Strict,
    Mild,
    Weak,
    Semantic,
}

impl From<DupeModeArg> for DupeMode {
    fn from(value: DupeModeArg) -> Self {
        match value {
            DupeModeArg::Strict => Self::Strict,
            DupeModeArg::Mild => Self::Mild,
            DupeModeArg::Weak => Self::Weak,
            DupeModeArg::Semantic => Self::Semantic,
        }
    }
}

/// What a successfully executed command concluded — [`main`] translates this
/// (and [`CliError`]) into the documented exit-code convention: `0` clean,
/// `1` a failing findings verdict, `2` a real error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CommandOutcome {
    /// No verdict-failing findings: exit 0. Commands without a verdict
    /// (plain reports, `inspect`, …) are always `Clean`.
    Clean,
    /// A baseline verdict failed on introduced findings: exit 1.
    FindingsFound,
}

/// A real error — always exit 2, never a findings verdict (see todo.md §5).
#[derive(Debug)]
enum CliError {
    /// A configuration input is broken: `judge.toml`, a baseline file
    /// (including an unsupported `schema_version`), or a stale baseline.
    Config(String),
    /// An analyzer/toolchain failure: cargo metadata, git, the Deep Tier,
    /// an unavailable score, or an unsupported invocation.
    Analyzer(String),
    /// Analysis produced errors, so a baseline verdict was withheld
    /// (see todo.md §15.1: no verdict on an incomplete basis).
    AnalysisIncomplete {
        context: &'static str,
        errors: Vec<String>,
    },
    /// The error was already fully rendered to the output stream (the JSON
    /// error envelope) — nothing further goes to stderr.
    Reported,
    /// Writing to the output stream failed. `BrokenPipe` gets special
    /// treatment in [`exit_code`].
    Io(std::io::Error),
}

impl From<std::io::Error> for CliError {
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}

impl From<judge::ingest::IngestError> for CliError {
    fn from(err: judge::ingest::IngestError) -> Self {
        Self::Analyzer(err.to_string())
    }
}

impl From<serde_json::Error> for CliError {
    fn from(err: serde_json::Error) -> Self {
        Self::Analyzer(err.to_string())
    }
}

impl From<judge::health_score::LocError> for CliError {
    fn from(err: judge::health_score::LocError) -> Self {
        Self::Analyzer(err.to_string())
    }
}

impl From<judge::baseline::BaselineError> for CliError {
    fn from(err: judge::baseline::BaselineError) -> Self {
        Self::Config(err.to_string())
    }
}

impl From<judge::pattern_baseline::PatternBaselineError> for CliError {
    fn from(err: judge::pattern_baseline::PatternBaselineError) -> Self {
        Self::Config(err.to_string())
    }
}

impl From<judge::rules::boundaries::BoundaryConfigError> for CliError {
    fn from(err: judge::rules::boundaries::BoundaryConfigError) -> Self {
        Self::Config(err.to_string())
    }
}

impl From<judge::advisory::coverage::LcovError> for CliError {
    fn from(err: judge::advisory::coverage::LcovError) -> Self {
        Self::Config(err.to_string())
    }
}

impl From<judge::advisory::advisories::AuditImportError> for CliError {
    fn from(err: judge::advisory::advisories::AuditImportError) -> Self {
        Self::Config(err.to_string())
    }
}

impl From<judge::advisory::clippy_import::ClippyImportError> for CliError {
    fn from(err: judge::advisory::clippy_import::ClippyImportError) -> Self {
        Self::Config(err.to_string())
    }
}

impl From<judge::advisory::mutants::MutantsImportError> for CliError {
    fn from(err: judge::advisory::mutants::MutantsImportError) -> Self {
        Self::Config(err.to_string())
    }
}

impl From<judge::suppression::SuppressionError> for CliError {
    fn from(err: judge::suppression::SuppressionError) -> Self {
        Self::Config(err.to_string())
    }
}

#[cfg(feature = "deep")]
impl From<judge::rules::dead_code::DeadCodeError> for CliError {
    fn from(err: judge::rules::dead_code::DeadCodeError) -> Self {
        Self::Analyzer(err.to_string())
    }
}

#[cfg(feature = "deep")]
impl From<judge::rules::slop_structural_deep::SlopStructuralDeepError> for CliError {
    fn from(err: judge::rules::slop_structural_deep::SlopStructuralDeepError) -> Self {
        Self::Analyzer(err.to_string())
    }
}

#[cfg(feature = "deep")]
impl From<judge::reachability::ReachabilityError> for CliError {
    fn from(err: judge::reachability::ReachabilityError) -> Self {
        Self::Analyzer(err.to_string())
    }
}

/// Renders `err` to stderr, matching the exact shapes the pre-refactor
/// `eprintln!`-then-`exit(2)` call sites produced.
fn report_error(err: &CliError) {
    match err {
        CliError::Config(message) | CliError::Analyzer(message) => eprintln!("error: {message}"),
        CliError::AnalysisIncomplete { context, errors } => {
            eprintln!("error: analysis incomplete; {context}");
            for error in errors {
                eprintln!("  {error}");
            }
        }
        CliError::Reported => {}
        CliError::Io(err) => eprintln!("error: {err}"),
    }
}

/// The documented exit-code convention: `0` clean, `1` findings verdict
/// failed, `2` real error. Broken pipe is the deliberate exception: before
/// this refactor a closed stdout (e.g. `cargo judge health --format json |
/// head`) made `println!` panic (exit 101); now it is a silent exit 0 — the
/// consumer chose to stop reading, and the verdict for the aborted render
/// was never delivered, so neither 1 nor 2 would be truthful.
fn exit_code(result: &Result<CommandOutcome, CliError>) -> u8 {
    match result {
        Ok(CommandOutcome::Clean) => 0,
        Ok(CommandOutcome::FindingsFound) => 1,
        Err(CliError::Io(err)) if err.kind() == std::io::ErrorKind::BrokenPipe => 0,
        Err(_) => 2,
    }
}

fn main() -> ExitCode {
    let mut args = std::env::args_os().collect::<Vec<_>>();
    if args.get(1).is_some_and(|arg| arg == "judge") {
        args.remove(1);
    }
    let cli = Cli::parse_from(args);

    let stdout = std::io::stdout();
    let mut out = stdout.lock();
    let result = run_with_artifact_output(cli, &mut out).and_then(|outcome| {
        out.flush()?;
        Ok(outcome)
    });
    if let Err(err) = &result {
        report_error(err);
    }
    ExitCode::from(exit_code(&result))
}

/// The functional core's entry: executes the parsed command, writing every
/// report to `out` and returning an outcome/error instead of exiting — only
/// [`main`] translates the result into a process exit code.
fn run(cli: Cli, out: &mut dyn Write) -> Result<CommandOutcome, CliError> {
    match cli.command {
        None => commands::combined::run(
            cli.baseline_args.format,
            cli.baseline_args.save_baseline,
            cli.baseline_args.baseline,
            cli.progress.as_deref(),
            cli.details,
            cli.color.is_enabled(),
            out,
        ),
        Some(command) => {
            if cli.progress.is_some() {
                return Err(CliError::Config(
                    "--progress is available only for the bare `cargo judge` combined run"
                        .to_string(),
                ));
            }
            if cli.details {
                return Err(CliError::Config(
                    "--details is available only for the bare `cargo judge` combined run"
                        .to_string(),
                ));
            }
            if cli.color != ColorChoice::Auto {
                return Err(CliError::Config(
                    "--color is available only for the bare `cargo judge` combined run".to_string(),
                ));
            }
            command.run(out)
        }
    }
}

/// Runs a JSON-formatted command, or the bare `cargo judge --format
/// markdown` review report (issue #14), into its artifact file while
/// keeping the internal command handlers stream-oriented. Every other
/// format/command combination keeps its existing stdout behavior;
/// `--output` is deliberately rejected for them so a file extension never
/// silently changes a rendering contract.
fn run_with_artifact_output(cli: Cli, out: &mut dyn Write) -> Result<CommandOutcome, CliError> {
    let Some((path, command, format)) = cli.output_artifact()? else {
        return run(cli, out);
    };

    let mut rendered = Vec::new();
    let result = run(cli, &mut rendered);
    if !rendered.is_empty() {
        match format {
            OutputFormat::Json => {
                let artifact = add_json_header(&rendered, &path, command, &result)?;
                write_artifact_file(&path, &artifact)?;
                writeln!(out, "JSON written to {}", path.display())?;
            }
            OutputFormat::Markdown => {
                write_artifact_file(&path, &rendered)?;
                writeln!(out, "Markdown written to {}", path.display())?;
            }
            OutputFormat::Tty | OutputFormat::Sarif => {
                unreachable!("output_artifact only returns Json or Markdown")
            }
        }
    }
    result
}

impl Cli {
    /// The command's default output-artifact naming for `--format json`, or
    /// (bare command only) `--format markdown` — the two formats that
    /// support `--output` and a default `.judge/` artifact path.
    fn output_artifact(&self) -> Result<Option<(PathBuf, &'static str, OutputFormat)>, CliError> {
        let format_and_name = match &self.command {
            Some(command) => command.json_artifact(),
            None => Some((self.baseline_args.format, "judge")),
        };
        let supports_artifact = matches!(
            (&self.command, format_and_name),
            (_, Some((OutputFormat::Json, _))) | (None, Some((OutputFormat::Markdown, _)))
        );
        if !supports_artifact {
            return self.output.is_none().then_some(None).ok_or_else(|| {
                CliError::Config(
                    "--output requires a command using `--format json`, or the bare `cargo judge --format markdown` run"
                        .to_string(),
                )
            });
        }
        let (format, name) = format_and_name.expect("checked by supports_artifact above");
        let extension = match format {
            OutputFormat::Json => "json",
            OutputFormat::Markdown => "md",
            OutputFormat::Tty | OutputFormat::Sarif => {
                unreachable!("checked by supports_artifact above")
            }
        };
        let path = self
            .output
            .clone()
            .unwrap_or_else(|| PathBuf::from(".judge").join(format!("{name}.{extension}")));
        Ok(Some((path, name, format)))
    }
}

fn write_artifact_file(path: &Path, contents: &[u8]) -> Result<(), CliError> {
    if let Some(parent) = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
    {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(path, contents)?;
    Ok(())
}

/// Adds artifact metadata without moving the command's established JSON
/// payload. Consumers can keep reading existing root fields while people and
/// agents get enough context to decide whether a follow-up is warranted.
fn add_json_header(
    contents: &[u8],
    output_path: &Path,
    command: &str,
    result: &Result<CommandOutcome, CliError>,
) -> Result<Vec<u8>, CliError> {
    let mut document: serde_json::Value = serde_json::from_slice(contents).map_err(|err| {
        CliError::Analyzer(format!("JSON renderer produced an invalid artifact: {err}"))
    })?;
    let Some(root) = document.as_object_mut() else {
        return Err(CliError::Analyzer(
            "JSON renderer produced a non-object artifact".to_string(),
        ));
    };

    let findings = root
        .get("findings")
        .and_then(serde_json::Value::as_array)
        .map_or(0, Vec::len);
    let candidates = ["candidates", "heuristics"]
        .into_iter()
        .filter_map(|key| root.get(key).and_then(serde_json::Value::as_array))
        .map(Vec::len)
        .sum::<usize>();
    let errors = root
        .get("errors")
        .and_then(serde_json::Value::as_array)
        .map_or(0, Vec::len);
    let blocking = matches!(result, Ok(CommandOutcome::FindingsFound));
    let (assessment, action_required, summary) = if errors > 0 {
        (
            "analysis_incomplete",
            true,
            format!("Resolve {errors} analysis error(s) before relying on this artifact."),
        )
    } else if blocking {
        (
            "blocking_findings",
            true,
            "The command returned a failing verdict; action is required.".to_string(),
        )
    } else if findings + candidates > 0 {
        (
            "review_recommended",
            true,
            format!(
                "Review {findings} finding(s) and {candidates} advisory candidate(s); this is not an automatic refactoring instruction."
            ),
        )
    } else {
        (
            "informational",
            false,
            "No findings or advisory candidates require follow-up.".to_string(),
        )
    };

    let generated_at_unix_seconds = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_err(|err| CliError::Analyzer(format!("system clock is before Unix epoch: {err}")))?
        .as_secs();
    let generated_at_utc = format_utc_timestamp(generated_at_unix_seconds)?;
    let analysis_universe = root
        .get("analysis_universe")
        .cloned()
        .unwrap_or(serde_json::Value::Null);

    let header = serde_json::json!({
        "schema_version": 1,
        "generated_at_utc": generated_at_utc,
        "generated_at_unix_seconds": generated_at_unix_seconds,
        "working_directory": std::env::current_dir()?.display().to_string(),
        "output_path": output_path.display().to_string(),
        "command": command,
        "description": json_artifact_description(command),
        "contains": "The command's normal versioned JSON payload plus this artifact header.",
        "context": {
            "analysis_universe": analysis_universe,
            "evidence_contract": {
                "finding_identity": "id, rule, severity, location, and origin are stable references within this artifact.",
                "evidence": "A finding's evidence and limitations state the observed facts and the boundary of the claim.",
                "recommendations": "Next actions are review order only; they are not automatic refactoring instructions.",
            },
            "next_actions": json_next_actions(command, findings, errors),
        },
        "assessment": {
            "kind": assessment,
            "action_required": action_required,
            "blocking": blocking,
            "findings": findings,
            "advisory_candidates": candidates,
            "analysis_errors": errors,
            "summary": summary,
        },
    });
    serde_json::to_vec_pretty(&HeaderFirstJson {
        header: &header,
        payload: root,
    })
    .map_err(|err| CliError::Analyzer(err.to_string()))
}

/// Small, stable navigation hints for humans and agents. These are derived
/// solely from the selected report and never claim a code change is required.
fn json_next_actions(command: &str, findings: usize, errors: usize) -> Vec<serde_json::Value> {
    if errors > 0 {
        return vec![serde_json::json!({
            "id": "resolve-analysis-errors",
            "reason": "The report is incomplete; inspect the errors array before acting on findings."
        })];
    }
    if findings == 0 {
        return Vec::new();
    }
    let mut actions = vec![serde_json::json!({
        "id": "inspect-findings",
        "reason": "Start with each finding's location, evidence, limitations, and causal links."
    })];
    match command {
        "judge" | "compare" => actions.push(serde_json::json!({
            "id": "refactoring-context",
            "command": "cargo judge dupes --format json",
            "reason": "Inspect duplicate families before deciding whether a shared extraction is justified."
        })),
        "deps" => actions.push(serde_json::json!({
            "id": "dependency-context",
            "command": "cargo judge deps --format json",
            "reason": "Use the dependency findings and their source locations to verify actual usage."
        })),
        _ => {}
    }
    actions
}

/// Serializes an artifact's context before its established command payload.
/// JSON object order is semantically irrelevant, but leading with the header
/// makes the file practical to inspect without scrolling past large findings.
struct HeaderFirstJson<'a> {
    header: &'a serde_json::Value,
    payload: &'a serde_json::Map<String, serde_json::Value>,
}

impl Serialize for HeaderFirstJson<'_> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let has_existing_header = self.payload.contains_key("header");
        let mut object = serializer
            .serialize_map(Some(self.payload.len() + usize::from(!has_existing_header)))?;
        object.serialize_entry("header", self.header)?;
        for (key, value) in self.payload {
            if key != "header" {
                object.serialize_entry(key, value)?;
            }
        }
        object.end()
    }
}

fn format_utc_timestamp(unix_seconds: u64) -> Result<String, CliError> {
    let seconds = i64::try_from(unix_seconds)
        .map_err(|_| CliError::Analyzer("system timestamp exceeds supported range".to_string()))?;
    let days = seconds.div_euclid(86_400);
    let seconds_of_day = seconds.rem_euclid(86_400);
    let (year, month, day) = civil_date_from_days(days);
    let hour = seconds_of_day / 3_600;
    let minute = (seconds_of_day % 3_600) / 60;
    let second = seconds_of_day % 60;
    Ok(format!(
        "{year:04}-{month:02}-{day:02}T{hour:02}:{minute:02}:{second:02}Z"
    ))
}

/// Gregorian civil date for a Unix-day offset, using the public-domain
/// civil-date algorithm by Howard Hinnant. Kept dependency-free because the
/// artifact header only needs a stable UTC rendering of `SystemTime`.
fn civil_date_from_days(days_since_unix_epoch: i64) -> (i64, i64, i64) {
    let z = days_since_unix_epoch + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let day_of_era = z - era * 146_097;
    let year_of_era =
        (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let year = year_of_era + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_prime = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
    let month = if month_prime < 10 {
        month_prime + 3
    } else {
        month_prime - 9
    };
    (if month <= 2 { year + 1 } else { year }, month, day)
}

fn json_artifact_description(command: &str) -> &'static str {
    match command {
        "dupes" => "Duplicated token spans grouped into clone families and refactoring priorities.",
        "map" => "Workspace facts and refactoring attention ranked from measured source metrics.",
        "impact" => "Direct analysis and Cargo-target context for one source file.",
        "patterns" => "Advisory Rust pattern candidates aggregated from project evidence.",
        "principles" => "Advisory design-principle heuristics aggregated from project evidence.",
        "judge" => "Combined Judge findings across the enabled default analyzers.",
        _ => "Versioned Judge analysis output for the selected command.",
    }
}

/// Loads `judge.toml`'s `[[boundary]]`/`[[crate_profile]]` config, if
/// present. Both are opt-in — a missing file is the default (empty) config,
/// not an error.
fn load_judge_toml(
    workspace_root: &Path,
) -> Result<judge::rules::boundaries::BoundaryConfig, CliError> {
    let config_path = workspace_root.join("judge.toml");
    if !config_path.exists() {
        return Ok(judge::rules::boundaries::BoundaryConfig::default());
    }
    parse_boundary_config(&config_path)
}

/// Reads and parses `judge.toml` at `config_path` into a
/// [`judge::rules::boundaries::BoundaryConfig`] — the "read, then parse, name the
/// file in either error" recipe shared by `load_judge_toml`'s
/// default-on-missing wrapper above, `run_boundaries`'s `--config`-overridable
/// path (see `analysis_commands::run_boundaries`), and the bare combined
/// run's own existence check (see `combined_analysis::collect_boundaries`) —
/// all three only differ in how they handle a missing file, not in how they
/// read one that exists.
fn parse_boundary_config(
    config_path: &Path,
) -> Result<judge::rules::boundaries::BoundaryConfig, CliError> {
    let config_text = std::fs::read_to_string(config_path)
        .map_err(|err| CliError::Config(format!("{}: {err}", config_path.display())))?;
    toml::from_str(&config_text).map_err(|err| {
        CliError::Config(format!("{}: failed to parse: {err}", config_path.display()))
    })
}

/// Treats an unavailable score as the analyzer error it is — exit 2,
/// matching `IngestError`/`GitError`/`BaselineError` (see todo.md §15.1).
fn require_score(
    outcome: judge::health_score::ScoreOutcome,
) -> Result<judge::health_score::HealthScore, CliError> {
    match outcome {
        judge::health_score::ScoreOutcome::Available(score) => Ok(score),
        judge::health_score::ScoreOutcome::Unavailable(reason) => Err(CliError::Analyzer(format!(
            "health score unavailable: {reason}"
        ))),
    }
}

/// Computes the current health score and its trend against the baseline at
/// `baseline_path` (see todo.md §4 point 4, "Trend vor Absolutwert").
fn compute_score_trend(
    workspace: &judge::ingest::Workspace,
    findings: &[Finding],
    total_loc: usize,
    baseline_path: &Path,
) -> Result<judge::health_score::Trend, CliError> {
    let baseline = judge::baseline::load(baseline_path)?;
    let config = load_judge_toml(&workspace.root)?;
    let current = require_score(judge::health_score::compute(
        findings,
        total_loc,
        workspace,
        &config.crate_profiles,
    ))?;
    Ok(judge::health_score::trend(
        current,
        &baseline,
        workspace,
        &config.crate_profiles,
    ))
}

/// Writes the current health score alongside the score a saved baseline
/// represents — or the explicit reason the two aren't directly comparable
/// (see todo.md §15.1), instead of a delta across different formulas.
fn print_score_trend(
    out: &mut dyn Write,
    trend: &judge::health_score::Trend,
) -> std::io::Result<()> {
    match trend {
        judge::health_score::Trend::Comparable {
            current,
            baseline_score,
            baseline_grade,
        } => writeln!(
            out,
            "health score: {:.1} ({}) — {:+.1} since baseline ({:.1} {})",
            current.score,
            current.grade.label(),
            current.score - baseline_score,
            baseline_score,
            baseline_grade.label(),
        ),
        judge::health_score::Trend::NotComparable { current, reason } => writeln!(
            out,
            "health score: {:.1} ({}) — baseline not directly comparable: {reason}",
            current.score,
            current.grade.label(),
        ),
    }
}

/// The `trend` JSON shape: `comparable` plus either the baseline score and
/// delta, or the explicit reason no delta can be computed.
fn trend_json(trend: &judge::health_score::Trend) -> serde_json::Value {
    match trend {
        judge::health_score::Trend::Comparable {
            current,
            baseline_score,
            baseline_grade,
        } => serde_json::json!({
            "comparable": true,
            "baseline_score": baseline_score,
            "baseline_grade": baseline_grade,
            "delta": current.score - baseline_score,
        }),
        judge::health_score::Trend::NotComparable { reason, .. } => serde_json::json!({
            "comparable": false,
            "reason": reason.code(),
            "message": reason.to_string(),
        }),
    }
}

/// AI-slop signals (see todo.md §G "AI-Slop-Signale", §12 "Entscheidungen":
/// "Der Slop-Block ist Teil von `health`, kein eigener Sub-Command"). Grouped
/// by rule with a per-rule count, then listed root-findings-first unless
/// `show_cascades` is set (see todo.md §14.2 P0#2), same convention as
/// `print_hotspots`.
const SLOP_RULES: [&str; 25] = [
    judge::rules::slop::SWALLOWED_RESULT_RULE,
    judge::rules::slop::EMPTY_ERROR_ARM_RULE,
    judge::rules::slop::CATCH_ALL_ERROR_RULE,
    judge::rules::slop::SUPPRESSION_DEBT_RULE,
    judge::rules::slop::MERGED_STUB_RULE,
    judge::rules::slop::EMPTY_IMPL_RULE,
    judge::rules::slop::ASSERTION_FREE_TEST_RULE,
    judge::rules::slop::TAUTOLOGICAL_TEST_RULE,
    judge::rules::slop::IGNORED_TEST_ACCUMULATION_RULE,
    judge::rules::slop::CONVERSATIONAL_ARTIFACT_RULE,
    judge::rules::slop::RESTATING_COMMENT_RULE,
    judge::rules::slop::STEP_COMMENT_INFLATION_RULE,
    judge::rules::slop::GENERIC_NAMING_RULE,
    judge::rules::slop::DOC_RESTATES_SIGNATURE_RULE,
    judge::rules::slop_structural::CHURN_HOTSPOT_RULE,
    judge::rules::slop_structural::COMPLEXITY_INFLATION_RULE,
    judge::rules::complexity::SIGNATURE_COMPLEXITY_RULE,
    judge::rules::complexity::MAINTAINABILITY_INDEX_RULE,
    judge::rules::slop_structural::ABSTRACTION_INFLATION_RULE,
    judge::rules::slop_structural::FRAGILE_SUBSTRING_CLASSIFICATION_RULE,
    judge::rules::security::UNSAFE_SURFACE_RULE,
    judge::rules::security::UNSAFE_DENSITY_RULE,
    judge::rules::security::INTEGER_CAST_RISK_RULE,
    judge::rules::security::PANIC_IN_LIB_RULE,
    judge::rules::security::HARDCODED_SECRET_RULE,
];

fn print_slop(
    out: &mut dyn Write,
    findings: &[judge::finding::Finding],
    show_cascades: bool,
) -> std::io::Result<()> {
    let shown: Vec<&judge::finding::Finding> = if show_cascades {
        findings
            .iter()
            .filter(|finding| SLOP_RULES.contains(&finding.rule.as_str()))
            .collect()
    } else {
        judge::finding::root_findings(findings)
            .into_iter()
            .filter(|finding| SLOP_RULES.contains(&finding.rule.as_str()))
            .collect()
    };

    if shown.is_empty() {
        writeln!(out, "slop signals: none")?;
        return Ok(());
    }

    let (gating, advisory) = judge::baseline::partition_gating(shown.iter().copied());
    writeln!(
        out,
        "slop signals: {} ({} advisory)",
        gating.len(),
        advisory.len()
    )?;
    for rule in SLOP_RULES {
        let count = shown.iter().filter(|finding| finding.rule == rule).count();
        if count > 0 {
            writeln!(out, "  {rule}: {count}")?;
        }
    }
    writeln!(out)?;
    for finding in &gating {
        write_slop_finding(out, finding)?;
    }
    if !advisory.is_empty() {
        writeln!(out)?;
        writeln!(
            out,
            "advisory (heuristic) — no verdict effect: {}",
            advisory.len()
        )?;
        for finding in &advisory {
            write_slop_finding(out, finding)?;
        }
    }
    Ok(())
}

/// One finding line of the slop block in the `health` TTY report.
fn write_slop_finding(out: &mut dyn Write, finding: &Finding) -> std::io::Result<()> {
    writeln!(
        out,
        "  [{}] {:<20} {}:{}  {}",
        severity_label(finding.severity),
        finding.rule,
        finding.location.file.display(),
        finding.location.line,
        finding.location.item_path
    )
}

fn severity_label(severity: judge::finding::Severity) -> &'static str {
    match severity {
        judge::finding::Severity::Fail => "fail",
        judge::finding::Severity::Warn => "warn",
        judge::finding::Severity::Info => "info",
    }
}

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

    /// A temp directory unique to one test, removed when it goes out of
    /// scope. Duplicated from `judge::test_util::TempDir` rather than
    /// reused, since that module is private to the `judge` library crate's
    /// own test builds and isn't reachable from this binary crate's tests
    /// (mirrors how `git.rs`'s tests build their own `git()` fixture helper
    /// rather than shelling out to the production code path).
    struct TempDir(PathBuf);

    impl TempDir {
        fn new(name: &str) -> Self {
            static COUNTER: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
            let id = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            let path = std::env::temp_dir().join(format!(
                "judge-main-test-{name}-{}-{id}",
                std::process::id()
            ));
            std::fs::create_dir_all(&path).expect("failed to create temp dir");
            Self(path)
        }
    }

    impl std::ops::Deref for TempDir {
        type Target = Path;

        fn deref(&self) -> &Path {
            &self.0
        }
    }

    impl Drop for TempDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    /// Runs `git` in `dir` with a fixed test identity — fixture setup only,
    /// never the production code path (see `git.rs`'s own tests).
    fn git(dir: &Path, args: &[&str]) {
        let status = std::process::Command::new("git")
            .args([
                "-c",
                "user.name=judge-test",
                "-c",
                "user.email=test@example.com",
                "-c",
                "commit.gpgsign=false",
            ])
            .args(args)
            .current_dir(dir)
            .status()
            .expect("failed to run git — required for these fixtures");
        assert!(status.success(), "git {args:?} failed");
    }

    /// The `judge-ignore` marker text, assembled at runtime rather than
    /// written as one literal in this file — a fixture string containing it
    /// verbatim (especially the deliberately-malformed, missing-reason
    /// cases below) would itself read as a real directive when `judge`
    /// analyzes its own `main.rs`, wherever a `duplicate-code`/
    /// `churn-hotspot`/etc. finding happens to land on or next to that line.
    fn ignore_marker() -> String {
        ["judge", "-ignore:"].concat()
    }

    fn commit_sha(dir: &Path, rev: &str) -> String {
        let output = std::process::Command::new("git")
            .args(["rev-parse", rev])
            .current_dir(dir)
            .output()
            .expect("failed to run git rev-parse");
        assert!(output.status.success());
        String::from_utf8(output.stdout).unwrap().trim().to_string()
    }

    fn write_fixture_crate(dir: &Path) {
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(dir.join("src/lib.rs"), "pub fn hello() {}\n").unwrap();
    }

    /// `cargo judge api-surface` end-to-end: a `pub fn` with a `///` doc
    /// comment produces no finding.
    #[test]
    fn run_api_surface_reports_clean_on_a_documented_fixture() {
        let dir = TempDir::new("api-surface-clean");
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(
            dir.join("src/lib.rs"),
            "/// Says hello.\npub fn hello() {}\n",
        )
        .unwrap();

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            api_surface_cli(OutputFormat::Tty, false, None),
            &mut out,
        )
        .expect("clean fixture must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("undocumented public items: 0"),
            "unexpected output: {text}"
        );
    }

    /// `cargo judge api-surface` end-to-end: an undocumented `pub fn`
    /// produces one `undocumented-public-item` finding, listed in the TTY
    /// output.
    #[test]
    fn run_api_surface_reports_undocumented_public_items() {
        let dir = TempDir::new("api-surface-findings");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            api_surface_cli(OutputFormat::Tty, false, None),
            &mut out,
        )
        .expect("fixture must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("undocumented public items: 1"),
            "unexpected output: {text}"
        );
        assert!(text.contains("hello"), "unexpected output: {text}");
    }

    /// (c) `--save-baseline` records each crate's api-surface-size count; a
    /// later run with 2 more `pub fn`s shows the delta against it (see
    /// todo.md §I "API-Surface-Größe pro Crate, Trend gegen Baseline").
    #[test]
    fn api_surface_baseline_shows_a_size_delta() {
        let dir = TempDir::new("api-surface-baseline-delta");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            api_surface_cli(OutputFormat::Tty, true, None),
            &mut out,
        )
        .expect("saving a baseline must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("api surface: fixture 1 items"),
            "unexpected output: {text}"
        );

        std::fs::write(
            dir.join("src/lib.rs"),
            "pub fn hello() {}\n\npub fn a() {}\n\npub fn b() {}\n",
        )
        .unwrap();
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "add two more pub fns"]);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            api_surface_cli(
                OutputFormat::Tty,
                false,
                Some(PathBuf::from(DEFAULT_BASELINE_API_SURFACE)),
            ),
            &mut out,
        )
        .expect("comparing against the baseline must not error");
        // The two new items are `undocumented-public-item` findings, but
        // that rule is `Severity::Info` — informational findings never fail
        // the verdict (see `judge::baseline::Delta::verdict`).
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("api surface: fixture 3 items (\u{394}+2 vs baseline)"),
            "unexpected output: {text}"
        );
    }

    /// (d) A baseline saved before `api_surface_size` existed — or by some
    /// other command's `--save-baseline` — still loads; the trend line says
    /// "not comparable" instead of crashing or showing a false delta (see
    /// todo.md §I, and `judge::baseline`'s own
    /// `baseline_without_api_surface_size_still_loads` unit test for the same
    /// backward-compatibility rule at the schema level).
    #[test]
    fn api_surface_baseline_without_size_field_is_not_comparable() {
        let dir = TempDir::new("api-surface-baseline-old-schema");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);
        let commit = commit_sha(&dir, "HEAD");

        let baseline_path = dir.join("old-baseline.json");
        std::fs::write(
            &baseline_path,
            format!(
                r#"{{
                    "schema_version": 2,
                    "judge_version": "0.1.0",
                    "commit": "{commit}",
                    "rule_revisions": {{}},
                    "total_loc": 1,
                    "findings": []
                }}"#
            ),
        )
        .unwrap();

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            api_surface_cli(OutputFormat::Tty, false, Some(baseline_path)),
            &mut out,
        )
        .expect("comparing against an old-schema baseline must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("api surface: fixture 1 items (not comparable to baseline)"),
            "unexpected output: {text}"
        );
    }

    /// A fixture crate with two `catch-all-error` boundary functions and a
    /// crate-local typed error — corroborated evidence for exactly one
    /// `stringly-error-boundary` pattern candidate (see `judge::rules::pattern`).
    fn write_pattern_candidate_fixture_crate(dir: &Path) {
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(
            dir.join("src/lib.rs"),
            "pub fn a() -> Result<(), Box<dyn std::error::Error>> {\n\
             \x20   std::fs::read_to_string(\"x\").map_err(|_| \"failed\".into())?;\n\
             \x20   Ok(())\n\
             }\n\
             pub fn b() -> Result<(), Box<dyn std::error::Error>> {\n\
             \x20   std::fs::read_to_string(\"y\").map_err(|_| \"failed\".into())?;\n\
             \x20   Ok(())\n\
             }\n\
             enum FixtureError { Bad }\n",
        )
        .unwrap();
    }

    /// A fixture crate combining fixtures for all five §16.3 MVP pattern
    /// rules at once: `stringly-error-boundary` (two `catch-all-error`
    /// boundary functions plus a crate-local typed error),
    /// `primitive-domain-value` (two `pub fn` signatures sharing a
    /// `threshold: u32` parameter, one of them guarded),
    /// `boolean-state-cluster` (a function with three bool parameters, two of
    /// which are combined in one condition), `public-invariant-bypass` (a
    /// `pub struct` with two `pub` fields and a constructor jointly
    /// validating both), and `manual-resource-lifecycle` (a function calling
    /// both an acquire- and a release-shaped operation, with no `impl Drop`
    /// anywhere in the crate) — corroborated evidence for five candidates of
    /// different patterns at once.
    fn write_multi_rule_pattern_candidate_fixture_crate(dir: &Path) {
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(
            dir.join("src/lib.rs"),
            "pub fn a() -> Result<(), Box<dyn std::error::Error>> {\n\
             \x20   std::fs::read_to_string(\"x\").map_err(|_| \"failed\".into())?;\n\
             \x20   Ok(())\n\
             }\n\
             pub fn b() -> Result<(), Box<dyn std::error::Error>> {\n\
             \x20   std::fs::read_to_string(\"y\").map_err(|_| \"failed\".into())?;\n\
             \x20   Ok(())\n\
             }\n\
             enum FixtureError { Bad }\n\
             pub fn set_a(threshold: u32) {}\n\
             pub fn set_b(threshold: u32) -> Result<(), String> {\n\
             \x20   if threshold > 100 {\n\
             \x20       return Err(\"too big\".to_string());\n\
             \x20   }\n\
             \x20   Ok(())\n\
             }\n\
             pub fn configure(verbose: bool, strict: bool, dry_run: bool) {\n\
             \x20   if verbose && strict {\n\
             \x20       do_thing();\n\
             \x20   }\n\
             \x20   let _ = dry_run;\n\
             }\n\
             fn do_thing() {}\n\
             pub struct Range {\n\
             \x20   pub low: u32,\n\
             \x20   pub high: u32,\n\
             }\n\
             impl Range {\n\
             \x20   pub fn new(low: u32, high: u32) -> Result<Self, String> {\n\
             \x20       if low >= high {\n\
             \x20           return Err(\"low must be less than high\".to_string());\n\
             \x20       }\n\
             \x20       Ok(Self { low, high })\n\
             \x20   }\n\
             }\n\
             pub fn manage(handle: u32) {\n\
             \x20   connect();\n\
             \x20   let _ = handle;\n\
             \x20   disconnect();\n\
             }\n\
             fn connect() {}\n\
             fn disconnect() {}\n",
        )
        .unwrap();
    }

    /// A fixture crate with one function satisfying both
    /// `functional-core-imperative-shell` signals: an `std::fs::read_to_string`
    /// call plus nine sequential `if`s (cyclomatic complexity 10, at
    /// [`judge::rules::principle::FUNCTIONAL_CORE_COMPLEXITY_THRESHOLD`]).
    fn write_principle_heuristic_fixture_crate(dir: &Path) {
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(
            dir.join("src/lib.rs"),
            "pub fn read_and_branch(path: &str) -> i32 {\n\
             \x20   let contents = std::fs::read_to_string(path).unwrap();\n\
             \x20   let mut total = contents.len() as i32;\n\
             \x20   if total > 0 { total += 1; }\n\
             \x20   if total > 1 { total += 1; }\n\
             \x20   if total > 2 { total += 1; }\n\
             \x20   if total > 3 { total += 1; }\n\
             \x20   if total > 4 { total += 1; }\n\
             \x20   if total > 5 { total += 1; }\n\
             \x20   if total > 6 { total += 1; }\n\
             \x20   if total > 7 { total += 1; }\n\
             \x20   if total > 8 { total += 1; }\n\
             \x20   total\n\
             }\n",
        )
        .unwrap();
    }

    /// A pair of duplicated function bodies (well over
    /// `judge::rules::duplication::DEFAULT_MIN_TOKENS`), both in one new file — a
    /// self-contained `code_introduced` duplication finding once that file
    /// is committed.
    const DUPE_FILE_CONTENT: &str = r#"
fn dup_one(x: i32) -> i32 {
    let mut total = 0;
    for i in 0..x {
        total += i;
    }
    total
}

fn dup_two(x: i32) -> i32 {
    let mut total = 0;
    for i in 0..x {
        total += i;
    }
    total
}
"#;

    /// Serializes every test that depends on the process working directory:
    /// [`run_in_dir`] points it at a fixture workspace (because
    /// `judge::ingest::load(None)` resolves the manifest from the current
    /// directory), and any test spawning `cargo metadata` — even with an
    /// explicit manifest path — needs it to *exist* while the child process
    /// starts. Both are process-global concerns, so they must not interleave.
    static CWD_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn lock_cwd() -> std::sync::MutexGuard<'static, ()> {
        CWD_LOCK
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    fn run_in_dir(dir: &Path, cli: Cli, out: &mut dyn Write) -> Result<CommandOutcome, CliError> {
        let _guard = lock_cwd();
        run_in_dir_locked(dir, cli, out)
    }

    /// [`run_in_dir`] for tests that already hold the [`CWD_LOCK`] guard —
    /// e.g. because their fixture setup itself spawns `cargo metadata` and
    /// must not interleave with another test's cwd change.
    fn run_in_dir_locked(
        dir: &Path,
        cli: Cli,
        out: &mut dyn Write,
    ) -> Result<CommandOutcome, CliError> {
        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(dir).unwrap();
        let result = run(cli, out);
        std::env::set_current_dir(original).unwrap();
        result
    }

    fn run_json_in_dir(
        dir: &Path,
        cli: Cli,
        out: &mut dyn Write,
    ) -> Result<CommandOutcome, CliError> {
        let _guard = lock_cwd();
        let original = std::env::current_dir().unwrap();
        std::env::set_current_dir(dir).unwrap();
        let result = run_with_artifact_output(cli, out);
        std::env::set_current_dir(original).unwrap();
        result
    }

    fn cli_with(command: Command) -> Cli {
        Cli {
            command: Some(command),
            baseline_args: baseline_args(OutputFormat::Tty, false, None),
            progress: None,
            details: false,
            color: ColorChoice::Auto,
            output: None,
        }
    }

    fn baseline_args(
        format: OutputFormat,
        save_baseline: bool,
        baseline: Option<PathBuf>,
    ) -> BaselineArgs {
        BaselineArgs {
            format,
            save_baseline,
            baseline,
        }
    }

    fn dupes_cli(format: OutputFormat, save_baseline: bool, baseline: Option<PathBuf>) -> Cli {
        cli_with(Command::Dupes(DupesOptions {
            mode: DupeModeArg::Mild,
            min_tokens: judge::rules::duplication::DEFAULT_MIN_TOKENS,
            baseline_args: baseline_args(format, save_baseline, baseline),
            include_generated: false,
            include_tests: false,
        }))
    }

    fn api_surface_cli(
        format: OutputFormat,
        save_baseline: bool,
        baseline: Option<PathBuf>,
    ) -> Cli {
        cli_with(Command::ApiSurface(ApiSurfaceOptions {
            baseline_args: baseline_args(format, save_baseline, baseline),
            include_generated: false,
        }))
    }

    /// Bare `cargo judge` (no subcommand — `Cli::command` is `None`).
    fn all_cli(save_baseline: bool, baseline: Option<PathBuf>) -> Cli {
        Cli {
            command: None,
            baseline_args: baseline_args(OutputFormat::Tty, save_baseline, baseline),
            progress: None,
            details: false,
            color: ColorChoice::Auto,
            output: None,
        }
    }

    fn all_cli_markdown() -> Cli {
        Cli {
            baseline_args: baseline_args(OutputFormat::Markdown, false, None),
            ..all_cli(false, None)
        }
    }

    #[test]
    fn compare_works_from_an_artifact_in_a_workspace_without_git() {
        let dir = TempDir::new("compare-without-git");
        write_fixture_crate(&dir);

        let mut save_out = Vec::new();
        run_in_dir(&dir, all_cli(true, None), &mut save_out)
            .expect("saving a baseline must not require Git");
        let baseline_path = dir.join(DEFAULT_BASELINE_ALL);
        let baseline: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&baseline_path).unwrap()).unwrap();
        assert!(baseline.get("commit").is_none(), "baseline: {baseline}");

        let mut compare_out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::Compare(CompareOptions {
                baseline: baseline_path,
                format: OutputFormat::Json,
            })),
            &mut compare_out,
        )
        .expect("comparison must not require Git");
        assert_eq!(outcome, CommandOutcome::Clean);
        let compare: serde_json::Value = serde_json::from_slice(&compare_out).unwrap();
        assert_eq!(compare["delta"]["introduced"], serde_json::json!([]));
        assert_eq!(compare["delta"]["resolved"], serde_json::json!([]));

        let mut structure_out = Vec::new();
        run_in_dir(
            &dir,
            cli_with(Command::Structure(StructureOptions {
                format: OutputFormat::Json,
            })),
            &mut structure_out,
        )
        .expect("structure must not require Git");
        assert!(
            serde_json::from_slice::<serde_json::Value>(&structure_out).unwrap()["crates"]
                .is_array()
        );

        let mut refactor_out = Vec::new();
        run_in_dir(
            &dir,
            cli_with(Command::Refactor(RefactorOptions {
                target: None,
                format: OutputFormat::Json,
            })),
            &mut refactor_out,
        )
        .expect("refactor must not require Git");
        let refactor: serde_json::Value = serde_json::from_slice(&refactor_out).unwrap();
        assert!(refactor["candidates"].is_array());
        assert!(refactor["findings"].is_array());
    }

    #[test]
    fn flattened_baseline_arguments_remain_available_to_root_and_subcommands() {
        let root = Cli::try_parse_from(["judge", "--format", "json", "--save-baseline"])
            .expect("root baseline arguments must parse");
        assert!(root.command.is_none());
        assert!(matches!(root.baseline_args.format, OutputFormat::Json));
        assert!(root.baseline_args.save_baseline);

        let cli = Cli::try_parse_from([
            "judge",
            "dupes",
            "--format",
            "sarif",
            "--baseline",
            "saved.json",
        ])
        .expect("subcommand baseline arguments must parse");
        let Some(Command::Dupes(options)) = cli.command else {
            panic!("expected dupes command");
        };
        assert!(matches!(options.baseline_args.format, OutputFormat::Sarif));
        assert_eq!(
            options.baseline_args.baseline,
            Some(PathBuf::from("saved.json"))
        );

        let root_details =
            Cli::try_parse_from(["judge", "--details"]).expect("combined details flag must parse");
        assert!(root_details.details);
    }

    #[test]
    fn combined_tty_defaults_to_a_grouped_summary_and_keeps_details_available() {
        let dir = TempDir::new("combined-tty-summary");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        std::fs::write(dir.join("src/lib.rs"), DUPE_FILE_CONTENT).unwrap();
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);

        let mut summary_out = Vec::new();
        run_in_dir(&dir, all_cli(false, None), &mut summary_out)
            .expect("combined summary must run");
        let summary = String::from_utf8(summary_out).unwrap();
        assert!(
            summary.starts_with("[WARN] Judge summary\n"),
            "unexpected output: {summary}"
        );
        assert!(
            summary.contains("duplicate-code"),
            "unexpected output: {summary}"
        );
        assert!(
            summary.contains("Next steps"),
            "unexpected output: {summary}"
        );
        assert!(
            !summary.contains("[warn] duplicate-code"),
            "the default must not print every clone member: {summary}"
        );

        let mut detailed_cli = all_cli(false, None);
        detailed_cli.details = true;
        let mut details_out = Vec::new();
        run_in_dir(&dir, detailed_cli, &mut details_out).expect("detailed report must run");
        let details = String::from_utf8(details_out).unwrap();
        assert!(
            details.contains("duplicate-code in dup_one"),
            "details must retain the exhaustive list: {details}"
        );
        assert!(
            details.contains("src/lib.rs:2"),
            "details must use workspace-relative paths: {details}"
        );
    }

    /// Success path: a clean fixture workspace runs through `run` to
    /// `CommandOutcome::Clean`, with the TTY report in the writer.
    #[test]
    fn run_reports_clean_on_a_fixture_without_findings() {
        let dir = TempDir::new("run-clean");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(&dir, dupes_cli(OutputFormat::Tty, false, None), &mut out)
            .expect("clean fixture must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("clone families: 0"),
            "unexpected output: {text}"
        );
    }

    #[test]
    fn dupes_json_includes_a_compact_refactoring_summary() {
        let dir = TempDir::new("dupes-refactoring-summary");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        run_in_dir(&dir, dupes_cli(OutputFormat::Json, false, None), &mut out)
            .expect("dupes must run");
        let json: serde_json::Value = serde_json::from_slice(&out).expect("dupes JSON");

        assert_eq!(json["refactoring_summary"]["schema_version"], 1);
        assert_eq!(json["refactoring_summary"]["clone_families"], 0);
        assert_eq!(json["refactoring_summary"]["clone_members"], 0);
        assert_eq!(
            json["refactoring_summary"]["top_families"],
            serde_json::json!([])
        );
    }

    /// GitHub issue #7: with more than [`DUPE_FAMILY_TTY_LIMIT`] clone
    /// families, the TTY view must say so explicitly instead of silently
    /// stopping after the cap — the header count alone isn't enough,
    /// grepping the (apparently complete) family list for a touched file
    /// and finding nothing must not read as "no duplication".
    #[test]
    fn run_dupes_tty_reports_a_trailer_when_families_exceed_the_cap() {
        let dir = TempDir::new("dupes-truncation-trailer");
        write_n_duplicate_clone_families(&dir, 16);

        let mut out = Vec::new();
        let outcome = run_in_dir(&dir, dupes_cli(OutputFormat::Tty, false, None), &mut out)
            .expect("fixture must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("clone families: 16"),
            "unexpected output: {text}"
        );
        assert!(
            text.contains("... and 1 more families (see --format json for the full list)"),
            "missing truncation trailer: {text}"
        );
    }

    /// The TTY trailer above promises "the full list" in `--format json` —
    /// `refactoring_summary.top_families` must actually carry every family in
    /// that format, not just the TTY's own top-5 ranked summary slice.
    #[test]
    fn dupes_json_refactoring_summary_is_not_truncated_to_the_tty_summary_cap() {
        let dir = TempDir::new("dupes-json-full-family-list");
        write_n_duplicate_clone_families(&dir, 16);

        let mut out = Vec::new();
        run_in_dir(&dir, dupes_cli(OutputFormat::Json, false, None), &mut out)
            .expect("dupes must run");
        let json: serde_json::Value = serde_json::from_slice(&out).expect("dupes JSON");

        assert_eq!(json["refactoring_summary"]["clone_families"], 16);
        assert_eq!(
            json["refactoring_summary"]["top_families"]
                .as_array()
                .expect("top_families array")
                .len(),
            16,
            "JSON top_families must list every family, not just the TTY's top 5"
        );
    }

    /// `count` distinct clone families (each a duplicated pair) — every pair
    /// has unique literals so Mild mode doesn't merge them into one family,
    /// and each body is well over `DEFAULT_MIN_TOKENS` (20).
    fn write_n_duplicate_clone_families(dir: &Path, count: usize) {
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        let mut source = String::new();
        for i in 0..count {
            for suffix in ["a", "b"] {
                source.push_str(&format!(
                    "pub fn dup_{i}_{suffix}() -> i32 {{ let v0 = {i}; let v1 = {i}; \
                     let v2 = {i}; let v3 = {i}; let v4 = {i}; let v5 = {i}; \
                     let v6 = {i}; v0 + v1 + v2 + v3 + v4 + v5 + v6 }}\n"
                ));
            }
        }
        std::fs::write(dir.join("src/lib.rs"), source).unwrap();
    }

    /// Findings path: a failing baseline-compare verdict becomes
    /// `CommandOutcome::FindingsFound` (exit 1), never an error.
    #[test]
    fn run_maps_a_failing_baseline_verdict_to_findings_found() {
        let dir = TempDir::new("run-findings-found");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);

        let mut out = Vec::new();
        let outcome = run_in_dir(&dir, dupes_cli(OutputFormat::Tty, true, None), &mut out)
            .expect("saving a baseline must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("baseline saved:"),
            "unexpected output: {text}"
        );

        std::fs::write(dir.join("src/dupe.rs"), DUPE_FILE_CONTENT).unwrap();
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "add duplicated code"]);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            dupes_cli(
                OutputFormat::Tty,
                false,
                Some(PathBuf::from(DEFAULT_BASELINE_DUPES)),
            ),
            &mut out,
        )
        .expect("a failing verdict is an outcome, not an error");
        assert_eq!(outcome, CommandOutcome::FindingsFound);
        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("verdict: fail"), "unexpected output: {text}");
    }

    /// (f) End-to-end (todo.md §5): a `// judge-ignore: <rule> — <reason>`
    /// comment on an otherwise-`Fail`-triggering finding (`swallowed-result`
    /// — `slop.rs`'s own tests cover that it fires without suppression)
    /// removes it before the baseline diff, so the verdict is
    /// `CommandOutcome::Clean`/`pass` instead of `FindingsFound`/`fail`.
    #[test]
    fn judge_ignore_suppresses_a_finding_so_it_does_not_fail_the_verdict() {
        let dir = TempDir::new("judge-ignore-verdict");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);

        let mut out = Vec::new();
        let outcome = run_in_dir(&dir, all_cli(true, None), &mut out)
            .expect("saving a baseline must not error");
        assert_eq!(outcome, CommandOutcome::Clean);

        std::fs::write(
            dir.join("src/risky.rs"),
            format!(
                "pub fn call_it() {{\n    let _ = std::fs::remove_file(\"x\"); // {} swallowed-result — best-effort cleanup\n}}\n",
                ignore_marker()
            ),
        )
        .unwrap();
        git(&dir, &["add", "."]);
        git(
            &dir,
            &["commit", "-q", "-m", "add suppressed swallowed-result"],
        );

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            all_cli(false, Some(PathBuf::from(DEFAULT_BASELINE_ALL))),
            &mut out,
        )
        .expect("a suppressed finding must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("verdict: pass"), "unexpected output: {text}");
    }

    /// (d) End-to-end (todo.md §5): a `judge-ignore` comment with no reason
    /// is a hard config error (exit 2), analogous to `judge-dupe-off`
    /// without a reason (`duplication::tests::judge_dupe_off_without_a_reason_is_a_hard_error`).
    #[test]
    fn judge_ignore_without_a_reason_is_a_config_error_end_to_end() {
        let dir = TempDir::new("judge-ignore-missing-reason");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        std::fs::write(
            dir.join("src/risky.rs"),
            format!(
                "pub fn call_it() {{\n    let _ = std::fs::remove_file(\"x\"); // {} swallowed-result\n}}\n",
                ignore_marker()
            ),
        )
        .unwrap();
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);

        let mut out = Vec::new();
        let err = run_in_dir(&dir, all_cli(false, None), &mut out)
            .expect_err("a judge-ignore comment with no reason must be a config error");
        match err {
            CliError::Config(message) => {
                assert!(message.contains("requires a reason"), "message: {message}");
            }
            other => panic!("expected CliError::Config, got {other:?}"),
        }
    }

    /// Config-error path: an unparseable `judge.toml` is a `CliError::Config`
    /// (exit 2), not a panic or a silent pass.
    #[test]
    fn run_reports_a_broken_judge_toml_as_a_config_error() {
        let dir = TempDir::new("run-broken-config");
        write_fixture_crate(&dir);
        std::fs::write(dir.join("judge.toml"), "this is { not toml").unwrap();

        let mut out = Vec::new();
        let err = run_in_dir(
            &dir,
            cli_with(Command::Boundaries(BoundariesOptions {
                config: None,
                baseline_args: baseline_args(OutputFormat::Tty, false, None),
                graph: None,
            })),
            &mut out,
        )
        .expect_err("a broken judge.toml must be an error");
        match err {
            CliError::Config(message) => {
                assert!(message.contains("failed to parse"), "message: {message}");
            }
            other => panic!("expected CliError::Config, got {other:?}"),
        }
    }

    /// `cargo judge boundaries --graph dot` is a pure projection of the
    /// crate graph (todo.md §H) — it must work without a `judge.toml`
    /// (boundaries proper are opt-in and require one; the graph does not),
    /// and must not touch findings/baseline machinery at all.
    #[test]
    fn run_boundaries_graph_dot_renders_the_crate_graph_without_a_judge_toml() {
        let dir = TempDir::new("boundaries-graph-dot");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::Boundaries(BoundariesOptions {
                config: None,
                baseline_args: baseline_args(OutputFormat::Tty, false, None),
                graph: Some(GraphFormat::Dot),
            })),
            &mut out,
        )
        .expect("graph projection must not require judge.toml");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert_eq!(text, "digraph crates {\n  \"fixture\";\n}\n");
    }

    /// Same fixture, Mermaid format — todo.md §H names both `dot` and
    /// `mermaid` as the two graph output formats.
    #[test]
    fn run_boundaries_graph_mermaid_renders_the_crate_graph() {
        let dir = TempDir::new("boundaries-graph-mermaid");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        run_in_dir(
            &dir,
            cli_with(Command::Boundaries(BoundariesOptions {
                config: None,
                baseline_args: baseline_args(OutputFormat::Tty, false, None),
                graph: Some(GraphFormat::Mermaid),
            })),
            &mut out,
        )
        .expect("graph projection must not require judge.toml");
        let text = String::from_utf8(out).unwrap();
        assert_eq!(text, "flowchart TD\n  fixture[\"fixture\"]\n");
    }

    /// Config-error path: a baseline with an unknown `schema_version` is a
    /// `CliError::Config` (exit 2) with the library's guidance message.
    #[test]
    fn run_reports_an_unsupported_baseline_schema_version_as_a_config_error() {
        let dir = TempDir::new("run-baseline-schema");
        write_fixture_crate(&dir);
        let baseline_path = dir.join("baseline.json");
        std::fs::write(&baseline_path, r#"{"schema_version": 999}"#).unwrap();

        let mut out = Vec::new();
        let err = run_in_dir(
            &dir,
            dupes_cli(OutputFormat::Tty, false, Some(baseline_path)),
            &mut out,
        )
        .expect_err("an unsupported baseline schema version must be an error");
        match err {
            CliError::Config(message) => {
                assert!(
                    message.contains("unsupported baseline schema_version 999"),
                    "message: {message}"
                );
            }
            other => panic!("expected CliError::Config, got {other:?}"),
        }
    }

    /// Analyzer-error path: no workspace at all is a `CliError::Analyzer`
    /// (exit 2) — `cargo metadata` cannot run.
    #[test]
    fn run_reports_a_missing_workspace_as_an_analyzer_error() {
        let dir = TempDir::new("run-no-workspace");

        let mut out = Vec::new();
        let err = run_in_dir(&dir, dupes_cli(OutputFormat::Tty, false, None), &mut out)
            .expect_err("a directory without a Cargo.toml must be an error");
        assert!(
            matches!(err, CliError::Analyzer(_)),
            "expected CliError::Analyzer, got {err:?}"
        );
    }

    /// JSON rendering goes to the writer handed to `run`, not to a global
    /// stream — the report envelope must parse from the captured bytes.
    #[test]
    fn run_writes_the_json_report_into_the_given_writer() {
        let dir = TempDir::new("run-json-writer");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(&dir, dupes_cli(OutputFormat::Json, false, None), &mut out)
            .expect("json report must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let value: serde_json::Value =
            serde_json::from_slice(&out).expect("writer must contain valid JSON");
        assert_eq!(value["schema_version"], judge::finding::SCHEMA_VERSION);
        assert!(value.get("findings").is_some());
    }

    /// `--format sarif` renders a SARIF 2.1.0 log with workspace-relative,
    /// forward-slash artifact URIs (see `judge::sarif`).
    #[test]
    fn run_writes_a_sarif_log_into_the_given_writer() {
        let dir = TempDir::new("run-sarif-writer");
        write_fixture_crate(&dir);
        std::fs::write(dir.join("src/dupe.rs"), DUPE_FILE_CONTENT).unwrap();

        let mut out = Vec::new();
        let outcome = run_in_dir(&dir, dupes_cli(OutputFormat::Sarif, false, None), &mut out)
            .expect("sarif report must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let value: serde_json::Value =
            serde_json::from_slice(&out).expect("writer must contain valid JSON");
        assert_eq!(value["version"], "2.1.0");
        let run = &value["runs"][0];
        assert_eq!(run["tool"]["driver"]["name"], "judge");
        assert_eq!(run["tool"]["driver"]["rules"][0]["id"], "duplicate-code");
        let result = &run["results"][0];
        assert_eq!(result["ruleId"], "duplicate-code");
        assert_eq!(result["level"], "warning");
        assert_eq!(
            result["locations"][0]["physicalLocation"]["artifactLocation"]["uri"],
            "src/dupe.rs"
        );
        assert_eq!(result["properties"]["evidence_class"], "derived_fact");
    }

    /// Markdown is delta-only (see todo.md §7): a plain report command must
    /// reject it as a config error (exit 2) instead of printing half-baked
    /// output.
    #[test]
    fn health_format_markdown_is_a_config_error() {
        let dir = TempDir::new("health-format-markdown");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        let err = run_in_dir(
            &dir,
            cli_with(Command::Health(HealthOptions {
                score: false,
                show_cascades: false,
                baseline_args: baseline_args(OutputFormat::Markdown, false, None),
                include_generated: false,
            })),
            &mut out,
        )
        .expect_err("`health --format markdown` must be a config error");
        match err {
            CliError::Config(message) => {
                assert!(
                    message.contains("--format markdown is not supported"),
                    "message: {message}"
                );
            }
            other => panic!("expected CliError::Config, got {other:?}"),
        }
    }

    /// `explain` supports neither SARIF nor Markdown — a clean config error
    /// (exit 2) in Fast and Deep Tier builds alike.
    #[test]
    fn explain_format_sarif_is_a_config_error() {
        let mut out = Vec::new();
        let err = run(
            cli_with(Command::Explain(ExplainOptions {
                item_path: "core::retry::backoff".to_string(),
                why_live: true,
                include_tests: false,
                format: OutputFormat::Sarif,
            })),
            &mut out,
        )
        .expect_err("`explain --format sarif` must be a config error");
        match err {
            CliError::Config(message) => {
                assert!(
                    message.contains("--format sarif is not supported"),
                    "message: {message}"
                );
            }
            other => panic!("expected CliError::Config, got {other:?}"),
        }
    }

    /// (d) `patterns` never fails the verdict, even with a real corroborated
    /// candidate (todo.md §16.6: "Kein Pattern-Kandidat allein führt zu
    /// Exitcode 1").
    #[test]
    fn patterns_command_is_clean_even_with_a_real_candidate() {
        let dir = TempDir::new("patterns-clean");
        write_pattern_candidate_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::Patterns(PatternsOptions {
                format: OutputFormat::Json,
                clippy_json: None,
                save_pattern_baseline: false,
                pattern_baseline: None,
            })),
            &mut out,
        )
        .expect("`patterns` must not error on a valid fixture");
        assert_eq!(outcome, CommandOutcome::Clean);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let candidates = json["candidates"].as_array().expect("candidates array");
        assert_eq!(
            candidates.len(),
            1,
            "expected one corroborated candidate: {json}"
        );
    }

    /// (d.2) Several pattern rules can produce candidates in the same run —
    /// here all five §16.3 MVP rules fire together on one fixture — and
    /// `patterns` still stays clean (exit 0).
    #[test]
    fn patterns_command_reports_candidates_from_multiple_rules_and_stays_clean() {
        let dir = TempDir::new("patterns-multi-rule");
        write_multi_rule_pattern_candidate_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::Patterns(PatternsOptions {
                format: OutputFormat::Json,
                clippy_json: None,
                save_pattern_baseline: false,
                pattern_baseline: None,
            })),
            &mut out,
        )
        .expect("`patterns` must not error on a valid fixture");
        assert_eq!(outcome, CommandOutcome::Clean);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let candidates = json["candidates"].as_array().expect("candidates array");
        assert_eq!(
            candidates.len(),
            5,
            "expected one candidate per rule: {json}"
        );
        let patterns: std::collections::BTreeSet<&str> = candidates
            .iter()
            .map(|candidate| candidate["pattern"].as_str().unwrap())
            .collect();
        assert_eq!(
            patterns,
            std::collections::BTreeSet::from([
                "domain_error",
                "validated_newtype",
                "options_struct",
                "smart_constructor",
                "raii_guard",
            ]),
            "expected candidates from five different rules: {json}"
        );
    }

    /// (d.3) `--save-pattern-baseline` writes a `PatternBaseline` JSON file
    /// — a separate mechanism from `Finding` baselines (see
    /// `judge::pattern_baseline`) — and `--pattern-baseline` reads it back,
    /// classifying candidates into new/resolved/unchanged with no verdict
    /// (pattern candidates never gate).
    #[test]
    fn pattern_baseline_flags_save_and_diff_candidates() {
        let dir = TempDir::new("patterns-baseline-flags");
        write_pattern_candidate_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::Patterns(PatternsOptions {
                format: OutputFormat::Tty,
                clippy_json: None,
                save_pattern_baseline: true,
                pattern_baseline: None,
            })),
            &mut out,
        )
        .expect("saving a pattern baseline must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(
            text.contains("pattern baseline saved:") && text.contains("(1 candidates)"),
            "unexpected output: {text}"
        );
        assert!(
            dir.join(DEFAULT_PATTERN_BASELINE).exists(),
            "expected {} to exist",
            DEFAULT_PATTERN_BASELINE
        );

        // The multi-rule fixture keeps `fn a`/`fn b`/`FixtureError` verbatim
        // (see its doc comment) and adds four more pattern-triggering
        // shapes, so the `domain-error` candidate saved above keeps the same
        // id (unchanged) while the other four are new.
        write_multi_rule_pattern_candidate_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::Patterns(PatternsOptions {
                format: OutputFormat::Tty,
                clippy_json: None,
                save_pattern_baseline: false,
                pattern_baseline: Some(PathBuf::from(DEFAULT_PATTERN_BASELINE)),
            })),
            &mut out,
        )
        .expect("comparing against a pattern baseline must not error");
        assert_eq!(outcome, CommandOutcome::Clean);
        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("unchanged: 1"), "unexpected output: {text}");
        assert!(text.contains("resolved: 0"), "unexpected output: {text}");
        assert!(text.contains("new: 4"), "unexpected output: {text}");
    }

    /// (e) `principles` reports the corroborated `functional-core-
    /// imperative-shell` heuristic and never fails the verdict (todo.md
    /// §16.7: advisory only, same as `patterns`).
    #[test]
    fn principles_command_reports_a_heuristic_and_stays_clean() {
        let dir = TempDir::new("principles-clean");
        write_principle_heuristic_fixture_crate(&dir);

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::Principles(PrinciplesOptions {
                format: OutputFormat::Json,
            })),
            &mut out,
        )
        .expect("`principles` must not error on a valid fixture");
        assert_eq!(outcome, CommandOutcome::Clean);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let heuristics = json["heuristics"].as_array().expect("heuristics array");
        assert_eq!(
            heuristics.len(),
            1,
            "expected one corroborated heuristic: {json}"
        );
        assert_eq!(
            heuristics[0]["principle"].as_str(),
            Some("functional_core_imperative_shell")
        );
    }

    /// (e) `explain-pattern` with an unknown id is a usage error (exit 2),
    /// not a findings verdict.
    #[test]
    fn explain_pattern_unknown_id_is_an_analyzer_error() {
        let dir = TempDir::new("explain-pattern-unknown");
        write_fixture_crate(&dir);

        let mut out = Vec::new();
        let err = run_in_dir(
            &dir,
            cli_with(Command::ExplainPattern(ExplainPatternOptions {
                id: "pattern:domain-error:doesnotexist".to_string(),
                format: OutputFormat::Tty,
            })),
            &mut out,
        )
        .expect_err("unknown pattern candidate id must be an error");
        match err {
            CliError::Analyzer(message) => {
                assert!(
                    message.contains("unknown pattern candidate id"),
                    "message: {message}"
                );
            }
            other => panic!("expected CliError::Analyzer, got {other:?}"),
        }
    }

    /// (e) `explain-principle` with an unknown id is a usage error (exit 2),
    /// not a findings verdict — same convention as `explain-pattern`.
    #[test]
    fn explain_principle_unknown_id_is_an_analyzer_error() {
        let dir = TempDir::new("explain-principle-unknown");
        write_principle_heuristic_fixture_crate(&dir);

        let mut out = Vec::new();
        let err = run_in_dir(
            &dir,
            cli_with(Command::ExplainPrinciple(ExplainPrincipleOptions {
                id: "principle:cohesion:doesnotexist".to_string(),
                format: OutputFormat::Tty,
            })),
            &mut out,
        )
        .expect_err("unknown principle heuristic id must be an error");
        match err {
            CliError::Analyzer(message) => {
                assert!(
                    message.contains("unknown principle heuristic id"),
                    "message: {message}"
                );
            }
            other => panic!("expected CliError::Analyzer, got {other:?}"),
        }
    }

    /// (e) `explain-principle` on a known id returns the same heuristic
    /// `principles` reports, rendered with its full evidence and
    /// interpretation.
    #[test]
    fn explain_principle_known_id_matches_principles_output() {
        let dir = TempDir::new("explain-principle-known");
        write_principle_heuristic_fixture_crate(&dir);

        let mut json_out = Vec::new();
        run_in_dir(
            &dir,
            cli_with(Command::Principles(PrinciplesOptions {
                format: OutputFormat::Json,
            })),
            &mut json_out,
        )
        .expect("`principles` must not error");
        let json: serde_json::Value = serde_json::from_slice(&json_out).unwrap();
        let id = json["heuristics"][0]["id"]
            .as_str()
            .expect("heuristic id")
            .to_string();

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::ExplainPrinciple(ExplainPrincipleOptions {
                id: id.clone(),
                format: OutputFormat::Json,
            })),
            &mut out,
        )
        .expect("`explain-principle` must not error for a known id");
        assert_eq!(outcome, CommandOutcome::Clean);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(json["id"].as_str(), Some(id.as_str()));
        assert_eq!(
            json["principle"].as_str(),
            Some("functional_core_imperative_shell")
        );

        let mut tty_out = Vec::new();
        run_in_dir(
            &dir,
            cli_with(Command::ExplainPrinciple(ExplainPrincipleOptions {
                id,
                format: OutputFormat::Tty,
            })),
            &mut tty_out,
        )
        .expect("`explain-principle` must not error for a known id");
        let text = String::from_utf8(tty_out).unwrap();
        assert!(text.contains("principle heuristic:"), "output: {text}");
        assert!(text.contains("interpretation:"), "output: {text}");
    }

    /// `explain-rule` is a pure static lookup — no workspace/cwd needed, so
    /// these tests call `run` directly instead of `run_in_dir`.
    ///
    /// (a) A known rule id resolves with every field rendered in TTY output.
    #[test]
    fn explain_rule_known_id_renders_all_fields_in_tty() {
        let mut out = Vec::new();
        let outcome = run(
            cli_with(Command::ExplainRule(ExplainRuleOptions {
                id: "catch-all-error".to_string(),
                format: OutputFormat::Tty,
            })),
            &mut out,
        )
        .expect("known rule id must not error");
        assert_eq!(outcome, CommandOutcome::Clean);

        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("rule: catch-all-error"), "{text}");
        assert!(text.contains("evidence class: derived_fact"), "{text}");
        assert!(text.contains("verdict effect: gating"), "{text}");
        assert!(text.contains("preconditions:"), "{text}");
        assert!(text.contains("exclusions:"), "{text}");
        assert!(text.contains("allowed wording:"), "{text}");
    }

    /// (a) Same known rule id, rendered as JSON — same field values, an
    /// unambiguous shape.
    #[test]
    fn explain_rule_known_id_renders_all_fields_in_json() {
        let mut out = Vec::new();
        let outcome = run(
            cli_with(Command::ExplainRule(ExplainRuleOptions {
                id: "catch-all-error".to_string(),
                format: OutputFormat::Json,
            })),
            &mut out,
        )
        .expect("known rule id must not error");
        assert_eq!(outcome, CommandOutcome::Clean);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(json["id"], "catch-all-error");
        assert_eq!(json["evidence_class"], "derived_fact");
        assert_eq!(json["verdict_effect"], "gating");
        assert!(
            !json["preconditions"]
                .as_str()
                .unwrap_or_default()
                .is_empty()
        );
        assert!(!json["exclusions"].as_str().unwrap_or_default().is_empty());
        assert!(
            !json["allowed_wording"]
                .as_str()
                .unwrap_or_default()
                .is_empty()
        );
    }

    /// (c) A rule id with a curated registry example renders it in both TTY
    /// and JSON; a rule id without one (`catch-all-error`, above) omits the
    /// section/field entirely rather than printing an empty placeholder.
    #[test]
    fn explain_rule_with_a_curated_example_renders_it_in_tty_and_json() {
        let mut tty_out = Vec::new();
        run(
            cli_with(Command::ExplainRule(ExplainRuleOptions {
                id: "swallowed-result".to_string(),
                format: OutputFormat::Tty,
            })),
            &mut tty_out,
        )
        .expect("known rule id must not error");
        let tty_text = String::from_utf8(tty_out).unwrap();
        assert!(tty_text.contains("  example:"), "{tty_text}");
        assert!(tty_text.contains("let _ ="), "{tty_text}");
        assert!(tty_text.contains("why it matters:"), "{tty_text}");

        let mut json_out = Vec::new();
        run(
            cli_with(Command::ExplainRule(ExplainRuleOptions {
                id: "swallowed-result".to_string(),
                format: OutputFormat::Json,
            })),
            &mut json_out,
        )
        .expect("known rule id must not error");
        let json: serde_json::Value = serde_json::from_slice(&json_out).unwrap();
        assert!(
            json["example"]["before"]
                .as_str()
                .unwrap()
                .contains("let _ =")
        );
        assert!(
            !json["example"]["why_it_matters"]
                .as_str()
                .unwrap()
                .is_empty()
        );

        let mut no_example_out = Vec::new();
        run(
            cli_with(Command::ExplainRule(ExplainRuleOptions {
                id: "crate-boundary-violation".to_string(),
                format: OutputFormat::Json,
            })),
            &mut no_example_out,
        )
        .expect("known rule id must not error");
        let json: serde_json::Value = serde_json::from_slice(&no_example_out).unwrap();
        assert!(json["example"].is_null());
    }

    /// (b) An unknown rule id is a usage error (exit 2), not a findings
    /// verdict — same convention as `explain-pattern`.
    #[test]
    fn explain_rule_unknown_id_is_an_analyzer_error() {
        let mut out = Vec::new();
        let err = run(
            cli_with(Command::ExplainRule(ExplainRuleOptions {
                id: "not-a-real-rule".to_string(),
                format: OutputFormat::Tty,
            })),
            &mut out,
        )
        .expect_err("unknown rule id must be an error");
        match &err {
            CliError::Analyzer(message) => {
                assert!(message.contains("unknown rule id"), "message: {message}");
            }
            other => panic!("expected CliError::Analyzer, got {other:?}"),
        }
        assert_eq!(exit_code(&Err(err)), 2);
    }

    /// (f) `fix-preview` returns only the migration plan and related
    /// findings — no patch.
    #[test]
    fn fix_preview_lists_migration_steps_without_a_patch() {
        let dir = TempDir::new("fix-preview");
        write_pattern_candidate_fixture_crate(&dir);

        let mut json_out = Vec::new();
        run_in_dir(
            &dir,
            cli_with(Command::Patterns(PatternsOptions {
                format: OutputFormat::Json,
                clippy_json: None,
                save_pattern_baseline: false,
                pattern_baseline: None,
            })),
            &mut json_out,
        )
        .expect("`patterns` must not error");
        let json: serde_json::Value = serde_json::from_slice(&json_out).unwrap();
        let id = json["candidates"][0]["id"]
            .as_str()
            .expect("candidate id")
            .to_string();

        let mut out = Vec::new();
        let outcome = run_in_dir(
            &dir,
            cli_with(Command::FixPreview(FixPreviewOptions {
                id,
                format: OutputFormat::Json,
            })),
            &mut out,
        )
        .expect("`fix-preview` must not error for a known id");
        assert_eq!(outcome, CommandOutcome::Clean);

        let preview: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert!(preview["patch"].is_null(), "preview: {preview}");
        assert!(
            !preview["migration"]
                .as_array()
                .expect("migration array")
                .is_empty()
        );
        assert!(
            !preview["related_findings"]
                .as_array()
                .expect("related_findings array")
                .is_empty()
        );
    }

    /// A writer that fails like a closed pipe (`cargo judge … | head`).
    struct BrokenPipeWriter;

    impl Write for BrokenPipeWriter {
        fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
            Err(std::io::Error::from(std::io::ErrorKind::BrokenPipe))
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    /// Broken pipe: the render aborts with `CliError::Io(BrokenPipe)`, which
    /// [`exit_code`] maps to a silent exit 0 — before the refactor this was
    /// a `println!` panic (exit 101).
    #[test]
    fn a_broken_pipe_while_rendering_maps_to_exit_zero() {
        let dir = TempDir::new("run-broken-pipe");
        write_fixture_crate(&dir);

        let result = run_in_dir(&dir, cli_with(Command::Inspect), &mut BrokenPipeWriter);
        let err = result.expect_err("writes to a broken pipe must surface as an error");
        match &err {
            CliError::Io(io_err) => {
                assert_eq!(io_err.kind(), std::io::ErrorKind::BrokenPipe);
            }
            other => panic!("expected CliError::Io, got {other:?}"),
        }
        assert_eq!(exit_code(&Err(err)), 0);
    }

    /// The exit-code convention `main` applies: 0 clean, 1 findings verdict
    /// failed, 2 real error — broken pipe being the documented exception.
    #[test]
    fn exit_codes_follow_the_documented_convention() {
        assert_eq!(exit_code(&Ok(CommandOutcome::Clean)), 0);
        assert_eq!(exit_code(&Ok(CommandOutcome::FindingsFound)), 1);
        assert_eq!(exit_code(&Err(CliError::Config("x".to_string()))), 2);
        assert_eq!(exit_code(&Err(CliError::Analyzer("x".to_string()))), 2);
        assert_eq!(exit_code(&Err(CliError::Reported)), 2);
        assert_eq!(
            exit_code(&Err(CliError::AnalysisIncomplete {
                context: "x",
                errors: Vec::new(),
            })),
            2
        );
        assert_eq!(
            exit_code(&Err(CliError::Io(std::io::Error::other("disk")))),
            2
        );
    }

    /// Artifact comparison classifies newly added source findings without
    /// consulting repository history.
    #[test]
    fn baseline_diff_classifies_a_new_files_duplication_as_introduced() {
        let _guard = lock_cwd();
        let dir = TempDir::new("audit-code-introduced");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);
        let base_commit = commit_sha(&dir, "HEAD");

        let manifest = dir.join("Cargo.toml");
        let workspace = judge::ingest::load(Some(&manifest)).unwrap();
        let baseline_collected = collect_findings(&workspace).unwrap();
        assert!(baseline_collected.analysis_errors.is_empty());
        let baseline = judge::baseline::Baseline::new(
            &baseline_collected.findings,
            base_commit.clone(),
            baseline_collected.rule_revisions,
            judge::health_score::total_authored_loc(&workspace),
            judge::health_score::ScoreContext::from_profiles(&[]),
        );

        // `judge::ingest::collect_source_files` walks the directory tree
        // rather than following `mod` declarations, so a new file is picked
        // up without needing to be wired into `lib.rs`.
        std::fs::write(dir.join("src/dupe.rs"), DUPE_FILE_CONTENT).unwrap();
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "add duplicated code"]);
        // Source file lists are captured at `ingest::load` time, not
        // re-scanned dynamically — reload to see the file added above.
        let workspace = judge::ingest::load(Some(&manifest)).unwrap();
        let mut collected = collect_findings(&workspace).unwrap();
        assert!(collected.analysis_errors.is_empty());
        judge::finding::relativize_paths(&mut collected.findings, &workspace.root);

        let delta = judge::baseline::diff(
            &collected.findings,
            &baseline,
            &std::collections::HashSet::new(),
            &collected.rule_revisions,
        );

        let dupe_introduced: Vec<_> = delta
            .introduced
            .iter()
            .filter(|finding| finding.rule == judge::rules::duplication::DUPLICATE_RULE)
            .collect();
        assert_eq!(dupe_introduced.len(), 2);
        for finding in &dupe_introduced {
            assert_eq!(finding.location.file, PathBuf::from("src/dupe.rs"));
            assert_eq!(finding.severity, judge::finding::Severity::Warn);
        }
        assert_eq!(delta.tri_verdict(), TriVerdict::Warn);
    }

    /// Artifact comparisons retain an identical finding even when the rule
    /// revision changed; Git history is irrelevant to the comparison.
    #[test]
    fn artifact_comparison_keeps_an_identical_finding_unchanged() {
        let dir = TempDir::new("audit-rule-introduced");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);
        let base_commit = commit_sha(&dir, "HEAD");

        // Second commit touches an unrelated file only — `src/lib.rs` (the
        // file the simulated pre-existing finding lives in) is untouched.
        std::fs::write(dir.join("src/other.rs"), "pub fn other() {}\n").unwrap();
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "unrelated change"]);

        let pre_existing = judge::finding::Finding::new(
            "duplicate-code:src/lib.rs:hello:0-20".to_string(),
            judge::rules::duplication::DUPLICATE_RULE.to_string(),
            judge::finding::Severity::Warn,
            judge::finding::Location {
                file: PathBuf::from("src/lib.rs"),
                line: judge::finding::OneBasedLine::FIRST,
                item_path: "hello".to_string(),
            },
            judge::finding::EvidenceClass::DerivedFact,
            judge::finding::Origin::Code,
            None,
        );
        let baseline = judge::baseline::Baseline::new(
            std::slice::from_ref(&pre_existing),
            base_commit,
            std::collections::HashMap::from([(
                judge::rules::duplication::DUPLICATE_RULE.to_string(),
                1,
            )]),
            0,
            judge::health_score::ScoreContext::from_profiles(&[]),
        );
        let bumped_revisions = std::collections::HashMap::from([(
            judge::rules::duplication::DUPLICATE_RULE.to_string(),
            2,
        )]);

        let delta = judge::baseline::diff(
            &[pre_existing],
            &baseline,
            &std::collections::HashSet::new(),
            &bumped_revisions,
        );

        assert_eq!(delta.unchanged_count, 1);
        assert!(delta.introduced.is_empty());
        assert!(delta.rule_introduced.is_empty());
        assert_eq!(delta.tri_verdict(), TriVerdict::Pass);
    }

    /// Tests todo.md §17.2/§17.5's advisory default at the wiring level: a
    /// workspace whose only findings are heuristic (here: `churn-hotspot`)
    /// (a) scores without deductions and reports an advisory count, and
    /// (c) never breaks the delta/audit verdict with newly introduced
    /// heuristic findings.
    #[test]
    fn heuristic_only_findings_pass_the_verdict_and_score_without_deductions() {
        let _guard = lock_cwd();
        let dir = TempDir::new("advisory-heuristics-only");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);
        let base_commit = commit_sha(&dir, "HEAD");

        let manifest = dir.join("Cargo.toml");
        let workspace = judge::ingest::load(Some(&manifest)).unwrap();
        let baseline_collected = collect_findings(&workspace).unwrap();
        assert!(baseline_collected.analysis_errors.is_empty());
        let baseline = judge::baseline::Baseline::new(
            &baseline_collected.findings,
            base_commit.clone(),
            baseline_collected.rule_revisions,
            judge::health_score::total_authored_loc(&workspace),
            judge::health_score::ScoreContext::from_profiles(&[]),
        );

        // Five more commits to the same file, all inside `churn-hotspot`'s
        // 14-day window — enough churn for the (heuristic) rule to fire,
        // without introducing any derived-fact (G1–G3) pattern.
        for revision in 1..=5 {
            std::fs::write(
                dir.join("src/lib.rs"),
                format!("pub fn hello() -> u32 {{ {revision} }}\n"),
            )
            .unwrap();
            git(&dir, &["add", "."]);
            git(&dir, &["commit", "-q", "-m", &format!("rev {revision}")]);
        }

        let workspace = judge::ingest::load(Some(&manifest)).unwrap();
        let mut collected = collect_findings(&workspace).unwrap();
        assert!(collected.analysis_errors.is_empty());
        judge::finding::relativize_paths(&mut collected.findings, &workspace.root);

        collected.findings.push(judge::finding::Finding::new(
            "synthetic-advisory:src/lib.rs".to_string(),
            "synthetic-advisory".to_string(),
            judge::finding::Severity::Info,
            judge::finding::Location {
                file: PathBuf::from("src/lib.rs"),
                line: judge::finding::OneBasedLine::FIRST,
                item_path: "hello".to_string(),
            },
            judge::finding::EvidenceClass::Heuristic,
            judge::finding::Origin::Code,
            None,
        ));
        let gating_rules: Vec<&judge::finding::RuleId> = collected
            .findings
            .iter()
            .filter(|finding| finding.is_gating())
            .map(|finding| &finding.rule)
            .collect();
        assert!(
            gating_rules.is_empty(),
            "fixture should only produce heuristic (advisory) findings, got gating: {gating_rules:?}"
        );

        // (c) Newly introduced heuristic findings stay visible in the delta
        // but never break the verdict.
        let delta = judge::baseline::diff(
            &collected.findings,
            &baseline,
            &std::collections::HashSet::new(),
            &collected.rule_revisions,
        );
        assert!(!delta.introduced.is_empty());
        assert_eq!(delta.tri_verdict(), TriVerdict::Pass);

        // (a) The score takes no deductions from advisory findings, and the
        // report envelope records them as advisory.
        let total_loc = judge::health_score::total_authored_loc(&workspace);
        let score =
            match judge::health_score::compute(&collected.findings, total_loc, &workspace, &[]) {
                judge::health_score::ScoreOutcome::Available(score) => score,
                judge::health_score::ScoreOutcome::Unavailable(reason) => {
                    panic!("score unavailable: {reason}")
                }
            };
        assert_eq!(score.score, 100.0);
        assert_eq!(score.fail_count, 0);
        assert_eq!(score.warn_count, 0);

        let report = Report::new(collected.findings);
        assert_eq!(report.counts.gating, 0);
        assert!(report.counts.advisory > 0);
    }

    /// The default current-state pass does not derive historical hotspots,
    /// even when its workspace happens to be a Git repository.
    #[test]
    fn collect_findings_omits_historical_hotspots() {
        let _guard = lock_cwd();
        let dir = TempDir::new("hotspot-limit");
        write_fixture_crate(&dir);

        const FILE_COUNT: usize = 20;
        for branches in 1..=FILE_COUNT {
            let mut body = String::from("pub fn f(x: i32) -> i32 {\n    let mut total = x;\n");
            for i in 0..branches {
                body.push_str(&format!("    if x > {i} {{ total += {i}; }}\n"));
            }
            body.push_str("    total\n}\n");
            std::fs::write(dir.join(format!("src/hotspot_{branches:02}.rs")), body).unwrap();
        }

        let manifest = dir.join("Cargo.toml");
        let workspace = judge::ingest::load(Some(&manifest)).unwrap();
        let mut collected = collect_findings(&workspace).unwrap();
        assert!(collected.analysis_errors.is_empty());
        judge::finding::relativize_paths(&mut collected.findings, &workspace.root);

        let hotspot_files: std::collections::HashSet<&Path> = collected
            .findings
            .iter()
            .filter(|finding| finding.rule == "hotspot")
            .map(|finding| finding.location.file.as_path())
            .collect();
        assert!(hotspot_files.is_empty());
    }

    #[test]
    fn refactoring_commands_keep_their_cli_contracts() {
        let map = Cli::try_parse_from(["judge", "map", "--format", "json", "--include-tests"])
            .expect("map arguments must parse");
        let Some(Command::Map(options)) = map.command else {
            panic!("expected map command");
        };
        assert!(matches!(options.format, OutputFormat::Json));
        assert!(options.include_tests);

        let impact = Cli::try_parse_from(["judge", "impact", "src/lib.rs", "--format", "json"])
            .expect("impact arguments must parse");
        let Some(Command::Impact(options)) = impact.command else {
            panic!("expected impact command");
        };
        assert_eq!(options.target, PathBuf::from("src/lib.rs"));
        assert!(matches!(options.format, OutputFormat::Json));

        let structure = Cli::try_parse_from(["judge", "structure", "--format", "json"])
            .expect("structure arguments must parse");
        assert!(matches!(structure.command, Some(Command::Structure(_))));

        let complexity = Cli::try_parse_from(["judge", "complexity", "--include-tests"])
            .expect("complexity arguments must parse");
        let Some(Command::Complexity(options)) = complexity.command else {
            panic!("expected complexity command");
        };
        assert!(options.include_tests);

        let refactor = Cli::try_parse_from(["judge", "refactor", "src/lib.rs", "--format", "json"])
            .expect("refactor arguments must parse");
        let Some(Command::Refactor(options)) = refactor.command else {
            panic!("expected refactor command");
        };
        assert_eq!(options.target, Some(PathBuf::from("src/lib.rs")));

        let combined = Cli::try_parse_from(["judge", "--progress", "progress.jsonl"])
            .expect("combined progress arguments must parse");
        assert_eq!(combined.progress, Some(PathBuf::from("progress.jsonl")));

        let output = Cli::try_parse_from([
            "judge",
            "dupes",
            "--format",
            "json",
            "--output",
            "report.json",
        ])
        .expect("JSON output arguments must parse");
        assert_eq!(output.output, Some(PathBuf::from("report.json")));
    }

    #[test]
    fn json_artifacts_use_a_command_default_or_an_explicit_output_path() {
        let dir = TempDir::new("json-artifacts");
        write_fixture_crate(&dir);

        let mut default_out = Vec::new();
        let outcome = run_json_in_dir(
            &dir,
            dupes_cli(OutputFormat::Json, false, None),
            &mut default_out,
        )
        .expect("default JSON artifact must be written");
        assert_eq!(outcome, CommandOutcome::Clean);
        assert_eq!(
            String::from_utf8(default_out).unwrap(),
            "JSON written to .judge/dupes.json\n"
        );
        let default_json: serde_json::Value =
            serde_json::from_slice(&std::fs::read(dir.join(".judge/dupes.json")).unwrap())
                .expect("default artifact JSON");
        let default_json_text = std::fs::read_to_string(dir.join(".judge/dupes.json")).unwrap();
        assert!(
            default_json_text.starts_with("{\n  \"header\": "),
            "artifact context must be the first root field"
        );
        assert!(default_json["findings"].is_array());
        assert_eq!(default_json["header"]["schema_version"], 1);
        assert_eq!(default_json["header"]["command"], "dupes");
        assert!(default_json["header"]["context"]["analysis_universe"].is_null());
        assert!(default_json["header"]["context"]["evidence_contract"].is_object());
        assert!(default_json["header"]["context"]["next_actions"].is_array());
        assert!(default_json["header"]["working_directory"].is_string());
        assert!(
            default_json["header"]["generated_at_utc"]
                .as_str()
                .is_some_and(|timestamp| timestamp.ends_with('Z'))
        );
        assert!(default_json["header"]["generated_at_unix_seconds"].is_u64());
        assert_eq!(
            default_json["header"]["assessment"]["kind"],
            "informational"
        );

        let custom_path = dir.join("reports/custom.json");
        let mut custom_cli = dupes_cli(OutputFormat::Json, false, None);
        custom_cli.output = Some(custom_path.clone());
        let mut custom_out = Vec::new();
        run_json_in_dir(&dir, custom_cli, &mut custom_out)
            .expect("explicit JSON artifact must be written");
        assert!(custom_path.is_file());

        let mut invalid_cli = dupes_cli(OutputFormat::Tty, false, None);
        invalid_cli.output = Some(PathBuf::from("report.json"));
        let error = run_with_artifact_output(invalid_cli, &mut Vec::new())
            .expect_err("--output without JSON must be rejected");
        assert!(matches!(error, CliError::Config(message) if message.contains("--format json")));
    }

    #[test]
    fn markdown_review_reports_use_a_default_or_explicit_judge_artifact_path() {
        let dir = TempDir::new("markdown-artifacts");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        std::fs::write(dir.join("src/lib.rs"), DUPE_FILE_CONTENT).unwrap();
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);

        let mut default_out = Vec::new();
        let outcome = run_json_in_dir(&dir, all_cli_markdown(), &mut default_out)
            .expect("default Markdown artifact must be written");
        assert_eq!(outcome, CommandOutcome::Clean);
        assert_eq!(
            String::from_utf8(default_out).unwrap(),
            "Markdown written to .judge/judge.md\n"
        );
        let default_markdown = std::fs::read_to_string(dir.join(".judge/judge.md")).unwrap();
        assert!(
            default_markdown.starts_with("# Judge summary\n"),
            "unexpected artifact: {default_markdown}"
        );
        assert!(
            default_markdown.contains("## Evidence-backed findings"),
            "unexpected artifact: {default_markdown}"
        );
        assert!(
            default_markdown.contains("## Next steps"),
            "unexpected artifact: {default_markdown}"
        );

        let custom_path = dir.join("reports/review.md");
        let mut custom_cli = all_cli_markdown();
        custom_cli.output = Some(custom_path.clone());
        let mut custom_out = Vec::new();
        run_json_in_dir(&dir, custom_cli, &mut custom_out)
            .expect("explicit Markdown artifact must be written");
        assert!(custom_path.is_file());

        let mut invalid_cli = all_cli(false, None);
        invalid_cli.output = Some(PathBuf::from("report.md"));
        let error = run_with_artifact_output(invalid_cli, &mut Vec::new())
            .expect_err("--output on the bare tty run must be rejected");
        assert!(matches!(error, CliError::Config(message) if message.contains("--format json")));
    }

    #[test]
    fn root_help_states_the_post_refactoring_purpose() {
        use clap::CommandFactory;

        let help = Cli::command().render_long_help().to_string();
        assert!(help.contains("Deterministic post-refactoring analysis for Rust workspaces"));
        assert!(help.contains("--progress <PATH>"));
    }

    #[test]
    fn map_and_impact_json_keep_refactoring_facts_machine_readable() {
        let dir = TempDir::new("refactoring-command-contracts");
        write_fixture_crate(&dir);

        let mut map_out = Vec::new();
        let map = cli_with(Command::Map(MapOptions {
            format: OutputFormat::Json,
            include_tests: false,
        }));
        let outcome = run_in_dir(&dir, map, &mut map_out).expect("map must run");
        assert_eq!(outcome, CommandOutcome::Clean);
        let map: serde_json::Value = serde_json::from_slice(&map_out).expect("map JSON");
        assert_eq!(map["schema_version"], judge::refactor_map::SCHEMA_VERSION);
        assert_eq!(map["includes_tests"], false);
        assert_eq!(map["crates"][0]["name"], "fixture");
        assert_eq!(map["files"][0]["production"]["functions"], 1);
        assert_eq!(map["duplication"]["schema_version"], 1);
        assert_eq!(map["duplication"]["clone_families"], 0);

        let mut impact_out = Vec::new();
        let impact = cli_with(Command::Impact(ImpactOptions {
            target: PathBuf::from("src/lib.rs"),
            format: OutputFormat::Json,
        }));
        let outcome = run_in_dir(&dir, impact, &mut impact_out).expect("impact must run");
        assert_eq!(outcome, CommandOutcome::Clean);
        let impact: serde_json::Value = serde_json::from_slice(&impact_out).expect("impact JSON");
        assert_eq!(impact["schema_version"], judge::impact::SCHEMA_VERSION);
        assert_eq!(impact["target"], "src/lib.rs");
        assert_eq!(impact["crate_name"], "fixture");
        assert!(impact["direct_analysis"].as_array().is_some_and(|effects| {
            effects
                .iter()
                .any(|effect| effect["command"] == "cargo judge map")
        }));
    }

    /// Two files with distinct duplicate-function pairs: `src/a.rs` holds two
    /// separate clone families (4 duplication findings), `src/b.rs` holds one
    /// (2 findings) — enough of a severity/reason_count spread to prove
    /// `refactor`'s `next_actions` mirrors `candidates`' own ranking rather
    /// than recomputing one.
    fn write_two_file_duplication_fixture(dir: &Path) {
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();

        let dup_fn = |value: usize, suffix: &str| {
            format!(
                "pub fn dup_{value}_{suffix}() -> i32 {{ let v0 = {value}; let v1 = {value}; \
                 let v2 = {value}; let v3 = {value}; let v4 = {value}; let v5 = {value}; \
                 let v6 = {value}; v0 + v1 + v2 + v3 + v4 + v5 + v6 }}\n"
            )
        };

        let mut a = String::new();
        for value in [0, 2] {
            a.push_str(&dup_fn(value, "a"));
            a.push_str(&dup_fn(value, "b"));
        }
        std::fs::write(dir.join("src/a.rs"), a).unwrap();

        let mut b = String::new();
        b.push_str(&dup_fn(1, "a"));
        b.push_str(&dup_fn(1, "b"));
        std::fs::write(dir.join("src/b.rs"), b).unwrap();

        std::fs::write(dir.join("src/lib.rs"), "pub fn hello() {}\n").unwrap();
    }

    /// GitHub issue #31: `refactor`'s `next_actions` is a ranked, evidence-
    /// linked top-N summary of the same `candidates` list already computed —
    /// not a separate analysis pass. Every field must mirror the candidate at
    /// the same rank, and identical project state must yield byte-identical
    /// output.
    #[test]
    fn refactor_json_next_actions_mirrors_the_ranked_candidates() {
        let dir = TempDir::new("refactor-next-actions");
        write_two_file_duplication_fixture(&dir);

        let refactor_cli = || {
            cli_with(Command::Refactor(RefactorOptions {
                target: None,
                format: OutputFormat::Json,
            }))
        };

        let mut first_out = Vec::new();
        let outcome = run_in_dir(&dir, refactor_cli(), &mut first_out).expect("refactor must run");
        assert_eq!(outcome, CommandOutcome::Clean);

        let mut second_out = Vec::new();
        run_in_dir(&dir, refactor_cli(), &mut second_out).expect("refactor must run again");

        let json: serde_json::Value = serde_json::from_slice(&first_out).expect("refactor JSON");
        let second_json: serde_json::Value =
            serde_json::from_slice(&second_out).expect("refactor JSON (second run)");
        // `next_actions` is derived solely from `candidates`, which sorts
        // deterministically by severity, reason_count, then file; both must
        // be byte-stable across runs regardless of the underlying findings'
        // discovery order (see GitHub issue #31).
        assert_eq!(
            json["candidates"], second_json["candidates"],
            "identical project state must yield byte-identical candidates"
        );
        assert_eq!(
            json["next_actions"], second_json["next_actions"],
            "identical project state must yield byte-identical next_actions"
        );
        let candidates = json["candidates"].as_array().expect("candidates array");
        let next_actions = json["next_actions"].as_array().expect("next_actions array");

        assert_eq!(candidates.len(), 2, "unexpected candidates: {candidates:?}");
        assert_eq!(candidates[0]["file"], "src/a.rs");
        assert_eq!(candidates[0]["reason_count"], 4);
        assert_eq!(candidates[1]["file"], "src/b.rs");
        assert_eq!(candidates[1]["reason_count"], 2);

        assert_eq!(next_actions.len(), candidates.len());
        for (index, (candidate, next_action)) in
            candidates.iter().zip(next_actions.iter()).enumerate()
        {
            assert_eq!(next_action["rank"], index + 1);
            assert_eq!(next_action["file"], candidate["file"]);
            assert_eq!(next_action["severity"], candidate["severity"]);
            assert_eq!(next_action["reason_count"], candidate["reason_count"]);
            assert_eq!(next_action["rules"], candidate["rules"]);
            assert_eq!(next_action["finding_ids"], candidate["finding_ids"]);
            let action = next_action["action"].as_str().expect("action string");
            assert!(
                action.contains(candidate["file"].as_str().unwrap()),
                "action must reference the candidate's file: {action}"
            );
            assert!(
                action.contains(&candidate["reason_count"].to_string()),
                "action must reference the candidate's reason_count: {action}"
            );
        }
    }

    #[test]
    fn map_include_tests_changes_ranking_scope_without_merging_metrics() {
        let dir = TempDir::new("map-include-tests-contract");
        std::fs::write(
            dir.join("Cargo.toml"),
            "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        )
        .unwrap();
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(
            dir.join("src/lib.rs"),
            "pub fn production() {}\n\n#[cfg(test)]\nmod tests { fn helper(value: bool) { if value {} } }\n",
        )
        .unwrap();

        let mut out = Vec::new();
        let cli = cli_with(Command::Map(MapOptions {
            format: OutputFormat::Json,
            include_tests: true,
        }));
        run_in_dir(&dir, cli, &mut out).expect("map with tests must run");
        let map: serde_json::Value = serde_json::from_slice(&out).expect("map JSON");
        assert_eq!(map["includes_tests"], true);
        let file = map["files"]
            .as_array()
            .and_then(|files| files.iter().find(|file| file["file"] == "src/lib.rs"))
            .expect("lib.rs must appear in the map");
        assert_eq!(file["production"]["functions"], 1);
        assert_eq!(file["tests"]["functions"], 1);
        assert_eq!(file["complexity_rank"], 1);
    }

    #[test]
    fn combined_progress_is_separate_versioned_jsonl_and_subcommands_reject_it() {
        let dir = TempDir::new("combined-progress-contract");
        git(&dir, &["init", "-q", "-b", "main"]);
        write_fixture_crate(&dir);
        git(&dir, &["add", "."]);
        git(&dir, &["commit", "-q", "-m", "initial"]);

        let mut out = Vec::new();
        let mut cli = all_cli(false, None);
        cli.progress = Some(PathBuf::from("judge-progress.jsonl"));
        run_in_dir(&dir, cli, &mut out).expect("combined run with progress must succeed");

        let progress = std::fs::read_to_string(dir.join("judge-progress.jsonl"))
            .expect("progress file must be written");
        let records: Vec<serde_json::Value> = progress
            .lines()
            .map(|line| serde_json::from_str(line).expect("one JSON record per line"))
            .collect();
        assert!(!records.is_empty());
        assert_eq!(records[0]["schema_version"], 1);
        assert_eq!(records[0]["event"], "phase_started");
        assert!(records.iter().all(|record| record["phase"].is_string()));

        let error = run(
            Cli {
                command: Some(Command::Init),
                baseline_args: baseline_args(OutputFormat::Tty, false, None),
                progress: Some(PathBuf::from("not-allowed.jsonl")),
                details: false,
                color: ColorChoice::Auto,
                output: None,
            },
            &mut Vec::new(),
        )
        .expect_err("subcommands must not share the combined progress channel");
        let CliError::Config(message) = error else {
            panic!("progress misuse must be a configuration error");
        };
        assert!(message.contains("bare `cargo judge` combined run"));
    }
}