spar-cli 0.3.5

Two AI coding agents alternate implementing and reviewing GitHub issues until a PR converges.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
//! The command line.

use std::collections::{BTreeSet, VecDeque};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

use clap::{Args, Parser, Subcommand};

use crate::agent::{self, Agent};
use crate::checkin;
use crate::config::{self, Config};
use crate::error::Result;
use crate::followups;
use crate::model::{Issue, IssueRun, ItemKind, Plan, Status};
use crate::proc::{self, ExecOpts};
use crate::repo::{Repo, WriteSummary};
use crate::review;
use crate::review_only;
use crate::split;
use crate::style;
use crate::tracker;
use crate::triage;
use crate::{bail, log, logdim, logging, logwarn, spar_err};

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Parser, Debug)]
#[command(
    name = "spar",
    version = VERSION,
    about = "Two coding agents alternate implementing and reviewing GitHub issues.",
    long_about = "Two coding agents alternate implementing and reviewing GitHub issues until a \
                  pull request converges. Neither agent reviews its own most recent edit.\n\n\
                  Arguments are issue numbers for `run` and `triage`, and pull request numbers \
                  for `resume`, `review`, and `checkin`. `split` takes either. Omit them and \
                  spar takes everything open, up to --limit. `split` applies that limit once to \
                  issues and once to pull requests. `followup` takes none: it works the queue in \
                  .spar/followups.md, and an entry there has no number to name.",
    max_term_width = 96
)]
pub struct Cli {
    /// Suppress progress logging. Warnings, errors, and the final summary still print.
    #[arg(short, long, global = true)]
    pub quiet: bool,

    #[command(subcommand)]
    pub command: Command,
}

#[derive(Subcommand, Debug)]
pub enum Command {
    /// Triage the issues, then work them in dependency order.
    Run {
        /// Issue numbers. Omit to take every open issue, up to --limit.
        issues: Vec<i64>,
        #[command(flatten)]
        common: Common,
        #[command(flatten)]
        loop_flags: LoopFlags,
        #[command(flatten)]
        triage_flags: TriageFlags,
        /// Where to write the triage plan.
        #[arg(long, default_value = "plan.json")]
        plan_out: PathBuf,
        /// Work in the main checkout instead of an isolated worktree per issue.
        #[arg(long)]
        no_worktrees: bool,
    },

    /// Work the follow-ups recorded in .spar/followups.md.
    ///
    /// One agent reads every entry against the current checkout and rules on
    /// it: still there, already fixed, not worth it, or a duplicate. What
    /// survives is filed as an issue and worked like any other, which means
    /// both agents still triage it before anything is implemented. An entry
    /// that was filed or dropped leaves the queue and is kept in
    /// .spar/followups.done.md.
    ///
    /// Takes no numbers: an entry has no number a person could type. --limit
    /// caps how many are taken, and --min-number does nothing here.
    Followup {
        #[command(flatten)]
        common: Common,
        #[command(flatten)]
        loop_flags: LoopFlags,
        #[command(flatten)]
        triage_flags: TriageFlags,
        /// Read this instead of .spar/followups.md.
        #[arg(long, value_name = "PATH")]
        file: Option<PathBuf>,
        /// Print the verdicts and stop. Nothing is filed and no file is touched.
        #[arg(long)]
        screen_only: bool,
        /// File the issues and stop, leaving them for a later `spar run`.
        #[arg(long, conflicts_with = "screen_only")]
        file_only: bool,
        /// Where to write the triage plan.
        #[arg(long, default_value = "plan.json")]
        plan_out: PathBuf,
        /// Work in the main checkout instead of an isolated worktree per issue.
        #[arg(long)]
        no_worktrees: bool,
    },

    /// Triage only. Writes the plan and touches nothing else.
    Triage {
        /// Issue numbers. Omit to take every open issue, up to --limit.
        issues: Vec<i64>,
        #[command(flatten)]
        common: Common,
        #[arg(long, default_value = "plan.json")]
        plan_out: PathBuf,
    },

    /// Continue the review loop on existing PRs, including ones spar did not create.
    Resume {
        /// Pull request numbers. Omit to take every open PR, up to --limit.
        prs: Vec<i64>,
        #[command(flatten)]
        common: Common,
        #[command(flatten)]
        loop_flags: LoopFlags,
        /// Which agent reviews next, overriding saved custody after the PR head changed.
        #[arg(long = "next", value_name = "AGENT")]
        next_actor: Option<String>,
    },

    /// Answer the comments on a pull request, and act on the ones worth acting on.
    ///
    /// Reads every comment somebody else left that has not been answered. Both
    /// agents judge each one. A change they both agree is right and belongs
    /// here is made, pushed, answered in its own thread, and the thread marked
    /// resolved. One they both judge wrong gets the reason and the thread is
    /// left open for you. One they disagree about is parked.
    Checkin {
        /// Pull request numbers. An issue number resolves to its open PR.
        /// Omit to take every open PR, up to --limit.
        items: Vec<i64>,
        #[command(flatten)]
        common: Common,
        /// Print every reply and every change instead of posting or pushing.
        #[arg(long)]
        dry_run: bool,
        /// Answer in words only. Nothing is committed, pushed, or resolved.
        #[arg(long)]
        reply_only: bool,
        /// Act on a comment from anyone, not only from somebody who can write
        /// to this repository.
        #[arg(long)]
        any_author: bool,
        /// Answer comments spar already answered, ignoring what it recorded.
        #[arg(long)]
        again: bool,
        /// Leave worktrees in place afterwards, for inspection.
        #[arg(long)]
        keep_worktrees: bool,
    },

    /// Review pull requests without changing them, including from a fork.
    ///
    /// Both agents review independently, then rule on each other's findings,
    /// then answer the objections. Nothing is committed, pushed, or merged.
    Review {
        /// Pull request numbers. An issue number resolves to its open PR.
        /// Omit to take every open PR, up to --limit.
        items: Vec<i64>,
        #[command(flatten)]
        common: Common,
        /// Print the review instead of posting it.
        #[arg(long)]
        dry_run: bool,
        /// Adjudication passes. 1 is two independent reviews with no
        /// cross-checking, 2 adds it, 3 adds a rebuttal on what they dispute.
        #[arg(long)]
        max_rounds: Option<u32>,
    },

    /// Break an issue or a pull request into smaller ones.
    ///
    /// One agent proposes the parts with the code open, the other rules on the
    /// proposal: accept, reject, or accept with named parts struck.
    /// Disagreement resolves toward not splitting.
    ///
    /// An issue's parts are filed as issues and the parent is rewritten into a
    /// checklist that points at them. A pull request's parts each get their own
    /// branch and their own pull request, and the original is left open and
    /// otherwise untouched: split branches use create-only pushes, and the
    /// parent is never closed or rebased. A pull request from a fork is proposed
    /// in a comment rather than split.
    ///
    /// It decomposes and stops. Nothing is triaged, implemented, or merged. Run
    /// the reported child numbers next, or enable `decompose_trackers` and run
    /// the issue parent.
    Split {
        /// Issue or pull request numbers. Omit to go through every open issue
        /// and every open pull request, and split what is worth splitting.
        items: Vec<i64>,
        #[command(flatten)]
        common: Common,
        /// Print the proposal and write nothing.
        #[arg(long)]
        dry_run: bool,
        /// Start a separate split even when one is recorded or retained. Does
        /// not resume retained branches.
        #[arg(long)]
        again: bool,
    },

    /// Post a review a dry run produced, without running the agents again.
    ///
    /// `spar review <pr> --dry-run` saves what it produced. Read it, edit the
    /// file if you like, then post exactly that.
    Post {
        /// Pull request numbers whose saved review should be posted.
        #[arg(required = true)]
        prs: Vec<i64>,
        #[arg(long, default_value = ".")]
        repo: PathBuf,
        #[arg(long)]
        config: Option<PathBuf>,
        /// Post this file instead of the saved review.
        #[arg(long, value_name = "PATH")]
        file: Option<PathBuf>,
        /// Print what would be posted and stop.
        #[arg(long)]
        dry_run: bool,
    },

    /// Detect installed agent CLIs and write a spar.toml.
    ///
    /// On an existing config, `--update` appends any settings it does not
    /// mention, which is how to pick up options added by a newer release.
    Init {
        #[arg(long, default_value = "spar.toml")]
        out: PathBuf,
        /// Overwrite an existing config.
        #[arg(long)]
        force: bool,
        /// Append settings the existing config does not mention, as comments.
        /// Nothing already in the file is changed.
        #[arg(long, conflicts_with = "force")]
        update: bool,
    },

    /// Remove worktrees, branches, and state whose PR is merged or closed.
    Clean {
        #[arg(long, default_value = ".")]
        repo: PathBuf,
        #[arg(long)]
        config: Option<PathBuf>,
        /// Remove every local worktree and local branch spar recorded, even for
        /// open PRs.
        #[arg(long)]
        all: bool,
        /// Also delete state comments left on finished PRs.
        #[arg(long)]
        pr_state: bool,
    },

    /// Check prerequisites and resolve each configured agent.
    Doctor {
        #[arg(long)]
        config: Option<PathBuf>,
    },

    /// Read a commit message on stdin and write the scrubbed version to stdout.
    ///
    /// Used by `git filter-branch`, not by people.
    #[command(hide = true)]
    ScrubFilter,
}

#[derive(Args, Debug, Clone)]
pub struct Common {
    /// Path to the git repository.
    #[arg(long, default_value = ".")]
    pub repo: PathBuf,
    /// Path to spar.toml.
    #[arg(long)]
    pub config: Option<PathBuf>,
    /// Base branch. Defaults to whatever origin/HEAD points at.
    #[arg(long)]
    pub base: Option<String>,
    /// Which agent implements first. A key from the `[agents]` table.
    #[arg(long)]
    pub first: Option<String>,
    /// Cap on open items of each kind when none are named. Bare `split` may take
    /// this many issues and this many pull requests.
    #[arg(long, default_value_t = 20)]
    pub limit: usize,
    /// Ignore issues and pull requests numbered below this when picking for
    /// itself. A number you name explicitly is always honoured.
    #[arg(long, value_name = "N")]
    pub min_number: Option<i64>,
    /// Extra instructions for both agents, for this run only. Added to any
    /// already in the config rather than replacing them.
    #[arg(long, value_name = "TEXT")]
    pub instructions: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct LoopFlags {
    /// Review rounds this run may spend asking for changes. A closing pass,
    /// when needed, is not one of them. Resuming grants a fresh budget.
    #[arg(long)]
    pub max_rounds: Option<u32>,
    /// Merge when no blocking findings remain. Off by default, deliberately.
    #[arg(long)]
    pub auto_merge: bool,
    /// Leave worktrees in place after a run, for inspection.
    #[arg(long)]
    pub keep_worktrees: bool,
    /// Waves of newly filed follow-ups to fold back into this run instead of
    /// leaving them for the next one. Each wave is triaged like any issue.
    #[arg(long, value_name = "N")]
    pub absorb: Option<u32>,
}

/// Only `run` triages, so only `run` can decline an issue. Offering these on
/// `resume` would accept a flag that does nothing.
#[derive(Args, Debug, Clone)]
pub struct TriageFlags {
    /// Close an issue both agents declined, after posting the reasoning.
    #[arg(long, conflicts_with = "no_close_skipped")]
    pub close_skipped: bool,
    /// Comment on a declined issue but leave it open.
    #[arg(long)]
    pub no_close_skipped: bool,
}

// ---------------------------------------------------------------------------
// Entry
// ---------------------------------------------------------------------------

pub fn main() -> i32 {
    let cli = Cli::parse();
    logging::init_color();
    logging::set_quiet(cli.quiet);

    match dispatch(cli) {
        Ok(code) => code,
        Err(e) => {
            logging::error(e.to_string());
            2
        }
    }
}

fn dispatch(cli: Cli) -> Result<i32> {
    match cli.command {
        Command::ScrubFilter => cmd_scrub_filter(),
        Command::Doctor { config } => cmd_doctor(config.as_deref()),
        Command::Review {
            items,
            common,
            dry_run,
            max_rounds,
        } => {
            let overrides = Overrides {
                max_rounds,
                ..Overrides::default()
            };
            let (cfg, repo, agents) = prepare(&common, Some(overrides))?;
            let numbers = if items.is_empty() {
                let found = repo.list_open_prs(common.limit, cfg.loop_cfg.min_number)?;
                if found.is_empty() {
                    log!("no open PRs");
                    return Ok(0);
                }
                log!("no PRs given, reviewing {} open", found.len());
                found
            } else {
                items
            };
            let sorted = classify(&repo, &numbers)?;
            let mut targets = sorted.prs;
            for number in sorted.issues {
                match repo.open_pr_for_issue(number) {
                    Some(pr) => {
                        log!("#{number} is an issue; reviewing its open PR {}", pr.url);
                        targets.push(pr.number);
                    }
                    None => logwarn!("#{number} is an issue with no open pull request to review"),
                }
            }
            let mut results = Vec::new();
            for number in targets {
                results.push(review_only::review_pr(
                    &agents, &cfg, &repo, number, dry_run,
                ));
            }
            if results.is_empty() {
                return Ok(0);
            }
            Ok(report(&results, &cfg, &repo))
        }

        Command::Checkin {
            items,
            common,
            dry_run,
            reply_only,
            any_author,
            again,
            keep_worktrees,
        } => {
            let overrides = Overrides {
                keep_worktrees: keep_worktrees.then_some(true),
                ..Overrides::default()
            };
            let (cfg, repo, agents) = prepare(&common, Some(overrides))?;
            let mode = checkin::Mode {
                dry_run,
                reply_only,
                trust: if any_author {
                    crate::config::Trust::Anyone
                } else {
                    cfg.loop_cfg.checkin_trust
                },
                again,
                resolve: cfg.loop_cfg.checkin_resolve,
                posts: checkin::posts(&cfg),
            };
            let numbers = if items.is_empty() {
                let found = repo.list_open_prs(common.limit, cfg.loop_cfg.min_number)?;
                if found.is_empty() {
                    log!("no open PRs");
                    return Ok(0);
                }
                log!(
                    "no PRs given, checking in on {} open: {}",
                    found.len(),
                    found
                        .iter()
                        .map(|n| format!("#{n}"))
                        .collect::<Vec<_>>()
                        .join(", ")
                );
                found
            } else {
                items
            };
            let sorted = classify(&repo, &numbers)?;
            let mut results = Vec::new();
            for number in sorted.prs {
                results.push(checkin::checkin_pr(&agents, &cfg, &repo, number, &mode));
            }
            // A change request left on the issue is exactly what this exists to
            // catch, and when the issue has work open there is a branch to act
            // on, so route to it rather than refusing.
            for number in sorted.issues {
                match repo.open_pr_for_issue(number) {
                    Some(pr) => {
                        log!(
                            "#{number} is an issue; checking in on its open PR {}",
                            pr.url
                        );
                        results.push(checkin::checkin_pr(&agents, &cfg, &repo, pr.number, &mode));
                    }
                    None => {
                        results.push(checkin::checkin_issue(&agents, &cfg, &repo, number, &mode))
                    }
                }
            }
            if results.is_empty() {
                return Ok(0);
            }
            Ok(report(&results, &cfg, &repo))
        }

        Command::Split {
            items,
            common,
            dry_run,
            again,
        } => {
            let (cfg, repo, agents) = prepare(&common, None)?;
            let mode = split::Mode { dry_run, again };
            let picked = if items.is_empty() {
                pick_for_split(&agents, &cfg, &repo, common.limit, &mode)?
            } else {
                let sorted = classify(&repo, &items)?;
                let mut out: Vec<(i64, ItemKind)> = sorted
                    .issues
                    .iter()
                    .map(|n| (*n, ItemKind::Issue))
                    .collect();
                out.extend(sorted.prs.iter().map(|n| (*n, ItemKind::Pr)));
                out
            };

            let mut results = Vec::new();
            let mut seen: BTreeSet<i64> = BTreeSet::new();
            for (number, kind) in picked {
                // An issue whose work is half done produces children describing
                // work that already exists on a branch, so it routes to the
                // branch instead, the way `checkin` and `review` already do.
                let target = match kind {
                    ItemKind::Pr => Some(number),
                    ItemKind::Issue => repo.open_pr_for_issue(number).map(|pr| {
                        log!("#{number} is an issue; splitting its open PR {}", pr.url);
                        pr.number
                    }),
                };
                // An issue and the pull request it routes to can both be named,
                // and the queue can hold both. Splitting one pull request twice
                // makes two sets of branches and pull requests out of it, which
                // `--again` would not stop.
                if !seen.insert(target.unwrap_or(number)) {
                    logdim!("#{number} was already covered by this run, skipping it");
                    continue;
                }
                results.push(match target {
                    Some(pr) => split::split_pr(&agents, &cfg, &repo, pr, &mode),
                    None => split::split_issue(&agents, &cfg, &repo, number, &mode),
                });
            }
            if results.is_empty() {
                return Ok(0);
            }
            Ok(report(&results, &cfg, &repo))
        }

        Command::Post {
            prs,
            repo: repo_path,
            config,
            file,
            dry_run,
        } => cmd_post(
            &prs,
            &repo_path,
            config.as_deref(),
            file.as_deref(),
            dry_run,
        ),

        Command::Init { out, force, update } => {
            if update {
                cmd_init_update(&out)
            } else {
                cmd_init(&out, force)
            }
        }
        Command::Clean {
            repo,
            config,
            all,
            pr_state,
        } => cmd_clean(&repo, config.as_deref(), all, pr_state),
        Command::Triage {
            issues,
            common,
            plan_out,
        } => {
            let (cfg, repo, agents) = prepare(&common, None)?;
            let numbers = pick_issues(&repo, issues, common.limit, cfg.loop_cfg.min_number)?;
            if numbers.is_empty() {
                return Ok(0);
            }
            let sorted = classify(&repo, &numbers)?;
            for number in &sorted.prs {
                log!("#{number} is a pull request, nothing to triage");
            }
            if sorted.issues.is_empty() {
                log!("no issues to triage");
                return Ok(0);
            }
            let issues = repo.fetch_issues(&sorted.issues)?;
            // Deliberately no act_on_plan here. `triage` is the command you
            // reach for to look before leaping, and a preview that comments on
            // and closes issues is a trap.
            let plan = make_plan(&agents, &cfg, &repo, &issues, &plan_out)?;
            // A preview that files issues and rewrites somebody's issue body is
            // that same trap, so this prints the decomposition and writes none
            // of it. It is where the first few real trackers should be checked.
            if cfg.loop_cfg.decompose_trackers {
                for item in plan.skipped.iter().filter(|i| i.tracker) {
                    tracker::preview(&cfg, &repo, item.issue);
                }
            }
            Ok(0)
        }
        Command::Run {
            issues,
            common,
            loop_flags,
            triage_flags,
            plan_out,
            no_worktrees,
        } => {
            let overrides = Overrides::for_working(&loop_flags, &triage_flags, no_worktrees);
            let (cfg, repo, agents) = prepare(&common, Some(overrides))?;
            let numbers = pick_issues(&repo, issues, common.limit, cfg.loop_cfg.min_number)?;
            if numbers.is_empty() {
                return Ok(0);
            }
            let sorted = classify(&repo, &numbers)?;
            let mut results = Vec::new();
            work_issues(
                &agents,
                &cfg,
                &repo,
                sorted.issues.clone(),
                &plan_out,
                &mut results,
            )?;

            for number in sorted.prs {
                results.push(review::resume_pr(&agents, &cfg, &repo, number, None));
            }

            if results.is_empty() {
                log!("nothing scheduled");
                return Ok(report_writes(&repo));
            }
            Ok(report(&results, &cfg, &repo))
        }
        Command::Followup {
            common,
            loop_flags,
            triage_flags,
            file,
            screen_only,
            file_only,
            plan_out,
            no_worktrees,
        } => {
            let overrides = Overrides::for_working(&loop_flags, &triage_flags, no_worktrees);
            let (cfg, repo, agents) = prepare(&common, Some(overrides))?;
            let path = file.unwrap_or_else(|| repo.followups_path());
            let mode = match (screen_only, file_only) {
                (true, _) => followups::Mode::ScreenOnly,
                (_, true) => followups::Mode::FileOnly,
                _ => followups::Mode::Work,
            };
            let outcome = followups::run(&agents, &cfg, &repo, &path, common.limit, mode)?;

            let wave = followups::wave(&outcome);
            if mode != followups::Mode::Work || wave.is_empty() {
                return Ok(report_writes(&repo));
            }
            let mut results = Vec::new();
            work_issues(&agents, &cfg, &repo, wave, &plan_out, &mut results)?;
            if results.is_empty() {
                log!("nothing scheduled");
                return Ok(report_writes(&repo));
            }
            Ok(report(&results, &cfg, &repo))
        }

        Command::Resume {
            prs,
            common,
            loop_flags,
            next_actor,
        } => {
            let (cfg, repo, agents) = prepare(&common, Some(Overrides::from(&loop_flags)))?;
            if let Some(name) = &next_actor {
                if !cfg.has_agent(name) {
                    bail!("--next must be one of: {}", cfg.agent_names().join(", "));
                }
            }
            let numbers = if prs.is_empty() {
                let found = repo.list_open_prs(common.limit, cfg.loop_cfg.min_number)?;
                if found.is_empty() {
                    log!("no open PRs");
                    return Ok(0);
                }
                log!(
                    "no PRs given, taking {} open: {}",
                    found.len(),
                    found
                        .iter()
                        .map(|n| format!("#{n}"))
                        .collect::<Vec<_>>()
                        .join(", ")
                );
                found
            } else {
                prs
            };
            let sorted = classify(&repo, &numbers)?;
            let mut results = Vec::new();
            for number in sorted.prs {
                results.push(review::resume_pr(
                    &agents,
                    &cfg,
                    &repo,
                    number,
                    next_actor.as_deref(),
                ));
            }
            // An issue number handed to `resume` is not a mistake worth
            // refusing over. If work is already open for it, continue that.
            for number in sorted.issues {
                match repo.open_pr_for_issue(number) {
                    Some(pr) => {
                        log!("#{number} is an issue; continuing its open PR {}", pr.url);
                        results.push(review::resume_pr(
                            &agents,
                            &cfg,
                            &repo,
                            pr.number,
                            next_actor.as_deref(),
                        ));
                    }
                    None => logwarn!(
                        "#{number} is an issue with no open pull request. Use `spar run {number}` \
                         to implement it."
                    ),
                }
            }
            if results.is_empty() {
                return Ok(0);
            }
            Ok(report(&results, &cfg, &repo))
        }
    }
}

// ---------------------------------------------------------------------------
// Shared setup
// ---------------------------------------------------------------------------

#[derive(Debug, Default, Clone)]
struct Overrides {
    max_rounds: Option<u32>,
    auto_merge: Option<bool>,
    keep_worktrees: Option<bool>,
    worktrees: Option<bool>,
    close_skipped: Option<bool>,
    absorb: Option<u32>,
}

impl From<&LoopFlags> for Overrides {
    fn from(flags: &LoopFlags) -> Self {
        Self {
            max_rounds: flags.max_rounds,
            auto_merge: flags.auto_merge.then_some(true),
            keep_worktrees: flags.keep_worktrees.then_some(true),
            worktrees: None,
            close_skipped: None,
            absorb: flags.absorb,
        }
    }
}

impl Overrides {
    /// What `run` and `followup` share past the loop flags: both triage, so
    /// both can decline, and both work issues in a worktree apiece.
    fn for_working(loop_flags: &LoopFlags, triage: &TriageFlags, no_worktrees: bool) -> Self {
        let mut over = Overrides::from(loop_flags);
        over.worktrees = if no_worktrees { Some(false) } else { None };
        over.close_skipped = match (triage.close_skipped, triage.no_close_skipped) {
            (true, _) => Some(true),
            (_, true) => Some(false),
            _ => None,
        };
        over
    }
}

/// Triage a set of issues, work them in dependency order, and fold each wave of
/// newly filed follow-ups back in as the absorb budget allows.
///
/// Shared by `run` and `followup`, which differ only in where the first wave
/// comes from: `run` takes it from the tracker, `followup` from the issues it
/// just filed out of the local queue. Everything after that is the same
/// pipeline, and it has to stay the same. An issue spar filed for itself gets
/// no easier a ride through triage than one a person opened, which is the whole
/// reason the screening pass is one agent and this is two.
fn work_issues(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    first_wave: Vec<i64>,
    plan_out: &Path,
    results: &mut Vec<IssueRun>,
) -> Result<()> {
    let mut handled: BTreeSet<i64> = BTreeSet::new();
    let mut leftover: BTreeSet<i64> = BTreeSet::new();
    let mut queue: VecDeque<Wave> = VecDeque::from([Wave::first(first_wave)]);
    let mut plans_written = 0usize;

    // The first wave is what was asked for. A wave of follow-ups the previous
    // one filed is folded back in as the absorb budget allows, and a wave of
    // children extracted from a tracker's checklist joins the same run because
    // it is named work rather than picked work. Every wave is triaged like
    // anything else, so both agents still have to agree each one is worth
    // doing.
    while let Some(mut wave) = queue.pop_front() {
        wave.numbers.retain(|n| !handled.contains(n));
        if wave.numbers.is_empty() {
            continue;
        }
        match wave.parent {
            Some(tracker) => log!(
                "working {} item(s) from the checklist in #{tracker}: {}",
                wave.numbers.len(),
                numbers(&wave.numbers)
            ),
            None if wave.absorbed > 0 => log!(
                "absorbing {} newly filed issue(s): {}",
                wave.numbers.len(),
                numbers(&wave.numbers)
            ),
            None => {}
        }
        handled.extend(wave.numbers.iter().copied());

        let fetched = match repo.fetch_issues(&wave.numbers) {
            Ok(fetched) => fetched,
            Err(e) => {
                logdim!("could not read the next wave: {e}");
                continue;
            }
        };
        // Named for the order the plans were written in, so the first is
        // plan.json exactly as before and no two waves overwrite each other.
        let plan_path = if plans_written == 0 {
            plan_out.to_path_buf()
        } else {
            plan_out.with_extension(format!("wave{plans_written}.json"))
        };
        plans_written += 1;
        let plan = make_plan(agents, cfg, repo, &fetched, &plan_path)?;
        act_on_plan(cfg, repo, &plan);

        // No recursion: a child that triage calls a tracker in its turn is
        // commented on and held like any other. `file_non_blocking` records
        // what happened the last time this codebase let something multiply.
        if cfg.loop_cfg.decompose_trackers && wave.parent.is_none() {
            for item in plan.skipped.iter().filter(|i| i.tracker) {
                let children = tracker::decompose(cfg, repo, item.issue);
                if !children.is_empty() {
                    queue.push_back(wave.child(item.issue, children));
                }
            }
        }

        let before = results.len();
        for item in &plan.order {
            let Some(issue) = fetched.iter().find(|i| i.number == item.issue) else {
                continue;
            };
            results.push(review::run_issue(agents, cfg, repo, item, issue));
        }

        // Whatever this wave filed becomes the next one, budget allowing.
        let filed: Vec<i64> = results[before..]
            .iter()
            .flat_map(|r| r.filed.iter())
            .filter_map(|url| review::filed_issue_number(url))
            .filter(|n| !handled.contains(n))
            .collect::<BTreeSet<_>>()
            .into_iter()
            .collect();
        if filed.is_empty() {
            continue;
        }
        match wave.absorbed < cfg.loop_cfg.absorb_new_issues {
            true => queue.push_back(wave.absorb(filed)),
            false => leftover.extend(filed),
        }
    }
    if !leftover.is_empty() && cfg.loop_cfg.absorb_new_issues > 0 {
        log!(
            "{} issue(s) filed in the last wave were left for a later run: {}",
            leftover.len(),
            numbers(&leftover.iter().copied().collect::<Vec<_>>())
        );
    }
    Ok(())
}

/// One pass of the pipeline, and where it came from.
struct Wave {
    numbers: Vec<i64>,
    /// Absorb rounds spent to reach it. A tracker's children inherit their
    /// parent's, because extracting named work is not absorbing a follow-up:
    /// `absorb_new_issues` is off by default, and spending it here would make
    /// `decompose_trackers` silently do nothing.
    absorbed: u32,
    /// The tracker whose checklist this wave came out of.
    parent: Option<i64>,
}

impl Wave {
    fn first(numbers: Vec<i64>) -> Self {
        Self {
            numbers,
            absorbed: 0,
            parent: None,
        }
    }

    fn child(&self, tracker: i64, numbers: Vec<i64>) -> Self {
        Self {
            numbers,
            absorbed: self.absorbed,
            parent: Some(tracker),
        }
    }

    fn absorb(&self, numbers: Vec<i64>) -> Self {
        Self {
            numbers,
            absorbed: self.absorbed + 1,
            parent: None,
        }
    }
}

fn numbers(items: &[i64]) -> String {
    items
        .iter()
        .map(|n| format!("#{n}"))
        .collect::<Vec<_>>()
        .join(", ")
}

fn prepare(common: &Common, overrides: Option<Overrides>) -> Result<(Config, Repo, Vec<Agent>)> {
    let mut cfg = config::load(common.config.as_deref())?;

    if let Some(first) = &common.first {
        if !cfg.has_agent(first) {
            bail!("--first must be one of: {}", cfg.agent_names().join(", "));
        }
        cfg.first_implementor = first.clone();
    }
    if let Some(base) = &common.base {
        cfg.loop_cfg.base_branch = base.clone();
    }
    if let Some(min) = common.min_number {
        cfg.loop_cfg.min_number = min;
    }
    // Added to the config's, not in place of them. One is what this repository
    // always wants and the other is what today wants, and a flag that silenced
    // the standing set would be a trap: you would notice it the run after.
    if let Some(extra) = common.instructions.as_deref().map(str::trim) {
        if !extra.is_empty() {
            let standing = cfg.loop_cfg.instructions.trim();
            cfg.loop_cfg.instructions = if standing.is_empty() {
                extra.to_string()
            } else {
                format!("{standing}\n{extra}")
            };
        }
    }
    if let Some(over) = overrides {
        if let Some(v) = over.max_rounds {
            if v == 0 {
                bail!("--max-rounds must be at least 1");
            }
            cfg.loop_cfg.max_rounds = v;
        }
        if let Some(v) = over.auto_merge {
            cfg.loop_cfg.auto_merge = v;
        }
        if let Some(v) = over.keep_worktrees {
            cfg.loop_cfg.keep_worktrees = v;
        }
        if let Some(v) = over.worktrees {
            cfg.loop_cfg.worktrees = v;
        }
        if let Some(v) = over.close_skipped {
            cfg.loop_cfg.close_skipped = v;
        }
        if let Some(v) = over.absorb {
            cfg.loop_cfg.absorb_new_issues = v;
        }
    }

    let repo = Repo::open(&common.repo, &cfg)?;
    if common.base.is_none() {
        cfg.loop_cfg.base_branch = repo.default_branch(cfg.base_branch());
    }

    let agents = agent::build(&cfg)?;
    if let Some(warning) = agent::correlation_warning(&agents) {
        logging::warn(warning);
    }

    // Sweep finished worktrees before starting, so they cannot accumulate.
    for stale in repo.prune_worktrees(false) {
        let what = if stale.starts_with("branch ") {
            stale
        } else {
            format!("worktree {stale}")
        };
        logdim!("cleaned up finished {what}");
    }

    log!("repo {} base {}", repo.root().display(), cfg.base_branch());
    log!(
        "agents: {}",
        agents
            .iter()
            .map(|a| format!("{}={}", a.name(), a.spec.describe()))
            .collect::<Vec<_>>()
            .join(", ")
    );
    Ok((cfg, repo, agents))
}

fn pick_issues(repo: &Repo, given: Vec<i64>, limit: usize, min_number: i64) -> Result<Vec<i64>> {
    if !given.is_empty() {
        // Naming a number is the point, so a floor never overrides it.
        if min_number > 0 {
            let below: Vec<String> = given
                .iter()
                .filter(|n| **n < min_number)
                .map(|n| format!("#{n}"))
                .collect();
            if !below.is_empty() {
                logdim!(
                    "{} below the #{min_number} floor, taking them because you named them",
                    below.join(", ")
                );
            }
        }
        return Ok(given);
    }
    let found = repo.list_open_issues(limit, min_number)?;
    if found.is_empty() {
        log!("no open issues");
        return Ok(found);
    }
    log!(
        "no issues given, taking {} open: {}",
        found.len(),
        found
            .iter()
            .map(|n| format!("#{n}"))
            .collect::<Vec<_>>()
            .join(", ")
    );
    Ok(found)
}

/// The bare `spar split`: every open issue and every open pull request, then
/// one screening call over both.
///
/// The first command that means both kinds when given nothing, so it says so
/// out loud. `--limit` applies to each list rather than to the two together: a
/// single budget shared across both would fill up on issues and starve the pull
/// requests. `min_number` applies because this is picked work rather than named
/// work.
///
/// One agent rules on the whole list in one call rather than two calls per
/// item, because two agent calls per item across a whole queue is the wrong
/// price for a question whose answer is usually no.
fn pick_for_split(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    limit: usize,
    mode: &split::Mode,
) -> Result<Vec<(i64, ItemKind)>> {
    let issues = repo.list_open_issues(limit, cfg.loop_cfg.min_number)?;
    let prs = repo.list_open_prs(limit, cfg.loop_cfg.min_number)?;
    log!(
        "no numbers given, considering {} open issue(s) and {} open pull request(s)",
        issues.len(),
        prs.len()
    );
    if issues.is_empty() && prs.is_empty() {
        return Ok(Vec::new());
    }

    let issue_rows = repo.open_issue_rows();
    let pr_rows = repo.open_pr_rows();
    let mut candidates: Vec<split::Candidate> = Vec::new();
    let mut already = 0usize;

    for number in &issues {
        let Some(row) = issue_rows.iter().find(|i| i.number == *number) else {
            logdim!("could not read #{number}, leaving it alone");
            continue;
        };
        // Free here, because the body is already in hand. A pull request that
        // has been split is caught by `split_pr`, which reads its comments.
        if split::already_split(row.body_text()) && !mode.again {
            already += 1;
            continue;
        }
        candidates.push(split::Candidate::from_issue(row));
    }
    for number in &prs {
        match pr_rows.iter().find(|p| p.number == *number) {
            Some(row) => candidates.push(split::Candidate::from_pr(row)),
            None => logdim!("could not read PR #{number}, leaving it alone"),
        }
    }
    if already > 0 {
        log!("{already} issue(s) already split, skipped. --again reopens them.");
    }
    if candidates.is_empty() {
        return Ok(Vec::new());
    }

    let agent = agent::find(agents, &cfg.first_implementor)?;
    log!(
        "screening {} item(s) with {}",
        candidates.len(),
        agent.name()
    );
    let verdicts = split::screen(agent, cfg, repo, &candidates)?;

    let mut picked = Vec::new();
    for candidate in &candidates {
        match verdicts.iter().find(|v| v.item == candidate.number) {
            Some(v) if v.split => {
                log!("  split #{}: {}", candidate.number, v.reason.trim());
                picked.push((candidate.number, candidate.kind));
            }
            // Not a warning: no is the expected answer, and one line per item
            // saying so is the whole screen printed twice.
            Some(v) => logdim!("  leave #{} whole: {}", candidate.number, v.reason.trim()),
            None => logdim!("  no verdict for #{}, leaving it whole", candidate.number),
        }
    }
    if picked.is_empty() {
        log!("nothing worth splitting");
    }
    Ok(picked)
}

/// Numbers split by what they actually name.
///
/// Issues and pull requests share one number sequence per repository, so a
/// person should not have to remember which command takes which. Both `run` and
/// `resume` sort the numbers themselves and route each one.
#[derive(Debug, Default)]
struct Sorted {
    issues: Vec<i64>,
    prs: Vec<i64>,
}

fn classify(repo: &Repo, numbers: &[i64]) -> Result<Sorted> {
    let mut sorted = Sorted::default();
    for number in numbers {
        match repo.item_kind(*number)? {
            ItemKind::Issue => sorted.issues.push(*number),
            ItemKind::Pr => sorted.prs.push(*number),
        }
    }
    if !sorted.issues.is_empty() && !sorted.prs.is_empty() {
        log!(
            "{} issue(s) and {} pull request(s) given",
            sorted.issues.len(),
            sorted.prs.len()
        );
    }
    Ok(sorted)
}

fn make_plan(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    issues: &[Issue],
    plan_out: &Path,
) -> Result<Plan> {
    let plan = triage::triage(agents, cfg, repo, issues)?;

    std::fs::write(plan_out, serde_json::to_vec_pretty(&plan)?)
        .map_err(|e| spar_err!("could not write {}: {e}", plan_out.display()))?;
    log!("plan written to {}", plan_out.display());

    for item in &plan.order {
        log!(
            "  do   #{} [{}/{}] {}",
            item.issue,
            item.complexity,
            item.risk,
            item.title
        );
    }
    for item in &plan.skipped {
        if item.tracker {
            log!(
                "  hold #{} (both reviewers: tracks work filed elsewhere)",
                item.issue
            );
        } else {
            log!("  skip #{} (both reviewers: not worth doing)", item.issue);
        }
    }
    for item in &plan.contested {
        log!("  ??   #{} contested, parked for you to decide", item.issue);
    }
    Ok(plan)
}

/// Post the shared reasoning on every issue both agents declined, and close it
/// when the config says so. Contested issues are never touched.
///
/// A tracker is never closed, whatever `close_skipped` says. Declining to open
/// a pull request for an umbrella is right, and closing it does not follow from
/// that: its parts are still open, and the shared context and the alternatives
/// somebody recorded against are the reason the issue exists. spar closed a
/// real one as "not planned" while all three of its subtasks were open, which
/// is what this exists to stop.
fn act_on_plan(cfg: &Config, repo: &Repo, plan: &Plan) {
    for item in &plan.skipped {
        let body = review::skip_comment(item, &repo.style);
        let close = cfg.loop_cfg.close_skipped && !item.tracker;
        let outcome = if close {
            repo.close_issue(item.issue, &body)
        } else {
            repo.comment_issue(item.issue, &body)
        };
        match outcome {
            Ok(()) if close => log!("  closed #{}", item.issue),
            Ok(()) if item.tracker => {
                log!(
                    "  left #{} open, it tracks work filed elsewhere",
                    item.issue
                )
            }
            Ok(()) => {}
            Err(e) => logdim!("could not update #{}: {e}", item.issue),
        }
    }
}

// ---------------------------------------------------------------------------
// Subcommands
// ---------------------------------------------------------------------------

fn cmd_scrub_filter() -> Result<i32> {
    let mut input = String::new();
    std::io::stdin()
        .read_to_string(&mut input)
        .map_err(|e| spar_err!("could not read a commit message from stdin: {e}"))?;
    let out = style::scrub(&input, &crate::repo::style_from_env());
    let mut stdout = std::io::stdout();
    stdout
        .write_all(out.as_bytes())
        .and_then(|_| stdout.write_all(b"\n"))
        .map_err(|e| spar_err!("could not write the scrubbed message: {e}"))?;
    Ok(0)
}

fn cmd_clean(
    repo_path: &Path,
    config_path: Option<&Path>,
    all: bool,
    pr_state: bool,
) -> Result<i32> {
    let cfg = config::load(config_path)?;
    let repo = Repo::open(repo_path, &cfg)?;
    let mut removed = repo.prune_worktrees(all);
    removed.extend(repo.prune_state());
    if pr_state {
        removed.extend(repo.prune_pr_state(None));
    }
    if removed.is_empty() {
        if repo.write_summary().failed > 0 {
            println!("nothing removed");
        } else {
            println!("nothing to clean");
        }
    } else {
        for item in removed {
            println!("removed {item}");
        }
    }
    Ok(report_writes(&repo))
}

/// Post a review that was produced earlier and not sent.
fn cmd_post(
    prs: &[i64],
    repo_path: &Path,
    config_path: Option<&Path>,
    file: Option<&Path>,
    dry_run: bool,
) -> Result<i32> {
    let cfg = config::load(config_path)?;
    let repo = Repo::open(repo_path, &cfg)?;

    if file.is_some() && prs.len() > 1 {
        bail!("--file posts one review, so give it one pull request number");
    }

    let mut failed = false;
    for number in prs {
        let text = match file {
            Some(path) => std::fs::read_to_string(path)
                .map_err(|e| spar_err!("could not read {}: {e}", path.display()))?,
            None => match repo.read_pending_comment(*number) {
                Some(text) => text,
                None => {
                    logging::error(format!(
                        "no saved review for PR #{number}. `spar review {number} --dry-run` \
                         produces one, or pass --file."
                    ));
                    failed = true;
                    continue;
                }
            },
        };
        if text.trim().is_empty() {
            logging::error(format!("the saved review for PR #{number} is empty"));
            failed = true;
            continue;
        }
        if dry_run {
            println!("\n{}\n", text.trim());
            log!("would post the above to PR #{number}");
            continue;
        }
        // Through the style gate like anything else spar sends, so an edit that
        // reintroduces a banned dash is caught rather than published.
        match repo.comment_pr(*number, &text) {
            Ok(()) => log!("posted to PR #{number}"),
            Err(e) => {
                logging::error(format!("could not post to PR #{number}: {e}"));
                failed = true;
            }
        }
    }
    Ok(i32::from(failed).max(report_writes(&repo)))
}

/// Append the settings a config does not mention, commented out.
///
/// Append only by design. Rewriting somebody's config to insert options would
/// take their comments and their ordering with it, and `--force` already exists
/// for anyone who wants the generated file back.
fn cmd_init_update(out: &Path) -> Result<i32> {
    let text = std::fs::read_to_string(out)
        .map_err(|e| spar_err!("could not read {}: {e}", out.display()))?;
    // Refuse to append to something that does not parse, rather than making a
    // broken config longer.
    config::parse(&text).map_err(|e| spar_err!("{} does not parse: {e}", out.display()))?;

    let unset = config::unmentioned_options(&text);
    if unset.is_empty() {
        println!("{} already mentions every setting.", out.display());
        return Ok(0);
    }

    let mut block = String::new();
    if !text.ends_with('\n') {
        block.push('\n');
    }
    block.push_str("\n# Added by `spar init --update`: settings this file did not mention,\n");
    block.push_str("# shown at their defaults. Uncomment one to change it.\n");
    let mut section = "";
    for option in &unset {
        if option.section != section {
            section = option.section;
            block.push_str(&format!("\n# [{section}]\n"));
        }
        // With the same note `spar init` writes. A config that gained a setting
        // this way used to gain a bare line and nothing saying what it was for,
        // which is the half of the setting that matters when you are reading it
        // for the first time.
        block.push('\n');
        block.push_str(&wrap_comment(note_for(&option.key)));
        block.push_str(&format!("# {} = {}\n", option.key, option.default));
    }

    use std::io::Write;
    std::fs::OpenOptions::new()
        .append(true)
        .open(out)
        .and_then(|mut f| f.write_all(block.as_bytes()))
        .map_err(|e| spar_err!("could not append to {}: {e}", out.display()))?;

    println!(
        "added {} setting(s) to {} as comments",
        unset.len(),
        out.display()
    );
    Ok(0)
}

fn cmd_init(out: &Path, force: bool) -> Result<i32> {
    if out.exists() && !force {
        logging::error(format!(
            "{} already exists. `--update` appends any settings it does not mention, \
             `--force` overwrites it.",
            out.display()
        ));
        return Ok(1);
    }

    let presets = config::available_presets();
    if presets.is_empty() {
        bail!("no presets available, which should be impossible in a released build");
    }

    let mut found: Vec<(String, PathBuf, config::AgentSpec)> = Vec::new();
    for name in &presets {
        let raw = config::load_preset(name)?;
        // A preset that will not build is a broken preset, not an uninstalled
        // CLI. Skipping it silently reported it as "missing" and sent people
        // looking for an install problem that was not there.
        let mut spec: config::AgentSpec = match raw
            .as_table()
            .cloned()
            .ok_or_else(|| spar_err!("not a table"))
            .and_then(|t| {
                toml::Value::Table(t)
                    .try_into()
                    .map_err(|e| spar_err!("{e}"))
            }) {
            Ok(spec) => spec,
            Err(e) => {
                println!("  BROKEN   {name:10} {}", e.first_line());
                continue;
            }
        };
        spec.name = name.clone();
        match Agent::new(spec.clone()).resolve_bin() {
            Ok(path) => {
                println!("  found    {name:10} {}", path.display());
                found.push((name.clone(), path.to_path_buf(), spec));
            }
            Err(_) => println!("  missing  {name}"),
        }
    }

    if found.len() < 2 {
        logging::error(format!(
            "need two agent CLIs, found {}. Install another, or write {} by hand using the \
             presets as a reference.",
            found.len(),
            out.display()
        ));
        return Ok(1);
    }

    // Prefer a pair that cannot share blind spots, if one is available.
    let chosen: Vec<&(String, PathBuf, config::AgentSpec)> = found.iter().take(2).collect();
    if found.len() > 2 {
        log!(
            "{} agents available, picking {} and {}. Edit {} to change.",
            found.len(),
            chosen[0].0,
            chosen[1].0,
            out.display()
        );
    }

    let mut text = String::from(
        "# Generated by `spar init`. Each agent inherits a command template from a\n\
         # built in preset; anything set here overrides it.\n\
         #\n\
         # Commented lines are the other options, each with a working value.\n\
         # Uncomment one to change it.\n\n",
    );
    for (name, _, spec) in &chosen {
        text.push_str(&agent_block(name, spec));
    }
    text.push_str(&settings_block(&chosen[0].0));

    std::fs::write(out, text).map_err(|e| spar_err!("could not write {}: {e}", out.display()))?;
    println!("\nwrote {}", out.display());
    println!("Next: `spar doctor` to check it, then `spar run` in a repo you have push access to.");
    Ok(0)
}

/// An agent's stand in, under the agent it stands in for.
///
/// Never counted against `doctor`'s exit code, deliberately. A fallback that is
/// not installed does not stop a run either, and a check that disagrees with
/// the runtime teaches people to ignore it.
fn report_fallback(agent: &Agent) {
    let Some(backup) = agent.fallback() else {
        return;
    };
    match backup.resolve_bin() {
        Ok(bin) => println!(
            "        fallback    {}  ({})",
            bin.display(),
            backup.spec.describe()
        ),
        Err(_) => println!(
            "        fallback    {} not found, so it will not stand in. Set {} to its path.",
            backup.program(),
            backup.env_key()
        ),
    }
}

/// One settable option: whether the generated config leaves it commented out,
/// its key, and the note beside it.
///
/// The value is deliberately absent. Every value comes from the defaults
/// themselves, because a value typed in here is a second copy of a number that
/// lives somewhere else, and the second copy is the one that goes stale. This
/// one did: the generated config offered a title budget of 90, a summary of
/// 200, a detail of 320, a body of 900 and an issue body of 4000, long after
/// those became 140, 2000, 6000, 8000 and 20000. Uncommenting a line to see
/// what it did cut every comment spar posts to a fifth of its length.
type Setting = (bool, &'static str, &'static str);

const LOOP_OPTIONS: &[Setting] = &[
    (false, "max_rounds", "In the custody loop, review rounds one invocation may spend asking for changes. A full-budget run may then use one extra closing pass. In `spar review`, this selects up to three phases: independent review, cross-adjudication, and rebuttal. Resuming a custody run grants a fresh budget, so this is not a lifetime cap."),
    (false, "auto_merge", "Merge when no blocking findings remain. Off on purpose: two models agreeing is not the same as being right, and neither carries the consequences."),
    (false, "first_implementor", "Which agent takes the first pass. The other one reviews it."),
    (false, "worktrees", "Isolate each issue in its own git worktree. Set false to work in the main checkout."),
    (false, "close_skipped", "Close an issue both reviewers declined, after posting the shared reasoning. A tracking issue is left open whatever this says."),
    (false, "followups", "Where a follow-up goes. issues files them, local writes .spar/followups.md and leaves the tracker alone, none drops them. `spar followup` works that file."),
    (true, "file_non_blocking", "File a non-blocking finding as a follow-up. Off, because not gating a merge is not the same as deserving somebody's triage queue."),
    (true, "max_followups", "Most follow-ups one run may record before it stops and says what it dropped. A backstop, not a target. `spar followup` is bounded by --limit instead."),
    (true, "max_split_parts", "Most parts `spar split` will make out of one issue or pull request. A backstop against a queue nobody asked for, not a target, and what it holds back is said out loud. A part is never itself split, so if one is still too big, `spar split <part>` is one command away."),
    (true, "keep_worktrees", "Keep worktrees after a run, for inspection."),
    (true, "min_number", "Ignore issues and pull requests numbered below this when spar picks for itself. 0 is no floor, and a number you name explicitly is always honoured."),
    (true, "parallel_triage", "Ask both agents to triage at once. They only read during triage, so there is nothing to serialise."),
    (true, "absorb_new_issues", "Waves of newly filed follow-ups to fold back into this run rather than leaving them for the next one. Multiplies what a run costs."),
    (true, "decompose_trackers", "Turn the checklist in a tracking issue into issues, link each item to the one covering it, tick an item off when its issue closes, and work the children in this run. Only ever acts on markdown task list items, so it is opt in per issue as well. `spar triage` prints what it would do without writing any of it."),
    (true, "max_tracker_children", "Most unchecked items from one tracker's checklist that one run will act on. A cap, not a target, and what it left is named out loud."),
    (true, "file_nits", "File nits as follow-ups too. Off, because a filed nit is somebody else's notification."),
    (true, "base_branch", "Only a fallback. Whatever origin/HEAD points at wins when it resolves."),
    (true, "branch_prefix", "Namespace the branches spar creates, for example \"spar/\". Without it they are issue-N, pr-N, and split-N-I with a suffix on repeated split names."),
    (true, "state_store", "Where resume state is kept. local uses .spar/state and keeps it off the pull request."),
    (true, "drafts", "Whether a pull request starts as a draft. until_approved opens one and marks it ready when the review converges, which is what the draft was saying while two agents were still arguing about it. always opens one and leaves it, and cannot be combined with auto_merge."),
    (true, "instructions", "Extra instructions handed to both agents with every request, for what this repository always wants that spar has no setting for. --instructions adds to this for one run."),
    (true, "max_issue_chars", "Most of one issue body that reaches a prompt. Sized so nothing a person wrote is cut, and a cut is said out loud when it happens."),
    (true, "max_triage_chars", "Most every issue body together may add to one triage prompt, or every recorded follow-up in one screening prompt. Past it, whole items wait for the next run rather than all of them losing their tails."),
    (true, "checkin_trust", "Whose comments `spar checkin` will act on. write is anybody GitHub says can write to this repository, which is the default because acting on a comment means pushing a commit to somebody's branch. anyone answers everyone, and still only changes code when both agents agree."),
    (true, "checkin_resolve", "Mark a review thread resolved when spar made the change it asked for. A thread spar disagreed with is left open whatever this says."),
    (true, "max_checkin_comments", "Most unanswered comments spar will answer on one pull request in a run. A backstop against a long argument being read back to somebody, not a target."),
];

const STYLE_OPTIONS: &[Setting] = &[
    (false, "ban_em_dash", "Strip em-dashes and en-dashes from everything spar posts, then refuse to post text that still has one."),
    (false, "ban_ai_attribution", "Strip mentions of the tooling, and Co-Authored-By trailers, from everything spar posts."),
    (false, "terse", "Hold model prose to a length budget. false removes the valves entirely."),
    (true, "pr_comments", "How much of its own working spar narrates into a pull request thread. outcome is one comment at the end, rounds is an audit trail, none never comments at all."),
    (true, "max_title_chars", "A finding, issue, or pull request title. Never ellipsised: a title ending in three dots reads as broken."),
    (true, "max_summary_chars", "A one line verdict, or a refutation's argument."),
    (true, "max_detail_chars", "A blocking finding's explanation, as it appears in the pull request thread."),
    (true, "max_body_chars", "A pull request body."),
    (true, "max_issue_body_chars", "A filed issue's body. Far larger on purpose: an issue is picked up cold months later. Fenced code blocks are never truncated and never count against it."),
];

/// The `[loop]` and `[style]` blocks of a generated config.
///
/// Safety valves, not editors: the length budgets here are sized so real
/// content is never touched, which is why they read as large numbers.
fn settings_block(first_implementor: &str) -> String {
    let defaults: std::collections::BTreeMap<String, String> = config::known_options()
        .into_iter()
        .map(|option| (option.key, option.default))
        .collect();
    // first_implementor has no default: it is whichever agent was written
    // first, and until there is a config there is no answer to give.
    let value = |key: &str| match key {
        "first_implementor" => format!("\"{first_implementor}\""),
        other => defaults.get(other).cloned().unwrap_or_default(),
    };

    let mut out = String::from("[loop]\n");
    out.push_str(&option_lines(LOOP_OPTIONS, &value));
    out.push_str(concat!(
        "\n[loop.effort_schedule]\n",
        "# Values are whatever each agent's own CLI accepts, listed above, so\n",
        "# these are examples rather than defaults. Left out, each agent uses\n",
        "# the effort its own block asked for.\n",
        "# round_1 = \"high\"   # the deep first review\n",
        "# rest    = \"low\"    # later rounds and the closing pass\n\n",
    ));
    out.push_str("[style]\n");
    out.push_str(&option_lines(STYLE_OPTIONS, &value));
    out
}

/// Option lines with their notes lined up in a column, a long note wrapping
/// onto continuation lines that stay in the column rather than running off the
/// edge or restarting at the margin.
fn option_lines(options: &[Setting], value: &dyn Fn(&str) -> String) -> String {
    let mut out = String::new();
    for (commented, key, note) in options {
        if !out.is_empty() {
            out.push('\n');
        }
        out.push_str(&wrap_comment(note));
        let lead = if *commented { "# " } else { "" };
        out.push_str(&format!("{lead}{key} = {}\n", value(key)));
    }
    out
}

/// One prerequisite check: a label and something that either reports a version
/// or explains what is missing.
type Probe = Box<dyn Fn() -> Result<String>>;

/// One agent's block, with the options commented out beside a working value.
///
/// The values come from the preset rather than from here, so a CLI that adds a
/// model is a file edit. They are hints only: nothing validates against them,
/// because a stale list that refused a model which actually works would be
/// worse than no hint at all.
fn agent_block(name: &str, spec: &config::AgentSpec) -> String {
    let mut out = format!("[agents.{name}]\npreset = \"{name}\"\n");

    // Only what the preset has hints for. An option with none used to be
    // written as `# effort = "..."`, and a placeholder is not a working value:
    // the line fails the moment somebody takes the file at its word and
    // uncomments it. Cursor has no effort setting at all, so for that agent the
    // line should not exist rather than exist and be wrong.
    //
    // The first entry of each list is the one written as the suggested value,
    // which is why the presets put the sensible default there rather than in
    // whatever order a CLI's help happens to print.
    // Once, above both. The preset's note is about the pair, and repeating it
    // under each put a sentence about models underneath the effort line.
    if let Some(extra) = &spec.options_note {
        if !spec.models.is_empty() || !spec.efforts.is_empty() {
            out.push('\n');
            out.push_str(&wrap_comment(extra));
        }
    }
    for (key, choices) in [("model", &spec.models), ("effort", &spec.efforts)] {
        let Some(suggested) = choices.first() else {
            continue;
        };
        let mut note = format!("Omit {key} to use the CLI's own default.");
        if choices.len() > 1 {
            note.push_str(&format!(" One of: {}.", choices.join(" | ")));
        }
        out.push('\n');
        out.push_str(&wrap_comment(&note));
        out.push_str(&format!("# {key} = \"{suggested}\"\n"));
    }

    // The value from the spec, not a number typed here, for the reason the
    // [loop] block learned: a second copy of a default is the one that goes
    // stale.
    out.push('\n');
    out.push_str(&wrap_comment(
        "Seconds one call may take before spar gives up. A timeout costs the whole call and is \
         never retried, so err long.",
    ));
    out.push_str(&format!("# timeout = {}\n", spec.timeout));

    // Anything but this agent's own preset: a CLI that has just refused is not
    // a stand in for itself.
    let backup = if name == "cursor" { "gemini" } else { "cursor" };
    out.push('\n');
    out.push_str(&wrap_comment(
        "A stand in for when this CLI refuses, stalls, or runs out of quota. It answers in place \
         of this agent, never alongside it.",
    ));
    out.push_str(&format!(
        "# [agents.{name}.fallback]\n# preset = \"{backup}\"\n"
    ));

    // The rest of what an agent block takes defines a CLI rather than tunes
    // one, so it is pointed at rather than offered: a generated file that
    // invites somebody to edit `command` or `output` on a working preset is
    // offering them a way to break it.
    out.push('\n');
    out.push_str(&wrap_comment(
        "command, output, search_paths and the rest are in spar.example.toml, for pairing a CLI \
         that has no preset.",
    ));
    out.push('\n');
    out
}

/// What `spar init` says about an option, for `--update` to say too.
///
/// Empty for one with nothing written about it, and for the effort schedule,
/// whose two keys are examples rather than settings and are described by the
/// stanza they sit in rather than one at a time.
fn note_for(key: &str) -> &'static str {
    LOOP_OPTIONS
        .iter()
        .chain(STYLE_OPTIONS)
        .find(|(_, name, _)| *name == key)
        .map(|(_, _, note)| *note)
        .unwrap_or("")
}

/// Wrap a note across comment lines so a long one does not run off the edge.
fn wrap_comment(text: &str) -> String {
    const WIDTH: usize = 76;
    let mut out = String::new();
    let mut line = String::from("#");
    for word in text.split_whitespace() {
        if line.chars().count() + 1 + word.chars().count() > WIDTH && line.len() > 1 {
            out.push_str(&line);
            out.push('\n');
            line = String::from("#");
        }
        line.push(' ');
        line.push_str(word);
    }
    if line.len() > 1 {
        out.push_str(&line);
        out.push('\n');
    }
    out
}

fn cmd_doctor(config_path: Option<&Path>) -> Result<i32> {
    let mut ok = true;

    let probes: Vec<(&str, Probe)> = vec![
        (
            "git",
            Box::new(|| {
                proc::run_str(&["git", "--version"], &ExecOpts::new().timeout_secs(30))
                    .map(|s| first_line(&s))
            }),
        ),
        (
            "gh",
            Box::new(|| {
                proc::run_str(&["gh", "--version"], &ExecOpts::new().timeout_secs(30))
                    .map(|s| first_line(&s))
            }),
        ),
        (
            "gh auth",
            Box::new(|| {
                let out = proc::exec(
                    &["gh".into(), "auth".into(), "status".into()],
                    &ExecOpts::new().check(false).timeout_secs(60),
                )?;
                let text = format!("{}\n{}", out.stderr.trim(), out.stdout.trim());
                if out.ok() {
                    Ok(first_line(&text))
                } else {
                    Err(spar_err!("not authenticated. Run `gh auth login`."))
                }
            }),
        ),
    ];

    for (label, probe) in probes {
        match probe() {
            Ok(detail) => println!("  ok    {label:12} {detail}"),
            Err(e) => {
                println!("  FAIL  {label:12} {}", e.first_line());
                ok = false;
            }
        }
    }

    let found = config::find_config(config_path)?;
    let Some(path) = found else {
        println!("\n  no spar.toml found. Run `spar init` to generate one.");
        println!(
            "  presets available: {}",
            config::available_presets().join(", ")
        );
        return Ok(if ok { 0 } else { 1 });
    };

    println!("\n  config: {}", path.display());
    let cfg = match config::load(Some(&path)) {
        Ok(cfg) => cfg,
        Err(e) => {
            println!("  FAIL  config       {e}");
            return Ok(1);
        }
    };

    // Kept apart from `ok`: a missing gh says nothing about whether the two
    // agents are the same CLI, and must not silence the warning below.
    let mut resolved = Vec::new();
    for spec in &cfg.agents {
        let agent = Agent::new(spec.clone());
        match agent.resolve_bin() {
            Ok(bin) => {
                println!(
                    "  ok    {:12} {}  ({})",
                    spec.name,
                    bin.display(),
                    spec.describe()
                );
                report_fallback(&agent);
                resolved.push(agent);
            }
            Err(e) => {
                println!("  FAIL  {:12} {}", spec.name, e.first_line());
                ok = false;
            }
        }
    }

    if resolved.len() == cfg.agents.len() {
        if let Some(warning) = agent::correlation_warning(&resolved) {
            println!("\n  WARNING  {warning}");
        }
    }

    println!(
        "\n  settings: max_rounds={} auto_merge={} worktrees={} followups={} terse={}",
        cfg.loop_cfg.max_rounds,
        cfg.loop_cfg.auto_merge,
        cfg.loop_cfg.worktrees,
        cfg.loop_cfg.followups,
        cfg.style.terse
    );
    // What somebody upgrading wants to know. `spar init` refuses to touch an
    // existing config, so without this there is no way to learn that a release
    // added a setting short of reading the source.
    if let Ok(text) = std::fs::read_to_string(&path) {
        let unset = config::unmentioned_options(&text);
        if !unset.is_empty() {
            println!(
                "\n  {} setting(s) this config does not mention, all at their defaults:",
                unset.len()
            );
            for option in &unset {
                println!(
                    "      [{}] {} = {}",
                    option.section, option.key, option.default
                );
            }
            println!(
                "  `spar init --update {}` appends them as comments.",
                path.display()
            );
        }
    }

    println!(
        "{}",
        if ok {
            "\nready"
        } else {
            "\nmissing prerequisites"
        }
    );
    Ok(if ok { 0 } else { 1 })
}

fn first_line(text: &str) -> String {
    text.trim().lines().next().unwrap_or("").trim().to_string()
}

// ---------------------------------------------------------------------------
// Reporting
// ---------------------------------------------------------------------------

fn report(results: &[IssueRun], cfg: &Config, repo: &Repo) -> i32 {
    println!("\n{}", "=".repeat(60));
    for r in results {
        println!(
            "#{:<5} {:<10} rounds={} {}",
            r.issue,
            r.status.to_string(),
            r.rounds,
            r.pr.as_deref().unwrap_or("")
        );
        for note in &r.notes {
            println!("       {}", first_line(note));
        }
        for url in &r.filed {
            println!("       filed {url}");
        }
        for dispute in &r.disputes {
            println!(
                "       disputed: {}",
                report_item(&dispute.title, &dispute.file)
            );
        }
        for finding in &r.noted {
            println!(
                "       noted, not blocking: {}",
                report_item(&finding.title, &finding.file)
            );
        }
    }
    println!("{}", "=".repeat(60));

    if !cfg.loop_cfg.auto_merge && results.iter().any(|r| r.status == Status::Approved) {
        println!("\nApproved PRs are waiting on you to merge.");
    }
    let recorded: usize = results.iter().map(|r| r.filed.len()).sum();
    if recorded > 0 && cfg.loop_cfg.followups == crate::config::Followups::Local {
        println!(
            "\n{recorded} follow-up(s) recorded in .spar/followups.md, not on the tracker. \
             Set followups = \"issues\" to file them."
        );
    }
    let issue_exit = i32::from(!results.iter().all(IssueRun::succeeded));
    issue_exit.max(report_writes(repo))
}

/// A failed write makes the final status non-zero, including a partial failure.
/// Independent writes still finish first, so one failure does not discard work
/// that another item can land. The non-zero status lets automation retry what
/// was missed.
fn report_writes(repo: &Repo) -> i32 {
    let writes = repo.write_summary();
    if let Some(line) = write_summary_line(writes) {
        println!("\n{line}");
    }
    write_exit_code(writes)
}

fn write_summary_line(writes: WriteSummary) -> Option<String> {
    (writes.attempted > 0).then(|| {
        format!(
            "writes: {} attempted, {} succeeded, {} failed",
            writes.attempted,
            writes.succeeded(),
            writes.failed
        )
    })
}

fn write_exit_code(writes: WriteSummary) -> i32 {
    i32::from(writes.failed > 0)
}

fn report_item(title: &str, file: &str) -> String {
    match file.trim() {
        "" => title.to_string(),
        location => format!("{title} ({location})"),
    }
}

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

    #[test]
    fn report_items_include_their_location() {
        assert_eq!(
            "Same title (src/a.rs:10)",
            report_item("Same title", "src/a.rs:10")
        );
        assert_eq!("General point", report_item("General point", ""));
    }

    #[test]
    fn every_attempted_write_failing_is_a_failed_run() {
        let writes = WriteSummary {
            attempted: 3,
            failed: 3,
        };

        assert_eq!(1, write_exit_code(writes));
        assert_eq!(
            Some("writes: 3 attempted, 0 succeeded, 3 failed".to_string()),
            write_summary_line(writes)
        );
    }

    #[test]
    fn a_partial_write_failure_is_reported_after_the_run() {
        let writes = WriteSummary {
            attempted: 3,
            failed: 1,
        };

        assert_eq!(1, write_exit_code(writes));
        assert_eq!(2, writes.succeeded());
    }

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

    #[test]
    fn quiet_is_accepted_before_or_after_the_subcommand() {
        for argv in [
            vec!["spar", "--quiet", "run", "42"],
            vec!["spar", "run", "42", "--quiet"],
            vec!["spar", "resume", "--quiet"],
            vec!["spar", "init", "-q"],
        ] {
            assert!(Cli::parse_from(&argv).quiet, "{argv:?}");
        }
        assert!(!Cli::parse_from(["spar", "run", "42"]).quiet);
    }

    #[test]
    fn several_issue_numbers_are_accepted() {
        let cli = Cli::parse_from(["spar", "run", "42", "51", "60"]);
        match cli.command {
            Command::Run { issues, .. } => assert_eq!(vec![42, 51, 60], issues),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn issue_numbers_and_flags_can_be_interleaved() {
        let cli = Cli::parse_from(["spar", "run", "42", "--auto-merge", "51"]);
        match cli.command {
            Command::Run {
                issues, loop_flags, ..
            } => {
                assert_eq!(vec![42, 51], issues);
                assert!(loop_flags.auto_merge);
            }
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn every_command_that_reads_a_config_accepts_one() {
        for argv in [
            vec!["spar", "run", "42"],
            vec!["spar", "triage"],
            vec!["spar", "resume"],
            vec!["spar", "followup"],
            vec!["spar", "checkin"],
            vec!["spar", "split"],
            vec!["spar", "clean"],
            vec!["spar", "doctor"],
        ] {
            let mut full = argv.clone();
            full.extend(["--config", "other.toml"]);
            let cli = Cli::parse_from(&full);
            let config = match cli.command {
                Command::Run { common, .. }
                | Command::Triage { common, .. }
                | Command::Resume { common, .. }
                | Command::Followup { common, .. }
                | Command::Split { common, .. }
                | Command::Checkin { common, .. } => common.config,
                Command::Clean { config, .. } | Command::Doctor { config } => config,
                other => panic!("{other:?}"),
            };
            assert_eq!(Some(PathBuf::from("other.toml")), config, "{argv:?}");
        }
    }

    #[test]
    fn auto_merge_is_off_unless_asked_for() {
        let cli = Cli::parse_from(["spar", "run"]);
        match cli.command {
            Command::Run { loop_flags, .. } => assert!(!loop_flags.auto_merge),
            other => panic!("{other:?}"),
        }
    }

    /// The flag reaches resume as well as run, which is what makes "stop, edit
    /// the config, carry on with something extra to say" a thing you can do.
    #[test]
    fn every_command_that_reads_a_config_takes_instructions() {
        // `followup` is given no number, because it takes none: its entries
        // have no identity a person could type.
        for argv in [
            vec!["spar", "run", "7", "--instructions", "Do not wait for CI."],
            vec![
                "spar",
                "triage",
                "7",
                "--instructions",
                "Do not wait for CI.",
            ],
            vec![
                "spar",
                "resume",
                "7",
                "--instructions",
                "Do not wait for CI.",
            ],
            vec![
                "spar",
                "review",
                "7",
                "--instructions",
                "Do not wait for CI.",
            ],
            vec!["spar", "followup", "--instructions", "Do not wait for CI."],
            vec![
                "spar",
                "checkin",
                "7",
                "--instructions",
                "Do not wait for CI.",
            ],
            vec![
                "spar",
                "split",
                "7",
                "--instructions",
                "Do not wait for CI.",
            ],
        ] {
            let parsed = Cli::parse_from(&argv);
            let common = match parsed.command {
                Command::Run { common, .. }
                | Command::Triage { common, .. }
                | Command::Resume { common, .. }
                | Command::Review { common, .. }
                | Command::Followup { common, .. }
                | Command::Split { common, .. }
                | Command::Checkin { common, .. } => common,
                other => panic!("{other:?}"),
            };
            assert_eq!(
                Some("Do not wait for CI."),
                common.instructions.as_deref(),
                "{argv:?}"
            );
        }
    }

    #[test]
    fn the_two_close_skipped_flags_are_mutually_exclusive() {
        assert!(
            Cli::try_parse_from(["spar", "run", "--close-skipped", "--no-close-skipped"]).is_err()
        );
    }

    /// Only `run` triages, so only `run` can decline an issue. Accepting the
    /// flag on `resume` would silently do nothing.
    #[test]
    fn close_skipped_is_offered_only_where_it_means_something() {
        assert!(Cli::try_parse_from(["spar", "run", "--close-skipped"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "run", "--no-close-skipped"]).is_ok());
        // `followup` triages what it files, so it can decline it too.
        assert!(Cli::try_parse_from(["spar", "followup", "--close-skipped"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "resume", "--close-skipped"]).is_err());
        assert!(Cli::try_parse_from(["spar", "review", "--close-skipped"]).is_err());
        assert!(Cli::try_parse_from(["spar", "triage", "--close-skipped"]).is_err());
    }

    /// An entry in the follow-up queue has no number and its title is prose, so
    /// a number on the command line could only be silently ignored.
    #[test]
    fn followup_takes_no_numbers() {
        assert!(Cli::try_parse_from(["spar", "followup"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "followup", "42"]).is_err());
    }

    /// `--screen-only` stops before `--file-only` does, so asking for both says
    /// nothing about where to stop.
    #[test]
    fn the_two_stopping_points_are_mutually_exclusive() {
        assert!(Cli::try_parse_from(["spar", "followup", "--screen-only"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "followup", "--file-only"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "followup", "--screen-only", "--file-only"]).is_err());
    }

    #[test]
    fn the_close_skipped_pair_resolves_to_a_tristate() {
        let read = |argv: &[&str]| match Cli::parse_from(argv).command {
            Command::Run { triage_flags, .. } => {
                match (triage_flags.close_skipped, triage_flags.no_close_skipped) {
                    (true, _) => Some(true),
                    (_, true) => Some(false),
                    _ => None,
                }
            }
            other => panic!("{other:?}"),
        };
        assert_eq!(None, read(&["spar", "run"]));
        assert_eq!(Some(true), read(&["spar", "run", "--close-skipped"]));
        assert_eq!(Some(false), read(&["spar", "run", "--no-close-skipped"]));
    }

    #[test]
    fn the_default_limit_is_twenty() {
        let cli = Cli::parse_from(["spar", "run"]);
        match cli.command {
            Command::Run { common, .. } => assert_eq!(20, common.limit),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn the_scrub_filter_subcommand_is_hidden_but_reachable() {
        assert!(matches!(
            Cli::parse_from(["spar", "scrub-filter"]).command,
            Command::ScrubFilter
        ));
        let help = Cli::command().render_long_help().to_string();
        assert!(
            !help.contains("scrub-filter"),
            "it is plumbing, not a command"
        );
    }

    #[test]
    fn review_takes_pr_numbers_and_a_dry_run() {
        let cli = Cli::parse_from(["spar", "review", "101", "102", "--dry-run"]);
        match cli.command {
            Command::Review { items, dry_run, .. } => {
                assert_eq!(vec![101, 102], items);
                assert!(dry_run);
            }
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn review_posts_unless_told_not_to() {
        match Cli::parse_from(["spar", "review", "101"]).command {
            Command::Review { dry_run, .. } => assert!(!dry_run),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn review_with_no_numbers_is_allowed() {
        match Cli::parse_from(["spar", "review"]).command {
            Command::Review { items, .. } => assert!(items.is_empty()),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn review_takes_its_own_round_budget() {
        match Cli::parse_from(["spar", "review", "101", "--max-rounds", "2"]).command {
            Command::Review { max_rounds, .. } => assert_eq!(Some(2), max_rounds),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn checkin_takes_pr_numbers_and_a_dry_run() {
        match Cli::parse_from(["spar", "checkin", "108", "112", "--dry-run"]).command {
            Command::Checkin { items, dry_run, .. } => {
                assert_eq!(vec![108, 112], items);
                assert!(dry_run);
            }
            other => panic!("{other:?}"),
        }
        match Cli::parse_from(["spar", "checkin"]).command {
            Command::Checkin { items, dry_run, .. } => {
                assert!(items.is_empty());
                assert!(!dry_run);
            }
            other => panic!("{other:?}"),
        }
    }

    /// `--auto-merge` must not exist on the one command whose input is written
    /// by somebody else, and `--max-rounds` would be worse than useless: the
    /// judgement is two passes by construction, so `--max-rounds 1` could only
    /// mean "let one agent decide alone", which removes the one thing standing
    /// between a stranger's comment and a push.
    #[test]
    fn checkin_offers_no_flag_that_would_weaken_the_pair() {
        assert!(Cli::try_parse_from(["spar", "checkin", "--auto-merge"]).is_err());
        assert!(Cli::try_parse_from(["spar", "checkin", "--max-rounds", "1"]).is_err());
        assert!(Cli::try_parse_from(["spar", "checkin", "--absorb", "1"]).is_err());
        assert!(Cli::try_parse_from(["spar", "checkin", "--close-skipped"]).is_err());
        // What it does offer.
        assert!(Cli::try_parse_from(["spar", "checkin", "--reply-only"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "checkin", "--any-author"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "checkin", "--again"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "checkin", "--keep-worktrees"]).is_ok());
    }

    #[test]
    fn resume_takes_a_next_override() {
        let cli = Cli::parse_from(["spar", "resume", "108", "--next", "codex"]);
        match cli.command {
            Command::Resume {
                prs, next_actor, ..
            } => {
                assert_eq!(vec![108], prs);
                assert_eq!(Some("codex".to_string()), next_actor);
            }
            other => panic!("{other:?}"),
        }
    }
}

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

    #[test]
    fn absorb_is_off_unless_asked_for() {
        match Cli::parse_from(["spar", "run"]).command {
            Command::Run { loop_flags, .. } => assert_eq!(None, loop_flags.absorb),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn absorb_takes_a_wave_count() {
        match Cli::parse_from(["spar", "run", "--absorb", "2"]).command {
            Command::Run { loop_flags, .. } => assert_eq!(Some(2), loop_flags.absorb),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn absorb_is_only_offered_where_issues_are_worked() {
        assert!(Cli::try_parse_from(["spar", "run", "--absorb", "1"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "resume", "--absorb", "1"]).is_ok());
        assert!(Cli::try_parse_from(["spar", "review", "--absorb", "1"]).is_err());
        // `split` decomposes and stops, so there is nothing for a wave of newly
        // filed issues to be absorbed into.
        assert!(Cli::try_parse_from(["spar", "split", "--absorb", "1"]).is_err());
    }
}

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

    #[test]
    fn split_takes_numbers_of_either_kind() {
        match Cli::parse_from(["spar", "split", "10", "12"]).command {
            Command::Split { items, .. } => assert_eq!(vec![10, 12], items),
            other => panic!("{other:?}"),
        }
    }

    /// The bare form means both kinds, which no other command does, so it has
    /// to be allowed to take nothing.
    #[test]
    fn split_with_no_numbers_is_allowed() {
        match Cli::parse_from(["spar", "split"]).command {
            Command::Split {
                items,
                dry_run,
                again,
                ..
            } => {
                assert!(items.is_empty());
                assert!(!dry_run);
                assert!(!again);
            }
            other => panic!("{other:?}"),
        }
    }

    /// A wrong split is N issues to close, a checklist to strip out of
    /// somebody's body, and branches and pull requests to delete, so the read
    /// only half is not optional.
    #[test]
    fn split_can_be_told_to_write_nothing() {
        match Cli::parse_from(["spar", "split", "10", "--dry-run"]).command {
            Command::Split { dry_run, .. } => assert!(dry_run),
            other => panic!("{other:?}"),
        }
    }

    /// The flag `checkin` established for exactly this: do it again on
    /// something spar already dealt with.
    #[test]
    fn split_can_be_told_to_split_something_it_already_split() {
        match Cli::parse_from(["spar", "split", "10", "--again"]).command {
            Command::Split { again, .. } => assert!(again),
            other => panic!("{other:?}"),
        }
    }

    /// It picks for itself when given nothing, so it takes both of the flags
    /// that govern picking, and its config like everything else.
    #[test]
    fn split_takes_the_flags_that_govern_picking() {
        match Cli::parse_from([
            "spar",
            "split",
            "--limit",
            "5",
            "--min-number",
            "480",
            "--config",
            "other.toml",
        ])
        .command
        {
            Command::Split { common, .. } => {
                assert_eq!(5, common.limit);
                assert_eq!(Some(480), common.min_number);
                assert_eq!(Some(PathBuf::from("other.toml")), common.config);
            }
            other => panic!("{other:?}"),
        }
    }

    /// `split` decomposes and stops. A flag that implied it works, reviews, or
    /// merges anything would be a flag that does nothing.
    #[test]
    fn split_offers_no_flag_that_would_make_it_a_second_run() {
        for flag in [
            vec!["--auto-merge"],
            vec!["--max-rounds", "2"],
            vec!["--close-skipped"],
            vec!["--no-close-skipped"],
            vec!["--keep-worktrees"],
        ] {
            let mut argv = vec!["spar", "split", "10"];
            argv.extend(flag.iter().copied());
            assert!(Cli::try_parse_from(&argv).is_err(), "{argv:?}");
        }
    }
}

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

    fn read(argv: &[&str]) -> Option<i64> {
        match Cli::parse_from(argv).command {
            Command::Run { common, .. }
            | Command::Triage { common, .. }
            | Command::Resume { common, .. }
            | Command::Review { common, .. }
            | Command::Followup { common, .. }
            | Command::Split { common, .. }
            | Command::Checkin { common, .. } => common.min_number,
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn there_is_no_floor_unless_one_is_asked_for() {
        assert_eq!(None, read(&["spar", "run"]));
    }

    #[test]
    fn every_command_that_picks_for_itself_accepts_a_floor() {
        for cmd in ["run", "triage", "resume", "review", "checkin", "split"] {
            assert_eq!(
                Some(480),
                read(&["spar", cmd, "--min-number", "480"]),
                "{cmd}"
            );
        }
    }
}

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

    /// The value a config line offers, with its trailing note removed. Quote
    /// aware, since a note is free to contain a `#` and several do.
    fn written(line: &str) -> String {
        let after = line.split_once('=').expect("an assignment").1;
        let mut quoted = false;
        for (i, c) in after.char_indices() {
            match c {
                '"' => quoted = !quoted,
                '#' if !quoted => return after[..i].trim().to_string(),
                _ => {}
            }
        }
        after.trim().to_string()
    }

    fn line_for(text: &str, key: &str) -> String {
        text.lines()
            .find(|l| {
                let bare = l.trim_start().trim_start_matches('#').trim_start();
                bare.starts_with(&format!("{key} ")) || bare.starts_with(&format!("{key}="))
            })
            .unwrap_or_else(|| panic!("{key} is not offered at all:\n{text}"))
            .to_string()
    }

    /// The guard that was missing. Every number in the generated config used to
    /// be typed in beside its comment, which is a second copy of a default that
    /// lives in the code, and the copies stopped agreeing: it offered a title
    /// budget of 90 against a real 140, a body of 900 against 8000, and three
    /// more like it. Uncommenting one to see what it did cut every comment spar
    /// posts to a fifth of its length.
    #[test]
    fn every_value_it_offers_is_the_default_it_actually_has() {
        let text = settings_block("claude");
        for option in config::known_options() {
            // The effort words are per CLI, so the schedule's are examples of
            // what one accepts rather than defaults. There is no default
            // effort: an agent that names none uses its own CLI's.
            if option.section == "loop.effort_schedule" {
                continue;
            }
            let line = line_for(&text, &option.key);
            assert_eq!(
                option.default,
                written(&line),
                "the generated config offers `{}`, but the default is {}",
                line.trim(),
                option.default
            );
        }
    }

    /// `doctor` reports what a config does not mention, so a generated one
    /// should send nobody to that list on the day it was written. pr_comments
    /// was missing from it for exactly that long.
    #[test]
    fn it_offers_every_option_the_parser_knows_about() {
        let text = settings_block("claude");
        let missing: Vec<String> = config::unmentioned_options(&text)
            .into_iter()
            .map(|o| format!("[{}] {}", o.section, o.key))
            .collect();
        assert!(missing.is_empty(), "not offered: {}", missing.join(", "));
    }

    /// The strongest of these: every line the file suggests has to be a line
    /// that works. A commented option is an invitation to uncomment it, and one
    /// that then fails to load is worse than never having offered it.
    #[test]
    fn every_option_it_offers_can_be_uncommented_and_still_load() {
        let mut text = String::from(
            "[agents.claude]\ncommand = [\"claude\"]\n\n\
             [agents.codex]\ncommand = [\"codex\"]\n\n",
        );
        for line in settings_block("claude").lines() {
            text.push_str(uncomment(line).unwrap_or(line));
            text.push('\n');
        }
        let cfg = config::parse(&text).expect("a config of its own suggestions");
        assert_eq!("claude", cfg.first_implementor);
    }

    /// A commented assignment with its `#` removed, or None for a line of
    /// prose, which stays a comment.
    fn uncomment(line: &str) -> Option<&str> {
        let bare = line.trim_start().strip_prefix('#')?.trim_start();
        // An assignment, not a wrapped note that happens to contain an `=`:
        // the key has to be one bare word.
        let key = bare.split_once('=')?.0.trim();
        let named = !key.is_empty()
            && key
                .chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_');
        named.then_some(bare)
    }

    #[test]
    fn the_agent_that_goes_first_is_the_one_that_was_chosen() {
        assert!(settings_block("codex").contains("first_implementor = \"codex\""));
    }

    /// Every line starts at the margin, and an option's note sits above the
    /// option rather than beside it. Beside meant three different columns in
    /// one file, and a note that wrapped left the reader tracking indentation
    /// to work out which setting it belonged to.
    #[test]
    fn a_note_sits_above_the_option_it_describes() {
        let text = settings_block("claude");
        assert!(
            text.lines().all(|l| l.chars().count() <= 80),
            "a line runs off the edge:\n{text}"
        );
        assert!(
            text.lines().all(|l| !l.starts_with(' ')),
            "a line is indented, so the columns are back:\n{text}"
        );

        // The line before an option carries its note, not another option.
        let lines: Vec<&str> = text.lines().collect();
        let at = lines
            .iter()
            .position(|l| l.starts_with("max_rounds"))
            .expect("max_rounds");
        assert!(lines[at - 1].starts_with('#'), "{:?}", lines[at - 1]);
        assert!(
            lines[at - 1].contains("lifetime cap"),
            "the note above it is the end of its own note: {:?}",
            lines[at - 1]
        );
    }

    /// A note reads as a sentence now that it leads rather than trails.
    #[test]
    fn every_note_starts_as_a_sentence() {
        for (_, key, note) in LOOP_OPTIONS.iter().chain(STYLE_OPTIONS) {
            let first = note.chars().next().expect("a note");
            assert!(
                first.is_uppercase(),
                "{key} reads as a margin scribble rather than a sentence: {note}"
            );
        }
    }
}

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

    fn spec(models: &[&str], efforts: &[&str]) -> config::AgentSpec {
        let mut spec: config::AgentSpec =
            toml::Value::Table(toml::from_str("command = [\"x\"]").expect("a minimal preset"))
                .try_into()
                .expect("builds");
        spec.models = models.iter().map(|s| s.to_string()).collect();
        spec.efforts = efforts.iter().map(|s| s.to_string()).collect();
        spec
    }

    /// A placeholder is not a working value. `# effort = "..."` was written for
    /// every preset that lists no efforts, and taking the file at its word by
    /// uncommenting it passes `...` to the CLI as a real setting.
    #[test]
    fn an_option_with_no_hints_is_left_out_rather_than_guessed_at() {
        let block = agent_block("cursor", &spec(&["composer-2.5", "auto"], &[]));
        assert!(!block.contains("..."), "{block}");
        assert!(!block.contains("effort"), "{block}");
        assert!(block.contains("# model = \"composer-2.5\""), "{block}");
    }

    /// Each option is introduced by its own line, so a block with no effort
    /// setting never mentions one.
    #[test]
    fn only_the_options_that_follow_are_introduced() {
        let model_only = agent_block("cursor", &spec(&["auto"], &[]));
        assert!(model_only.contains("Omit model to use"), "{model_only}");
        assert!(!model_only.contains("Omit effort"), "{model_only}");

        let both = agent_block("claude", &spec(&["fable"], &["high"]));
        assert!(both.contains("Omit model to use"), "{both}");
        assert!(both.contains("Omit effort to use"), "{both}");
    }

    /// A preset with no hints at all still has to produce a loadable block,
    /// which is every preset that has never listed any: gemini and aider.
    #[test]
    fn a_preset_with_no_hints_still_writes_a_usable_block() {
        let block = agent_block("gemini", &spec(&[], &[]));
        assert!(!block.contains("..."), "{block}");
        assert!(!block.contains("Omit"), "{block}");
        assert!(
            block.starts_with("[agents.gemini]\npreset = \"gemini\"\n"),
            "{block}"
        );
        // What the block does still carry is unaffected.
        assert!(block.contains("[agents.gemini.fallback]"), "{block}");
        assert!(block.contains("# timeout = "), "{block}");
    }

    /// The timeout comes from the spec rather than a number typed into the
    /// generator, for the reason the [loop] block learned the hard way.
    #[test]
    fn the_timeout_offered_is_the_one_the_agent_would_use() {
        let mut spec = spec(&["a"], &[]);
        spec.timeout = 7200;
        assert!(
            agent_block("custom", &spec).contains("# timeout = 7200"),
            "the generator kept its own copy"
        );
    }

    /// Alternatives are named, and a single choice is not dressed up as one.
    #[test]
    fn alternatives_are_listed_only_when_there_are_any() {
        assert!(agent_block("a", &spec(&["one", "two"], &[])).contains("One of: one | two."));
        let single = agent_block("b", &spec(&["only"], &[]));
        assert!(!single.contains("One of:"), "{single}");
    }

    /// The preset's own note is about the pair, so it appears once above them
    /// rather than under each, which put a sentence about models beneath the
    /// effort line.
    #[test]
    fn the_presets_note_is_said_once() {
        let mut spec = spec(&["m1", "m2"], &["e1", "e2"]);
        spec.options_note = Some("Check the current sets with: mytool --help".into());
        let block = agent_block("mytool", &spec);
        assert_eq!(
            1,
            block.matches("Check the current sets").count(),
            "{block}"
        );
    }
}