cruise 0.1.79

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
use std::collections::HashSet;
use std::fmt;
use std::io::{IsTerminal, Read};
use std::path::{Path, PathBuf};
use std::process::Stdio;

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

use std::sync::Arc;

use crate::ask_handler::{AskHandler, CliAskHandler, NoninteractiveAskHandler};
use crate::cancellation::CancellationToken;
use crate::cli::{DEFAULT_RATE_LIMIT_RETRIES, PLAN_STDIN_SENTINEL, PlanArgs, PlanWorkerArgs};
use crate::config::{WorkflowConfig, validate_config};
use crate::error::{CruiseError, Result};
use crate::multiline_input::{InputResult, prompt_multiline};
use crate::new_session_history::{
    BUILTIN_CONFIG_KEY, HistoryScope, NewSessionHistory, resolved_config_key_for_session,
};
use crate::planning::{
    PlanPromptCtx, ask_plan_template, fix_plan_template, initial_plan_template, setup_plan_vars,
};
use crate::resolver::ConfigSource;
use crate::session::{SessionManager, SessionPhase, SessionState};
use crate::variable::VariableStore;
use crate::workflow::{SkippableStepNode, list_skippable_after_pr_steps, list_skippable_steps};

/// Build a CLI planning context (interactive prompts via [`CliAskHandler`]).
fn cli_plan_ctx<'a>(
    config: &'a WorkflowConfig,
    plan_path: &'a Path,
    working_dir: Option<&'a Path>,
    interactive: bool,
    rate_limit_retries: usize,
    grill: bool,
    cancel_token: Option<&'a CancellationToken>,
) -> PlanPromptCtx<'a> {
    // Only the interactive approve loop can prompt the user; non-TTY contexts use
    // a handler that errors rather than blocking on stdin (ask_user is not
    // registered there anyway).
    let ask: Arc<dyn AskHandler> = if interactive {
        Arc::new(CliAskHandler)
    } else {
        Arc::new(NoninteractiveAskHandler)
    };
    PlanPromptCtx {
        config,
        ask,
        plan_path,
        interactive,
        rate_limit_retries,
        working_dir,
        grill,
        cancel_token,
    }
}

#[expect(
    clippy::too_many_lines,
    reason = "plan-creation flow: input + dry-run + attachments + worktree + plan + approve loop"
)]
pub async fn run(args: PlanArgs) -> Result<()> {
    // Resolve config first so the path is visible before prompting for input.
    // For repo sessions the config lives in the temporary clone, which doesn't
    // exist yet; resolution is deferred until after cloning.
    let target = resolve_plan_target(args.repo.as_deref(), args.config.as_deref())?;

    // noninteractive is true whenever stdin is not a terminal (pipe, redirect,
    // or backward-compat path where cli.rs already consumed stdin and placed
    // the content in args.input).  This prevents inquire from attempting to
    // read interactive input from a non-TTY file descriptor.
    let noninteractive = !std::io::stdin().is_terminal();

    // Grill mode interviews the user via interactive prompts, so it cannot run in
    // a non-TTY context. Fail before creating any session.
    if args.grill && noninteractive {
        return Err(CruiseError::Other(
            "--grill requires an interactive terminal (it interviews you one \
             question at a time); it cannot run in a non-TTY context"
                .to_string(),
        ));
    }

    let (raw_input, from_interactive) = read_plan_input(args.input, noninteractive)?;
    // Auto-detect image paths only from the interactive prompt (Claude-Code-like
    // drag-and-drop). For arg/stdin input, treat the text as opaque so prose
    // mentioning a path like "what changed in /tmp/x.png" stays intact.
    let (input, mut images) = if from_interactive {
        crate::attachments::extract_image_paths(&raw_input)
    } else {
        (raw_input, vec![])
    };
    for p in &args.images {
        images.push(PathBuf::from(p));
    }

    if args.dry_run {
        eprintln!(
            "{}",
            style(format!("Would plan: \"{}\"", input.trim())).dim()
        );
        if !images.is_empty() {
            for img in &images {
                eprintln!(
                    "{}",
                    style(format!("  attached image: {}", img.display())).dim()
                );
            }
        }
        return Ok(());
    }

    let manager = SessionManager::new(crate::paths::data_dir()?);
    let (mut config, mut session) =
        create_session_for_target(&manager, target, args.config.as_deref(), input.trim())?;

    if let Err(e) = attach_images_or_discard(&manager, &mut session, &images) {
        eprintln!(
            "\n{} Failed to attach images. Session {} discarded.",
            style("").red().bold(),
            session.id
        );
        return Err(e);
    }

    if args.no_interactive_planning {
        config.interactive_planning = false;
    }

    // Grill mode relies on the SDK `ask_user` tool, which is only registered in
    // the interactive tool-based planning flow. Reject when the SDK backend is
    // absent or when `interactive_planning` is disabled, discarding the session
    // we just created.
    if args.grill && !crate::planning::sdk_plan_tools_enabled(&config) {
        if let Err(del_err) = manager.delete(&session.id) {
            eprintln!("warning: failed to clean up session: {del_err}");
        }
        return Err(CruiseError::Other(
            "--grill requires the SDK backend with interactive planning enabled \
             (`sdk:` must be set and `interactive_planning` must not be disabled); \
             the command backend and tool-less planning have no interactive \
             ask_user tool"
                .to_string(),
        ));
    }

    setup_planning_worktree_or_discard(&manager, &mut session)?;

    // Set up variables with the session plan path.
    let plan_path = session.plan_path(&manager.sessions_dir());
    let mut vars = setup_plan_vars(session.input_with_attachments(), plan_path.clone(), &config);

    // SDK session id, shared across the plan / fix / ask turns so they resume the
    // same conversation. Stays `None` in command mode.
    let mut resume: Option<String> = None;
    let interactive = !noninteractive;

    if args.skip_planning {
        // Write the augmented input so the saved plan references the attached
        // images; the LLM running steps will pick those up via plan.md.
        let plan_content = session.input_with_attachments();
        if let Err(e) = crate::planning::write_input_as_plan(&plan_path, &plan_content) {
            eprintln!(
                "\n{} Failed to write input as plan. Session {} discarded.",
                style("").red().bold(),
                session.id
            );
            cleanup_discarded_session_workspace(&manager, &session);
            if let Err(del_err) = manager.delete(&session.id) {
                eprintln!("warning: failed to clean up session: {del_err}");
            }
            return Err(e);
        }
    } else {
        let work_dir = plan_working_dir(&session).to_path_buf();
        let cancel_token = CancellationToken::new();
        let ctx = cli_plan_ctx(
            &config,
            &plan_path,
            Some(&work_dir),
            interactive,
            args.rate_limit_retries,
            args.grill,
            Some(&cancel_token),
        );
        let plan_result = tokio::select! {
            result = generate_plan_markdown(&ctx, &mut vars, &mut resume) => result,
            _ = tokio::signal::ctrl_c() => {
                cancel_token.cancel();
                Err(CruiseError::Interrupted)
            },
        };
        if let Err(e) = plan_result {
            eprintln!(
                "\n{} Plan generation failed. Session {} discarded.",
                style("").red().bold(),
                session.id
            );
            cleanup_discarded_session_workspace(&manager, &session);
            if let Err(del_err) = manager.delete(&session.id) {
                eprintln!("warning: failed to clean up session: {del_err}");
            }
            return Err(e);
        }
    }

    // Approve-plan loop.
    run_approve_loop(
        &config,
        &manager,
        &mut session,
        &plan_path,
        &mut vars,
        args.rate_limit_retries,
        noninteractive,
        &mut resume,
    )
    .await
}

pub fn launch_background_plan(
    plan_input: &str,
    skip_planning: bool,
    repo: Option<&str>,
    images: &[String],
) -> Result<()> {
    let target = resolve_plan_target(repo, None)?;

    // Background planning input never comes from the interactive prompt, so
    // skip text-based path extraction (which would shadow legitimate prose
    // mentions of file paths) and accept image attachments only via `--image`.
    let input = read_background_plan_input(plan_input)?;
    let mut detected_images: Vec<PathBuf> = Vec::with_capacity(images.len());
    for p in images {
        detected_images.push(PathBuf::from(p));
    }

    let manager = SessionManager::new(crate::paths::data_dir()?);
    let (_config, mut session) = create_session_for_target(&manager, target, None, &input)?;
    attach_images_or_discard(&manager, &mut session, &detected_images)?;
    setup_planning_worktree_or_discard(&manager, &mut session)?;

    if skip_planning {
        let plan_path = session.plan_path(&manager.sessions_dir());
        let plan_content = session.input_with_attachments();
        let write_result = crate::planning::write_input_as_plan(&plan_path, &plan_content);
        match write_result {
            Ok(content) => {
                crate::metadata::refresh_session_title_from_plan(&mut session, &content);
                if let Err(e) = finalize_skip_planning_session(&manager, &mut session) {
                    cleanup_discarded_session_workspace(&manager, &session);
                    if let Err(del_err) = manager.delete(&session.id) {
                        eprintln!("warning: failed to clean up session: {del_err}");
                    }
                    return Err(e);
                }
            }
            Err(e) => {
                cleanup_discarded_session_workspace(&manager, &session);
                if let Err(del_err) = manager.delete(&session.id) {
                    eprintln!("warning: failed to clean up session: {del_err}");
                }
                return Err(e);
            }
        }
        eprintln!(
            "\n{} Session {} created (input used as plan).",
            style("").green().bold(),
            session.id
        );
        eprintln!("  Check status with: {}", style("cruise list").cyan());
        eprintln!(
            "  Run with: {}",
            style(format!("cruise run {}", session.id)).cyan()
        );
    } else {
        spawn_plan_worker(&session.id, DEFAULT_RATE_LIMIT_RETRIES)?;
        eprintln!(
            "\n{} Session {} created. Planning in background.",
            style("").green().bold(),
            session.id
        );
        eprintln!("  Check status with: {}", style("cruise list").cyan());
        eprintln!(
            "  Run once ready: {}",
            style(format!("cruise run {}", session.id)).cyan()
        );
    }
    Ok(())
}

fn finalize_skip_planning_session(
    manager: &SessionManager,
    session: &mut SessionState,
) -> Result<()> {
    session.approve(); // AwaitingApproval -> Planned, clears plan_error
    crate::repo_clone::cleanup_after_approval(manager, session);
    manager.save(session)
}

pub async fn run_plan_worker(args: PlanWorkerArgs) -> Result<()> {
    let manager = SessionManager::new(crate::paths::data_dir()?);
    let mut session = match manager.load(&args.session) {
        Ok(session) => session,
        Err(CruiseError::SessionError(_)) => return Ok(()),
        Err(err) => return Err(err),
    };
    session.plan_error = None;
    manager.save(&session)?;

    let result = generate_plan_for_session(&manager, &session, args.rate_limit_retries).await;
    match result {
        Ok(plan_markdown) => {
            crate::metadata::refresh_session_title_from_plan(&mut session, &plan_markdown);
            session.plan_error = None;
            manager.save(&session)?;
            Ok(())
        }
        Err(err) => {
            let plan_error = err.to_string();
            session.plan_error = Some(plan_error.clone());
            manager.save(&session)?;
            Err(CruiseError::Other(plan_error))
        }
    }
}

/// Read task input from CLI arg, piped stdin, or interactive prompt. The
/// returned `from_interactive` flag is true only when the text came from the
/// `reedline` prompt — callers use it to decide whether to auto-detect image
/// paths in the body (a drag-and-drop UX) versus preserving piped/arg text
/// verbatim.
fn read_plan_input(input: Option<String>, noninteractive: bool) -> Result<(String, bool)> {
    let stdin_input = if input.is_none() && noninteractive {
        let mut s = String::new();
        std::io::stdin()
            .read_to_string(&mut s)
            .map_err(CruiseError::IoError)?;
        Some(s)
    } else {
        None
    };
    let from_arg_or_stdin = input.is_some() || stdin_input.is_some();
    let text = resolve_input(input, stdin_input, || {
        if noninteractive {
            return Err(CruiseError::Other(
                "no input provided: stdin is not a terminal and no --input flag was given"
                    .to_string(),
            ));
        }
        prompt_for_plan_input()
    })?;
    Ok((text, !from_arg_or_stdin))
}

fn read_background_plan_input(input: &str) -> Result<String> {
    if input == PLAN_STDIN_SENTINEL {
        if std::io::stdin().is_terminal() {
            return Err(CruiseError::Other(format!(
                "--plan {PLAN_STDIN_SENTINEL} requires piped stdin"
            )));
        }
        let mut stdin_input = String::new();
        std::io::stdin()
            .read_to_string(&mut stdin_input)
            .map_err(CruiseError::IoError)?;
        let trimmed = stdin_input.trim().to_string();
        if trimmed.is_empty() {
            return Err(CruiseError::Other(
                "no input provided on stdin for background planning".to_string(),
            ));
        }
        return Ok(trimmed);
    }

    let trimmed = input.trim().to_string();
    if trimmed.is_empty() {
        return Err(CruiseError::Other(
            "background planning input cannot be empty".to_string(),
        ));
    }
    Ok(trimmed)
}

async fn approve_with_title(
    session: &mut SessionState,
    manager: &SessionManager,
    config: &WorkflowConfig,
    plan_content: &str,
    cancel_token: Option<&CancellationToken>,
) -> Result<()> {
    if config.sdk.is_some() {
        match generate_title_via_sdk(config, &session.input, plan_content, cancel_token).await {
            Ok(title) => session.title = Some(title),
            Err(CruiseError::Interrupted) => return Err(CruiseError::Interrupted),
            Err(e) => {
                eprintln!("warning: SDK title generation failed: {e}");
                crate::metadata::refresh_session_title_from_plan(session, plan_content);
            }
        }
    } else {
        crate::metadata::refresh_session_title_from_plan(session, plan_content);
    }
    session.approve();
    crate::repo_clone::cleanup_after_approval(manager, session);
    manager.save(session)
}

async fn generate_title_via_sdk(
    config: &WorkflowConfig,
    input: &str,
    plan_content: &str,
    cancel_token: Option<&CancellationToken>,
) -> Result<String> {
    let title_store = std::sync::Arc::new(std::sync::Mutex::new(None::<String>));
    let tool = crate::sdk_tools::generate_title_tool(std::sync::Arc::clone(&title_store));

    let executor = crate::executor::Executor::new(config.sdk.as_deref(), &config.command);
    let model_or_mode =
        executor.plan_model_or_mode(config.plan_model.as_deref(), config.model.as_deref());
    let prompt = format!(
        "Generate a concise session title (max 80 chars) for this task and plan. \
         Call the generate_title tool with your title.\n\n\
         Task: {input}\n\nPlan:\n{plan_content}"
    );
    let env = std::collections::HashMap::new();
    executor
        .run(crate::executor::PromptRun {
            prompt: &prompt,
            model_or_mode: model_or_mode.as_deref(),
            max_retries: 1,
            env: &env,
            on_retry: None,
            cancel_token,
            working_dir: None,
            stream: None,
            tools: vec![tool],
            resume: None,
        })
        .await?;

    title_store
        .lock()
        .map_err(|e| CruiseError::Other(format!("title store lock poisoned: {e}")))?
        .clone()
        .ok_or_else(|| CruiseError::Other("SDK agent did not call generate_title tool".to_string()))
}

/// Return the directory where plan-related LLM calls should run.
///
/// If a planning worktree was created the LLM executes inside it, keeping the
/// original working copy clean.  Falls back to `base_dir` for non-git repos.
fn plan_working_dir(session: &SessionState) -> &Path {
    session
        .worktree_path
        .as_deref()
        .unwrap_or(session.base_dir.as_path())
}

/// Create a planning worktree for `session` and record its path/branch.
///
/// On success the session is saved with `worktree_path` and `worktree_branch`
/// set.  If `base_dir` is not a git repository the call succeeds silently and
/// the session keeps `worktree_path = None` (plan runs in `base_dir`).
fn setup_planning_worktree(manager: &SessionManager, session: &mut SessionState) -> Result<()> {
    let worktrees_dir = manager.worktrees_dir();
    match crate::worktree::setup_session_worktree(
        &session.base_dir,
        &session.id,
        &session.input,
        &worktrees_dir,
        session.worktree_branch.as_deref(),
    ) {
        Ok((ctx, _reused)) => {
            eprintln!(
                "{} worktree: {} (planning)",
                style("->").cyan(),
                ctx.path.display()
            );
            session.worktree_path = Some(ctx.path.clone());
            session.worktree_branch = Some(ctx.branch.clone());
            manager.save(session)?;
        }
        Err(CruiseError::NotGitRepository) => {
            eprintln!("warning: not a git repository; planning in base directory");
        }
        Err(e) => return Err(e),
    }
    Ok(())
}

/// [`setup_planning_worktree`], discarding the freshly created session (and
/// its temporary clone, for repo-backed sessions) when worktree setup fails so
/// nothing leaks on the error path.
fn setup_planning_worktree_or_discard(
    manager: &SessionManager,
    session: &mut SessionState,
) -> Result<()> {
    setup_planning_worktree(manager, session).inspect_err(|_| {
        eprintln!(
            "\n{} Worktree setup failed. Session {} discarded.",
            style("").red().bold(),
            session.id
        );
        cleanup_discarded_session_workspace(manager, session);
        if let Err(del_err) = manager.delete(&session.id) {
            eprintln!("warning: failed to clean up session: {del_err}");
        }
    })
}

/// Clean up the planning worktree if one was created.  Failures are logged as
/// warnings and do not propagate — cleanup is best-effort.
fn cleanup_planning_worktree(session: &SessionState) {
    if let Some(ctx) = session.worktree_context()
        && let Err(e) = crate::worktree::cleanup_worktree(&ctx)
    {
        eprintln!("warning: failed to clean up planning worktree: {e}");
    }
}

fn create_planning_session(
    manager: &SessionManager,
    source: &ConfigSource,
    input: String,
) -> Result<SessionState> {
    let session_id = SessionManager::new_session_id();
    let base_dir = std::env::current_dir()?;
    let mut session =
        SessionState::new(session_id.clone(), base_dir, source.display_string(), input);
    session.config_path = source.path().cloned();
    manager.create(&session)?;

    Ok(session)
}

/// Where a new plan session sources its working copy and config from.
enum PlanTarget {
    /// Plan in the current directory using an already-resolved config.
    Local { yaml: String, source: ConfigSource },
    /// Clone `owner/repo` into a temporary directory and plan there.
    Repo(String),
}

/// Validate `--repo` and resolve the workflow config for the upcoming session.
///
/// For local sessions the config is resolved (and displayed) immediately; for
/// repo sessions only the spec is validated -- the config lives in the
/// temporary clone, which is created later.
fn resolve_plan_target(repo: Option<&str>, explicit_config: Option<&str>) -> Result<PlanTarget> {
    match repo.map(str::trim) {
        Some(spec) if !spec.is_empty() => {
            crate::repo_clone::validate_repo_spec(spec)?;
            crate::worktree_pr::ensure_gh_available()?;
            Ok(PlanTarget::Repo(spec.to_string()))
        }
        _ => {
            let (yaml, source) = crate::resolver::resolve_config(explicit_config)?;
            eprintln!("{}", style(source.display_string()).dim());
            Ok(PlanTarget::Local { yaml, source })
        }
    }
}

/// Create the planning session (and, for repo targets, the temporary clone)
/// for `target`, returning the parsed config alongside the session.
fn create_session_for_target(
    manager: &SessionManager,
    target: PlanTarget,
    explicit_config: Option<&str>,
    input: &str,
) -> Result<(WorkflowConfig, SessionState)> {
    match target {
        PlanTarget::Local { yaml, source } => {
            let config = match source.path() {
                Some(path) => crate::workflow_call::resolve_workflow_calls_from_path(path)?,
                None => crate::workflow_call::resolve_workflow_calls(
                    WorkflowConfig::from_yaml(&yaml)
                        .map_err(|e| CruiseError::ConfigParseError(e.to_string()))?,
                    std::env::current_dir()?,
                )?,
            };
            validate_config(&config)?;
            let session = create_planning_session(manager, &source, input.to_string())?;
            Ok((config, session))
        }
        PlanTarget::Repo(repo) => {
            create_repo_planning_session(manager, &repo, explicit_config, input.to_string())
        }
    }
}

/// Clone `repo` into the session clone directory and create a session whose
/// `base_dir` points at the clone. On failure the clone and the session
/// directory are removed.
fn create_repo_planning_session(
    manager: &SessionManager,
    repo: &str,
    explicit_config: Option<&str>,
    input: String,
) -> Result<(WorkflowConfig, SessionState)> {
    let session_id = SessionManager::new_session_id();
    let clone_path = manager.clones_dir().join(&session_id);
    eprintln!("{} cloning {} (planning)...", style("->").cyan(), repo);
    crate::repo_clone::clone_repo(repo, &clone_path)?;

    let result = build_repo_planning_session(
        manager,
        repo,
        explicit_config,
        input,
        &session_id,
        &clone_path,
    );
    if result.is_err() {
        let _ = std::fs::remove_dir_all(&clone_path);
        if let Err(del_err) = manager.delete(&session_id) {
            eprintln!("warning: failed to clean up session: {del_err}");
        }
    }
    result
}

fn build_repo_planning_session(
    manager: &SessionManager,
    repo: &str,
    explicit_config: Option<&str>,
    input: String,
    session_id: &str,
    clone_path: &Path,
) -> Result<(WorkflowConfig, SessionState)> {
    let (yaml, source) = crate::resolver::resolve_config_in_dir(explicit_config, clone_path)?;
    eprintln!("{}", style(source.display_string()).dim());
    let config = match source.path() {
        Some(path) => crate::workflow_call::resolve_workflow_calls_from_path(path)?,
        None => crate::workflow_call::resolve_workflow_calls(
            WorkflowConfig::from_yaml(&yaml)
                .map_err(|e| CruiseError::ConfigParseError(e.to_string()))?,
            clone_path,
        )?,
    };
    validate_config(&config)?;

    let mut session = SessionState::new(
        session_id.to_string(),
        clone_path.to_path_buf(),
        source.display_string(),
        input,
    );
    session.repo = Some(repo.to_string());
    // Configs that live inside the clone (or the builtin default) are copied
    // into the session directory so they stay readable after the clone is
    // removed at approval time.
    session.config_path = crate::repo_clone::persistent_config_path(&source, clone_path);
    manager.create(&session)?;
    if session.config_path.is_none() {
        let session_dir = manager.sessions_dir().join(session_id);
        let config_to_persist = serde_yaml::to_string(&config).map_err(|e| {
            CruiseError::Other(format!(
                "failed to serialize resolved workflow config for session: {e}"
            ))
        })?;
        std::fs::write(session_dir.join("config.yaml"), config_to_persist)?;
    }
    Ok((config, session))
}

/// Copy `images` into the freshly-created `session`'s attachments directory
/// and persist the stored paths on `session.attachments`. On any failure the
/// session (and, for repo-backed sessions, its temporary clone) is removed
/// before propagating the error so nothing leaks behind a half-attached
/// session. No-op when `images` is empty.
fn attach_images_or_discard(
    manager: &SessionManager,
    session: &mut SessionState,
    images: &[PathBuf],
) -> Result<()> {
    if images.is_empty() {
        return Ok(());
    }
    let session_dir = manager.sessions_dir().join(&session.id);
    match crate::attachments::copy_images_into_session(&session_dir, images) {
        Ok(stored) => {
            session.attachments = stored;
            manager.save(session)
        }
        Err(e) => {
            cleanup_discarded_session_workspace(manager, session);
            if let Err(del_err) = manager.delete(&session.id) {
                eprintln!("warning: failed to clean up session: {del_err}");
            }
            Err(e)
        }
    }
}

/// Best-effort cleanup when a session is discarded before approval: removes
/// the planning worktree and, for repo-backed sessions, the temporary clone.
fn cleanup_discarded_session_workspace(manager: &SessionManager, session: &SessionState) {
    if session.repo.is_some() {
        crate::repo_clone::cleanup_session_workspace(manager, session);
    } else {
        cleanup_planning_worktree(session);
    }
}

fn spawn_plan_worker(session_id: &str, rate_limit_retries: usize) -> Result<()> {
    let exe = std::env::current_exe().map_err(CruiseError::IoError)?;
    std::process::Command::new(exe)
        .arg("plan-worker")
        .arg("--session")
        .arg(session_id)
        .arg("--rate-limit-retries")
        .arg(rate_limit_retries.to_string())
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .map_err(|e| CruiseError::ProcessSpawnError(format!("failed to spawn plan worker: {e}")))?;
    Ok(())
}

async fn generate_plan_for_session(
    manager: &SessionManager,
    session: &SessionState,
    rate_limit_retries: usize,
) -> Result<String> {
    let config = manager.load_config(session)?;
    let plan_path = session.plan_path(&manager.sessions_dir());
    let mut vars = setup_plan_vars(session.input_with_attachments(), plan_path.clone(), &config);
    // Background worker: no interactive user, so the SDK agent proceeds on
    // assumptions (no `ask_user`). `resume` is unused for a one-shot generation.
    let mut resume: Option<String> = None;
    // Background worker is non-interactive, so grill mode is never used here.
    let ctx = cli_plan_ctx(
        &config,
        &plan_path,
        Some(plan_working_dir(session)),
        false,
        rate_limit_retries,
        false,
        None,
    );
    generate_plan_markdown(&ctx, &mut vars, &mut resume).await
}

async fn generate_plan_markdown(
    ctx: &PlanPromptCtx<'_>,
    vars: &mut VariableStore,
    resume: &mut Option<String>,
) -> Result<String> {
    let prompt_result = crate::planning::run_plan_prompt_template(
        ctx,
        vars,
        initial_plan_template(ctx.config, ctx.grill),
        "[plan] creating plan...",
        None,
        resume,
        true,
    )
    .await?;
    // Read the backend transcript only when content resolution fails; on
    // success the plan document is already on disk and the transcript read
    // would be dead I/O.
    crate::planning::resolve_generated_plan_content(
        ctx.plan_path,
        &prompt_result.output,
        &prompt_result.stderr,
        None,
    )
    .or_else(|_| {
        let transcript = resume.as_deref().and_then(|session_id| {
            crate::planning::read_sdk_transcript(ctx.working_dir, session_id)
        });
        crate::planning::resolve_generated_plan_content(
            ctx.plan_path,
            &prompt_result.output,
            &prompt_result.stderr,
            transcript.as_deref(),
        )
    })
}

#[derive(Clone)]
struct FlatNode {
    label: String,
    expanded_step_ids: Vec<String>,
}

impl fmt::Display for FlatNode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.label)
    }
}

fn flatten_nodes(nodes: &[SkippableStepNode], prefix: &str) -> Vec<FlatNode> {
    let mut flat = Vec::new();
    flatten_nodes_into(nodes, 0, prefix, &mut flat);
    flat
}

fn flatten_nodes_into(
    nodes: &[SkippableStepNode],
    depth: usize,
    prefix: &str,
    flat: &mut Vec<FlatNode>,
) {
    for node in nodes {
        let label = if depth == 0 {
            format!("{}{}", prefix, node.id)
        } else {
            node.id
                .rsplit('/')
                .next()
                .unwrap_or(node.id.as_str())
                .to_string()
        };
        flat.push(FlatNode {
            label: format!("{}{}", "  ".repeat(depth), label),
            expanded_step_ids: node.expanded_step_ids.clone(),
        });
        flatten_nodes_into(&node.children, depth + 1, prefix, flat);
    }
}

fn collect_expanded_ids(selected_nodes: Vec<FlatNode>) -> Vec<String> {
    let mut seen = HashSet::new();
    let mut expanded_ids = Vec::new();

    for expanded_id in selected_nodes
        .into_iter()
        .flat_map(|node| node.expanded_step_ids)
    {
        if seen.insert(expanded_id.clone()) {
            expanded_ids.push(expanded_id);
        }
    }

    expanded_ids
}

fn flat_node_default_indices(flat: &[FlatNode], previously_skipped: &[String]) -> Vec<usize> {
    let skipped_set: HashSet<&str> = previously_skipped.iter().map(String::as_str).collect();
    flat.iter()
        .enumerate()
        .filter(|(_, node)| {
            !node.expanded_step_ids.is_empty()
                && node
                    .expanded_step_ids
                    .iter()
                    .all(|id| skipped_set.contains(id.as_str()))
        })
        .map(|(i, _)| i)
        .collect()
}

pub(crate) enum StepSkipSelection {
    Confirmed(Vec<String>),
    Cancelled,
}
/// Present a `MultiSelect` prompt so the user can choose which steps to skip.
///
/// Returns [`StepSkipSelection::Cancelled`] when the user cancels or an
/// interruption is received so the approve flow can continue unblocked.
/// Steps that were previously skipped are pre-selected via `previously_skipped`.
pub(crate) fn select_steps_to_skip(
    config: &WorkflowConfig,
    previously_skipped: &[String],
) -> Result<StepSkipSelection> {
    let main_nodes = list_skippable_steps(config)?;
    let after_pr_nodes = list_skippable_after_pr_steps(config)?;
    if main_nodes.is_empty() && after_pr_nodes.is_empty() {
        return Ok(StepSkipSelection::Confirmed(vec![]));
    }

    let mut flat = flatten_nodes(&main_nodes, "");
    flat.extend(flatten_nodes(&after_pr_nodes, "[after-pr] "));
    let defaults = flat_node_default_indices(&flat, previously_skipped);

    crate::platform::reclaim_terminal_foreground();
    match inquire::MultiSelect::new("Steps to skip (Space to toggle, Enter to confirm):", flat)
        .with_help_message("No selection = run all steps")
        .with_default(&defaults)
        .prompt()
    {
        Ok(selected_nodes) => Ok(StepSkipSelection::Confirmed(collect_expanded_ids(
            selected_nodes,
        ))),
        Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => {
            Ok(StepSkipSelection::Cancelled)
        }
        Err(e) => Err(CruiseError::Other(format!("selection error: {e}"))),
    }
}

fn apply_skip_step_selection(
    history: &mut NewSessionHistory,
    scope: HistoryScope<'_>,
    resolved_config_key: &str,
    selection: StepSkipSelection,
) -> (Vec<String>, bool) {
    match selection {
        StepSkipSelection::Confirmed(skipped_steps) => {
            history.record_skip_selection_for_scope(
                scope,
                resolved_config_key,
                skipped_steps.clone(),
            );
            (skipped_steps, true)
        }
        StepSkipSelection::Cancelled => (vec![], false),
    }
}

/// Let the user choose steps to skip with history-based defaults, then record
/// the selection for future sessions. History is loaded once.
fn select_skipped_steps_with_history(
    session: &SessionState,
    config: &WorkflowConfig,
) -> Result<Vec<String>> {
    if config.steps.is_empty() {
        return Ok(vec![]);
    }

    let key = match session.config_path.as_deref() {
        Some(p) => resolved_config_key_for_session(p),
        None => BUILTIN_CONFIG_KEY.to_string(),
    };
    let mut history = NewSessionHistory::load_best_effort();

    let base_dir_str = session.base_dir.to_string_lossy();
    let scope = match session.repo.as_deref() {
        Some(repo) => HistoryScope::Repo(repo),
        None => HistoryScope::Directory(base_dir_str.as_ref()),
    };

    let previously_skipped = history
        .latest_entry_for_scope(scope, &key)
        .map(|entry| entry.skipped_steps.clone())
        .unwrap_or_default();

    let selection = select_steps_to_skip(config, &previously_skipped)?;
    let (skipped_steps, should_persist) =
        apply_skip_step_selection(&mut history, scope, &key, selection);
    if should_persist {
        history.save_best_effort();
    }

    Ok(skipped_steps)
}

/// Interactive approve-plan loop: show plan, let user approve/fix/ask/execute.
/// When `noninteractive` is true (e.g. stdin was piped), auto-approves the plan
/// without prompting so that inquire never tries to read from a non-TTY stdin.
#[expect(
    clippy::too_many_lines,
    reason = "approve/fix/ask/execute loop with multiple action branches"
)]
#[expect(
    clippy::too_many_arguments,
    reason = "approve loop threads session state, config, and the SDK resume id"
)]
async fn run_approve_loop(
    config: &WorkflowConfig,
    manager: &SessionManager,
    session: &mut SessionState,
    plan_path: &std::path::Path,
    vars: &mut VariableStore,
    rate_limit_retries: usize,
    noninteractive: bool,
    resume: &mut Option<String>,
) -> Result<()> {
    let working_dir = session
        .worktree_path
        .clone()
        .unwrap_or_else(|| session.base_dir.clone());
    // Fix / Ask turns reuse one planning context. It is only reached on the
    // interactive path — the noninteractive branch auto-approves below.
    // Grill affects only the initial plan template; fix/ask turns are standard.
    let cancel_token = CancellationToken::new();
    let ctx = cli_plan_ctx(
        config,
        plan_path,
        Some(working_dir.as_path()),
        !noninteractive,
        rate_limit_retries,
        false,
        Some(&cancel_token),
    );

    // Read the plan once up front; re-read only after Fix modifies it.
    let mut plan_content = match crate::metadata::read_plan_markdown(plan_path) {
        Ok(content) => content,
        Err(err) => {
            eprintln!(
                "\n{} Generated plan is missing or empty. Session {} discarded.",
                style("x").red().bold(),
                session.id
            );
            cleanup_discarded_session_workspace(manager, session);
            if let Err(del_err) = manager.delete(&session.id) {
                eprintln!("warning: failed to clean up session: {del_err}");
            }
            return Err(err);
        }
    };

    loop {
        crate::display::print_bordered(&plan_content, Some("plan.md"));

        if noninteractive {
            approve_with_title(session, manager, config, &plan_content, None).await?;
            eprintln!(
                "\n{} Session {} created.",
                style("v").green().bold(),
                session.id
            );
            eprintln!(
                "  Run with: {}",
                style(format!("cruise run {}", session.id)).cyan()
            );
            return Ok(());
        }

        let options = vec!["Approve", "Fix", "Ask", "Execute now", "Publish as Issue"];
        crate::platform::reclaim_terminal_foreground();
        let selected = match inquire::Select::new("Action:", options).prompt() {
            Ok(s) => s,
            Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => {
                eprintln!("\nCancelled. Session {} discarded.", session.id);
                cleanup_discarded_session_workspace(manager, session);
                manager.delete(&session.id)?;
                return Ok(());
            }
            Err(e) => return Err(CruiseError::Other(format!("selection error: {e}"))),
        };

        match selected {
            "Approve" => {
                session.skipped_steps = select_skipped_steps_with_history(session, config)?;
                tokio::select! {
                    result = approve_with_title(session, manager, config, &plan_content, Some(&cancel_token)) => result?,
                    _ = tokio::signal::ctrl_c() => {
                        cancel_token.cancel();
                        eprintln!("\nCancelled. Session {} discarded.", session.id);
                        cleanup_discarded_session_workspace(manager, session);
                        if let Err(del_err) = manager.delete(&session.id) {
                            eprintln!("warning: failed to clean up session: {del_err}");
                        }
                        return Err(CruiseError::Interrupted);
                    },
                }
                eprintln!(
                    "\n{} Session {} created.",
                    style("v").green().bold(),
                    session.id
                );
                eprintln!(
                    "  Run with: {}",
                    style(format!("cruise run {}", session.id)).cyan()
                );
                return Ok(());
            }
            "Fix" => {
                let text = match prompt_multiline("Describe the changes needed:")? {
                    InputResult::Submitted(t) => t,
                    InputResult::Cancelled => continue,
                };
                vars.set_prev_input(Some(text));
                tokio::select! {
                    result = run_fix_plan(&ctx, vars, resume) => result?,
                    _ = tokio::signal::ctrl_c() => {
                        cancel_token.cancel();
                        return Err(CruiseError::Interrupted);
                    },
                }
                plan_content = crate::metadata::read_plan_markdown(plan_path)?;
            }
            "Ask" => {
                let text = match prompt_multiline("Your question:")? {
                    InputResult::Submitted(t) => t,
                    InputResult::Cancelled => continue,
                };
                vars.set_prev_input(Some(text));
                tokio::select! {
                    result = run_ask_plan(&ctx, vars, resume) => result?,
                    _ = tokio::signal::ctrl_c() => {
                        cancel_token.cancel();
                        return Err(CruiseError::Interrupted);
                    },
                }
            }
            "Execute now" => {
                session.skipped_steps = select_skipped_steps_with_history(session, config)?;
                tokio::select! {
                    result = approve_with_title(session, manager, config, &plan_content, Some(&cancel_token)) => result?,
                    _ = tokio::signal::ctrl_c() => {
                        cancel_token.cancel();
                        eprintln!("\nCancelled. Session {} discarded.", session.id);
                        cleanup_discarded_session_workspace(manager, session);
                        if let Err(del_err) = manager.delete(&session.id) {
                            eprintln!("warning: failed to clean up session: {del_err}");
                        }
                        return Err(CruiseError::Interrupted);
                    },
                }
                eprintln!(
                    "\n{} Executing session {}...",
                    style("->").cyan(),
                    session.id
                );
                let run_args = crate::cli::RunArgs {
                    session: Some(session.id.clone()),
                    all: false,
                    max_retries: None,
                    rate_limit_retries,
                    dry_run: false,
                    cleanup_after_pr: false,
                    no_cleanup_after_pr: false,
                };
                return crate::run_cmd::run(run_args).await;
            }
            "Publish as Issue" => {
                let Some(trigger_cruise) = prompt_trigger_cruise()? else {
                    continue;
                };
                match crate::issue_publish::publish_plan_issue_and_delete(
                    manager,
                    session.clone(),
                    trigger_cruise,
                ) {
                    Ok(published) => {
                        eprintln!(
                            "\n{} Published plan as issue: {}",
                            style("v").green().bold(),
                            published.url
                        );
                        return Ok(());
                    }
                    Err(e) => {
                        eprintln!(
                            "\n{} Failed to publish plan as issue: {e}",
                            style("x").red().bold()
                        );
                        if let Ok(reloaded) = manager.load(&session.id) {
                            *session = reloaded;
                        }
                    }
                }
            }
            _ => {}
        }
    }
}

/// Prompt whether to post an `@cruise run` comment after creating the issue.
///
/// Returns `Ok(None)` if the user cancels the prompt.
fn prompt_trigger_cruise() -> Result<Option<bool>> {
    crate::platform::reclaim_terminal_foreground();
    match inquire::Confirm::new("Post an @cruise run comment after creating the issue?")
        .with_default(false)
        .prompt()
    {
        Ok(answer) => Ok(Some(answer)),
        Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => Ok(None),
        Err(e) => Err(CruiseError::Other(format!("selection error: {e}"))),
    }
}

/// Replan an existing session using the built-in fix-plan prompt.
pub async fn replan_session(
    manager: &SessionManager,
    session: &mut SessionState,
    feedback: String,
    rate_limit_retries: usize,
) -> Result<()> {
    if crate::repo_clone::ensure_repo_session_workspace(manager, session)? {
        manager.save(session)?;
    }
    let config = manager.load_config(session)?;
    let plan_path = session.plan_path(&manager.sessions_dir());
    let mut vars = setup_plan_vars(session.input_with_attachments(), plan_path.clone(), &config);
    vars.set_prev_input(Some(feedback));
    let working_dir = session
        .worktree_path
        .clone()
        .unwrap_or_else(|| session.base_dir.clone());
    let mut resume: Option<String> = None;
    // Fix-plan reuses the standard template regardless of grill.
    let ctx = cli_plan_ctx(
        &config,
        &plan_path,
        Some(working_dir.as_path()),
        std::io::stdin().is_terminal(),
        rate_limit_retries,
        false,
        None,
    );
    run_fix_plan(&ctx, &mut vars, &mut resume).await?;

    let plan_markdown = crate::metadata::read_plan_markdown(&plan_path)?;
    crate::metadata::refresh_session_title_from_plan(session, &plan_markdown);
    session.plan_error = None;
    manager.save(session)?;
    Ok(())
}

/// Run the built-in fix-plan prompt.
async fn run_fix_plan(
    ctx: &PlanPromptCtx<'_>,
    vars: &mut VariableStore,
    resume: &mut Option<String>,
) -> Result<()> {
    run_plan_prompt(
        ctx,
        vars,
        fix_plan_template(ctx.config),
        "[fix-plan] applying fixes...",
        resume,
        true,
    )
    .await
}

/// Run the built-in ask-plan prompt.
async fn run_ask_plan(
    ctx: &PlanPromptCtx<'_>,
    vars: &mut VariableStore,
    resume: &mut Option<String>,
) -> Result<()> {
    run_plan_prompt(
        ctx,
        vars,
        ask_plan_template(ctx.config),
        "[ask-plan] answering question...",
        resume,
        // Read-only: the Ask flow must never overwrite the saved plan, so no
        // plan-writing tools are registered.
        false,
    )
    .await
}

/// Shared implementation for fix-plan and ask-plan: resolve the given
/// `template`, display `label`, and run it as a prompt step.
async fn run_plan_prompt(
    ctx: &PlanPromptCtx<'_>,
    vars: &mut VariableStore,
    template: &str,
    label: &str,
    resume: &mut Option<String>,
    register_plan_tools: bool,
) -> Result<()> {
    let result = crate::planning::run_plan_prompt_template(
        ctx,
        vars,
        template,
        label,
        None,
        resume,
        register_plan_tools,
    )
    .await?;
    vars.set_prev_output(Some(result.output));
    vars.set_prev_stderr(Some(result.stderr));
    vars.set_prev_input(None);
    Ok(())
}

fn resolve_input<F>(
    arg: Option<String>,
    stdin_input: Option<String>,
    interactive: F,
) -> Result<String>
where
    F: FnOnce() -> Result<String>,
{
    if let Some(input) = arg {
        return Ok(input);
    }

    if let Some(input) = stdin_input {
        let trimmed = input.trim().to_string();
        if !trimmed.is_empty() {
            return Ok(trimmed);
        }
    }

    interactive()
}

/// Prompt interactively for the initial plan input.
fn prompt_for_plan_input() -> Result<String> {
    prompt_multiline("What would you like to implement?")?.into_result()
}

/// Drive plan generation for a session currently in `Draft` phase.
///
/// On success the session transitions to `AwaitingApproval`.
pub async fn generate_plan_for_draft_session(
    manager: &SessionManager,
    session: &mut SessionState,
    rate_limit_retries: usize,
) -> Result<()> {
    if !matches!(session.phase, SessionPhase::Draft) {
        return Err(CruiseError::Other(format!(
            "expected Draft phase, got {}",
            session.phase.label()
        )));
    }
    if crate::repo_clone::ensure_repo_session_workspace(manager, session)? {
        manager.save(session)?;
    }
    let config = manager.load_config(session)?;
    setup_planning_worktree(manager, session)?;
    let plan_path = session.plan_path(&manager.sessions_dir());
    let mut vars = setup_plan_vars(session.input_with_attachments(), plan_path.clone(), &config);

    // Own the working dir so `ctx` doesn't borrow `session` across the
    // mutable `inspect_err` below.
    let work_dir = plan_working_dir(session).to_path_buf();
    let mut resume: Option<String> = None;
    // Draft regeneration uses the standard plan flow; grill is a `cruise plan`
    // flag and is not threaded through drafts.
    let ctx = cli_plan_ctx(
        &config,
        &plan_path,
        Some(&work_dir),
        std::io::stdin().is_terminal(),
        rate_limit_retries,
        false,
        None,
    );
    generate_plan_markdown(&ctx, &mut vars, &mut resume)
        .await
        .inspect_err(|e| {
            session.plan_error = Some(e.to_string());
            cleanup_planning_worktree(session);
            session.worktree_path = None;
            session.worktree_branch = None;
            if let Err(save_err) = manager.save(session) {
                eprintln!("warning: failed to persist plan error state: {save_err}");
            }
        })?;

    let plan_markdown = crate::metadata::read_plan_markdown(&plan_path)?;
    crate::metadata::refresh_session_title_from_plan(session, &plan_markdown);

    session.phase = SessionPhase::AwaitingApproval;
    session.plan_error = None;
    manager.save(session)?;
    Ok(())
}

/// Regenerate the plan for a session that is already past the Draft phase.
///
/// Unlike [`generate_plan_for_draft_session`], this function accepts sessions in
/// `Draft | AwaitingInput | AwaitingApproval | Planned` phases. The phase
/// transition after successful regeneration matches the GUI's `regenerate_plan`
/// command (`src-tauri/src/commands.rs:1422-1427`):
///
/// - `Draft | AwaitingInput` → `AwaitingApproval`
/// - `AwaitingApproval` → `AwaitingApproval` (no-op)
/// - `Planned` → `Planned` (preserve approval; do NOT silently un-approve)
pub async fn regenerate_plan_for_session(
    manager: &SessionManager,
    session: &mut SessionState,
    rate_limit_retries: usize,
) -> Result<()> {
    match &session.phase {
        SessionPhase::Draft
        | SessionPhase::AwaitingInput
        | SessionPhase::AwaitingApproval
        | SessionPhase::Planned => {}
        other => {
            return Err(CruiseError::Other(format!(
                "expected Draft, AwaitingInput, AwaitingApproval, or Planned phase, got {}",
                other.label()
            )));
        }
    }

    let original_phase = session.phase.clone();

    if crate::repo_clone::ensure_repo_session_workspace(manager, session)? {
        manager.save(session)?;
    }
    let config = manager.load_config(session)?;
    // Save worktree context before planning; a Planned session already has an
    // approved worktree that must survive a planning failure.
    let saved_worktree_path = session.worktree_path.clone();
    let saved_worktree_branch = session.worktree_branch.clone();
    setup_planning_worktree(manager, session)?;
    let plan_path = session.plan_path(&manager.sessions_dir());
    let mut vars = setup_plan_vars(session.input_with_attachments(), plan_path.clone(), &config);

    let work_dir = plan_working_dir(session).to_path_buf();
    let mut resume: Option<String> = None;
    let ctx = cli_plan_ctx(
        &config,
        &plan_path,
        Some(&work_dir),
        std::io::stdin().is_terminal(),
        rate_limit_retries,
        false,
        None,
    );
    generate_plan_markdown(&ctx, &mut vars, &mut resume)
        .await
        .inspect_err(|e| {
            session.plan_error = Some(e.to_string());
            if saved_worktree_path.is_none() {
                // Worktree was freshly created for this planning attempt; clean it up.
                cleanup_planning_worktree(session);
                session.worktree_path = None;
                session.worktree_branch = None;
            } else {
                // Restore the pre-existing approved worktree; do not delete it.
                session.worktree_path = saved_worktree_path;
                session.worktree_branch = saved_worktree_branch;
            }
            if let Err(save_err) = manager.save(session) {
                eprintln!("warning: failed to persist plan error state: {save_err}");
            }
        })?;

    let plan_markdown = crate::metadata::read_plan_markdown(&plan_path)?;
    crate::metadata::refresh_session_title_from_plan(session, &plan_markdown);

    session.plan_error = None;
    // Phase transition matching GUI's regenerate_plan (src-tauri/src/commands.rs:1422-1427):
    // - Draft | AwaitingInput → AwaitingApproval
    // - AwaitingApproval → AwaitingApproval (no-op)
    // - Planned → Planned (preserve approval; do NOT silently un-approve)
    session.phase = match original_phase {
        SessionPhase::Draft | SessionPhase::AwaitingInput => SessionPhase::AwaitingApproval,
        _ => original_phase,
    };
    manager.save(session)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::new_session_history::{NewSessionHistory, NewSessionHistoryEntry};
    use crate::session::SessionManager;
    use crate::test_support::{init_git_repo, lock_process, make_session};
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_setup_plan_vars_sets_configured_plan_language() {
        // Given: a workflow config with a custom planning language
        let yaml = r"
command: [echo]
plan_language: Japanese
steps:
  s1:
    command: echo hi
";
        let config = WorkflowConfig::from_yaml(yaml).unwrap_or_else(|e| panic!("{e:?}"));

        // When: planning variables are set up
        let vars = setup_plan_vars("task".to_string(), PathBuf::from("plan.md"), &config);

        // Then: the plan language variable resolves to the configured value
        assert_eq!(
            vars.resolve("{plan.language}")
                .unwrap_or_else(|e| panic!("{e:?}")),
            "Japanese"
        );
    }

    #[test]
    fn test_setup_plan_vars_defaults_blank_plan_language_to_english() {
        // Given: a workflow config with a blank planning language
        let yaml = r#"
command: [echo]
plan_language: "   "
steps:
  s1:
    command: echo hi
"#;
        let config = WorkflowConfig::from_yaml(yaml).unwrap_or_else(|e| panic!("{e:?}"));

        // When: planning variables are set up
        let vars = setup_plan_vars("task".to_string(), PathBuf::from("plan.md"), &config);

        // Then: the plan language variable falls back to English
        assert_eq!(
            vars.resolve("{plan.language}")
                .unwrap_or_else(|e| panic!("{e:?}")),
            crate::config::DEFAULT_PLAN_LANGUAGE
        );
    }

    #[test]
    fn test_setup_planning_worktree_creates_worktree_and_sets_session_fields() {
        let _lock = lock_process();
        // Given: a valid git repo and a persisted session
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let repo = tmp.path().join("repo");
        let cruise_home = tmp.path().join(".cruise");
        fs::create_dir_all(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);
        let manager = SessionManager::new(cruise_home);
        let mut session = make_session("20260522080000", &repo);
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When: the planning worktree is set up
        setup_planning_worktree(&manager, &mut session).unwrap_or_else(|e| panic!("{e:?}"));

        // Then: the session has a worktree path and branch recorded
        assert!(
            session.worktree_path.is_some(),
            "worktree_path should be set after setup_planning_worktree"
        );
        let wt_path = session
            .worktree_path
            .as_ref()
            .unwrap_or_else(|| panic!("worktree_path should be set"));
        assert!(wt_path.exists(), "worktree directory should exist on disk");
        assert!(
            session.worktree_branch.is_some(),
            "worktree_branch should be set after setup_planning_worktree"
        );

        // Cleanup
        let ctx = session
            .worktree_context()
            .unwrap_or_else(|| panic!("expected worktree context"));
        crate::worktree::cleanup_worktree(&ctx).unwrap_or_else(|e| panic!("{e:?}"));
    }

    #[test]
    fn test_setup_planning_worktree_noop_for_non_git_repo() {
        // Given: a directory that is NOT a git repository
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let non_git_dir = tmp.path().join("not-a-repo");
        let cruise_home = tmp.path().join(".cruise");
        fs::create_dir_all(&non_git_dir).unwrap_or_else(|e| panic!("{e:?}"));
        let manager = SessionManager::new(cruise_home);
        let mut session = make_session("20260522080001", &non_git_dir);

        // When: the planning worktree is set up for a non-git directory
        let result = setup_planning_worktree(&manager, &mut session);

        // Then: no error is returned and worktree fields remain unset (graceful fallback)
        assert!(
            result.is_ok(),
            "setup_planning_worktree should not fail for non-git repo: {result:?}"
        );
        assert!(
            session.worktree_path.is_none(),
            "worktree_path should remain None for non-git repo"
        );
        assert!(
            session.worktree_branch.is_none(),
            "worktree_branch should remain None for non-git repo"
        );
    }

    #[test]
    fn test_resolve_input_from_arg() {
        // Given: a CLI arg is provided
        let result = resolve_input(Some("add feature X".to_string()), None, || {
            panic!("interactive prompt should not run")
        });
        assert_eq!(result.unwrap_or_else(|e| panic!("{e:?}")), "add feature X");
    }

    #[test]
    fn test_resolve_input_from_stdin() {
        // Given: stdin input is present and no CLI arg is provided
        let result = resolve_input(None, Some("  add feature from pipe\n".to_string()), || {
            panic!("interactive prompt should not run")
        });
        assert_eq!(
            result.unwrap_or_else(|e| panic!("{e:?}")),
            "add feature from pipe"
        );
    }

    #[test]
    fn test_resolve_input_without_arg_or_stdin_uses_interactive_result() {
        // Given: no CLI arg or stdin input is available
        let result = resolve_input(None, None, || Ok("resume in place".to_string()));
        assert_eq!(
            result.unwrap_or_else(|e| panic!("{e:?}")),
            "resume in place"
        );
    }

    // -- resolve_input with multiline stdin ----------------------------------

    #[test]
    fn test_resolve_input_multiline_from_stdin_preserves_internal_newlines() {
        // Given: multi-line stdin input (piped, etc.)
        let stdin = "line1\nline2\nline3\n".to_string();
        let result = resolve_input(None, Some(stdin), || {
            panic!("interactive prompt should not run")
        });
        // Then: only leading/trailing whitespace is trimmed, internal newlines are preserved
        assert_eq!(
            result.unwrap_or_else(|e| panic!("{e:?}")),
            "line1\nline2\nline3"
        );
    }

    #[test]
    fn test_resolve_input_multiline_trims_only_leading_trailing_whitespace() {
        // Given: multi-line stdin input with extra whitespace at start and end
        let stdin = "  line1\nline2  \n".to_string();
        let result = resolve_input(None, Some(stdin), || {
            panic!("interactive prompt should not run")
        });
        // Then: only leading/trailing whitespace is removed, internal newlines are preserved
        assert_eq!(result.unwrap_or_else(|e| panic!("{e:?}")), "line1\nline2");
    }

    #[test]
    fn test_collect_expanded_ids_deduplicates_parent_and_child_selection() {
        let selected = vec![
            FlatNode {
                label: "review-pass".to_string(),
                expanded_step_ids: vec![
                    "review-pass/simplify".to_string(),
                    "review-pass/coderabbit".to_string(),
                ],
            },
            FlatNode {
                label: "  simplify".to_string(),
                expanded_step_ids: vec!["review-pass/simplify".to_string()],
            },
        ];

        assert_eq!(
            collect_expanded_ids(selected),
            vec!["review-pass/simplify", "review-pass/coderabbit"]
        );
    }

    #[test]
    fn test_apply_skip_step_selection_records_confirmed_empty_selection() {
        let mut history = NewSessionHistory::default();
        let (skipped_steps, should_persist) = apply_skip_step_selection(
            &mut history,
            HistoryScope::Directory("/home/user/proj"),
            "/config/a.yaml",
            StepSkipSelection::Confirmed(vec![]),
        );

        assert!(should_persist);
        assert!(skipped_steps.is_empty());
        assert_eq!(history.entries.len(), 1);
        assert_eq!(
            history.entries[0],
            NewSessionHistoryEntry {
                selected_at: history.entries[0].selected_at.clone(),
                input: String::new(),
                requested_config_path: None,
                working_dir: "/home/user/proj".to_string(),
                repo: None,
                resolved_config_key: "/config/a.yaml".to_string(),
                skipped_steps: vec![],
            }
        );
    }

    #[test]
    fn test_apply_skip_step_selection_does_not_record_cancelled_prompt() {
        let mut history = NewSessionHistory::default();
        history.record_selection(NewSessionHistoryEntry {
            selected_at: String::new(),
            input: String::new(),
            requested_config_path: None,
            working_dir: String::new(),
            repo: None,
            resolved_config_key: "/config/a.yaml".to_string(),
            skipped_steps: vec!["review".to_string()],
        });

        let (skipped_steps, should_persist) = apply_skip_step_selection(
            &mut history,
            HistoryScope::Directory(""),
            "/config/a.yaml",
            StepSkipSelection::Cancelled,
        );

        assert!(!should_persist);
        assert!(skipped_steps.is_empty());
        assert_eq!(history.entries.len(), 1);
        assert_eq!(history.entries[0].skipped_steps, vec!["review"]);
    }

    #[test]
    fn test_apply_skip_step_selection_updates_existing_gui_history_entry() {
        let mut history = NewSessionHistory::default();
        history.record_selection(NewSessionHistoryEntry {
            selected_at: "2026-04-07T00:00:00Z".to_string(),
            input: String::new(),
            requested_config_path: Some("/config/a.yaml".to_string()),
            working_dir: "/Users/test/project".to_string(),
            repo: None,
            resolved_config_key: "/config/a.yaml".to_string(),
            skipped_steps: vec!["plan".to_string()],
        });

        let (skipped_steps, should_persist) = apply_skip_step_selection(
            &mut history,
            HistoryScope::Directory("/Users/test/project"),
            "/config/a.yaml",
            StepSkipSelection::Confirmed(vec!["review".to_string()]),
        );

        assert!(should_persist);
        assert_eq!(skipped_steps, vec!["review"]);
        assert_eq!(history.entries.len(), 1);
        assert_eq!(
            history.entries[0].requested_config_path.as_deref(),
            Some("/config/a.yaml")
        );
        assert_eq!(history.entries[0].working_dir, "/Users/test/project");
        assert_eq!(history.entries[0].skipped_steps, vec!["review"]);
    }

    // -- generate_title_via_sdk cancellation -----------------------------------

    /// Given: a command-backend config, a pre-cancelled token
    /// When: `generate_title_via_sdk` is called
    /// Then: returns `CruiseError::Interrupted` before the 5-second timeout
    ///
    /// If this test times out, the `cancel_token` is not forwarded to `PromptRun`
    /// inside `generate_title_via_sdk`.
    #[cfg(unix)]
    #[tokio::test]
    async fn generate_title_via_sdk_pre_cancelled_token_returns_interrupted() {
        use crate::cancellation::CancellationToken;
        use crate::config::WorkflowConfig;
        use crate::error::CruiseError;

        let _guard = crate::test_support::lock_process();

        // Given: command backend that blocks indefinitely, pre-cancelled token
        let yaml = "command: [\"sleep\", \"100\"]\nsteps:\n  s1:\n    prompt: hi\n";
        let config = WorkflowConfig::from_yaml(yaml).unwrap_or_else(|e| panic!("bad yaml: {e:?}"));
        let token = CancellationToken::new();
        token.cancel();

        // When
        let timed = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            generate_title_via_sdk(&config, "my task", "# Plan\n- step", Some(&token)),
        )
        .await;

        // Then: completes before timeout and returns Interrupted
        assert!(
            timed.is_ok(),
            "timed out — cancel_token is not forwarded inside generate_title_via_sdk"
        );
        assert!(
            matches!(
                timed.unwrap_or_else(|e| panic!("{e:?}")),
                Err(CruiseError::Interrupted)
            ),
            "expected CruiseError::Interrupted"
        );
    }

    /// Given: a command-backend config, no cancel token
    /// When: `generate_title_via_sdk` is called with a fast command (cat)
    /// Then: returns an error (no title tool called), but does not panic
    ///       — this is a baseline test to verify no regression in the happy path signature
    #[cfg(unix)]
    #[tokio::test]
    async fn generate_title_via_sdk_no_token_returns_err_without_sdk_tool() {
        use crate::config::WorkflowConfig;

        let _guard = crate::test_support::lock_process();

        let yaml = "command: [\"cat\"]\nsteps:\n  s1:\n    prompt: hi\n";
        let config = WorkflowConfig::from_yaml(yaml).unwrap_or_else(|e| panic!("bad yaml: {e:?}"));

        // cat echoes the prompt; the generate_title tool is never called.
        // With no token, the function should complete (with an Err: None from the store).
        let result = generate_title_via_sdk(&config, "task", "# Plan", None).await;

        // The title store remains empty because cat doesn't call the tool,
        // so the function returns Err with "title store returned None".
        assert!(
            result.is_err(),
            "expected Err when title tool was not called"
        );
    }

    // -----------------------------------------------------------------------
    // regenerate_plan_for_session — phase gate
    // -----------------------------------------------------------------------

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_running_phase_fails() {
        // Given: a Running session — cannot regenerate while executing
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let repo = tmp.path().join("repo");
        fs::create_dir_all(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);
        fs::write(
            repo.join("cruise.yaml"),
            "command: [echo]\nsteps:\n  s:\n    prompt: hi",
        )
        .unwrap_or_else(|e| panic!("{e:?}"));

        let manager = SessionManager::new(tmp.path().join(".cruise"));
        let mut session = make_session("20260619200001", &repo);
        session.phase = SessionPhase::Running;
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then: must return an error (not silently succeed or panic)
        assert!(
            result.is_err(),
            "regenerate_plan_for_session should fail for Running phase"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_completed_phase_fails() {
        // Given: a Completed session
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let repo = tmp.path().join("repo");
        fs::create_dir_all(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);
        fs::write(
            repo.join("cruise.yaml"),
            "command: [echo]\nsteps:\n  s:\n    prompt: hi",
        )
        .unwrap_or_else(|e| panic!("{e:?}"));

        let manager = SessionManager::new(tmp.path().join(".cruise"));
        let mut session = make_session("20260619200002", &repo);
        session.phase = SessionPhase::Completed;
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then
        assert!(
            result.is_err(),
            "regenerate_plan_for_session should fail for Completed phase"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_suspended_phase_fails() {
        // Given: a Suspended session
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let repo = tmp.path().join("repo");
        fs::create_dir_all(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);
        fs::write(
            repo.join("cruise.yaml"),
            "command: [echo]\nsteps:\n  s:\n    prompt: hi",
        )
        .unwrap_or_else(|e| panic!("{e:?}"));

        let manager = SessionManager::new(tmp.path().join(".cruise"));
        let mut session = make_session("20260619200003", &repo);
        session.phase = SessionPhase::Suspended;
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then
        assert!(
            result.is_err(),
            "regenerate_plan_for_session should fail for Suspended phase"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_failed_phase_fails() {
        // Given: a Failed session
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let repo = tmp.path().join("repo");
        fs::create_dir_all(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);
        fs::write(
            repo.join("cruise.yaml"),
            "command: [echo]\nsteps:\n  s:\n    prompt: hi",
        )
        .unwrap_or_else(|e| panic!("{e:?}"));

        let manager = SessionManager::new(tmp.path().join(".cruise"));
        let mut session = make_session("20260619200004", &repo);
        session.phase = SessionPhase::Failed("prior error".to_string());
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then
        assert!(
            result.is_err(),
            "regenerate_plan_for_session should fail for Failed phase"
        );
    }

    // -----------------------------------------------------------------------
    // regenerate_plan_for_session — success paths
    // -----------------------------------------------------------------------

    /// Config for a command backend that actually produces a plan: it drains
    /// stdin (the resolved prompt) and writes a fixed markdown plan --
    /// `MOCK_PLAN_CONTENT` -- to `{plan}` (resolved via the `PLAN_FILE` env
    /// var). Mirrors the `sh -c` command-backend pattern already used in
    /// `planning.rs`'s tests (e.g.
    /// `run_plan_prompt_template_forwards_default_env_to_command_backend`).
    fn regenerate_success_config_yaml() -> &'static str {
        // Uses a two-hash raw string delimiter because the YAML content below
        // contains a literal `"#` sequence (the mock plan heading), which
        // would otherwise terminate a single-hash raw string early.
        r##"command: ["sh", "-c", "cat >/dev/null; printf '%s' \"$MOCK_PLAN_CONTENT\" > \"$PLAN_FILE\""]
env:
  PLAN_FILE: "{plan}"
  MOCK_PLAN_CONTENT: "# Regenerated Plan Title\n\nStep 1: do the thing.\n"
steps:
  s:
    prompt: hi
"##
    }

    /// Build a session + manager pair with a real git repo, a command-backend
    /// config that successfully writes a plan (see
    /// [`regenerate_success_config_yaml`]), and `session.config_path` pointing
    /// at it (`load_config` falls back to the session dir otherwise).
    fn make_regenerate_success_fixture(id: &str, tmp: &TempDir) -> (SessionManager, SessionState) {
        let repo = tmp.path().join("repo");
        fs::create_dir_all(&repo).unwrap_or_else(|e| panic!("{e:?}"));
        init_git_repo(&repo);
        let config_path = repo.join("cruise.yaml");
        fs::write(&config_path, regenerate_success_config_yaml())
            .unwrap_or_else(|e| panic!("{e:?}"));

        let manager = SessionManager::new(tmp.path().join(".cruise"));
        let mut session = make_session(id, &repo);
        session.config_path = Some(config_path);
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));
        (manager, session)
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_draft_transitions_to_awaiting_approval() {
        // Given: a Draft session and a config whose command backend successfully
        // writes a plan
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let (manager, mut session) = make_regenerate_success_fixture("20260701090001", &tmp);
        session.phase = SessionPhase::Draft;
        manager.save(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then: succeeds and transitions Draft -> AwaitingApproval
        assert!(result.is_ok(), "expected Ok, got: {result:?}");
        assert_eq!(session.phase, SessionPhase::AwaitingApproval);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_awaiting_input_transitions_to_awaiting_approval() {
        // Given: an AwaitingInput session (blocked on an SDK ask_user answer)
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let (manager, mut session) = make_regenerate_success_fixture("20260701090002", &tmp);
        session.phase = SessionPhase::AwaitingInput;
        manager.save(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then: succeeds and transitions AwaitingInput -> AwaitingApproval
        assert!(result.is_ok(), "expected Ok, got: {result:?}");
        assert_eq!(session.phase, SessionPhase::AwaitingApproval);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_awaiting_approval_stays_awaiting_approval() {
        // Given: a session already in AwaitingApproval (regenerating before approval)
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let (manager, mut session) = make_regenerate_success_fixture("20260701090003", &tmp);
        session.phase = SessionPhase::AwaitingApproval;
        manager.save(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then: succeeds and stays in AwaitingApproval (no-op transition)
        assert!(result.is_ok(), "expected Ok, got: {result:?}");
        assert_eq!(session.phase, SessionPhase::AwaitingApproval);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_planned_stays_planned() {
        // Given: an already-approved Planned session. Regenerating its plan must
        // NOT silently un-approve it back to AwaitingApproval.
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let (manager, mut session) = make_regenerate_success_fixture("20260701090004", &tmp);
        session.phase = SessionPhase::Planned;
        manager.save(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then: succeeds and preserves the Planned (approved) phase
        assert!(result.is_ok(), "expected Ok, got: {result:?}");
        assert_eq!(
            session.phase,
            SessionPhase::Planned,
            "Planned session must not be silently un-approved after regeneration"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_clears_plan_error_on_success() {
        // Given: a session carrying a stale plan_error from a prior failed attempt
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let (manager, mut session) = make_regenerate_success_fixture("20260701090005", &tmp);
        session.phase = SessionPhase::AwaitingApproval;
        session.plan_error = Some("previous planning attempt failed".to_string());
        manager.save(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then: succeeds and clears the stale plan_error
        assert!(result.is_ok(), "expected Ok, got: {result:?}");
        assert_eq!(
            session.plan_error, None,
            "plan_error should be cleared after a successful regeneration"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn test_regenerate_plan_for_session_refreshes_title_from_plan() {
        // Given: a session with a stale title from a previous plan
        let _guard = lock_process();
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let (manager, mut session) = make_regenerate_success_fixture("20260701090006", &tmp);
        session.phase = SessionPhase::AwaitingApproval;
        session.title = Some("Old stale title".to_string());
        manager.save(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When
        let result = regenerate_plan_for_session(&manager, &mut session, 0).await;

        // Then: succeeds and the title is refreshed from the newly generated
        // plan's heading (see `regenerate_success_config_yaml`'s
        // `MOCK_PLAN_CONTENT`)
        assert!(result.is_ok(), "expected Ok, got: {result:?}");
        assert_eq!(
            session.title.as_deref(),
            Some("Regenerated Plan Title"),
            "session title should be refreshed from the regenerated plan content"
        );
    }

    #[test]
    fn test_finalize_skip_planning_session_lands_in_planned() {
        // Given: a session manager and a fresh session in AwaitingApproval state
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let base_dir = tmp.path().join("repo");
        let cruise_home = tmp.path().join(".cruise");
        fs::create_dir_all(&base_dir).unwrap_or_else(|e| panic!("{e:?}"));
        let manager = SessionManager::new(cruise_home);
        let mut session = SessionState::new(
            "20260621100000".to_string(),
            base_dir,
            "cruise.yaml".to_string(),
            "implement the feature".to_string(),
        );
        // SessionState::new starts in AwaitingApproval — the default
        assert_eq!(session.phase, SessionPhase::AwaitingApproval);
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When: finalize_skip_planning_session is called
        finalize_skip_planning_session(&manager, &mut session).unwrap_or_else(|e| panic!("{e:?}"));

        // Then: phase transitions to Planned (not stuck at AwaitingApproval)
        assert_eq!(
            session.phase,
            SessionPhase::Planned,
            "skip-planning session should land in Planned, got {:?}",
            session.phase
        );
    }

    #[test]
    fn test_finalize_skip_planning_session_clears_worktree_for_repo_session() {
        // Given: a repo-backed session in AwaitingApproval with a worktree_path set.
        // The "wt" directory is intentionally absent: worktree_context() returns None so
        // the git-level worktree removal is skipped, but cleanup_after_approval still
        // clears worktree_path on the session. Actual filesystem deletion is covered by
        // repo_clone::tests::test_cleanup_after_approval_removes_clone_and_clears_worktree_path.
        let tmp = TempDir::new().unwrap_or_else(|e| panic!("{e:?}"));
        let base_dir = tmp.path().join("repo");
        let cruise_home = tmp.path().join(".cruise");
        fs::create_dir_all(&base_dir).unwrap_or_else(|e| panic!("{e:?}"));
        let manager = SessionManager::new(cruise_home);
        let mut session = SessionState::new(
            "20260621100001".to_string(),
            base_dir.clone(),
            "cruise.yaml".to_string(),
            "implement the feature".to_string(),
        );
        session.repo = Some("owner/repo".to_string());
        session.worktree_path = Some(base_dir.join("wt"));
        session.worktree_branch = Some("cruise/20260621100001-task".to_string());
        manager.create(&session).unwrap_or_else(|e| panic!("{e:?}"));

        // When: finalize_skip_planning_session is called
        finalize_skip_planning_session(&manager, &mut session).unwrap_or_else(|e| panic!("{e:?}"));

        // Then: phase is Planned and the temporary clone worktree_path is cleared
        assert_eq!(
            session.phase,
            SessionPhase::Planned,
            "repo session should land in Planned after skip-planning, got {:?}",
            session.phase
        );
        assert!(
            session.worktree_path.is_none(),
            "worktree_path should be cleared by cleanup_after_approval for repo session"
        );
    }
}