cruise 0.1.35

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

use console::style;
use inquire::InquireError;

use crate::cancellation::CancellationToken;
use crate::cli::RunArgs;
use crate::config::validate_config;
use crate::engine::{execute_steps, print_dry_run};
use crate::error::{CruiseError, Result};
use crate::file_tracker::FileTracker;
use crate::option_handler::CliOptionHandler;
use crate::session::PLAN_VAR;
use crate::session::{
    SessionFileContents, SessionLogger, SessionManager, SessionPhase, SessionState,
    SessionStateFingerprint, WorkspaceMode, current_iso8601, get_cruise_home,
};
use crate::variable::VariableStore;
use crate::workflow::CompiledWorkflow;
use crate::workspace::{ExecutionWorkspace, prepare_execution_workspace, update_session_workspace};

const SESSION_STATE_CONFLICT_ABORT_LABEL: &str = "Abort run";
const SESSION_STATE_CONFLICT_OVERWRITE_LABEL: &str = "Overwrite external state";
const WORKSPACE_WORKTREE_LABEL: &str = "Create worktree (new branch)";
const WORKSPACE_CURRENT_BRANCH_LABEL: &str = "Use current branch";

#[cfg(test)]
const TEST_STATE_CONFLICT_ACTION_ENV: &str = "CRUISE_TEST_STATE_CONFLICT_ACTION";
#[cfg(test)]
const TEST_STATE_CONFLICT_LOG_ENV: &str = "CRUISE_TEST_STATE_CONFLICT_LOG";
#[cfg(test)]
const TEST_STDIN_IS_TERMINAL_ENV: &str = "CRUISE_TEST_STDIN_IS_TERMINAL";
#[cfg(test)]
const TEST_WORKSPACE_MODE_ENV: &str = "CRUISE_TEST_WORKSPACE_MODE";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WorkspaceOverride {
    RespectSession,
    ForceWorktree,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SessionStateConflictChoice {
    Abort,
    Overwrite,
}

/// Returns a safe fallback directory when `set_current_dir` fails.
fn fallback_root() -> PathBuf {
    #[cfg(windows)]
    {
        PathBuf::from(std::env::var("SYSTEMDRIVE").unwrap_or_else(|_| "C:".into()) + "\\")
    }
    #[cfg(not(windows))]
    {
        PathBuf::from("/")
    }
}

struct CurrentDirGuard {
    original: PathBuf,
}

impl CurrentDirGuard {
    fn capture() -> Result<Self> {
        Ok(Self {
            original: std::env::current_dir()?,
        })
    }
}

impl Drop for CurrentDirGuard {
    fn drop(&mut self) {
        if std::env::set_current_dir(&self.original).is_err() {
            let _ = std::env::set_current_dir(fallback_root());
        }
    }
}

fn save_session_state_with_conflict_resolution(
    manager: &SessionManager,
    session: &SessionState,
    expected_fingerprint: SessionStateFingerprint,
) -> Result<SessionStateFingerprint> {
    // Single read: inspect gives us both the fingerprint and parsed contents.
    let current_contents = manager.inspect_state_file(&session.id)?;
    let current_fingerprint = current_contents.fingerprint();
    if current_fingerprint == Some(expected_fingerprint) {
        return manager.save_with_fingerprint(session);
    }

    // Conflict detected -- build a user-facing message from the already-read contents.
    let state_path = manager.state_path(&session.id);
    let message = session_state_conflict_message(&state_path, &current_contents);

    if !stdin_is_terminal() {
        return Err(CruiseError::SessionStateConflict(message));
    }

    match prompt_for_session_state_conflict(&message)? {
        SessionStateConflictChoice::Abort => {
            #[cfg(test)]
            record_session_state_conflict_choice("abort");
            Err(CruiseError::SessionStateConflictAborted(message))
        }
        SessionStateConflictChoice::Overwrite => {
            #[cfg(test)]
            record_session_state_conflict_choice("overwrite");
            manager.save_with_fingerprint(session)
        }
    }
}

fn session_state_conflict_message(path: &Path, current_contents: &SessionFileContents) -> String {
    match current_contents {
        SessionFileContents::Missing => {
            format!(
                "{} was deleted while the session was running",
                path.display()
            )
        }
        SessionFileContents::Parsed { .. } => {
            format!(
                "{} changed externally while the session was running",
                path.display()
            )
        }
        SessionFileContents::Invalid { error, .. } => format!(
            "{} changed externally and now contains invalid JSON: {}",
            path.display(),
            error
        ),
    }
}

fn prompt_for_session_state_conflict(message: &str) -> Result<SessionStateConflictChoice> {
    #[cfg(test)]
    if let Some(choice) = test_session_state_conflict_choice() {
        return Ok(choice);
    }

    eprintln!("{} {}", style("!").yellow().bold(), message);
    let options = vec![
        SESSION_STATE_CONFLICT_ABORT_LABEL,
        SESSION_STATE_CONFLICT_OVERWRITE_LABEL,
    ];
    match inquire::Select::new("How should cruise proceed?", options).prompt() {
        Ok(choice) if choice == SESSION_STATE_CONFLICT_ABORT_LABEL => {
            Ok(SessionStateConflictChoice::Abort)
        }
        Ok(_) => Ok(SessionStateConflictChoice::Overwrite),
        Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => {
            Ok(SessionStateConflictChoice::Abort)
        }
        Err(e) => Err(CruiseError::Other(format!("selection error: {e}"))),
    }
}

fn stdin_is_terminal() -> bool {
    #[cfg(test)]
    if let Ok(value) = std::env::var(TEST_STDIN_IS_TERMINAL_ENV) {
        return value == "1";
    }

    std::io::stdin().is_terminal()
}

fn prompt_workspace_mode() -> Result<WorkspaceMode> {
    #[cfg(test)]
    if let Some(mode) = test_workspace_mode_choice() {
        return Ok(mode);
    }

    if !stdin_is_terminal() {
        return Ok(WorkspaceMode::Worktree);
    }

    let options = vec![WORKSPACE_WORKTREE_LABEL, WORKSPACE_CURRENT_BRANCH_LABEL];
    match inquire::Select::new("Where should cruise execute?", options).prompt() {
        Ok(choice) if choice == WORKSPACE_CURRENT_BRANCH_LABEL => Ok(WorkspaceMode::CurrentBranch),
        Ok(_) => Ok(WorkspaceMode::Worktree),
        Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => Err(
            CruiseError::Other("workspace mode selection cancelled".to_string()),
        ),
        Err(e) => Err(CruiseError::Other(format!("selection error: {e}"))),
    }
}

#[cfg(test)]
fn test_workspace_mode_choice() -> Option<WorkspaceMode> {
    std::env::var(TEST_WORKSPACE_MODE_ENV)
        .ok()
        .and_then(|v| match v.as_str() {
            "current_branch" => Some(WorkspaceMode::CurrentBranch),
            "worktree" => Some(WorkspaceMode::Worktree),
            _ => None,
        })
}

#[cfg(test)]
fn test_session_state_conflict_choice() -> Option<SessionStateConflictChoice> {
    std::env::var(TEST_STATE_CONFLICT_ACTION_ENV)
        .ok()
        .and_then(|value| match value.as_str() {
            "abort" => Some(SessionStateConflictChoice::Abort),
            "overwrite" => Some(SessionStateConflictChoice::Overwrite),
            _ => None,
        })
}

#[cfg(test)]
fn record_session_state_conflict_choice(choice: &str) {
    if let Ok(path) = std::env::var(TEST_STATE_CONFLICT_LOG_ENV)
        && let Ok(mut file) = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)
    {
        use std::io::Write;

        let _ = writeln!(file, "{choice}");
    }
}

fn load_run_all_result_state(
    manager: &SessionManager,
    fallback: &SessionState,
) -> Result<SessionState> {
    let contents = manager.inspect_state_file(&fallback.id)?;
    if let SessionFileContents::Parsed { state, .. } = contents {
        Ok(*state)
    } else {
        let state_path = manager.state_path(&fallback.id);
        let message = session_state_conflict_message(&state_path, &contents);
        let mut state = fallback.clone();
        state.phase = SessionPhase::Failed(message);
        state.completed_at = Some(current_iso8601());
        Ok(state)
    }
}

pub async fn run(args: RunArgs) -> Result<()> {
    if args.all {
        if args.session.is_some() {
            return Err(CruiseError::Other(
                "Cannot specify both --all and a session ID".to_string(),
            ));
        }
        return run_all(args).await;
    }

    match run_single(args, WorkspaceOverride::RespectSession).await {
        Err(CruiseError::StepPaused) => {
            eprintln!("Session paused. Resume with `cruise run`.");
            Ok(())
        }
        other => other,
    }
}

#[expect(clippy::too_many_lines)]
async fn run_single(args: RunArgs, workspace_override: WorkspaceOverride) -> Result<()> {
    let _current_dir_guard = CurrentDirGuard::capture()?;
    let manager = SessionManager::new(get_cruise_home()?);
    let session_id = args
        .session
        .map_or_else(|| select_pending_session(&manager), Ok)?;
    let (mut session, initial_fingerprint) = manager.load_with_fingerprint(&session_id)?;

    if !session.phase.is_runnable() {
        return Err(CruiseError::Other(format!(
            "Session {} is in '{}' phase and cannot be run. Approve it first with `cruise list`.",
            session_id,
            session.phase.label()
        )));
    }

    let config = manager.load_config(&session)?;
    validate_config(&config)?;

    if args.dry_run {
        eprintln!("{}", style(format!("Session: {session_id}")).dim());
        print_dry_run(&config, session.current_step.as_deref());
        return Ok(());
    }

    let compiled = crate::workflow::compile(config)?;
    let effective_workspace_mode = match workspace_override {
        WorkspaceOverride::ForceWorktree => WorkspaceMode::Worktree,
        WorkspaceOverride::RespectSession => {
            if session.current_step.is_none() && session.workspace_mode == WorkspaceMode::Worktree {
                prompt_workspace_mode()?
            } else {
                session.workspace_mode
            }
        }
    };
    session.workspace_mode = effective_workspace_mode;
    if effective_workspace_mode == WorkspaceMode::Worktree {
        crate::worktree_pr::ensure_gh_available()?;
    }
    let start_step = session.current_step.clone().map_or_else(
        || {
            compiled
                .steps
                .keys()
                .next()
                .ok_or_else(|| CruiseError::Other("config has no steps".to_string()))
                .cloned()
        },
        Ok,
    )?;
    log_resume_message(&session);
    std::env::set_current_dir(session.base_dir.clone())?;
    let execution_workspace =
        prepare_execution_workspace(&manager, &mut session, effective_workspace_mode)?;
    log_execution_workspace(&execution_workspace);
    update_session_workspace(&mut session, &execution_workspace);
    session.phase = SessionPhase::Running;
    let initial_fingerprint =
        save_session_state_with_conflict_resolution(&manager, &session, initial_fingerprint)?;
    std::env::set_current_dir(execution_workspace.path())?;

    let plan_path = session.plan_path(&manager.sessions_dir());
    let mut vars = VariableStore::new(session.input.clone());
    vars.set_named_file(PLAN_VAR, plan_path);
    let mut tracker = FileTracker::with_root(execution_workspace.path().to_path_buf());
    let config_reloader: Option<Box<dyn Fn() -> Result<Option<CompiledWorkflow>>>> =
        session.config_path.as_ref().map(|path| {
            let path = path.clone();
            let last_mtime = Cell::new(std::fs::metadata(&path).and_then(|m| m.modified()).ok());
            Box::new(move || {
                let current_mtime = std::fs::metadata(&path).and_then(|m| m.modified()).ok();
                if current_mtime == last_mtime.get() {
                    return Ok(None);
                }
                let yaml = std::fs::read_to_string(&path).map_err(|e| {
                    CruiseError::Other(format!("failed to read config {}: {}", path.display(), e))
                })?;
                let config = crate::config::WorkflowConfig::from_yaml(&yaml)
                    .map_err(|e| CruiseError::ConfigParseError(e.to_string()))?;
                let compiled = crate::workflow::compile(config)?;
                last_mtime.set(current_mtime);
                Ok(Some(compiled))
            }) as Box<dyn Fn() -> Result<Option<CompiledWorkflow>>>
        });
    let log_path = manager.run_log_path(&session_id);
    let logger = SessionLogger::new(log_path);
    logger.write("--- run started ---");
    let skipped_steps = session.skipped_steps.clone();
    let session_cell = RefCell::new(&mut session);
    let session_fingerprint = Cell::new(initial_fingerprint);
    let on_step_start = |step: &str| {
        logger.write(step);
        let mut s = session_cell.borrow_mut();
        s.current_step = Some(step.to_string());
        let fingerprint =
            save_session_state_with_conflict_resolution(&manager, &s, session_fingerprint.get())?;
        session_fingerprint.set(fingerprint);
        Ok(())
    };
    let cancel_token = CancellationToken::new();
    let ctx = crate::engine::ExecutionContext {
        compiled: &compiled,
        max_retries: args.max_retries,
        rate_limit_retries: args.rate_limit_retries,
        on_step_start: &on_step_start,
        cancel_token: Some(&cancel_token),
        option_handler: &CliOptionHandler,
        config_reloader: config_reloader.as_deref(),
        working_dir: Some(execution_workspace.path()),
        skipped_steps: &skipped_steps,
    };
    let exec_result = tokio::select! {
        result = execute_steps(&ctx, &mut vars, &mut tracker, &start_step) => result,
        _ = tokio::signal::ctrl_c() => {
            cancel_token.cancel();
            Err(CruiseError::Interrupted)
        },
    };
    let session = session_cell.into_inner();

    // Handle Ctrl+C: save as Suspended so the session can be resumed later.
    if matches!(exec_result, Err(CruiseError::Interrupted)) {
        logger.write("|| cancelled");
        eprintln!(
            "\n{} Interrupted -- session saved as Suspended.",
            style("||").yellow().bold()
        );
        session.phase = SessionPhase::Suspended;
        manager.save(session)?;
        return Err(CruiseError::Interrupted);
    }

    let overall_result = match exec_result {
        Ok(exec) => {
            logger.write(&format!(
                "v completed -- run: {}, skipped: {}, failed: {}",
                exec.run, exec.skipped, exec.failed
            ));
            match &execution_workspace {
                ExecutionWorkspace::CurrentBranch { .. } => Ok(()),
                ExecutionWorkspace::Worktree { ctx, .. } => {
                    crate::worktree_pr::handle_worktree_pr(
                        ctx,
                        &compiled,
                        &mut vars,
                        &mut tracker,
                        session,
                        args.rate_limit_retries,
                        args.max_retries,
                    )
                    .await
                }
            }
        }
        Err(e) => {
            logger.write(&format!("x failed: {}", e.detailed_message()));
            Err(e)
        }
    };

    if let Err(e) = &overall_result
        && matches!(
            e,
            CruiseError::SessionStateConflict(_) | CruiseError::SessionStateConflictAborted(_)
        )
    {
        return overall_result;
    }

    apply_run_result_to_session(session, &overall_result);
    save_session_state_with_conflict_resolution(&manager, session, session_fingerprint.get())?;
    overall_result
}

/// Apply the result of a step execution to the session state.
///
/// - `Ok(())` -> `Completed`
/// - `Err(StepPaused)` -> keep `Running` (session can be resumed with `cruise run`)
/// - `Err(other)` -> `Failed`
fn apply_run_result_to_session(session: &mut SessionState, result: &Result<()>) {
    match result {
        Ok(()) => {
            session.phase = SessionPhase::Completed;
            session.completed_at = Some(current_iso8601());
        }
        Err(CruiseError::StepPaused) => {
            // Keep Running phase so the session can be resumed later.
        }
        Err(e) => {
            session.phase = SessionPhase::Failed(e.to_string());
            session.completed_at = Some(current_iso8601());
        }
    }
}

/// Log a resume message if the session is being restarted.
fn log_resume_message(session: &SessionState) {
    let Some(ref step) = session.current_step else {
        return;
    };
    match &session.phase {
        SessionPhase::Running | SessionPhase::Suspended => {
            eprintln!("{} Resuming from step: {}", style("R").cyan(), step);
        }
        SessionPhase::Failed(_) => {
            eprintln!(
                "{} Retrying from failed step: {}",
                style("R").yellow(),
                step
            );
        }
        _ => {}
    }
}

/// Log the chosen execution workspace for CLI users.
fn log_execution_workspace(ws: &ExecutionWorkspace) {
    match ws {
        ExecutionWorkspace::Worktree { ctx, reused } => {
            let suffix = if *reused { " (reused)" } else { "" };
            eprintln!(
                "{} worktree: {}{}",
                style("->").cyan(),
                ctx.path.display(),
                suffix
            );
        }
        ExecutionWorkspace::CurrentBranch { path } => {
            eprintln!("{} current branch: {}", style("->").cyan(), path.display());
        }
    }
}

async fn run_all(args: RunArgs) -> Result<()> {
    let manager = SessionManager::new(get_cruise_home()?);
    let mut seen: HashSet<String> = HashSet::new();
    let mut results: Vec<SessionState> = Vec::new();

    loop {
        let remaining = manager.run_all_remaining(&seen)?;
        let Some(session) = remaining.into_iter().next() else {
            break;
        };
        seen.insert(session.id.clone());

        let session_args = RunArgs {
            session: Some(session.id.clone()),
            all: false,
            max_retries: args.max_retries,
            rate_limit_retries: args.rate_limit_retries,
            dry_run: args.dry_run,
        };
        let run_result = Box::pin(run_single(session_args, WorkspaceOverride::ForceWorktree)).await;
        let interrupted = matches!(run_result, Err(CruiseError::Interrupted));
        match run_result {
            Err(CruiseError::StepPaused) => {
                eprintln!("session {} paused by user", session.id);
            }
            Err(e) if !interrupted => {
                eprintln!(
                    "warning: session {} encountered an error: {}",
                    session.id,
                    e.detailed_message()
                );
            }
            Ok(()) | Err(_) => {}
        }
        results.push(load_run_all_result_state(&manager, &session)?);
        if interrupted {
            break;
        }
    }

    let summary = format_run_all_summary(&results);
    if !summary.is_empty() {
        eprintln!("\n{summary}");
    }

    Ok(())
}

/// Select a pending session interactively (or automatically if only one).
fn select_pending_session(manager: &SessionManager) -> Result<String> {
    let pending = manager.pending()?;

    if pending.is_empty() {
        return Err(CruiseError::Other(
            "No pending sessions. Run `cruise plan` first.".to_string(),
        ));
    }

    if pending.len() == 1 {
        let s = &pending[0];
        eprintln!(
            "{} Selected session: {} -- {}",
            style("->").cyan(),
            s.id,
            crate::display::truncate(&s.input, 60)
        );
        return Ok(s.id.clone());
    }

    // Multiple pending sessions: let the user choose.
    let labels: Vec<String> = pending
        .iter()
        .map(|s| {
            format!(
                "{} | {} | {}",
                s.id,
                s.phase.label(),
                crate::display::truncate(&s.input, 60)
            )
        })
        .collect();
    let label_refs: Vec<&str> = labels.iter().map(std::string::String::as_str).collect();

    let selected = match inquire::Select::new("Select a session to run:", label_refs).prompt() {
        Ok(s) => s,
        Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => {
            return Err(CruiseError::Other(
                "session selection cancelled".to_string(),
            ));
        }
        Err(e) => return Err(CruiseError::Other(format!("selection error: {e}"))),
    };

    let idx = labels
        .iter()
        .position(|l| l.as_str() == selected)
        .ok_or_else(|| CruiseError::Other(format!("selected session not found: {selected}")))?;
    Ok(pending[idx].id.clone())
}

/// Format a summary of all sessions run by `run --all`.
/// Returns an empty string if `results` is empty.
fn format_run_all_summary(results: &[SessionState]) -> String {
    const MAX_INPUT_CHARS: usize = 60;

    if results.is_empty() {
        return String::new();
    }

    let mut lines = Vec::with_capacity(results.len() + 1);
    lines.push(format!(
        "=== Run All Summary ({} sessions) ===",
        results.len()
    ));

    for (i, result) in results.iter().enumerate() {
        let truncated = crate::display::truncate(&result.input, MAX_INPUT_CHARS);

        let line = match &result.phase {
            SessionPhase::Completed => {
                let pr = result
                    .pr_url
                    .as_deref()
                    .map(|url| format!(" {} {url}", style("->").yellow()))
                    .unwrap_or_default();
                format!(
                    "[{}] {} {}{}",
                    i + 1,
                    style("v").green().bold(),
                    truncated,
                    pr
                )
            }
            SessionPhase::Failed(err) => {
                format!(
                    "[{}] {} {} -- Failed: {}",
                    i + 1,
                    style("x").red().bold(),
                    truncated,
                    err
                )
            }
            SessionPhase::Suspended => {
                format!(
                    "[{}] {} {} -- Suspended",
                    i + 1,
                    style("||").yellow().bold(),
                    truncated
                )
            }
            SessionPhase::AwaitingApproval | SessionPhase::Planned | SessionPhase::Running => {
                format!("[{}] ? {}", i + 1, truncated)
            }
        };
        lines.push(line);
    }

    lines.join("\n")
}

#[cfg(test)]
#[cfg(unix)]
mod tests {
    use super::*;
    use crate::cli::{DEFAULT_MAX_RETRIES, DEFAULT_RATE_LIMIT_RETRIES};
    use crate::session::WorkspaceMode;
    use crate::worktree_pr::{
        CommitOutcome, PrAttemptOutcome, attempt_pr_creation, build_pr_prompt,
        extract_last_path_segment, parse_pr_metadata, strip_code_block,
    };
    use std::fs;
    use std::path::{Path, PathBuf};
    use std::process::Command;
    use tempfile::TempDir;

    use crate::test_binary_support::PathEnvGuard;
    use crate::test_support::{init_git_repo, run_git_ok};
    use crate::worktree;

    fn git_stdout_ok(dir: &Path, args: &[&str]) -> String {
        let output = Command::new("git")
            .args(args)
            .current_dir(dir)
            .output()
            .unwrap_or_else(|e| panic!("git command failed to start: {e}"));
        assert!(
            output.status.success(),
            "git {:?} failed: {}",
            args,
            String::from_utf8_lossy(&output.stderr).trim()
        );
        String::from_utf8_lossy(&output.stdout).trim().to_string()
    }

    fn create_worktree(tmp: &TempDir, session_id: &str) -> (PathBuf, worktree::WorktreeContext) {
        let repo = tmp.path().join("repo");
        fs::create_dir(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);

        // Set up a local bare repo as "origin" so git push works in tests
        let bare = tmp.path().join("origin.git");
        run_git_ok(tmp.path(), &["init", "--bare", "origin.git"]);
        run_git_ok(
            &repo,
            &[
                "remote",
                "add",
                "origin",
                bare.to_str().unwrap_or_else(|| panic!("unexpected None")),
            ],
        );

        let worktrees_dir = tmp.path().join("worktrees");
        let (ctx, reused) =
            worktree::setup_session_worktree(&repo, session_id, "test task", &worktrees_dir, None)
                .unwrap_or_else(|e| panic!("{e:?}"));
        assert!(!reused, "test worktree should be created fresh");
        (repo, ctx)
    }

    fn install_fake_gh(bin_dir: &Path, log_path: &Path, head_path: &Path, url: &str) {
        fs::create_dir_all(bin_dir).unwrap_or_else(|e| panic!("{e:?}"));
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;

            let script_path = bin_dir.join("gh");
            let script = format!(
                "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"{}\"\ngit rev-parse HEAD > \"{}\"\nprintf '%s\\n' \"{}\"\n",
                log_path.display(),
                head_path.display(),
                url
            );
            fs::write(&script_path, script).unwrap_or_else(|e| panic!("{e:?}"));
            let mut perms = fs::metadata(&script_path)
                .unwrap_or_else(|e| panic!("{e:?}"))
                .permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&script_path, perms).unwrap_or_else(|e| panic!("{e:?}"));
        }
        #[cfg(windows)]
        {
            let script_path = bin_dir.join("gh.cmd");
            let script = format!(
                "@echo off\r\necho %*>>\"{}\"\r\ngit rev-parse HEAD > \"{}\"\r\necho {}\r\n",
                log_path.display(),
                head_path.display(),
                url
            );
            fs::write(&script_path, script).unwrap();
        }
    }

    fn install_logging_gh(bin_dir: &Path, log_path: &Path, url: &str) {
        fs::create_dir_all(bin_dir).unwrap_or_else(|e| panic!("{e:?}"));
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;

            let script_path = bin_dir.join("gh");
            let script = format!(
                "#!/bin/sh\nif [ \"$1\" = \"--version\" ]; then\n  printf '%s\\n' 'gh version test'\n  exit 0\nfi\nprintf '%s\\n' \"$*\" >> \"{}\"\nif [ \"$1\" = \"pr\" ] && [ \"$2\" = \"create\" ]; then\n  printf '%s\\n' \"{}\"\nfi\nif [ \"$1\" = \"pr\" ] && [ \"$2\" = \"view\" ]; then\n  printf '%s\\n' \"{}\"\nfi\n",
                log_path.display(),
                url,
                url
            );
            fs::write(&script_path, script).unwrap_or_else(|e| panic!("{e:?}"));
            let mut perms = fs::metadata(&script_path)
                .unwrap_or_else(|e| panic!("{e:?}"))
                .permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&script_path, perms).unwrap_or_else(|e| panic!("{e:?}"));
        }
        #[cfg(windows)]
        {
            let script_path = bin_dir.join("gh.cmd");
            let script = format!(
                "@echo off\r\nif \"%1\"==\"--version\" (\r\n  echo gh version test\r\n  exit /b 0\r\n)\r\necho %*>>\"{}\"\r\nif \"%1\"==\"pr\" if \"%2\"==\"create\" echo {}\r\nif \"%1\"==\"pr\" if \"%2\"==\"view\" echo {}\r\n",
                log_path.display(),
                url,
                url
            );
            fs::write(&script_path, script).unwrap();
        }
    }

    struct ProcessStateGuard {
        prev_home: Option<std::ffi::OsString>,
        prev_userprofile: Option<std::ffi::OsString>,
        prev_path: Option<std::ffi::OsString>,
        prev_dir: PathBuf,
        extra_env: Vec<(String, Option<std::ffi::OsString>)>,
        lock: crate::test_support::ProcessLock,
    }

    impl ProcessStateGuard {
        fn new(home: &Path) -> Self {
            let lock = crate::test_support::lock_process();
            let prev_home = std::env::var_os("HOME");
            let prev_userprofile = std::env::var_os("USERPROFILE");
            let prev_path = std::env::var_os("PATH");
            let prev_dir = std::env::current_dir().unwrap_or_else(|_| fallback_root());
            unsafe {
                std::env::set_var("HOME", home);
                std::env::set_var("USERPROFILE", home);
            }
            Self {
                prev_home,
                prev_userprofile,
                prev_path,
                prev_dir,
                extra_env: Vec::new(),
                lock,
            }
        }

        fn prepend_path(&self, dir: &Path) {
            // `self.lock` ensures the caller holds a `ProcessStateGuard` (and therefore the process lock).
            let _ = &self.lock;
            let mut paths = vec![dir.to_path_buf()];
            if let Some(existing) = std::env::var_os("PATH") {
                paths.extend(std::env::split_paths(&existing));
            }
            if let Ok(joined) = std::env::join_paths(paths) {
                unsafe { std::env::set_var("PATH", joined) };
            }
        }

        fn set_current_dir(&self, dir: &Path) {
            // `self.lock` ensures the caller holds a `ProcessStateGuard` (and therefore the process lock).
            let _ = &self.lock;
            let _ = std::env::set_current_dir(dir);
        }

        fn set_env(&mut self, key: &str, value: impl AsRef<std::ffi::OsStr>) {
            self.remember_env(key);
            unsafe {
                std::env::set_var(key, value);
            }
        }

        fn remove_env(&mut self, key: &str) {
            self.remember_env(key);
            unsafe {
                std::env::remove_var(key);
            }
        }

        fn remember_env(&mut self, key: &str) {
            if self.extra_env.iter().all(|(existing, _)| existing != key) {
                self.extra_env
                    .push((key.to_string(), std::env::var_os(key)));
            }
        }
    }

    impl Drop for ProcessStateGuard {
        fn drop(&mut self) {
            if std::env::set_current_dir(&self.prev_dir).is_err() {
                let _ = std::env::set_current_dir(fallback_root());
            }
            unsafe {
                for (key, previous) in self.extra_env.iter().rev() {
                    if let Some(value) = previous {
                        std::env::set_var(key, value);
                    } else {
                        std::env::remove_var(key);
                    }
                }

                if let Some(ref prev_home) = self.prev_home {
                    std::env::set_var("HOME", prev_home);
                } else {
                    std::env::remove_var("HOME");
                }

                if let Some(ref prev_userprofile) = self.prev_userprofile {
                    std::env::set_var("USERPROFILE", prev_userprofile);
                } else {
                    std::env::remove_var("USERPROFILE");
                }

                if let Some(ref prev_path) = self.prev_path {
                    std::env::set_var("PATH", prev_path);
                } else {
                    std::env::remove_var("PATH");
                }
            }
        }
    }

    fn create_repo_with_origin(tmp: &TempDir) -> PathBuf {
        let repo = tmp.path().join("repo");
        fs::create_dir(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);

        let bare = tmp.path().join("origin.git");
        run_git_ok(tmp.path(), &["init", "--bare", "origin.git"]);
        run_git_ok(
            &repo,
            &[
                "remote",
                "add",
                "origin",
                bare.to_str().unwrap_or_else(|| panic!("unexpected None")),
            ],
        );

        repo
    }

    fn make_current_branch_session(
        id: &str,
        repo: &Path,
        input: &str,
        target_branch: &str,
    ) -> SessionState {
        let mut session = SessionState::new(
            id.to_string(),
            repo.to_path_buf(),
            "cruise.yaml".to_string(),
            input.to_string(),
        );
        session.phase = SessionPhase::Planned;
        session.workspace_mode = WorkspaceMode::CurrentBranch;
        session.target_branch = Some(target_branch.to_string());
        session
    }

    fn write_config(manager: &SessionManager, session_id: &str, yaml: &str) {
        let session_dir = manager.sessions_dir().join(session_id);
        fs::create_dir_all(&session_dir).unwrap_or_else(|e| panic!("{e:?}"));
        fs::write(session_dir.join("config.yaml"), yaml).unwrap_or_else(|e| panic!("{e:?}"));
    }

    fn single_command_config(step_name: &str, command: &str) -> String {
        format!("command:\n  - cat\nsteps:\n  {step_name}:\n    command: |\n      {command}\n")
    }

    fn run_args(session_id: &str) -> RunArgs {
        RunArgs {
            session: Some(session_id.to_string()),
            all: false,
            max_retries: 10,
            rate_limit_retries: 0,
            dry_run: false,
        }
    }

    fn blocking_conflict_config() -> String {
        r"command:
  - cat
steps:
  first:
    command: |
      while [ ! -f proceed.txt ]; do sleep 0.05; done
  second:
    command: |
      printf second > second.txt
"
        .to_string()
    }

    fn setup_current_branch_conflict_session(
        tmp: &TempDir,
        session_id: &str,
        input: &str,
    ) -> (ProcessStateGuard, PathBuf, SessionManager) {
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session = make_current_branch_session(session_id, &repo, input, "main");
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(&manager, session_id, &blocking_conflict_config());

        (process, repo, manager)
    }

    fn configure_conflict_test_env(
        process: &mut ProcessStateGuard,
        is_terminal: bool,
        action: Option<&str>,
        log_path: &Path,
    ) {
        process.set_env(
            TEST_STDIN_IS_TERMINAL_ENV,
            if is_terminal { "1" } else { "0" },
        );
        if let Some(action) = action {
            process.set_env(TEST_STATE_CONFLICT_ACTION_ENV, action);
        } else {
            process.remove_env(TEST_STATE_CONFLICT_ACTION_ENV);
        }
        process.set_env(TEST_STATE_CONFLICT_LOG_ENV, log_path);
    }

    async fn wait_for_session_step(manager: &SessionManager, session_id: &str, step: &str) {
        for _ in 0..200 {
            if let Ok(state) = manager.load(session_id)
                && matches!(state.phase, SessionPhase::Running)
                && state.current_step.as_deref() == Some(step)
            {
                return;
            }
            tokio::time::sleep(std::time::Duration::from_millis(25)).await;
        }

        panic!("timed out waiting for session {session_id} to reach step {step}");
    }

    async fn mutate_state_after_first_step<F, G>(
        manager: &SessionManager,
        session_id: &str,
        workspace_path: G,
        mutate: F,
    ) where
        F: FnOnce(&SessionManager, &str),
        G: FnOnce(&SessionState) -> PathBuf,
    {
        wait_for_session_step(manager, session_id, "first").await;
        let state = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        let workspace = workspace_path(&state);
        mutate(manager, session_id);
        fs::write(workspace.join("proceed.txt"), "go").unwrap_or_else(|e| panic!("{e:?}"));
    }

    fn write_external_failed_state(manager: &SessionManager, session_id: &str) {
        let mut external = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        external.phase = SessionPhase::Failed("external edit".to_string());
        external.current_step = Some("external-step".to_string());
        manager.save(&external).unwrap_or_else(|e| panic!("{e:?}"));
    }

    fn make_pr_prompt_config(pr_language_yaml: Option<&str>) -> CompiledWorkflow {
        let mut yaml = String::from("command: [claude, -p]\n");
        if let Some(pr_language_yaml) = pr_language_yaml {
            yaml.push_str(pr_language_yaml);
        }
        yaml.push_str("steps:\n  implement:\n    prompt: test\n");
        let config =
            crate::config::WorkflowConfig::from_yaml(&yaml).unwrap_or_else(|e| panic!("{e:?}"));
        crate::workflow::compile(config).unwrap_or_else(|e| panic!("{e:?}"))
    }

    fn make_pr_prompt_vars() -> VariableStore {
        let mut vars = VariableStore::new("test input".to_string());
        vars.set_named_file(PLAN_VAR, PathBuf::from("plan.md"));
        vars
    }

    #[test]
    fn test_extract_last_path_segment_github_pr_url() {
        // Given: a standard GitHub PR URL
        let url = "https://github.com/owner/repo/pull/42";
        // When: extracting the last segment
        let result = extract_last_path_segment(url);
        // Then: last segment is returned
        assert_eq!(result, Some("42".to_string()));
    }

    #[test]
    fn test_build_pr_prompt_includes_configured_pr_language() {
        // Given: a workflow config with a custom PR language
        let config = make_pr_prompt_config(Some("pr_language: Japanese\n"));
        let mut vars = make_pr_prompt_vars();

        // When: building the PR prompt
        let prompt = build_pr_prompt(&mut vars, &config).unwrap_or_else(|e| panic!("{e:?}"));

        // Then: the configured language is injected into the prompt
        assert!(
            prompt.contains("Japanese"),
            "prompt should include configured language: {prompt}"
        );
        assert!(
            prompt.contains("plan.md"),
            "prompt should continue resolving existing variables: {prompt}"
        );
    }

    #[test]
    fn test_build_pr_prompt_defaults_blank_pr_language_to_english() {
        // Given: a workflow config with a blank PR language
        let config = make_pr_prompt_config(Some("pr_language: \"   \"\n"));
        let mut vars = make_pr_prompt_vars();

        // When: building the PR prompt
        let prompt = build_pr_prompt(&mut vars, &config).unwrap_or_else(|e| panic!("{e:?}"));

        // Then: the prompt falls back to the built-in English default
        assert!(
            prompt.contains(crate::config::DEFAULT_PR_LANGUAGE),
            "prompt should include the default language: {prompt}"
        );
    }

    #[test]
    fn test_attempt_pr_creation_skips_gh_when_branch_has_no_commits() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let (_repo, ctx) = create_worktree(&tmp, "20260307225900");
        let bin_dir = tmp.path().join("bin");
        let log_path = tmp.path().join("gh.log");
        let head_path = tmp.path().join("gh-head.txt");
        install_fake_gh(
            &bin_dir,
            &log_path,
            &head_path,
            "https://github.com/owner/repo/pull/1",
        );
        let _path_guard = PathEnvGuard::prepend(&bin_dir);

        let result =
            attempt_pr_creation(&ctx, "test task", "", "").unwrap_or_else(|e| panic!("{e:?}"));

        assert_eq!(result, PrAttemptOutcome::SkippedNoCommits);
        assert!(
            !log_path.exists(),
            "gh should not be called when no commit exists"
        );
        assert!(
            !head_path.exists(),
            "gh should not observe HEAD when skipped"
        );
        worktree::cleanup_worktree(&ctx).unwrap_or_else(|e| panic!("{e:?}"));
    }

    #[test]
    fn test_attempt_pr_creation_commits_changes_before_calling_gh() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let f = setup_pr_test(
            &tmp,
            "20260307225901",
            "https://github.com/owner/repo/pull/2",
        );
        let base_head = git_stdout_ok(&f.repo, &["rev-parse", "HEAD"]);

        let result =
            attempt_pr_creation(&f.ctx, "add feature", "", "").unwrap_or_else(|e| panic!("{e:?}"));

        assert_eq!(
            result,
            PrAttemptOutcome::Created {
                url: f.url.clone(),
                commit_outcome: CommitOutcome::Created,
            }
        );
        assert_eq!(
            git_stdout_ok(&f.ctx.path, &["log", "-1", "--pretty=%s"]),
            "add feature"
        );
        let worktree_head = git_stdout_ok(&f.ctx.path, &["rev-parse", "HEAD"]);
        assert_ne!(
            worktree_head, base_head,
            "helper should create a new commit"
        );
        assert_eq!(
            fs::read_to_string(&f.head_path)
                .unwrap_or_else(|e| panic!("{e:?}"))
                .trim(),
            worktree_head
        );
        let gh_args = fs::read_to_string(&f.log_path).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            gh_args.contains("pr create --head") && gh_args.contains("--fill"),
            "fake gh should receive a pr create invocation, got: {gh_args}"
        );
        assert!(
            gh_args.contains("--draft"),
            "gh pr create should include --draft flag, got: {gh_args}"
        );
        worktree::cleanup_worktree(&f.ctx).unwrap_or_else(|e| panic!("{e:?}"));
    }

    #[test]
    fn test_attempt_pr_creation_reuses_existing_branch_commits() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let f = setup_pr_test(
            &tmp,
            "20260307225902",
            "https://github.com/owner/repo/pull/3",
        );
        let base_head = git_stdout_ok(&f.repo, &["rev-parse", "HEAD"]);
        run_git_ok(&f.ctx.path, &["add", "."]);
        run_git_ok(&f.ctx.path, &["commit", "-m", "existing commit"]);

        let existing_head = git_stdout_ok(&f.ctx.path, &["rev-parse", "HEAD"]);
        assert_ne!(existing_head, base_head);

        let result = attempt_pr_creation(&f.ctx, "rerun without changes", "", "")
            .unwrap_or_else(|e| panic!("{e:?}"));

        assert_eq!(
            result,
            PrAttemptOutcome::Created {
                url: f.url.clone(),
                commit_outcome: CommitOutcome::NoChanges,
            }
        );
        assert_eq!(
            git_stdout_ok(&f.ctx.path, &["rev-parse", "HEAD"]),
            existing_head
        );
        assert_eq!(
            fs::read_to_string(&f.head_path)
                .unwrap_or_else(|e| panic!("{e:?}"))
                .trim(),
            existing_head
        );
        worktree::cleanup_worktree(&f.ctx).unwrap_or_else(|e| panic!("{e:?}"));
    }

    struct PrTestFixture {
        repo: PathBuf,
        ctx: worktree::WorktreeContext,
        #[expect(dead_code)]
        path_guard: PathEnvGuard,
        log_path: PathBuf,
        head_path: PathBuf,
        url: String,
    }

    fn setup_pr_test(tmp: &TempDir, session_id: &str, url: &str) -> PrTestFixture {
        let (repo, ctx) = create_worktree(tmp, session_id);
        fs::write(ctx.path.join("feature.txt"), "hello").unwrap_or_else(|e| panic!("{e:?}"));

        let bin_dir = tmp.path().join("bin");
        let log_path = tmp.path().join("gh.log");
        let head_path = tmp.path().join("gh-head.txt");
        install_fake_gh(&bin_dir, &log_path, &head_path, url);
        let path_guard = PathEnvGuard::prepend(&bin_dir);

        PrTestFixture {
            repo,
            ctx,
            path_guard,
            log_path,
            head_path,
            url: url.to_string(),
        }
    }

    #[test]
    fn test_attempt_pr_creation_uses_pr_title_as_commit_message_when_title_is_present() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let f = setup_pr_test(
            &tmp,
            "20260310pr_title_commit_01",
            "https://github.com/owner/repo/pull/10",
        );

        let pr_title = "feat: add user icon registration";
        let result = attempt_pr_creation(&f.ctx, "implement user icon feature", pr_title, "")
            .unwrap_or_else(|e| panic!("{e:?}"));

        assert_eq!(
            result,
            PrAttemptOutcome::Created {
                url: f.url.clone(),
                commit_outcome: CommitOutcome::Created
            }
        );
        assert_eq!(
            git_stdout_ok(&f.ctx.path, &["log", "-1", "--pretty=%s"]),
            pr_title,
            "commit subject should equal the PR title when title is non-empty"
        );
        let gh_args = fs::read_to_string(&f.log_path).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            gh_args.contains("--title") && gh_args.contains(pr_title),
            "fake gh should receive --title with the PR title; got: {gh_args}"
        );
        worktree::cleanup_worktree(&f.ctx).unwrap_or_else(|e| panic!("{e:?}"));
    }

    #[test]
    fn test_attempt_pr_creation_falls_back_to_message_when_pr_title_is_empty() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let f = setup_pr_test(
            &tmp,
            "20260310pr_title_commit_02",
            "https://github.com/owner/repo/pull/11",
        );

        let fallback = "implement user icon feature";
        let result =
            attempt_pr_creation(&f.ctx, fallback, "", "").unwrap_or_else(|e| panic!("{e:?}"));

        assert_eq!(
            result,
            PrAttemptOutcome::Created {
                url: f.url.clone(),
                commit_outcome: CommitOutcome::Created
            }
        );
        assert_eq!(
            git_stdout_ok(&f.ctx.path, &["log", "-1", "--pretty=%s"]),
            fallback,
            "commit subject should equal the fallback message when PR title is empty"
        );
        let gh_args = fs::read_to_string(&f.log_path).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            gh_args.contains("--fill"),
            "fake gh should receive --fill when PR title is empty; got: {gh_args}"
        );
        worktree::cleanup_worktree(&f.ctx).unwrap_or_else(|e| panic!("{e:?}"));
    }

    #[test]
    fn test_attempt_pr_creation_treats_whitespace_only_title_as_empty() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let f = setup_pr_test(
            &tmp,
            "20260310pr_title_commit_03",
            "https://github.com/owner/repo/pull/12",
        );

        let fallback = "implement user icon feature";
        let result =
            attempt_pr_creation(&f.ctx, fallback, "   ", "").unwrap_or_else(|e| panic!("{e:?}"));

        assert_eq!(
            result,
            PrAttemptOutcome::Created {
                url: f.url.clone(),
                commit_outcome: CommitOutcome::Created
            }
        );
        assert_eq!(
            git_stdout_ok(&f.ctx.path, &["log", "-1", "--pretty=%s"]),
            fallback,
            "whitespace-only title should be treated as empty and use fallback message"
        );
        let gh_args = fs::read_to_string(&f.log_path).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            gh_args.contains("--fill"),
            "fake gh should receive --fill when PR title is whitespace-only; got: {gh_args}"
        );
        worktree::cleanup_worktree(&f.ctx).unwrap_or_else(|e| panic!("{e:?}"));
    }

    // --- parse_pr_metadata tests ---

    #[test]
    fn test_parse_pr_metadata_standard_frontmatter() {
        // Given: standard frontmatter output from LLM
        let output = r#"---
title: "feat: Add user icon registration feature"
---
## Overview
Enabled users to upload icon images.

## Background
Previously, emojis were used as user icons."#;
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: title and body are extracted correctly
        assert_eq!(title, "feat: Add user icon registration feature");
        assert_eq!(
            body.trim(),
            "## Overview\nEnabled users to upload icon images.\n\n## Background\nPreviously, emojis were used as user icons."
        );
    }

    #[test]
    fn test_parse_pr_metadata_wrapped_in_markdown_code_block() {
        // Given: LLM output wrapped in ```md code block
        let output =
            "```md\n---\ntitle: \"fix: Resolve login bug\"\n---\nFixed the login issue.\n```";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: code block delimiters are stripped and content is parsed
        assert_eq!(title, "fix: Resolve login bug");
        assert_eq!(body.trim(), "Fixed the login issue.");
    }

    #[test]
    fn test_parse_pr_metadata_title_without_quotes() {
        // Given: frontmatter with unquoted title
        let output = "---\ntitle: feat: Add feature without quotes\n---\nBody text here.";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: title is extracted without quotes
        assert_eq!(title, "feat: Add feature without quotes");
        assert_eq!(body.trim(), "Body text here.");
    }

    #[test]
    fn test_parse_pr_metadata_no_frontmatter_returns_empty() {
        // Given: output without frontmatter delimiters
        let output = "This is just a plain text response without frontmatter.";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: both title and body are empty (caller falls back to session.input)
        assert_eq!(title, "");
        assert_eq!(body, "");
    }

    #[test]
    fn test_parse_pr_metadata_missing_title_field_returns_empty() {
        // Given: frontmatter without a title field
        let output = "---\nauthor: someone\n---\nBody without title.";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: both title and body are empty (fallback)
        assert_eq!(title, "");
        assert_eq!(body, "");
    }

    #[test]
    fn test_parse_pr_metadata_empty_body_after_frontmatter() {
        // Given: frontmatter with title but no body
        let output = "---\ntitle: \"chore: Update deps\"\n---\n";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: title is extracted, body is empty string
        assert_eq!(title, "chore: Update deps");
        assert_eq!(body.trim(), "");
    }

    #[test]
    fn test_parse_pr_metadata_only_closing_delimiter_missing_returns_empty() {
        // Given: frontmatter with only opening --- and no closing ---
        let output = "---\ntitle: \"feat: something\"\nBody without closing delimiter.";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: both title and body are empty (malformed frontmatter)
        assert_eq!(title, "");
        assert_eq!(body, "");
    }

    #[test]
    fn test_parse_pr_metadata_wrapped_in_plain_code_block() {
        // Given: LLM output wrapped in plain ``` (no language specifier)
        let output = "```\n---\ntitle: \"docs: Update README\"\n---\nUpdated documentation.\n```";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: code block is stripped and parsed correctly
        assert_eq!(title, "docs: Update README");
        assert_eq!(body.trim(), "Updated documentation.");
    }

    #[test]
    fn test_parse_pr_metadata_with_preamble_then_frontmatter() {
        // Given: LLM output with preamble text before frontmatter
        let output = "Here is the PR information:\n---\ntitle: \"feat: Add new feature\"\n---\nThis adds a new feature.";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: preamble is ignored, frontmatter is parsed
        assert_eq!(title, "feat: Add new feature");
        assert_eq!(body.trim(), "This adds a new feature.");
    }

    #[test]
    fn test_parse_pr_metadata_with_preamble_then_code_block() {
        // Given: LLM output with preamble then a code-block-wrapped frontmatter
        let output = "Here is the PR information:\n```md\n---\ntitle: \"fix: Fix the bug\"\n---\nFixed the critical bug.\n```";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: preamble and code block delimiters are stripped, frontmatter is parsed
        assert_eq!(title, "fix: Fix the bug");
        assert_eq!(body.trim(), "Fixed the critical bug.");
    }

    #[test]
    fn test_parse_pr_metadata_with_multiline_preamble() {
        // Given: LLM output with multiple lines of preamble
        let output = "I have reviewed the changes.\nBased on the commits, here is the PR:\n\n---\ntitle: \"refactor: Clean up code\"\n---\nRefactored the core module.";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: all preamble lines are skipped, frontmatter is parsed
        assert_eq!(title, "refactor: Clean up code");
        assert_eq!(body.trim(), "Refactored the core module.");
    }

    #[test]
    fn test_parse_pr_metadata_heading_format() {
        // Given: LLM output using Markdown h1 heading as title
        let output = "# feat: Add user icon registration feature\n## Overview\nEnabled users to upload icon images.";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: h1 line is used as title, rest as body
        assert_eq!(title, "feat: Add user icon registration feature");
        assert_eq!(
            body.trim(),
            "## Overview\nEnabled users to upload icon images."
        );
    }

    #[test]
    fn test_parse_pr_metadata_heading_format_in_code_block() {
        // Given: LLM output wrapped in code block using h1 heading
        let output = "```md\n# fix: Resolve login bug\nFixed the login issue.\n```";
        // When: parsing PR metadata
        let (title, body) = parse_pr_metadata(output);
        // Then: code block is stripped and h1 heading is used as title
        assert_eq!(title, "fix: Resolve login bug");
        assert_eq!(body.trim(), "Fixed the login issue.");
    }

    #[test]
    fn test_parse_pr_metadata_empty_input_returns_empty() {
        // Given: empty input
        let (title, body) = parse_pr_metadata("");
        // Then: both are empty
        assert_eq!(title, "");
        assert_eq!(body, "");
    }

    #[test]
    fn test_parse_pr_metadata_whitespace_only_returns_empty() {
        // Given: whitespace-only input
        let (title, body) = parse_pr_metadata("   \n  \n  ");
        // Then: both are empty
        assert_eq!(title, "");
        assert_eq!(body, "");
    }

    #[test]
    fn test_strip_code_block_with_preamble() {
        // Given: text with preamble before code block
        let input = "Some intro text\n```\n---\ntitle: test\n---\nbody\n```";
        // When: stripping code block
        let result = strip_code_block(input);
        // Then: inner content is extracted
        assert_eq!(result.trim(), "---\ntitle: test\n---\nbody");
    }

    #[test]
    fn test_strip_code_block_no_code_block_unchanged() {
        // Given: text without any code block
        let input = "Just plain text here.";
        // When: stripping code block
        let result = strip_code_block(input);
        // Then: input is returned unchanged (trimmed)
        assert_eq!(result, "Just plain text here.");
    }

    // -----------------------------------------------------------------------
    // run_all() integration tests
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn test_run_all_errors_when_session_and_all_both_specified() {
        // Given: both --all and a session ID are specified
        let args = RunArgs {
            session: Some("some-session-id".to_string()),
            all: true,
            max_retries: DEFAULT_MAX_RETRIES,
            rate_limit_retries: DEFAULT_RATE_LIMIT_RETRIES,
            dry_run: false,
        };

        // When: call run()
        let result = run(args).await;

        // Then: returns a "Cannot specify both --all and a session ID" error
        assert!(result.is_err(), "expected error but got Ok");
        let msg = result
            .map_or_else(|e| e, |v| panic!("expected Err, got Ok({v:?})"))
            .to_string();
        assert!(
            msg.contains("Cannot specify both --all and a session ID"),
            "unexpected error message: {msg}"
        );
    }

    #[tokio::test]
    async fn test_run_all_returns_ok_when_no_planned_sessions() {
        // Given: empty cruise home with no planned sessions
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let cruise_home = tmp.path().join(".cruise");
        std::fs::create_dir_all(cruise_home.join("sessions")).unwrap_or_else(|e| panic!("{e:?}"));

        // Hold the lock in a narrow scope so it is dropped before the await.
        let orig_home = {
            let _guard = crate::test_support::lock_process();
            let orig = std::env::var("HOME").ok();
            // SAFETY: only modified within this test and restored before exit.
            unsafe {
                std::env::set_var("HOME", tmp.path());
            }
            orig
            // _guard is dropped here
        };

        let args = RunArgs {
            session: None,
            all: true,
            max_retries: DEFAULT_MAX_RETRIES,
            rate_limit_retries: DEFAULT_RATE_LIMIT_RETRIES,
            dry_run: false,
        };

        // When: call run() with 0 planned sessions
        let result = run(args).await;

        // Restore HOME
        {
            let _guard = crate::test_support::lock_process();
            unsafe {
                match orig_home {
                    Some(h) => std::env::set_var("HOME", h),
                    None => std::env::remove_var("HOME"),
                }
            }
        }

        // Then: returns Ok(()) without error
        assert!(result.is_ok(), "expected Ok but got: {:?}", result.err());
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_mode_keeps_changes_in_base_repo_and_skips_pr_flow() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260309120000";
        let session = make_current_branch_session(session_id, &repo, "edit in place", "main");
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf direct > current-branch.txt"),
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/99");
        process.prepend_path(&bin_dir);

        let result = run(run_args(session_id)).await;
        assert!(
            result.is_ok(),
            "expected current-branch mode to succeed: {result:?}"
        );

        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            loaded.worktree_path.is_none(),
            "current-branch mode should not persist a worktree path"
        );
        assert!(
            loaded.worktree_branch.is_none(),
            "current-branch mode should not persist a worktree branch"
        );
        assert!(
            loaded.pr_url.is_none(),
            "current-branch mode should skip PR creation"
        );
        assert!(
            repo.join("current-branch.txt").exists(),
            "current-branch mode should write changes into the base repository"
        );
        assert_eq!(
            fs::read_to_string(repo.join("current-branch.txt")).unwrap_or_else(|e| panic!("{e:?}")),
            "direct"
        );
        assert!(
            git_stdout_ok(&repo, &["status", "--short"]).contains("current-branch.txt"),
            "current-branch mode should leave the new file uncommitted in the base repository"
        );
        assert!(
            !manager.worktrees_dir().join(session_id).exists(),
            "current-branch mode should not create a cruise worktree directory"
        );
        assert!(
            !gh_log.exists(),
            "current-branch mode should not invoke gh at all"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_mode_errors_when_branch_has_changed() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260309121000";
        let session =
            make_current_branch_session(session_id, &repo, "stay on planned branch", "main");
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf nope > wrong-branch.txt"),
        );

        run_git_ok(&repo, &["checkout", "-b", "other-branch"]);

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/100");
        process.prepend_path(&bin_dir);

        let result = run(run_args(session_id)).await;
        assert!(
            result.is_err(),
            "expected current-branch mode to reject a branch mismatch"
        );
        let message = result
            .map_or_else(|e| e, |v| panic!("expected Err, got Ok({v:?})"))
            .to_string();
        assert!(message.contains("branch"), "unexpected error: {message}");
        assert!(message.contains("main"), "unexpected error: {message}");
        assert!(
            message.contains("other-branch"),
            "unexpected error: {message}"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_mode_errors_when_working_tree_is_dirty() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260309122000";
        let session = make_current_branch_session(session_id, &repo, "edit dirty tree", "main");
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf more > new-file.txt"),
        );

        fs::write(repo.join("already-dirty.txt"), "dirty").unwrap_or_else(|e| panic!("{e:?}"));

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/101");
        process.prepend_path(&bin_dir);

        let result = run(run_args(session_id)).await;
        assert!(
            result.is_err(),
            "expected current-branch mode to reject a dirty working tree"
        );
        let message = result
            .map_or_else(|e| e, |v| panic!("expected Err, got Ok({v:?})"))
            .to_string();
        assert!(message.contains("dirty"), "unexpected error: {message}");
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_mode_errors_on_detached_head() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260309123000";
        let session = make_current_branch_session(session_id, &repo, "edit detached head", "main");
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf nope > detached.txt"),
        );

        run_git_ok(&repo, &["checkout", "--detach"]);

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/102");
        process.prepend_path(&bin_dir);

        let result = run(run_args(session_id)).await;
        assert!(
            result.is_err(),
            "expected current-branch mode to reject detached HEAD"
        );
        let message = result
            .map_or_else(|e| e, |v| panic!("expected Err, got Ok({v:?})"))
            .to_string();
        assert!(message.contains("detached"), "unexpected error: {message}");
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_mode_resumes_from_saved_step_without_pr_flow() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260309124000";
        let mut session = make_current_branch_session(session_id, &repo, "resume in place", "main");
        session.phase = SessionPhase::Running;
        session.current_step = Some("second".to_string());
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            r"command:
  - cat
steps:
  first:
    command: |
      printf first > first.txt
  second:
    command: |
      printf second > second.txt
",
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/103");
        process.prepend_path(&bin_dir);

        let result = run(run_args(session_id)).await;
        assert!(
            result.is_ok(),
            "expected current-branch resume to succeed: {result:?}"
        );

        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            loaded.worktree_path.is_none(),
            "current-branch resume should not persist a worktree path"
        );
        assert!(
            loaded.pr_url.is_none(),
            "current-branch resume should skip PR creation"
        );
        assert!(
            !repo.join("first.txt").exists(),
            "resume should continue from the saved step instead of rerunning earlier steps"
        );
        assert!(
            repo.join("second.txt").exists(),
            "resume should execute the saved current step in the base repository"
        );
        assert!(
            !gh_log.exists(),
            "current-branch resume should not invoke gh"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_conflict_overwrite_continues_and_logs_choice() {
        // Given: a running current-branch session whose state.json is edited externally mid-run
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let session_id = "20260310140000";
        let (mut process, repo, manager) =
            setup_current_branch_conflict_session(&tmp, session_id, "overwrite external state");
        let log_path = tmp.path().join("conflict-overwrite.log");
        configure_conflict_test_env(&mut process, true, Some("overwrite"), &log_path);

        // When: the run reaches the next step after an external edit and the user chooses overwrite
        let run_fut = run(run_args(session_id));
        let mutate_fut = mutate_state_after_first_step(
            &manager,
            session_id,
            |_| repo.clone(),
            write_external_failed_state,
        );
        let (result, ()) = tokio::join!(run_fut, mutate_fut);

        // Then: the run completes using the in-memory state and records the conflict decision
        assert!(
            result.is_ok(),
            "overwrite choice should allow the run to continue: {result:?}"
        );
        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(matches!(loaded.phase, SessionPhase::Completed));
        assert_eq!(loaded.current_step.as_deref(), Some("second"));
        assert!(repo.join("second.txt").exists());
        let log = fs::read_to_string(&log_path).unwrap_or_else(|e| {
            panic!("conflict resolution should be logged for overwrite tests: {e:?}")
        });
        assert!(
            log.contains("overwrite"),
            "expected overwrite decision in log, got: {log}"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_conflict_abort_preserves_external_state() {
        // Given: a running current-branch session whose state.json is edited externally mid-run
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let session_id = "20260310140001";
        let (mut process, repo, manager) =
            setup_current_branch_conflict_session(&tmp, session_id, "abort on conflict");
        let log_path = tmp.path().join("conflict-abort.log");
        configure_conflict_test_env(&mut process, true, Some("abort"), &log_path);

        // When: the run reaches the next step after an external edit and the user chooses abort
        let run_fut = run(run_args(session_id));
        let mutate_fut = mutate_state_after_first_step(
            &manager,
            session_id,
            |_| repo.clone(),
            write_external_failed_state,
        );
        let (result, ()) = tokio::join!(run_fut, mutate_fut);

        // Then: the run stops, leaves the external state untouched, and does not execute later steps
        match result {
            Err(CruiseError::SessionStateConflictAborted(message)) => {
                assert!(
                    message.contains("state.json"),
                    "abort message should mention state.json: {message}"
                );
            }
            other => panic!("expected SessionStateConflictAborted, got {other:?}"),
        }
        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert_eq!(loaded.current_step.as_deref(), Some("external-step"));
        assert!(matches!(
            loaded.phase,
            SessionPhase::Failed(ref message) if message == "external edit"
        ));
        assert!(
            !repo.join("second.txt").exists(),
            "aborting on conflict should prevent later steps from running"
        );
        let log = fs::read_to_string(&log_path).unwrap_or_else(|e| {
            panic!("conflict resolution should be logged for abort tests: {e:?}")
        });
        assert!(
            log.contains("abort"),
            "expected abort decision in log, got: {log}"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_conflict_noninteractive_returns_error_without_prompt() {
        // Given: a running session with an external state edit and stdin treated as non-terminal
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let session_id = "20260310140002";
        let (mut process, repo, manager) =
            setup_current_branch_conflict_session(&tmp, session_id, "noninteractive conflict");
        let log_path = tmp.path().join("conflict-noninteractive.log");
        configure_conflict_test_env(&mut process, false, None, &log_path);

        // When: the run hits the conflicting save point in noninteractive mode
        let run_fut = run(run_args(session_id));
        let mutate_fut = mutate_state_after_first_step(
            &manager,
            session_id,
            |_| repo.clone(),
            write_external_failed_state,
        );
        let (result, ()) = tokio::join!(run_fut, mutate_fut);

        // Then: the run errors immediately and preserves the externally edited state
        match result {
            Err(CruiseError::SessionStateConflict(message)) => {
                assert!(
                    message.contains("state.json"),
                    "noninteractive conflict should mention state.json: {message}"
                );
            }
            other => panic!("expected SessionStateConflict, got {other:?}"),
        }
        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert_eq!(loaded.current_step.as_deref(), Some("external-step"));
        assert!(matches!(
            loaded.phase,
            SessionPhase::Failed(ref message) if message == "external edit"
        ));
        assert!(
            !repo.join("second.txt").exists(),
            "noninteractive conflicts should stop before later steps run"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_conflict_abort_preserves_invalid_state_file() {
        // Given: a running session whose state.json becomes invalid JSON before the next save
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let session_id = "20260310140003";
        let (mut process, repo, manager) =
            setup_current_branch_conflict_session(&tmp, session_id, "invalid json conflict");
        let log_path = tmp.path().join("conflict-invalid-json.log");
        configure_conflict_test_env(&mut process, true, Some("abort"), &log_path);

        // When: the run reaches the conflicting save point and the user aborts
        let run_fut = run(run_args(session_id));
        let mutate_fut = mutate_state_after_first_step(
            &manager,
            session_id,
            |_| repo.clone(),
            |manager, id| {
                fs::write(manager.state_path(id), "{invalid json")
                    .unwrap_or_else(|e| panic!("{e:?}"));
            },
        );
        let (result, ()) = tokio::join!(run_fut, mutate_fut);

        // Then: the invalid external file is preserved and later steps do not run
        match result {
            Err(CruiseError::SessionStateConflictAborted(message)) => {
                assert!(
                    message.contains("state.json"),
                    "abort message should mention state.json: {message}"
                );
            }
            other => panic!("expected SessionStateConflictAborted, got {other:?}"),
        }
        assert_eq!(
            fs::read_to_string(manager.state_path(session_id)).unwrap_or_else(|e| panic!("{e:?}")),
            "{invalid json"
        );
        assert!(
            !repo.join("second.txt").exists(),
            "aborting on invalid external JSON should stop before later steps run"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_current_branch_conflict_noninteractive_preserves_missing_state_file() {
        // Given: a running session whose state.json is deleted before the next save
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let session_id = "20260310140004";
        let (mut process, repo, manager) =
            setup_current_branch_conflict_session(&tmp, session_id, "missing state conflict");
        let log_path = tmp.path().join("conflict-missing.log");
        configure_conflict_test_env(&mut process, false, None, &log_path);

        // When: the run reaches the conflicting save point in noninteractive mode
        let run_fut = run(run_args(session_id));
        let mutate_fut = mutate_state_after_first_step(
            &manager,
            session_id,
            |_| repo.clone(),
            |manager, id| {
                fs::remove_file(manager.state_path(id)).unwrap_or_else(|e| panic!("{e:?}"));
            },
        );
        let (result, ()) = tokio::join!(run_fut, mutate_fut);

        // Then: the run returns a conflict error and leaves the file deleted
        match result {
            Err(CruiseError::SessionStateConflict(message)) => {
                assert!(
                    message.contains("state.json"),
                    "missing-file conflict should mention state.json: {message}"
                );
            }
            other => panic!("expected SessionStateConflict, got {other:?}"),
        }
        assert!(
            !manager.state_path(session_id).exists(),
            "noninteractive conflict should preserve the missing state file"
        );
        assert!(
            !repo.join("second.txt").exists(),
            "noninteractive missing-file conflicts should stop before later steps run"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_all_forces_worktree_even_for_current_branch_sessions() {
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260309125000";
        let session = make_current_branch_session(session_id, &repo, "batch run", "main");
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf batch > run-all.txt"),
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/104");
        process.prepend_path(&bin_dir);

        let result = run(RunArgs {
            session: None,
            all: true,
            max_retries: 10,
            rate_limit_retries: 0,
            dry_run: false,
        })
        .await;
        assert!(result.is_ok(), "expected run --all to succeed: {result:?}");

        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            loaded.worktree_path.is_some(),
            "run --all should still use a worktree execution path"
        );
        assert!(
            loaded.worktree_branch.is_some(),
            "run --all should assign a worktree branch"
        );
        assert_eq!(
            loaded.pr_url.as_deref(),
            Some("https://github.com/owner/repo/pull/104"),
            "run --all should keep the existing PR flow"
        );
        assert!(
            !repo.join("run-all.txt").exists(),
            "run --all should not write changes into the base repository"
        );
        assert!(
            manager
                .worktrees_dir()
                .join(session_id)
                .join("run-all.txt")
                .exists(),
            "run --all should write changes inside the session worktree"
        );
        let gh_log_contents = fs::read_to_string(&gh_log).unwrap_or_default();
        assert!(
            gh_log_contents.contains("pr create --head"),
            "run --all should still invoke PR creation through gh"
        );
        assert!(
            gh_log_contents.contains("--draft"),
            "gh pr create should include --draft flag, got: {gh_log_contents}"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_all_preserves_invalid_external_state_without_failing_summary_reload() {
        // Given: a planned session that will abort on a state.json conflict and leave invalid JSON
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260310140005";
        let mut session = SessionState::new(
            session_id.to_string(),
            repo.clone(),
            "cruise.yaml".to_string(),
            "run all conflict".to_string(),
        );
        session.phase = SessionPhase::Planned;
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(&manager, session_id, &blocking_conflict_config());

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/105");
        process.prepend_path(&bin_dir);

        let log_path = tmp.path().join("run-all-conflict.log");
        configure_conflict_test_env(&mut process, true, Some("abort"), &log_path);

        // When: run --all hits the conflict, aborts that session, and tries to build its summary
        let run_fut = run(RunArgs {
            session: None,
            all: true,
            max_retries: 10,
            rate_limit_retries: 0,
            dry_run: false,
        });
        let mutate_fut = mutate_state_after_first_step(
            &manager,
            session_id,
            |state| {
                state.worktree_path.clone().unwrap_or_else(|| {
                    panic!("run --all should persist a worktree path before step execution")
                })
            },
            |manager, id| {
                fs::write(manager.state_path(id), "{invalid json")
                    .unwrap_or_else(|e| panic!("{e:?}"));
            },
        );
        let (result, ()) = tokio::join!(run_fut, mutate_fut);

        // Then: run --all still returns Ok and leaves the invalid external file untouched
        assert!(
            result.is_ok(),
            "run --all should not fail when summary reload sees preserved invalid state: {result:?}"
        );
        assert_eq!(
            fs::read_to_string(manager.state_path(session_id)).unwrap_or_else(|e| panic!("{e:?}")),
            "{invalid json"
        );
        assert!(
            !gh_log.exists(),
            "the aborted conflict session should not reach PR creation"
        );
    }

    // -----------------------------------------------------------------------
    // format_run_all_summary() unit tests
    // -----------------------------------------------------------------------

    fn make_session(input: &str, phase: SessionPhase, pr_url: Option<&str>) -> SessionState {
        let mut s = SessionState::new(
            "20260101000000".to_string(),
            std::path::PathBuf::from("/tmp"),
            "test.yaml".to_string(),
            input.to_string(),
        );
        s.phase = phase;
        s.pr_url = pr_url.map(str::to_string);
        s
    }

    // -----------------------------------------------------------------------
    // format_run_all_summary -- Suspended
    // -----------------------------------------------------------------------

    #[test]
    fn test_format_run_all_summary_suspended_session_shows_suspended_indicator() {
        // Given: a Suspended session is included in the results
        let results = vec![make_session("add feature", SessionPhase::Suspended, None)];

        // When
        let summary = format_run_all_summary(&results);
        let summary_plain = console::strip_ansi_codes(&summary).to_string();

        // Then: the session's input and "Suspended" marker are included
        assert!(
            summary_plain.contains("add feature"),
            "summary should contain input: {summary_plain}"
        );
        assert!(
            summary_plain.contains("Suspended"),
            "summary should contain Suspended indicator: {summary_plain}"
        );
        assert!(
            !summary_plain.is_empty(),
            "summary should not be empty: {summary_plain}"
        );
    }

    #[test]
    fn test_format_run_all_summary_mixed_with_suspended() {
        // Given: mixed results of Completed, Suspended, and Failed
        let results = vec![
            make_session(
                "task a",
                SessionPhase::Completed,
                Some("https://github.com/org/repo/pull/1"),
            ),
            make_session("task b", SessionPhase::Suspended, None),
            make_session(
                "task c",
                SessionPhase::Failed("build error".to_string()),
                None,
            ),
        ];

        // When
        let summary = format_run_all_summary(&results);
        let summary_plain = console::strip_ansi_codes(&summary).to_string();

        // Then: information for all 3 sessions is included and the header count is correct
        assert!(
            summary_plain.contains("task a"),
            "summary should contain task a: {summary_plain}"
        );
        assert!(
            summary_plain.contains("task b"),
            "summary should contain task b: {summary_plain}"
        );
        assert!(
            summary_plain.contains("task c"),
            "summary should contain task c: {summary_plain}"
        );
        assert!(
            summary_plain.contains("=== Run All Summary (3 sessions) ==="),
            "header should show correct count: {summary_plain}"
        );
        assert!(
            summary_plain.contains("Suspended"),
            "summary should distinguish Suspended from Failed: {summary_plain}"
        );
        assert!(
            summary_plain.contains("Failed"),
            "summary should show Failed reason: {summary_plain}"
        );
    }

    #[test]
    fn test_format_run_all_summary_empty_returns_empty_string() {
        // Given: empty result list
        let results: Vec<SessionState> = vec![];

        // When
        let summary = format_run_all_summary(&results);

        // Then: returns empty string
        assert!(
            summary.is_empty(),
            "expected empty string, got: {summary:?}"
        );
    }

    #[test]
    fn test_format_run_all_summary_single_completed_with_pr() {
        // Given: Completed session with PR URL
        let results = vec![make_session(
            "add login feature",
            SessionPhase::Completed,
            Some("https://github.com/org/repo/pull/42"),
        )];

        // When
        let summary = format_run_all_summary(&results);

        // Then: summary contains input and PR URL
        assert!(
            summary.contains("add login feature"),
            "summary should contain input: {summary}"
        );
        assert!(
            summary.contains("https://github.com/org/repo/pull/42"),
            "summary should contain PR URL: {summary}"
        );
        assert!(
            !summary.contains("Failed") && !summary.contains('x'),
            "completed session should not show failure: {summary}"
        );
    }

    #[test]
    fn test_format_run_all_summary_single_completed_without_pr() {
        // Given: Completed session without PR URL
        let results = vec![make_session(
            "refactor database layer",
            SessionPhase::Completed,
            None,
        )];

        // When
        let summary = format_run_all_summary(&results);

        // Then: summary contains input without failure indicators
        assert!(
            summary.contains("refactor database layer"),
            "summary should contain input: {summary}"
        );
        assert!(
            !summary.contains("Failed") && !summary.contains('x'),
            "completed session should not show failure: {summary}"
        );
    }

    #[test]
    fn test_format_run_all_summary_single_failed_session() {
        // Given: Failed session with an error message
        let results = vec![make_session(
            "fix login bug",
            SessionPhase::Failed("CI timeout".to_string()),
            None,
        )];

        // When
        let summary = format_run_all_summary(&results);

        // Then: summary contains input, failure indicator and error message
        assert!(
            summary.contains("fix login bug"),
            "summary should contain input: {summary}"
        );
        assert!(
            summary.contains("Failed: CI timeout"),
            "summary should contain failure prefix and error message: {summary}"
        );
    }

    #[test]
    fn test_format_run_all_summary_mixed_results() {
        // Given: mixed Completed and Failed sessions
        let results = vec![
            make_session(
                "add auth module",
                SessionPhase::Completed,
                Some("https://github.com/org/repo/pull/10"),
            ),
            make_session(
                "fix broken test",
                SessionPhase::Failed("build error".to_string()),
                None,
            ),
        ];

        // When
        let summary = format_run_all_summary(&results);

        // Then: summary contains info for both sessions
        assert!(
            summary.contains("add auth module"),
            "summary should contain first input: {summary}"
        );
        assert!(
            summary.contains("https://github.com/org/repo/pull/10"),
            "summary should contain PR URL: {summary}"
        );
        assert!(
            summary.contains("fix broken test"),
            "summary should contain second input: {summary}"
        );
        assert!(
            summary.contains("Failed: build error"),
            "summary should contain failure prefix and error message for second session: {summary}"
        );
    }

    #[test]
    fn test_format_run_all_summary_mixed_with_completed_no_pr() {
        // Given: 3 sessions -- success with PR, completed without PR, and explicit failure
        let results = vec![
            make_session(
                "add auth module",
                SessionPhase::Completed,
                Some("https://github.com/org/repo/pull/10"),
            ),
            make_session("refactor cache layer", SessionPhase::Completed, None),
            make_session(
                "fix broken test",
                SessionPhase::Failed("CI timeout".to_string()),
                None,
            ),
        ];

        // When
        let summary = format_run_all_summary(&results);

        // Then: first session shows success with PR URL
        assert!(
            summary.contains("add auth module"),
            "summary should contain first session: {summary}"
        );
        assert!(
            summary.contains("https://github.com/org/repo/pull/10"),
            "summary should show PR URL for success: {summary}"
        );

        // Then: second completed session remains a success even without PR URL
        assert!(
            summary.contains("refactor cache layer"),
            "summary should contain second session: {summary}"
        );
        let refactor_line = summary
            .lines()
            .find(|l| l.contains("refactor cache layer"))
            .unwrap_or_else(|| panic!("refactor cache layer line not found in summary"));
        assert!(
            !refactor_line.contains("Failed") && !refactor_line.contains('x'),
            "completed session should not show failure, got: {refactor_line:?}"
        );

        // Then: third session shows failure prefix and error message
        let failed_line = summary
            .lines()
            .find(|l| l.contains("fix broken test"))
            .unwrap_or_else(|| panic!("fix broken test line not found in summary"));
        assert!(
            failed_line.contains("Failed: CI timeout"),
            "failed session should show failure prefix and error message, got: {failed_line:?}"
        );
    }

    #[test]
    fn test_format_run_all_summary_long_input_is_truncated() {
        // Given: completed session with a very long input
        let long_input = "a".repeat(200);
        let results = vec![make_session(&long_input, SessionPhase::Completed, None)];

        // When
        let summary = format_run_all_summary(&results);

        // Then: each summary line is within a reasonable length (300 chars max)
        for line in summary.lines() {
            assert!(
                line.chars().count() <= 300,
                "line too long ({} chars): {line}",
                line.chars().count()
            );
        }
    }

    // -----------------------------------------------------------------------
    // apply_run_result_to_session() integration tests
    // These test the finalization logic across engine -> run_cmd -> session.
    // -----------------------------------------------------------------------

    #[test]
    fn test_apply_run_result_completed_sets_completed_phase() {
        // Given: a Running session and a successful result
        let mut session = make_session("some task", SessionPhase::Running, None);
        // When: applying a successful result
        apply_run_result_to_session(&mut session, &Ok(()));
        // Then: session phase becomes Completed
        assert!(
            matches!(session.phase, SessionPhase::Completed),
            "Expected Completed, got {:?}",
            session.phase
        );
    }

    #[test]
    fn test_apply_run_result_step_paused_keeps_running_phase() {
        // Given: a Running session and a StepPaused error
        // (StepPaused means user pressed Esc -- session should be resumable)
        let mut session = make_session("some task", SessionPhase::Running, None);
        // When: applying StepPaused
        apply_run_result_to_session(&mut session, &Err(CruiseError::StepPaused));
        // Then: session stays Running so it can be resumed later
        assert!(
            matches!(session.phase, SessionPhase::Running),
            "Expected Running after StepPaused, got {:?}",
            session.phase
        );
    }

    #[test]
    fn test_apply_run_result_other_error_sets_failed_phase() {
        // Given: a Running session and a generic error
        let mut session = make_session("some task", SessionPhase::Running, None);
        // When: applying a generic command error
        apply_run_result_to_session(
            &mut session,
            &Err(CruiseError::CommandError("some failure".to_string())),
        );
        // Then: session phase becomes Failed
        assert!(
            matches!(session.phase, SessionPhase::Failed(_)),
            "Expected Failed, got {:?}",
            session.phase
        );
    }

    #[test]
    fn test_apply_run_result_step_paused_does_not_set_completed_at() {
        // Given: a session and StepPaused
        let mut session = make_session("some task", SessionPhase::Running, None);
        // When: applying StepPaused
        apply_run_result_to_session(&mut session, &Err(CruiseError::StepPaused));
        // Then: completed_at is not set (session is not finished)
        assert!(
            session.completed_at.is_none(),
            "completed_at should not be set on pause"
        );
    }

    #[test]
    fn test_apply_run_result_completed_sets_completed_at() {
        // Given: a Running session
        let mut session = make_session("some task", SessionPhase::Running, None);
        // When: applying success
        apply_run_result_to_session(&mut session, &Ok(()));
        // Then: completed_at is recorded
        assert!(
            session.completed_at.is_some(),
            "completed_at should be set on completion"
        );
    }

    #[test]
    fn test_apply_run_result_failed_sets_completed_at() {
        // Given: a Running session
        let mut session = make_session("some task", SessionPhase::Running, None);
        // When: applying a fatal error
        apply_run_result_to_session(&mut session, &Err(CruiseError::Other("fatal".to_string())));
        // Then: completed_at is recorded
        assert!(
            session.completed_at.is_some(),
            "completed_at should be set on failure"
        );
    }

    // -- prompt_workspace_mode unit tests --------------------------------

    #[test]
    fn test_prompt_workspace_mode_returns_worktree_when_noninteractive() {
        // Given: stdin is not a terminal
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        process.set_env(TEST_STDIN_IS_TERMINAL_ENV, "0");
        process.remove_env(TEST_WORKSPACE_MODE_ENV);

        // When: prompt_workspace_mode is called in non-interactive environment
        let result = prompt_workspace_mode();

        // Then: returns Worktree without showing a prompt
        assert_eq!(
            result.unwrap_or_else(|e| panic!("{e:?}")),
            WorkspaceMode::Worktree,
            "non-interactive should default to Worktree"
        );
    }

    #[test]
    fn test_prompt_workspace_mode_returns_current_branch_via_test_env() {
        // Given: test env var selects current_branch
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        process.set_env(TEST_WORKSPACE_MODE_ENV, "current_branch");

        // When: prompt_workspace_mode is called
        let result = prompt_workspace_mode();

        // Then: returns CurrentBranch as the env var dictates
        assert_eq!(
            result.unwrap_or_else(|e| panic!("{e:?}")),
            WorkspaceMode::CurrentBranch,
            "env override should select CurrentBranch"
        );
    }

    #[test]
    fn test_prompt_workspace_mode_returns_worktree_via_test_env() {
        // Given: test env var selects worktree
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        process.set_env(TEST_WORKSPACE_MODE_ENV, "worktree");

        // When: prompt_workspace_mode is called
        let result = prompt_workspace_mode();

        // Then: returns Worktree as the env var dictates
        assert_eq!(
            result.unwrap_or_else(|e| panic!("{e:?}")),
            WorkspaceMode::Worktree,
            "env override should select Worktree"
        );
    }

    // -- run_single workspace mode selection integration tests ------------

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_single_prompts_on_fresh_default_session_and_selects_current_branch() {
        // Given: a fresh session with default workspace_mode (Worktree) and no current_step
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);
        // prompt resolves to current_branch via test env
        process.set_env(TEST_STDIN_IS_TERMINAL_ENV, "1");
        process.set_env(TEST_WORKSPACE_MODE_ENV, "current_branch");

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260321091000";
        let mut session = SessionState::new(
            session_id.to_string(),
            repo.clone(),
            "cruise.yaml".to_string(),
            "run in place".to_string(),
        );
        session.phase = SessionPhase::Planned;
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf in-place > in-place.txt"),
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/300");
        process.prepend_path(&bin_dir);

        // When: run is called on the fresh session
        let result = run(run_args(session_id)).await;

        // Then: run succeeds and changes land in the base repo (not a worktree)
        assert!(
            result.is_ok(),
            "expected run to succeed when prompt selects current_branch: {result:?}"
        );
        assert!(
            repo.join("in-place.txt").exists(),
            "current_branch selection should write changes into the base repository"
        );
        assert!(
            !manager.worktrees_dir().join(session_id).exists(),
            "current_branch selection should not create a worktree directory"
        );
        assert!(
            !gh_log.exists(),
            "current_branch selection should not invoke gh"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_single_saves_workspace_mode_and_target_branch_after_prompt_selects_current_branch()
     {
        // Given: a fresh session with default workspace_mode (Worktree)
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);
        process.set_env(TEST_STDIN_IS_TERMINAL_ENV, "1");
        process.set_env(TEST_WORKSPACE_MODE_ENV, "current_branch");

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260321091001";
        let mut session = SessionState::new(
            session_id.to_string(),
            repo.clone(),
            "cruise.yaml".to_string(),
            "save mode test".to_string(),
        );
        session.phase = SessionPhase::Planned;
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf mode > mode.txt"),
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/301");
        process.prepend_path(&bin_dir);

        // When: run completes
        run(run_args(session_id))
            .await
            .unwrap_or_else(|e| panic!("{e:?}"));

        // Then: the persisted session state reflects the chosen mode and target branch
        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert_eq!(
            loaded.workspace_mode,
            WorkspaceMode::CurrentBranch,
            "workspace_mode should be persisted as CurrentBranch after prompt selection"
        );
        assert_eq!(
            loaded.target_branch.as_deref(),
            Some("main"),
            "target_branch should be set to the current branch at run time"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_single_does_not_prompt_when_session_already_has_current_branch_mode() {
        // Given: a session with workspace_mode=CurrentBranch already set (no current_step)
        // TEST_WORKSPACE_MODE_ENV is intentionally absent -- if prompt were called, it would hang
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);
        process.set_env(TEST_STDIN_IS_TERMINAL_ENV, "1");
        process.remove_env(TEST_WORKSPACE_MODE_ENV);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260321091002";
        let session =
            make_current_branch_session(session_id, &repo, "already current branch", "main");
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf skip > skip-prompt.txt"),
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/302");
        process.prepend_path(&bin_dir);

        // When: run is called
        let result = run(run_args(session_id)).await;

        // Then: run succeeds in the base repo without prompting
        assert!(
            result.is_ok(),
            "expected run to succeed without prompting: {result:?}"
        );
        assert!(
            repo.join("skip-prompt.txt").exists(),
            "already-CurrentBranch session should write changes into the base repository"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_single_does_not_prompt_when_resuming_saved_current_branch_session() {
        // Given: a session being resumed (current_step is Some) in CurrentBranch mode
        // TEST_WORKSPACE_MODE_ENV is absent -- if prompt were called, it would hang
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);
        process.set_env(TEST_STDIN_IS_TERMINAL_ENV, "1");
        process.remove_env(TEST_WORKSPACE_MODE_ENV);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260321091003";
        let mut session =
            make_current_branch_session(session_id, &repo, "resume no prompt", "main");
        session.phase = SessionPhase::Running;
        session.current_step = Some("second".to_string());
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            r"command:
  - cat
steps:
  first:
    command: |
      printf first > first.txt
  second:
    command: |
      printf second > second.txt
",
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/303");
        process.prepend_path(&bin_dir);

        // When: run is called to resume from second step
        let result = run(run_args(session_id)).await;

        // Then: run continues from the saved step without prompting
        assert!(
            result.is_ok(),
            "expected resume to succeed without prompting: {result:?}"
        );
        assert!(
            !repo.join("first.txt").exists(),
            "resume should skip already-executed earlier steps"
        );
        assert!(
            repo.join("second.txt").exists(),
            "resume should execute from the saved current step"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_single_defaults_to_worktree_when_stdin_is_not_terminal() {
        // Given: a fresh session with default workspace_mode (Worktree) and non-interactive stdin
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let mut process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);
        process.set_env(TEST_STDIN_IS_TERMINAL_ENV, "0");
        process.remove_env(TEST_WORKSPACE_MODE_ENV);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));
        let session_id = "20260321091004";
        let mut session = SessionState::new(
            session_id.to_string(),
            repo.clone(),
            "cruise.yaml".to_string(),
            "default to worktree".to_string(),
        );
        session.phase = SessionPhase::Planned;
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        write_config(
            &manager,
            session_id,
            &single_command_config("edit", "printf wt > wt.txt"),
        );

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/304");
        process.prepend_path(&bin_dir);

        // When: run is called in non-interactive mode
        let result = run(run_args(session_id)).await;

        // Then: run succeeds using worktree mode (non-interactive defaults to safe Worktree)
        assert!(
            result.is_ok(),
            "expected run to succeed in worktree mode: {result:?}"
        );
        let loaded = manager.load(session_id).unwrap_or_else(|e| panic!("{e:?}"));
        assert_eq!(
            loaded.workspace_mode,
            WorkspaceMode::Worktree,
            "non-interactive stdin should default to Worktree mode"
        );
        assert!(
            !repo.join("wt.txt").exists(),
            "worktree mode should not write changes into the base repository"
        );
        assert!(
            manager
                .worktrees_dir()
                .join(session_id)
                .join("wt.txt")
                .exists(),
            "worktree mode should write changes into the session worktree"
        );
    }

    #[tokio::test(flavor = "current_thread")]
    async fn test_run_all_picks_up_session_added_while_first_session_is_running() {
        // Given: one Planned session with a blocking first step (blocks until proceed.txt exists)
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let process = ProcessStateGuard::new(tmp.path());
        let repo = create_repo_with_origin(&tmp);
        process.set_current_dir(&repo);

        let manager = SessionManager::new(get_cruise_home().unwrap_or_else(|e| panic!("{e:?}")));

        let session_id_1 = "20260403400000";
        let session_id_2 = "20260403400001"; // added mid-run -- newer ID

        let mut session_1 = SessionState::new(
            session_id_1.to_string(),
            repo.clone(),
            "cruise.yaml".to_string(),
            "first task".to_string(),
        );
        session_1.phase = SessionPhase::Planned;
        manager
            .create(&session_1)
            .unwrap_or_else(|e| panic!("{e:?}"));
        write_config(&manager, session_id_1, &blocking_conflict_config());

        let bin_dir = tmp.path().join("bin");
        let gh_log = tmp.path().join("gh.log");
        install_logging_gh(&bin_dir, &gh_log, "https://github.com/owner/repo/pull/201");
        process.prepend_path(&bin_dir);

        // When: run --all starts. Concurrently, add session_2 once session_1 is blocking.
        let run_fut = run(RunArgs {
            session: None,
            all: true,
            max_retries: 10,
            rate_limit_retries: 0,
            dry_run: false,
        });

        let add_and_unblock_fut = async {
            // Wait for session_1 to reach its blocking "first" step
            wait_for_session_step(&manager, session_id_1, "first").await;

            // Add session_2 as a Planned session with a simple command
            let mut session_2 = SessionState::new(
                session_id_2.to_string(),
                repo.clone(),
                "cruise.yaml".to_string(),
                "second task added mid-run".to_string(),
            );
            session_2.phase = SessionPhase::Planned;
            manager
                .create(&session_2)
                .unwrap_or_else(|e| panic!("{e:?}"));
            write_config(
                &manager,
                session_id_2,
                &single_command_config("do", "printf done2 > session2-output.txt"),
            );

            // Unblock session_1 by writing proceed.txt in its worktree
            let state_1 = manager
                .load(session_id_1)
                .unwrap_or_else(|e| panic!("{e:?}"));
            let worktree = state_1
                .worktree_path
                .clone()
                .unwrap_or_else(|| panic!("session_1 should have worktree_path set when Running"));
            fs::write(worktree.join("proceed.txt"), "go").unwrap_or_else(|e| panic!("{e:?}"));
        };

        let (result, ()) = tokio::join!(run_fut, add_and_unblock_fut);

        // Then: run --all completes without error
        assert!(
            result.is_ok(),
            "run --all should succeed even when a session is added mid-run: {result:?}"
        );

        // And: session_2 was also executed and completed
        let state_2 = manager
            .load(session_id_2)
            .unwrap_or_else(|e| panic!("{e:?}"));
        assert!(
            matches!(state_2.phase, SessionPhase::Completed),
            "session_2 added mid-run should be Completed, got {:?}",
            state_2.phase
        );
        assert!(
            manager
                .worktrees_dir()
                .join(session_id_2)
                .join("session2-output.txt")
                .exists(),
            "session_2 command should have written session2-output.txt in its worktree"
        );
    }
}