devflow 2.7.0

DevFlow CLI — an opinionated take on AI-driven development automation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
//! Every CLI subcommand handler and the display/rendering helpers they
//! share: `start`, the gate/status/logs/history family, worktree listing,
//! recovery, and `devflow doctor`'s project-aware reconciliation core.
//!
//! D-07: this is deliberately one flat file, not a `commands/`
//! subdirectory. Mapping Phase 18's plans onto clusters showed this
//! cluster absorbed only 2 of 7 plans (pipeline absorbed 3), so a
//! per-subcommand directory buys zero measured wave reduction — and it
//! tends to re-centralise the shared display helpers this file already
//! keeps flat into a `common.rs`, recreating exactly the contention the
//! split is meant to remove.

use crate::CliError;
use crate::config_parse;
use crate::config_parse::GATE_ESCALATION_THRESHOLD_SECS;
use crate::parallel::ensure_phase_worktree;
use crate::pipeline_gate::print_dry_run;
use crate::pipeline_launch::{launch_stage, single_active_phase};
use crate::pipeline_outcomes::render_gate_context;
use crate::preflight::{
    agent_program, ensure_agent_binary, ensure_base_ref_current, ensure_phase_reachable_on_base,
};
use crate::staleness::{enforce_build_staleness, run_git_stdout};
use devflow_core::agent;
use devflow_core::agent_result;
use devflow_core::agents;
use devflow_core::config::{self, DEVELOP, FEATURE_PREFIX, MAIN};
use devflow_core::events;
use devflow_core::gates::{GateAction, GateError, GateResponse, Gates, OpenGate};
use devflow_core::git::{GitFlow, git_command, hermetic_command};
use devflow_core::history;
use devflow_core::lock;
use devflow_core::mode::Mode;
use devflow_core::phase_id::PhaseId;
use devflow_core::recover;
use devflow_core::registry;
use devflow_core::ship_evidence;
use devflow_core::stage::Stage;
use devflow_core::state::{AgentKind, State};
use devflow_core::version;
use devflow_core::workflow;
use devflow_core::worktree;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

pub(crate) fn resolve_gate_target(
    positional: Option<String>,
    legacy_project: Option<PathBuf>,
    stage_option: Option<Stage>,
    project: PathBuf,
) -> Result<(Option<Stage>, PathBuf), CliError> {
    let Some(positional) = positional else {
        return Ok((stage_option, project));
    };
    if let Ok(positional_stage) = positional.parse::<Stage>() {
        if let Some(flagged_stage) = stage_option
            && flagged_stage != positional_stage
        {
            return Err(CliError::Message(format!(
                "conflicting stages: positional {positional_stage} and --stage {flagged_stage}"
            )));
        }
        let target = legacy_project.unwrap_or(project);
        return Ok((Some(stage_option.unwrap_or(positional_stage)), target));
    }
    if legacy_project.is_some() {
        return Err(CliError::Message(format!(
            "unsupported stage `{positional}`; expected define, plan, code, validate, or ship"
        )));
    }
    if project.as_path() != Path::new(".") {
        return Err(CliError::Message(
            "project was supplied both positionally and with --project".into(),
        ));
    }
    Ok((stage_option, PathBuf::from(positional)))
}

// ---------------------------------------------------------------------------
// start / pipeline driving
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
/// Whether phase `{NN}`'s GSD planning artifact (a `.planning/phases/{NN}-*/`
/// file ending in `suffix`, e.g. `-CONTEXT.md`) exists on `develop` — the
/// branch phase worktrees fork from. Fail-open on git errors (missing
/// develop, not a repo): pre-flight must never block a run the later, more
/// specific checks would allow.
pub(crate) fn phase_artifact_on_develop(project_root: &Path, phase: PhaseId, suffix: &str) -> bool {
    let prefix = format!(".planning/phases/{padded}-", padded = phase.padded());
    let output = git_command(project_root)
        .args([
            "ls-tree",
            "-r",
            "--name-only",
            "develop",
            "--",
            ".planning/phases/",
        ])
        .output();
    let Ok(out) = output else { return true };
    if !out.status.success() {
        return true;
    }
    String::from_utf8_lossy(&out.stdout).lines().any(|path| {
        path.strip_prefix(&prefix)
            .is_some_and(|rest| rest.contains('/') && rest.ends_with(suffix))
    })
}

/// Build the fresh [`State`] a `devflow start` begins with, carrying the
/// per-phase Validate-failure total forward from any persisted state for the
/// SAME phase (999.78/A-11, D-07).
///
/// **Why exactly one field is treated differently from every other counter.**
/// `State::new` zeroes all of them, and `start()` calls it unconditionally —
/// `--force` included. A plain `State` field is therefore per-RUN, while D-07
/// specifies a per-PHASE bound, and those are different lifetimes: a bound a
/// restart resets does not bound the unattended case the bound was built for.
/// Everything else on `State` genuinely is per-run and must keep starting at
/// zero, so this copies `phase_validate_failures` and nothing else — a
/// wholesale copy would silently resurrect a stale streak, a stale baseline
/// and a stale stop point along with it.
///
/// An **absent** persisted state means zero, which is the correct reading for
/// both cases that produce it: a phase's genuine first start, and a phase
/// whose completion already cleared the file.
///
/// An **unreadable** one is a third case and does not mean zero (WR-02,
/// 35-REVIEW). A state file that exists but does not deserialize — hand-edited,
/// truncated by a full disk, or written by a future schema — carries a total
/// this function cannot see, and treating it as zero hands the phase a fresh
/// full budget silently, defeating the bound whose entire purpose is to survive
/// a restart. The total still restarts at zero, because there is nothing else
/// it could do, but the operator is told so rather than left to infer it.
///
/// **The two events that DO reset the total**, neither of which is "a new
/// process started":
///
/// 1. **Phase completion** — `finish_workflow_with_gate_timeout` calls
///    `workflow::clear_state`, deleting `.devflow/state-{NN}.json`, so the
///    next start for this phase finds nothing to carry. No code here.
/// 2. **Operator approval at the ceiling gate** — `handle_validate_outcome`
///    zeroes the total when a human advances or loops back AND
///    `mode::phase_failure_ceiling_reached` is true.
pub(crate) fn fresh_state_carrying_phase_failures(
    project_root: &Path,
    phase: PhaseId,
    agent: AgentKind,
    mode: Mode,
) -> State {
    let mut state = State::new(phase, agent, mode, project_root.to_path_buf());
    let (carried, warning) =
        carried_phase_failures(phase, workflow::load_state(project_root, phase));
    state.phase_validate_failures = carried;
    if let Some(warning) = warning {
        println!("{warning}");
    }
    state
}

/// The three-way carry-forward decision, split from its I/O so each case can
/// be asserted without reaching for a stdout capture (WR-02, 35-REVIEW).
///
/// This was `if let Ok(persisted) = load_state(..)`, which discarded every
/// `WorkflowError` identically. `load_state` returns `MissingState` for an
/// absent file and a `serde_json` error for one that exists but does not
/// deserialize, and only the first means "no failures recorded". The second
/// silently handed the phase a fresh full budget — defeating, with no operator
/// signal at all, the bound whose entire purpose is to survive a restart.
///
/// The corrupt case still yields zero, because the total it should have
/// carried is exactly what could not be read. What changes is that the
/// operator is told.
fn carried_phase_failures(
    phase: PhaseId,
    loaded: Result<State, workflow::WorkflowError>,
) -> (u32, Option<String>) {
    match loaded {
        Ok(persisted) => (persisted.phase_validate_failures, None),
        // Genuine zero: a phase's first start, or one whose completion already
        // cleared the file.
        // Genuine zero: a phase's first start, or one whose completion already
        // cleared the file.
        Err(workflow::WorkflowError::MissingState(_)) => (0, None),
        Err(err) => (
            0,
            Some(format!(
                "warning: phase {phase} state could not be read ({err}) — the per-phase \
                 Validate-failure budget restarts at zero"
            )),
        ),
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn start(
    project_root: &Path,
    phase: PhaseId,
    agent: AgentKind,
    mode: Mode,
    force: bool,
    worktree: bool,
    dry_run: bool,
    until: Option<Stage>,
    yes_ship: bool,
    legacy_claude_launch: bool,
) -> Result<(), CliError> {
    // 999.78/A-11: NOT a bare `State::new`. See
    // `fresh_state_carrying_phase_failures` for why one field survives a
    // forced restart and every other counter does not.
    let mut state = fresh_state_carrying_phase_failures(project_root, phase, agent, mode);
    state.stop_until = until;
    // The only assignment in the crate that ever sets `yes_ship` to a
    // non-default value. Provenance (D-12, `28-CONTEXT.md`): the typed
    // `--yes-ship` CLI flag OR a standing `yes_ship = true` in
    // `devflow.toml`, combined here with logical OR since the flag has no
    // negative form. Combined before the first `save_state` below, so the
    // persisted authorization exists before the detached monitor that will
    // later consult it is ever spawned. `run_gate_with_timeout` must never
    // re-derive this from `state` itself — see its own comment.
    let config_yes_ship = config::yes_ship(project_root);
    // D-12's compensating control: a standing default is never silent. Only
    // fires when config alone supplied the authorization — a typed flag
    // needs no explanation of where it came from.
    if config_yes_ship && !yes_ship {
        println!(
            "note: Ship gate pre-authorized by devflow.toml (yes_ship = true) — see D-12, 28-CONTEXT.md"
        );
    }
    state.yes_ship = yes_ship || config_yes_ship;

    // D-11's opt-out (31-04), combined on the same shape and for the same
    // reasons as `yes_ship` directly above: OR-ed with an environment override
    // rather than replacing it, before the first `save_state` below so the
    // persisted value exists before any detached monitor consults it, and with
    // a notice when the environment ALONE supplied it — a standing default is
    // never a silent one.
    if crate::pipeline_launch::apply_legacy_launch_opt_out(&mut state, legacy_claude_launch) {
        println!(
            "note: legacy Claude launch forced by DEVFLOW_CLAUDE_LEGACY_LAUNCH \
             (D-11, 31-CONTEXT.md) — a persisted default is never a silent one"
        );
    }

    if dry_run {
        print_dry_run(&state);
        return Ok(());
    }

    // 14-CR-05: fail on a missing agent binary BEFORE any branch/worktree is
    // scaffolded (launch_stage re-checks for the advance-time launch paths).
    ensure_agent_binary(agent_program(agent))?;

    // 25e (999.51/D-18a): before even asking whether phase N is reachable,
    // make sure the base branch itself is current with its remote. A stale
    // base is the single most common cause of a phase heading appearing
    // absent — 999.51 recorded local `develop` sitting 21 commits behind
    // `origin/develop` while the phase heading existed only on the remote —
    // and the DANGEROUS variant is silent: a stale local `develop` that
    // still happens to carry the heading passes the reachability guard
    // below and forks a green run from stale code. Ordering here is
    // load-bearing for the same reason the comment below already gives for
    // the Codex leg: running reachability first would misdiagnose the cause
    // (base is stale) as the symptom (phase not found). `ensure_base_ref_current`
    // fast-forwards a safely-behind base and proceeds unattended, or refuses
    // loudly on divergence / an unsafe fast-forward (see that function's own
    // doc comment for the full decision).
    ensure_base_ref_current(project_root, DEVELOP)?;

    // 23f (gap closure, 23-12): refuse before ANY git mutation when phase N
    // is not reachable from DEVELOP — the exact branch `ensure_phase_worktree`
    // passes to `worktree::add` as `start_point`, so the branch this guard
    // inspects and the branch the run forks can never disagree. Precedes
    // BOTH fork paths (`ensure_phase_worktree` below, and `GitFlow::feature_start`
    // in the `else` branch), and precedes the Codex leg deliberately: if
    // phase N is absent from `develop` entirely, "no CONTEXT.md on develop"
    // is a narrower and misleading diagnosis of the same root fact.
    ensure_phase_reachable_on_base(project_root, phase, DEVELOP)?;

    // 999.106 driver-driven pre-flight: whether a fresh headless run can pass
    // a stage is declared by the driver's `interactivity_mode`, not a
    // hardcoded `agent == Codex`. A `RequiresExistingArtifact` Define must
    // have CONTEXT.md on develop; a `RequiresExistingArtifact` Plan warns
    // without a PLAN.md. Fail in one second with instructions instead of after
    // a burned agent run and a dead-end gate. Checked on `develop` (the
    // branch worktrees fork from), so the result does not depend on what the
    // primary checkout happens to have checked out.
    let driver = agents::driver_for(agent);
    if driver.interactivity_mode(Stage::Define)
        == agents::InteractivityMode::RequiresExistingArtifact
        && !phase_artifact_on_develop(project_root, phase, "-CONTEXT.md")
    {
        return Err(CliError::Message(format!(
            "phase {phase} has no CONTEXT.md on develop, and {} cannot run an \
             interactive discussion headless. Run /gsd-discuss-phase {phase} \
             interactively first (any agent), or use --agent claude.",
            driver.name()
        )));
    }
    if driver.interactivity_mode(Stage::Plan) == agents::InteractivityMode::RequiresExistingArtifact
        && !phase_artifact_on_develop(project_root, phase, "-PLAN.md")
    {
        println!(
            "warning: phase {phase} has no PLAN.md on develop — headless {} \
             planning is untested and may need input; pre-writing plans is safer",
            driver.name()
        );
    }

    // Pre-start divergence check: runs on current HEAD before any git
    // mutation. WR-10 (13-REVIEW.md): only meaningful for the --no-worktree
    // (branch-in-place) flow, where `start` actually branches from the main
    // checkout's current HEAD. In worktree mode (the default) the agent's
    // work always forks fresh from `develop` via `worktree::add`, independent
    // of whatever happens to be checked out in the main repo — checking the
    // main checkout's divergence there is unrelated to what's about to
    // happen and can either hard-fail on a stale unrelated branch or
    // silently no-op if the main checkout happens to be on develop.
    if !worktree && let Ok((_ahead, behind)) = GitFlow::new(project_root).divergence_from_develop()
    {
        if behind > 50 {
            return Err(CliError::Message(format!(
                "develop is {behind} commits ahead — your branch is too far behind. \
                 Rebase onto develop first, or use --force to override."
            )));
        }
        if behind > 10 {
            println!("warning: develop is {behind} commits ahead — consider rebasing first");
        }
    }

    if worktree {
        let wt = ensure_phase_worktree(project_root, phase, force)?;
        println!(
            "created worktree: {} (branch {FEATURE_PREFIX}phase-{padded})",
            wt.display(),
            padded = phase.padded(),
        );
        state.worktree_path = Some(wt);
    } else {
        let git = GitFlow::new(project_root);
        let result = if force {
            git.feature_start_force(phase)
        } else {
            git.feature_start(phase)
        };
        match result {
            Ok(branch) => println!("created feature branch: {branch}"),
            Err(err) => {
                if !force {
                    return Err(CliError::Message(format!(
                        "{err}\nUse --force to overwrite the existing branch."
                    )));
                }
                return Err(err.into());
            }
        }
    }

    // 35.1 D-01, `start`'s half: repair a leaked
    // `workflow._auto_chain_active` before anything is spawned.
    //
    // PLACEMENT IS LOAD-BEARING in both directions. It sits AFTER the
    // `if worktree { ... }` fork above, because that fork is what creates the
    // worktree and sets `state.worktree_path` — the repair targets the copy of
    // `.planning/config.json` the agent will actually read, and before the fork
    // that copy does not exist (F-10). It sits BEFORE the first
    // `workflow::save_state` below, matching this function's own
    // "combine before the first save_state" idiom for `yes_ship` and D-11's
    // opt-out.
    //
    // Why `start` needs this at all, and not just `resume`: a freshly forked
    // worktree inherits whatever `develop` carries. A leak that already reached
    // the base branch — through Ship, or any other route — arrives in every new
    // phase's worktree, and this call site is the one that catches it there.
    // See `pipeline_launch::repair_leaked_auto_chain_flag` for the D-01/D-03
    // reasoning in full. (35.1's `D-` numbers, a different sequence from phase
    // 31's D-11 cited above.)
    let launch_root: PathBuf = state
        .worktree_path
        .clone()
        .unwrap_or_else(|| project_root.to_path_buf());
    crate::pipeline_launch::repair_leaked_auto_chain_flag(
        project_root,
        &launch_root,
        phase,
        crate::pipeline_launch::AUTO_CHAIN_REPAIR_FROM_START,
    );

    // 999.79 (35-05, A-05): record what this phase's `{N}-VERIFICATION.md`
    // looked like BEFORE this run's Validate agent has had any opportunity to
    // rewrite it. `handle_validate_outcome`'s loop-back selector compares the
    // artifact against this baseline: unchanged means it was inherited from a
    // previous run and its verdict must not be reused; changed (or newly
    // present) means the Validate agent authored it during this run.
    //
    // Nothing deletes, dates or invalidates the artifact, and a
    // `devflow start --phase N --force` checks out a branch that still carries
    // the previous run's committed copy. Without this baseline that re-run —
    // mid-arc by construction — reads the inherited artifact as a verdict and
    // dispatches a `--gaps-only` pass against zero matching plans, gating
    // unresolvably.
    //
    // PLACEMENT IS LOAD-BEARING, and this is A-05's whole point: the capture
    // sits AFTER the `if worktree { ... }` fork above, where
    // `state.worktree_path` holds its final value for this run. The artifact
    // lives under the EVIDENCE ROOT, and in worktree mode that directory is
    // created by `ensure_phase_worktree` inside that fork — it does not exist
    // when `fresh_state_carrying_phase_failures` builds the state near the top
    // of this function. Capturing there would record "absent" for every
    // worktree run and make the very first freshness check read as fresh,
    // which is the failure direction this baseline exists to prevent.
    //
    // The evidence root is resolved the same way every loop-back arm resolves
    // it — the worktree path when present, the project root otherwise — and
    // NOT unconditionally as the project root. `.planning/` is tracked and the
    // Validate agent runs in the worktree, so the artifact lands on
    // `feature/phase-{N}` inside the worktree and is invisible from the main
    // checkout; reading the project root here is exactly the defect plan 33-05
    // closed (CR-01) and must not be reintroduced. Persisted by the
    // `workflow::save_state` below rather than by a second write.
    let evidence_root: PathBuf = state
        .worktree_path
        .clone()
        .unwrap_or_else(|| project_root.to_path_buf());
    state.last_verification_fingerprint =
        agent_result::phase_verification_fingerprint(&evidence_root, phase);
    // WR-06: read from the SAME evidence root in the same breath, so the pair
    // is one observation of one file rather than two readings that could
    // disagree about which artifact they saw.
    state.last_verification_mtime_nanos =
        agent_result::phase_verification_mtime_nanos(&evidence_root, phase);
    // WR-05: the observation happened. Without this flag a `None` fingerprint
    // from an old state file is indistinguishable from a `None` that means
    // "looked, found nothing", and the two demand opposite dispatches.
    state.verification_baseline_captured = true;

    // 25b (D-03): the self-dogfood build-staleness gate, hoisted here from
    // `pipeline_launch::launch_stage_inner` (17d originally placed it there,
    // re-running it before every stage launch — which meant a phase that
    // modified DevFlow's own source could never progress past the first
    // stage boundary once its own diff tripped the guard). Adjudicated
    // exactly once per run, here, so it evaluates once and the rest of the
    // pipeline never re-asks the question.
    //
    // Placement is load-bearing: this call sits AFTER `state.worktree_path`
    // is set by the `if worktree { ... }` branch above, so
    // `enforce_build_staleness` evaluates against the phase's own worktree
    // HEAD — not the main checkout, which is what would happen if this ran
    // any earlier. The block message explicitly promises the operator the
    // check runs against "this phase's WORKTREE HEAD, not the main
    // checkout"; placing the call above the worktree fork would silently
    // break that promise.
    //
    // Placement is also deliberately BEFORE `workflow::save_state` below: a
    // refusal here must never leave persisted state behind for a run that
    // is not going to start (mirroring the same "don't persist a run that
    // will never launch" reasoning `ensure_agent_binary` and
    // `ensure_phase_reachable_on_base` already follow earlier in this
    // function).
    //
    // D-04 (accepted trade, recorded here rather than hidden): this hoist
    // means `pipeline_launch::resume` (used after a rate-limit or infra
    // pause) no longer re-adjudicates staleness mid-run — a *different*
    // binary resuming a phase is never re-checked. This is accepted because
    // the scenario is already forbidden by 999.48's rejected alternatives
    // and the operator's standing 2026-07-27 position that only validated,
    // pushed code should ever drive a run. If this trade proves wrong, the
    // reversal path is a persisted `staleness_pin` field on `State`,
    // re-checked at resume — not reintroduced here.
    //
    // D-05 (not weakened): the check itself is relocated, not removed or
    // softened. `is_self_dogfood_workspace` still gates the whole module on
    // this repository's exact workspace shape and is unmodified by this
    // plan. Neither of 999.48's rejected alternatives — rebuilding the
    // driving binary mid-run, or a dogfood bypass flag — is introduced here
    // or anywhere else in this plan's diff.
    enforce_build_staleness(
        project_root,
        &state,
        env!("DEVFLOW_BUILD_COMMIT"),
        env!("DEVFLOW_BUILD_DIRTY") == "true",
    )?;

    // WR-11 (13-REVIEW.md), revised: state must be on disk BEFORE the monitor
    // exists. launch_stage spawns the detached monitor, which runs `devflow
    // advance` the moment the agent exits — and advance begins with
    // load_state. Launching first (the previous WR-11 order) raced a
    // fast-exiting agent against this save: the monitor's advance found no
    // state.json, died silently into /dev/null, and the save below then wrote
    // an in-progress state nothing would ever advance. Save first; if the
    // launch fails, clear the just-saved state so `devflow status`/`recover`
    // don't report a phantom run (the failure WR-11 originally targeted).
    workflow::save_state(&state)?;
    events::emit(
        project_root,
        phase,
        "workflow_started",
        workflow_started_payload(&state),
    );
    if let Err(err) = launch_stage(&mut state, None, None) {
        if let Err(clear_err) = workflow::clear_state(project_root, phase) {
            eprintln!("warning: could not clear state after failed launch: {clear_err}");
        }
        return Err(err);
    }
    println!(
        "started phase {} in {mode} mode at {} — monitor will auto-advance",
        state.phase, state.started_at
    );
    println!("  watch live: devflow logs -f --phase {phase}");
    Ok(())
}

// ---------------------------------------------------------------------------
// 17d: build provenance + self-dogfood staleness gate (D-17-D-21).
// ---------------------------------------------------------------------------

/// D-21: the `workflow_started` event payload, including build provenance —
/// factored out of `start()` so the payload shape is directly unit-testable
/// without spawning a real agent (`start()` calls `launch_stage` immediately
/// after emitting this event).
fn workflow_started_payload(state: &State) -> serde_json::Value {
    serde_json::json!({
        "agent": state.agent.to_string(),
        "mode": state.mode.to_string(),
        "worktree": state.worktree_path.as_ref().map(|p| p.display().to_string()),
        "version": env!("CARGO_PKG_VERSION"),
        "commit": env!("DEVFLOW_BUILD_COMMIT"),
        "dirty": env!("DEVFLOW_BUILD_DIRTY"),
        // WR-02: filename only, never the full path (leaks home dir/username
        // into OPERATIONS.md's tail-and-paste file); to_string_lossy (not
        // to_str) so non-UTF-8 names still yield a string, not null.
        "exe_path": std::env::current_exe()
            .ok()
            .and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned())),
    })
}

// ---------------------------------------------------------------------------
// reference / cleanup / list / status / recover
// ---------------------------------------------------------------------------

/// Create or refresh the static reference worktree.
pub(crate) fn reference(
    project_root: &Path,
    branch: Option<String>,
    refresh: bool,
) -> Result<(), CliError> {
    let branch = branch.unwrap_or_else(|| DEVELOP.to_string());
    let path = worktree::reference_path(project_root);

    // Detached snapshot: `branch` may already be checked out in the main
    // worktree, so we pin a detached HEAD at its tip rather than checking it out.
    if path.exists() {
        if !refresh {
            println!(
                "reference exists at {} (use --refresh to update it)",
                path.display()
            );
            return Ok(());
        }
        worktree::remove(project_root, &path, true)?;
        worktree::add_detached(project_root, &path, &branch)?;
        println!(
            "refreshed reference worktree at {} (snapshot of {branch})",
            path.display()
        );
    } else {
        worktree::add_detached(project_root, &path, &branch)?;
        println!(
            "created reference worktree at {} (snapshot of {branch})",
            path.display()
        );
    }
    Ok(())
}

/// Parse the phase number encoded in a `.worktrees/phase-NN[-agent]` path.
/// Used only as a fallback join key when no persisted `State.worktree_path`
/// matches the worktree entry (review: Codex MEDIUM — worktree->phase join).
/// Returns `None` for paths that don't follow this naming (e.g. the static
/// `reference` worktree), which correctly excludes it from the liveness
/// guard — a snapshot has no owning phase/agent to be alive.
fn phase_from_worktree_path(worktrees_dir: &Path, path: &Path) -> Option<PhaseId> {
    let name = path.strip_prefix(worktrees_dir).ok()?.to_str()?;
    let rest = name.strip_prefix("phase-")?;
    // A dot is part of the identifier (`phase-35.1`), so it must be consumed
    // here; stopping at the first non-digit would read `phase-35.1` as phase
    // 35 and join a decimal phase's worktree to its integer sibling's state.
    // The agent suffix (`phase-07-claude`) still terminates the run.
    let label: String = rest
        .chars()
        .take_while(|c| c.is_ascii_digit() || *c == '.')
        .collect();
    label.parse().ok()
}

/// Join a `git worktree list` entry to its owning phase `State`, preferring
/// the persisted `worktree_path` (set by `start`/`parallel`) and falling back
/// to worktree-directory-name or branch-name matching only when no
/// `worktree_path` match exists (review: Codex MEDIUM). Returns `None` when
/// no owning state can be found at all (e.g. the phase already shipped and
/// its state was cleared) — callers treat that as "no liveness signal",
/// not as an implicit "safe to remove."
fn state_for_worktree<'a>(
    states: &'a [State],
    worktrees_dir: &Path,
    wt: &worktree::WorktreeInfo,
) -> Option<&'a State> {
    if let Some(state) = states
        .iter()
        .find(|s| s.worktree_path.as_deref() == Some(wt.path.as_path()))
    {
        return Some(state);
    }
    if let Some(phase) = phase_from_worktree_path(worktrees_dir, &wt.path)
        && let Some(state) = states.iter().find(|s| s.phase == phase)
    {
        return Some(state);
    }
    if let Some(branch) = &wt.branch {
        return states
            .iter()
            .find(|s| *branch == format!("{FEATURE_PREFIX}phase-{}", s.phase.padded()));
    }
    None
}

/// Bounded-backoff retry around `worktree::remove`, absorbing the transient
/// `Directory not empty` race that can occur even after a phase is confirmed
/// dead (a lingering fd/writer from the just-exited agent). NOT a substitute
/// for the liveness guard above — only reached once a phase is confirmed
/// dead (agent dead AND monitor not active). `git worktree prune` is
/// deliberately not used here: it only clears metadata for already-absent
/// directories and would orphan leftover files on disk (Pitfall 3).
fn remove_worktree_with_retry(
    project_root: &Path,
    path: &Path,
    force: bool,
) -> Result<(), worktree::WorktreeError> {
    const ATTEMPTS: u32 = 3;
    const BASE_DELAY_MS: u64 = 50;
    let mut last_err = None;
    for attempt in 0..ATTEMPTS {
        match worktree::remove(project_root, path, force) {
            Ok(()) => return Ok(()),
            Err(err) => {
                last_err = Some(err);
                if attempt + 1 < ATTEMPTS {
                    std::thread::sleep(std::time::Duration::from_millis(
                        BASE_DELAY_MS * 2u64.pow(attempt),
                    ));
                }
            }
        }
    }
    Err(last_err.expect("loop runs ATTEMPTS >= 1 times"))
}

/// Remove phase worktrees (and the reference with --force), deleting their
/// associated feature branches, then prune and clean up merged branches.
///
/// Hard-refuses (D-06, no override flag) removal of any worktree whose owning
/// phase has a live agent (any monitor state, including Unknown/Stuck) or an
/// active monitor (Healthy/BetweenStages) — closing the race where a real
/// `cleanup --force` run could delete a worktree a live agent/monitor is
/// still writing into (review: Codex HIGH, fail-closed on a live agent).
pub(crate) fn cleanup(project_root: &Path, force: bool) -> Result<(), CliError> {
    let git = GitFlow::new(project_root);
    let worktrees_dir = worktree::worktrees_dir(project_root);
    let reference = worktree::reference_path(project_root);
    let states = workflow::list_states(project_root);

    let worktrees = worktree::list(project_root)?;
    let mut removed = 0usize;
    for wt in &worktrees {
        // Only touch worktrees under `.worktrees/` (never the main checkout).
        if !wt.path.starts_with(&worktrees_dir) {
            continue;
        }
        if wt.path == reference && !force {
            println!("keeping reference worktree (use --force to remove it)");
            continue;
        }

        let matched_state = state_for_worktree(&states, &worktrees_dir, wt);
        let phase = matched_state
            .map(|s| s.phase)
            .or_else(|| phase_from_worktree_path(&worktrees_dir, &wt.path));
        let agent_alive = phase
            .and_then(|p| agent_pid_from_file(project_root, p))
            .is_some_and(agent::agent_running);
        let monitor_pid = matched_state.and_then(|s| s.monitor_pid);
        let monitor_alive = monitor_pid.is_some_and(agent::agent_running);
        let phase_liveness = liveness(monitor_pid, monitor_alive, agent_alive);

        // A phase halted via `devflow start --until <stage>` (20c) clears
        // `monitor_pid` and its agent has already exited by design — that
        // reads as `Liveness::Unknown` with `agent_alive == false`, which
        // would otherwise sail straight through the live-agent refusal
        // below. Treat it the same way `doctor`'s `check_dead_agent`/
        // `check_dead_monitor` were taught about `facts.stopped` in this
        // same phase: an intentionally-parked worktree is never implicitly
        // safe to remove — require `--force`, mirroring the `reference`
        // worktree's own precedent above.
        let stopped = matched_state.is_some_and(|s| s.stopped);
        if stopped && !force {
            let phase_label = phase
                .map(|p| p.to_string())
                .unwrap_or_else(|| "?".to_string());
            println!(
                "keeping worktree {} for phase {phase_label} — halted via --until; run `devflow resume --phase {phase_label}` first, or pass --force to discard it",
                wt.path.display()
            );
            continue;
        }

        // Fail-closed on a live agent: refuse whenever the agent is alive
        // (regardless of monitor liveness — Unknown/Stuck included) OR the
        // monitor is actively running the stage (Healthy/BetweenStages).
        // Only Stuck/Unknown WITHOUT a live agent proceeds.
        if agent_alive || matches!(phase_liveness, Liveness::Healthy | Liveness::BetweenStages) {
            let phase_label = phase
                .map(|p| p.to_string())
                .unwrap_or_else(|| "?".to_string());
            return Err(CliError::Message(format!(
                "refusing to remove worktree {} for phase {phase_label} ({}) — run `devflow resume --phase {phase_label}` or wait for it to finish",
                wt.path.display(),
                phase_liveness.describe(),
            )));
        }

        match remove_worktree_with_retry(project_root, &wt.path, force) {
            Ok(()) => {
                print!("removed worktree {}", wt.path.display());
                match &wt.branch {
                    Some(branch) if branch.starts_with(FEATURE_PREFIX) => {
                        match git.delete_branch(branch, force) {
                            Ok(()) => println!(" + deleted branch {branch}"),
                            Err(err) => println!(" (branch {branch} kept: {err})"),
                        }
                    }
                    _ => println!(),
                }
                removed += 1;
            }
            Err(err) => {
                println!(
                    "warning: could not remove worktree {} after retrying — manually delete this directory: {err}",
                    wt.path.display()
                );
            }
        }
    }

    worktree::prune(project_root)?;
    if removed == 0 {
        println!("no worktrees to clean up");
    }
    match git.cleanup_merged() {
        Ok(merged) => {
            for branch in merged {
                println!("deleted merged branch {branch}");
            }
        }
        Err(err) => println!("warning: could not prune merged branches: {err}"),
    }
    Ok(())
}

/// A phase's monitor/agent liveness, distinguishing a dead monitor (nothing
/// will call `devflow advance` when the agent exits) from a normal
/// between-stages moment (18b — "who watches the watcher").
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Liveness {
    /// Monitor and agent are both alive — the stage is actively running.
    Healthy,
    /// Monitor is alive, agent has exited — normal between-stages moment;
    /// the monitor will advance the phase shortly.
    BetweenStages,
    /// The recorded monitor is dead. Whether or not the agent is also dead,
    /// nothing will call `devflow advance` for this phase — it needs a
    /// manual `devflow resume`.
    Stuck,
    /// No monitor PID has been recorded for this state — either none has
    /// been spawned yet, or the state was written by a binary predating
    /// this field. Never reported as a problem.
    Unknown,
}

impl Liveness {
    pub(crate) fn describe(self) -> &'static str {
        match self {
            Liveness::Healthy => "healthy",
            Liveness::BetweenStages => "between stages",
            Liveness::Stuck => "stuck — needs devflow resume",
            Liveness::Unknown => "unknown (no monitor recorded)",
        }
    }
}

/// Pure liveness predicate — no I/O. `monitor_pid` is matched `None` first
/// so a state written by a pre-18b binary (carrying no `monitor_pid`) can
/// never be misclassified as `Stuck` (T-18-11).
fn liveness(monitor_pid: Option<u32>, monitor_alive: bool, agent_alive: bool) -> Liveness {
    match monitor_pid {
        None => Liveness::Unknown,
        Some(_) => match (monitor_alive, agent_alive) {
            (true, true) => Liveness::Healthy,
            (true, false) => Liveness::BetweenStages,
            (false, _) => Liveness::Stuck,
        },
    }
}

/// Recovery verbs discoverable from a phase's liveness (21a, D-03) — the
/// pure, testable counterpart to `status`'s old inline Stuck `println!`.
/// Always includes `devflow resume` for `Stuck`; additionally includes
/// `devflow advance` when the phase is gate-pending (the operator answers
/// the gate then advances — the primary footgun this closes; widening the
/// predicate further risks suggesting `advance` where nothing proves it is
/// right, per 21-CONTEXT.md's Review Incorporation). Empty for any other
/// liveness, so a healthy/between-stages/unknown phase prints nothing new.
fn recovery_hints(state: &State, liveness: Liveness) -> Vec<String> {
    if liveness != Liveness::Stuck {
        return Vec::new();
    }
    let mut hints = vec![format!("devflow resume --phase {}", state.phase)];
    if state.gate_pending {
        hints.push(format!("devflow advance --phase {}", state.phase));
    }
    hints
}

/// `status`'s in-stage progress line: real elapsed time since the phase's
/// most recent `stage_launched` event. `None` (no such event yet) renders
/// the stage name with no age, rather than mislabeling phase age as stage
/// age — never pass `state.started_at`'s age here (3/3 review MEDIUM).
fn render_stage_progress_line(stage: Stage, stage_launched_ts: Option<u64>) -> String {
    match stage_launched_ts {
        Some(ts) => format!(
            "  in stage {stage}: {}",
            recover::format_age(&ts.to_string())
        ),
        None => format!("  in stage {stage}"),
    }
}

pub(crate) fn status(project_root: &Path) -> Result<(), CliError> {
    // 13-DEFERRED-CR-03 acceptance: enumerate every active phase, not just
    // the last one started.
    let states = workflow::list_states(project_root);
    let mut current_worktree: Option<PathBuf> = None;
    if states.is_empty() {
        println!("stage: idle");
        println!("project_root: {}", project_root.display());
    } else {
        // 14-CR-10: one pass over events.jsonl for every phase's last event,
        // instead of a full-file scan per phase.
        let mut last_events = events::last_events_by_phase(project_root);
        println!("project_root: {}", project_root.display());
        println!(
            "active phases: {}",
            states
                .iter()
                .map(|s| s.phase.to_string())
                .collect::<Vec<_>>()
                .join(", ")
        );
        for state in &states {
            let gate = if state.gate_pending {
                "pending"
            } else {
                "none"
            };
            println!("\nphase {}:", state.phase);
            println!(
                "  stage: {} | mode: {} | gate: {}",
                state.stage, state.mode, gate
            );
            println!("  agent: {}", agents::driver_for(state.agent).name());
            if state.consecutive_failures > 0 {
                println!("  validate failures: {}", state.consecutive_failures);
            }
            println!(
                "  started: {} ({})",
                state.started_at,
                recover::format_age(&state.started_at)
            );
            if let Some(ref wt) = state.worktree_path {
                println!("  worktree: {}", wt.display());
            }
            current_worktree = current_worktree.or_else(|| state.worktree_path.clone());
            let agent_pid = agent_pid_from_file(project_root, state.phase);
            match agent_pid {
                Some(pid) => {
                    println!(
                        "  agent_pid: {pid} (running: {})",
                        agent::agent_running(pid)
                    );
                }
                None => println!("  agent_pid: none"),
            }
            match state.monitor_pid {
                Some(pid) => {
                    println!(
                        "  monitor_pid: {pid} (running: {})",
                        agent::agent_running(pid)
                    );
                }
                None => println!("  monitor_pid: none"),
            }
            let agent_alive = agent_pid.is_some_and(agent::agent_running);
            let monitor_alive = state.monitor_pid.is_some_and(agent::agent_running);
            let phase_liveness = liveness(state.monitor_pid, monitor_alive, agent_alive);
            println!("  liveness: {}", phase_liveness.describe());
            let phase_summary = last_events.remove(&state.phase);
            println!(
                "{}",
                render_stage_progress_line(
                    state.stage,
                    phase_summary.as_ref().and_then(|s| s.stage_launched_ts)
                )
            );
            for hint in recovery_hints(state, phase_liveness) {
                println!("{hint}");
            }
            if let Some(summary) = phase_summary {
                let ago = summary
                    .event
                    .get("ts")
                    .and_then(|t| t.as_u64())
                    .map(|t| format!(" ({})", recover::format_age(&t.to_string())))
                    .unwrap_or_default();
                println!("  last action: {}{ago}", events::describe(&summary.event));
            }
        }
    }
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    if let Some(banner) = render_pending_gate_banner(&Gates::list_open(project_root), now) {
        println!("\n{banner}");
    }
    print_open_branches(project_root);
    print_worktrees(project_root, current_worktree.as_deref());
    for hint in cron_instruction_hints(project_root) {
        println!("\n{hint}");
    }
    Ok(())
}

/// Report DevFlow's own structural record of whether `phase` shipped
/// (23-06) — reads `devflow_core::ship_evidence::collect`, a strictly
/// read-only oracle sourced from the append-only event log, never from an
/// agent-authored attestation document.
///
/// **Three dead-end predicates/placements this plan disproved. Documented
/// here so the next person reading this code does not re-derive and
/// re-attempt them:**
///
/// 1. **Pre-gate merge check.** `hooks_after_ship` (`hooks.rs:105-112`) only
///    runs from `finish_workflow_with_gate_timeout`, itself only reached from
///    `handle_ship_outcome`'s `GateAction::Advance` arm — i.e. AFTER the Ship
///    gate has already been approved. A check for a merge/tag/push placed
///    before gate approval would fail for every legitimate Ship; RESEARCH.md's
///    Question C / Pattern 3 recommendation to place it there was verified
///    wrong against live source.
/// 2. **Post-batch ancestry check.** `BranchCleanup` runs immediately after
///    `Merge` in that same `hooks_after_ship` batch and deletes the feature
///    branch. `GitFlow::is_merged_into_develop` fails closed on an absent
///    branch (`git.rs:89-97`: "an absent branch is not proof of a merge"), so
///    an ancestry check run after the batch completes returns `false` for
///    EVERY successfully shipped phase.
/// 3. **`workflow_finished` as the shipped predicate.** Emitted at TWO
///    sites: real Ship finalization, and `transition`'s `devflow start
///    --until <stage>` clean-stop branch
///    (`crates/devflow-cli/src/pipeline_gate.rs:67`, the
///    `state.stop_until == Some(from)` arm near the top of `transition`),
///    which returns with payload `{"reason": "stopped_at", …}` BEFORE any
///    checkout hook, before `state.stage = to`, before the `"transition"`
///    event, and before `launch_stage` runs — nothing resembling a Ship has
///    happened. A phase halted after one stage would read as shipped. This
///    is the dead end most likely to be reintroduced: a cross-AI review
///    caught it after it had already been written into this plan three
///    times as "the only site emitting `workflow_finished`."
pub(crate) fn evidence(
    project_root: &Path,
    phase: PhaseId,
    json: bool,
    require_shipped: bool,
) -> Result<(), CliError> {
    let evidence = ship_evidence::collect(project_root, phase);

    if json {
        println!(
            "{}",
            serde_json::to_string_pretty(&evidence).expect("ShipEvidence must serialize")
        );
    } else {
        println!("phase: {}", evidence.phase);
        println!("shipped: {}", evidence.shipped);
        println!(
            "workflow_finished_seen: {}",
            evidence.workflow_finished_seen
        );
        println!(
            "finished_reason: {}",
            evidence.finished_reason.as_deref().unwrap_or("none")
        );
        println!(
            "stage: {}",
            evidence
                .stage
                .map(|s| s.to_string())
                .unwrap_or_else(|| "none".into())
        );
        println!("state_present: {}", evidence.state_present);
        println!("feature_branch_exists: {}", evidence.feature_branch_exists);
        println!("merged_into_develop: {}", evidence.merged_into_develop);
        println!("has_remote: {}", evidence.has_remote);
    }

    if require_shipped && !evidence.shipped {
        // "It finished but it did not ship" is the confusing case a reader
        // hits first — say so explicitly rather than a generic "not shipped"
        // (Task 1 acceptance criteria).
        let detail = if ship_evidence::is_stopped_at(&evidence) {
            format!(
                "phase {phase} has not shipped — DevFlow's own record shows it stopped after \
                 one stage (--until) rather than reaching a finalized Ship"
            )
        } else {
            format!("phase {phase} has not shipped — DevFlow has no record of a completed Ship")
        };
        return Err(CliError::Message(detail));
    }

    Ok(())
}

/// Build the persistent status-side signal for gates awaiting an operator.
/// Context is agent-controlled, so it must use the same bounded rendering as
/// gate notifications and failure events.
fn render_pending_gate_banner(open: &[OpenGate], now: u64) -> Option<String> {
    if open.is_empty() {
        return None;
    }

    let mut banner = String::from("==================== PENDING GATE ====================\n");
    for gate in open {
        let timestamp = gate.timestamp.parse::<u64>().ok();
        let escalated = timestamp
            .and_then(|timestamp| now.checked_sub(timestamp))
            .is_some_and(|age| age >= GATE_ESCALATION_THRESHOLD_SECS);
        let marker = if escalated { "!!! ESCALATED" } else { "!!!" };
        let context = render_gate_context(&gate.context, 300);
        let stage = gate.stage.to_string();
        banner.push_str(&format!(
            "{marker}: phase {} {stage} ({})\n  {context}\n  approve: devflow gate approve {} --stage {stage}\n  reject:  devflow gate reject {} --stage {stage} --note <reason>\n",
            gate.phase,
            recover::format_age(&gate.timestamp),
            gate.phase,
            gate.phase,
        ));
    }
    banner.push_str("======================================================");
    Some(banner)
}

/// List every gate awaiting a human response. When `all_roots` is set,
/// answers "what is gated on this machine?" across every root this machine
/// has registered (`registry::load_roots`) in one invocation, with a
/// leading ROOT column and a per-gate age; behaviour is otherwise
/// byte-identical to the single-root listing (23-03).
pub(crate) fn gate_list(project_root: &Path, all_roots: bool) -> Result<(), CliError> {
    if all_roots {
        return gate_list_all_roots();
    }
    let open = Gates::list_open(project_root);
    if open.is_empty() {
        println!("no open gates");
        return Ok(());
    }
    println!("{:<6} {:<9} {:<9} CONTEXT", "PHASE", "STAGE", "AGE");
    for gate in &open {
        let context = render_gate_context(&gate.context, 100);
        println!(
            "{:<6} {:<9} {:<9} {context}",
            gate.phase,
            gate.stage.to_string(),
            recover::format_age(&gate.timestamp),
        );
    }
    println!(
        "\nanswer with: devflow gate approve <phase> [--note ...] | \
         devflow gate reject <phase> --note ... (note with \"abort\" ends the phase)"
    );
    Ok(())
}

/// The `--all-roots` half of [`gate_list`]: fan out `Gates::list_open`
/// across every registered root and render an additional leading ROOT
/// column. A registered root with no open gates simply contributes no rows.
fn gate_list_all_roots() -> Result<(), CliError> {
    let roots = registry::load_roots();
    let mut rows: Vec<(PathBuf, OpenGate)> = Vec::new();
    for root in &roots {
        for gate in Gates::list_open(&root.project_root) {
            rows.push((root.project_root.clone(), gate));
        }
    }
    if rows.is_empty() {
        println!("no open gates across {} registered root(s)", roots.len());
        return Ok(());
    }
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    println!("{:<6} {:<9} {:<9} ROOT / CONTEXT", "PHASE", "STAGE", "AGE");
    for (root, gate) in &rows {
        println!("{}", render_all_roots_gate_row(root, gate, now));
    }
    Ok(())
}

/// Render one `--all-roots` gate row: the PHASE/STAGE/AGE/ROOT line plus a
/// second, indented context line. Pure given `now` — the same pattern
/// `render_pending_gate_banner` already uses — so it's unit-testable
/// without mutating wall-clock time.
fn render_all_roots_gate_row(root: &Path, gate: &OpenGate, now: u64) -> String {
    let context = render_gate_context(&gate.context, 100);
    format!(
        "{:<6} {:<9} {:<9} {}\n           {context}",
        gate.phase,
        gate.stage.to_string(),
        render_gate_age(&gate.timestamp, now),
        root.display(),
    )
}

/// Render a gate's `timestamp` as a compact age for `--all-roots`, with a
/// trailing urgency marker once it reaches
/// [`GATE_ESCALATION_THRESHOLD_SECS`] — reusing the same threshold
/// `render_pending_gate_banner` escalates on, rather than inventing a
/// second one. A `timestamp` that does not parse as `u64`, or that is
/// somehow in the future relative to `now`, renders `?` — the row is still
/// listed, matching the forensics record that dropping unusual rows is
/// exactly how the orphan population stayed invisible.
fn render_gate_age(timestamp: &str, now: u64) -> String {
    let Ok(ts) = timestamp.parse::<u64>() else {
        return "?".to_string();
    };
    let Some(age) = now.checked_sub(ts) else {
        return "?".to_string();
    };
    let compact = match age {
        s if s < 60 => format!("{s}s"),
        s if s < 3600 => format!("{}m", s / 60),
        s if s < 86400 => format!("{}h", s / 3600),
        s => format!("{}d", s / 86400),
    };
    if age >= GATE_ESCALATION_THRESHOLD_SECS {
        format!("{compact}!")
    } else {
        compact
    }
}

/// Answer an open gate from the CLI — the dogfood-facing replacement for
/// hand-writing `.devflow/gates/NN-stage.response.json` (15a).
/// Resolve an omitted `--stage` to the single open gate for `phase` from an
/// already-fetched `Gates::list_open` collection — the shared no-open /
/// single-open / ambiguous-open behavior `gate_respond` and `gate_show` both
/// need, kept in one place so it cannot drift between them (Phase 21 review
/// WR-01). Callers own the `Gates::list_open` read so `gate_show` can resolve
/// and select from one fetched collection instead of reading twice (WR-03).
fn resolve_single_open_gate_stage(open: &[OpenGate], phase: PhaseId) -> Result<Stage, CliError> {
    let matching: Vec<&OpenGate> = open.iter().filter(|g| g.phase == phase).collect();
    match matching.as_slice() {
        [] => Err(CliError::Message(format!(
            "no open gate for phase {phase} — see `devflow gate list`"
        ))),
        [one] => Ok(one.stage),
        many => Err(CliError::Message(format!(
            "phase {phase} has several open gates ({}) — pass --stage",
            many.iter()
                .map(|g| g.stage.to_string())
                .collect::<Vec<_>>()
                .join(", ")
        ))),
    }
}

pub(crate) fn gate_respond(
    project_root: &Path,
    phase: PhaseId,
    stage: Option<Stage>,
    approved: bool,
    note: Option<String>,
) -> Result<(), CliError> {
    let stage = match stage {
        Some(stage) => stage,
        None => resolve_single_open_gate_stage(&Gates::list_open(project_root), phase)?,
    };
    let responded_by = std::env::var("USER")
        .ok()
        .filter(|user| !user.is_empty())
        .unwrap_or_else(|| "devflow-cli".into());
    let response = GateResponse {
        approved,
        note,
        responded_by: Some(responded_by),
    };
    let path = Gates::respond(project_root, phase, stage, &response)?;
    events::emit(
        project_root,
        phase,
        "gate_response_written",
        serde_json::json!({
            "stage": stage.to_string(),
            "approved": approved,
            "via": "cli",
        }),
    );
    let outcome = match GateAction::from_response(&response) {
        GateAction::Advance => "workflow will advance",
        GateAction::LoopBack(_) => "workflow will loop back to Code",
        GateAction::Abort(_) => "phase will abort",
    };
    println!(
        "{} gate for phase {phase} {stage}{outcome} once the waiting monitor polls it \
         (response at {})",
        if approved { "approved" } else { "rejected" },
        path.display()
    );
    Ok(())
}

/// Answer or report every aged, unattended gate across every registered root
/// (or a single `root`, when given) — the acting half of 23b's bound gate
/// lifetime. Never signals any process; `Gates::reap` (structurally
/// incapable of approving, T-23-41) is the ONLY write path, so the sweep's
/// sole effect on the world is written bytes a live poller was already
/// watching for.
///
/// `GateError::NoOpenGate`/`AlreadyResponded` are treated as benign races —
/// a human or `--yes-ship` may have answered the same gate between
/// `list_open` and `reap` — and counted as skipped rather than returned as
/// an error; `Gates::respond`'s own refusal to clobber an unconsumed
/// response is the first-writer-wins resolution that makes this safe with
/// no new coordination code. Every successful reap emits `gate_reaped` in
/// this process (the audit trail half — `run_gate_with_timeout`'s own
/// `gate_resolved` fires independently in the target process when it picks
/// the response up).
pub(crate) fn gate_sweep(
    max_age_secs: Option<u64>,
    dry_run: bool,
    root: Option<PathBuf>,
    reap_strays: bool,
) -> Result<(), CliError> {
    let threshold = max_age_secs.unwrap_or_else(config_parse::gate_max_unattended_age_secs);

    // Cloned before the `match` below moves `root` into `roots` — the stray
    // pass (below) needs it too, unioned into the machine-wide reachable-pid
    // safety set rather than substituted for it (CR-01, 25-15).
    let explicit_root: Vec<PathBuf> = root.clone().into_iter().collect();

    let roots: Vec<PathBuf> = match root {
        Some(root) => vec![root],
        None => {
            registry::prune_missing();
            registry::load_roots()
                .into_iter()
                .map(|r| r.project_root)
                .collect()
        }
    };

    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let mut reaped = 0u32;
    let mut skipped = 0u32;
    let mut left_alone = 0u32;

    for project_root in &roots {
        for gate in Gates::list_open(project_root) {
            let Ok(ts) = gate.timestamp.parse::<u64>() else {
                left_alone += 1;
                continue;
            };
            let age = now.saturating_sub(ts);
            if age < threshold {
                left_alone += 1;
                continue;
            }
            if dry_run {
                reaped += 1;
                println!(
                    "would reap phase {} {} (age {age}s) at {}",
                    gate.phase,
                    gate.stage,
                    project_root.display()
                );
                continue;
            }
            match Gates::reap(
                project_root,
                gate.phase,
                gate.stage,
                "abort: reaped by devflow gate sweep (unattended gate exceeded max age)",
                "devflow-reap",
            ) {
                Ok(_) => {
                    reaped += 1;
                    events::emit(
                        project_root,
                        gate.phase,
                        "gate_reaped",
                        serde_json::json!({
                            "stage": gate.stage.to_string(),
                            "age_secs": age,
                            "max_age_secs": threshold,
                        }),
                    );
                    println!(
                        "reaped phase {} {} (age {age}s) at {}",
                        gate.phase,
                        gate.stage,
                        project_root.display()
                    );
                }
                Err(err) => {
                    skipped += 1;
                    println!(
                        "skipped phase {} {} at {} — already answered ({err})",
                        gate.phase,
                        gate.stage,
                        project_root.display()
                    );
                }
            }
        }
    }

    // 999.44/DEN-68: opt-in only. Registry-independent, so this pass is NOT
    // scoped to `roots` above — a stray by definition has no project root
    // for any registry entry, lock file, or state file to name, which is
    // exactly why the census above cannot see it either. Extends the SAME
    // reaped/skipped/left-alone counters and dry-run summary line rather
    // than printing a second, separate report.
    //
    // CR-01/25-15: the SAFETY half is likewise never narrowed by `--root`.
    // `unreachable_stray_candidates` unions `explicit_root` into the
    // machine-wide reachable-pid set rather than substituting it, because
    // that set is what gets PROTECTED, not what gets acted on — scoping it
    // to one root would leave every OTHER root's live processes unprotected
    // while this pass still reaps machine-wide, a strictly larger blast
    // radius than leaving it machine-wide (see `25-15-PLAN.md`'s
    // `<resolved_decision>`).
    if reap_strays {
        if !explicit_root.is_empty() {
            println!(
                "note: --root does not scope this stray pass -- a stray has no project root \
                 for any registry entry, lock file, or state file to name, so discovery and \
                 reaping are always machine-wide; the reachability safety filter is also \
                 computed across every registered root regardless of --root, deliberately, \
                 because narrowing it would leave other projects' live processes unprotected"
            );
        }
        let candidates = unreachable_stray_candidates(&explicit_root);
        let results = reap_stray_candidates(&candidates, dry_run, agent::STRAY_MIN_AGE);
        // The event's natural home: the explicit `--root`, when given
        // (exactly 999.44's own reproduction shape — an operator who
        // already knows which root's stray they're chasing), else the
        // first registered root, arbitrarily but deterministically, so the
        // audit trail lands somewhere tailable rather than nowhere. A
        // stray has no root of its own to prefer instead.
        let event_root = roots.first();
        for result in &results {
            let layer = stray_layer_label(result.layer);
            match result.outcome {
                StrayReapOutcome::Reaped if dry_run => {
                    reaped += 1;
                    println!("would reap stray pid {} ({layer})", result.pid);
                }
                StrayReapOutcome::Reaped => {
                    reaped += 1;
                    if let Some(event_root) = event_root {
                        events::emit(
                            event_root,
                            // Machine-scoped, not phase-scoped (999.44): a
                            // stray has no phase, so `0` is a sentinel
                            // meaning "not tied to any specific phase" —
                            // never a real phase number, which this
                            // project's phases never assign.
                            PhaseId::new(0),
                            "stray_reaped",
                            serde_json::json!({
                                "pid": result.pid,
                                "layer": layer,
                            }),
                        );
                    }
                    println!("reaped stray pid {} ({layer})", result.pid);
                }
                StrayReapOutcome::IdentityMismatch => {
                    skipped += 1;
                    println!(
                        "skipped stray pid {} ({layer}) — identity could not be re-confirmed \
                         immediately before signalling (the pid may have been recycled since \
                         discovery); inspect it manually (e.g. `ps -p {}`) before assuming it \
                         is safe",
                        result.pid, result.pid
                    );
                }
                StrayReapOutcome::ReapFailed => {
                    skipped += 1;
                    println!(
                        "failed to verify death for stray pid {} ({layer}) even after SIGKILL \
                         escalation — inspect it manually (e.g. `ps -p {}`)",
                        result.pid, result.pid
                    );
                }
                StrayReapOutcome::TooYoung => {
                    // No `stray_reaped` event: nothing was reaped.
                    skipped += 1;
                    println!(
                        "skipped stray pid {} ({layer}) — younger than the minimum age \
                         ({:?}); it may be a process that has not finished starting. A \
                         genuine stray will still be there on the next invocation",
                        result.pid,
                        agent::STRAY_MIN_AGE
                    );
                }
            }
        }
        // Reap both layers together (999.44): clearing only the wrapper
        // manufactures a fresh orphan out of its trailing advance child.
        // Re-run discovery once more after the pass to report anything
        // newly exposed rather than leaving it silently behind, instead of
        // looping unboundedly within one invocation.
        if !dry_run && !results.is_empty() {
            let remaining = agent::discover_stray_devflow_processes();
            if !remaining.is_empty() {
                println!(
                    "note: {} stray process(es) still discoverable after this pass — re-run \
                     `devflow gate sweep --reap-strays` to clear them",
                    remaining.len()
                );
            }
        }
    }

    if dry_run {
        println!(
            "sweep complete (dry run): {reaped} would be reaped, {skipped} skipped, {left_alone} left alone"
        );
    } else {
        println!("sweep complete: {reaped} reaped, {skipped} skipped, {left_alone} left alone");
    }
    Ok(())
}

/// What became of one [`agent::StrayProcess`] candidate the opt-in
/// stray-reaping pass considered.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StrayReapOutcome {
    /// Signalled and verified dead (or, under `dry_run`, "would have been
    /// signalled" — nothing is actually sent in that case).
    Reaped,
    /// The pid's start time no longer matched what discovery recorded, so
    /// it was refused rather than signalled — 999.47's "Related TOCTOU":
    /// the pid could have been recycled between the census and this pass.
    IdentityMismatch,
    /// Identity re-confirmed, but the process was still alive even after
    /// [`agent::terminate_and_verify`]'s bounded `SIGKILL` escalation.
    ReapFailed,
    /// The candidate could be inside its own `fork()`->`execve()` window
    /// (25-12/999.47, the production half of the defect class): between
    /// `Command::spawn()` returning and the child completing `execve`,
    /// `/proc/<pid>/cmdline` reports the PARENT's argv, so the structural
    /// match [`agent::discover_stray_devflow_processes`] made was against
    /// the wrong process's argv. [`agent::is_same_process`]'s
    /// re-confirmation does NOT catch this — a mid-`execve` child is
    /// genuinely the same process with genuinely the same recorded start
    /// time as its parent, so that guard passes. [`agent::process_age`]
    /// and [`agent::STRAY_MIN_AGE`] are the guard that does: a candidate
    /// whose age is unknown, or below the floor, is refused rather than
    /// signalled.
    TooYoung,
}

/// One candidate's outcome, paired with its pid and layer so a caller can
/// report it without re-deriving either from side effects.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct StrayReapResult {
    pid: u32,
    layer: agent::StrayLayer,
    outcome: StrayReapOutcome,
}

/// The opt-in reaper's pure(-ish) core: given an already-discovered
/// candidate list, re-confirm each one's identity immediately before acting
/// and clear it with a verified, escalating signal — never a bare
/// unverified one. Split from [`gate_sweep`] and injectable (a `&[..]`
/// slice, not a live call to [`agent::discover_stray_devflow_processes`])
/// the same way [`reconcile_planning_docs`] is split from
/// `collect_planning_doc_findings`, so this is directly unit-testable
/// against a synthetic or a real-but-fixture-owned candidate list, never
/// the whole machine's live census.
///
/// Its only side effect (beyond the `dry_run` preview, which signals
/// nothing) is [`agent::terminate_and_verify`]'s TERM→KILL escalation — a
/// single unverified `SIGTERM` is exactly what makes the CURRENT recovery
/// path report success while the process keeps running (999.44's own
/// lesson), so this never calls the bare [`agent::terminate`].
///
/// `min_age` (25-12/999.47): a candidate whose [`agent::process_age`] is
/// unknown or below this floor is refused (`TooYoung`) rather than
/// signalled — see [`StrayReapOutcome::TooYoung`] for why
/// `is_same_process` above does not already cover this. Taken as a
/// parameter rather than read from [`agent::STRAY_MIN_AGE`] directly so
/// the existing fixture-owned unit tests can drive this deterministically
/// (`Duration::ZERO` to disable the floor) without sleeping past it, and
/// so the real floor's value is visible at its one call site
/// (`gate_sweep`) instead of being implicit.
fn reap_stray_candidates(
    candidates: &[agent::StrayProcess],
    dry_run: bool,
    min_age: std::time::Duration,
) -> Vec<StrayReapResult> {
    candidates
        .iter()
        .map(|candidate| {
            // Re-confirm identity immediately before acting, closing the
            // discovery-to-signal TOCTOU window (999.47's "Related
            // TOCTOU") — copies `stop_via_lock`'s fail-closed posture:
            // refuse on any uncertainty, never signal on a mismatch.
            if !agent::is_same_process(candidate.pid, candidate.start_time) {
                return StrayReapResult {
                    pid: candidate.pid,
                    layer: candidate.layer,
                    outcome: StrayReapOutcome::IdentityMismatch,
                };
            }
            // Evaluated AFTER identity re-confirmation and BEFORE the
            // dry_run early return, so a dry run previews the same
            // verdict a real run would produce rather than promising a
            // reap the real path would refuse. `None` (age unresolvable)
            // and "younger than the floor" are both refused — fail
            // closed on uncertainty, matching `is_same_process`'s own
            // posture above.
            let old_enough = agent::process_age(candidate.pid).is_some_and(|age| age >= min_age);
            if !old_enough {
                return StrayReapResult {
                    pid: candidate.pid,
                    layer: candidate.layer,
                    outcome: StrayReapOutcome::TooYoung,
                };
            }
            if dry_run {
                return StrayReapResult {
                    pid: candidate.pid,
                    layer: candidate.layer,
                    outcome: StrayReapOutcome::Reaped,
                };
            }
            let cleared = agent::terminate_and_verify(
                candidate.pid,
                agent::TERMINATE_VERIFY_WAIT,
                agent::TERMINATE_VERIFY_POLL,
            );
            StrayReapResult {
                pid: candidate.pid,
                layer: candidate.layer,
                outcome: if cleared {
                    StrayReapOutcome::Reaped
                } else {
                    StrayReapOutcome::ReapFailed
                },
            }
        })
        .collect()
}

/// End a running phase cleanly (23c) — the missing primitive
/// `23-ORPHAN-FORENSICS.md` names as the reason 54 processes accumulated
/// with no remedy but `kill(1)`. Answers `phase`'s open gate if it has one —
/// the primary path, since it is the only one that produces a clean final
/// state: the target unwinds through its own `abort()` and releases its
/// lock; otherwise signals the process recorded in `phase`'s per-phase lock
/// file (never `state.monitor_pid` — T-23-51: the monitor shell script's
/// `trap` closure only ever captures the **agent's** pid, never the
/// trailing `advance` invocation, so by the time `advance` is the shell's
/// foreground child, `monitor_pid` already names a process that exited long
/// ago; `lock::holder`'s recorded pid is the only correct target).
pub(crate) fn stop(project_root: &Path, phase: PhaseId) -> Result<(), CliError> {
    if !stop_via_gate(project_root, phase)? {
        stop_via_lock(project_root, phase)?;
    }
    persist_stopped_state(project_root, phase)
}

/// The primary path: answer `phase`'s open gate with a rejection whose note
/// contains the abort keyword, so `GateAction::from_response` resolves to
/// `GateAction::Abort` rather than `LoopBack(Code)` — looping back would
/// relaunch an agent on a phase the operator just asked to stop.
///
/// Returns `Ok(true)` when a gate was found and handled — either reaped
/// here, or discovered already answered by a race — telling the caller not
/// to fall through to [`stop_via_lock`]. Returns `Ok(false)` when there is
/// no open gate for `phase` at all, including the race where `Gates::reap`
/// reports `NoOpenGate` after this function's own `Gates::list_open` scan
/// found one; that is the signal to fall through, not an error (cross-AI
/// review 23-10).
fn stop_via_gate(project_root: &Path, phase: PhaseId) -> Result<bool, CliError> {
    let Some(gate) = Gates::list_open(project_root)
        .into_iter()
        .find(|g| g.phase == phase)
    else {
        return Ok(false);
    };
    match Gates::reap(
        project_root,
        phase,
        gate.stage,
        "abort: stopped by `devflow stop`",
        "devflow-stop",
    ) {
        Ok(path) => {
            println!(
                "stop: wrote a rejection for phase {phase} {} at {} — the process waiting \
                 on it will pick this up on its next poll, within the 60s backoff cap",
                gate.stage,
                path.display()
            );
            Ok(true)
        }
        // A human, `--yes-ship`, or `devflow gate sweep` already answered
        // this gate between our `list_open` scan and this `reap` call. The
        // phase is already ending — that is success, not a failure to
        // report.
        Err(GateError::AlreadyResponded { .. }) => {
            println!(
                "stop: phase {phase} {} already has a response awaiting pickup — the phase \
                 is already ending",
                gate.stage
            );
            Ok(true)
        }
        Err(GateError::NoOpenGate { .. }) => Ok(false),
        Err(err) => Err(err.into()),
    }
}

/// The fallback: signal the process recorded in `phase`'s per-phase lock
/// file — the process actually running `advance()` and holding the lock
/// across the gate wait. Fail-closed on identity (T-23-52): a live pid that
/// does not look like a devflow process is refused, not signalled, since
/// the lock may be stale with a recycled pid. Never reads
/// `state.monitor_pid` — see [`stop`]'s doc comment.
fn stop_via_lock(project_root: &Path, phase: PhaseId) -> Result<(), CliError> {
    let Some((pid_str, _path)) = lock::holder(project_root, phase) else {
        println!("stop: no lock held for phase {phase} — nothing is running `advance()`");
        return Ok(());
    };
    let Ok(pid) = pid_str.parse::<u32>() else {
        println!(
            "stop: phase {phase}'s lock file holds a corrupt pid ({pid_str}) — treating it \
             as stale"
        );
        return Ok(());
    };
    if !agent::agent_running(pid) {
        println!(
            "stop: phase {phase}'s lock names pid {pid}, which is not alive — stale lock, \
             nothing to signal"
        );
        return Ok(());
    }
    // Identity must be MATCHED against what the lock recorded, never inferred
    // from /proc (999.47). A bare cmdline-basename check alone returns true
    // for any freshly forked child of a devflow process that has not finished
    // execve — during that window the child carries its parent's cmdline and
    // exe — so it would authorise signalling an unrelated process. Confirmed
    // in CI, not theorised.
    //
    // `(pid, starttime)` is unique for the life of a boot: a recycled pid
    // necessarily starts later than the one it replaced, and a mid-execve
    // child has its own start time distinct from its parent's.
    match lock::holder_identity(project_root, phase) {
        Some((recorded_pid, Some(recorded_start))) if recorded_pid == pid => {
            if !agent::is_same_process(pid, recorded_start) {
                return Err(CliError::Message(format!(
                    "refusing to signal pid {pid} for phase {phase} — it is not the \
                     process that took the lock. The lock recorded start time \
                     {recorded_start}, but pid {pid} now reports {:?}, so the pid has \
                     been recycled and belongs to something else. Inspect it manually \
                     (e.g. `ps -p {pid}`) before proceeding.",
                    agent::process_start_time(pid)
                )));
            }
        }
        Some((_, None)) => {
            // Legacy single-line lock, written before start times were
            // recorded. Identity cannot be confirmed, so fail closed rather
            // than fall back to the unsound cmdline check.
            return Err(CliError::Message(format!(
                "refusing to signal pid {pid} for phase {phase} — the lock file records \
                 no start time, so this process's identity cannot be confirmed. The lock \
                 predates identity recording; if the run is genuinely stuck, inspect the \
                 pid manually (e.g. `ps -p {pid}`) and remove \
                 .devflow/lock-{padded} once you are satisfied.",
                padded = phase.padded()
            )));
        }
        _ => {
            return Err(CliError::Message(format!(
                "refusing to signal pid {pid} for phase {phase} — the lock file's holder \
                 could not be read back for identity confirmation. Inspect it manually \
                 (e.g. `ps -p {pid}`) before proceeding."
            )));
        }
    }
    if agent::terminate(pid) {
        println!("stop: signalled pid {pid}, phase {phase}'s lock holder");
    } else {
        println!("stop: pid {pid} could not be signalled (it may have just exited)");
    }
    Ok(())
}

/// Persist the operator's intent: mark `stopped` and record why, preserving
/// any earlier reason (e.g. a prior `--until` halt) by appending rather
/// than overwriting. Never touches `stop_until` — that field means
/// something different (the requested halt stage for `devflow start
/// --until`) and `transition()` reads it. A phase with no persisted state
/// at all — never started, or already cleared by a completed abort — is
/// already stopped; that is success, not an error.
fn persist_stopped_state(project_root: &Path, phase: PhaseId) -> Result<(), CliError> {
    let mut state = match workflow::load_state(project_root, phase) {
        Ok(state) => state,
        Err(workflow::WorkflowError::MissingState(_)) => {
            println!("stop: no persisted state for phase {phase} — already stopped");
            return Ok(());
        }
        Err(err) => return Err(err.into()),
    };
    state.stopped = true;
    let reason = "stopped via `devflow stop`".to_string();
    state.stop_reason = Some(match state.stop_reason.take() {
        Some(existing) if !existing.is_empty() => format!("{existing}; {reason}"),
        _ => reason,
    });
    workflow::save_state(&state)?;
    Ok(())
}

/// Print an open gate's full, untruncated (but sanitized) context — the
/// discoverability counterpart to `gate_list`'s 100-char table truncation
/// (21a, D-03). Shares `resolve_single_open_gate_stage` with `gate_respond`
/// (999.30 / DEN-55 WR-01) so the two commands' gate-resolution behavior
/// cannot drift, and resolves/selects from one fetched open-gate collection
/// instead of reading twice (WR-03).
pub(crate) fn gate_show(
    project_root: &Path,
    phase: PhaseId,
    stage: Option<Stage>,
) -> Result<(), CliError> {
    let open = Gates::list_open(project_root);
    let stage = match stage {
        Some(stage) => stage,
        None => resolve_single_open_gate_stage(&open, phase)?,
    };
    let gate = open
        .into_iter()
        .find(|g| g.phase == phase && g.stage == stage)
        .ok_or_else(|| {
            CliError::Message(format!(
                "no open gate for phase {phase} stage {stage} — see `devflow gate list`"
            ))
        })?;
    println!("{}", render_gate_show(&gate));
    Ok(())
}

/// Pure render for `gate_show`'s output block — the FULL context via
/// `render_gate_context(.., usize::MAX)` (sanitize, never truncate; contrast
/// `gate_list`'s `render_gate_context(.., 100)`). Factored out of `gate_show`
/// so the untruncated-context guarantee is unit-testable without capturing
/// process stdout.
fn render_gate_show(gate: &OpenGate) -> String {
    format!(
        "phase {} {} ({})\n{}",
        gate.phase,
        gate.stage,
        recover::format_age(&gate.timestamp),
        render_gate_context(&gate.context, usize::MAX),
    )
}

/// Print (or follow) a phase's captured agent output.
pub(crate) fn logs(
    project_root: &Path,
    phase: Option<PhaseId>,
    follow: bool,
    stderr: bool,
) -> Result<(), CliError> {
    let phase = match phase {
        Some(p) => p,
        None => default_logs_phase(project_root)?,
    };
    let path = if stderr {
        agent_result::stderr_path(project_root, phase)
    } else {
        agent_result::stdout_path(project_root, phase)
    };
    if !path.exists() && !follow {
        return Err(CliError::Message(format!(
            "no capture file for phase {phase} at {}",
            path.display()
        )));
    }
    eprintln!("== phase {phase}: {} ==", path.display());
    let mut offset = print_capture_from(&path, 0)?;
    if !follow {
        return Ok(());
    }
    // Follow until the agent's exit code lands AND one further quiescent
    // poll produced no new bytes — the natural end of a run. (An operator
    // can always Ctrl-C sooner.)
    let exit_path = agent_result::exit_code_path(project_root, phase);
    loop {
        std::thread::sleep(std::time::Duration::from_millis(500));
        // 14-CR-03: a stage transition archives and recreates the capture
        // file (launch_stage → archive_phase_files), so a shrunken file
        // means the next stage started — reset to the top instead of
        // seeking past EOF forever and silently skipping its output.
        let base = rollover_offset(&path, offset);
        if base != offset {
            eprintln!("== capture restarted (next stage) — following from the top ==");
        }
        let new_offset = print_capture_from(&path, base)?;
        // Quiescent only if no rollover happened AND no new bytes appeared.
        if exit_path.exists() && base == offset && new_offset == offset {
            if let Ok(code) = std::fs::read_to_string(&exit_path) {
                eprintln!("== agent exited with code {} ==", code.trim());
            }
            return Ok(());
        }
        offset = new_offset;
    }
}

/// Render the read-only cross-attempt view for one phase.
pub(crate) fn history_cmd(project_root: &Path, phase: Option<PhaseId>) -> Result<(), CliError> {
    let phase = match phase {
        Some(phase) => phase,
        None => single_active_phase(project_root)?.ok_or_else(|| {
            CliError::Message("no active phase — pass a phase number to `devflow history`".into())
        })?,
    };
    println!(
        "{}",
        history::render_timeline(&history::attempt_timeline(project_root, phase))
    );
    Ok(())
}

/// Detect capture-file rollover for `logs --follow` (14-CR-03): a file
/// shorter than the follower's offset was deleted and recreated by the next
/// stage's monitor, so following must restart from 0. A missing file (the
/// mid-rollover gap) keeps the current offset — the recreated file's shorter
/// length triggers the reset on a later poll if output restarted.
fn rollover_offset(path: &Path, offset: u64) -> u64 {
    match std::fs::metadata(path) {
        Ok(meta) if meta.len() < offset => 0,
        _ => offset,
    }
}

/// Print the capture file's contents from `offset`, returning the new offset.
/// A missing file is treated as empty (it may not exist yet under --follow).
fn print_capture_from(path: &Path, offset: u64) -> Result<u64, CliError> {
    let stdout = std::io::stdout();
    write_capture_from(path, offset, &mut stdout.lock())
}

fn write_capture_from(
    path: &Path,
    offset: u64,
    output: &mut impl std::io::Write,
) -> Result<u64, CliError> {
    use std::io::{Read, Seek, SeekFrom};
    let Ok(mut file) = std::fs::File::open(path) else {
        return Ok(offset);
    };
    file.seek(SeekFrom::Start(offset))
        .map_err(|err| CliError::Message(format!("could not seek capture file: {err}")))?;
    let mut buf = Vec::new();
    file.read_to_end(&mut buf)
        .map_err(|err| CliError::Message(format!("could not read capture file: {err}")))?;
    if !buf.is_empty() {
        let _ = output.write_all(&buf);
        let _ = output.flush();
    }
    Ok(offset + buf.len() as u64)
}

/// Pick the phase `devflow logs` should show when none is given: the single
/// active phase, else the phase with the most recently modified capture file.
fn default_logs_phase(project_root: &Path) -> Result<PhaseId, CliError> {
    if let Some(phase) = single_active_phase(project_root)? {
        return Ok(phase);
    }
    // No active state: fall back to the newest capture file on disk.
    let devflow = workflow::devflow_dir(project_root);
    let mut newest: Option<(std::time::SystemTime, PhaseId)> = None;
    if let Ok(entries) = std::fs::read_dir(&devflow) {
        for entry in entries.flatten() {
            let name = entry.file_name();
            let Some(name) = name.to_str() else { continue };
            let Some(phase) = name
                .strip_prefix("phase-")
                .and_then(|rest| rest.strip_suffix("-stdout"))
                .and_then(|num| num.parse::<PhaseId>().ok())
            else {
                continue;
            };
            let Ok(modified) = entry.metadata().and_then(|m| m.modified()) else {
                continue;
            };
            if newest.is_none_or(|(when, _)| modified > when) {
                newest = Some((modified, phase));
            }
        }
    }
    newest.map(|(_, phase)| phase).ok_or_else(|| {
        CliError::Message("no active phase and no capture files — nothing to show".into())
    })
}

/// Read the launched agent PID the monitor recorded for `phase`, if present.
fn agent_pid_from_file(project_root: &Path, phase: PhaseId) -> Option<u32> {
    let path = agent_result::agent_pid_path(project_root, phase);
    std::fs::read_to_string(path).ok()?.trim().parse().ok()
}

fn cron_instruction_hints(project_root: &Path) -> Vec<String> {
    devflow_core::ship::list_cron_instructions(project_root)
        .iter()
        .map(|instructions| cron_hint_line(instructions, project_root))
        .collect()
}

/// Build one cron-instruction hint line, appending a sanitized rate-limit
/// reset segment when `instructions.retry_after` is non-empty (21a, D-03) —
/// the reset time is already computed and persisted (`CronInstructions.
/// retry_after`, ship.rs), this only presents it; no new detection logic.
/// Pure so it's unit-testable without capturing process stdout.
fn cron_hint_line(
    instructions: &devflow_core::ship::CronInstructions,
    project_root: &Path,
) -> String {
    let base = format!(
        "Cron instruction pending (phase {}): hermes cron create --from-devflow {}",
        instructions.phase,
        project_root.display()
    );
    let retry_after = instructions.retry_after.trim();
    if retry_after.is_empty() {
        base
    } else {
        let reset = render_gate_context(retry_after, 100);
        format!("{base} (rate-limit resets: {reset})")
    }
}

/// Print active phase worktrees with branch and inferred phase/agent.
fn print_worktrees(project_root: &Path, current: Option<&Path>) {
    let worktrees_dir = worktree::worktrees_dir(project_root);
    let worktrees = match worktree::list(project_root) {
        Ok(w) => w,
        Err(_) => return,
    };
    let active: Vec<_> = worktrees
        .iter()
        .filter(|w| w.path.starts_with(&worktrees_dir))
        .collect();
    if active.is_empty() {
        return;
    }
    println!("\nactive worktrees:");
    for wt in active {
        let label = wt
            .path
            .file_name()
            .map(|n| describe_worktree_dir(&n.to_string_lossy()))
            .unwrap_or_default();
        let branch = wt.branch.as_deref().unwrap_or("(detached)");
        let marker = if current == Some(wt.path.as_path()) {
            " *"
        } else {
            ""
        };
        println!("  {} [{branch}]{label}{marker}", wt.path.display());
    }
}

/// Turn a worktree dir name like `phase-07-claude` into ` — phase 7, agent claude`.
fn describe_worktree_dir(name: &str) -> String {
    let Some(rest) = name.strip_prefix("phase-") else {
        return String::new();
    };
    match rest.split_once('-') {
        Some((phase, agent)) => {
            format!(" — phase {}, agent {agent}", phase.trim_start_matches('0'))
        }
        None => format!(" — phase {}", rest.trim_start_matches('0')),
    }
}

pub(crate) fn list(project_root: &Path) -> Result<(), CliError> {
    let git = GitFlow::new(project_root);
    let branches = git.list_feature_branches()?;
    if branches.is_empty() {
        println!("no open feature branches");
        return Ok(());
    }
    println!(
        "{:<25} {:>6} {:>7}  LAST COMMIT",
        "BRANCH", "AHEAD", "BEHIND"
    );
    for b in &branches {
        println!(
            "{:<25} {:>6} {:>7}  {}",
            b.name, b.ahead, b.behind, b.last_commit
        );
    }
    Ok(())
}

fn print_open_branches(project_root: &Path) {
    let git = GitFlow::new(project_root);
    let branches = match git.list_feature_branches() {
        Ok(b) => b,
        Err(_) => return,
    };
    if branches.is_empty() {
        return;
    }
    println!("\nopen branches:");
    for b in &branches {
        let staleness = if b.behind > 0 {
            format!(" ({} behind develop)", b.behind)
        } else {
            String::new()
        };
        println!("  {}{} ahead{staleness}", b.name, b.ahead);
    }
}

pub(crate) fn recover_cmd(
    project_root: &Path,
    do_clean: bool,
    phase: Option<PhaseId>,
) -> Result<(), CliError> {
    if do_clean {
        let warnings = match phase {
            // Explicit phase: clear it regardless of staleness (14-CR-01's
            // escape hatch for a wedged-but-fresh run).
            Some(phase) => recover::clean_phase(project_root, phase)?,
            // Implicit sweep: stale phases only.
            None => recover::clean(project_root)?,
        };
        for warning in &warnings {
            println!("warning: {warning}");
        }
        match phase {
            Some(phase) => println!("cleaned up workflow state for phase {phase}"),
            None => println!("cleaned up stale workflow state"),
        }
        return Ok(());
    }

    let statuses = match recover::inspect_all(project_root) {
        Ok(s) => s,
        Err(recover::RecoverError::NothingToRecover) => {
            println!("no state to recover — project is idle");
            return Ok(());
        }
        Err(err) => {
            return Err(CliError::Message(format!(
                "recover inspection failed: {err}"
            )));
        }
    };

    let mut any_stale = false;
    for status in &statuses {
        if let Some(only) = phase
            && status.state.phase != only
        {
            continue;
        }
        println!("phase: {}", status.state.phase);
        println!("  stage: {}", status.state.stage);
        println!("  mode: {}", status.state.mode);
        println!("  agent: {}", agents::driver_for(status.state.agent).name());
        println!("  started: {} ({})", status.state.started_at, status.age);
        match agent_pid_from_file(project_root, status.state.phase) {
            Some(pid) => {
                let running = agent::agent_running(pid);
                println!("  agent_pid: {pid} (running: {running})");
                if !running {
                    println!("  agent is not running — the monitor may have already advanced");
                }
            }
            None => println!("  agent_pid: none"),
        }
        if status.is_stale {
            any_stale = true;
            println!("  state is stale");
        }
    }

    if any_stale {
        println!(
            "\nstale state found — `devflow recover --clean` clears stale phases only; \
             use `--clean --phase N` for a specific phase"
        );
    }

    Ok(())
}

/// Run the local quality gate: cargo test, clippy, and fmt --check.
pub(crate) fn test_cmd(project_root: &Path) -> Result<(), CliError> {
    let checks = [
        ("cargo test", "cargo test"),
        (
            "cargo clippy",
            "cargo clippy --workspace --all-targets -- -D warnings",
        ),
        ("cargo fmt --check", "cargo fmt --check"),
    ];
    let mut failures = Vec::new();
    for (label, cmd) in checks {
        println!("=== {label} ===");
        let status = hermetic_command("sh", project_root)
            .arg("-c")
            .arg(cmd)
            .status()
            .map_err(|err| CliError::Message(format!("could not run `{cmd}`: {err}")))?;
        if status.success() {
            println!("{label}");
        } else {
            println!("{label}");
            failures.push(label);
        }
    }
    if failures.is_empty() {
        println!("\nall checks passed");
        Ok(())
    } else {
        Err(CliError::Message(format!(
            "quality checks failed: {}",
            failures.join(", ")
        )))
    }
}

// ---------------------------------------------------------------------------
// doctor
// ---------------------------------------------------------------------------

/// One tool/environment check from `doctor`'s pre-existing audit (git,
/// cargo, agent CLIs, `RUST_LOG`, ...). Module-level (WR-01, 18-fix) so
/// `checks_json_value` and `doctor_json_body` can compose it into
/// `doctor --json`'s single output document without living inside `doctor`
/// itself.
pub(crate) struct Check {
    pub(crate) name: String,
    pub(crate) status: String,
    pub(crate) version: Option<String>,
    pub(crate) install_hint: Option<String>,
}

/// Audit the environment and report what's installed, missing, or broken.
pub(crate) fn doctor(project_root: &Path, json: bool) -> Result<(), CliError> {
    use std::process::Command;

    fn cmd_check(name: &str, cmd: &str, version_arg: &str, install_hint: &str) -> Check {
        match Command::new(cmd).arg(version_arg).output() {
            Ok(out) if out.status.success() => {
                let version = String::from_utf8_lossy(&out.stdout)
                    .lines()
                    .next()
                    .unwrap_or("unknown")
                    .trim()
                    .to_string();
                Check {
                    name: name.into(),
                    status: "ok".into(),
                    version: Some(version),
                    install_hint: None,
                }
            }
            Ok(out) => {
                let detail = String::from_utf8_lossy(&out.stderr)
                    .lines()
                    .next()
                    .unwrap_or("unknown")
                    .trim()
                    .to_string();
                Check {
                    name: name.into(),
                    status: "warn".into(),
                    version: Some(detail),
                    install_hint: Some(format!(
                        "`{cmd} {version_arg}` exited non-zero — reinstall or check PATH"
                    )),
                }
            }
            Err(_) => Check {
                name: name.into(),
                status: "missing".into(),
                version: None,
                install_hint: Some(install_hint.into()),
            },
        }
    }

    fn bool_check(name: &str, ok: bool, version: &str, install_hint: &str) -> Check {
        Check {
            name: name.into(),
            status: if ok { "ok".into() } else { "missing".into() },
            version: Some(version.into()),
            install_hint: if ok { None } else { Some(install_hint.into()) },
        }
    }

    let devflow_version = env!("CARGO_PKG_VERSION");

    // RUST_LOG environment check: validate the value is a parsable log directive.
    let (rust_log_status, rust_log_version, rust_log_hint) = match std::env::var("RUST_LOG") {
        Ok(ref val) if val.is_empty() => (
            "warn",
            Some("empty (logging disabled)".into()),
            Some("Set RUST_LOG=info for better diagnostics".into()),
        ),
        Ok(val) => {
            let all_valid = val.split(',').all(|directive| {
                let directive = directive.trim();
                if let Some((_target, level)) = directive.split_once('=') {
                    matches!(level.trim(), "error" | "warn" | "info" | "debug" | "trace")
                } else {
                    matches!(directive, "error" | "warn" | "info" | "debug" | "trace")
                }
            });
            if all_valid {
                ("ok", Some(val), None)
            } else {
                (
                    "warn",
                    Some(val),
                    Some("RUST_LOG value may be invalid — expected error, warn, info, debug, or trace".into()),
                )
            }
        }
        Err(_) => (
            "missing",
            Some("not set — defaulting to info".into()),
            Some("Set RUST_LOG=info for better diagnostics".into()),
        ),
    };

    let checks: Vec<Check> = vec![
        cmd_check(
            "git",
            "git",
            "--version",
            "Install from https://git-scm.com/downloads",
        ),
        bool_check("sh (POSIX shell)", cfg!(unix), "built-in", "Unsupported OS"),
        cmd_check(
            "cargo/rust",
            "cargo",
            "--version",
            "curl https://sh.rustup.rs -sSf | sh",
        ),
        cmd_check(
            "gh CLI",
            "gh",
            "--version",
            "brew install gh / apt install gh",
        ),
        cmd_check(
            "claude",
            "claude",
            "--version",
            "npm i -g @anthropic-ai/claude-code",
        ),
        cmd_check("codex", "codex", "--version", "npm i -g @openai/codex"),
        cmd_check(
            "opencode",
            "opencode",
            "--version",
            "cargo install opencode",
        ),
        cmd_check(
            "pi",
            "pi",
            "--version",
            "Install Pi (see https://github.com/earendil-works/pi-mono)",
        ),
        pi_subagent_dispatch_check(),
        Check {
            name: format!("devflow v{devflow_version}"),
            status: "ok".into(),
            version: Some(devflow_version.into()),
            install_hint: None,
        },
        Check {
            name: "RUST_LOG".into(),
            status: rust_log_status.into(),
            version: rust_log_version,
            install_hint: rust_log_hint,
        },
    ];

    let facts = collect_phase_facts(project_root);
    let doc_findings = collect_planning_doc_findings(project_root);
    // 999.44/DEN-68: a registry-independent, read-only /proc census — the
    // only I/O this adds is `agent::discover_stray_devflow_processes`'s
    // scan, which never signals anything (T-25-62, doctor's read-only
    // contract).
    let stray_findings = collect_stray_process_findings(project_root);

    if json {
        // WR-01 (18-fix): a single top-level JSON document —
        // `{"environment": [...], "reconciliation": [...],
        // "planning_doc_staleness": [...], "stray_processes": [...]}` —
        // instead of the pre-fix behavior of printing the tool checks as
        // one top-level `[...]` array and then printing a SECOND,
        // independent top-level array right after it. That concatenation
        // is not valid single-document JSON for any parser that isn't
        // NDJSON-aware (`json.load` raised "Extra data"). 21b's
        // planning-doc check (D-05) and this plan's stray-process finding
        // (999.44) each extend this SAME object with one more key rather
        // than forking a second array.
        let body = doctor_json_body(&checks, &facts, &doc_findings, &stray_findings);
        println!(
            "{}",
            serde_json::to_string_pretty(&body).expect("doctor --json body must serialize")
        );
    } else {
        for c in &checks {
            let icon = match c.status.as_str() {
                "ok" => "",
                "missing" => "",
                "warn" => "",
                _ => "?",
            };
            let version_str = c.version.as_deref().unwrap_or("-");
            print!("  {:<20} {:<20} {}", c.name, version_str, icon);
            #[allow(clippy::collapsible_if)]
            if c.status == "missing" || c.status == "warn" {
                if let Some(hint) = &c.install_hint {
                    print!("{}", hint);
                }
            }
            println!();
        }
        print!("{}", render_reconciliation_text(&facts));
        print!("{}", render_planning_doc_text(&doc_findings));
        print!("{}", render_stray_process_text(&stray_findings));
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// release --check (20d)
// ---------------------------------------------------------------------------

/// Read-only release-cut preflight. Ceiling is `--check` only (D-03): no
/// state-mutating helper, no `git tag`/publish, and (once Task 2/3 land) no
/// `git fetch` — every check here reads already-available local state. This
/// no-fetch property is scoped to `release --check`; it does NOT extend to
/// `devflow start`, whose `ensure_base_ref_current` (999.51) issues its own
/// `git fetch` on the start path — see that function's doc comment.
/// Follows `doctor`'s `Check`-list-then-report shape (reuses the same
/// `Check` struct) so the two commands stay visually consistent.
pub(crate) fn release_check(project_root: &Path) -> Result<(), CliError> {
    let checks: Vec<Check> = vec![
        check_self_pin(project_root),
        check_divergence(project_root),
        check_publish_order(project_root),
        check_changelog_version(project_root),
    ];

    let mut failed = false;
    for c in &checks {
        let icon = match c.status.as_str() {
            "ok" => "",
            "warn" => "",
            "fail" => "",
            _ => "?",
        };
        let detail = c.version.as_deref().unwrap_or("-");
        println!("  {:<32} {icon}  {detail}", c.name);
        if matches!(c.status.as_str(), "warn" | "fail")
            && let Some(hint) = &c.install_hint
        {
            println!("{hint}");
        }
        if c.status == "fail" {
            failed = true;
        }
    }

    if failed {
        Err(CliError::Message(
            "release preflight failed — see checks above".into(),
        ))
    } else {
        println!("\nrelease preflight passed");
        Ok(())
    }
}

/// The `pi subagent dispatch` doctor check: reports whether the vetted
/// `@bacnh85/pi-subagent` extension is installed at user scope. Warns when
/// absent — the baseline single-agent path still works without it. Split out
/// (phase-39 code review) so the check's mapping is unit-testable.
fn pi_subagent_dispatch_check() -> Check {
    let dispatch = agents::driver_for(AgentKind::Pi)
        .capabilities()
        .subagent_dispatch;
    pi_subagent_dispatch_check_for(dispatch)
}

/// The pure boolean→`Check` mapping behind [`pi_subagent_dispatch_check`],
/// separated from the `pi list` probe so the doctor rendering is testable
/// without spawning a process.
fn pi_subagent_dispatch_check_for(dispatch: bool) -> Check {
    Check {
        name: "pi subagent dispatch".into(),
        status: if dispatch { "ok".into() } else { "warn".into() },
        version: Some(if dispatch {
            "available".into()
        } else {
            "not installed".into()
        }),
        install_hint: if dispatch {
            None
        } else {
            Some(
                "optional — `pi install npm:@bacnh85/pi-subagent` (user scope) enables subagent dispatch"
                    .into(),
            )
        },
    }
}

/// Self-pin check (asserts 20a's invariant): every local-path
/// `[workspace.dependencies]` self-pin must equal `[workspace.package]
/// version`, compared dynamically — never against a hardcoded expected
/// version.
fn check_self_pin(project_root: &Path) -> Check {
    const NAME: &str = "self-pin (workspace member versions)";

    let cargo_toml = project_root.join("Cargo.toml");
    let contents = match std::fs::read_to_string(&cargo_toml) {
        Ok(contents) => contents,
        Err(err) => {
            return Check {
                name: NAME.into(),
                status: "warn".into(),
                version: Some(format!("could not read Cargo.toml: {err}")),
                install_hint: None,
            };
        }
    };

    let (workspace_version, pins) = version::read_workspace_self_pins(&contents);
    let Some(workspace_version) = workspace_version else {
        return Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("not a workspace Cargo.toml (no [workspace.package] version)".into()),
            install_hint: None,
        };
    };

    let drifted: Vec<String> = pins
        .iter()
        .filter(|pin| pin.version != workspace_version)
        .map(|pin| format!("{} pinned {} != {workspace_version}", pin.name, pin.version))
        .collect();

    if drifted.is_empty() {
        Check {
            name: NAME.into(),
            status: "ok".into(),
            version: Some(format!(
                "{} member pin(s) match {workspace_version}",
                pins.len()
            )),
            install_hint: None,
        }
    } else {
        Check {
            name: NAME.into(),
            status: "fail".into(),
            version: Some(drifted.join("; ")),
            install_hint: Some(format!(
                "every [workspace.dependencies] self-pin must equal [workspace.package] \
                 version = \"{workspace_version}\" — VersionBump should have rewritten this; \
                 see 20a/DEN-49"
            )),
        }
    }
}

/// Divergence check: whether `origin/main` is an ancestor of `HEAD` — i.e.
/// whether `scripts/sync-main-to-develop.sh` would be a no-op — read
/// against ALREADY-FETCHED local refs, issuing NO `git fetch` (review:
/// Codex HIGH — a "read-only" preflight must not depend on the network).
/// This property is scoped to `release --check`'s read-only preflight, not
/// a project-wide invariant: `devflow start`'s `ensure_base_ref_current`
/// (999.51) issues its own `git fetch` before comparing on the start path,
/// since a trustworthy currency comparison requires one either way.
fn check_divergence(project_root: &Path) -> Check {
    const NAME: &str = "develop/main divergence (origin/main ancestor)";
    match devflow_core::git::origin_main_ancestor_status(project_root) {
        devflow_core::git::AncestorStatus::Ancestor => Check {
            name: NAME.into(),
            status: "ok".into(),
            version: Some("origin/main is an ancestor of HEAD — sync would be a no-op".into()),
            install_hint: None,
        },
        devflow_core::git::AncestorStatus::Diverged => Check {
            name: NAME.into(),
            status: "fail".into(),
            version: Some("origin/main is NOT an ancestor of HEAD — develop has diverged".into()),
            install_hint: Some(
                "run scripts/sync-main-to-develop.sh before cutting the next release PR".into(),
            ),
        },
        devflow_core::git::AncestorStatus::RefAbsent => Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("origin/main not fetched — cannot determine divergence".into()),
            install_hint: Some("run `git fetch` first, then re-run this check".into()),
        },
    }
}

/// Publish-order check: crates.io requires `devflow-core` to be live before
/// `devflow` (path-dependency `--dry-run`/verify resolves against the
/// *published* registry version, not local source). Sourced from the
/// workspace's own members/dependency graph, never a hardcoded prose
/// string.
fn check_publish_order(project_root: &Path) -> Check {
    const NAME: &str = "crates.io publish order";
    let order = devflow_core::git::publish_order(project_root);
    if order.is_empty() {
        return Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("could not determine workspace publish order".into()),
            install_hint: None,
        };
    }
    Check {
        name: NAME.into(),
        status: "ok".into(),
        version: Some(format!("publish in order: {}", order.join(" -> "))),
        install_hint: None,
    }
}

/// Changelog-version check (999.96): `CHANGELOG.md`'s topmost `## <version>`
/// heading must agree with the workspace version, so a forgotten bump can't
/// ship a changelog under the old version. The negative control is a SYNTHETIC
/// mismatched fixture — not "the current tree", which the v2.5.0 cut already
/// brought into agreement.
fn check_changelog_version(project_root: &Path) -> Check {
    const NAME: &str = "changelog version (matches workspace)";

    let cargo_toml = project_root.join("Cargo.toml");
    let contents = match std::fs::read_to_string(&cargo_toml) {
        Ok(contents) => contents,
        Err(err) => {
            return Check {
                name: NAME.into(),
                status: "warn".into(),
                version: Some(format!("could not read Cargo.toml: {err}")),
                install_hint: None,
            };
        }
    };
    let (workspace_version, _) = version::read_workspace_self_pins(&contents);
    let Some(workspace_version) = workspace_version else {
        return Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("not a workspace Cargo.toml (no [workspace.package] version)".into()),
            install_hint: None,
        };
    };

    let changelog_contents = match std::fs::read_to_string(project_root.join("CHANGELOG.md")) {
        Ok(contents) => contents,
        Err(err) => {
            return Check {
                name: NAME.into(),
                status: "warn".into(),
                version: Some(format!("could not read CHANGELOG.md: {err}")),
                install_hint: None,
            };
        }
    };
    let changelog_version = changelog_contents.lines().find_map(|line| {
        let rest = line.trim().strip_prefix("## ")?;
        let version = rest.split_whitespace().next().map(|w| {
            w.trim_start_matches('[')
                .trim_end_matches(']')
                .trim_start_matches('v')
        });
        // A version looks like `X.Y` — a leading digit, a dot, then another
        // digit — so a `## [2.5.0]` link and a `## v2.5.0` prefix both parse,
        // while a numbered section like `## 1. Overview` does not.
        version
            .filter(|v| {
                let Some(dot) = v.find('.') else {
                    return false;
                };
                let bytes = v.as_bytes();
                bytes.first().is_some_and(|b| b.is_ascii_digit())
                    && bytes.get(dot + 1).is_some_and(|b| b.is_ascii_digit())
            })
            .map(str::to_string)
    });
    let Some(changelog_version) = changelog_version else {
        return Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("no `## <version>` heading found in CHANGELOG.md".into()),
            install_hint: None,
        };
    };

    if changelog_version == workspace_version {
        Check {
            name: NAME.into(),
            status: "ok".into(),
            version: Some(format!("changelog {changelog_version} matches workspace")),
            install_hint: None,
        }
    } else {
        let direction = match compare_versions(&changelog_version, &workspace_version) {
            Some(std::cmp::Ordering::Greater) => {
                "changelog ahead of Cargo.toml (version bump not yet applied)"
            }
            Some(std::cmp::Ordering::Less) => {
                "Cargo.toml ahead of changelog (release notes missing)"
            }
            _ => "direction undetermined (equal-numeric or unparseable version)",
        };
        Check {
            name: NAME.into(),
            status: "fail".into(),
            version: Some(format!(
                "changelog {changelog_version} != workspace {workspace_version}{direction}"
            )),
            install_hint: Some(direction.into()),
        }
    }
}

/// Best-effort comparison of two `X.Y.Z` version strings, component-wise, with
/// standard semver prerelease ordering (`2.5.0` > `2.5.0-rc.1`). `None` if
/// either is unparseable.
fn compare_versions(a: &str, b: &str) -> Option<std::cmp::Ordering> {
    let components = |v: &str| -> Option<(Vec<u32>, bool)> {
        let parts = v
            .split(|c: char| !c.is_ascii_digit())
            .filter(|s| !s.is_empty())
            .take(3)
            .map(|s| s.parse().ok())
            .collect::<Option<Vec<_>>>()?;
        if parts.is_empty() {
            return None;
        }
        let has_prerelease = v.contains('-');
        Some((parts, has_prerelease))
    };
    let (a, a_pre) = components(a)?;
    let (b, b_pre) = components(b)?;
    let mut cmp = a.cmp(&b);
    if cmp == std::cmp::Ordering::Equal {
        cmp = match (a_pre, b_pre) {
            (false, true) => std::cmp::Ordering::Greater,
            (true, false) => std::cmp::Ordering::Less,
            _ => std::cmp::Ordering::Equal,
        };
    }
    Some(cmp)
}

// Tag-signing viability check removed (999.104): the probe was capability-only
// ("can this key sign"), never identity ("is this the maintainer's key"), and
// `scripts/cut-release.sh` now fails loudly when `devflow.releaseSigningKey` is
// unset. The identity check that matters lives in `scripts/hooks/pre-push`.

// ---------------------------------------------------------------------------
// release --verify (post-cut)
// ---------------------------------------------------------------------------

/// Read-only post-cut verifier. `release --check` guards the PRE-release
/// invariants; this guards the POST-release ones that `--check` cannot see
/// and that have been the easy-to-miss manual steps in practice: the release
/// tag must point at a commit on `main` (not the `develop` release commit),
/// and the `main`→`develop` sync must have been run. Same
/// `Check`-list-then-report shape as `release --check`.
pub(crate) fn release_verify(project_root: &Path) -> Result<(), CliError> {
    let checks: Vec<Check> = vec![
        check_tag_on_main(project_root),
        check_sync_done(project_root),
    ];

    let mut failed = false;
    for c in &checks {
        let icon = match c.status.as_str() {
            "ok" => "",
            "warn" => "",
            "fail" => "",
            _ => "?",
        };
        let detail = c.version.as_deref().unwrap_or("-");
        println!("  {:<32} {icon}  {detail}", c.name);
        if matches!(c.status.as_str(), "warn" | "fail")
            && let Some(hint) = &c.install_hint
        {
            println!("{hint}");
        }
        if c.status == "fail" {
            failed = true;
        }
    }

    if failed {
        Err(CliError::Message(
            "release verification failed — see checks above".into(),
        ))
    } else {
        println!("\nrelease verification passed");
        Ok(())
    }
}

/// The release tag (`v{workspace version}`) must point at a commit on
/// `origin/main`. A tag placed on the `develop` release commit (the mistake
/// made cutting v2.5.0) is NOT an ancestor of `main`'s squash-merge lineage,
/// so an ancestry test cleanly separates correct from incorrect placement.
fn check_tag_on_main(project_root: &Path) -> Check {
    const NAME: &str = "release tag on main";

    let cargo_toml = project_root.join("Cargo.toml");
    let contents = match std::fs::read_to_string(&cargo_toml) {
        Ok(c) => c,
        Err(err) => {
            return Check {
                name: NAME.into(),
                status: "warn".into(),
                version: Some(format!("could not read Cargo.toml: {err}")),
                install_hint: None,
            };
        }
    };
    let (Some(workspace_version), _) = version::read_workspace_self_pins(&contents) else {
        return Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("no [workspace.package] version to derive the tag from".into()),
            install_hint: None,
        };
    };
    let tag = format!("v{workspace_version}");

    let tag_exists = devflow_core::git::git_command(project_root)
        .args(["rev-parse", "--verify", "--quiet", &tag])
        .output()
        .map(|out| out.status.success())
        .unwrap_or(false);
    if !tag_exists {
        return Check {
            name: NAME.into(),
            status: "fail".into(),
            version: Some(format!("tag {tag} does not exist")),
            install_hint: Some("tag the release commit on main before verifying".into()),
        };
    }

    match devflow_core::git::ref_is_ancestor(project_root, &tag, "origin/main") {
        devflow_core::git::AncestorStatus::Ancestor => Check {
            name: NAME.into(),
            status: "ok".into(),
            version: Some(format!("{tag} is on origin/main")),
            install_hint: None,
        },
        devflow_core::git::AncestorStatus::Diverged => Check {
            name: NAME.into(),
            status: "fail".into(),
            version: Some(format!(
                "{tag} is NOT an ancestor of origin/main — tagged the wrong branch"
            )),
            install_hint: Some(
                "re-tag on main:  git -c user.signingkey=\"$(git config --get \
                 devflow.releaseSigningKey)\" tag -s -f vX.Y.Z origin/main"
                    .into(),
            ),
        },
        devflow_core::git::AncestorStatus::RefAbsent => Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("origin/main not fetched — cannot compare tag placement".into()),
            install_hint: Some("run `git fetch` first, then re-run".into()),
        },
    }
}

/// After a release, `origin/main` must be an ancestor of `origin/develop`
/// (the `scripts/sync-main-to-develop.sh` step). Skipping it means the next
/// release PR conflicts against a stale merge-base.
fn check_sync_done(project_root: &Path) -> Check {
    const NAME: &str = "main→develop sync";
    match devflow_core::git::ref_is_ancestor(project_root, "origin/main", "origin/develop") {
        devflow_core::git::AncestorStatus::Ancestor => Check {
            name: NAME.into(),
            status: "ok".into(),
            version: Some("origin/main is an ancestor of origin/develop".into()),
            install_hint: None,
        },
        devflow_core::git::AncestorStatus::Diverged => Check {
            name: NAME.into(),
            status: "fail".into(),
            version: Some(
                "origin/main is NOT an ancestor of origin/develop — sync was skipped".into(),
            ),
            install_hint: Some(
                "run scripts/sync-main-to-develop.sh, then PR the merge commit (not squash)".into(),
            ),
        },
        devflow_core::git::AncestorStatus::RefAbsent => Check {
            name: NAME.into(),
            status: "warn".into(),
            version: Some("origin/main or origin/develop not fetched — cannot check sync".into()),
            install_hint: Some("run `git fetch` first, then re-run".into()),
        },
    }
}

// ---------------------------------------------------------------------------
// doctor reconciliation (18a)
// ---------------------------------------------------------------------------

/// Severity of a reconciliation finding, matching the existing `Check.status`
/// convention (lowercase strings) so both `doctor` renderers stay consistent.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Severity {
    Ok,
    Warn,
    Problem,
}

impl Severity {
    pub(crate) fn label(self) -> &'static str {
        match self {
            Severity::Ok => "ok",
            Severity::Warn => "warn",
            Severity::Problem => "problem",
        }
    }
}

/// The read-only facts `doctor` gathers for one active phase before
/// reconciling them. Collected by `collect_phase_facts` (all I/O); consumed
/// with zero I/O by `reconcile_phase`.
pub(crate) struct PhaseFacts {
    pub(crate) phase: PhaseId,
    pub(crate) stage: Stage,
    pub(crate) gate_pending: bool,
    pub(crate) agent_pid: Option<u32>,
    pub(crate) agent_alive: bool,
    /// The monitor pid recorded in `State.monitor_pid` (18b). `None` means
    /// no monitor has been spawned for this state yet, or the state was
    /// written by a binary predating the field — never treated as a problem.
    pub(crate) monitor_pid: Option<u32>,
    pub(crate) monitor_alive: bool,
    /// The most recent event's `event` field value, for display context.
    pub(crate) last_event: Option<String>,
    /// The `stage` field of the most recent `stage_launched` event; `None`
    /// when the last event recorded for this phase is not a launch.
    pub(crate) last_launched_stage: Option<Stage>,
    pub(crate) open_gate_stages: Vec<Stage>,
    pub(crate) feature_branch_exists: bool,
    /// Whether this phase was intentionally halted by `devflow start --until
    /// <stage>` (20c) — `State.stopped`. A stopped phase's dead agent
    /// pid/stale monitor pid are expected, not a crash; both
    /// `check_dead_agent` and `check_dead_monitor` must recognize this
    /// marker instead of reporting a `Problem` (review: Codex HIGH — the
    /// doctor gap is bigger than `check_dead_agent` alone).
    pub(crate) stopped: bool,
}

/// One diagnostic finding for a phase, with a copy-pasteable repair command
/// when one exists. Never carries a filesystem path or username (T-18-01) —
/// only phase numbers, stage names, and pids identify the disagreement.
pub(crate) struct PhaseFinding {
    pub(crate) phase: PhaseId,
    pub(crate) severity: Severity,
    pub(crate) detail: String,
    pub(crate) repair: Option<String>,
}

/// `gate_pending` is set but no gate file is open for this phase — the gate
/// answer path is stuck. `doctor` only reports this; it never repairs it
/// (T-18-02).
fn check_gate_pending_without_gate(facts: &PhaseFacts) -> Option<PhaseFinding> {
    if !facts.gate_pending || !facts.open_gate_stages.is_empty() {
        return None;
    }
    Some(PhaseFinding {
        phase: facts.phase,
        severity: Severity::Problem,
        detail: format!(
            "phase {}: gate_pending is true at stage {} but no gate file is open",
            facts.phase, facts.stage
        ),
        repair: Some(format!("devflow resume --phase {}", facts.phase)),
    })
}

/// An open gate file exists but `gate_pending` is false — an unanswered
/// operator question that `status`/`doctor` isn't surfacing as pending.
fn check_orphan_gate(facts: &PhaseFacts) -> Option<PhaseFinding> {
    if facts.gate_pending || facts.open_gate_stages.is_empty() {
        return None;
    }
    let gate_stage = facts.open_gate_stages[0];
    Some(PhaseFinding {
        phase: facts.phase,
        severity: Severity::Problem,
        detail: format!(
            "phase {}: gate open for stage {} but state.gate_pending is false",
            facts.phase, gate_stage
        ),
        repair: Some(format!(
            "devflow gate approve {} --stage {}",
            facts.phase, gate_stage
        )),
    })
}

/// The recorded agent pid is not alive while the phase sits at an
/// agent-driven stage — the "who watches the watcher" class of silent death
/// CONTEXT.md cites (two incidents, ~4h lost, found only via `ps`).
fn check_dead_agent(facts: &PhaseFacts) -> Option<PhaseFinding> {
    let pid = facts.agent_pid?;
    if facts.stopped || facts.agent_alive || !facts.stage.is_agent_stage() {
        return None;
    }
    Some(PhaseFinding {
        phase: facts.phase,
        severity: Severity::Problem,
        detail: format!(
            "phase {}: agent pid {pid} recorded but not running at stage {}",
            facts.phase, facts.stage
        ),
        repair: Some(format!("devflow resume --phase {}", facts.phase)),
    })
}

/// The recorded monitor pid is dead — nothing will call `devflow advance`
/// for this phase, whether or not the agent is also dead (an agent that
/// outlived its monitor is orphaned too, since nothing will advance it when
/// it exits either). Reuses `liveness` rather than re-deriving the matrix,
/// so the two copies can never drift (18b, T-18-11's `Unknown` guard applies
/// here transitively — an unrecorded monitor is silently `Unknown`, never a
/// finding).
fn check_dead_monitor(facts: &PhaseFacts) -> Option<PhaseFinding> {
    if facts.stopped
        || liveness(facts.monitor_pid, facts.monitor_alive, facts.agent_alive) != Liveness::Stuck
    {
        return None;
    }
    let pid = facts.monitor_pid?;
    Some(PhaseFinding {
        phase: facts.phase,
        severity: Severity::Problem,
        detail: format!(
            "phase {}: monitor pid {pid} recorded but not running at stage {}",
            facts.phase, facts.stage
        ),
        repair: Some(format!("devflow resume --phase {}", facts.phase)),
    })
}

/// The last `stage_launched` event named a different stage than
/// `state.stage`. A `Warn`, not a `Problem` — a healthy pipeline legitimately
/// has one stage in flight between the launch event and the next
/// transition; exact equality is agreement, never an off-by-one mismatch.
fn check_stage_event_drift(facts: &PhaseFacts) -> Option<PhaseFinding> {
    let launched = facts.last_launched_stage?;
    if launched == facts.stage {
        return None;
    }
    Some(PhaseFinding {
        phase: facts.phase,
        severity: Severity::Warn,
        detail: format!(
            "phase {}: last stage_launched event named {launched} but state.stage is {}",
            facts.phase, facts.stage
        ),
        repair: None,
    })
}

/// The phase's feature branch does not exist even though its stage is past
/// `Define`. A `Warn` — a not-yet-pushed or manually deleted branch is
/// recoverable without state surgery.
fn check_missing_branch(facts: &PhaseFacts) -> Option<PhaseFinding> {
    if facts.feature_branch_exists || facts.stage == Stage::Define {
        return None;
    }
    Some(PhaseFinding {
        phase: facts.phase,
        severity: Severity::Warn,
        detail: format!(
            "phase {}: feature/phase-{} does not exist but stage is {}",
            facts.phase,
            facts.phase.padded(),
            facts.stage
        ),
        repair: None,
    })
}

/// Pure reconciliation core: diffs `state.stage` against the latest event,
/// live agent pid, open gates, and branch existence, evaluating checks in a
/// fixed order so the returned findings never depend on how `facts` was
/// assembled (ordering edge). Takes no path, performs no I/O, and mutates
/// nothing (T-18-02) — directly unit-testable without a repository.
fn reconcile_phase(facts: &PhaseFacts) -> Vec<PhaseFinding> {
    [
        check_gate_pending_without_gate(facts),
        check_orphan_gate(facts),
        check_dead_agent(facts),
        check_dead_monitor(facts),
        check_stage_event_drift(facts),
        check_missing_branch(facts),
    ]
    .into_iter()
    .flatten()
    .collect()
}

/// Gather the read-only facts `reconcile_phase` needs for every active
/// phase, sorted by phase ascending so output ordering never depends on
/// directory-read order (ordering edge). Every call here is a read-only
/// primitive already used elsewhere (`status`, `recover::inspect_all`) —
/// none of it is reimplemented.
fn collect_phase_facts(project_root: &Path) -> Vec<PhaseFacts> {
    let states = workflow::list_states(project_root);
    // 14-CR-10: one pass over events.jsonl for every phase's last event,
    // matching status()'s optimization, not a per-phase rescan.
    let mut last_events = events::last_events_by_phase(project_root);
    let open_gates = Gates::list_open(project_root);

    let mut facts: Vec<PhaseFacts> = states
        .into_iter()
        .map(|state| build_phase_facts(project_root, state, &mut last_events, &open_gates))
        .collect();

    facts.sort_by_key(|f| f.phase);
    facts
}

/// Build one phase's [`PhaseFacts`] from already-fetched state, events, and
/// gates — the per-phase half of `collect_phase_facts`, split out to keep
/// that function short.
fn build_phase_facts(
    project_root: &Path,
    state: State,
    last_events: &mut std::collections::HashMap<PhaseId, events::PhaseEventSummary>,
    open_gates: &[OpenGate],
) -> PhaseFacts {
    let phase = state.phase;
    let stopped = state.stopped;
    let agent_pid = agent_pid_from_file(project_root, phase);
    let agent_alive = agent_pid.is_some_and(agent::agent_running);
    let monitor_pid = state.monitor_pid;
    let monitor_alive = monitor_pid.is_some_and(agent::agent_running);
    let last_event = last_events.remove(&phase).map(|summary| summary.event);
    let last_launched_stage = last_event.as_ref().and_then(last_launched_stage_from_event);
    let last_event_name = last_event
        .as_ref()
        .and_then(|e| e.get("event"))
        .and_then(|e| e.as_str())
        .map(str::to_string);
    let open_gate_stages = open_gates
        .iter()
        .filter(|g| g.phase == phase)
        .map(|g| g.stage)
        .collect();
    let branch_ref = format!("refs/heads/feature/phase-{padded}", padded = phase.padded());
    let feature_branch_exists =
        run_git_stdout(project_root, &["rev-parse", "--verify", &branch_ref]).is_some();

    PhaseFacts {
        phase,
        stage: state.stage,
        gate_pending: state.gate_pending,
        agent_pid,
        agent_alive,
        monitor_pid,
        monitor_alive,
        last_event: last_event_name,
        last_launched_stage,
        open_gate_stages,
        feature_branch_exists,
        stopped,
    }
}

/// Derive the stage named by an event's `stage` field, but only when the
/// event's `event` field is `"stage_launched"` — any other event kind (or
/// an unparsable stage name) yields `None`, never a panic.
fn last_launched_stage_from_event(event: &serde_json::Value) -> Option<Stage> {
    if event.get("event").and_then(|e| e.as_str()) != Some("stage_launched") {
        return None;
    }
    event
        .get("stage")
        .and_then(|s| s.as_str())
        .and_then(|s| s.parse::<Stage>().ok())
}

/// The findings to display for one phase: real findings when any exist,
/// otherwise a single synthetic `ok` finding — the display-only counterpart
/// to `reconcile_phase`'s "zero findings" agreement case, shared by both
/// the text and `--json` renderers.
fn findings_for_display(facts: &PhaseFacts) -> Vec<PhaseFinding> {
    let findings = reconcile_phase(facts);
    if !findings.is_empty() {
        return findings;
    }
    vec![PhaseFinding {
        phase: facts.phase,
        severity: Severity::Ok,
        detail: format!("phase {}: ok", facts.phase),
        repair: None,
    }]
}

/// Build `doctor`'s per-phase reconciliation section (after the existing
/// tool/env checks), read-only: it never calls `workflow::save_state`,
/// `events::emit`, `Gates::cleanup`/`Gates::write`, or any `recover::clean*`
/// function (T-18-02). A pure string builder (not a direct `println!`) so
/// it's directly assertable in tests without capturing process stdout.
fn render_reconciliation_text(facts: &[PhaseFacts]) -> String {
    let mut out = String::from("\nreconciliation:\n");
    if facts.is_empty() {
        out.push_str("  no active phases — nothing to reconcile\n");
        return out;
    }
    for phase_facts in facts {
        for finding in findings_for_display(phase_facts) {
            out.push_str(&format!("  {}\n", finding.detail));
            if let Some(repair) = &finding.repair {
                out.push_str(&format!("    repair: {repair}\n"));
            }
        }
    }
    out
}

/// Build the `--json` reconciliation array as a `serde_json::Value` (WR-01,
/// 18-fix). No longer prints its own top-level `[...]` document — `doctor()`
/// nests this under `"reconciliation"` in the single object
/// `doctor_json_body` composes alongside `checks_json_value`'s
/// `"environment"` array.
fn render_reconciliation_json(facts: &[PhaseFacts]) -> serde_json::Value {
    // Pair each finding with its originating phase's last recorded event, so
    // a `--json` consumer gets that context without re-reading events.jsonl.
    let findings: Vec<(&PhaseFacts, PhaseFinding)> = facts
        .iter()
        .flat_map(|pf| findings_for_display(pf).into_iter().map(move |f| (pf, f)))
        .collect();
    serde_json::Value::Array(
        findings
            .iter()
            .map(|(phase_facts, finding)| {
                serde_json::json!({
                    "phase": finding.phase,
                    "severity": finding.severity.label(),
                    "detail": finding.detail,
                    "repair": finding.repair,
                    "last_event": phase_facts.last_event,
                })
            })
            .collect(),
    )
}

/// Build `doctor --json`'s `"environment"` array from the pre-existing
/// tool/env checks (WR-01, 18-fix). Extracted so it can be composed with
/// `render_reconciliation_json`'s array into ONE JSON document instead of
/// being printed as its own top-level array.
fn checks_json_value(checks: &[Check]) -> serde_json::Value {
    serde_json::Value::Array(
        checks
            .iter()
            .map(|c| {
                serde_json::json!({
                    "name": c.name,
                    "status": c.status,
                    "version": c.version,
                    "install_hint": c.install_hint,
                })
            })
            .collect(),
    )
}

/// Compose `doctor --json`'s single JSON document (WR-01, 18-fix). Pre-fix,
/// `doctor()` printed the tool checks as one top-level `[...]` array and
/// then printed `render_reconciliation_json`'s array as a SECOND,
/// independent top-level array right after it — invalid single-document
/// JSON for any parser that isn't NDJSON-aware (`json.load` raised "Extra
/// data" against a live fixture with one active phase). There is now
/// exactly one top-level value: `{"environment": [...], "reconciliation":
/// [...], "planning_doc_staleness": [...]}` — 21b's addition (D-05) extends
/// this SAME object with a third key rather than forking a second reporter
/// or printing a second top-level array.
fn doctor_json_body(
    checks: &[Check],
    facts: &[PhaseFacts],
    doc_findings: &[PlanningDocFinding],
    stray_findings: &[StrayProcessFinding],
) -> serde_json::Value {
    serde_json::json!({
        "environment": checks_json_value(checks),
        "reconciliation": render_reconciliation_json(facts),
        "planning_doc_staleness": render_planning_doc_findings_json(doc_findings),
        "stray_processes": render_stray_process_findings_json(stray_findings),
    })
}

// ---------------------------------------------------------------------------
// doctor planning-doc staleness reconciliation (21b, D-04/D-05)
// ---------------------------------------------------------------------------

/// The `v1.5.0` numeric-tuple cutoff (RESEARCH Pitfall #2): the first phase
/// whose version claim was consistently tagged. A claimed version at or
/// after this cutoff with no matching/reachable git tag is a real
/// `Severity::Problem`; a claim before it is legacy history and downgrades
/// to `Severity::Warn` — otherwise a naive per-row check floods `doctor`
/// with pre-Phase-18 noise, the exact alert-fatigue class 999.14 exists to
/// prevent. Compared as a NUMERIC `(major, minor, patch)` tuple, never a
/// string, so a real future `v1.10.0` is correctly treated as post-cutoff
/// (a lexicographic `"1.10.0" < "1.5.0"` would wrongly sort it as legacy).
const PLANNING_DOC_STALENESS_CUTOFF: (u32, u32, u32) = (1, 5, 0);

/// One detection-only finding produced by reconciling a `ROADMAP.md`/
/// `STATE.md` version claim against the repo's git tags. A sibling of
/// `PhaseFinding`, not a reuse of it: most claims here are about
/// already-shipped phases with no active `state-NN.json`/`PhaseFacts`
/// (RESEARCH Open Q1). `repair` is always `None` — D-04 forbids any
/// auto-correction of planning-doc prose; nothing in this module has a
/// write path to either file. Never carries a filesystem path or username
/// (T-18-01 discipline) — only the source label, the claimed version, and
/// a git tag name.
pub(crate) struct PlanningDocFinding {
    pub(crate) source: String,
    pub(crate) claim: String,
    pub(crate) severity: Severity,
    pub(crate) detail: String,
    pub(crate) repair: Option<String>,
}

/// Parse a table cell as a bare `(major, minor, patch)` semver tuple,
/// stripping an optional leading `v`. Returns `None` for anything that
/// isn't EXACTLY three dot-separated numeric components — this is what
/// keeps version ranges (`0.1.0–0.6.0`), em-dash placeholders (`—`), and
/// any other non-semver cell out of every downstream finding (RESEARCH
/// Pitfall #2), without needing a regex crate: the range's `–` makes its
/// middle component fail `str::parse::<u32>`, and the em-dash fails
/// outright.
pub(crate) fn parse_semver(cell: &str) -> Option<(u32, u32, u32)> {
    let cell = cell.strip_prefix('v').unwrap_or(cell);
    let mut parts = cell.split('.');
    let major = parts.next()?.trim().parse().ok()?;
    let minor = parts.next()?.trim().parse().ok()?;
    let patch = parts.next()?.trim().parse().ok()?;
    if parts.next().is_some() {
        return None; // more than three `.`-separated components
    }
    Some((major, minor, patch))
}

/// Scan a `## Shipped`/`## Completed`-shaped markdown table for `(label,
/// version)` rows whose version cell is a bare or `v`-prefixed single
/// semver. Hand-scans lines split on `|` rather than pulling in a
/// markdown-table parser crate — the `is_self_dogfood_workspace` (D-17)
/// convention this codebase already follows for small, fixed-shape
/// structured text. Skips the header row, the `|---|---|` separator row,
/// and any cell that isn't a bare semver (ranges, em-dashes, anything
/// else) outright; never panics on a malformed row (T-21b-03 — parse
/// defensively, degrade rather than die). `source` (e.g. `"ROADMAP.md"`)
/// is folded into the returned label so a caller that concatenates rows
/// from multiple documents can still tell them apart downstream.
pub(crate) fn parse_planning_doc_versions(text: &str, source: &str) -> Vec<(String, String)> {
    let mut rows = Vec::new();
    for line in text.lines() {
        let trimmed = line.trim();
        if !trimmed.starts_with('|') {
            continue;
        }
        let cells: Vec<&str> = trimmed
            .trim_matches('|')
            .split('|')
            .map(str::trim)
            .collect();
        if cells.len() < 2 {
            continue;
        }
        let label = cells[0];
        // Header row (`Phase | Name | Version`) and the `|---|---|---|`
        // separator row both have a first cell that is never a real phase
        // label — skip both rather than trying to special-case each shape.
        if label.is_empty()
            || label.eq_ignore_ascii_case("phase")
            || label.chars().all(|c| c == '-')
        {
            continue;
        }
        // The version column's position differs between ROADMAP.md's
        // `## Shipped` table (Phase | Name | Version) and STATE.md's
        // `## Completed` table (Phase | Description | Version | Date) —
        // scan every non-label cell and keep whichever ones parse as a
        // bare semver, rather than hardcoding a column index.
        for cell in &cells[1..] {
            if parse_semver(cell).is_some() {
                rows.push((format!("{source} phase {label}"), (*cell).to_string()));
            }
        }
    }
    rows
}

/// Whether `tag` exists in `project_root` AND is reachable from
/// `base_branch` — argv-array `git` shelling only (T-21b-02: tag strings
/// passed in here are already validated `^v?\d+\.\d+\.\d+$` cells, never
/// free-form; no `sh -c`). Two separate invocations, mirroring
/// `staleness::run_git_stdout`'s idiom: existence first, so a missing tag
/// short-circuits before the (more expensive) ancestry check.
pub(crate) fn tag_exists_and_reachable(project_root: &Path, tag: &str, base_branch: &str) -> bool {
    let exists = git_command(project_root)
        .args(["rev-parse", "--verify", &format!("refs/tags/{tag}")])
        .output()
        .is_ok_and(|o| o.status.success());
    exists
        && git_command(project_root)
            .args(["merge-base", "--is-ancestor", tag, base_branch])
            .output()
            .is_ok_and(|o| o.status.success())
}

/// Pure reconciliation core: for each `(label, version_cell)` row, ask the
/// caller-supplied `tag_lookup` closure whether a `v`-normalized tag exists
/// and is reachable — kept injectable (rather than calling
/// `tag_exists_and_reachable` directly) so this is unit-testable without a
/// real repository, mirroring `reconcile_phase`'s zero-I/O discipline. A
/// miss becomes a `PlanningDocFinding` at `Severity::Problem` when the
/// claimed version is at or after the `v1.5.0` cutoff — compared as a
/// NUMERIC tuple via `parse_semver`, never lexicographically — else
/// `Severity::Warn` (RESEARCH Pitfall #2). `repair` is always `None` (D-04:
/// detection-only). Skips any row whose cell doesn't parse as a semver,
/// defensively — `parse_planning_doc_versions` already filters these out,
/// but this must never panic even if called with a stray malformed row.
pub(crate) fn reconcile_planning_docs(
    rows: &[(String, String)],
    tag_lookup: &mut impl FnMut(&str) -> bool,
) -> Vec<PlanningDocFinding> {
    let mut findings = Vec::new();
    for (label, version_cell) in rows {
        let Some(parsed) = parse_semver(version_cell) else {
            continue;
        };
        let tag = if version_cell.starts_with('v') {
            version_cell.clone()
        } else {
            format!("v{version_cell}")
        };
        if tag_lookup(&tag) {
            continue;
        }
        let severity = if parsed >= PLANNING_DOC_STALENESS_CUTOFF {
            Severity::Problem
        } else {
            Severity::Warn
        };
        findings.push(PlanningDocFinding {
            source: label.clone(),
            claim: format!("{label} claims {tag}"),
            severity,
            detail: format!(
                "{label} claims {tag}, but no git tag `{tag}` exists (or it isn't reachable from the base branch)"
            ),
            repair: None,
        });
    }
    findings
}

/// Read `.planning/ROADMAP.md`'s `## Shipped` table and `.planning/STATE.md`'s
/// `## Completed` table (best-effort — a MISSING file yields no rows for
/// that document, never an error; `doctor` must not fabricate a `Problem`
/// from an absent doc), parse both, and reconcile every row against the
/// repo's git tags via `tag_exists_and_reachable(project_root, tag, MAIN)`.
/// `MAIN` is a LOCAL branch in this repo (verified: `git branch --list
/// main`; `git merge-base --is-ancestor v1.7.0 main` succeeds offline) —
/// deliberately not `origin/main` (no network dependency in `doctor`'s
/// read-only contract) and not `develop` (wrong base). The only I/O here is
/// two `std::fs::read_to_string` calls plus `tag_exists_and_reachable`'s
/// `git` subprocesses — `doctor` stays read-only (no write path to either
/// file).
fn collect_planning_doc_findings(project_root: &Path) -> Vec<PlanningDocFinding> {
    let roadmap =
        std::fs::read_to_string(project_root.join(".planning/ROADMAP.md")).unwrap_or_default();
    let state =
        std::fs::read_to_string(project_root.join(".planning/STATE.md")).unwrap_or_default();

    let mut rows = parse_planning_doc_versions(&roadmap, "ROADMAP.md");
    rows.extend(parse_planning_doc_versions(&state, "STATE.md"));

    let mut lookup = |tag: &str| tag_exists_and_reachable(project_root, tag, MAIN);
    reconcile_planning_docs(&rows, &mut lookup)
}

/// Build `doctor --json`'s `"planning_doc_staleness"` array (D-05, Pattern
/// 2), mirroring `render_reconciliation_json`'s array-building idiom.
fn render_planning_doc_findings_json(findings: &[PlanningDocFinding]) -> serde_json::Value {
    serde_json::Value::Array(
        findings
            .iter()
            .map(|f| {
                serde_json::json!({
                    "source": f.source,
                    "claim": f.claim,
                    "severity": f.severity.label(),
                    "detail": f.detail,
                    "repair": f.repair,
                })
            })
            .collect(),
    )
}

/// Build `doctor`'s text planning-docs section, printed after the
/// reconciliation section. A pure string builder (not a direct
/// `println!`), mirroring `render_reconciliation_text`'s shape so it's
/// directly assertable in tests without capturing process stdout. No
/// findings prints a single `"planning docs: consistent with git tags"`
/// line, matching the action spec.
fn render_planning_doc_text(findings: &[PlanningDocFinding]) -> String {
    if findings.is_empty() {
        return "\nplanning docs: consistent with git tags\n".to_string();
    }
    let mut out = String::from("\nplanning docs:\n");
    for finding in findings {
        out.push_str(&format!(
            "  [{}] {}\n",
            finding.severity.label(),
            finding.detail
        ));
    }
    out
}

// ---------------------------------------------------------------------------
// doctor stray-process finding (999.44 / DEN-68)
// ---------------------------------------------------------------------------

/// Human-readable label for a [`agent::StrayLayer`], shared between
/// `doctor`'s finding and `gate_sweep`'s opt-in reaping path so the two
/// surfaces never describe the same layer with different words.
fn stray_layer_label(layer: agent::StrayLayer) -> &'static str {
    match layer {
        agent::StrayLayer::MonitorWrapper => "monitor wrapper",
        agent::StrayLayer::AdvanceChild => "advance child",
    }
}

/// The set of pids that a live registry entry still reaches: every
/// [`devflow_core::state::State::monitor_pid`] recorded under any of
/// `roots` (via [`workflow::list_states`]) plus every lock holder pid for
/// those same roots' phases (via [`lock::holder_identity`]). A pid in this
/// set is by definition not a stray (CR-01, 25-15-PLAN.md) — a live
/// registry entry, lock file, or state file still names it.
///
/// Deliberately excludes the per-phase agent pid file: the `/proc` census
/// this set filters ([`agent::discover_stray_devflow_processes`]) can only
/// ever produce a monitor-wrapper or `devflow advance` pid, never a
/// `claude`/`codex` agent process's own pid, so adding the agent pid file
/// would widen the safety set with pids the census structurally cannot
/// produce.
///
/// Read-only, always: uses [`lock::holder_identity`] (a pure read), never
/// [`lock::holder`] (which deletes an empty lock file — a write this
/// function must never perform, since it also runs on `doctor`'s strictly
/// read-only path).
///
/// RESIDUAL (T-25-15-08, not eliminated): this is a point-in-time read. A
/// root registered, or a `monitor_pid` written, between this read and a
/// caller's later signal is not covered by it. The untouched
/// `agent::is_same_process` identity re-confirmation and
/// [`agent::STRAY_MIN_AGE`] age floor downstream remain the last line of
/// defence in that window.
pub(crate) fn registry_reachable_pids(roots: &[PathBuf]) -> HashSet<u32> {
    let mut reachable = HashSet::new();
    let mut scanned = HashSet::new();
    for root in roots {
        if !scanned.insert(root.clone()) {
            // One root with N phases yields N registry entries;
            // `list_states` already covers every phase in one pass, so a
            // second entry for the same root would only repeat the scan.
            continue;
        }
        for state in workflow::list_states(root) {
            if let Some(pid) = state.monitor_pid {
                reachable.insert(pid);
            }
            if let Some((pid, _start_time)) = lock::holder_identity(root, state.phase) {
                reachable.insert(pid);
            }
        }
    }
    reachable
}

/// Pure filter: the entries in `strays` whose pid is absent from
/// `reachable`. Zero I/O — mirrors [`build_stray_process_findings`]'s own
/// pure-builder posture (split out the same way
/// [`collect_stray_process_findings`]'s I/O half is split from it), so this
/// is directly unit-testable with a synthetic stray list and a synthetic
/// reachable set, with no real orphan process required on the machine
/// running the test.
pub(crate) fn retain_unreachable_strays(
    strays: &[agent::StrayProcess],
    reachable: &HashSet<u32>,
) -> Vec<agent::StrayProcess> {
    strays
        .iter()
        .filter(|stray| !reachable.contains(&stray.pid))
        .copied()
        .collect()
}

/// Every registered root's `project_root` ([`registry::load_roots`]),
/// unioned with `extra`, deduplicated. NEVER narrowed by a caller's scope
/// argument (T-25-15-03) — this is a SAFETY set, not a scope. Narrowing it
/// to one root would un-protect every OTHER root's live processes while
/// the stray pass itself still acts machine-wide, which is a strictly
/// LARGER blast radius than leaving it machine-wide (see `25-15-PLAN.md`'s
/// `<resolved_decision>`).
///
/// Read-only: never calls [`registry::prune_missing`], which mutates the
/// registry and would break `doctor`'s read-only contract.
fn stray_safety_roots(extra: &[PathBuf]) -> Vec<PathBuf> {
    let mut roots: Vec<PathBuf> = registry::load_roots()
        .into_iter()
        .map(|r| r.project_root)
        .collect();
    for root in extra {
        if !roots.contains(root) {
            roots.push(root.clone());
        }
    }
    roots
}

/// The ONE composition both `doctor` and `gate sweep --reap-strays` route
/// through, so the two surfaces' claim and action cannot drift apart
/// (T-25-15-07): the registry-independent `/proc` census
/// ([`agent::discover_stray_devflow_processes`]), filtered by
/// [`retain_unreachable_strays`] against
/// [`registry_reachable_pids`]`(&`[`stray_safety_roots`]`(extra_roots))`.
fn unreachable_stray_candidates(extra_roots: &[PathBuf]) -> Vec<agent::StrayProcess> {
    let reachable = registry_reachable_pids(&stray_safety_roots(extra_roots));
    retain_unreachable_strays(&agent::discover_stray_devflow_processes(), &reachable)
}

/// One `doctor` finding for a state-orphaned process (999.44): a process
/// [`agent::discover_stray_devflow_processes`] matched structurally, but
/// that no registry entry, lock file, or state file can reach — that
/// absence is exactly why the existing per-phase [`PhaseFinding`]s cannot
/// describe it. Machine-scoped, not phase-scoped: a stray by definition has
/// no project root, so this finding is never attached to any
/// [`PhaseFacts`]. Never carries a filesystem path (T-18-01/WR-02) — a
/// stray has no meaningful root to name, so there is nothing to redact.
pub(crate) struct StrayProcessFinding {
    pub(crate) pid: u32,
    pub(crate) layer: &'static str,
    pub(crate) severity: Severity,
    pub(crate) detail: String,
    pub(crate) repair: Option<String>,
}

/// Pure builder: turn already-discovered stray processes into `doctor`
/// findings — zero I/O, split out from [`collect_stray_process_findings`]
/// the same way [`reconcile_planning_docs`] is split from
/// `collect_planning_doc_findings`, so this is directly unit-testable with a
/// synthetic candidate list instead of requiring a real orphan process to
/// exist on the machine running the test.
pub(crate) fn build_stray_process_findings(
    strays: &[agent::StrayProcess],
) -> Vec<StrayProcessFinding> {
    strays
        .iter()
        .map(|stray| {
            let layer = stray_layer_label(stray.layer);
            StrayProcessFinding {
                pid: stray.pid,
                layer,
                severity: Severity::Problem,
                detail: format!(
                    "pid {} ({layer}) matches DevFlow's monitor-wrapper or advance-child \
                     argv shape, is owned by the calling user, and is named by no \
                     registered project root's state file or lock file",
                    stray.pid
                ),
                repair: Some(
                    "devflow gate sweep --reap-strays --dry-run (preview first; re-run \
                     without --dry-run to reap)"
                        .to_string(),
                ),
            }
        })
        .collect()
}

/// Gather `doctor`'s stray-process finding. The ONLY I/O here is
/// [`agent::discover_stray_devflow_processes`]'s read-only `/proc` census
/// (never a signal, T-25-62) and the reachable-pid computation's own
/// read-only `registry`/`lock`/`workflow` reads (CR-01, 25-15) — `doctor`
/// stays strictly read-only. `project_root` is `doctor`'s own root, unioned
/// into the reachable set via [`unreachable_stray_candidates`] so a project
/// the machine registry has not recorded still has its own live monitors
/// protected from being reported as orphans.
fn collect_stray_process_findings(project_root: &Path) -> Vec<StrayProcessFinding> {
    build_stray_process_findings(&unreachable_stray_candidates(&[project_root.to_path_buf()]))
}

/// Build `doctor --json`'s `"stray_processes"` array, mirroring
/// [`render_planning_doc_findings_json`]'s shape.
fn render_stray_process_findings_json(findings: &[StrayProcessFinding]) -> serde_json::Value {
    serde_json::Value::Array(
        findings
            .iter()
            .map(|f| {
                serde_json::json!({
                    "pid": f.pid,
                    "layer": f.layer,
                    "severity": f.severity.label(),
                    "detail": f.detail,
                    "repair": f.repair,
                })
            })
            .collect(),
    )
}

/// Build `doctor`'s text stray-process section. Unlike
/// [`render_reconciliation_text`] (which always prints a per-phase "ok"
/// line) this prints NOTHING when no stray is found — the no-stray case
/// must leave `doctor`'s existing output byte-for-byte unchanged, since a
/// machine-scoped finding with nothing to report is not a standing fact the
/// way "no active phases" is.
fn render_stray_process_text(findings: &[StrayProcessFinding]) -> String {
    if findings.is_empty() {
        return String::new();
    }
    let mut out = String::from(
        "\nstray processes (state-orphaned -- no registry/lock/state file reaches them):\n",
    );
    for finding in findings {
        out.push_str(&format!("  {}\n", finding.detail));
        if let Some(repair) = &finding.repair {
            out.push_str(&format!("    repair: {repair}\n"));
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Cli, Command, GateCmd};
    use clap::Parser;

    /// The doctor check's rendering maps the `pi list` capability probe onto a
    /// `Check` without spawning `pi` — the pure mapping is what needs a test,
    /// the probe itself is covered by `PiDriver::capabilities()` tests
    /// (phase-39 code review, finding 5 / claude LOW #8).
    #[test]
    fn pi_subagent_dispatch_check_renders_both_arms() {
        let available = pi_subagent_dispatch_check_for(true);
        assert_eq!(available.name, "pi subagent dispatch");
        assert_eq!(available.status, "ok");
        assert_eq!(available.version.as_deref(), Some("available"));
        assert_eq!(available.install_hint, None);

        let missing = pi_subagent_dispatch_check_for(false);
        assert_eq!(missing.name, "pi subagent dispatch");
        assert_eq!(missing.status, "warn");
        assert_eq!(missing.version.as_deref(), Some("not installed"));
        assert!(missing.install_hint.is_some());
        assert!(
            missing
                .install_hint
                .as_deref()
                .is_some_and(|h| h.contains("@bacnh85/pi-subagent")),
            "the absent hint must name the vetted install command"
        );
    }

    /// 999.78/A-11, reset event ZERO — the one that must NOT happen. The
    /// per-phase Validate-failure total survives a `devflow start --force`
    /// restart of the same phase, because a bound a restart resets does not
    /// bound the unattended case D-07 exists for.
    ///
    /// Exercises the function `start()` itself calls, not a reimplementation
    /// of it — `start()`'s own body does git plumbing, agent-binary probes and
    /// worktree scaffolding that have nothing to do with the carry-forward.
    ///
    /// **The second half of the assertion is the control.** A carry-forward
    /// that copied the whole persisted state wholesale would satisfy the first
    /// assertion for entirely the wrong reason, and would silently resurrect a
    /// stale streak, a stale commit baseline and a stale stop point along with
    /// the total. Every other counter is seeded non-zero on disk here
    /// specifically so a wholesale copy cannot pass.
    #[test]
    fn phase_validate_failures_survive_a_forced_restart() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(71);

        let mut persisted = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
        persisted.phase_validate_failures = 6;
        persisted.consecutive_failures = 2;
        persisted.infra_failures = 4;
        persisted.preflight_retries = 3;
        persisted.checkpoint_resumes = 1;
        persisted.last_validate_failure_commit_count = Some(9);
        persisted.stage = Stage::Validate;
        persisted.stop_until = Some(Stage::Code);
        workflow::save_state(&persisted).unwrap();

        let fresh = fresh_state_carrying_phase_failures(root, phase, AgentKind::Claude, Mode::Auto);

        assert_eq!(
            fresh.phase_validate_failures, 6,
            "the per-phase total must be carried across a forced restart — a new process \
             starting is not one of A-11's two reset events"
        );
        assert_eq!(
            fresh.consecutive_failures, 0,
            "the streak is per-run and must start at zero"
        );
        assert_eq!(fresh.infra_failures, 0);
        assert_eq!(fresh.preflight_retries, 0);
        assert_eq!(fresh.checkpoint_resumes, 0);
        assert_eq!(
            fresh.last_validate_failure_commit_count, None,
            "the commit baseline is per-run; carrying it would compare a new run's count \
             against an old run's observation"
        );
        assert_eq!(fresh.stage, Stage::Define);
        assert_eq!(fresh.stop_until, None);
    }

    /// A-11 reset event ONE: phase completion. `finish_workflow` calls
    /// `workflow::clear_state`, which deletes the phase's state file — so the
    /// next start for that phase finds nothing to carry and begins at zero.
    ///
    /// Written as clear-then-construct rather than never-write-at-all, so it is
    /// the opposite-result case for the carry-forward test above: same phase,
    /// same construction call, the ONLY difference being whether completion
    /// removed the file.
    #[test]
    fn phase_validate_failures_reset_when_the_phase_completes() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(72);

        let mut persisted = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
        persisted.phase_validate_failures = 8;
        workflow::save_state(&persisted).unwrap();
        assert_eq!(
            fresh_state_carrying_phase_failures(root, phase, AgentKind::Claude, Mode::Auto)
                .phase_validate_failures,
            8,
            "premise: while the state file exists, the total IS carried — otherwise the \
             assertion below proves nothing about completion"
        );

        // What `finish_workflow_with_gate_timeout` does on genuine completion.
        workflow::clear_state(root, phase).unwrap();

        assert_eq!(
            fresh_state_carrying_phase_failures(root, phase, AgentKind::Claude, Mode::Auto)
                .phase_validate_failures,
            0,
            "phase completion cleared the state, so the next start begins with a full budget"
        );
    }

    /// WR-02 (35-REVIEW): a state file that EXISTS but does not deserialize is
    /// a third case, and the old `if let Ok(..)` collapsed it into the absent
    /// case. Both still produce a zero total — the number that should have been
    /// carried is precisely what could not be read — so the ONLY observable
    /// difference is whether the operator is told, which is why the decision
    /// was split out from its `println!`.
    ///
    /// All three cases in one test with real `load_state` calls, because the
    /// pair is the measurement: an implementation that warned on every `Err`,
    /// or on none, fails one half. The absent case is the negative control and
    /// is the one a reader should check first.
    #[test]
    fn a_corrupt_state_file_warns_while_an_absent_one_is_silent() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(73);

        // NEGATIVE CONTROL: nothing on disk — a genuine zero, no warning.
        let (absent_total, absent_warning) =
            carried_phase_failures(phase, workflow::load_state(root, phase));
        assert_eq!(absent_total, 0);
        assert_eq!(
            absent_warning, None,
            "a phase's first start is not an anomaly and must stay silent"
        );

        // A readable file: the total is carried and, again, nothing is said.
        let mut persisted = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
        persisted.phase_validate_failures = 6;
        workflow::save_state(&persisted).unwrap();
        let (ok_total, ok_warning) =
            carried_phase_failures(phase, workflow::load_state(root, phase));
        assert_eq!(ok_total, 6);
        assert_eq!(ok_warning, None);

        // Truncate the same file in place — the shape a full disk leaves.
        let state_path = workflow::state_path(root, phase);
        assert!(
            state_path.exists(),
            "the fixture must corrupt an EXISTING file; a missing one is the case above"
        );
        std::fs::write(&state_path, "{\"phase\": 73, \"stage\":").unwrap();
        assert!(
            !matches!(
                workflow::load_state(root, phase),
                Err(workflow::WorkflowError::MissingState(_))
            ),
            "premise: a corrupt file must be a DIFFERENT error from an absent one, or \
             nothing downstream could tell them apart"
        );

        let (corrupt_total, corrupt_warning) =
            carried_phase_failures(phase, workflow::load_state(root, phase));
        assert_eq!(
            corrupt_total, 0,
            "there is no total to carry — that is the point of the warning"
        );
        let corrupt_warning =
            corrupt_warning.expect("an unreadable budget must not restart at zero silently");
        assert!(
            corrupt_warning.contains("restarts at zero"),
            "the operator must be told what the consequence is, got: {corrupt_warning:?}"
        );
    }

    #[test]
    fn gate_approve_arg_parsing_accepts_positional_stage() {
        let cli = Cli::try_parse_from(["devflow", "gate", "approve", "15", "ship"]).unwrap();
        let Command::Gate {
            action: GateCmd::Approve { stage, project, .. },
        } = cli.command
        else {
            panic!("expected gate approve command");
        };

        assert_eq!(stage.as_deref(), Some("ship"));
        assert_eq!(project, PathBuf::from("."));

        let flagged =
            Cli::try_parse_from(["devflow", "gate", "approve", "15", "--stage", "ship"]).unwrap();
        let Command::Gate {
            action:
                GateCmd::Approve {
                    stage,
                    stage_option,
                    ..
                },
        } = flagged.command
        else {
            panic!("expected flagged gate approve command");
        };
        assert_eq!(stage, None);
        assert_eq!(stage_option, Some(Stage::Ship));

        let bare = Cli::try_parse_from(["devflow", "gate", "approve", "15"]).unwrap();
        let Command::Gate {
            action:
                GateCmd::Approve {
                    stage,
                    stage_option,
                    ..
                },
        } = bare.command
        else {
            panic!("expected bare gate approve command");
        };
        assert_eq!(stage, None);
        assert_eq!(stage_option, None);

        let legacy =
            Cli::try_parse_from(["devflow", "gate", "approve", "15", "/tmp/example-project"])
                .unwrap();
        let Command::Gate {
            action:
                GateCmd::Approve {
                    stage,
                    legacy_project,
                    stage_option,
                    project,
                    ..
                },
        } = legacy.command
        else {
            panic!("expected legacy gate approve command");
        };
        let (stage, project) =
            resolve_gate_target(stage, legacy_project, stage_option, project).unwrap();
        assert_eq!(stage, None);
        assert_eq!(project, PathBuf::from("/tmp/example-project"));
    }

    #[test]
    fn gate_show_arg_parsing_accepts_phase_and_optional_stage() {
        let bare = Cli::try_parse_from(["devflow", "gate", "show", "15"]).unwrap();
        let Command::Gate {
            action: GateCmd::Show { phase, stage, .. },
        } = bare.command
        else {
            panic!("expected gate show command");
        };
        assert_eq!(phase, PhaseId::new(15));
        assert_eq!(stage, None);

        let flagged =
            Cli::try_parse_from(["devflow", "gate", "show", "15", "--stage", "ship"]).unwrap();
        let Command::Gate {
            action: GateCmd::Show { phase, stage, .. },
        } = flagged.command
        else {
            panic!("expected gate show command with stage");
        };
        assert_eq!(phase, PhaseId::new(15));
        assert_eq!(stage, Some(Stage::Ship));
    }

    #[test]
    fn gate_show_renders_full_untruncated_sanitized_context() {
        let dir = tempfile::tempdir().unwrap();
        let context = format!("first line\n\u{1b}[2J{}", "x".repeat(150));
        Gates::write_gate(dir.path(), PhaseId::new(15), Stage::Ship, &context).unwrap();
        let gate = Gates::list_open(dir.path())
            .into_iter()
            .find(|g| g.phase == PhaseId::new(15))
            .unwrap();

        let rendered = render_gate_show(&gate);

        assert!(rendered.contains(&"x".repeat(150)));
        assert!(!rendered.contains("[truncated"));
        assert!(!rendered.contains('\u{1b}'));
    }

    #[test]
    fn gate_show_errors_naming_gate_list_when_no_open_gate() {
        let dir = tempfile::tempdir().unwrap();
        let err = gate_show(dir.path(), PhaseId::new(15), None).unwrap_err();
        assert!(err.to_string().contains("devflow gate list"));
    }

    #[test]
    fn gate_show_errors_asking_for_stage_with_several_open_gates() {
        let dir = tempfile::tempdir().unwrap();
        Gates::write_gate(dir.path(), PhaseId::new(15), Stage::Ship, "ctx1").unwrap();
        Gates::write_gate(dir.path(), PhaseId::new(15), Stage::Validate, "ctx2").unwrap();

        let err = gate_show(dir.path(), PhaseId::new(15), None).unwrap_err();

        assert!(err.to_string().contains("--stage"));
    }

    #[test]
    fn gate_show_auto_resolves_single_open_gate() {
        let dir = tempfile::tempdir().unwrap();
        Gates::write_gate(
            dir.path(),
            PhaseId::new(15),
            Stage::Ship,
            "the only open gate",
        )
        .unwrap();

        assert!(gate_show(dir.path(), PhaseId::new(15), None).is_ok());
    }

    #[test]
    fn describe_worktree_dir_infers_phase_and_agent() {
        assert_eq!(
            describe_worktree_dir("phase-07-claude"),
            " — phase 7, agent claude"
        );
        assert_eq!(describe_worktree_dir("phase-08"), " — phase 8");
        assert_eq!(describe_worktree_dir("reference"), "");
    }

    #[test]
    fn cron_instruction_hints_include_hermes_command_per_phase() {
        let dir = tempfile::tempdir().unwrap();
        // Empty retry_after here so the exact-match assertion below isolates
        // the base hermes-command hint from 21a's reset-time fragment
        // (covered separately by cron_hint_line_* below).
        for phase in [PhaseId::new(7), PhaseId::new(9)] {
            let instructions =
                devflow_core::ship::build_single_agent_cron_instructions(dir.path(), phase, "");
            devflow_core::ship::write_cron_instructions(dir.path(), &instructions).unwrap();
        }

        let hints = cron_instruction_hints(dir.path());

        assert_eq!(hints.len(), 2);
        assert_eq!(
            hints[0],
            format!(
                "Cron instruction pending (phase 7): hermes cron create --from-devflow {}",
                dir.path().display()
            )
        );
        assert!(hints[1].contains("(phase 9)"));
    }

    #[test]
    fn cron_hint_line_appends_sanitized_reset_when_retry_after_present() {
        let dir = tempfile::tempdir().unwrap();
        let instructions = devflow_core::ship::build_single_agent_cron_instructions(
            dir.path(),
            PhaseId::new(7),
            "2026-06-18T15:45:30Z",
        );

        let hint = cron_hint_line(&instructions, dir.path());

        assert!(hint.starts_with(&format!(
            "Cron instruction pending (phase 7): hermes cron create --from-devflow {}",
            dir.path().display()
        )));
        assert!(hint.contains("(rate-limit resets: 2026-06-18T15:45:30Z)"));
    }

    #[test]
    fn cron_hint_line_omits_reset_fragment_when_retry_after_empty() {
        let dir = tempfile::tempdir().unwrap();
        let instructions = devflow_core::ship::build_single_agent_cron_instructions(
            dir.path(),
            PhaseId::new(7),
            "",
        );

        let hint = cron_hint_line(&instructions, dir.path());

        assert_eq!(
            hint,
            format!(
                "Cron instruction pending (phase 7): hermes cron create --from-devflow {}",
                dir.path().display()
            )
        );
        assert!(!hint.contains("resets"));
    }

    #[test]
    fn default_logs_phase_prefers_single_active_state() {
        let dir = tempfile::tempdir().unwrap();
        let state = State::new(
            PhaseId::new(6),
            AgentKind::Claude,
            Mode::Auto,
            dir.path().to_path_buf(),
        );
        workflow::save_state(&state).unwrap();

        assert_eq!(default_logs_phase(dir.path()).unwrap(), PhaseId::new(6));
    }

    #[test]
    fn default_logs_phase_is_ambiguous_with_two_active_states() {
        let dir = tempfile::tempdir().unwrap();
        for phase in [PhaseId::new(6), PhaseId::new(7)] {
            let state = State::new(
                phase,
                AgentKind::Claude,
                Mode::Auto,
                dir.path().to_path_buf(),
            );
            workflow::save_state(&state).unwrap();
        }

        let err = default_logs_phase(dir.path()).unwrap_err();
        assert!(err.to_string().contains("--phase"));
    }

    #[test]
    fn default_logs_phase_falls_back_to_newest_capture_file() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(dir.path().join(".devflow")).unwrap();
        std::fs::write(
            agent_result::stdout_path(dir.path(), PhaseId::new(3)),
            "old",
        )
        .unwrap();
        // Ensure a strictly newer mtime on the second capture.
        std::thread::sleep(std::time::Duration::from_millis(20));
        std::fs::write(
            agent_result::stdout_path(dir.path(), PhaseId::new(5)),
            "new",
        )
        .unwrap();

        assert_eq!(default_logs_phase(dir.path()).unwrap(), PhaseId::new(5));
    }

    #[test]
    fn default_logs_phase_errors_with_nothing_to_show() {
        let dir = tempfile::tempdir().unwrap();
        assert!(default_logs_phase(dir.path()).is_err());
    }

    /// 18b: a state with no recorded monitor is never reported as stuck,
    /// regardless of the (unreliable, since no monitor was ever recorded)
    /// liveness bits passed alongside it.
    #[test]
    fn liveness_unknown_when_no_monitor_recorded() {
        assert_eq!(liveness(None, false, false), Liveness::Unknown);
        assert_eq!(liveness(None, false, true), Liveness::Unknown);
        assert_eq!(liveness(None, true, false), Liveness::Unknown);
        assert_eq!(liveness(None, true, true), Liveness::Unknown);
    }

    /// 18b: the full four-row matrix for a recorded monitor pid. A dead
    /// agent with a dead monitor OR a live monitor with a dead agent are
    /// different states — only the former is `Stuck` (nothing will call
    /// `devflow advance`); the latter is a normal between-stages moment. An
    /// agent that outlived its monitor is also `Stuck` — orphaned, since
    /// nothing will advance it when it exits either.
    #[test]
    fn liveness_matrix_covers_all_four_rows() {
        let pid = Some(4242);
        assert_eq!(liveness(pid, true, true), Liveness::Healthy);
        assert_eq!(liveness(pid, true, false), Liveness::BetweenStages);
        assert_eq!(liveness(pid, false, false), Liveness::Stuck);
        assert_eq!(liveness(pid, false, true), Liveness::Stuck);
    }

    /// 18b: a corrupt pid (0, or above `i32::MAX`) must never read as alive
    /// — `liveness` relies entirely on `agent::agent_running`'s existing
    /// hardening (no second probe is written), so it can only ever produce
    /// `Stuck` or `Unknown` for a corrupt pid, never a false `Healthy`.
    #[test]
    fn liveness_treats_zero_and_overflow_pids_as_dead() {
        assert!(!agent::agent_running(0));
        assert!(!agent::agent_running(u32::MAX));
    }

    /// 18b: persisting `monitor_pid` for one phase must not disturb a
    /// concurrently-active sibling phase's `monitor_pid` (concurrency edge).
    #[test]
    fn monitor_pid_persisted_for_one_phase_does_not_disturb_a_sibling() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let mut phase7 = State::new(
            PhaseId::new(7),
            AgentKind::Claude,
            Mode::Auto,
            root.to_path_buf(),
        );
        phase7.monitor_pid = Some(111);
        workflow::save_state(&phase7).unwrap();

        let mut phase8 = State::new(
            PhaseId::new(8),
            AgentKind::Claude,
            Mode::Auto,
            root.to_path_buf(),
        );
        phase8.monitor_pid = Some(222);
        workflow::save_state(&phase8).unwrap();

        let reloaded7 = workflow::load_state(root, PhaseId::new(7)).unwrap();
        let reloaded8 = workflow::load_state(root, PhaseId::new(8)).unwrap();
        assert_eq!(reloaded7.monitor_pid, Some(111));
        assert_eq!(reloaded8.monitor_pid, Some(222));
    }

    /// 18b (idempotency edge): running `devflow status` twice must produce
    /// byte-identical `.devflow/` state — the new monitor liveness probe is
    /// purely a read, same as the existing agent liveness probe it sits
    /// beside. Also exercises the `u32::MAX` boundary pid (precision edge,
    /// via `agent::agent_running`'s existing hardening) so the probe can
    /// only ever report `Stuck`, never a false `Healthy`.
    #[test]
    fn status_reading_monitor_liveness_writes_no_state_and_no_event() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(66);
        let mut state = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
        state.monitor_pid = Some(u32::MAX);
        workflow::save_state(&state).unwrap();

        let state_path = workflow::state_path(root, phase);
        let before_len = std::fs::metadata(&state_path).unwrap().len();
        let before_modified = std::fs::metadata(&state_path).unwrap().modified().unwrap();
        let events_log = events::events_path(root);
        let before_lines = std::fs::read_to_string(&events_log)
            .unwrap_or_default()
            .lines()
            .count();

        status(root).unwrap();
        status(root).unwrap();

        let after_len = std::fs::metadata(&state_path).unwrap().len();
        let after_modified = std::fs::metadata(&state_path).unwrap().modified().unwrap();
        let after_lines = std::fs::read_to_string(&events_log)
            .unwrap_or_default()
            .lines()
            .count();

        assert_eq!(
            before_len, after_len,
            "status must not rewrite the state file"
        );
        assert_eq!(
            before_modified, after_modified,
            "status must not touch the state file's mtime"
        );
        assert_eq!(
            before_lines, after_lines,
            "status must not append to events.jsonl"
        );
    }

    /// 15a: `devflow gate approve` resolves the stage automatically when a
    /// phase has exactly one open gate and writes a response the workflow's
    /// poller will consume.
    #[test]
    fn gate_respond_auto_resolves_single_open_gate() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        Gates::write_gate(root, PhaseId::new(15), Stage::Ship, "approve merge?").unwrap();

        gate_respond(root, PhaseId::new(15), None, true, Some("lgtm".into())).unwrap();

        let polled = Gates::poll_response(root, PhaseId::new(15), Stage::Ship, 1)
            .expect("response readable");
        assert!(polled.approved);
        assert_eq!(polled.note.as_deref(), Some("lgtm"));
        let event = devflow_core::events::last_event_for_phase(root, PhaseId::new(15)).unwrap();
        assert_eq!(event["event"], "gate_response_written");
        assert_eq!(event["stage"], "ship");
    }

    #[test]
    fn gate_respond_requires_stage_when_ambiguous_and_errors_when_none_open() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let err = gate_respond(root, PhaseId::new(15), None, true, None).unwrap_err();
        assert!(err.to_string().contains("no open gate"), "{err}");

        Gates::write_gate(root, PhaseId::new(15), Stage::Validate, "a").unwrap();
        Gates::write_gate(root, PhaseId::new(15), Stage::Ship, "b").unwrap();
        let err =
            gate_respond(root, PhaseId::new(15), None, false, Some("nope".into())).unwrap_err();
        assert!(err.to_string().contains("--stage"), "{err}");

        // Explicit --stage disambiguates.
        gate_respond(
            root,
            PhaseId::new(15),
            Some(Stage::Validate),
            false,
            Some("gaps".into()),
        )
        .unwrap();
        assert!(
            Gates::response_path(root, PhaseId::new(15), Stage::Validate).exists(),
            "explicit-stage rejection must land"
        );
        assert!(!Gates::response_path(root, PhaseId::new(15), Stage::Ship).exists());
    }

    /// Backdate an already-written gate's `timestamp` so it reads as
    /// `age_secs` old — the deterministic way `gate_sweep` tests make a gate
    /// look abandoned without sleeping.
    /// An age comfortably past whatever the sweep's threshold currently is,
    /// derived from the same default `gate_sweep` reads instead of hard-coded.
    ///
    /// The literal `7 * 60 * 60` these tests used before outlived the six-hour
    /// default it had been chosen against. When the threshold moved to three
    /// days (equality with the gate poll timeout, so a sweep cannot reap a
    /// gate a live monitor is still polling), a seven-hour backdate stopped
    /// reaching the reap path entirely — and only ONE of the three tests below
    /// noticed. The other two kept passing while asserting nothing, because
    /// "no response was written" is equally true of a gate the sweep declined
    /// to consider.
    fn aged_past_threshold() -> u64 {
        config_parse::gate_max_unattended_age_secs() + 60 * 60
    }

    fn backdate_gate(root: &Path, phase: PhaseId, stage: Stage, age_secs: u64) {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let gate = devflow_core::gates::GateFile {
            phase,
            stage,
            context: "ctx".to_string(),
            timestamp: now.saturating_sub(age_secs).to_string(),
        };
        std::fs::write(
            Gates::gate_path(root, phase, stage),
            serde_json::to_string_pretty(&gate).unwrap(),
        )
        .unwrap();
    }

    /// Task 2 behavior: `--dry-run` computes and prints every decision but
    /// writes nothing — the aged gate stays open with no response file.
    #[test]
    fn gate_sweep_dry_run_does_not_write_a_response() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        Gates::write_gate(root, PhaseId::new(30), Stage::Ship, "ctx").unwrap();
        backdate_gate(root, PhaseId::new(30), Stage::Ship, aged_past_threshold());

        gate_sweep(None, true, Some(root.to_path_buf()), false).unwrap();

        assert!(
            !Gates::response_path(root, PhaseId::new(30), Stage::Ship).exists(),
            "dry-run must never write a response file"
        );
        let open = Gates::list_open(root);
        assert_eq!(open.len(), 1, "dry-run must leave the gate open");
    }

    /// Task 2 behavior: a gate that already has a response is a benign race
    /// (a human or `--yes-ship` may have answered it between `list_open`
    /// and `reap`) — the sweep must return `Ok` and leave the existing
    /// response byte-for-byte untouched, never overwrite it.
    #[test]
    fn gate_sweep_skips_already_responded_gate_without_clobbering() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        Gates::write_gate(root, PhaseId::new(31), Stage::Ship, "ctx").unwrap();
        backdate_gate(root, PhaseId::new(31), Stage::Ship, aged_past_threshold());
        let response = GateResponse {
            approved: true,
            note: None,
            responded_by: Some("human".into()),
        };
        Gates::respond(root, PhaseId::new(31), Stage::Ship, &response).unwrap();
        let before =
            std::fs::read_to_string(Gates::response_path(root, PhaseId::new(31), Stage::Ship))
                .unwrap();

        let result = gate_sweep(None, false, Some(root.to_path_buf()), false);

        assert!(result.is_ok(), "an already-answered gate must not error");
        let after =
            std::fs::read_to_string(Gates::response_path(root, PhaseId::new(31), Stage::Ship))
                .unwrap();
        assert_eq!(
            before, after,
            "the pre-existing response must be byte-identical afterwards"
        );
    }

    /// Task 2 behavior (T-23-43 audit trail): a successful reap emits
    /// `gate_reaped` in the sweep's own process — one of the two
    /// independent, attributed records a reaped gate leaves.
    #[test]
    fn gate_sweep_emits_gate_reaped_event_on_reap() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        Gates::write_gate(root, PhaseId::new(32), Stage::Ship, "ctx").unwrap();
        backdate_gate(root, PhaseId::new(32), Stage::Ship, aged_past_threshold());

        gate_sweep(None, false, Some(root.to_path_buf()), false).unwrap();

        let event = devflow_core::events::last_event_for_phase(root, PhaseId::new(32)).unwrap();
        assert_eq!(event["event"], "gate_reaped");
        assert_eq!(event["stage"], "ship");
    }

    /// Wrap a real, test-owned pid as the `agent::StrayProcess` shape
    /// `discover_stray_devflow_processes` would have produced for it,
    /// WITHOUT actually scanning the whole machine's `/proc` — 999.44's own
    /// per-test safety rule: a reaping test must never act on anything it
    /// did not spawn itself, and this machine's live process table
    /// legitimately contains other, unrelated devflow activity while these
    /// tests run.
    fn stray_candidate_for(pid: u32, layer: agent::StrayLayer) -> agent::StrayProcess {
        agent::StrayProcess {
            pid,
            start_time: agent::process_start_time(pid)
                .expect("must be able to read the fixture's own recorded start time"),
            layer,
        }
    }

    /// Task 2 behavior: `--dry-run` computes and reports the outcome but
    /// signals nothing — mirrors the existing gate-reaping dry-run
    /// contract, extended to strays.
    #[test]
    fn reap_stray_candidates_dry_run_never_signals() {
        let mut child = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .expect("spawn sleep fixture");
        let pid = child.id();
        let candidate = stray_candidate_for(pid, agent::StrayLayer::MonitorWrapper);

        // 25-12: floor deliberately disabled (Duration::ZERO) — this test
        // asserts the dry-run signalling behavior, not the age gate,
        // which Task 1's dedicated process_age tests cover.
        let results = reap_stray_candidates(&[candidate], true, std::time::Duration::ZERO);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].outcome, StrayReapOutcome::Reaped);
        assert!(
            agent::agent_running(pid),
            "dry-run must never actually signal the candidate"
        );

        let _ = child.kill();
        let _ = child.wait();
    }

    /// Task 2 behavior: a real candidate is cleared with VERIFIED death —
    /// counted as reaped only once `agent::terminate_and_verify` confirms
    /// it, never on the basis of a signal alone (999.44's own lesson: an
    /// unverified `SIGTERM` is what makes the CURRENT recovery path report
    /// success while the process keeps running).
    #[test]
    fn reap_stray_candidates_clears_a_real_child_with_verified_death() {
        let mut child = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .expect("spawn sleep fixture");
        let pid = child.id();
        let candidate = stray_candidate_for(pid, agent::StrayLayer::AdvanceChild);

        // 25-12: floor deliberately disabled (Duration::ZERO) — this test
        // asserts the signalling behavior, not the age gate, which
        // Task 1's dedicated process_age tests cover.
        let results = reap_stray_candidates(&[candidate], false, std::time::Duration::ZERO);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].outcome, StrayReapOutcome::Reaped);
        assert!(
            !agent::agent_running(pid),
            "a successful reap must leave the candidate verified dead, not merely signalled"
        );

        let _ = child.wait();
    }

    /// D-17's escalation, exercised through `gate_sweep`'s own reaping
    /// core (`reap_stray_candidates`) rather than the raw
    /// `terminate_and_verify` primitive 25-02 already covers directly — a
    /// `SIGTERM`-ignoring child must still be cleared, via `SIGKILL`,
    /// within the bounded wait.
    #[test]
    fn reap_stray_candidates_escalates_to_kill_for_a_term_ignoring_child() {
        let mut child = std::process::Command::new("sh")
            .arg("-c")
            .arg("trap '' TERM; sleep 30")
            .spawn()
            .expect("spawn TERM-ignoring fixture");
        let pid = child.id();
        // Give the shell a moment to install its trap before signalling.
        std::thread::sleep(std::time::Duration::from_millis(100));
        let candidate = stray_candidate_for(pid, agent::StrayLayer::MonitorWrapper);

        // 25-12: floor deliberately disabled (Duration::ZERO) — this test
        // asserts the SIGKILL escalation behavior, not the age gate,
        // which Task 1's dedicated process_age tests cover.
        let results = reap_stray_candidates(&[candidate], false, std::time::Duration::ZERO);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].outcome, StrayReapOutcome::Reaped);
        assert!(
            !agent::agent_running(pid),
            "a TERM-ignoring candidate must still be cleared via SIGKILL escalation"
        );

        let _ = child.wait();
    }

    /// The safety-critical case (999.47's "Related TOCTOU"): a candidate
    /// whose recorded start time no longer matches at signal time — the pid
    /// could have been recycled between discovery and this pass — must be
    /// refused, counted separately from a successful reap, and, the
    /// assertion that actually matters, the live process behind that pid
    /// must NOT be signalled.
    #[test]
    fn reap_stray_candidates_refuses_on_identity_mismatch_without_signalling() {
        let mut child = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .expect("spawn sleep fixture");
        let pid = child.id();
        let real_start = agent::process_start_time(pid).expect("read real start time");
        let mismatched = agent::StrayProcess {
            pid,
            start_time: real_start.wrapping_add(1),
            layer: agent::StrayLayer::MonitorWrapper,
        };

        // 25-12: floor deliberately disabled (Duration::ZERO) — this test
        // asserts the identity re-confirmation, not the age gate, which
        // Task 1's dedicated process_age tests cover.
        let results = reap_stray_candidates(&[mismatched], false, std::time::Duration::ZERO);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].outcome, StrayReapOutcome::IdentityMismatch);
        assert!(
            agent::agent_running(pid),
            "an identity mismatch must never be signalled — the whole point of the \
             re-confirmation"
        );

        let _ = child.kill();
        let _ = child.wait();
    }

    /// Task 1's whole point (25-12/999.47, production half): a candidate
    /// caught inside its own `fork()`->`execve()` window is genuinely the
    /// same process with genuinely the same recorded start time as its
    /// parent, so `is_same_process` alone cannot refuse it — the age floor
    /// must. The assertion that actually matters is not the outcome enum
    /// alone (a `TooYoung` outcome that still signalled would satisfy an
    /// enum-only check) but that the fixture is still alive afterwards.
    #[test]
    fn reap_stray_candidates_refuses_a_candidate_younger_than_the_minimum_age() {
        let mut child = std::process::Command::new("sh")
            .arg("-c")
            .arg("trap cleanup TERM INT; sleep 30")
            .spawn()
            .expect("spawn monitor-wrapper-shaped fixture");
        let pid = child.id();

        // 999.47: cross the exec-visibility barrier before building the
        // candidate from a cmdline-derived census, or this test races the
        // fixture's own fork()->execve() window (25-11).
        assert!(
            devflow_core::test_support::wait_for_exec_visibility(
                pid,
                "sh",
                devflow_core::test_support::EXEC_VISIBILITY_WAIT,
                devflow_core::test_support::EXEC_VISIBILITY_POLL,
            ),
            "pid {pid}: exec visibility timed out before the fixture became discoverable"
        );

        let candidate = stray_candidate_for(pid, agent::StrayLayer::MonitorWrapper);

        let results = reap_stray_candidates(&[candidate], false, agent::STRAY_MIN_AGE);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].outcome, StrayReapOutcome::TooYoung);
        assert!(
            agent::agent_running(pid),
            "a candidate refused for youth must never actually be signalled"
        );

        let _ = child.kill();
        let _ = child.wait();
    }

    /// The floor is a floor, not a blanket refusal: the same fixture shape
    /// as the test above, but with the floor disabled, IS reaped — without
    /// this, the test above would pass equally well against a reaper that
    /// refused unconditionally.
    #[test]
    fn reap_stray_candidates_reaps_when_the_floor_is_zero() {
        let mut child = std::process::Command::new("sh")
            .arg("-c")
            .arg("trap cleanup TERM INT; sleep 30")
            .spawn()
            .expect("spawn monitor-wrapper-shaped fixture");
        let pid = child.id();

        assert!(
            devflow_core::test_support::wait_for_exec_visibility(
                pid,
                "sh",
                devflow_core::test_support::EXEC_VISIBILITY_WAIT,
                devflow_core::test_support::EXEC_VISIBILITY_POLL,
            ),
            "pid {pid}: exec visibility timed out before the fixture became discoverable"
        );

        let candidate = stray_candidate_for(pid, agent::StrayLayer::MonitorWrapper);

        let results = reap_stray_candidates(&[candidate], false, std::time::Duration::ZERO);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].outcome, StrayReapOutcome::Reaped);
        assert!(
            !agent::agent_running(pid),
            "with the floor disabled, a genuine candidate must still be reaped and verified dead"
        );

        let _ = child.wait();
    }

    /// Task 2 behavior: fail-closed on an unresolvable age. Constructed
    /// from a dead pid (the same shape
    /// `stop_is_a_success_no_op_when_the_lock_names_a_dead_pid` uses:
    /// above the default kernel `pid_max`, guaranteed not alive) — this
    /// documents the actual interaction rather than assuming one:
    /// `agent::process_age` DOES return `None` for it, but
    /// `agent::is_same_process` is evaluated FIRST in
    /// `reap_stray_candidates` and ALSO fails for the identical reason
    /// (both derive from `agent::process_start_time`), so this candidate
    /// is classified `IdentityMismatch`, not `TooYoung` — the age check is
    /// never reached. That is not a design gap: an unconfirmable identity
    /// is refused regardless of which guard catches it first.
    ///
    /// The `TooYoung`-via-unresolvable-age arm specifically (identity
    /// re-confirmed alive, but `process_age` itself returns `None`)
    /// requires `/proc/uptime` or `sysconf(_SC_CLK_TCK)` to fail while
    /// `/proc/<pid>/stat` succeeds for the SAME live pid — a combination
    /// no black-box process fixture in this suite can construct without
    /// faking `/proc`. Left uncovered by assertion, following this file's
    /// own established precedent for an unreachable-by-black-box-test
    /// match arm (`stop_via_lock`'s wildcard arm, documented above this
    /// test group rather than faked). Covered by source reasoning
    /// instead: `reap_stray_candidates`'s age check —
    /// `agent::process_age(candidate.pid).is_some_and(|age| age >= min_age)`
    /// — treats `None` as `false` by construction of `Option::is_some_and`,
    /// so an unresolvable age can never accidentally satisfy the `>=`
    /// comparison and fall through to `Reaped`.
    #[test]
    fn reap_stray_candidates_refuses_a_dead_pid_as_identity_mismatch_before_the_age_check_runs() {
        let dead_pid = 9_999_999;
        assert!(
            agent::process_age(dead_pid).is_none(),
            "precondition: a dead pid's age must be unresolvable"
        );
        let candidate = agent::StrayProcess {
            pid: dead_pid,
            start_time: 0,
            layer: agent::StrayLayer::MonitorWrapper,
        };

        let results = reap_stray_candidates(&[candidate], false, agent::STRAY_MIN_AGE);

        assert_eq!(results.len(), 1);
        assert_eq!(
            results[0].outcome,
            StrayReapOutcome::IdentityMismatch,
            "is_same_process is evaluated before the age check and fails first for a dead pid"
        );
    }

    /// Flag wiring, exercised through the real `gate_sweep` entry point.
    /// `--dry-run` is provably safe to run against the live machine (it
    /// never signals anything, no matter what else is discovered) — this
    /// is the one test in this file that drives the actual CLI-facing
    /// function with `reap_strays: true` rather than the injectable core
    /// directly, deliberately avoiding a non-dry-run invocation: this
    /// machine's live process table legitimately contains other, unrelated
    /// devflow activity (concurrent phases in sibling worktrees) while
    /// these tests run, and a non-dry-run sweep is registry-independent by
    /// design — it would act on that too, not just this fixture.
    #[test]
    fn gate_sweep_reap_strays_dry_run_discovers_a_real_stray_without_signalling() {
        let mut child = std::process::Command::new("sh")
            .arg("-c")
            .arg("trap cleanup TERM INT; sleep 30")
            .spawn()
            .expect("spawn monitor-wrapper-shaped fixture");
        let pid = child.id();
        let dir = tempfile::tempdir().unwrap();

        // 999.47: `spawn()` returns after `fork()`, before `execve()` — cross
        // the exec-visibility barrier before reading the cmdline-derived
        // census, or this assertion races the child's own exec (25-11).
        assert!(
            devflow_core::test_support::wait_for_exec_visibility(
                pid,
                "sh",
                devflow_core::test_support::EXEC_VISIBILITY_WAIT,
                devflow_core::test_support::EXEC_VISIBILITY_POLL,
            ),
            "pid {pid}: exec visibility timed out before the fixture became discoverable"
        );

        assert!(
            agent::discover_stray_devflow_processes()
                .iter()
                .any(|p| p.pid == pid),
            "the fixture must be part of the real discovery census gate_sweep would use"
        );

        gate_sweep(None, true, Some(dir.path().to_path_buf()), true).unwrap();

        assert!(
            agent::agent_running(pid),
            "--dry-run must never signal a discovered stray, no matter what else the machine \
             is running"
        );

        let _ = child.kill();
        let _ = child.wait();
    }

    /// Task 2 behavior: without the flag, `gate_sweep` never even looks at
    /// the process table — a live stray-shaped fixture is neither
    /// discovered nor touched, and `gate_sweep`'s existing behavior stays
    /// byte-for-byte unchanged.
    #[test]
    fn gate_sweep_without_reap_strays_flag_ignores_a_live_stray() {
        let mut child = std::process::Command::new("sh")
            .arg("-c")
            .arg("trap cleanup TERM INT; sleep 30")
            .spawn()
            .expect("spawn monitor-wrapper-shaped fixture");
        let pid = child.id();
        let dir = tempfile::tempdir().unwrap();

        gate_sweep(None, false, Some(dir.path().to_path_buf()), false).unwrap();

        assert!(
            agent::agent_running(pid),
            "gate_sweep without --reap-strays must never signal anything discoverable only \
             via the process table"
        );

        let _ = child.kill();
        let _ = child.wait();
    }

    /// Retargeted (999.47/D-13). This test used to spawn a real `sleep`
    /// child and assert `stop()` refused to signal it, instrumented with
    /// the now-deprecated cmdline-basename predicate and a page of
    /// `/proc`-forensics helpers built to diagnose a CI flake
    /// ("MECHANISM CONFIRMED 2026-07-26": between `spawn()` returning and
    /// the child completing `execve`, its cmdline transiently reads as its
    /// parent's, which is what made a cmdline-based guard race). It is the
    /// serious one of 999.47's two tests: in CI it panicked on the error
    /// expectation, meaning the identity guard passed and `devflow stop`
    /// sent `SIGTERM` to an unrelated process — the guard's stated purpose
    /// failing end-to-end, not merely inferred.
    ///
    /// The retarget removes the race by construction rather than making it
    /// rarer: it asserts the `(pid, starttime)` guard `stop_via_lock`
    /// ACTUALLY uses today (the deprecated predicate is no longer even
    /// consulted for a decision — the cmdline-based mechanism it embodied
    /// was superseded before this retarget, and D-13 records that
    /// tightening it further to a single argv position was considered and
    /// rejected as ineffective, since the breaking marker sits inside the
    /// same inherited data). A LEGACY lock file — recording a pid with no
    /// start time at all, the format every lock file had before identity
    /// recording existed — must be refused, because identity cannot be
    /// confirmed for it. Uses this test's own pid, which is genuinely alive
    /// throughout, so there is no `spawn()` and therefore no `execve` to
    /// race.
    ///
    /// This is one of `stop_via_lock`'s three fail-closed match arms. The
    /// other two:
    /// - a recorded start time that no longer matches — covered
    ///   deterministically, no-spawn, by
    ///   `stop_refuses_when_the_recorded_start_time_does_not_match` below.
    /// - the match's final wildcard arm ("the lock file's holder could not
    ///   be read back for identity confirmation") — NOT exercised by any
    ///   test in this file. Source analysis: by the time `stop_via_lock`
    ///   reaches this match, `lock::holder()` has already confirmed this
    ///   SAME lock file's first line parses as a pid, via the identical
    ///   `read_holder_pid` helper `lock::holder_identity` calls again
    ///   moments later over UNCHANGED file content — so `holder_identity`'s
    ///   `recorded_pid` is guaranteed to equal the pid already in hand, and
    ///   its `Option<u64>` half is caught by the `Some((_, None))` arm
    ///   whenever it's absent. The wildcard arm is reachable only if the
    ///   lock file's content changes between those two sequential reads —
    ///   a genuine external race that no deterministic black-box test of
    ///   `stop()` can construct without reintroducing exactly the class of
    ///   flake this retarget exists to remove. Recorded here rather than
    ///   faked with a timing-dependent fixture.
    #[test]
    fn stop_refuses_to_signal_a_live_pid_that_fails_the_identity_check() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(200);

        // Our own pid is genuinely alive throughout — no spawn, so no
        // execve to race. The lock records ONLY the pid, matching every
        // lock file written before start times were recorded (999.47).
        let pid = std::process::id();
        let lock_path = root
            .join(".devflow")
            .join(format!("lock-{padded}", padded = phase.padded()));
        std::fs::create_dir_all(lock_path.parent().unwrap()).unwrap();
        std::fs::write(&lock_path, pid.to_string()).unwrap();

        // Confirm the fixture is genuinely the legacy shape this test
        // exercises — `lock::holder_identity` (what `stop_via_lock` itself
        // consults) must report the pid with NO recorded start time, the
        // exact input that drives `stop_via_lock`'s `Some((_, None))` arm.
        assert_eq!(
            lock::holder_identity(root, phase),
            Some((pid, None)),
            "the fixture must be a legacy lock (pid recorded, no start time) — the shape \
             stop_via_lock's identity guard treats as unconfirmable"
        );

        let err = stop(root, phase)
            .expect_err("a legacy lock with no recorded start time must be refused");
        let message = err.to_string();
        assert!(
            message.contains(&pid.to_string()),
            "error must name the pid it refused to signal, got: {message}"
        );
        assert!(
            message.contains("records no start time"),
            "error must say identity cannot be confirmed for a legacy lock, got: {message}"
        );
        assert!(
            lock_path.exists(),
            "the lock file must be untouched — stop must not signal anything"
        );
    }

    /// 999.47: a lock recording a start time that does not match the live
    /// process must be refused. This is the pid-recycling case — the lock
    /// named pid N, N died, and something unrelated now holds it.
    ///
    /// Unlike the cmdline check this replaced, the verdict does not depend
    /// on what the process *looks* like, so a `sleep` and a real devflow
    /// binary are rejected identically.
    #[test]
    fn stop_refuses_when_the_recorded_start_time_does_not_match() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(201);

        // Our own pid is genuinely alive and genuinely devflow-named, so the
        // old cmdline check would have happily signalled it. Only the start
        // time distinguishes "this process" from "whatever holds this pid".
        let pid = std::process::id();
        let real_start = devflow_core::agent::process_start_time(pid)
            .expect("read our own start time from /proc");
        let wrong_start = real_start + 1;

        let lock_path = root
            .join(".devflow")
            .join(format!("lock-{padded}", padded = phase.padded()));
        std::fs::create_dir_all(lock_path.parent().unwrap()).unwrap();
        std::fs::write(&lock_path, format!("{pid}\n{wrong_start}")).unwrap();

        let err = stop(root, phase).expect_err("a start-time mismatch must be refused");
        let message = err.to_string();
        assert!(
            message.contains(&pid.to_string()),
            "error must name the pid it refused to signal, got: {message}"
        );
        assert!(
            message.contains("not the process that took the lock"),
            "error must say the identity did not match, got: {message}"
        );
        assert!(
            lock_path.exists(),
            "the lock file must be untouched — stop must not signal anything"
        );
    }

    /// The positive case: when the lock's recorded identity matches the live
    /// process, `stop` proceeds. Without this, the refusal test above would
    /// pass equally well against a `stop` that refused unconditionally.
    #[test]
    fn stop_signals_the_holder_when_the_recorded_identity_matches() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(202);

        let mut child = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .expect("spawn sleep");
        let child_pid = child.id();

        // Record the child's OWN identity — the honest case, where the lock
        // holder really is the process named. Poll for the start time: a
        // freshly forked child may not have exec'd yet (999.47).
        let mut child_start = None;
        for _ in 0..100 {
            child_start = devflow_core::agent::process_start_time(child_pid);
            if child_start.is_some() {
                break;
            }
            std::thread::sleep(std::time::Duration::from_millis(10));
        }
        let child_start = child_start.expect("read the child's start time");

        let lock_path = root
            .join(".devflow")
            .join(format!("lock-{padded}", padded = phase.padded()));
        std::fs::create_dir_all(lock_path.parent().unwrap()).unwrap();
        std::fs::write(&lock_path, format!("{child_pid}\n{child_start}")).unwrap();

        let result = stop(root, phase);

        let _ = child.kill();
        let _ = child.wait();

        assert!(
            result.is_ok(),
            "a matching identity must be signalled, not refused: {result:?}"
        );
    }

    /// Task 2 behavior: a lock file naming a dead pid is stale — reclaimed
    /// silently, never signalled, and never an error. An explicit state
    /// file is present so this test exercises only the lock-fallback path
    /// under test, not Task 3's separate missing-state tolerance.
    #[test]
    fn stop_is_a_success_no_op_when_the_lock_names_a_dead_pid() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(202);
        let lock_path = root
            .join(".devflow")
            .join(format!("lock-{padded}", padded = phase.padded()));
        std::fs::create_dir_all(lock_path.parent().unwrap()).unwrap();
        // Above default kernel pid_max — guaranteed not alive.
        std::fs::write(&lock_path, "9999999").unwrap();
        let state = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
        workflow::save_state(&state).unwrap();

        stop(root, phase).expect("stop against a stale lock must succeed, not error");
    }

    /// T-23-51: a live `monitor_pid` recorded in `State` — with no open gate
    /// and no lock file — must never be treated as a signalling target.
    /// `stop` only ever looks at the lock file.
    #[test]
    fn stop_never_treats_monitor_pid_as_a_signalling_target() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let phase = PhaseId::new(203);
        let mut state = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
        state.monitor_pid = Some(std::process::id());
        workflow::save_state(&state).unwrap();

        stop(root, phase).expect(
            "stop must succeed — a recorded monitor_pid alone must never be signalled or \
             treated as a blocker",
        );
    }

    /// Task 3 behavior: no open gate, no lock file, and no persisted state
    /// at all — a phase that was never started — is a successful no-op.
    #[test]
    fn stop_is_a_success_no_op_with_no_gate_and_no_lock() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        stop(root, PhaseId::new(201)).expect("stop against nothing present must succeed");
    }

    /// 14-CR-03: a capture file SHORTER than the follower's offset means the
    /// next stage's monitor deleted and recreated it — the follower must
    /// restart from 0, not seek past EOF forever.
    #[test]
    fn rollover_offset_resets_on_shrunken_capture() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("capture");
        std::fs::write(&path, "abc").unwrap();

        // File (3 bytes) shorter than offset 10 → rollover → 0.
        assert_eq!(rollover_offset(&path, 10), 0);
        // File longer than or equal to the offset → keep the offset.
        assert_eq!(rollover_offset(&path, 3), 3);
        assert_eq!(rollover_offset(&path, 2), 2);
        // Missing file (mid-rollover gap) → keep the offset for now.
        assert_eq!(rollover_offset(&dir.path().join("gone"), 7), 7);
    }

    #[test]
    fn print_capture_from_tracks_offsets_across_appends() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("capture");
        std::fs::write(&path, "hello ").unwrap();
        let mut output = Vec::new();

        let offset = write_capture_from(&path, 0, &mut output).unwrap();
        assert_eq!(offset, 6);
        assert_eq!(output, b"hello ");

        // Nothing new: offset unchanged.
        output.clear();
        assert_eq!(write_capture_from(&path, offset, &mut output).unwrap(), 6);
        assert!(output.is_empty());

        use std::io::Write as _;
        let mut f = std::fs::OpenOptions::new()
            .append(true)
            .open(&path)
            .unwrap();
        f.write_all(b"world").unwrap();
        drop(f);
        assert_eq!(write_capture_from(&path, offset, &mut output).unwrap(), 11);
        assert_eq!(output, b"world");

        // Missing file is treated as "no new bytes yet".
        output.clear();
        assert_eq!(
            write_capture_from(Path::new("/nonexistent/x"), 4, &mut output).unwrap(),
            4
        );
        assert!(output.is_empty());
    }

    /// 13-06 dogfood regression (Codex leg): a fresh headless Codex run can
    /// never pass Define, so `start --agent codex` pre-flights on the
    /// phase's CONTEXT.md existing on develop.
    #[test]
    fn phase_artifact_on_develop_detects_context_and_fails_open() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let run = |args: &[&str]| {
            let out = devflow_core::test_support::git_command(root)
                .args(args)
                .output()
                .expect("spawn git");
            assert!(out.status.success(), "git {args:?} failed");
        };
        run(&["init", "-q", "-b", "main"]);
        run(&["config", "user.email", "t@e.st"]);
        run(&["config", "user.name", "t"]);
        run(&["config", "commit.gpgsign", "false"]);
        run(&["config", "core.hooksPath", "/dev/null"]);
        std::fs::create_dir_all(root.join(".planning/phases/03-widget")).unwrap();
        std::fs::write(root.join(".planning/phases/03-widget/03-CONTEXT.md"), "ctx").unwrap();
        run(&["add", "-A"]);
        run(&["commit", "-q", "-m", "init"]);
        run(&["branch", "develop"]);

        assert!(phase_artifact_on_develop(
            root,
            PhaseId::new(3),
            "-CONTEXT.md"
        ));
        assert!(!phase_artifact_on_develop(
            root,
            PhaseId::new(3),
            "-PLAN.md"
        ));
        assert!(!phase_artifact_on_develop(
            root,
            PhaseId::new(4),
            "-CONTEXT.md"
        ));

        // Fail-open: outside a repo (or with no develop branch) the
        // pre-flight must not block.
        let empty = tempfile::tempdir().unwrap();
        assert!(phase_artifact_on_develop(
            empty.path(),
            PhaseId::new(3),
            "-CONTEXT.md"
        ));
    }

    // -----------------------------------------------------------------
    // 17d: build provenance + self-dogfood staleness gate (D-17-D-21, Task 2)
    // -----------------------------------------------------------------

    /// D-21: the `workflow_started` payload carries every provenance field,
    /// tested directly without spawning a real agent. No `build_timestamp`
    /// field any more (CR-02, 17-11) — it was removed from `build.rs`
    /// entirely, not just this payload. Also pins the WR-02 redaction: the
    /// `exe_path` field must never carry a directory component (the
    /// operator's home directory / OS username), since `OPERATIONS.md`
    /// documents `events.jsonl` as a file that's safe to tail and paste.
    #[test]
    fn workflow_started_payload_carries_build_provenance() {
        let state = State::new(
            PhaseId::new(66),
            AgentKind::Claude,
            Mode::Auto,
            PathBuf::from("/repo"),
        );
        let payload = workflow_started_payload(&state);
        assert_eq!(payload["agent"], "claude");
        assert_eq!(payload["mode"], "auto");
        assert!(payload["version"].as_str().is_some());
        assert!(payload["commit"].is_string());
        assert!(payload["dirty"].is_string());
        assert!(
            payload.get("build_timestamp").is_none(),
            "build_timestamp was removed (CR-02) and must not reappear"
        );
        assert!(
            payload.get("exe_path").is_some(),
            "WR-02: exe_path key must still exist — a future refactor must not \
             satisfy the redaction assertion by deleting the field"
        );
        assert!(payload["exe_path"].is_string() || payload["exe_path"].is_null());
        if let Some(exe_path) = payload["exe_path"].as_str() {
            assert!(
                !exe_path.contains('/') && !exe_path.contains('\\'),
                "WR-02: exe_path must be a bare filename with no directory \
                 separator — OPERATIONS.md documents events.jsonl as safe to \
                 tail and paste, so a full absolute path here leaks the \
                 operator's home directory and OS username; got {exe_path:?}"
            );
        }
    }

    #[test]
    fn status_shows_pending_gate_prominently() {
        let dir = tempfile::tempdir().unwrap();
        let context = format!("first line\n\u{1b}[2J{}", "sensitive detail ".repeat(80));
        Gates::write_gate(dir.path(), PhaseId::new(16), Stage::Ship, &context).unwrap();
        let open = Gates::list_open(dir.path());

        let banner = render_pending_gate_banner(&open, u64::MAX).unwrap();

        assert!(banner.contains("PENDING GATE"));
        assert!(banner.contains("phase 16"));
        assert!(banner.contains("ship"));
        assert!(banner.contains("devflow gate approve 16 --stage ship"));
        assert!(banner.contains("devflow gate reject 16 --stage ship"));
        assert!(banner.contains("[truncated; full output in .devflow/]"));
        assert!(!banner.contains(&context));
        assert!(!banner.contains('\u{1b}'));
        assert!(banner.contains("ESCALATED"));
    }

    /// 23-03 Task 3: a gate whose age has crossed the escalation threshold
    /// renders with a trailing urgency marker.
    #[test]
    fn render_gate_age_marks_escalated_gate_urgent() {
        let now = 10_000u64;
        let old_timestamp = (now - GATE_ESCALATION_THRESHOLD_SECS - 60).to_string();

        let age = render_gate_age(&old_timestamp, now);

        assert!(
            age.ends_with('!'),
            "an escalated gate's age must carry a trailing urgency marker, got {age:?}"
        );
    }

    /// A gate younger than the escalation threshold renders with an age and
    /// no urgency marker.
    #[test]
    fn render_gate_age_no_marker_for_fresh_gate() {
        let now = 10_000u64;
        let fresh_timestamp = (now - 30).to_string();

        let age = render_gate_age(&fresh_timestamp, now);

        assert!(
            !age.ends_with('!'),
            "a fresh gate must not carry an urgency marker, got {age:?}"
        );
    }

    /// A `timestamp` that does not parse as `u64` must render an unknown
    /// age (`?`) rather than panicking or being silently dropped — the
    /// forensics record shows dropping unusual rows is exactly how the
    /// orphan population stayed invisible.
    #[test]
    fn render_gate_age_unknown_for_non_numeric_timestamp() {
        let age = render_gate_age("not-a-number", 10_000);
        assert_eq!(age, "?");
    }

    /// The `--all-roots` row-rendering must still include a gate whose
    /// timestamp is non-numeric — the row is present in the output, just
    /// with an unknown age, matching the acceptance criterion literally.
    #[test]
    fn all_roots_row_includes_gate_with_non_numeric_timestamp() {
        let gate = OpenGate {
            phase: PhaseId::new(42),
            stage: Stage::Ship,
            context: "ctx".to_string(),
            timestamp: "not-a-number".to_string(),
        };

        let row = render_all_roots_gate_row(Path::new("/tmp/some-root"), &gate, 10_000);

        assert!(row.contains("42"), "row must still name the phase: {row}");
        assert!(row.contains('?'), "row must render the unknown age: {row}");
    }

    /// 21a: `recovery_hints` returns a `resume` hint for a stuck phase,
    /// additionally an `advance` hint when the phase is gate-pending
    /// (answer the gate, then advance), and nothing for a non-stuck phase.
    #[test]
    fn recovery_hints_includes_resume_for_stuck() {
        let dir = tempfile::tempdir().unwrap();
        let state = State::new(
            PhaseId::new(7),
            AgentKind::Claude,
            Mode::Auto,
            dir.path().to_path_buf(),
        );

        let hints = recovery_hints(&state, Liveness::Stuck);

        assert_eq!(hints, vec!["devflow resume --phase 7".to_string()]);
    }

    #[test]
    fn recovery_hints_includes_advance_when_stuck_and_gate_pending() {
        let dir = tempfile::tempdir().unwrap();
        let mut state = State::new(
            PhaseId::new(7),
            AgentKind::Claude,
            Mode::Auto,
            dir.path().to_path_buf(),
        );
        state.gate_pending = true;

        let hints = recovery_hints(&state, Liveness::Stuck);

        assert_eq!(
            hints,
            vec![
                "devflow resume --phase 7".to_string(),
                "devflow advance --phase 7".to_string(),
            ]
        );
    }

    #[test]
    fn recovery_hints_empty_for_healthy() {
        let dir = tempfile::tempdir().unwrap();
        let state = State::new(
            PhaseId::new(7),
            AgentKind::Claude,
            Mode::Auto,
            dir.path().to_path_buf(),
        );

        assert!(recovery_hints(&state, Liveness::Healthy).is_empty());
    }

    /// 21a / 999.30 IN-01: the shared one-pass event summary's
    /// `stage_launched_ts` is the LAST `stage_launched` event's `ts` — the
    /// real stage-entry time — and is `None` without one, never falling
    /// back to any other field. `status` now sources this from
    /// `events::last_events_by_phase` instead of a per-phase rescan.
    #[test]
    fn stage_launched_ts_none_without_event() {
        let dir = tempfile::tempdir().unwrap();
        assert_eq!(
            events::last_events_by_phase(dir.path())
                .get(&PhaseId::new(7))
                .and_then(|s| s.stage_launched_ts),
            None
        );
    }

    /// The closing proof for the 3/3 cross-AI review MEDIUM
    /// (21-REVIEWS.md): a phase whose latest `stage_launched` event is ~90s
    /// old but whose phase-level `started_at` is ~30m old must report the
    /// ~90s stage age — the summary's `stage_launched_ts` must never be
    /// sourced from `state.started_at`.
    #[test]
    fn stage_launched_ts_reflects_event_age_not_phase_started_at() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        let stage_ts = now - 90;
        let phase_started_at = now - 30 * 60;

        let mut state = State::new(
            PhaseId::new(7),
            AgentKind::Claude,
            Mode::Auto,
            root.to_path_buf(),
        );
        state.started_at = phase_started_at.to_string();
        workflow::save_state(&state).unwrap();

        events::emit(
            root,
            PhaseId::new(7),
            "stage_launched",
            serde_json::json!({"stage": "code", "agent": "claude", "monitor_pid": 1}),
        );
        // events::emit always stamps `ts` with the current time; rewrite it
        // to a fixed, known-past value so the assertion is deterministic
        // instead of racing the live clock.
        let events_path = devflow_core::events::events_path(root);
        let rewritten: String = std::fs::read_to_string(&events_path)
            .unwrap()
            .lines()
            .map(|line| {
                let mut value: serde_json::Value = serde_json::from_str(line).unwrap();
                value["ts"] = serde_json::json!(stage_ts);
                value.to_string()
            })
            .collect::<Vec<_>>()
            .join("\n")
            + "\n";
        std::fs::write(&events_path, rewritten).unwrap();

        let ts = events::last_events_by_phase(root)
            .get(&PhaseId::new(7))
            .and_then(|s| s.stage_launched_ts);
        assert_eq!(ts, Some(stage_ts));

        let line = render_stage_progress_line(Stage::Code, ts);
        assert!(line.contains("1m ago"), "expected ~90s age, got: {line}");
        assert!(
            !line.contains("30m ago"),
            "must not render phase-level started_at age: {line}"
        );
    }

    #[test]
    fn render_stage_progress_line_omits_age_without_stage_launched_event() {
        assert_eq!(
            render_stage_progress_line(Stage::Plan, None),
            "  in stage plan"
        );
    }

    /// Unit tests for the pure `doctor` reconciliation core (18a). Each test
    /// builds a `PhaseFacts` directly — no repository, no I/O — proving
    /// `reconcile_phase` is a predicate over facts alone.
    #[cfg(test)]
    mod doctor_reconciliation {
        use super::*;

        /// A fully-agreeing baseline: `reconcile_phase` over this returns
        /// zero findings. Each test overrides only the field(s) needed to
        /// trigger the one check it's proving.
        fn agreeing_facts(phase: PhaseId) -> PhaseFacts {
            PhaseFacts {
                phase,
                stage: Stage::Code,
                gate_pending: false,
                agent_pid: Some(4242),
                agent_alive: true,
                monitor_pid: Some(4343),
                monitor_alive: true,
                last_event: Some("stage_launched".into()),
                last_launched_stage: Some(Stage::Code),
                open_gate_stages: Vec::new(),
                feature_branch_exists: true,
                stopped: false,
            }
        }

        #[test]
        fn reconcile_phase_returns_no_findings_when_all_agree() {
            let facts = agreeing_facts(PhaseId::new(1));
            assert!(reconcile_phase(&facts).is_empty());
        }

        #[test]
        fn reconcile_phase_flags_gate_pending_without_open_gate() {
            let facts = PhaseFacts {
                gate_pending: true,
                open_gate_stages: Vec::new(),
                ..agreeing_facts(PhaseId::new(2))
            };
            let findings = reconcile_phase(&facts);
            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].severity, Severity::Problem);
            assert!(findings[0].detail.contains("gate_pending is true"));
            assert_eq!(
                findings[0].repair.as_deref(),
                Some("devflow resume --phase 2")
            );
        }

        #[test]
        fn reconcile_phase_flags_orphan_open_gate() {
            let facts = PhaseFacts {
                gate_pending: false,
                open_gate_stages: vec![Stage::Validate],
                ..agreeing_facts(PhaseId::new(3))
            };
            let findings = reconcile_phase(&facts);
            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].severity, Severity::Problem);
            assert!(findings[0].detail.contains("gate open for stage validate"));
            assert_eq!(
                findings[0].repair.as_deref(),
                Some("devflow gate approve 3 --stage validate")
            );
        }

        #[test]
        fn reconcile_phase_flags_dead_agent_at_agent_stage() {
            let facts = PhaseFacts {
                agent_pid: Some(999_999),
                agent_alive: false,
                ..agreeing_facts(PhaseId::new(4))
            };
            let findings = reconcile_phase(&facts);
            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].severity, Severity::Problem);
            assert!(findings[0].detail.contains("agent pid 999999"));
            assert_eq!(
                findings[0].repair.as_deref(),
                Some("devflow resume --phase 4")
            );
        }

        #[test]
        fn reconcile_phase_flags_stage_event_drift() {
            let facts = PhaseFacts {
                stage: Stage::Validate,
                last_launched_stage: Some(Stage::Code),
                ..agreeing_facts(PhaseId::new(5))
            };
            let findings = reconcile_phase(&facts);
            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].severity, Severity::Warn);
            assert!(
                findings[0]
                    .detail
                    .contains("last stage_launched event named code")
            );
            assert!(findings[0].repair.is_none());
        }

        #[test]
        fn reconcile_phase_flags_missing_feature_branch() {
            let facts = PhaseFacts {
                stage: Stage::Plan,
                last_launched_stage: Some(Stage::Plan),
                feature_branch_exists: false,
                ..agreeing_facts(PhaseId::new(6))
            };
            let findings = reconcile_phase(&facts);
            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].severity, Severity::Warn);
            assert!(findings[0].detail.contains("feature/phase-06"));
            assert!(findings[0].repair.is_none());
        }

        /// 18b: a dead monitor with a dead agent is `Stuck` — nothing will
        /// call `devflow advance` for this phase — and reports a `Problem`
        /// finding with a `devflow resume --phase N` repair.
        #[test]
        fn reconcile_reports_stuck_when_monitor_and_agent_are_both_dead() {
            let facts = PhaseFacts {
                monitor_pid: Some(5150),
                monitor_alive: false,
                agent_pid: Some(4242),
                agent_alive: false,
                ..agreeing_facts(PhaseId::new(8))
            };
            let findings = reconcile_phase(&facts);
            let monitor_finding = findings
                .iter()
                .find(|f| f.detail.contains("monitor pid"))
                .expect("expected a monitor finding when monitor and agent are both dead");
            assert_eq!(monitor_finding.severity, Severity::Problem);
            assert!(monitor_finding.detail.contains("monitor pid 5150"));
            assert_eq!(
                monitor_finding.repair.as_deref(),
                Some("devflow resume --phase 8")
            );
        }

        /// 20c (D-09 + review: Codex HIGH — the doctor gap is bigger than
        /// `check_dead_agent`): a phase intentionally halted by `devflow
        /// start --until <stage>` sits at an agent stage with a dead agent
        /// pid on disk. `check_dead_agent` must recognize `facts.stopped`
        /// and report ZERO findings — this is not a crash.
        #[test]
        fn reconcile_phase_ignores_dead_agent_when_stopped() {
            let facts = PhaseFacts {
                stage: Stage::Plan,
                agent_pid: Some(999_999),
                agent_alive: false,
                stopped: true,
                ..agreeing_facts(PhaseId::new(11))
            };
            let findings = reconcile_phase(&facts);
            assert!(
                findings.iter().all(|f| f.severity != Severity::Problem),
                "a --until-stopped phase must yield zero Problem findings, got: \
                 {:?}",
                findings.iter().map(|f| &f.detail).collect::<Vec<_>>()
            );
        }

        /// 20c (D-09 + review: Codex HIGH — the doctor gap is bigger than
        /// `check_dead_agent`): the same stopped phase may also carry a
        /// stale `monitor_pid` (though the stop path clears it — this
        /// proves the guard holds even if that clear were ever bypassed).
        /// `check_dead_monitor` must also recognize `facts.stopped` and
        /// report ZERO findings.
        #[test]
        fn reconcile_phase_ignores_dead_monitor_when_stopped() {
            let facts = PhaseFacts {
                stage: Stage::Plan,
                monitor_pid: Some(5150),
                monitor_alive: false,
                agent_pid: Some(4242),
                agent_alive: false,
                stopped: true,
                ..agreeing_facts(PhaseId::new(12))
            };
            let findings = reconcile_phase(&facts);
            assert!(
                findings.iter().all(|f| f.severity != Severity::Problem),
                "a --until-stopped phase must yield zero Problem findings even with a \
                 stale monitor_pid, got: {:?}",
                findings.iter().map(|f| &f.detail).collect::<Vec<_>>()
            );
        }

        /// 18b (T-18-11): an unrecorded monitor is unknown, not a problem —
        /// a state file written by a pre-18b binary must never render as
        /// stuck.
        #[test]
        fn reconcile_is_silent_when_monitor_pid_is_unrecorded() {
            let facts = PhaseFacts {
                monitor_pid: None,
                monitor_alive: false,
                ..agreeing_facts(PhaseId::new(9))
            };
            assert!(
                reconcile_phase(&facts).is_empty(),
                "an unrecorded monitor must never produce a finding"
            );
        }

        /// 18b: a live monitor with a dead agent is a normal between-stages
        /// moment (the monitor hasn't advanced the phase yet), not a monitor
        /// finding. `check_dead_agent`'s own pre-existing finding for the
        /// dead agent pid is unrelated to this check and out of this plan's
        /// scope.
        #[test]
        fn reconcile_is_silent_when_monitor_alive_and_agent_dead() {
            let facts = PhaseFacts {
                monitor_pid: Some(5150),
                monitor_alive: true,
                agent_alive: false,
                ..agreeing_facts(PhaseId::new(10))
            };
            let findings = reconcile_phase(&facts);
            assert!(
                findings.iter().all(|f| !f.detail.contains("monitor pid")),
                "a live monitor with a dead agent must not produce a monitor finding"
            );
        }

        /// Several checks trigger simultaneously; the returned findings must
        /// come back in the fixed order `reconcile_phase` evaluates checks
        /// in, not in whatever order the facts happen to be populated.
        #[test]
        fn reconcile_phase_ordering_is_input_order_independent() {
            let facts = PhaseFacts {
                gate_pending: true,
                agent_pid: Some(999_999),
                agent_alive: false,
                monitor_pid: Some(999_998),
                monitor_alive: false,
                last_launched_stage: Some(Stage::Validate),
                open_gate_stages: Vec::new(),
                feature_branch_exists: false,
                ..agreeing_facts(PhaseId::new(7))
            };
            let findings = reconcile_phase(&facts);
            let severities: Vec<Severity> = findings.iter().map(|f| f.severity).collect();
            assert_eq!(
                severities,
                vec![
                    Severity::Problem, // check_gate_pending_without_gate
                    Severity::Problem, // check_dead_agent
                    Severity::Problem, // check_dead_monitor
                    Severity::Warn,    // check_stage_event_drift
                    Severity::Warn,    // check_missing_branch
                ]
            );
            assert!(findings[0].detail.contains("gate_pending is true"));
            assert!(findings[1].detail.contains("agent pid 999999"));
            assert!(findings[2].detail.contains("monitor pid 999998"));
            assert!(
                findings[3]
                    .detail
                    .contains("last stage_launched event named validate")
            );
            assert!(findings[4].detail.contains("feature/phase-07"));
        }

        /// `doctor`'s idle-project path (Task 2, 18a): the exact code path
        /// `doctor(root, false)` runs for its reconciliation section is
        /// `collect_phase_facts` + `render_reconciliation_text` — asserted
        /// directly here rather than capturing process stdout, since this
        /// codebase has no stdout-capture dependency and this phase adds no
        /// new ones (18-RESEARCH.md).
        #[test]
        fn doctor_reports_no_active_phases_when_idle() {
            let dir = tempfile::tempdir().unwrap();
            let facts = collect_phase_facts(dir.path());
            assert!(facts.is_empty());
            assert!(render_reconciliation_text(&facts).contains("no active phases"));
        }

        #[test]
        fn doctor_reports_gate_pending_without_gate_file() {
            let dir = tempfile::tempdir().unwrap();
            let root = dir.path();
            let phase = PhaseId::new(90);
            let mut state = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
            state.stage = Stage::Validate;
            state.gate_pending = true;
            workflow::save_state(&state).unwrap();

            let facts = collect_phase_facts(root);
            assert_eq!(facts.len(), 1);
            let text = render_reconciliation_text(&facts);
            assert!(text.contains(&format!("phase {phase}: gate_pending is true")));
            assert!(text.contains(&format!("repair: devflow resume --phase {phase}")));
        }

        /// WR-01 (18-fix): `doctor --json` must emit ONE JSON document, not
        /// two concatenated top-level arrays. Exercises the exact
        /// composition `doctor()`'s `--json` path uses (`doctor_json_body`),
        /// then round-trips it through `serde_json::to_string`/`from_str` —
        /// the failure mode this reproduces (pre-fix) is a single-document
        /// parser (`json.load`, `JSON.parse`) raising "Extra data" on the
        /// old two-array output; `jq` tolerated it (NDJSON-style streaming),
        /// which is why it went unnoticed.
        #[test]
        fn doctor_json_is_a_single_object_with_environment_and_reconciliation() {
            let checks = vec![Check {
                name: "git".into(),
                status: "ok".into(),
                version: Some("2.40.0".into()),
                install_hint: None,
            }];

            let dir = tempfile::tempdir().unwrap();
            let root = dir.path();
            let phase = PhaseId::new(92);
            let mut state = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
            state.stage = Stage::Validate;
            state.gate_pending = true; // mismatched: no gate file — produces a finding
            workflow::save_state(&state).unwrap();
            let facts = collect_phase_facts(root);

            let body = doctor_json_body(&checks, &facts, &[], &[]);
            let serialized = serde_json::to_string(&body).unwrap();
            let reparsed: serde_json::Value = serde_json::from_str(&serialized)
                .expect("doctor --json must be single-document JSON, not two concatenated arrays");

            assert!(
                reparsed.get("environment").is_some(),
                "must carry the tool checks under \"environment\": {reparsed}"
            );
            assert!(
                reparsed.get("reconciliation").is_some(),
                "must carry the reconciliation findings under \"reconciliation\": {reparsed}"
            );
            assert!(
                reparsed.get("planning_doc_staleness").is_some(),
                "21b: must carry the planning-doc findings under a THIRD key, \
                 never a second concatenated array: {reparsed}"
            );
            assert!(
                reparsed.get("stray_processes").is_some(),
                "999.44: must carry the stray-process findings under a FOURTH key, \
                 never a second concatenated array: {reparsed}"
            );
            assert_eq!(
                reparsed.as_object().unwrap().len(),
                4,
                "doctor --json must have exactly four top-level keys: {reparsed}"
            );
            assert!(reparsed["environment"].is_array());
            assert!(reparsed["reconciliation"].is_array());
            assert!(reparsed["planning_doc_staleness"].is_array());
            assert!(reparsed["stray_processes"].is_array());
            let reconciliation = reparsed["reconciliation"].as_array().unwrap();
            assert!(
                !reconciliation.is_empty(),
                "the mismatched gate_pending fixture must produce at least one finding"
            );
            assert!(
                reconciliation.iter().any(|f| f["detail"]
                    .as_str()
                    .unwrap_or("")
                    .contains("gate_pending is true")),
                "must carry the gate_pending finding: {reconciliation:?}"
            );
        }

        /// T-18-02: running `doctor` twice against a mismatched fixture must
        /// leave `.devflow/` byte-identical — no state rewrite, no event
        /// append, no gate file appears or disappears.
        #[test]
        fn doctor_is_read_only_on_a_mismatched_project() {
            let dir = tempfile::tempdir().unwrap();
            let root = dir.path();
            let phase = PhaseId::new(91);
            let mut state = State::new(phase, AgentKind::Claude, Mode::Auto, root.to_path_buf());
            state.stage = Stage::Validate;
            state.gate_pending = true; // mismatched: no gate file will exist
            workflow::save_state(&state).unwrap();
            events::emit(
                root,
                phase,
                "stage_launched",
                serde_json::json!({"stage": "code"}),
            );

            let state_path = workflow::state_path(root, phase);
            let before_len = std::fs::metadata(&state_path).unwrap().len();
            let before_modified = std::fs::metadata(&state_path).unwrap().modified().unwrap();
            let events_log = events::events_path(root);
            let before_lines = std::fs::read_to_string(&events_log)
                .unwrap()
                .lines()
                .count();

            doctor(root, false).unwrap();
            doctor(root, false).unwrap();

            let after_len = std::fs::metadata(&state_path).unwrap().len();
            let after_modified = std::fs::metadata(&state_path).unwrap().modified().unwrap();
            let after_lines = std::fs::read_to_string(&events_log)
                .unwrap()
                .lines()
                .count();

            assert_eq!(
                before_len, after_len,
                "doctor must not rewrite the state file"
            );
            assert_eq!(
                before_modified, after_modified,
                "doctor must not touch the state file's mtime"
            );
            assert_eq!(
                before_lines, after_lines,
                "doctor must not append to events.jsonl"
            );
        }
    }

    /// Unit tests for `doctor`'s stray-process finding (999.44 / DEN-68).
    /// `build_stray_process_findings` takes an injected `&[StrayProcess]`
    /// list, so most of these run with zero I/O and no real orphan process
    /// on the machine running the test — mirrors `reconcile_planning_docs`'s
    /// zero-I/O discipline above. The one test that DOES need a real
    /// process is read-only (`doctor` never signals), so it is safe to run
    /// on a machine that may have unrelated, legitimate devflow activity
    /// running concurrently — it only ever asserts the fixture it spawned
    /// itself is still alive, never acts on anything else `discover_stray_
    /// devflow_processes` might also see.
    #[cfg(test)]
    mod stray_process_finding {
        use super::*;

        #[test]
        fn build_stray_process_findings_is_empty_for_no_strays() {
            assert!(build_stray_process_findings(&[]).is_empty());
        }

        #[test]
        fn build_stray_process_findings_names_pid_layer_and_repair() {
            let strays = vec![agent::StrayProcess {
                pid: 424242,
                start_time: 0,
                layer: agent::StrayLayer::MonitorWrapper,
            }];
            let findings = build_stray_process_findings(&strays);
            assert_eq!(findings.len(), 1);
            let finding = &findings[0];
            assert_eq!(finding.severity, Severity::Problem);
            assert!(finding.detail.contains("424242"));
            assert!(finding.detail.contains("monitor wrapper"));
            assert!(
                finding
                    .repair
                    .as_deref()
                    .unwrap()
                    .contains("--reap-strays --dry-run"),
                "the repair must name the preview form first, not the destructive form alone: \
                 {:?}",
                finding.repair
            );
            assert!(
                !finding.detail.contains('/'),
                "no finding may embed a filesystem path (WR-02): {}",
                finding.detail
            );
        }

        #[test]
        fn build_stray_process_findings_names_advance_child_layer() {
            let strays = vec![agent::StrayProcess {
                pid: 424243,
                start_time: 0,
                layer: agent::StrayLayer::AdvanceChild,
            }];
            let findings = build_stray_process_findings(&strays);
            assert!(findings[0].detail.contains("advance child"));
        }

        /// `doctor`'s existing text output is byte-for-byte unchanged when
        /// there is no stray to report — the new section must contribute
        /// NOTHING, not even a header line, unlike the always-present
        /// reconciliation/planning-docs sections.
        #[test]
        fn render_stray_process_text_is_empty_when_no_strays() {
            assert_eq!(render_stray_process_text(&[]), "");
        }

        #[test]
        fn render_stray_process_text_names_pid_and_repair_when_present() {
            let strays = vec![agent::StrayProcess {
                pid: 555555,
                start_time: 0,
                layer: agent::StrayLayer::MonitorWrapper,
            }];
            let text = render_stray_process_text(&build_stray_process_findings(&strays));
            assert!(text.contains("555555"));
            assert!(text.contains("repair: devflow gate sweep --reap-strays --dry-run"));
        }

        #[test]
        fn doctor_json_body_carries_stray_processes_as_a_fourth_key() {
            let checks: Vec<Check> = Vec::new();
            let facts: Vec<PhaseFacts> = Vec::new();
            let stray_findings = vec![StrayProcessFinding {
                pid: 1,
                layer: "monitor wrapper",
                severity: Severity::Problem,
                detail: "detail".to_string(),
                repair: None,
            }];
            let body = doctor_json_body(&checks, &facts, &[], &stray_findings);
            let obj = body.as_object().unwrap();
            assert_eq!(obj.len(), 4, "must be exactly four top-level keys: {body}");
            assert!(obj.contains_key("stray_processes"));
            let strays = obj["stray_processes"].as_array().unwrap();
            assert_eq!(strays.len(), 1);
            assert_eq!(strays[0]["pid"], 1);
        }

        /// Behavior test (999.44's exact reproduction, Phase 18-01's
        /// read-only proof extended to strays): a real process shaped
        /// exactly like the monitor wrapper `discover_stray_devflow_
        /// processes` matches is spawned, and `doctor` is run TWICE
        /// against it — directly, and end to end through `doctor()`
        /// itself. Every run must report it as a finding and leave it
        /// alive: `doctor` never signals, no matter how many times it
        /// runs.
        #[test]
        fn doctor_finds_a_real_stray_and_never_signals_it_across_two_runs() {
            let mut child = std::process::Command::new("sh")
                .arg("-c")
                .arg("trap cleanup TERM INT; sleep 30")
                .spawn()
                .expect("spawn monitor-wrapper-shaped fixture");
            let pid = child.id();

            let dir = tempfile::tempdir().unwrap();

            // 999.47: cross the exec-visibility barrier before either census
            // read below, or both races the fixture's own fork()->execve()
            // window (25-11).
            assert!(
                devflow_core::test_support::wait_for_exec_visibility(
                    pid,
                    "sh",
                    devflow_core::test_support::EXEC_VISIBILITY_WAIT,
                    devflow_core::test_support::EXEC_VISIBILITY_POLL,
                ),
                "pid {pid}: exec visibility timed out before the fixture became discoverable"
            );

            let first = collect_stray_process_findings(dir.path());
            assert!(
                agent::agent_running(pid),
                "fixture must still be alive after the first collection"
            );
            let second = collect_stray_process_findings(dir.path());
            assert!(
                agent::agent_running(pid),
                "fixture must still be alive after the second collection — doctor's stray \
                 finding must never signal"
            );

            assert!(
                first.iter().any(|f| f.pid == pid),
                "the fixture must be reported by the first run"
            );
            assert!(
                second.iter().any(|f| f.pid == pid),
                "the fixture must be reported by the second run"
            );

            // Also exercise doctor() itself, end to end, twice — not just
            // the pure finding collector — proving the full read-only path.
            doctor(dir.path(), false).unwrap();
            assert!(agent::agent_running(pid), "doctor() itself must not signal");
            doctor(dir.path(), false).unwrap();
            assert!(
                agent::agent_running(pid),
                "doctor() must remain read-only across repeated runs"
            );

            let _ = child.kill();
            let _ = child.wait();
        }
    }

    /// Task 2 (CR-01/T-25-15-02): the rewritten `detail` states only
    /// what the code actually checked — matched DevFlow's structural
    /// argv shape, owned by the caller, named by no registered root's
    /// state file or lock file — and no longer asserts a conclusion
    /// (unqualified "reachable through no registry entry, lock file, or
    /// state file") the code never established.
    #[test]
    fn stray_finding_detail_states_only_what_was_checked() {
        let strays = vec![agent::StrayProcess {
            pid: 909090,
            start_time: 0,
            layer: agent::StrayLayer::MonitorWrapper,
        }];
        let findings = build_stray_process_findings(&strays);
        assert_eq!(findings.len(), 1);
        let finding = &findings[0];

        assert!(finding.detail.contains("909090"));
        assert!(finding.detail.contains("monitor-wrapper"));
        assert!(finding.detail.contains("owned by the calling user"));
        assert!(
            finding
                .detail
                .contains("named by no registered project root's state file or lock file"),
            "the detail must name the specific checked absence, not an unqualified \
             conclusion: {}",
            finding.detail
        );
        // Reconstructed from two halves rather than embedded as one literal
        // string constant: the acceptance gate greps the whole file for
        // this exact old phrase and expects a `0` count, and a literal
        // occurrence here (even inside a negative assertion) would make
        // that grep self-defeating.
        let old_unverified_orphan_phrase = format!(
            "{}{}",
            "reachable through no registry entry, ", "lock file, or state file"
        );
        assert!(
            !finding.detail.contains(&old_unverified_orphan_phrase),
            "the old, unverified-orphan phrasing must be gone: {}",
            finding.detail
        );
        assert!(
            !finding.detail.contains('/'),
            "no finding may embed a filesystem path (WR-02): {}",
            finding.detail
        );
        assert!(
            finding
                .repair
                .as_deref()
                .unwrap()
                .starts_with("devflow gate sweep --reap-strays --dry-run"),
            "the repair must name the --dry-run preview form first: {:?}",
            finding.repair
        );
    }

    /// Spawn a real process shaped like the monitor wrapper Layer 1
    /// matches, crossing the exec-visibility barrier before returning
    /// (999.47/25-11) so every caller of this helper is guaranteed the
    /// fixture's own `execve()` has completed before any census read.
    fn spawn_wrapper_shaped_fixture() -> std::process::Child {
        let child = std::process::Command::new("sh")
            .arg("-c")
            .arg("trap cleanup TERM INT; sleep 30")
            .spawn()
            .expect("spawn monitor-wrapper-shaped fixture");
        let pid = child.id();
        assert!(
            devflow_core::test_support::wait_for_exec_visibility(
                pid,
                "sh",
                devflow_core::test_support::EXEC_VISIBILITY_WAIT,
                devflow_core::test_support::EXEC_VISIBILITY_POLL,
            ),
            "pid {pid}: exec visibility timed out before the fixture became discoverable"
        );
        child
    }

    /// Task 1's tracer (CR-01, 999.44/DEN-68): one discovery pass names
    /// a live `monitor_pid`-recorded pid, a live lock-holder pid, and a
    /// genuine orphan under no registered root at all. Only the orphan
    /// may survive `unreachable_stray_candidates`'s filter. All three
    /// pids are real, spawned processes crossed through
    /// `wait_for_exec_visibility` — not synthesised numbers — so the
    /// census and the filter are both exercised for real, in one pass.
    #[test]
    fn reachable_pids_are_excluded_from_both_the_findings_and_the_reap_candidates() {
        let mut state_child = spawn_wrapper_shaped_fixture();
        let mut lock_child = spawn_wrapper_shaped_fixture();
        let mut orphan_child = spawn_wrapper_shaped_fixture();
        let state_pid = state_child.id();
        let lock_pid = lock_child.id();
        let orphan_pid = orphan_child.id();

        let cache_dir = tempfile::tempdir().unwrap();
        let project_root_guard = tempfile::tempdir().unwrap();
        let project_root = project_root_guard.path().to_path_buf();

        // Phase 1: a real `State` naming `state_pid` as its monitor.
        let mut state = State::new(
            PhaseId::new(1),
            AgentKind::Claude,
            Mode::Auto,
            project_root.clone(),
        );
        state.monitor_pid = Some(state_pid);
        workflow::save_state(&state).unwrap();

        // Phase 2: a real `State` (so `list_states` enumerates the
        // phase at all — mirrors real operation, where a phase with an
        // active lock also has a persisted state), deliberately with NO
        // `monitor_pid`, so this phase's only path into the reachable
        // set is the lock file below, not the monitor_pid path Phase 1
        // already exercises.
        let state_2 = State::new(
            PhaseId::new(2),
            AgentKind::Claude,
            Mode::Auto,
            project_root.clone(),
        );
        workflow::save_state(&state_2).unwrap();

        // A lock file for phase 2, written directly in the documented
        // `.devflow/lock-{phase:02}` shape (lock.rs:4, :236) — pid on
        // line 1, start time on line 2 — naming `lock_pid` as holder.
        let lock_start_time = agent::process_start_time(lock_pid)
            .expect("must read the lock fixture's own recorded start time");
        let devflow_dir = project_root.join(".devflow");
        std::fs::create_dir_all(&devflow_dir).unwrap();
        std::fs::write(
            devflow_dir.join("lock-02"),
            format!("{lock_pid}\n{lock_start_time}"),
        )
        .unwrap();
        // Load-bearing: turns the implicit lock-file-format coupling
        // into a self-checking one — a future format change fails this
        // test loudly instead of silently exercising only half of it.
        assert_eq!(
            lock::holder_identity(&project_root, PhaseId::new(2)),
            Some((lock_pid, Some(lock_start_time))),
            "the directly-written lock file must read back through holder_identity exactly \
             as one lock::acquire itself wrote would"
        );

        registry::register_in(cache_dir.path(), &project_root, PhaseId::new(1)).unwrap();
        let registered_roots: Vec<PathBuf> = registry::load_roots_in(cache_dir.path())
            .into_iter()
            .map(|r| r.project_root)
            .collect();

        let reachable = registry_reachable_pids(&registered_roots);
        assert!(
            reachable.contains(&state_pid),
            "state_pid must be reachable via its recorded monitor_pid"
        );
        assert!(
            reachable.contains(&lock_pid),
            "lock_pid must be reachable via the lock file's holder_identity"
        );
        assert!(
            !reachable.contains(&orphan_pid),
            "orphan_pid is named by no state file and no lock file, so it must not be \
             reachable"
        );

        // Without this, the test could pass vacuously because the
        // census never saw the fixtures at all (T-25-15-11).
        let census = agent::discover_stray_devflow_processes();
        for (pid, label) in [
            (state_pid, "state"),
            (lock_pid, "lock"),
            (orphan_pid, "orphan"),
        ] {
            assert!(
                census.iter().any(|p| p.pid == pid),
                "the {label} fixture (pid {pid}) must be part of the real /proc census"
            );
        }

        let retained = retain_unreachable_strays(&census, &reachable);
        assert!(
            retained.iter().any(|p| p.pid == orphan_pid),
            "the orphan must survive the filter"
        );
        assert!(
            !retained.iter().any(|p| p.pid == state_pid),
            "the state-named pid must be filtered out"
        );
        assert!(
            !retained.iter().any(|p| p.pid == lock_pid),
            "the lock-held pid must be filtered out"
        );

        let findings = build_stray_process_findings(&retained);
        assert!(
            findings.iter().any(|f| f.pid == orphan_pid),
            "the orphan must produce a finding"
        );
        assert!(
            !findings.iter().any(|f| f.pid == state_pid),
            "the state-named pid must produce no finding"
        );
        assert!(
            !findings.iter().any(|f| f.pid == lock_pid),
            "the lock-held pid must produce no finding"
        );

        // Reap all three with a VERIFIED signal, never a bare one
        // (mirrors `reap_strays_e2e.rs:202-223`'s own teardown), plus a
        // final `wait()` on each to reclaim the zombie regardless.
        for pid in [state_pid, lock_pid, orphan_pid] {
            agent::terminate_and_verify(
                pid,
                agent::TERMINATE_VERIFY_WAIT,
                agent::TERMINATE_VERIFY_POLL,
            );
        }
        let _ = state_child.wait();
        let _ = lock_child.wait();
        let _ = orphan_child.wait();
    }

    /// 999.44's originating case, as an executable assertion rather than
    /// prose (see `25-15-PLAN.md`'s `<why_this_does_not_violate_d17>`):
    /// once a registered root is deleted off disk, its state file and
    /// lock file are gone with it, so it contributes ZERO pids to the
    /// reachable set — even while the OS process it named is still
    /// alive. The filter is therefore structurally incapable of hiding
    /// this population.
    #[test]
    fn a_deleted_root_contributes_nothing_to_the_reachable_set() {
        let outer = tempfile::tempdir().unwrap();
        let root = outer.path().join("project-root");
        std::fs::create_dir_all(&root).unwrap();

        let mut child = spawn_wrapper_shaped_fixture();
        let pid = child.id();

        let mut state = State::new(PhaseId::new(1), AgentKind::Claude, Mode::Auto, root.clone());
        state.monitor_pid = Some(pid);
        workflow::save_state(&state).unwrap();

        assert!(
            registry_reachable_pids(std::slice::from_ref(&root)).contains(&pid),
            "the pid must be reachable while its root and state file still exist"
        );

        std::fs::remove_dir_all(&root).unwrap();
        assert!(!root.exists(), "the root must actually be gone");
        assert!(
            agent::agent_running(pid),
            "deleting the root must not touch the still-alive process"
        );

        let reachable_after_deletion = registry_reachable_pids(std::slice::from_ref(&root));
        assert!(
            reachable_after_deletion.is_empty(),
            "a deleted root's state file and lock file are gone with it, so it must \
             contribute nothing to the reachable set: got {reachable_after_deletion:?}"
        );

        let census = vec![agent::StrayProcess {
            pid,
            start_time: agent::process_start_time(pid)
                .expect("fixture must still be alive and readable"),
            layer: agent::StrayLayer::MonitorWrapper,
        }];
        let retained = retain_unreachable_strays(&census, &reachable_after_deletion);
        assert!(
            retained.iter().any(|p| p.pid == pid),
            "with the reachable set empty, the pid must still be treated as a stray"
        );

        // Verified reap (mirrors `reap_strays_e2e.rs:202-223`), plus a
        // final `wait()` to reclaim the zombie regardless.
        agent::terminate_and_verify(
            pid,
            agent::TERMINATE_VERIFY_WAIT,
            agent::TERMINATE_VERIFY_POLL,
        );
        let _ = child.wait();
    }

    /// Unit tests for the pure planning-doc staleness core (21b, D-04/D-05).
    /// `reconcile_planning_docs` takes an injected `tag_lookup` closure, so
    /// every test here runs with zero I/O and no real repository — mirrors
    /// `doctor_reconciliation`'s zero-I/O discipline above.
    #[cfg(test)]
    mod planning_doc_staleness {
        use super::*;

        const SAMPLE_TABLE: &str = "\
| Phase | Name | Version |
|---|---|---|
| 20 | Release Correctness | 1.7.0 |
| 10 | Logging | — |
| 1–5 | Core workflow | 0.1.0–0.6.0 |
| 9 | OSS Polish | 1.2.0 |
| 11 | GSD-Native | 1.2.0 |
";

        #[test]
        fn parse_planning_doc_versions_skips_non_semver_cells() {
            let rows = parse_planning_doc_versions(SAMPLE_TABLE, "ROADMAP.md");
            assert_eq!(
                rows,
                vec![
                    ("ROADMAP.md phase 20".to_string(), "1.7.0".to_string()),
                    ("ROADMAP.md phase 9".to_string(), "1.2.0".to_string()),
                    ("ROADMAP.md phase 11".to_string(), "1.2.0".to_string()),
                ],
                "em-dash and range cells must be skipped; duplicate versions across \
                 phases (9 and 11 both claim 1.2.0) must both still parse"
            );
        }

        #[test]
        fn parse_planning_doc_versions_accepts_v_prefixed_cells() {
            let text = "| Phase | Description | Version | Date |\n\
                         |---|---|---|---|\n\
                         | 18 | Dogfood Hardening | v1.5.0 | 2026-07-21 |\n";
            let rows = parse_planning_doc_versions(text, "STATE.md");
            assert_eq!(
                rows,
                vec![("STATE.md phase 18".to_string(), "v1.5.0".to_string())]
            );
        }

        #[test]
        fn parse_semver_rejects_ranges_and_em_dash() {
            assert_eq!(parse_semver("1.7.0"), Some((1, 7, 0)));
            assert_eq!(parse_semver("v1.7.0"), Some((1, 7, 0)));
            assert_eq!(parse_semver("0.1.0–0.6.0"), None);
            assert_eq!(parse_semver(""), None);
            assert_eq!(parse_semver("1.7"), None);
            assert_eq!(parse_semver("1.7.0.1"), None);
        }

        #[test]
        fn reconcile_planning_docs_flags_problem_for_unreachable_post_cutoff_version() {
            let rows = vec![("ROADMAP.md phase 20".to_string(), "1.7.0".to_string())];
            let mut lookup = |_tag: &str| false; // no tag exists / unreachable
            let findings = reconcile_planning_docs(&rows, &mut lookup);
            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].severity, Severity::Problem);
            assert!(
                findings[0].repair.is_none(),
                "D-04: detection-only, no repair"
            );
            assert!(findings[0].detail.contains("v1.7.0"));
        }

        #[test]
        fn reconcile_planning_docs_downgrades_pre_cutoff_mismatch_to_warn() {
            // Phase 7 claims 1.0.0 in this repo's real ROADMAP.md/STATE.md,
            // but no v1.0.0 tag exists (tags start at v1.0.1) — must never
            // surface as Problem (RESEARCH Pitfall #2).
            let rows = vec![("ROADMAP.md phase 7".to_string(), "1.0.0".to_string())];
            let mut lookup = |_tag: &str| false;
            let findings = reconcile_planning_docs(&rows, &mut lookup);
            assert_eq!(findings.len(), 1);
            assert_eq!(
                findings[0].severity,
                Severity::Warn,
                "pre-v1.5.0 mismatches must downgrade to Warn, never Problem"
            );
            assert!(findings[0].repair.is_none());
        }

        #[test]
        fn reconcile_planning_docs_numeric_cutoff_is_not_lexicographic() {
            // A lexicographic string compare would sort "1.10.0" < "1.5.0"
            // and wrongly downgrade a real future release to Warn (Codex
            // MEDIUM, cross-AI review). The cutoff must compare
            // parse_semver's numeric tuple instead.
            let rows = vec![
                ("label A".to_string(), "1.10.0".to_string()),
                ("label B".to_string(), "1.4.0".to_string()),
            ];
            let mut lookup = |_tag: &str| false;
            let findings = reconcile_planning_docs(&rows, &mut lookup);
            assert_eq!(findings.len(), 2);
            assert_eq!(
                findings[0].severity,
                Severity::Problem,
                "1.10.0 is numerically >= v1.5.0 (post-cutoff), even though \
                 \"1.10.0\" < \"1.5.0\" as a string"
            );
            assert_eq!(
                findings[1].severity,
                Severity::Warn,
                "1.4.0 is numerically < v1.5.0 (pre-cutoff)"
            );
        }

        #[test]
        fn reconcile_planning_docs_produces_no_finding_when_tag_is_reachable() {
            let rows = vec![("ROADMAP.md phase 20".to_string(), "1.7.0".to_string())];
            let mut lookup = |_tag: &str| true; // tag exists and is reachable
            let findings = reconcile_planning_docs(&rows, &mut lookup);
            assert!(findings.is_empty());
        }

        #[test]
        fn reconcile_planning_docs_normalizes_bare_cell_to_v_prefixed_tag() {
            let rows = vec![("ROADMAP.md phase 20".to_string(), "1.7.0".to_string())];
            let mut seen_tag = None;
            let mut lookup = |tag: &str| {
                seen_tag = Some(tag.to_string());
                true
            };
            reconcile_planning_docs(&rows, &mut lookup);
            assert_eq!(seen_tag.as_deref(), Some("v1.7.0"));
        }

        #[test]
        fn reconcile_planning_docs_skips_a_malformed_row_defensively() {
            // Defensive path: reconcile must never panic even if handed a
            // row whose version cell isn't a semver (parse_planning_doc_versions
            // already filters this upstream, but reconcile must degrade, not die).
            let rows = vec![("bad row".to_string(), "not-a-version".to_string())];
            let mut lookup = |_tag: &str| false;
            let findings = reconcile_planning_docs(&rows, &mut lookup);
            assert!(findings.is_empty());
        }

        /// Fixture-backed proof of `tag_exists_and_reachable`'s two-check
        /// contract, mirroring `staleness::init_repo_with_diverged_commit`'s
        /// idiom: a real tempdir git repo with a tagged, reachable commit,
        /// an untagged commit, and a commit on a diverged, unreachable branch.
        fn init_tagged_repo(root: &Path) {
            let git = |args: &[&str]| {
                assert!(
                    devflow_core::test_support::git_command(root)
                        .args(args)
                        .output()
                        .unwrap()
                        .status
                        .success(),
                    "git {args:?} failed"
                );
            };
            git(&["init", "-q", "-b", "main"]);
            git(&["config", "user.email", "t@e.st"]);
            git(&["config", "user.name", "t"]);
            git(&["config", "commit.gpgsign", "false"]);
            git(&["config", "tag.gpgsign", "false"]);
            git(&["config", "core.hooksPath", "/dev/null"]);
            std::fs::write(root.join("a.txt"), "one").unwrap();
            git(&["add", "."]);
            git(&["commit", "-q", "-m", "base"]);
            git(&["tag", "v1.7.0"]);

            git(&["checkout", "-q", "-b", "side"]);
            std::fs::write(root.join("side.txt"), "s").unwrap();
            git(&["add", "."]);
            git(&["commit", "-q", "-m", "side"]);
            git(&["tag", "v9.9.9"]); // tagged, but only reachable from `side`, not `main`

            git(&["checkout", "-q", "main"]);
        }

        #[test]
        fn tag_exists_and_reachable_true_for_a_tagged_ancestor() {
            let dir = tempfile::tempdir().unwrap();
            init_tagged_repo(dir.path());
            assert!(tag_exists_and_reachable(dir.path(), "v1.7.0", "main"));
        }

        #[test]
        fn tag_exists_and_reachable_false_for_a_missing_tag() {
            let dir = tempfile::tempdir().unwrap();
            init_tagged_repo(dir.path());
            assert!(!tag_exists_and_reachable(dir.path(), "v0.0.1", "main"));
        }

        #[test]
        fn tag_exists_and_reachable_false_for_a_tag_unreachable_from_base() {
            let dir = tempfile::tempdir().unwrap();
            init_tagged_repo(dir.path());
            assert!(!tag_exists_and_reachable(dir.path(), "v9.9.9", "main"));
        }

        // -----------------------------------------------------------------
        // 27-04 (D-01/D-03): tag_exists_and_reachable's two direct sites
        // (base-commit lines 2886, 2892) now scrubbed via the git_command
        // constructor, called with project_root
        // -----------------------------------------------------------------

        /// D-01/D-03: `tag_exists_and_reachable` produces the correct,
        /// non-hijacked answer for `project_root` even when a hostile
        /// `GIT_DIR` points at an unrelated foreign repository that DOES
        /// carry a tag by the name under test — the dangerous, false-positive
        /// direction T-27-01 names explicitly (a foreign repository
        /// asserting a release tag already exists feeds a release-cut
        /// decision). `GIT_DIR` is genuinely present in a process's
        /// environment for this proof — never via `std::env::set_var` on
        /// THIS test's own process (Rust 2024 `unsafe`, unsound under
        /// threaded tests — Phase 25 D-14, and the plan's own instruction).
        /// Instead it is set only on a freshly spawned CHILD process: this
        /// same test binary, re-invoked filtered to just this one test —
        /// the same "spawned child only" shape used by
        /// `staleness::tests::embedded_commit_is_stale_resolves_execution_root_under_a_hostile_git_dir`
        /// (27-04), needed here for the identical reason:
        /// `tag_exists_and_reachable` is a private function with no
        /// injection point of its own, and its two git subcommands
        /// (`rev-parse --verify`, `merge-base --is-ancestor`) are both
        /// ref-resolving — the class 27-01-SUMMARY.md Deviation 1 verified
        /// (git 2.55.0) genuinely honors a `GIT_DIR` chained directly onto a
        /// `git_command`-built Command, so a literal reproduction of that
        /// shape against the real function would prove nothing specific to
        /// it.
        #[test]
        fn tag_exists_and_reachable_resolves_caller_root_under_a_hostile_git_dir() {
            const INNER_ROOT: &str = "DEVFLOW_27_04_TAG_INNER_ROOT";
            const INNER_TAG: &str = "DEVFLOW_27_04_TAG_INNER_TAG";
            const INNER_BASE: &str = "DEVFLOW_27_04_TAG_INNER_BASE";

            if let Ok(root) = std::env::var(INNER_ROOT) {
                // Inner mode: this process was spawned by the outer half
                // below with GIT_DIR pointed at a foreign repository that
                // DOES carry the tag under test — scoped to this child
                // process only.
                let tag = std::env::var(INNER_TAG).expect("inner tag env set by parent");
                let base = std::env::var(INNER_BASE).expect("inner base env set by parent");
                assert!(
                    !tag_exists_and_reachable(Path::new(&root), &tag, &base),
                    "a hostile GIT_DIR pointed at a foreign repository that DOES \
                     carry this tag must not cause tag_exists_and_reachable to \
                     report it as belonging to project_root"
                );
                return;
            }

            // Outer mode: build the real repository with a base branch and
            // NO tag by the name under test, plus a second, unrelated
            // foreign repository (reusing init_tagged_repo verbatim) that
            // DOES carry that tag, reachable from its own base branch — the
            // dangerous direction.
            let real = tempfile::tempdir().unwrap();
            let real_root = real.path();
            let git = |args: &[&str]| {
                assert!(
                    devflow_core::test_support::git_command(real_root)
                        .args(args)
                        .output()
                        .unwrap()
                        .status
                        .success(),
                    "git {args:?} failed"
                );
            };
            git(&["init", "-q", "-b", "main"]);
            git(&["config", "user.email", "t@e.st"]);
            git(&["config", "user.name", "t"]);
            git(&["config", "commit.gpgsign", "false"]);
            git(&["config", "core.hooksPath", "/dev/null"]);
            std::fs::write(real_root.join("a.txt"), "one").unwrap();
            git(&["add", "."]);
            git(&["commit", "-q", "-m", "base"]);
            // real_root deliberately carries no tag named v1.7.0.

            let foreign = tempfile::tempdir().unwrap();
            init_tagged_repo(foreign.path());

            let exe = std::env::current_exe().expect("current_exe for child re-invocation");
            let status = std::process::Command::new(&exe)
                .arg("tag_exists_and_reachable_resolves_caller_root_under_a_hostile_git_dir")
                .arg("--test-threads=1")
                .env(INNER_ROOT, real_root.to_str().unwrap())
                .env(INNER_TAG, "v1.7.0")
                .env(INNER_BASE, "main")
                .env("GIT_DIR", foreign.path().join(".git"))
                .status()
                .expect("spawn hostile child test process");
            assert!(
                status.success(),
                "child test process (hostile GIT_DIR pointed at a foreign repo \
                 that DOES carry v1.7.0) must still report \
                 tag_exists_and_reachable == false for the real repository; \
                 child exit status {status:?}"
            );
        }

        /// D-05/D-04: a MISSING `.planning/ROADMAP.md`/`STATE.md` must yield
        /// no findings and never an error — `doctor` must not fabricate a
        /// `Problem` from an absent doc. Proven against a tempdir with no
        /// `.planning/` directory at all, not just asserted.
        #[test]
        fn collect_planning_doc_findings_missing_files_yield_no_findings_not_error() {
            let dir = tempfile::tempdir().unwrap();
            let findings = collect_planning_doc_findings(dir.path());
            assert!(
                findings.is_empty(),
                "a project with no .planning/ dir at all must yield zero findings, not an error"
            );
        }

        /// 999.30 / DEN-55 WR-02: `collect_planning_doc_findings` must
        /// reconcile against the named `MAIN` production branch, not an
        /// unlinked duplicate. `init_tagged_repo` tags `v9.9.9` only on
        /// `side`, unreachable from `main` — so a ROADMAP.md claiming
        /// `v9.9.9` must surface a `Problem` when reconciled against `MAIN`.
        #[test]
        fn collect_planning_doc_findings_reconciles_against_main() {
            let dir = tempfile::tempdir().unwrap();
            init_tagged_repo(dir.path());
            std::fs::create_dir_all(dir.path().join(".planning")).unwrap();
            std::fs::write(
                dir.path().join(".planning/ROADMAP.md"),
                "| Phase | Name | Version |\n|---|---|---|\n| 99 | Fixture | 9.9.9 |\n",
            )
            .unwrap();

            let findings = collect_planning_doc_findings(dir.path());

            assert_eq!(findings.len(), 1);
            assert_eq!(findings[0].severity, Severity::Problem);
        }

        #[test]
        fn render_planning_doc_text_reports_consistent_when_no_findings() {
            assert_eq!(
                render_planning_doc_text(&[]),
                "\nplanning docs: consistent with git tags\n"
            );
        }

        #[test]
        fn render_planning_doc_text_lists_each_finding_detail() {
            let findings = vec![PlanningDocFinding {
                source: "ROADMAP.md phase 20".to_string(),
                claim: "ROADMAP.md phase 20 claims v1.7.0".to_string(),
                severity: Severity::Problem,
                detail: "ROADMAP.md phase 20 claims v1.7.0, but no git tag `v1.7.0` exists"
                    .to_string(),
                repair: None,
            }];
            let text = render_planning_doc_text(&findings);
            assert!(text.contains("[problem]"));
            assert!(text.contains("ROADMAP.md phase 20 claims v1.7.0"));
        }

        #[test]
        fn render_planning_doc_findings_json_is_an_array_of_objects() {
            let findings = vec![PlanningDocFinding {
                source: "ROADMAP.md phase 20".to_string(),
                claim: "ROADMAP.md phase 20 claims v1.7.0".to_string(),
                severity: Severity::Problem,
                detail: "detail text".to_string(),
                repair: None,
            }];
            let value = render_planning_doc_findings_json(&findings);
            assert!(value.is_array());
            let arr = value.as_array().unwrap();
            assert_eq!(arr.len(), 1);
            assert_eq!(arr[0]["severity"], "problem");
            assert_eq!(arr[0]["source"], "ROADMAP.md phase 20");
            assert_eq!(arr[0]["repair"], serde_json::Value::Null);
        }

        /// D-05/Pattern 2: `doctor --json` must stay a SINGLE JSON object
        /// with `planning_doc_staleness` as a THIRD key, never a second
        /// top-level array — the exact WR-01 regression class this phase
        /// must not reintroduce.
        #[test]
        fn doctor_json_body_carries_planning_doc_staleness_as_a_third_key() {
            let checks: Vec<Check> = Vec::new();
            let facts: Vec<PhaseFacts> = Vec::new();
            let doc_findings = vec![PlanningDocFinding {
                source: "ROADMAP.md phase 20".to_string(),
                claim: "claim".to_string(),
                severity: Severity::Problem,
                detail: "detail".to_string(),
                repair: None,
            }];
            let body = doctor_json_body(&checks, &facts, &doc_findings, &[]);
            let obj = body.as_object().unwrap();
            assert_eq!(
                obj.len(),
                4,
                "must be exactly {{environment, reconciliation, planning_doc_staleness, \
                 stray_processes}}: {body}"
            );
            assert!(obj.contains_key("environment"));
            assert!(obj.contains_key("reconciliation"));
            let staleness = obj["planning_doc_staleness"].as_array().unwrap();
            assert_eq!(staleness.len(), 1);
            assert!(obj.contains_key("stray_processes"));
        }
    }

    /// 23-06 Task 3 acceptance: `--require-shipped` is exit-code-stable on
    /// the strict `shipped` predicate ALONE — a shipped phase's call
    /// succeeds, an otherwise-identical unshipped phase's call fails, with
    /// every other evidence field held constant (neither phase has any git
    /// state or persisted workflow state in this fixture).
    #[test]
    fn evidence_require_shipped_exits_ok_iff_the_phase_has_shipped() {
        let dir = tempfile::tempdir().unwrap();
        events::emit(
            dir.path(),
            PhaseId::new(30),
            "workflow_shipped",
            serde_json::json!({"stage": "ship"}),
        );

        assert!(evidence(dir.path(), PhaseId::new(30), false, true).is_ok());
        assert!(evidence(dir.path(), PhaseId::new(31), false, true).is_err());
    }

    /// 23-06 Task 3 acceptance: a phase that only stopped (`--until`) must
    /// fail `--require-shipped` with a message that says so explicitly,
    /// not a generic "not shipped" — this is the confusing case named in
    /// Task 1's acceptance criteria ("it finished but it did not ship").
    #[test]
    fn evidence_require_shipped_names_stopped_at_rather_than_generic_not_shipped() {
        let dir = tempfile::tempdir().unwrap();
        events::emit(
            dir.path(),
            PhaseId::new(32),
            "workflow_finished",
            serde_json::json!({"reason": "stopped_at", "stage": "plan"}),
        );

        let err = evidence(dir.path(), PhaseId::new(32), false, true).unwrap_err();
        let message = err.to_string();
        assert!(
            message.contains("stopped"),
            "message must name the stopped-at case, got: {message}"
        );
    }

    /// 23-06 Task 3 acceptance: the `--require-shipped` failure message is a
    /// single line (it ends up verbatim in a gate context read on a phone)
    /// and names the phase.
    #[test]
    fn evidence_require_shipped_failure_message_is_single_line_and_names_phase() {
        let dir = tempfile::tempdir().unwrap();

        let err = evidence(dir.path(), PhaseId::new(33), false, true).unwrap_err();
        let message = err.to_string();
        assert!(
            !message.contains('\n'),
            "message must be one line: {message:?}"
        );
        assert!(
            message.contains("33"),
            "message must name the phase: {message}"
        );
    }

    #[test]
    fn changelog_version_check_flags_mismatch_and_passes_on_agreement() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        let write_workspace = |version: &str| {
            std::fs::write(
                root.join("Cargo.toml"),
                format!("[workspace.package]\nversion = \"{version}\"\n"),
            )
            .unwrap();
        };

        // Mismatch: changelog 2.4.0 vs workspace 2.5.0 → fail, "Cargo.toml ahead".
        write_workspace("2.5.0");
        std::fs::write(root.join("CHANGELOG.md"), "## 2.4.0 — 2026-01-01\n").unwrap();
        let mismatch = check_changelog_version(root);
        assert_eq!(mismatch.status, "fail");
        assert!(
            mismatch.version.unwrap().contains("Cargo.toml ahead"),
            "workspace 2.5.0 is newer than changelog 2.4.0"
        );

        // Reverse: changelog 2.6.0 vs workspace 2.5.0 → fail, "changelog ahead".
        write_workspace("2.5.0");
        std::fs::write(root.join("CHANGELOG.md"), "## 2.6.0 — 2026-01-01\n").unwrap();
        let reverse = check_changelog_version(root);
        assert_eq!(reverse.status, "fail");
        assert!(
            reverse.version.unwrap().contains("changelog ahead"),
            "changelog 2.6.0 is newer than workspace 2.5.0"
        );

        // Agreement: both 2.5.0 → ok.
        write_workspace("2.5.0");
        std::fs::write(root.join("CHANGELOG.md"), "## 2.5.0 — 2026-08-15\n").unwrap();
        assert_eq!(check_changelog_version(root).status, "ok");

        // Bracketed Keep-a-Changelog heading `## [2.5.0]` → parses, ok.
        std::fs::write(root.join("CHANGELOG.md"), "## [2.5.0] - 2026-08-15\n").unwrap();
        assert_eq!(check_changelog_version(root).status, "ok");

        // Missing heading → warn, not a hard fail.
        std::fs::write(root.join("CHANGELOG.md"), "no heading here\n").unwrap();
        assert_eq!(check_changelog_version(root).status, "warn");
    }
}