spar-cli 0.3.9

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
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
//! Breaking one issue or one pull request into smaller ones.
//!
//! The unit of work was fixed: one issue became one branch and one pull
//! request whatever its size, and the only thing that changed it was somebody
//! filing the smaller issues by hand. The review loop is the product and its
//! quality falls off with diff size, so the size of the unit is the biggest
//! lever there is on how well spar works, and it was the one a person could not
//! pull.
//!
//! `split` decomposes and stops. It never triages, never implements a child,
//! and never merges: it produces smaller units and hands them to the commands
//! that already exist. That keeps one invocation cheap and comprehensible, and
//! it keeps the blast radius of a wrong split to some issues and some branches
//! rather than to hours of implementation.
//!
//! Two agents, asymmetrically. One proposes the parts with the code open, the
//! other rules on the proposal: accept, reject, or accept with named parts
//! struck. Not two independent proposals, because two decompositions of one
//! thing cannot be reconciled mechanically and reconciling them is a third
//! judgement nobody asked for. Disagreement resolves toward not splitting, on
//! the same asymmetry `checkin` runs on: getting a decline wrong costs one
//! person one read of something that stays as it was, and getting the write
//! wrong costs them a queue to clean up.
//!
//! **spar never rewrites the branch behind somebody's pull request.** Splitting
//! a pull request is purely additive: new branches, new pull requests, one
//! comment. No existing branch is moved, nothing is closed, and nothing is
//! rebased under anybody. Removing half of a pull request in place is destroying
//! work in place, and two models agreeing does not make that reversible for the
//! person who wrote it. `additive` is that invariant as code.

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

use crate::agent::{self, Agent};
use crate::config::Config;
use crate::error::{ErrorKind, Result, SparError};
use crate::model::{
    Implementation, Issue, IssueRun, ItemKind, PrRow, PrView, SplitCheck, SplitPart, SplitProposal,
    SplitScreen, SplitScreenDoc, Status,
};
use crate::repo::{Repo, SplitPushError, WorktreeCheckpoint};
use crate::style::{self, Style};
use crate::{bail, log, logdim, logwarn, schema, spar_err};

// ---------------------------------------------------------------------------
// Prompts
// ---------------------------------------------------------------------------

const SCREEN_PROMPT: &str = "\
Below are the open issues and pull requests on this repository. For each, say
whether it is worth splitting into smaller pieces.

Read the code in your working directory before judging. Do not modify anything.

Split is for something that is plainly several separate pieces of work, and
would be reviewed better as several: three unrelated fixes filed as one issue, a
pull request that grew a refactor while it was being corrected. Size alone is not
the test. One change across forty files is one piece of work; a mess across three
files can be three.

Say false when you are unsure. No is the common answer, and a split proposed on a
whim is a proposal somebody now has to read.

Items:
";

const PROPOSE_ISSUE_PROMPT: &str = "\
Issue #{number}: {title}
{url}

Somebody has read this and decided it is too big to work in one piece. Read the
code in your working directory before you decide anything. Do not modify,
commit, or push anything.

Say whether it really is several separate pieces of work, and if it is, what they
are. Each part becomes its own issue, implemented and reviewed on its own branch,
so a part has to be worth a pull request by itself: implementable without the
others and reviewable without them.

Being large is not the test. One change that touches forty files is one part.

Set should_split=false if this is one piece of work. That is a fine answer and it
is the common one. Set files to null: there is no diff here to partition.

The issue:
{body}";

const PROPOSE_PR_PROMPT: &str = "\
Pull request #{number} against `{base}`: {title}

Somebody has read this and decided it is too big to review in one piece. Your
checkout is the head of that pull request, detached and read only. Do not modify,
commit, or push anything.

Say whether the change is several separate pieces, and if it is, which files each
piece carries. Every part is then built on its own branch, carrying only its own
files, and has to build and pass there. A part that cannot do that is not a part:
fold its files into another part, or leave them out.

Use only paths from the list below, copied exactly. A path belongs to at most one
part, and leaving a path out is allowed: what no part carries is reported as left
over on the original pull request, which stays open.

Set should_split=false if this is one change, however large.

The {count} file(s) this pull request changes:
{files}";

const CHECK_PROMPT: &str = "\
Another agent read {what} and proposed splitting it into the parts below. You did
not make this call.

Go to the code and rule on it. Do not defer to them, and do not agree to be
agreeable. Getting a rejection wrong costs one person one read of something that
stays as it was. Getting an acceptance wrong costs them issues to close, a
checklist to strip out of somebody's body, and branches and pull requests to
delete.

Reject the proposal outright, or accept it with the parts that do not hold
struck. Striking so many that fewer than two remain means nothing is split, which
is the right answer when that is what you think.

Their reason: {reason}
They say the parts are {shape}.

The parts:
{parts}";

const STAND_ALONE_PROMPT: &str = "\
This branch holds one part of pull request #{parent}, split out of it. The other
parts are not here and are not coming.

Make this part stand on its own against `{base}`. Read what is here, add whatever
it needs to build and to pass its tests without the rest, and leave those edits
uncommitted for the harness.

If it cannot stand on its own, set not_worth_doing=true and give the reason. Make
no changes in that case, and the part is dropped rather than pushed
broken. The whole value of splitting is that each part can be reviewed and merged
independently, and a part that does not build has none of it.

Write summary, problem, changes and testing for this part alone. They become the
body of its own pull request, read by somebody who has not seen the parent.

Part {index} of {total}: {title}

{body}

The files it carries:
{files}";

// ---------------------------------------------------------------------------
// What says a split already happened
// ---------------------------------------------------------------------------

/// Written into a split parent's body and into the comment left on a split
/// pull request. An HTML comment, so GitHub renders it as nothing.
pub const SPLIT_MARKER: &str = "<!-- spar:split -->";

/// Whether this text already carries a checklist or a comment spar wrote.
///
/// The marker rather than a shape, for the reason `FOLLOWUP_MARKER` exists: a
/// checklist is exactly what somebody would write by hand, so its shape can
/// never establish who wrote it.
pub fn already_split(text: &str) -> bool {
    text.contains(SPLIT_MARKER)
}

/// A split parent's body: what it said, then the checklist of its parts.
///
/// The original text comes through untouched, byte for byte, and the checklist
/// is appended after it. This is the one place spar rewrites prose somebody
/// else wrote, and appending is the only shape of that which cannot lose any
/// of it.
///
/// Each line carries its `#N`, which is what makes the parent an ordinary
/// tracker: a checklist whose items are issue numbers.
pub fn tracker_body(original: &str, parts: &[(String, i64)]) -> String {
    let mut out = original.to_string();
    if !out.is_empty() {
        if let Some(fence) = unclosed_fence(&out) {
            // A fence somebody left open swallows everything after it, so the
            // checklist would render as code rather than as a checklist.
            end_line(&mut out);
            out.push_str(&fence);
        }
        separate(&mut out);
    }
    out.push_str(SPLIT_MARKER);
    out.push_str("\n\n## Parts\n\nThis is now a tracker. Each part below is its own issue.\n\n");
    for (title, number) in parts {
        out.push_str(&format!("- [ ] #{number} {}\n", title.trim()));
    }
    out
}

fn end_line(text: &mut String) {
    if !text.ends_with('\n') {
        text.push('\n');
    }
}

fn separate(text: &mut String) {
    end_line(text);
    if !text.ends_with("\n\n") {
        text.push('\n');
    }
}

/// The fence that closes this text, when it ends inside a fenced code block.
///
/// The opener is carried rather than counted, because only a fence of the same
/// character and at least the same length closes it: three backticks inside a
/// block opened with four are content, and no number of backticks closes a
/// block opened with tildes. Anything else would answer with a fence that does
/// not close the block, and the checklist stays inside it either way.
fn unclosed_fence(text: &str) -> Option<String> {
    let mut open: Option<(char, usize)> = None;
    for line in text.lines() {
        let start = line.trim_start();
        let Some(ch @ ('`' | '~')) = start.chars().next() else {
            continue;
        };
        let run = start.chars().take_while(|c| *c == ch).count();
        if run < 3 {
            continue;
        }
        match open {
            None => open = Some((ch, run)),
            // A closing fence is the same character, no shorter than the one
            // that opened the block, and carries no info string.
            Some((open_ch, len))
                if open_ch == ch && run >= len && start.trim_end().chars().all(|c| c == ch) =>
            {
                open = None;
            }
            Some(_) => {}
        }
    }
    open.map(|(ch, len)| ch.to_string().repeat(len))
}

// ---------------------------------------------------------------------------
// The invariant
// ---------------------------------------------------------------------------

/// Refuse any branch a split did not itself create.
///
/// **spar never rewrites the branch behind somebody's pull request.** This is
/// the one write path a pull request split has, and it is worth an assertion
/// rather than a paragraph in a prompt: everything else here adds objects, and
/// a push to the wrong name is the single change that would destroy work in
/// place.
pub fn additive(branch: &str, parent_head: &str, prefix: &str) -> Result<()> {
    let wanted = format!("{prefix}split-");
    if !branch.starts_with(&wanted) {
        bail!("refusing to push to {branch}: a split only ever writes {wanted}* branches");
    }
    if branch == parent_head.trim() {
        bail!("refusing to push to {branch}: it is the branch behind the pull request being split");
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Deciding
// ---------------------------------------------------------------------------

/// Where a run of `spar split` stops.
#[derive(Debug, Clone, Copy)]
pub struct Mode {
    /// Print the proposal and write nothing.
    pub dry_run: bool,
    /// Split something that already carries a split spar made.
    pub again: bool,
}

/// What survived the proposal, the check, and the cap.
#[derive(Debug, Clone, Default)]
pub struct Decision {
    pub parts: Vec<SplitPart>,
    /// Each part branched off the one before it, rather than off the base.
    pub stacked: bool,
    /// Why nothing is being split, when nothing is.
    pub declined: Option<String>,
    /// Parts that were proposed and are not being made, said out loud because a
    /// cap that fires silently is a cap nobody can raise.
    pub dropped: Vec<String>,
}

impl Decision {
    pub fn splits(&self) -> bool {
        self.declined.is_none() && self.parts.len() > 1
    }
}

/// Rule on a proposal, given the check and the cap.
///
/// Every path out of disagreement is "do not split". The proposing agent
/// declining, the checking agent rejecting, and the checking agent striking
/// enough parts that one is left all mean the same thing here, because a split
/// into one part is not a split.
pub fn decide(proposal: &SplitProposal, check: &SplitCheck, cap: usize) -> Decision {
    let mut out = Decision {
        // Stacked whenever either agent says so. The two shapes are not equally
        // safe to be wrong about: parts built independently out of a change that
        // is really sequential do not build, and a part that does not build is
        // dropped. A stacked part always applies on its predecessor.
        stacked: proposal.stacked || check.stacked,
        ..Decision::default()
    };

    if !proposal.should_split {
        out.declined = Some(reason_or(&proposal.reason, "it is one piece of work"));
        return out;
    }
    if !check.accept {
        out.declined = Some(reason_or(
            &check.reasoning,
            "the second agent did not accept the split",
        ));
        return out;
    }

    let struck: BTreeSet<i64> = check.strike.iter().copied().collect();
    for (i, part) in proposal.parts.iter().enumerate() {
        let number = i as i64 + 1;
        if struck.contains(&number) {
            out.dropped
                .push(format!("{} (struck by the second agent)", label(part)));
            continue;
        }
        if part.title.trim().is_empty() {
            out.dropped.push("a part with no title".to_string());
            continue;
        }
        out.parts.push(part.clone());
    }

    if out.parts.len() < 2 {
        out.declined = Some(reason_or(
            &check.reasoning,
            "fewer than two parts survived, and a split into one part is not a split",
        ));
        out.parts.clear();
        return out;
    }

    if out.parts.len() > cap {
        for part in out.parts.split_off(cap) {
            out.dropped
                .push(format!("{} (over the max_split_parts cap)", label(&part)));
        }
    }
    out
}

fn label(part: &SplitPart) -> String {
    style::clip(part.title.trim(), 80)
}

fn reason_or(text: &str, fallback: &str) -> String {
    let trimmed = text.trim();
    if trimmed.is_empty() {
        fallback.to_string()
    } else {
        trimmed.to_string()
    }
}

/// The files a change touches that none of `carried` holds.
///
/// Reported on the original, which stays open, so that nothing goes missing
/// without somebody being told where it went. Takes the file lists rather than
/// the parts, because what a proposal claims and what was actually built are
/// different answers and the record has to be the second one.
pub fn leftover<'a>(
    changed: &[String],
    carried: impl IntoIterator<Item = &'a [String]>,
) -> Vec<String> {
    let taken: BTreeSet<&str> = carried.into_iter().flatten().map(String::as_str).collect();
    changed
        .iter()
        .filter(|path| !taken.contains(path.as_str()))
        .cloned()
        .collect()
}

/// Hold every part to the paths the change actually touches.
///
/// A path the model invented carries nothing and would be checked out of the
/// pull request head as a failure, and a path with `..` in it is one that would
/// be written outside the worktree. The list came from spar, so anything not in
/// it is a paraphrase.
fn confine(parts: &mut [SplitPart], changed: &[String]) -> Vec<String> {
    let known: BTreeSet<&str> = changed.iter().map(String::as_str).collect();
    let mut unknown = Vec::new();
    let mut claimed: BTreeSet<String> = BTreeSet::new();
    for part in parts.iter_mut() {
        part.files.retain(|path| {
            if !known.contains(path.as_str()) {
                unknown.push(path.clone());
                return false;
            }
            // A path in two parts would be carried twice and reviewed twice.
            claimed.insert(path.clone())
        });
    }
    unknown
}

// ---------------------------------------------------------------------------
// Screening a whole queue
// ---------------------------------------------------------------------------

/// One open item the bare `spar split` is considering.
#[derive(Debug, Clone)]
pub struct Candidate {
    pub number: i64,
    pub kind: ItemKind,
    pub title: String,
    /// The issue body, or the pull request's size.
    pub detail: String,
}

impl Candidate {
    pub fn from_issue(issue: &Issue) -> Self {
        Self {
            number: issue.number,
            kind: ItemKind::Issue,
            title: issue.title.clone(),
            detail: issue.body_text().trim().to_string(),
        }
    }

    pub fn from_pr(row: &PrRow) -> Self {
        Self {
            number: row.number,
            kind: ItemKind::Pr,
            title: row.title.clone(),
            detail: row.size(),
        }
    }
}

/// The queue as one prompt carries it, and what did not fit.
fn render(items: &[Candidate], cfg: &Config) -> (String, usize) {
    let mut parts: Vec<String> = Vec::new();
    let mut total = 0usize;
    let mut deferred = 0usize;
    for item in items {
        if deferred > 0 {
            deferred += 1;
            continue;
        }
        let block = format!(
            "{} #{}: {}\n{}",
            item.kind, item.number, item.title, item.detail
        );
        let len = block.chars().count();
        // The first item goes in whatever its size, for the reason
        // `followups::render` does it: a queue of one that does not fit is a
        // command that does nothing, forever.
        if !parts.is_empty() && total + len > cfg.loop_cfg.max_triage_chars {
            deferred += 1;
            continue;
        }
        total += len;
        parts.push(block);
    }
    (parts.join("\n\n"), deferred)
}

/// One agent's verdict on the whole queue in one call.
///
/// The asymmetry here is the opposite of the follow-up screen's. There the
/// screen says keep when unsure because what survives still faces two agent
/// triage. Here what survives faces propose and check, which is also two
/// agents, so a permissive screen costs money rather than damage. The prompt
/// still says not to split when unsure: no is the common answer, and a split
/// proposed on a whim is a proposal somebody now has to read.
pub fn screen(
    agent: &Agent,
    cfg: &Config,
    repo: &Repo,
    items: &[Candidate],
) -> Result<Vec<SplitScreen>> {
    let (text, deferred) = render(items, cfg);
    if deferred > 0 {
        logwarn!(
            "{deferred} item(s) did not fit in one screening prompt and were left for a later run"
        );
    }
    let answer: SplitScreenDoc = agent.ask_json(
        &format!("{SCREEN_PROMPT}{text}"),
        &schema::split_screen(),
        repo.root(),
        cfg.effort_for_round(&agent.spec, 1).as_deref(),
    )?;
    Ok(answer.items)
}

// ---------------------------------------------------------------------------
// Splitting an issue
// ---------------------------------------------------------------------------

pub fn split_issue(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    number: i64,
    mode: &Mode,
) -> IssueRun {
    match issue_inner(agents, cfg, repo, number, mode) {
        Ok(state) => state,
        Err(e) => failed(number, format!("#{number}"), e),
    }
}

fn failed(number: i64, label: String, e: crate::error::SparError) -> IssueRun {
    log!("{label} split failed: {e}");
    let mut state = IssueRun::new(number, label);
    state.status = Status::Error;
    state.notes.push(e.to_string());
    state
}

fn issue_inner(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    number: i64,
    mode: &Mode,
) -> Result<IssueRun> {
    let issue = repo
        .fetch_issues(&[number])?
        .into_iter()
        .next()
        .ok_or_else(|| spar_err!("#{number} is closed"))?;
    let mut state = IssueRun::new(number, issue.title.clone());

    if already_split(issue.body_text()) && !mode.again {
        log!("#{number} already carries a checklist spar wrote. --again splits it anyway.");
        state.status = Status::Whole;
        state.notes.push("already split".into());
        return Ok(state);
    }

    let (body, shortened) = issue.body_for_prompt(cfg.loop_cfg.max_issue_chars);
    if shortened {
        logwarn!(
            "#{number}: the issue body was shortened to fit the prompt. Raise max_issue_chars if \
             the rest matters."
        );
    }
    let prompt = PROPOSE_ISSUE_PROMPT
        .replace("{number}", &number.to_string())
        .replace("{title}", &issue.title)
        .replace("{url}", &issue.url)
        .replace("{body}", &body);

    let decision = propose_and_check(
        agents,
        cfg,
        repo.root(),
        &format!("#{number}"),
        &prompt,
        &format!("issue #{number}"),
    )?;

    for note in &decision.dropped {
        log!("  dropped {note}");
        state.notes.push(format!("dropped {note}"));
    }
    if !decision.splits() {
        let why = decision
            .declined
            .unwrap_or_else(|| "nothing survived the check".to_string());
        log!("#{number} left whole: {why}");
        state.status = Status::Whole;
        state.notes.push(why);
        return Ok(state);
    }

    if mode.dry_run {
        print_proposal(number, "issue", &decision, &[]);
        state.status = Status::Whole;
        state.notes.push(format!(
            "dry run: {} part(s) proposed",
            decision.parts.len()
        ));
        return Ok(state);
    }

    // Filed first, the parent rewritten after. An issue filed with no checklist
    // yet is findable and says where it came from; a checklist pointing at
    // issues that were never filed is not recoverable by reading it.
    let mut listed: Vec<(String, i64)> = Vec::new();
    for (i, part) in decision.parts.iter().enumerate() {
        // Cleaned here rather than only inside `file_as_issue`, because the
        // same string also goes into the parent's checklist, and that write
        // deliberately does not run the body through the style gate.
        let title = match repo.clean_nonempty_title_for_write(&part.title) {
            Ok(title) => title,
            Err(_) => {
                logwarn!("nothing left of '{}' after cleaning it", label(part));
                state.notes.push(format!(
                    "dropped {}: its title would not clean",
                    label(part)
                ));
                continue;
            }
        };
        let body = format!(
            "{}\n\nPart {} of {}, split out of #{number}.",
            part.body.trim(),
            i + 1,
            decision.parts.len()
        );
        match crate::review::file_as_issue_apart_from(repo, &title, &body, Some(number)) {
            Ok(filed) => {
                log!("  {}", filed.describe(&title));
                if let Some(url) = filed.url() {
                    state.filed.push(url.to_string());
                }
                // `file_as_issue` answers with an existing issue when the part
                // is already filed, and what a part resembles most is the issue
                // it came out of. A parent listed as its own child is a tracker
                // pointing at itself, and two parts that resolved to one issue
                // are one part twice.
                match filed.number() {
                    Some(n) if n == number => {
                        logwarn!("'{title}' came back as #{number} itself, so it is not a part");
                        state
                            .notes
                            .push(format!("dropped {title}: it matched #{number} itself"));
                    }
                    Some(n) if listed.iter().any(|(_, listed)| *listed == n) => {
                        logwarn!("'{title}' came back as #{n}, which another part already is");
                        state
                            .notes
                            .push(format!("dropped {title}: #{n} is already a part"));
                    }
                    Some(n) => listed.push((title, n)),
                    None => state.notes.push(format!("{title}: {}", filed.note())),
                }
            }
            Err(e) => {
                logwarn!("could not file '{title}': {e}");
                if e.kind() == ErrorKind::UncertainWrite {
                    record_uncertain_issue_part(
                        &mut state,
                        number,
                        &title,
                        &listed,
                        &e.to_string(),
                    );
                    return Ok(state);
                }
                state.notes.push(format!("could not file {title}: {e}"));
            }
        }
    }

    if listed.len() < 2 {
        // One child is still an external write even though it is not a useful
        // decomposition. Leaving it unrecorded and calling this whole invites
        // a later run to make another one.
        if !listed.is_empty() {
            record_partial_issue_split(&mut state, number, &listed);
            return Ok(state);
        }
        log!("#{number} left whole: no parts were filed");
        state.status = Status::Whole;
        return Ok(state);
    }

    let original = issue.body_text();
    let wanted = tracker_body(original, &listed);
    let inserted = &wanted[original.len()..];
    match repo.edit_issue_body(number, original, &wanted, inserted) {
        Ok(()) => {
            log!("#{number} is now a tracker for {} part(s)", listed.len());
            state.status = Status::Split;
        }
        Err(e) => {
            // The parts exist and the parent does not point at them, so `run`
            // would work the parent as a whole again. That needs somebody, so
            // it is an error rather than a note on a success.
            record_issue_tracker_failure(&mut state, number, &listed, &e);
        }
    }
    Ok(state)
}

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

fn record_issue_tracker_failure(
    state: &mut IssueRun,
    number: i64,
    parts: &[(String, i64)],
    error: &SparError,
) {
    state.status = Status::Error;
    let recovery = if error.kind() == ErrorKind::UncertainWrite {
        format!(
            "The parent write may already have landed. Do not rerun this split. Inspect the \
             current body of #{number} for the split marker `{SPLIT_MARKER}` and every line in \
             this exact checklist. If the marker and every line are present, do not add them \
             again. Otherwise add only the missing marker or child links by hand:\n{}",
            checklist(parts)
        )
    } else {
        format!(
            "The child issues were filed. Do not rerun this split. Add them to #{number} by \
             hand:\n{}",
            checklist(parts)
        )
    };
    state.notes.push(format!("{error}\n{recovery}"));
}

fn record_partial_issue_split(state: &mut IssueRun, number: i64, parts: &[(String, i64)]) {
    state.status = Status::Error;
    state.notes.push(format!(
        "Only one child issue survived, so #{number} was not rewritten into a tracker. Do not \
         rerun this split while the child is unrecorded. Link it from #{number} by hand and decide \
         which issue owns the work, or close it first if it was newly created and should not \
         remain:\n{}",
        checklist(parts)
    ));
}

fn record_uncertain_issue_part(
    state: &mut IssueRun,
    number: i64,
    title: &str,
    listed: &[(String, i64)],
    reason: &str,
) {
    state.status = Status::Error;
    let recovery = if listed.is_empty() {
        format!(
            "Inspect recent issues for an exact `{title}` child before doing anything else. If it \
             exists, add it to #{number} by hand. If it does not exist, this split can be run \
             again."
        )
    } else {
        format!(
            "These earlier child issues were filed:\n{}\nInspect recent issues for an exact \
             `{title}` child, then complete the tracker on #{number} by hand. Do not rerun this \
             split while these partial results exist.",
            checklist(listed)
        )
    };
    state.notes.push(format!(
        "{reason}\nWhether that child write landed is unknown. {recovery}"
    ));
}

// ---------------------------------------------------------------------------
// Splitting a pull request
// ---------------------------------------------------------------------------

pub fn split_pr(agents: &[Agent], cfg: &Config, repo: &Repo, number: i64, mode: &Mode) -> IssueRun {
    match pr_inner(agents, cfg, repo, number, mode) {
        Ok(state) => state,
        Err(e) => failed(number, format!("PR #{number}"), e),
    }
}

fn pr_inner(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    number: i64,
    mode: &Mode,
) -> Result<IssueRun> {
    let pr: PrView = repo.pr_view(number)?;
    if !pr.is_open() {
        bail!("PR #{number} is {}", pr.state.to_lowercase());
    }
    let mut state = IssueRun::new(number, pr.title.clone());
    state.pr = Some(pr.url.clone());

    if !mode.again {
        match prior_split(repo, number)? {
            PriorSplit::None => {}
            PriorSplit::Recorded => {
                log!("PR #{number} already has parts spar made. --again splits it again.");
                state.status = Status::Whole;
                state.notes.push("already split".into());
                return Ok(state);
            }
            PriorSplit::RetainedBranches => {
                log!("PR #{number} has retained branches from an incomplete split");
                state.status = Status::Error;
                state.notes.push(retained_branches_note(number));
                return Ok(state);
            }
        }
    }

    let base = if pr.base_ref_name.trim().is_empty() {
        cfg.base_branch().to_string()
    } else {
        pr.base_ref_name.clone()
    };
    repo.git_try(&["fetch", "origin", &base]);

    let head_ref = crate::repo::review_ref(number);
    let read_only = repo.worktree_for_pr_head(number)?;
    let checkpoint = repo.worktree_checkpoint(&read_only)?;
    let head_oid = repo
        .git_at(Some(&read_only), &["rev-parse", "HEAD"])?
        .trim()
        .to_string();
    if head_oid.is_empty() {
        bail!("could not read the fetched head of PR #{number}");
    }
    let outcome = split_pr_inner(
        agents,
        cfg,
        repo,
        &pr,
        &base,
        &head_ref,
        &head_oid,
        &read_only,
        &checkpoint,
        mode,
        &mut state,
    );
    outcome?;
    if !cfg.loop_cfg.keep_worktrees {
        repo.release_review_worktree_checked(number, &checkpoint)?;
    }
    Ok(state)
}

#[allow(clippy::too_many_arguments)]
fn split_pr_inner(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    pr: &PrView,
    base: &str,
    head_ref: &str,
    head_oid: &str,
    read_only: &Path,
    checkpoint: &WorktreeCheckpoint,
    mode: &Mode,
    state: &mut IssueRun,
) -> Result<()> {
    let number = pr.number;
    let changed = repo.changed_files(read_only, base);
    if changed.is_empty() {
        log!("PR #{number} changes no files, so there is nothing to split");
        state.status = Status::Whole;
        return Ok(());
    }

    let prompt = PROPOSE_PR_PROMPT
        .replace("{number}", &number.to_string())
        .replace("{title}", &pr.title)
        .replace("{base}", base)
        .replace("{count}", &changed.len().to_string())
        .replace("{files}", &listed(&changed));

    let mut decision = propose_and_check(
        agents,
        cfg,
        read_only,
        &format!("PR #{number}"),
        &prompt,
        &format!("pull request #{number}"),
    )?;
    repo.require_unchanged_worktree(
        read_only,
        checkpoint,
        &format!("review worktree for PR #{number}"),
    )?;
    for path in confine(&mut decision.parts, &changed) {
        logdim!("PR #{number}: a part named `{path}`, which this change does not touch");
    }

    for note in &decision.dropped {
        log!("  dropped {note}");
        state.notes.push(format!("dropped {note}"));
    }
    if !decision.splits() {
        let why = decision
            .declined
            .clone()
            .unwrap_or_else(|| "nothing survived the check".to_string());
        log!("PR #{number} left whole: {why}");
        state.status = Status::Whole;
        state.notes.push(why);
        return Ok(());
    }

    let proposed_left = proposed_leftover(&changed, &decision);
    if mode.dry_run {
        print_proposal(number, "pull request", &decision, &proposed_left);
        state.status = Status::Whole;
        state.notes.push(format!(
            "dry run: {} part(s) proposed",
            decision.parts.len()
        ));
        return Ok(());
    }

    // A fork is proposed, not split. spar cannot push to the fork, and carving
    // somebody's contribution into pull requests of your own without asking is
    // not something to do automatically. This is how `review` already treats a
    // fork.
    if pr.is_cross_repository {
        log!("PR #{number} comes from a fork, so the parts are proposed rather than made");
        let body = proposal_comment(number, &decision, &proposed_left, &repo.style);
        ensure_parent_head(repo, number, head_oid)?;
        repo.comment_pr(number, &body)?;
        state.status = Status::Whole;
        state
            .notes
            .push("from a fork, so the split was proposed rather than made".into());
        return Ok(());
    }

    let parent = Parent {
        number,
        base,
        head_ref,
        head_oid,
        head_branch: pr.head_ref_name.trim(),
    };
    let built = build_parts(agents, cfg, repo, &parent, &decision, state)?;
    let made_before_failure = built.made.len();
    if let Some(reason) = built.failure {
        state.status = Status::Error;
        state.notes.push(with_partial_pr_recovery(
            number,
            made_before_failure,
            &reason,
        ));
        return Ok(());
    }
    let made = built.made;
    if made.is_empty() {
        log!("PR #{number} left whole: no part would stand on its own");
        state.status = Status::Whole;
        release_part_worktrees(repo, cfg, state.status, built.worktrees);
        return Ok(());
    }

    // From the parts that exist, not the ones that were proposed. A part
    // dropped for carrying nothing, for failing, or for not standing on its own
    // leaves its files on the original, and this comment is the only permanent
    // record of where anything went.
    let left = leftover(&changed, made.iter().map(|m| m.files.as_slice()));
    let body = parts_comment(&made, &left, &repo.style);
    if let Err(e) = ensure_parent_head(repo, number, head_oid) {
        state.status = Status::Error;
        state.notes.push(format!(
            "{e}. The part pull requests were opened, but the parent was left uncommented."
        ));
        return Ok(());
    }
    if let Err(e) = repo.comment_pr(number, &body) {
        logwarn!(
            "made {} part(s) but could not say so on #{number}: {e}",
            made.len()
        );
        record_parent_comment_failure(state, number, &body, &e);
        return Ok(());
    }
    // A split into one part is not a split: everything that part carries is
    // still on the original, so the two would be reviewed twice over. It was
    // pushed before that was knowable and is named on the original for that
    // reason, but this run did not decompose anything.
    if made.len() < 2 {
        log!("PR #{number} left whole: only one part stood on its own");
        state.status = Status::Whole;
        state
            .notes
            .push("only one part stood on its own, so nothing was decomposed".into());
        release_part_worktrees(repo, cfg, state.status, built.worktrees);
        return Ok(());
    }
    state.status = Status::Split;
    release_part_worktrees(repo, cfg, state.status, built.worktrees);
    Ok(())
}

fn ensure_parent_head(repo: &Repo, number: i64, expected: &str) -> Result<()> {
    let checked = repo
        .pr_head_oid(number)
        .and_then(|live| same_parent_head(number, expected, &live));
    repo.record_failed_write(checked)
}

fn same_parent_head(number: i64, expected: &str, live: &str) -> Result<()> {
    if expected == live {
        return Ok(());
    }
    bail!(
        "PR #{number} changed from {expected} to {live} while it was being split; refusing to \
         write parts from an unread head"
    )
}

fn release_part_worktrees(
    repo: &Repo,
    cfg: &Config,
    status: Status,
    worktrees: Vec<(PathBuf, String)>,
) {
    release_part_worktrees_with(
        cfg.loop_cfg.keep_worktrees,
        status,
        worktrees,
        |dir, branch| repo.release_split_worktree(dir, branch),
    );
}

fn release_part_worktrees_with(
    configured: bool,
    status: Status,
    worktrees: Vec<(PathBuf, String)>,
    mut release: impl FnMut(&Path, &str),
) {
    if keep_part_worktrees(configured, status) {
        return;
    }
    for (dir, branch) in worktrees {
        release(&dir, &branch);
    }
}

fn keep_part_worktrees(configured: bool, status: Status) -> bool {
    configured || status == Status::Error
}

fn record_parent_comment_failure(state: &mut IssueRun, number: i64, body: &str, error: &SparError) {
    state.status = Status::Error;
    let recovery = if error.kind() == ErrorKind::UncertainWrite {
        format!(
            "The comment may already have landed. The part branches stop an automatic retry. \
             Inspect every top-level comment on #{number} for the exact body below. If it is \
             present, do not post it again. If it is absent, post it once by hand to finish \
             recording the split:\n{body}"
        )
    } else {
        format!(
            "The part branches stop an automatic retry. Post this comment by hand to finish \
             recording the split. A normal rerun stops while those branches exist:\n{body}"
        )
    };
    state.notes.push(format!(
        "could not comment on #{number}: {error}\n{recovery}"
    ));
}

/// What no proposed part claims. For the two paths that build nothing: a dry
/// run and a fork, where the proposal is all there is.
fn proposed_leftover(changed: &[String], decision: &Decision) -> Vec<String> {
    leftover(changed, decision.parts.iter().map(|p| p.files.as_slice()))
}

/// One part as it was actually built: its pull request, and the files its
/// branch really changed.
struct Built {
    pr: crate::model::PrRef,
    files: Vec<String>,
}

enum BuildOne {
    Made(Built),
    Declined {
        disposable_head: String,
    },
    Halted {
        reason: String,
        /// An exact mechanical-slice tip that may be discarded. `None` means
        /// the worktree contains or may contain recovery work and must stay.
        disposable_head: Option<String>,
    },
}

impl BuildOne {
    fn parent_moved(
        error: &crate::error::SparError,
        dir: &Path,
        disposable_head: Option<String>,
    ) -> Self {
        let reason = if disposable_head.is_none() {
            format!(
                "{error}. The stand-alone worktree was not confirmed to match its mechanical \
                 slice, so it was kept at {} for recovery.",
                dir.display()
            )
        } else {
            error.to_string()
        };
        Self::Halted {
            reason,
            disposable_head,
        }
    }

    fn push_failed(
        branch: &str,
        error: &SplitPushError,
        mut disposable_head: Option<String>,
    ) -> Self {
        let remote_uncertain = error.retain_worktree();
        if remote_uncertain {
            disposable_head = None;
        }
        let reason = if remote_uncertain {
            format!(
                "{error}\nThe split stopped because `{branch}` may now exist on origin. Its local \
                 worktree and branch record were kept. Inspect the exact remote ref before \
                 continuing."
            )
        } else if disposable_head.is_none() {
            format!(
                "could not create the new branch `{branch}`: {error}\nThe stand-alone worktree was \
                 not confirmed to match its mechanical slice, so its worktree and branch record \
                 were kept for recovery."
            )
        } else {
            format!(
                "could not create the new branch `{branch}`: {error}\nAnother writer may have \
                 taken the name, so the split stopped before opening competing pull requests."
            )
        };
        Self::Halted {
            reason,
            disposable_head,
        }
    }
}

struct BuiltParts {
    made: Vec<Made>,
    worktrees: Vec<(PathBuf, String)>,
    failure: Option<String>,
}

fn worktree_allocation_failure(
    parent: i64,
    index: usize,
    made: usize,
    error: &crate::error::SparError,
) -> String {
    let reason = format!("could not allocate part {index}: {}", error.last_line());
    with_partial_pr_recovery(parent, made, &reason)
}

fn with_partial_pr_recovery(parent: i64, made: usize, reason: &str) -> String {
    const LEAD: &str = "Earlier child pull requests and their worktrees were kept.";
    if made == 0 || reason.contains(LEAD) {
        return reason.to_string();
    }
    format!(
        "{reason}\n{LEAD} {made} child pull request(s) already exist. Compare them with the \
         current parent and record the partial result on #{parent} by hand. To start over, remove \
         every retained local worktree and branch, child pull request, and remote split branch \
         first."
    )
}

/// One part that exists: its number, its title, its pull request, and the files
/// it took with it.
struct Made {
    index: usize,
    title: String,
    url: String,
    files: Vec<String>,
}

/// The pull request being split, as the part builder needs it.
struct Parent<'a> {
    number: i64,
    /// What an independent part is branched from and opened against.
    base: &'a str,
    /// The fetched head, which the slices are taken out of.
    head_ref: &'a str,
    /// The exact pull request head the proposal and check read.
    head_oid: &'a str,
    /// The branch behind it, which nothing here may ever write to.
    head_branch: &'a str,
}

fn build_parts(
    agents: &[Agent],
    cfg: &Config,
    repo: &Repo,
    parent: &Parent<'_>,
    decision: &Decision,
    state: &mut IssueRun,
) -> Result<BuiltParts> {
    let implementor = agent::find(agents, &cfg.first_implementor)?;
    let total = decision.parts.len();
    let mut made: Vec<Made> = Vec::new();
    let mut worktrees: Vec<(PathBuf, String)> = Vec::new();

    // What the next part is branched from, and what its pull request is opened
    // against. Independent parts both stay on the base; stacked parts move to
    // each part that was actually made, so a dropped part does not strand the
    // ones after it.
    let mut start = format!("origin/{}", parent.base);
    let mut against = parent.base.to_string();

    for (i, part) in decision.parts.iter().enumerate() {
        let index = i + 1;
        if part.files.is_empty() {
            log!("  part {index} carries no files, dropping it");
            state
                .notes
                .push(format!("dropped {}: it carried no files", label(part)));
            continue;
        }
        if let Err(e) = ensure_parent_head(repo, parent.number, parent.head_oid) {
            return Ok(BuiltParts {
                made,
                worktrees,
                failure: Some(e.to_string()),
            });
        }

        let (dir, branch) = match repo.worktree_for_split(parent.number, index, &start) {
            Ok(worktree) => worktree,
            Err(e) => {
                return Ok(BuiltParts {
                    failure: Some(worktree_allocation_failure(
                        parent.number,
                        index,
                        made.len(),
                        &e,
                    )),
                    made,
                    worktrees,
                });
            }
        };
        let outcome = build_one(
            repo,
            implementor,
            cfg,
            parent,
            index,
            total,
            part,
            &dir,
            &branch,
            &against,
        );
        match outcome {
            Ok(BuildOne::Made(built)) => {
                log!("  part {index}: {}", built.pr.url);
                state.filed.push(built.pr.url.clone());
                // The proposal's list only as a fallback. The branch was pushed
                // with a commit on it, so an empty answer means the diff could
                // not be read, and taking it would report every file the part
                // carries as still being only on the original.
                let files = if built.files.is_empty() {
                    part.files.clone()
                } else {
                    built.files
                };
                for path in overlapping(&made, &files, decision.stacked) {
                    // Not an error: the part is pushed and its pull request is
                    // open. But two parts carrying one path is the thing a
                    // split exists to avoid, so it cannot pass unsaid.
                    logwarn!("  part {index} also changed `{path}`, which an earlier part carries");
                    state.notes.push(format!(
                        "part {index} and an earlier part both carry {path}"
                    ));
                }
                made.push(Made {
                    index,
                    title: part.title.clone(),
                    url: built.pr.url,
                    files,
                });
                worktrees.push((dir, branch.clone()));
                if decision.stacked {
                    start = branch.clone();
                    against = branch;
                }
            }
            Ok(BuildOne::Declined { disposable_head }) => {
                if !repo.discard_split_worktree(&dir, &branch, &disposable_head) {
                    let reason = format!(
                        "part {index} was declined, but its exact mechanical slice could not be \
                         discarded safely. The worktree and branch were kept at {} for recovery.",
                        dir.display()
                    );
                    worktrees.push((dir, branch));
                    return Ok(BuiltParts {
                        made,
                        worktrees,
                        failure: Some(reason),
                    });
                }
            }
            Ok(BuildOne::Halted {
                mut reason,
                disposable_head,
            }) => {
                match disposable_head {
                    Some(head) => {
                        if !repo.discard_split_worktree(&dir, &branch, &head) {
                            reason.push_str(&format!(
                                "\nThe exact mechanical slice could not be discarded safely. Its \
                                 worktree and branch were kept at {} for recovery.",
                                dir.display()
                            ));
                            worktrees.push((dir, branch));
                        }
                    }
                    None => worktrees.push((dir, branch)),
                }
                return Ok(BuiltParts {
                    made,
                    worktrees,
                    failure: Some(reason),
                });
            }
            Err(e) => {
                let reason = format!(
                    "part {index} could not be completed: {e}. Its worktree and branch were kept \
                     at {} for recovery.",
                    dir.display()
                );
                logwarn!("  {reason}");
                state.notes.push(format!("halted {}: {e}", label(part)));
                worktrees.push((dir, branch));
                return Ok(BuiltParts {
                    made,
                    worktrees,
                    failure: Some(reason),
                });
            }
        }
    }
    Ok(BuiltParts {
        made,
        worktrees,
        failure: None,
    })
}

/// The paths this part changed that a part already made carries too.
///
/// `confine` keeps the proposal's parts disjoint, but the stand-alone pass
/// commits whatever the part needed to build and nothing tells it what the
/// other parts own, so this is only answerable once a part exists.
///
/// Nothing for a stacked split, where each part is diffed against the one
/// before it: changing a file an earlier part changed is what a stack is, and
/// the parts land in that order.
fn overlapping(made: &[Made], files: &[String], stacked: bool) -> Vec<String> {
    if stacked {
        return Vec::new();
    }
    let taken: BTreeSet<&str> = made
        .iter()
        .flat_map(|m| m.files.iter())
        .map(String::as_str)
        .collect();
    files
        .iter()
        .filter(|path| taken.contains(path.as_str()))
        .cloned()
        .collect()
}

/// Build one part on its own branch, or say it will not stand.
///
/// Returns None for a part the agent declined, which is dropped rather than
/// pushed: a part that does not build has none of the value of splitting.
#[allow(clippy::too_many_arguments)]
fn build_one(
    repo: &Repo,
    implementor: &Agent,
    cfg: &Config,
    parent: &Parent<'_>,
    index: usize,
    total: usize,
    part: &SplitPart,
    dir: &Path,
    branch: &str,
    against: &str,
) -> Result<BuildOne> {
    let number = parent.number;
    log!(
        "PR #{number}: building part {index} of {total} on {branch} ({} file(s))",
        part.files.len()
    );
    if !apply_slice(repo, dir, parent.base, parent.head_ref, &part.files)? {
        bail!("applying its files changed nothing");
    }
    let raw_subject = format!("{} (part {index} of #{number})", part.title.trim());
    let mut subject = repo.clean_title(&raw_subject)?;
    if subject.trim().is_empty() {
        subject = format!("Make part {index} of #{number}");
    }
    repo.commit_staged_changes(dir, &subject)
        .map_err(|e| e.with_message(format!("could not commit the slice: {}", e.last_line())))?;
    let slice_head = repo.head_oid_checked(dir)?;

    let prompt = STAND_ALONE_PROMPT
        .replace("{parent}", &number.to_string())
        .replace("{base}", against)
        .replace("{index}", &index.to_string())
        .replace("{total}", &total.to_string())
        .replace("{title}", part.title.trim())
        .replace("{body}", part.body.trim())
        .replace("{files}", &listed(&part.files));
    let worktree_baseline = repo.worktree_baseline(dir)?;
    let work: Implementation = match implementor.edit_json(
        &prompt,
        &schema::implementation(),
        dir,
        cfg.effort_for_round(&implementor.spec, 1).as_deref(),
    ) {
        Ok(work) => work,
        Err(e) if e.kind() == ErrorKind::UncertainWrite => {
            return Ok(BuildOne::Halted {
                reason: format!(
                    "{e}. Git state could not be restored safely, so the worktree was kept at {} \
                     for recovery.",
                    dir.display()
                ),
                disposable_head: None,
            });
        }
        Err(e) => {
            if let Err(recovery) =
                repo.refuse_unrepresented_tracked_changes(dir, &worktree_baseline)
            {
                return Ok(BuildOne::Halted {
                    reason: format!(
                        "{e}. The editing call also changed tracked working-file bytes or modes: \
                         {recovery}. The worktree was kept at {} for recovery.",
                        dir.display()
                    ),
                    disposable_head: None,
                });
            }
            if let Err(ignored) = repo.refuse_new_ignored_files(dir, &worktree_baseline) {
                return Ok(BuildOne::Halted {
                    reason: format!(
                        "{e}. The editing call also left ignored work or its ignored files could \
                         not be checked: {ignored}. The worktree was kept at {} for recovery.",
                        dir.display()
                    ),
                    disposable_head: None,
                });
            }
            return failed_part_edit(repo, dir, &slice_head, e);
        }
    };
    if let Err(e) = repo.refuse_changed_attributes(dir, &worktree_baseline) {
        return Ok(BuildOne::Halted {
            reason: format!(
                "the stand-alone edit changed an attribute file: {e}. The worktree was kept at \
                 {} for recovery.",
                dir.display()
            ),
            disposable_head: None,
        });
    }
    if work.not_worth_doing {
        if let Err(e) = repo.refuse_changed_existing_untracked(dir, &worktree_baseline) {
            return Ok(BuildOne::Halted {
                reason: format!(
                    "part {index} was declined after changing existing untracked work: {e}. The \
                     worktree was kept at {} for recovery.",
                    dir.display()
                ),
                disposable_head: None,
            });
        }
        if let Err(e) = repo.refuse_unrepresented_tracked_changes(dir, &worktree_baseline) {
            return Ok(BuildOne::Halted {
                reason: format!(
                    "part {index} was declined after changing tracked working-file bytes or modes: \
                     {e}. The worktree was kept at {} for recovery.",
                    dir.display()
                ),
                disposable_head: None,
            });
        }
        match work_since_slice(repo, dir, &slice_head) {
            Ok(true) => {
                return Ok(BuildOne::Halted {
                    reason: format!(
                        "part {index} was declined after its worktree changed. The worktree was \
                         kept at {} for recovery.",
                        dir.display()
                    ),
                    disposable_head: None,
                });
            }
            Err(e) => {
                return Ok(BuildOne::Halted {
                    reason: format!(
                        "part {index} was declined, but its worktree state could not be verified: \
                         {e}. The worktree was kept at {} for recovery.",
                        dir.display()
                    ),
                    disposable_head: None,
                });
            }
            Ok(false) => {
                if let Err(e) = repo.refuse_new_ignored_files(dir, &worktree_baseline) {
                    return Ok(BuildOne::Halted {
                        reason: format!(
                            "part {index} created ignored work that could not be included: {e}. \
                             The worktree was kept at {} for recovery.",
                            dir.display()
                        ),
                        disposable_head: None,
                    });
                }
            }
        }
        let reason = style::sentence(&work.reason, &repo.style);
        log!(
            "  part {index} will not stand on its own, dropping it: {}",
            if reason.is_empty() {
                "no reason given"
            } else {
                &reason
            }
        );
        return Ok(BuildOne::Declined {
            disposable_head: slice_head,
        });
    }

    let committed = repo
        .commit_pending_changes(
            dir,
            &worktree_baseline,
            &work.summary,
            &format!("Make part {index} of #{number} stand alone"),
        )
        .and_then(|committed| {
            repo.refuse_unrepresented_tracked_changes(dir, &worktree_baseline)?;
            Ok(committed)
        });
    if let Err(e) = committed {
        return failed_part_edit(repo, dir, &slice_head, e);
    }
    let stand_alone_work = match work_since_slice(repo, dir, &slice_head) {
        Ok(false) => {
            if let Err(e) = repo.refuse_new_ignored_files(dir, &worktree_baseline) {
                return Ok(BuildOne::Halted {
                    reason: format!(
                        "the stand-alone edit created ignored work that could not be included: \
                         {e}. The worktree was kept at {} for recovery.",
                        dir.display()
                    ),
                    disposable_head: None,
                });
            }
            false
        }
        Ok(true) => true,
        Err(e) => {
            return Ok(BuildOne::Halted {
                reason: format!(
                    "the stand-alone worktree could not be verified after editing: {e}. It was \
                     kept at {} for recovery.",
                    dir.display()
                ),
                disposable_head: None,
            });
        }
    };

    // Every failure from here to a confirmed push is classified against the
    // mechanical slice. Stand-alone work is kept, while a clean slice with no
    // additional edit preserves the old drop behavior.
    if let Err(e) = additive(branch, parent.head_branch, &repo.branch_prefix) {
        return failed_part_edit(repo, dir, &slice_head, e);
    }
    if let Err(e) = repo.rewrite_commits_if_needed(dir, against) {
        return failed_part_edit(repo, dir, &slice_head, e);
    }
    let disposable_head = if stand_alone_work {
        None
    } else if repo.head_oid_checked(dir)? == slice_head {
        Some(slice_head.clone())
    } else {
        None
    };
    if let Err(e) = ensure_parent_head(repo, number, parent.head_oid) {
        return Ok(BuildOne::parent_moved(&e, dir, disposable_head));
    }
    if let Err(e) = repo.push_split_branch(dir, branch) {
        return Ok(BuildOne::push_failed(branch, &e, disposable_head));
    }

    if let Err(e) = ensure_parent_head(repo, number, parent.head_oid) {
        return Ok(BuildOne::Halted {
            reason: format!(
                "{e}. Branch `{branch}` was pushed and its worktree was kept. Compare it with \
                 the new parent head. If it is still valid, open its pull request by hand and \
                 record it on #{number}. To start over, remove every retained local worktree and \
                 branch, child pull request, and remote split branch first."
            ),
            disposable_head: None,
        });
    }

    let title = format!("{} (part {index} of #{number})", part.title.trim());
    let body = part_body(number, index, total, part, &work, &repo.style);
    // The branch, not the proposal. The stand-alone pass commits whatever the
    // part needed to build, so what it carries is only knowable here, and this
    // list is what the comment on the original reports as taken.
    let files = repo.changed_files(dir, against);
    match repo.create_pr(dir, branch, against, &title, &body) {
        Ok(pr) => Ok(BuildOne::Made(Built { pr, files })),
        Err(e) => Ok(BuildOne::Halted {
            reason: format!(
                "branch `{branch}` was pushed, but its pull request could not be opened: {e}. Its \
                 worktree and branch record were kept. Compare it with the current parent. If it \
                 is still valid, open `{branch}` against `{against}` by hand with title `{title}`, \
                 then record the new pull request on #{number}. To start over, remove every \
                 retained local worktree and branch, child pull request, and remote split branch \
                 first."
            ),
            disposable_head: None,
        }),
    }
}

fn failed_part_edit(
    repo: &Repo,
    dir: &Path,
    slice_head: &str,
    error: crate::error::SparError,
) -> Result<BuildOne> {
    if error.kind() == crate::error::ErrorKind::UncertainWrite {
        return Ok(BuildOne::Halted {
            reason: format!(
                "{error}. Git state could not be restored safely, so the worktree was kept at {} \
                 for recovery.",
                dir.display()
            ),
            disposable_head: None,
        });
    }
    match work_since_slice(repo, dir, slice_head) {
        Ok(false) => Ok(BuildOne::Halted {
            reason: error.to_string(),
            disposable_head: Some(slice_head.to_string()),
        }),
        Ok(true) => Ok(BuildOne::Halted {
            reason: format!(
                "{error}. The editing worktree was kept at {} for recovery.",
                dir.display()
            ),
            disposable_head: None,
        }),
        Err(probe) => Ok(BuildOne::Halted {
            reason: format!(
                "{error}. The editing worktree could not be verified: {probe}. It was kept at {} \
                 for recovery.",
                dir.display()
            ),
            disposable_head: None,
        }),
    }
}

fn work_since_slice(repo: &Repo, dir: &Path, slice_head: &str) -> Result<bool> {
    if repo.has_uncommitted_changes(dir)? {
        return Ok(true);
    }
    Ok(repo.head_oid_checked(dir)? != slice_head)
}

/// Whether anything in this tree is missing from the commit, untracked files
/// included.
///
/// `review::snapshot` asks only about tracked files, and the shape that matters
/// here is a file the stand-alone pass wrote and never added: a new module a
/// part needs to build is exactly that, and it would be pushed missing.
///
/// Public so that it can be tested against a real repository.
pub fn uncommitted(repo: &Repo, dir: &Path) -> bool {
    repo.has_uncommitted_changes(dir).unwrap_or(true)
}

/// Put one slice of the parent's change on this branch.
///
/// The parent's own diff for those paths, applied, rather than its versions of
/// those files copied over. The branch starts from the base as it is now, and
/// copying would revert whatever the base did to the same file after the pull
/// request was opened, which is a deletion nobody asked for in a change
/// advertised as one part of somebody else's.
///
/// Reports whether anything is actually staged, because a slice that changed
/// nothing is a part with no content.
///
/// Public so that it can be tested against a real repository, which is where it
/// goes wrong.
pub fn apply_slice(
    repo: &Repo,
    dir: &Path,
    base: &str,
    head_ref: &str,
    files: &[String],
) -> Result<bool> {
    let from = repo.merge_base(dir, base, head_ref)?;
    let patch = patch_path(dir);
    let written = write_patch(repo, dir, &from, head_ref, files, &patch);
    let outcome = written.and_then(|carries| {
        if !carries {
            return Ok(false);
        }
        // Three way so the slice still lands when the base has moved under the
        // hunks. A conflict is an error rather than markers left in a commit:
        // the part is dropped and said out loud, which is what every other way
        // a part fails to stand on its own already does.
        repo.git_at(
            Some(dir),
            &["apply", "--index", "--3way", &patch.display().to_string()],
        )
        .map_err(|e| spar_err!("could not apply its files onto {base}: {}", e.last_line()))?;
        Ok(!repo
            .git_try_at(Some(dir), &["status", "--porcelain"])
            .trim()
            .is_empty())
    });
    let _ = std::fs::remove_file(&patch);
    outcome
}

/// The parent's diff for these paths alone, written to `patch`. False when it
/// is empty.
fn write_patch(
    repo: &Repo,
    dir: &Path,
    from: &str,
    head_ref: &str,
    files: &[String],
    patch: &Path,
) -> Result<bool> {
    let mut args: Vec<String> = vec![
        "diff".into(),
        // `--binary` so a change to an image or a fixture survives the round
        // trip, `--no-renames` to match `changed_files`: a part carries paths,
        // and a rename it holds only one end of has to stay a deletion and an
        // addition rather than become a patch git cannot apply.
        "--binary".into(),
        "--no-renames".into(),
        // Straight to a file rather than through stdout, since a diff of a
        // file that is not valid UTF-8 does not survive being read as a string.
        format!("--output={}", patch.display()),
        from.to_string(),
        head_ref.to_string(),
        "--".into(),
    ];
    // `:(literal)` because `--` ends the options and does nothing to pathspec
    // matching: a file actually named `*.txt` would otherwise pull in every
    // other `.txt` the change touches, including ones another part carries.
    args.extend(files.iter().map(|path| format!(":(literal){path}")));

    let argv: Vec<&str> = args.iter().map(String::as_str).collect();
    repo.git_at(Some(dir), &argv).map_err(|e| {
        spar_err!(
            "could not read its files out of the head: {}",
            e.last_line()
        )
    })?;
    Ok(std::fs::metadata(patch)
        .map(|m| m.len() > 0)
        .unwrap_or(false))
}

/// Beside the worktree rather than in it, so the patch is never something the
/// slice could commit and never something `--output` writes into the tree it
/// describes. The worktree directory is already spar's and already excluded
/// from git.
fn patch_path(dir: &Path) -> std::path::PathBuf {
    let name = dir
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_else(|| "part".to_string());
    let holder = dir.parent().unwrap_or_else(|| Path::new("."));
    holder.join(format!("{name}.patch"))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PriorSplit {
    None,
    Recorded,
    RetainedBranches,
}

/// Whether this pull request has a complete split or retained work from one.
///
/// An error rather than `None` when either source cannot be read. These checks
/// are the only thing standing between a rerun and a second set of branches and
/// pull requests, and a failed read is not evidence that the first set is gone.
fn prior_split(repo: &Repo, number: i64) -> Result<PriorSplit> {
    let comments = repo.try_issue_comments(number).map_err(|e| {
        spar_err!(
            "could not read the comments on #{number}, so whether it has already been split is \
             unknown: {}",
            e.last_line()
        )
    })?;
    if comments.iter().any(|c| {
        c.get("body")
            .and_then(serde_json::Value::as_str)
            .is_some_and(already_split)
    }) {
        return Ok(PriorSplit::Recorded);
    }
    let retained = repo.has_remote_split_branch(number).map_err(|e| {
        spar_err!(
            "could not check whether branches for #{number} already exist, so whether it has \
             already been split is unknown: {}",
            e.last_line()
        )
    })?;
    Ok(if retained {
        PriorSplit::RetainedBranches
    } else {
        PriorSplit::None
    })
}

fn retained_branches_note(number: i64) -> String {
    format!(
        "remote split branches for PR #{number} show that an earlier split did not finish \
         cleanly. Inspect the retained branches and worktrees and compare each branch with the \
         current parent. Open any missing child pull request only when its branch is still valid, \
         then post the parts summary on #{number} by hand. To start over, remove every retained \
         local worktree and branch, child pull request, and remote split branch first. --again \
         starts a separate split and does not resume these branches."
    )
}

// ---------------------------------------------------------------------------
// Asking the pair
// ---------------------------------------------------------------------------

/// One agent proposes with the code open, the other rules on the proposal.
fn propose_and_check(
    agents: &[Agent],
    cfg: &Config,
    work_dir: &Path,
    label: &str,
    propose_prompt: &str,
    what: &str,
) -> Result<Decision> {
    let proposer_name = cfg.first_implementor.clone();
    let proposer = agent::find(agents, &proposer_name)?;
    let checker_name = cfg.other(&proposer_name);
    let checker = agent::find(agents, &checker_name)?;

    log!("{label}: {proposer_name} proposing a split");
    let proposal: SplitProposal = proposer.ask_json(
        propose_prompt,
        &schema::split_proposal(),
        work_dir,
        cfg.effort_for_round(&proposer.spec, 1).as_deref(),
    )?;
    if !proposal.should_split {
        return Ok(decide(
            &proposal,
            &SplitCheck::default(),
            cfg.loop_cfg.max_split_parts,
        ));
    }

    log!(
        "{label}: {checker_name} checking {} proposed part(s)",
        proposal.parts.len()
    );
    let check: SplitCheck = checker.ask_json(
        &CHECK_PROMPT
            .replace("{what}", what)
            .replace("{reason}", proposal.reason.trim())
            .replace(
                "{shape}",
                if proposal.stacked {
                    "stacked, each needing the one before it"
                } else {
                    "independent of each other"
                },
            )
            .replace("{parts}", &render_parts(&proposal.parts)),
        &schema::split_check(),
        work_dir,
        cfg.effort_for_round(&checker.spec, 2).as_deref(),
    )?;

    Ok(decide(&proposal, &check, cfg.loop_cfg.max_split_parts))
}

fn render_parts(parts: &[SplitPart]) -> String {
    parts
        .iter()
        .enumerate()
        .map(|(i, p)| {
            let files = if p.files.is_empty() {
                String::new()
            } else {
                format!("\n   files: {}", p.files.join(", "))
            };
            format!("{}. {}\n   {}{files}", i + 1, p.title.trim(), p.body.trim())
        })
        .collect::<Vec<_>>()
        .join("\n\n")
}

// ---------------------------------------------------------------------------
// What a person reads
// ---------------------------------------------------------------------------

fn listed(paths: &[String]) -> String {
    paths
        .iter()
        .map(|p| format!("- {p}"))
        .collect::<Vec<_>>()
        .join("\n")
}

fn bullets(lines: &[String]) -> String {
    lines
        .iter()
        .map(|line| format!("- {line}"))
        .collect::<Vec<_>>()
        .join("\n")
}

/// The body of one part's pull request.
///
/// It says which pull request it came out of, because that is the parent record:
/// parts get no issues of their own, and the comment on the original is the
/// list of them.
fn part_body(
    parent: i64,
    index: usize,
    total: usize,
    part: &SplitPart,
    work: &Implementation,
    style: &Style,
) -> String {
    let mut out = vec![format!("Part {index} of {total}, split out of #{parent}.")];
    for lead in [&work.summary, &work.problem] {
        let text = style::sentence(lead, style);
        if !text.is_empty() {
            out.push(text);
        }
    }
    if out.len() == 1 {
        // Nothing was said about the change, so say what the part is for.
        let text = style::sentence(&part.body, style);
        if !text.is_empty() {
            out.push(text);
        }
    }
    for (heading, lines) in [
        ("What changed", &work.changes),
        ("How to test", &work.testing),
    ] {
        let items: Vec<String> = lines
            .iter()
            .map(|line| style::summary(line, style))
            .filter(|line| !line.is_empty())
            .collect();
        if !items.is_empty() {
            out.push(format!("## {heading}\n\n{}", bullets(&items)));
        }
    }
    let notes = style::sentence(work.notes.as_deref().unwrap_or_default(), style);
    if !notes.is_empty() {
        out.push(format!("## Notes\n\n{notes}"));
    }
    style::body(&out.join("\n\n"), style)
}

/// The comment left on a pull request that has been split.
///
/// It says what was made and what was left over, and it says that the pull
/// request itself was not touched, because that is the thing somebody opening
/// this wants to know first.
///
/// One part is a separate sentence rather than a count of one. Every other part
/// was dropped, so nothing was decomposed and the one that exists duplicates a
/// piece of this pull request. Somebody has to be told that plainly, because
/// the only thing to do with it is close it or close this.
fn parts_comment(made: &[Made], left: &[String], style: &Style) -> String {
    let listed: Vec<String> = made
        .iter()
        .map(|m| format!("part {}: {} {}", m.index, m.url, m.title.trim()))
        .collect();
    let lead = if made.len() < 2 {
        "Only one part of this stood on its own, so it has not been split. That part was already \
         opened as its own pull request, and it carries files this one still carries:"
            .to_string()
    } else {
        format!("Split into {} pull request(s):", made.len())
    };
    let mut out = vec![SPLIT_MARKER.to_string(), lead, bullets(&listed)];
    if !left.is_empty() {
        out.push(format!(
            "{} file(s) are in no part, and are still only here:\n{}",
            left.len(),
            bullets(left)
        ));
    }
    out.push(
        "This pull request has not been changed. Its branch, its commits, and its own review are \
         untouched, and it is still open."
            .to_string(),
    );
    style::body(&out.join("\n\n"), style)
}

/// The comment left on a fork's pull request, which is proposed and not split.
fn proposal_comment(number: i64, decision: &Decision, left: &[String], style: &Style) -> String {
    let listed: Vec<String> = decision
        .parts
        .iter()
        .map(|p| {
            if p.files.is_empty() {
                p.title.trim().to_string()
            } else {
                format!("{} ({})", p.title.trim(), p.files.join(", "))
            }
        })
        .collect();
    let mut out = vec![
        SPLIT_MARKER.to_string(),
        format!(
            "#{number} comes from a fork, so this is a proposal rather than a change. Two agents \
             read it and agreed it would review better as {} pieces:",
            decision.parts.len()
        ),
        bullets(&listed),
    ];
    if decision.stacked {
        out.push("They have to land in that order.".to_string());
    }
    if !left.is_empty() {
        out.push(format!(
            "{} file(s) are in none of them:\n{}",
            left.len(),
            bullets(left)
        ));
    }
    out.push("Nothing has been changed here.".to_string());
    style::body(&out.join("\n\n"), style)
}

/// What `--dry-run` prints. `println!`, not `log!`: it is the whole output of
/// the command in that mode.
fn print_proposal(number: i64, kind: &str, decision: &Decision, left: &[String]) {
    println!(
        "\n{kind} #{number} would be split into {} part(s):",
        decision.parts.len()
    );
    for (i, part) in decision.parts.iter().enumerate() {
        println!("  {}. {}", i + 1, style::clip(part.title.trim(), 90));
        if !part.files.is_empty() {
            println!("     {}", part.files.join(", "));
        }
    }
    if decision.stacked {
        println!("  each part is based on the one before it");
    }
    if !left.is_empty() {
        println!("  left over: {}", left.join(", "));
    }
    for note in &decision.dropped {
        println!("  dropped {note}");
    }
    println!("Nothing was written.");
}

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

    fn part(title: &str, files: &[&str]) -> SplitPart {
        SplitPart {
            title: title.into(),
            body: format!("what {title} is"),
            files: files.iter().map(|f| f.to_string()).collect(),
        }
    }

    fn proposal(parts: Vec<SplitPart>) -> SplitProposal {
        SplitProposal {
            should_split: true,
            reason: "three things".into(),
            stacked: false,
            parts,
        }
    }

    fn accept() -> SplitCheck {
        SplitCheck {
            accept: true,
            stacked: false,
            strike: vec![],
            reasoning: "read it".into(),
        }
    }

    /// The multiplication guard. One command that turns twenty open items into
    /// sixty is a queue nobody asked for, and this codebase has been burned by
    /// exactly that once already.
    #[test]
    fn the_cap_holds_and_says_what_it_held_back() {
        let parts = vec![
            part("one", &[]),
            part("two", &[]),
            part("three", &[]),
            part("four", &[]),
        ];
        let out = decide(&proposal(parts), &accept(), 2);
        assert_eq!(2, out.parts.len());
        assert!(out.splits());
        assert_eq!(2, out.dropped.len(), "{:?}", out.dropped);
        assert!(out.dropped[0].contains("three"), "{:?}", out.dropped);
        assert!(out.dropped[0].contains("cap"), "{:?}", out.dropped);
    }

    /// The proposing agent declining is the cheapest way out, and it must not
    /// cost a second call or produce parts.
    #[test]
    fn a_proposal_that_declines_splits_nothing() {
        let mut p = proposal(vec![part("one", &[]), part("two", &[])]);
        p.should_split = false;
        p.reason = "it is one change".into();
        let out = decide(&p, &accept(), 4);
        assert!(!out.splits());
        assert_eq!(Some("it is one change".to_string()), out.declined);
        assert!(out.parts.is_empty());
    }

    /// Disagreement resolves toward not splitting.
    #[test]
    fn a_rejected_proposal_splits_nothing() {
        let check = SplitCheck {
            accept: false,
            reasoning: "these are the same change".into(),
            ..accept()
        };
        let out = decide(
            &proposal(vec![part("one", &[]), part("two", &[])]),
            &check,
            4,
        );
        assert!(!out.splits());
        assert!(out.declined.unwrap().contains("same change"));
    }

    #[test]
    fn a_struck_part_is_not_made() {
        let parts = vec![part("one", &[]), part("two", &[]), part("three", &[])];
        let check = SplitCheck {
            strike: vec![2],
            ..accept()
        };
        let out = decide(&proposal(parts), &check, 4);
        assert_eq!(2, out.parts.len());
        assert_eq!(vec!["one", "three"], titles(&out));
        assert!(out.dropped[0].contains("two"), "{:?}", out.dropped);
    }

    /// A split into one part is not a split, so striking enough parts is a way
    /// of saying no.
    #[test]
    fn striking_all_but_one_part_leaves_it_whole() {
        let parts = vec![part("one", &[]), part("two", &[])];
        let check = SplitCheck {
            strike: vec![1],
            reasoning: "only one of these stands alone".into(),
            ..accept()
        };
        let out = decide(&proposal(parts), &check, 4);
        assert!(!out.splits());
        assert!(out.parts.is_empty());
        assert!(out.declined.unwrap().contains("stands alone"));
    }

    /// Whether the parts are stacked is a property of the change, and being
    /// wrong about it is not symmetric: parts built independently out of a
    /// sequential change do not build, and a part that does not build is
    /// dropped.
    #[test]
    fn either_agent_calling_it_stacked_makes_it_stacked() {
        let mut p = proposal(vec![part("one", &[]), part("two", &[])]);
        assert!(!decide(&p, &accept(), 4).stacked);

        p.stacked = true;
        assert!(decide(&p, &accept(), 4).stacked);

        p.stacked = false;
        let check = SplitCheck {
            stacked: true,
            ..accept()
        };
        assert!(decide(&p, &check, 4).stacked);
    }

    fn titles(d: &Decision) -> Vec<String> {
        d.parts.iter().map(|p| p.title.clone()).collect()
    }

    /// The whole text somebody wrote comes through, and the checklist is added
    /// after it. This is the one place spar rewrites prose it did not write.
    #[test]
    fn a_tracker_body_keeps_every_byte_of_the_original() {
        let original =
            "The retry loop spins.\n\n```rust\nfn go() {}\n```\n\n## Impact\n\nBad.  \n\n";
        let out = tracker_body(original, &[("First".into(), 101), ("Second".into(), 102)]);
        assert!(out.starts_with(original), "{out}");
        assert_eq!(original.as_bytes(), &out.as_bytes()[..original.len()]);
        assert!(out.contains("- [ ] #101 First"), "{out}");
        assert!(out.contains("- [ ] #102 Second"), "{out}");
    }

    #[test]
    fn a_parent_head_must_still_be_the_one_the_agents_read() {
        assert!(same_parent_head(34, "abc123", "abc123").is_ok());
        let error = same_parent_head(34, "abc123", "def456").unwrap_err();
        assert!(error.to_string().contains("unread head"), "{error}");
    }

    #[test]
    fn filed_child_issues_require_manual_tracker_recovery() {
        let mut state = IssueRun::new(34, "split this");
        let parts = vec![("first".to_string(), 101), ("second".to_string(), 102)];
        let error = SparError::new("parent changed");
        record_issue_tracker_failure(&mut state, 34, &parts, &error);

        assert_eq!(Status::Error, state.status);
        let note = state.notes.join("\n");
        assert!(note.contains("Do not rerun this split"), "{note}");
        assert!(note.contains("Add them to #34 by hand"), "{note}");
        assert!(note.contains("#101 first"), "{note}");
        assert!(note.contains("#102 second"), "{note}");
    }

    #[test]
    fn an_uncertain_tracker_write_requires_inspection_before_editing() {
        let mut state = IssueRun::new(34, "split this");
        let parts = vec![("first".to_string(), 101), ("second".to_string(), 102)];
        let error = SparError::uncertain_write("the parent could not be reread");
        record_issue_tracker_failure(&mut state, 34, &parts, &error);

        assert_eq!(Status::Error, state.status);
        let note = state.notes.join("\n");
        assert!(note.contains("write may already have landed"), "{note}");
        assert!(note.contains("current body of #34"), "{note}");
        assert!(note.contains(SPLIT_MARKER), "{note}");
        assert!(note.contains("do not add them again"), "{note}");
        assert!(
            note.contains("only the missing marker or child links"),
            "{note}"
        );
        assert!(!note.contains("Add them to #34 by hand"), "{note}");
        assert!(note.contains("#101 first"), "{note}");
        assert!(note.contains("#102 second"), "{note}");
    }

    #[test]
    fn one_confirmed_child_is_an_error_until_it_is_recorded_or_closed() {
        let mut state = IssueRun::new(34, "split this");
        let parts = vec![("first".to_string(), 101)];
        record_partial_issue_split(&mut state, 34, &parts);

        assert_eq!(Status::Error, state.status);
        let note = state.notes.join("\n");
        assert!(note.contains("Do not rerun this split"), "{note}");
        assert!(note.contains("Link it from #34 by hand"), "{note}");
        assert!(note.contains("close it first"), "{note}");
        assert!(note.contains("#101 first"), "{note}");
    }

    #[test]
    fn an_uncertain_child_write_stops_before_rewriting_the_parent() {
        let mut state = IssueRun::new(34, "split this");
        let listed = vec![("first".to_string(), 101)];
        record_uncertain_issue_part(
            &mut state,
            34,
            "second",
            &listed,
            "the result could not be verified",
        );

        assert_eq!(Status::Error, state.status);
        let note = state.notes.join("\n");
        assert!(note.contains("write landed is unknown"), "{note}");
        assert!(note.contains("#101 first"), "{note}");
        assert!(
            note.contains("complete the tracker on #34 by hand"),
            "{note}"
        );
        assert!(note.contains("Do not rerun this split"), "{note}");
    }

    #[test]
    fn a_later_allocation_failure_preserves_partial_recovery_guidance() {
        let error = crate::error::SparError::new("no free branch name");
        let note = worktree_allocation_failure(34, 3, 2, &error);
        assert!(note.contains("2 child pull request"), "{note}");
        assert!(note.contains("worktrees were kept"), "{note}");
        assert!(note.contains("record the partial result on #34"), "{note}");
        assert!(note.contains("remove every retained"), "{note}");
    }

    #[test]
    fn successful_stacked_worktrees_are_all_released_unless_kept() {
        let worktrees = vec![
            (PathBuf::from("one"), "split-34-1".to_string()),
            (PathBuf::from("two"), "split-34-2".to_string()),
        ];
        let mut released = Vec::new();
        release_part_worktrees_with(false, Status::Split, worktrees.clone(), |dir, branch| {
            released.push((dir.to_path_buf(), branch.to_string()))
        });
        assert_eq!(worktrees, released);

        for (configured, status) in [(true, Status::Split), (false, Status::Error)] {
            let mut released = Vec::new();
            release_part_worktrees_with(configured, status, worktrees.clone(), |dir, branch| {
                released.push((dir.to_path_buf(), branch.to_string()))
            });
            assert!(released.is_empty(), "worktrees were not retained");
        }
    }

    #[test]
    fn a_missing_parent_comment_is_an_error_with_recovery_text() {
        let mut state = IssueRun::new(34, "split this");
        let error = SparError::new("offline");
        record_parent_comment_failure(&mut state, 34, "the summary", &error);

        assert_eq!(Status::Error, state.status);
        let note = state.notes.join("\n");
        assert!(note.contains("branches stop an automatic retry"), "{note}");
        assert!(note.contains("finish recording the split"), "{note}");
        assert!(note.contains("the summary"), "{note}");
    }

    #[test]
    fn an_uncertain_parent_comment_requires_inspection_before_posting() {
        let mut state = IssueRun::new(34, "split this");
        let error = SparError::uncertain_write("the comments could not be reread");
        record_parent_comment_failure(&mut state, 34, "the summary", &error);

        assert_eq!(Status::Error, state.status);
        let note = state.notes.join("\n");
        assert!(note.contains("comment may already have landed"), "{note}");
        assert!(
            note.contains("Inspect every top-level comment on #34"),
            "{note}"
        );
        assert!(note.contains("do not post it again"), "{note}");
        assert!(note.contains("If it is absent, post it once"), "{note}");
        assert!(!note.contains("Post this comment by hand"), "{note}");
        assert!(note.contains("the summary"), "{note}");
    }

    #[test]
    fn retained_branches_explain_manual_recovery_and_again() {
        let note = retained_branches_note(34);
        assert!(
            note.contains("Open any missing child pull request"),
            "{note}"
        );
        assert!(note.contains("current parent"), "{note}");
        assert!(note.contains("local worktree and branch"), "{note}");
        assert!(note.contains("remove every retained"), "{note}");
        assert!(note.contains("does not resume"), "{note}");
    }

    #[test]
    fn a_push_collision_halts_instead_of_dropping_one_part() {
        let error = SplitPushError::new("the create-only lease was rejected", false);
        match BuildOne::push_failed("split-34-1", &error, Some("slice".into())) {
            BuildOne::Halted {
                reason,
                disposable_head,
            } => {
                assert_eq!(Some("slice".to_string()), disposable_head);
                assert!(reason.contains("stopped"), "{reason}");
                assert!(reason.contains("competing pull requests"), "{reason}");
            }
            _ => panic!("a push collision did not halt the split"),
        }
    }

    #[test]
    fn an_unverified_push_halts_and_keeps_the_worktree() {
        let error = SplitPushError::new("origin could not be read", true);
        match BuildOne::push_failed("split-34-1", &error, Some("slice".into())) {
            BuildOne::Halted {
                reason,
                disposable_head,
            } => {
                assert!(disposable_head.is_none());
                assert!(reason.contains("may now exist on origin"), "{reason}");
                assert!(
                    reason.contains("worktree and branch record were kept"),
                    "{reason}"
                );
            }
            _ => panic!("an unverified push did not halt the split"),
        }
    }

    #[test]
    fn a_definite_push_refusal_keeps_stand_alone_edit_work() {
        let error = SplitPushError::new("origin is known not to contain the branch", false);
        match BuildOne::push_failed("split-34-1", &error, None) {
            BuildOne::Halted {
                reason,
                disposable_head,
            } => {
                assert!(disposable_head.is_none());
                assert!(
                    reason.contains("not confirmed to match its mechanical slice"),
                    "{reason}"
                );
                assert!(reason.contains("kept for recovery"), "{reason}");
            }
            _ => panic!("a local edit was dropped after the push refusal"),
        }
    }

    #[test]
    fn a_parent_move_keeps_stand_alone_edit_work() {
        let error = SparError::new("the parent head moved before the push");
        match BuildOne::parent_moved(&error, Path::new("/tmp/split-part"), None) {
            BuildOne::Halted {
                reason,
                disposable_head,
            } => {
                assert!(disposable_head.is_none());
                assert!(reason.contains("parent head moved"), "{reason}");
                assert!(reason.contains("/tmp/split-part"), "{reason}");
                assert!(reason.contains("kept"), "{reason}");
            }
            _ => panic!("a local edit was dropped after the parent moved"),
        }
    }

    /// The parent is what #29 consumes, so what is written has to be what is
    /// recognised.
    #[test]
    fn a_body_this_wrote_reads_back_as_already_split() {
        assert!(!already_split("just an issue"));
        let out = tracker_body("just an issue", &[("a".into(), 1), ("b".into(), 2)]);
        assert!(already_split(&out), "{out}");
    }

    /// A fence somebody left open would otherwise swallow the checklist, which
    /// then renders as code while `already_split` still says the parent is a
    /// tracker: it looks untouched and no later run comes back to it.
    #[test]
    fn a_fence_left_open_is_closed_before_the_checklist() {
        let out = tracker_body("Here:\n\n```rust\nfn unfinished() {}", &[("a".into(), 1)]);
        assert!(out.contains("fn unfinished"), "{out}");
        let after = out.split("fn unfinished() {}").nth(1).unwrap();
        assert!(after.trim_start().starts_with("```"), "{out}");
        assert!(already_split(&out), "{out}");
    }

    /// The closer has to match the opener, or the checklist stays inside the
    /// block while `already_split` reports the parent as a tracker: it looks
    /// untouched and no later run comes back to it.
    #[test]
    fn an_open_fence_is_closed_by_one_that_actually_closes_it() {
        for original in [
            // Backticks close nothing here.
            "Here:\n\n~~~\nfn unfinished() {}",
            // Three backticks are content inside a block opened with four.
            "Here:\n\n````\n```\nfn unfinished() {}",
        ] {
            let out = tracker_body(original, &[("a".into(), 1)]);
            let marker = out.split(SPLIT_MARKER).next().unwrap();
            assert!(
                unclosed_fence(marker).is_none(),
                "the checklist is inside a code block: {out}"
            );
            assert!(already_split(&out), "{out}");
        }
    }

    /// The other half of that: a body whose fences are balanced must not have
    /// one opened for it, which would put the checklist inside a code block
    /// this wrote.
    #[test]
    fn a_closed_fence_is_left_alone() {
        for original in [
            "Here:\n\n```rust\nfn done() {}\n```",
            // A fence of the other character inside a block is content, not a
            // fence, so counting them would find three and close one.
            "Here:\n\n```\n~~~\n```",
            // Same for a shorter run of the same character.
            "Here:\n\n````\n```\n````",
            "no fences at all",
        ] {
            let out = tracker_body(original, &[("a".into(), 1)]);
            assert!(
                out.starts_with(&format!("{original}\n\n{SPLIT_MARKER}")),
                "{out}"
            );
        }
    }

    /// An empty body is not a reason to write a checklist with a blank line
    /// above it, or to lose the marker.
    #[test]
    fn a_tracker_body_from_nothing_is_still_a_tracker() {
        let out = tracker_body("", &[("a".into(), 1)]);
        assert!(already_split(&out), "{out}");
        assert!(out.starts_with(SPLIT_MARKER), "{out}");
    }

    /// The invariant, as code. Splitting a pull request touches code somebody
    /// wrote, and the only thing that makes it safe is that it is purely
    /// additive.
    #[test]
    fn only_a_branch_the_split_made_may_be_pushed_to() {
        assert!(additive("split-12-1", "pr-12", "").is_ok());
        assert!(additive("spar/split-12-1", "spar/pr-12", "spar/").is_ok());

        for branch in ["main", "pr-12", "issue-12", "their-feature", "split-12-1"] {
            assert!(
                additive(branch, "main", "spar/").is_err(),
                "{branch} was allowed outside the split namespace"
            );
        }
        // Even inside the namespace, never the parent's own branch.
        assert!(additive("split-12-1", "split-12-1", "").is_err());
    }

    fn carried(parts: &[SplitPart]) -> Vec<&[String]> {
        parts.iter().map(|p| p.files.as_slice()).collect()
    }

    #[test]
    fn what_no_part_carries_is_reported_as_left_over() {
        let changed = vec!["a.rs".to_string(), "b.rs".into(), "c.rs".into()];
        let parts = vec![part("one", &["a.rs"]), part("two", &["c.rs"])];
        assert_eq!(
            vec!["b.rs".to_string()],
            leftover(&changed, carried(&parts))
        );
        let all = [part("all", &["a.rs", "b.rs", "c.rs"])];
        assert!(leftover(&changed, carried(&all)).is_empty());
    }

    /// A part that was proposed and then dropped leaves its files on the
    /// original, and the comment on the original is the only permanent record
    /// of that. Counting from the proposal instead loses them silently.
    #[test]
    fn a_dropped_part_leaves_its_files_in_the_leftover_report() {
        let changed = vec!["a.rs".to_string(), "b.rs".into()];
        let made = vec![Made {
            index: 1,
            title: "one".into(),
            url: "https://example.invalid/pull/1".into(),
            files: vec!["a.rs".to_string()],
        }];
        let left = leftover(&changed, made.iter().map(|m| m.files.as_slice()));
        assert_eq!(vec!["b.rs".to_string()], left);
        assert!(
            parts_comment(&made, &left, &Style::default()).contains("b.rs"),
            "the file of the dropped part went unsaid"
        );
    }

    /// The file list came from spar, so anything not in it is a paraphrase. A
    /// path with `..` in it would otherwise be written outside the worktree.
    #[test]
    fn a_part_may_only_carry_paths_the_change_actually_touches() {
        let changed = vec!["a.rs".to_string(), "b.rs".into()];
        let mut parts = vec![part("one", &["a.rs", "../../etc/passwd", "invented.rs"])];
        let unknown = confine(&mut parts, &changed);
        assert_eq!(vec!["a.rs".to_string()], parts[0].files);
        assert_eq!(2, unknown.len(), "{unknown:?}");
    }

    /// A path in two parts would be carried twice and reviewed twice, and for a
    /// stacked split the second copy would conflict with the first.
    #[test]
    fn a_path_claimed_twice_stays_with_the_first_part() {
        let changed = vec!["a.rs".to_string(), "b.rs".into()];
        let mut parts = vec![part("one", &["a.rs", "b.rs"]), part("two", &["b.rs"])];
        confine(&mut parts, &changed);
        assert_eq!(vec!["a.rs".to_string(), "b.rs".to_string()], parts[0].files);
        assert!(parts[1].files.is_empty(), "{:?}", parts[1].files);
    }

    /// What a part carries is read off its branch, so the stand-alone pass can
    /// put it on top of a file another part already took. `confine` cannot see
    /// that: it runs before either part exists.
    #[test]
    fn a_path_two_parts_ended_up_carrying_is_reported() {
        let made = vec![Made {
            index: 1,
            title: "one".into(),
            url: "https://example.invalid/pull/1".into(),
            files: vec!["a.rs".to_string(), "lib.rs".into()],
        }];
        let second = vec!["b.rs".to_string(), "lib.rs".into()];
        assert_eq!(
            vec!["lib.rs".to_string()],
            overlapping(&made, &second, false)
        );
        // A stacked part is diffed against the one before it, so a shared path
        // is the stack working, not two parts fighting over a file.
        assert!(overlapping(&made, &second, true).is_empty());
    }

    /// Both comments a pull request split can leave have to be recognisable
    /// again, or the next run proposes the same split, and for a fork that
    /// means the same comment once a run forever.
    #[test]
    fn a_comment_this_wrote_reads_back_as_already_split() {
        let style = Style::default();
        let made = vec![Made {
            index: 1,
            title: "First".into(),
            url: "https://example.invalid/pull/201".into(),
            files: vec!["first.rs".to_string()],
        }];
        let comment = parts_comment(&made, &["left.rs".to_string()], &style);
        assert!(already_split(&comment), "{comment}");
        assert!(comment.contains("/pull/201"), "{comment}");
        assert!(comment.contains("left.rs"), "{comment}");
        assert!(comment.contains("has not been changed"), "{comment}");

        let decision = decide(
            &proposal(vec![part("one", &["a.rs"]), part("two", &["b.rs"])]),
            &accept(),
            4,
        );
        let proposed = proposal_comment(12, &decision, &[], &style);
        assert!(already_split(&proposed), "{proposed}");
        assert!(proposed.contains("Nothing has been changed"), "{proposed}");
    }

    /// One surviving part is not a decomposition: the original still carries
    /// everything that part carries. It exists and cannot be unmade, so it is
    /// still named, but the comment must not call that a split.
    #[test]
    fn one_surviving_part_is_not_announced_as_a_split() {
        let made = vec![Made {
            index: 1,
            title: "First".into(),
            url: "https://example.invalid/pull/201".into(),
            files: vec!["first.rs".to_string()],
        }];
        let comment = parts_comment(&made, &[], &Style::default());
        assert!(!comment.contains("Split into"), "{comment}");
        assert!(comment.contains("has not been split"), "{comment}");
        assert!(comment.contains("/pull/201"), "{comment}");
        // Still recognisable, or the next run makes a second copy of it.
        assert!(already_split(&comment), "{comment}");
    }

    /// A part with no title cannot be filed and cannot be branched, so it is
    /// dropped with the rest still made.
    #[test]
    fn a_part_with_no_title_is_dropped_rather_than_filed() {
        let parts = vec![part("one", &[]), part("", &[]), part("three", &[])];
        let out = decide(&proposal(parts), &accept(), 4);
        assert_eq!(vec!["one", "three"], titles(&out));
        assert!(out.dropped[0].contains("no title"), "{:?}", out.dropped);
    }
}