argonauts-app 0.1.0

Argonauts Code's app controller and run host: the conversation, routing, choosing Captain or crew, and the run host each run has.
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
//! The conversation with the main agent: a new request's estimate, `/ask`, `/captain` and
//! `/crew`, and the `StartWork` choice. Every turn resumes the conversation's CLI session in the
//! repo root, read-only but for a direct turn: Captain you choose, which `direct` runs in your
//! checkout as the CLI itself would.
//!
//! Text typed while a turn runs waits in a queue and is sent once the turn ends. `lifecycle`
//! runs a turn from start to finish, `offer` asks whether to start work, `excerpt` primes a
//! session that can't resume the conversation, and `carry` makes a new main model on another CLI
//! its summary. `/clear` starts a new conversation, and `past` reads earlier ones back so `/resume`
//! can carry them on. `agent` does the same for the main agent CLI's own sessions, and `agent` for
//! the sessions of a run's agents, which carry on as direct conversations in the folder each ran
//! in.
//!
//! The commands every agent CLI has in some form work on the conversation whatever CLI the
//! main agent is: `side` asks and names off the record, compacts, recaps, and reviews; `copies`
//! branches, forks, exports, and copies; `prefs` holds `/fast`, `/output-style`, `/plan`, and
//! `/goal`.

mod agent;
#[cfg(test)]
mod agent_tests;
#[cfg(test)]
mod autopilot_tests;
mod carry;
#[cfg(test)]
mod carry_tests;
mod cli;
#[cfg(test)]
mod cli_tests;
mod copies;
mod crew;
mod direct;
#[cfg(test)]
mod direct_outside_tests;
#[cfg(test)]
mod direct_tests;
mod edits;
mod excerpt;
mod fork;
mod goal;
mod handover;
mod keep;
mod lifecycle;
mod live;
#[cfg(test)]
mod live_tests;
#[cfg(test)]
mod main_session_tests;
#[cfg(test)]
mod mention_tests;
mod mirror;
mod offer;
mod past;
#[cfg(test)]
mod picture_tests;
mod prefs;
pub(crate) mod redraw;
#[cfg(test)]
mod request_tests;
#[cfg(test)]
mod resume_tests;
mod run_news;
mod run_worktree;
#[cfg(test)]
mod shared_tests;
mod side;
mod standing;
mod talk;
#[cfg(test)]
mod tests;
mod tools;

pub(crate) use crew::{cli_sessions as crew_cli_sessions, rows as crew_rows};
use std::collections::{HashMap, VecDeque};
use std::path::Path;
use std::time::{Duration, Instant};

use anyhow::Context as _;

use argonauts_core::NoteLevel;
use argonauts_core::RunId;
use argonauts_core::agent_sessions::{AgentSessionRef, Resume};
use argonauts_core::api::{RunSummary, StartRun};
use argonauts_core::app::{
    AppEvent, AppMode, CommandReply, ConversationEvent, ConversationSummary, ReplayTurn, UiCommand,
    ViewData,
};
use argonauts_core::cli_commands::CliCommand;
use argonauts_core::cli_sessions::{CliSession, CliSessionRef};
use argonauts_core::events::context::{ContextDetail, ContextUsage};
use argonauts_core::events::run::{Party, RunMode};
use argonauts_core::events::session::CliWorkflowRun;
use argonauts_core::input::SlashCommand;
use argonauts_core::names::{ChoiceId, ConversationId, ModelRef, SessionKey, TurnId, WorkflowId};
use argonauts_core::pictures::Picture;
use argonauts_core::session_runner::RunnerEvent;
use argonauts_core::settings::approvals::ApprovalMode;
use argonauts_core::settings::workflows::RunStyle;
use argonauts_core::steps::intake::ChangeEstimate;
use argonauts_core::steps::{StepError, StepOutcome};
use argonauts_core::views::{ContextRow, InnerAgent};
use tokio_util::sync::CancellationToken;

pub(crate) use self::agent::SessionCache;
pub(crate) use self::copies::{branch, copy, export, fork};
pub(crate) use self::direct::{rewind, set_approval};
use self::excerpt::Exchange;
pub(crate) use self::goal::{
    Checked as GoalChecked, goal, goal_run_started, on_checked as goal_checked,
};
use self::lifecycle::{begin, on_done, on_event, on_retried};
use self::offer::Offer;
use self::past::PastConversation;
use self::prefs::Prefs;
pub(crate) use self::prefs::{fast, output_style, plan, saved};
pub(crate) use self::run_news::{
    hear as hear_runs, run_not_started, run_started as run_started_here,
};
pub(crate) use self::run_worktree::tidy_worktrees;
pub(crate) use self::side::{btw, compact, recap, rename, review};
pub(crate) use self::talk::conversation_steps;
#[cfg(test)]
pub(crate) use self::tests::checked_captain;
use super::instructions::{self, Command, Roster};
use super::recap::{mode_name, title_from_request};
use super::shell::ShellRan;
use super::turn::{DirectWords, TurnStep};
use super::ultra;
use super::{Ctx, Effect};
use crate::conversation_store::{
    ConversationPlace, ConversationRun, ConversationSession, ConversationStore,
};
use crate::model_memory;
use argonauts_core::events::approval::AnswerAction;

pub(crate) struct ConversationState {
    id: ConversationId,
    store: ConversationStore,
    title: String,
    turns: u32,
    /// The turn in flight.
    turn: Option<Turn>,
    /// Cancelled turns whose step hasn't ended yet. Their agent process may still write the
    /// session, so no turn resumes it meanwhile.
    stopping: Vec<TurnId>,
    /// Typed while a turn ran, oldest first, each with the last `!` command that had ended by
    /// then. Each is sent once the turn ends.
    queued: VecDeque<(Queued, u32)>,
    /// Open `StartWork` questions.
    offers: HashMap<ChoiceId, Offer>,
    /// Open "Keep this for later requests?" questions, each with what it would keep.
    keeps: HashMap<ChoiceId, keep::Kept>,
    /// `!` commands run since the main agent's last turn, oldest first. Your next message takes
    /// them: to the main agent, or to the run going.
    ran: Vec<ShellRan>,
    /// The id of the last `!` command that ended.
    last_shell: u32,
    /// While a message typed during a turn goes out, the last command that had ended when you
    /// typed it. Commands that ended later wait for your next message.
    shell_cutoff: Option<u32>,
    /// Each request and its answer, for priming a fresh session.
    history: Vec<Exchange>,
    /// What a switch of the main model to another CLI carries over.
    carry: carry::Carried,
    session: Option<ConversationSession>,
    /// The model of a CLI session this conversation carries on, kept while the conversation resumes
    /// it unless you set one.
    carried_model: Option<String>,
    /// Set when the conversation carries on an agent's session from a run: its turns are direct,
    /// with that agent, in the folder it ran in.
    place: Option<ConversationPlace>,
    context: Option<ContextRow>,
    /// Said once, so a broken journal doesn't flood the transcript.
    journal_warned: bool,
    /// Said once: Claude Code's notices stopped reading in this version.
    notice_warned: bool,
    /// The workflows the last turn's CLI ran itself, as it reported them.
    workflows: Vec<CliWorkflowRun>,
    /// Side questions asked so far, which number their turns apart from the conversation's.
    sides: u32,
    /// Choices for this app session, kept across `/clear` and `/resume`.
    prefs: Prefs,
    /// What `/goal` keeps for this conversation beyond the setting that holds the goal.
    goal: goal::Kept,
    direct: direct::DirectState,
    /// Something on screen shows the conversation's context by category, asked with this
    /// detail. Each turn's session is told as it starts.
    context_watch: Option<ContextDetail>,
    /// `/context print` asked the session at work to count itself, and prints once it has.
    print_wait: Option<PrintWait>,
    /// What the main session's setup, guide and lookup tools hold between calls.
    talk: talk::TalkState,
    /// The runs the conversation started, which the main session hears of.
    run_news: run_news::Watched,
    /// The MCP servers the main session's CLI brought from its own settings, by name, once a
    /// session has said.
    mcp_servers: Option<Vec<String>>,
    /// The pictures of the message just sent, for the turn that carries your words.
    pictures: Vec<Picture>,
    /// The files Codex's edits change, read around each edit for its patch.
    edits: edits::Edits,
}

struct Turn {
    id: TurnId,
    step: TurnStep,
    text: String,
    worker: ModelRef,
    /// Workers that already failed this turn, so a fallback never repeats one.
    tried: Vec<ModelRef>,
    cancel: CancellationToken,
    started: Instant,
    /// A run started or attached while this turn ran, so its reply starts nothing more.
    run_began: bool,
    /// `/plan <task>`: a run it starts plans first and waits for you.
    plan_first: bool,
    /// The `!` commands this turn took, back for a fallback's try.
    ran: Vec<ShellRan>,
    /// A direct turn's listener, snapshot, and what it ran.
    direct: Option<direct::Running>,
    /// What the session takes while it works: your messages, send now, and the screen.
    live: live::Live,
    /// The CLI's last count of the session's context, which the record keeps as the turn ends.
    counted: Option<Box<ContextUsage>>,
    /// Its agent's CLI turned out signed out, so whoever carries it on says so.
    signed_out: Option<argonauts_core::app::SignedOut>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Queued {
    /// Your words, with the pictures their chips name.
    Request(String, Vec<Picture>),
    /// A request you typed `ultracode` in.
    WorkflowRequest(String, Vec<Picture>),
    Ask(String),
    /// One of the main agent CLI's own commands.
    Cli {
        name: String,
        args: String,
    },
    Side(String),
    Plan(String),
    /// `/captain <task>`.
    Captain(String),
    /// `/crew <task>`, whose words are read before the run starts.
    Crew(String),
    /// `/workflow <task>`, read the same way.
    Workflow(String),
    /// Your words for a subagent of the conversation's session, which the main agent passes on.
    Subagent {
        name: String,
        text: String,
    },
}

/// From a turn's task to the loop.
#[derive(Debug)]
pub(crate) enum TurnUpdate {
    /// An event of the turn's session, as it happens.
    Event { turn: TurnId, event: RunnerEvent },
    /// The step ended: its output, or why there is none.
    Done {
        turn: TurnId,
        outcome: Result<StepOutcome, StepError>,
    },
    /// A direct turn's CLI asks you something, and waits for the answer.
    Asked { turn: TurnId, asked: direct::Asked },
    /// What the turn's CLI asked was answered, or closed without an answer.
    Answered { turn: TurnId },
    /// The answer didn't fit its format, and the step asks the agent for it again.
    Retried { turn: TurnId },
}

impl ConversationState {
    /// A new conversation, with an id set aside so no other app session takes it.
    pub(crate) fn new(ctx: &Ctx) -> anyhow::Result<Self> {
        let conversation_dir = ctx.env.conversation_dir();
        let id = ConversationStore::claim(&conversation_dir).unwrap_or_else(|err| {
            // The journal's first write tells you, as it fails the same way.
            tracing::warn!("setting a conversation id aside: {err:#}");
            ConversationStore::next_id(&conversation_dir)
        });
        Ok(Self::empty(ctx, id))
    }

    fn empty(ctx: &Ctx, id: ConversationId) -> Self {
        let store = ConversationStore::open(&ctx.env.conversation_dir(), &id);
        Self {
            id,
            store,
            title: String::new(),
            turns: 0,
            turn: None,
            stopping: Vec::new(),
            queued: VecDeque::new(),
            offers: HashMap::new(),
            keeps: HashMap::new(),
            ran: Vec::new(),
            last_shell: 0,
            shell_cutoff: None,
            history: Vec::new(),
            carry: carry::Carried::default(),
            session: None,
            carried_model: None,
            place: None,
            context: None,
            journal_warned: false,
            notice_warned: false,
            workflows: Vec::new(),
            sides: 0,
            prefs: Prefs::starting(ctx.settings()),
            goal: goal::Kept::default(),
            direct: direct::DirectState::default(),
            context_watch: None,
            print_wait: None,
            talk: talk::TalkState::default(),
            run_news: run_news::Watched::default(),
            mcp_servers: None,
            pictures: Vec::new(),
            edits: edits::Edits::default(),
        }
    }

    /// An earlier conversation, carried on: its turns go on numbering from its last, its
    /// answers prime a session that can't resume, and its CLI session resumes when the
    /// same agent takes the next turn.
    fn resumed(ctx: &Ctx, past: &PastConversation) -> Self {
        let id = past.summary.conversation.clone();
        let store = ConversationStore::open(&ctx.env.conversation_dir(), &id);
        let session = store.session();
        let place = store.place();
        let history = past
            .since_compacted()
            .iter()
            .filter_map(|turn| Some(Exchange::new(turn.request.clone(), turn.reply.as_ref()?)))
            .collect();
        let context = past::recorded_row(&store, past);
        Self {
            id,
            store,
            title: past.summary.title.clone(),
            turns: past.last_turn_number(),
            turn: None,
            stopping: Vec::new(),
            queued: VecDeque::new(),
            offers: HashMap::new(),
            keeps: HashMap::new(),
            ran: Vec::new(),
            last_shell: 0,
            shell_cutoff: None,
            history,
            carry: carry::Carried::default(),
            session,
            carried_model: None,
            place,
            context,
            journal_warned: false,
            notice_warned: false,
            workflows: Vec::new(),
            sides: 0,
            prefs: Prefs::starting(ctx.settings()),
            goal: goal::Kept::default(),
            direct: direct::DirectState::default(),
            context_watch: None,
            print_wait: None,
            talk: talk::TalkState::default(),
            run_news: run_news::Watched::default(),
            mcp_servers: None,
            pictures: Vec::new(),
            edits: edits::Edits::default(),
        }
    }

    /// The agent whose session this conversation carries on, when its CLI isn't the main
    /// agent's. One that carries on a run's session keeps that agent whichever is the main one.
    fn written_by_another_cli(&self, ctx: &Ctx) -> Option<ModelRef> {
        let main = &ctx.settings().crew.main;
        self.session
            .as_ref()
            .filter(|session| self.place.is_none() && session.worker.agent != main.agent)
            .map(|session| session.worker.clone())
    }

    /// A new conversation to take `current`'s place, with `current`'s number when nothing was
    /// ever written there, so opening the app to carry a session on leaves no gap.
    fn instead_of(ctx: &Ctx, current: &ConversationState) -> anyhow::Result<Self> {
        if current.store.is_unused() {
            return Ok(Self::empty(ctx, current.id.clone()));
        }
        Self::new(ctx)
    }

    /// A new conversation that carries on a CLI's own session: its next turn resumes that
    /// session, or a copy of it when the launch forks, and its turns so far prime a fresh one on
    /// a fallback.
    fn carrying(
        ctx: &Ctx,
        current: &ConversationState,
        picked: &cli::Picked,
        worker: ModelRef,
    ) -> anyhow::Result<Self> {
        let mut conversation = Self::instead_of(ctx, current)?;
        let session = ConversationSession {
            worker,
            cli_session: picked.launch.session.clone(),
            fork: picked.launch.fork,
        };
        // Saved now, so `/resume conversation-<n>` finds the session after a restart too.
        conversation.store.save_session(&session)?;
        conversation.session = Some(session);
        conversation.title = picked.session.title.clone();
        conversation.history = picked.history();
        conversation.carried_model = picked.launch.model.clone();
        Ok(conversation)
    }

    /// Makes the next turn carry on a copy of `session`, which stays as it was.
    fn branch_off(&mut self, session: ConversationSession) -> anyhow::Result<()> {
        let session = ConversationSession {
            fork: true,
            ..session
        };
        // Saved now, so the branch still copies the session after a restart.
        self.store.save_session(&session)?;
        self.session = Some(session);
        Ok(())
    }

    /// A new conversation that carries on an agent's session from a run, through its CLI's own
    /// resume, with that agent, in `place.folder`.
    fn continuing(
        ctx: &Ctx,
        current: &ConversationState,
        found: &agent::Found,
        place: ConversationPlace,
    ) -> anyhow::Result<Self> {
        let mut conversation = Self::instead_of(ctx, current)?;
        let cli_session = found
            .cli_session
            .clone()
            .context("the session left no id to resume")?;
        let session = ConversationSession {
            worker: place.worker.clone(),
            cli_session,
            fork: false,
        };
        // Saved now, so `/resume conversation-<n>` carries it on in the same folder after a
        // restart.
        conversation.store.save_session(&session)?;
        conversation.store.save_place(&place)?;
        conversation.session = Some(session);
        conversation.title = found
            .task_title
            .clone()
            .unwrap_or_else(|| format!("{} {}", found.role.as_str(), found.run));
        conversation.carried_model = found.worker.model.clone();
        conversation.place = Some(place);
        Ok(conversation)
    }

    /// The conversation after this one keeps this app session's choices and open questions.
    /// What `/rewind` can put back stays only while both work in the same folder, and a worktree
    /// made again for this one goes once the next one works elsewhere.
    fn succeed(&mut self, ctx: &Ctx, mut next: ConversationState) {
        next.offers = std::mem::take(&mut self.offers);
        next.prefs = self.prefs.clone();
        next.direct = self.direct.carry();
        let same_folder = next.place.as_ref().map(|place| &place.folder)
            == self.place.as_ref().map(|place| &place.folder);
        if !same_folder {
            next.direct.forget_turns();
        }
        next.last_shell = self.last_shell;
        let left = self
            .place
            .take()
            .filter(|place| place.made_again && !same_folder);
        if next.id != self.id {
            self.store.release_if_empty();
            let out = ctx.out.clone();
            model_memory::conversation_ended(&ctx.env.paths, ctx.names.names(), move |text| {
                out.send(AppEvent::Notice {
                    level: NoteLevel::Info,
                    text,
                });
            });
        }
        *self = next;
        if let Some(place) = left {
            run_worktree::leave(ctx, &place);
        }
    }

    /// Where its turns work: the folder of the agent's session it carries on, or your checkout.
    pub(super) fn folder<'a>(&'a self, ctx: &'a Ctx) -> &'a Path {
        self.place
            .as_ref()
            .map_or(ctx.env.repo_root.as_path(), |place| place.folder.as_path())
    }

    /// Whether `turn` runs in the conversation's own session, on its CLI, so what that session
    /// reports of itself, such as its command list, is the conversation's. A review, a side
    /// question, or another agent answering in its place reports its own.
    pub(crate) fn speaks_for_the_conversation(&self, ctx: &Ctx, turn: &TurnId) -> bool {
        self.turn
            .as_ref()
            .filter(|running| &running.id == turn)
            .is_some_and(|running| {
                !running.step.own_session() && running.worker.agent == self.cli(ctx)
            })
    }

    /// The CLI its turns go to, such as `claude-code`: the agent's whose session it carries on,
    /// or the main agent's.
    pub(super) fn cli<'a>(&'a self, ctx: &'a Ctx) -> &'a str {
        self.place
            .as_ref()
            .map_or(ctx.settings().crew.main.agent.as_str(), |place| {
                place.worker.agent.as_str()
            })
    }

    /// Typed while a turn runs, to go out once it ends. A message typed again as it goes out
    /// keeps the commands it was typed after.
    fn queue(&mut self, queued: Queued) {
        let after = self.shell_cutoff.unwrap_or(self.last_shell);
        self.queued.push_back((queued, after));
    }
}

/// The CLIs' own sessions, newest first, for `/resume`: this folder's, or every folder's.
pub(crate) async fn cli_sessions(ctx: &Ctx, every_folder: bool) -> Vec<CliSession> {
    cli::list(ctx, every_folder).await
}

/// Earlier conversations in this repo, newest first, for `/resume`. One whose run waits on you
/// is marked, since a run is found through the conversation that started it.
pub(crate) fn list(ctx: &Ctx, runs: &[RunSummary]) -> Vec<ConversationSummary> {
    let waiting: Vec<RunId> = runs
        .iter()
        .filter(|run| run.needs_you())
        .map(|run| run.run.clone())
        .collect();
    past::list(&ctx.env.conversation_dir(), &waiting)
}

/// `/clear`: a new conversation with the main agent, and the screen starts over under its
/// header, which says how to get back to the old one when it had turns. The old one stays in
/// `/resume`, open `StartWork` questions stay answerable, and runs keep going.
pub(crate) fn clear(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
) -> anyhow::Result<Vec<Effect>> {
    if !settled(conversation) {
        ctx.notice(
            NoteLevel::Info,
            "A turn is still running. Esc stops it, then /clear starts a new conversation.",
        );
        return Ok(Vec::new());
    }
    let old = conversation.id.clone();
    let had_turns = conversation.turns > 0;
    // A goal covers the work that follows in its own conversation, and no other.
    goal::forget(ctx)?;
    conversation.succeed(ctx, ConversationState::new(ctx)?);
    ctx.send(AppEvent::ConversationCleared {
        way_back: had_turns.then_some(old),
    });
    Ok(vec![Effect::Status])
}

/// `/resume conversation-<n>`, or Enter on a conversation in the picker: draws it again as it
/// printed live, the runs it started included, and carries it on. The next turn resumes its CLI session, and a session another CLI wrote makes
/// that CLI the main agent, as picking the CLI's own session does. Where that CLI can't carry
/// it on, the main agent starts a fresh session primed with the conversation so far. One that
/// carries on an agent's session from a run is checked again first, since its run may be going
/// again.
pub(crate) async fn resume(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    id: &ConversationId,
) -> anyhow::Result<Vec<Effect>> {
    let id = past::normalize(id);
    if id == conversation.id && (conversation.turns > 0 || !conversation.store.runs().is_empty()) {
        ctx.notice(
            NoteLevel::Info,
            format!("{id} is the conversation on screen."),
        );
        return Ok(Vec::new());
    }
    if !settled(conversation) {
        ctx.notice(
            NoteLevel::Info,
            "A turn is still running. Esc stops it, then /resume carries the other one on.",
        );
        return Ok(Vec::new());
    }
    let past = past::load(&ctx.env.conversation_dir(), &id).filter(|past| !past.is_empty());
    let Some(past) = past else {
        anyhow::bail!("There's no conversation {id} in this repo. /resume lists them.");
    };
    let mut resumed = ConversationState::resumed(ctx, &past);
    // It carries on an agent's session, whose run may be going again since, and whose worktree may
    // be gone again.
    let mut made_again = false;
    if let Some(place) = resumed.place.as_mut() {
        if let Some(why) = agent::place_cannot(ctx, place).await {
            anyhow::bail!("{id} can't carry on: {why}.");
        }
        made_again = run_worktree::ensure_worktree(
            &ctx.env.repo_root,
            &place.folder,
            place.branch.as_deref(),
        )
        .with_context(|| format!("{id} can't carry on"))?;
        if made_again && !place.made_again {
            place.made_again = true;
            resumed.store.save_place(place)?;
        }
    }
    let where_it_works = resumed
        .place
        .as_ref()
        .map(|place| where_it_works(ctx, place, made_again));
    let written_elsewhere = resumed.written_by_another_cli(ctx);
    // Another CLI's session carries on in that CLI too, which takes the main agent's place.
    let taken_over = written_elsewhere
        .as_ref()
        .and_then(|writer| make_main(ctx, writer));
    let who_takes_it = written_elsewhere
        .as_ref()
        .map(|writer| who_takes_it(ctx, writer, taken_over.is_some()));
    conversation.succeed(ctx, resumed);
    ctx.send(AppEvent::ConversationResumed {
        conversation: id,
        title: past.summary.title.clone(),
        worker: past.summary.worker.clone(),
        turns: Vec::new(),
        artifacts: past::artifacts(&ctx.env.conversation_dir(), &past.summary.conversation),
    });
    redraw::conversation(ctx, &conversation.store, &past.runs);
    for text in where_it_works.into_iter().chain(who_takes_it) {
        ctx.notice(NoteLevel::Info, text);
    }
    note_run_on_screen(ctx);
    let mut effects = taken_over.unwrap_or_default();
    effects.push(Effect::Status);
    Ok(effects)
}

/// Before `run` comes on screen, the conversation that started it does, unless it is on screen
/// already or the run has none. `None` when it can't come now, as while a turn runs, and then
/// the run stays where it is too.
pub(crate) async fn come_home(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    run: &RunId,
) -> anyhow::Result<Option<Vec<Effect>>> {
    let home = past::home_of(&ctx.env.conversation_dir(), run);
    let Some(home) = home.filter(|home| *home != conversation.id) else {
        return Ok(Some(Vec::new()));
    };
    if !settled(conversation) {
        ctx.notice(
            NoteLevel::Info,
            format!(
                "{run} opens in {home}, the conversation that started it. A turn is still \
                 running here: Esc stops it, then open the run again."
            ),
        );
        return Ok(None);
    }
    resume(ctx, conversation, &home).await.map(Some)
}

/// Who takes the next message of a conversation `writer`'s CLI wrote, once it is the main agent
/// or couldn't be made it.
fn who_takes_it(ctx: &Ctx, writer: &ModelRef, is_main_now: bool) -> String {
    let name = cli::name(ctx, &writer.agent);
    if is_main_now {
        return format!(
            "{name} wrote this conversation, so it is the main agent now, as /model would make \
             it, and your next message goes to its session through its own resume."
        );
    }
    let main = &ctx.settings().crew.main;
    format!(
        "{name} wrote this conversation and can't carry its session on here, so {main} takes \
         the next message, primed with its last turns."
    )
}

/// After a conversation is carried on while a run is on screen, where plain text goes.
fn note_run_on_screen(ctx: &Ctx) {
    if let AppMode::Working { run } | AppMode::Pausing { run } | AppMode::Waiting { run, .. } =
        &ctx.mode
    {
        ctx.notice(
            NoteLevel::Info,
            format!(
                "{run} is on screen, so plain text guides it. /ask talks to this conversation."
            ),
        );
    }
}

/// `/resume conversation-<n> branch`, or `b` on a conversation in the picker: a new conversation
/// that starts from that one's turns, as `/branch` does from the one on screen. The original
/// keeps its turns and its CLI session: the new one's first turn carries on a copy of that
/// session where the main agent's CLI can fork it, and otherwise starts a fresh session
/// primed with the turns.
pub(crate) async fn branch_from(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    id: &ConversationId,
) -> anyhow::Result<Vec<Effect>> {
    if !settled(conversation) {
        ctx.notice(NoteLevel::Info, BRANCH_WAITS);
        return Ok(Vec::new());
    }
    let id = past::normalize(id);
    let past = past::load(&ctx.env.conversation_dir(), &id).filter(|past| !past.is_empty());
    let Some(past) = past else {
        anyhow::bail!("There's no conversation {id} in this repo. /resume lists them.");
    };
    let original = ConversationState::resumed(ctx, &past);
    // One that works in a run's folder keeps its session there, where this branch isn't.
    let to_copy = original
        .session
        .filter(|session| original.place.is_none() && main_agent_forks(ctx, &session.worker.agent));
    let mut next = ConversationState::instead_of(ctx, conversation)?;
    next.title = past.summary.title.clone();
    next.history = original.history;
    let starts = match to_copy {
        Some(session) => {
            let starts = branch_copies("its session", &cli::binary(ctx, &session.worker.agent));
            next.branch_off(session)?;
            starts
        }
        None => BRANCH_STARTS.to_owned(),
    };
    conversation.succeed(ctx, next);
    ctx.send(AppEvent::ConversationResumed {
        conversation: conversation.id.clone(),
        title: conversation.title.clone(),
        worker: past.summary.worker.clone(),
        turns: Vec::new(),
        artifacts: Vec::new(),
    });
    let original = ConversationStore::open(&ctx.env.conversation_dir(), &id);
    redraw::conversation(ctx, &original, &past.runs);
    ctx.notice(
        NoteLevel::Info,
        format!(
            "Branched from {id} into {}. {starts} /resume {id} goes back to the original.",
            conversation.id
        ),
    );
    note_run_on_screen(ctx);
    Ok(vec![Effect::Status])
}

const BRANCH_WAITS: &str =
    "A turn is still running. Esc stops it, then /resume branches from the other one.";

const BRANCH_STARTS: &str = "Your next message starts a new session that has its last turns.";

/// What a branch says when its first turn carries on a copy of `what`, such as "it".
fn branch_copies(what: &str, binary: &str) -> String {
    format!("Your next message carries on a copy of {what}, through {binary}'s own fork.")
}

/// Whether the main agent can carry on a copy of a session `agent`'s CLI wrote: it is that
/// CLI, conversations resume their sessions, and the CLI has a fork of its own.
fn main_agent_forks(ctx: &Ctx, agent: &str) -> bool {
    let settings = ctx.settings();
    let main = &settings.crew.main;
    main.agent == agent
        && settings.sessions.conversation_resumes
        && ctx
            .services
            .workers
            .get(main)
            .is_ok_and(|handle| handle.capabilities().fork)
}

/// `/resume claude-code:<id> branch`, or `b` on a CLI's session in the picker: reprints its
/// last turns and carries on from them, so the CLI's own session is never written to. Where
/// the main agent's CLI wrote the session here and can fork it, the next turn carries on a copy
/// of it. Otherwise it starts a new session primed with the turns.
pub(crate) async fn branch_from_cli_session(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    reference: &CliSessionRef,
) -> anyhow::Result<Vec<Effect>> {
    if !settled(conversation) {
        ctx.notice(NoteLevel::Info, BRANCH_WAITS);
        return Ok(Vec::new());
    }
    let mut picked = cli::pick(ctx, reference).await?;
    let worker = ctx.settings().crew.main.clone();
    // A CLI finds a session to copy from the folder it ran in, which is where turns run here.
    let copies = picked.session.folder == ctx.env.repo_root
        && main_agent_forks(ctx, &picked.session.agent_id.0);
    let next = if copies {
        picked.launch = picked.launch.forked();
        ConversationState::carrying(ctx, conversation, &picked, worker.clone())?
    } else {
        let mut next = ConversationState::instead_of(ctx, conversation)?;
        next.title = picked.session.title.clone();
        next.history = picked.history();
        next
    };
    conversation.succeed(ctx, next);
    ctx.send(AppEvent::ConversationResumed {
        conversation: conversation.id.clone(),
        title: conversation.title.clone(),
        worker: Some(worker.clone()),
        turns: picked.replay(&picked.worker(&worker)),
        artifacts: Vec::new(),
    });
    let short = cli::short_id(&picked.session.id);
    let starts = if copies {
        branch_copies("it", picked.binary)
    } else {
        BRANCH_STARTS.to_owned()
    };
    ctx.notice(
        NoteLevel::Info,
        format!(
            "Branched from {} session {short}, which stays as it was. {starts}",
            picked.name
        ),
    );
    note_run_on_screen(ctx);
    Ok(vec![Effect::Status])
}

/// `/resume claude-code:<id>`, or Enter on a CLI's session in the picker: reprints its last
/// turns condensed, as a resumed conversation does, and carries it on in a new conversation
/// whose next turn goes through the CLI's own resume. A session another CLI wrote makes that
/// CLI the main agent, so it carries on the same way.
pub(crate) async fn resume_cli_session(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    reference: &CliSessionRef,
) -> anyhow::Result<Vec<Effect>> {
    if !settled(conversation) {
        ctx.notice(
            NoteLevel::Info,
            "A turn is still running. Esc stops it, then /resume carries the other one on.",
        );
        return Ok(Vec::new());
    }
    let picked = cli::pick(ctx, reference).await?;
    // A conversation that only branches from the session isn't the session itself.
    let on_screen = conversation
        .session
        .as_ref()
        .is_some_and(|session| !session.fork && session.cli_session == picked.launch.session);
    let short = cli::short_id(&picked.session.id).to_owned();
    if on_screen {
        ctx.notice(
            NoteLevel::Info,
            format!(
                "{} session {short} is the conversation on screen.",
                picked.name
            ),
        );
        return Ok(Vec::new());
    }
    let default = ctx.settings().crew.main.clone();
    let worker = picked.worker(&default);
    // Another CLI's session carries on in that CLI too, which takes the main agent's place.
    let taken_over = if worker.agent == default.agent {
        Some(Vec::new())
    } else {
        make_main(ctx, &worker)
    };
    let next = ConversationState::carrying(ctx, conversation, &picked, worker.clone())?;
    conversation.succeed(ctx, next);
    ctx.send(AppEvent::ConversationResumed {
        conversation: conversation.id.clone(),
        title: conversation.title.clone(),
        worker: Some(worker.clone()),
        turns: picked.replay(&worker),
        artifacts: Vec::new(),
    });
    let binary = picked.binary;
    let how = match &taken_over {
        Some(_) if worker.agent == default.agent => {
            format!("Your next message goes to it through {binary}'s own resume.")
        }
        Some(_) => format!(
            "{} is the main agent now, as /model would make it, and your next message goes to \
             the session through {binary}'s own resume.",
            picked.name
        ),
        None => format!(
            "{} can't take a turn here, so {default} takes the next message, primed with this \
             session's last turns.",
            picked.name
        ),
    };
    ctx.notice(
        NoteLevel::Info,
        format!("Carrying on {} session {short}. {how}", picked.name),
    );
    note_run_on_screen(ctx);
    let mut effects = taken_over.unwrap_or_default();
    effects.push(Effect::Status);
    Ok(effects)
}

/// Makes `worker`'s CLI the main agent for this app session, so a session that CLI wrote carries
/// on through its own resume. `None` when it can't take a turn here or can't resume, and the
/// main agent stays.
fn make_main(ctx: &mut Ctx, worker: &ModelRef) -> Option<Vec<Effect>> {
    let resumes = ctx
        .services
        .workers
        .get(worker)
        .is_ok_and(|handle| handle.capabilities().resume);
    if !resumes {
        return None;
    }
    let saved = super::settings::save_answered(
        ctx,
        "crew.main",
        format!("\"{worker}\""),
        argonauts_core::settings::SettingScope::Session,
    );
    match saved {
        // The conversation that follows already holds the session, so nothing carries over
        // from the one it replaces. The new main agent still brings its own commands.
        Ok(effects) => Some(
            effects
                .into_iter()
                .map(|effect| match effect {
                    Effect::FreshConversation => Effect::CommandsChanged,
                    other => other,
                })
                .collect(),
        ),
        Err(err) => {
            tracing::warn!("making {worker} the main agent: {err:#}");
            None
        }
    }
}

/// `/resume run-<n>/s-<n>`, or `c` on a finished agent: carries the agent's session on in a new
/// conversation whose turns are direct, through its CLI's own resume, in the folder it ran in.
/// That's the run's worktree, made again from its branch when the run merged and the worktree is
/// gone. Only a run that ended lets go of its sessions: a session still running is taken over
/// instead, one whose run may still start it again waits for the run, and one that can't resume
/// shows its transcript.
pub(crate) async fn resume_agent(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    reference: &AgentSessionRef,
) -> anyhow::Result<Vec<Effect>> {
    if !settled(conversation) {
        ctx.notice(
            NoteLevel::Info,
            "A turn is still running. Esc stops it, then /resume carries the other one on.",
        );
        return Ok(Vec::new());
    }
    let (found, resume) = agent::pick(ctx, reference).await?;
    let who = found.who();
    match resume {
        Resume::Ready => {}
        Resume::Working => {
            ctx.notice(
                NoteLevel::Info,
                format!(
                    "{reference} is still working. t in its agent view takes it over, and once \
                     its run ends, its session carries on here."
                ),
            );
            return Ok(Vec::new());
        }
        Resume::RunOwns => {
            ctx.notice(NoteLevel::Info, format!("{}.", agent::owned_by(reference)));
            return Ok(Vec::new());
        }
        Resume::Cannot { why } => {
            ctx.notice(
                NoteLevel::Info,
                format!("{reference} can't carry on, since {why}. Here is its transcript."),
            );
            let raw = ctx
                .services
                .conductor
                .raw(&reference.run, &reference.session)
                .await
                .with_context(|| format!("the transcript of {reference}"))?;
            ctx.send(AppEvent::View {
                data: Box::new(ViewData::Raw { raw }),
            });
            return Ok(Vec::new());
        }
    }
    // A branch of it works in the same place on a session of its own.
    let on_screen = conversation
        .place
        .as_ref()
        .is_some_and(|place| &place.agent == reference)
        && conversation
            .session
            .as_ref()
            .map(|session| &session.cli_session)
            == found.cli_session.as_ref();
    if on_screen {
        ctx.notice(
            NoteLevel::Info,
            format!("{reference} is the conversation on screen."),
        );
        return Ok(Vec::new());
    }
    let repo = ctx.env.repo_root.clone();
    let (folder, made_again) = {
        let found = found.clone();
        tokio::task::spawn_blocking(move || agent::place(&repo, &found))
            .await
            .context("making the run's worktree again")?
            .with_context(|| format!("{reference} can't carry on"))?
    };
    let place = ConversationPlace {
        agent: reference.clone(),
        worker: found.worker.clone(),
        folder,
        branch: found.branch.clone(),
        made_again,
    };
    let next = ConversationState::continuing(ctx, conversation, &found, place.clone())?;
    conversation.succeed(ctx, next);
    let worker = place.worker.clone();
    let turns = found
        .last_message
        .iter()
        .map(|text| ReplayTurn {
            from: Party::Agent {
                session: SessionKey::numbered(0),
                worker: worker.clone(),
            },
            text: text.clone(),
            steps: Vec::new(),
        })
        .collect();
    ctx.send(AppEvent::ConversationResumed {
        conversation: conversation.id.clone(),
        title: conversation.title.clone(),
        worker: Some(worker.clone()),
        turns,
        artifacts: Vec::new(),
    });
    let agent = ctx.names.worker(&worker);
    let binary = agent::binary(ctx, &worker.agent);
    let mut text = format!(
        "Carrying on {who}, {agent}. Your messages go to it through {binary}'s own resume, and \
         it can change files. "
    );
    text.push_str(&where_it_works(ctx, &place, made_again));
    ctx.notice(NoteLevel::Info, text);
    note_run_on_screen(ctx);
    Ok(vec![Effect::Status])
}

/// Which files a carried-on agent works on, said plainly: the run's worktree, not your checkout.
fn where_it_works(ctx: &Ctx, place: &ConversationPlace, made_again: bool) -> String {
    let folder = ctx.env.display(&place.folder);
    if place.folder == ctx.env.repo_root {
        return "It works in your checkout.".to_owned();
    }
    let on_branch = place
        .branch
        .as_deref()
        .map_or_else(String::new, |branch| format!(" on {branch}"));
    let mut text =
        format!("It works on the run's worktree in {folder}{on_branch}, not your checkout.");
    if made_again {
        text.push_str(" That worktree was gone, so it was made again from the branch.");
    }
    text
}

/// `argonauts -c`: the newest conversation in this repo, or a new one when there's none.
pub(crate) async fn continue_newest(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
) -> anyhow::Result<Vec<Effect>> {
    match list(ctx, &[]).first() {
        Some(newest) => {
            let id = newest.conversation.clone();
            resume(ctx, conversation, &id).await
        }
        None => {
            ctx.notice(
                NoteLevel::Info,
                "No earlier conversation in this repo, so this one starts new.",
            );
            Ok(Vec::new())
        }
    }
}

pub(crate) fn title(conversation: &ConversationState) -> &str {
    &conversation.title
}

pub(crate) fn id(conversation: &ConversationState) -> &ConversationId {
    &conversation.id
}

/// When the conversation's first words were written, for the Status tab.
pub(crate) fn started(conversation: &ConversationState) -> Option<time::OffsetDateTime> {
    conversation.store.started()
}

/// The tools a turn that starts now is offered, by name.
pub(crate) fn main_tools(ctx: &Ctx, conversation: &ConversationState) -> Vec<String> {
    tools::offered(ctx, conversation)
}

/// The servers a session's tools come from, beside Argonauts Code's own: Claude Code names an MCP
/// tool `mcp__<server>__<tool>`.
pub(crate) fn own_mcp_servers(tools: &[String]) -> Vec<String> {
    let mut servers: Vec<String> = tools
        .iter()
        .filter_map(|tool| tool.strip_prefix("mcp__")?.split_once("__"))
        .map(|(server, _)| server.to_owned())
        .filter(|server| !server.starts_with("argonauts"))
        .collect();
    servers.sort();
    servers.dedup();
    servers
}

/// The pictures of the message just sent, which the turn that carries it takes.
pub(crate) fn send_pictures(conversation: &mut ConversationState, pictures: Vec<Picture>) {
    conversation.pictures = pictures;
}

/// The MCP servers the main session's CLI said it brought, once one has started.
pub(crate) fn mcp_servers(conversation: &ConversationState) -> Option<Vec<String>> {
    conversation.mcp_servers.clone()
}

/// No step of this conversation is still running.
pub(crate) fn settled(conversation: &ConversationState) -> bool {
    conversation.turn.is_none() && conversation.stopping.is_empty()
}

/// A run started or attached: the turn in flight answers, but starts no run and offers none.
pub(crate) fn run_attached(conversation: &mut ConversationState) {
    if let Some(turn) = conversation.turn.as_mut() {
        turn.run_began = true;
    }
}

/// The runs this conversation started or opened, which carrying it on brings back to the panel.
pub(crate) fn own_runs(conversation: &ConversationState) -> Vec<RunId> {
    conversation.store.runs()
}

/// Runs that came on screen are this conversation's, so `argonauts -c` brings them back.
pub(crate) fn keep_runs(conversation: &ConversationState, runs: Vec<ConversationRun>) {
    for kept in runs {
        if let Err(err) = conversation.store.keep_run(&kept) {
            tracing::warn!("saving {} with {}: {err:#}", kept.run, conversation.id);
        }
    }
}

/// A `!` command ended. The main agent hears of it with your next message, never by itself.
pub(crate) fn shell_ran(conversation: &mut ConversationState, ran: ShellRan) {
    conversation.last_shell = ran.id;
    super::shell::hold(&mut conversation.ran, ran);
}

/// The commands the message going out now hears of. One typed while a turn ran hears only of
/// those that had ended when you typed it.
pub(crate) fn take_shell(conversation: &mut ConversationState) -> Vec<ShellRan> {
    match conversation.shell_cutoff {
        None => std::mem::take(&mut conversation.ran),
        Some(cutoff) => {
            let (heard, later) = conversation.ran.drain(..).partition(|ran| ran.id <= cutoff);
            conversation.ran = later;
            heard
        }
    }
}

/// Commands a message took but couldn't deliver wait for the next one.
pub(crate) fn give_back_shell(conversation: &mut ConversationState, ran: Vec<ShellRan>) {
    conversation.ran.splice(0..0, ran);
}

/// A message typed during a turn goes out now, or has gone: `typed_after` is the last
/// command that had ended when you typed it.
pub(crate) fn sending_queued(conversation: &mut ConversationState, typed_after: Option<u32>) {
    conversation.shell_cutoff = typed_after;
}

/// The commands you ran before a run started were for the conversation, not the run.
pub(crate) fn run_started(conversation: &mut ConversationState) {
    conversation.ran.clear();
}

/// The conversation session's row for `/context`.
pub(crate) fn context_row(conversation: &ConversationState) -> Option<ContextRow> {
    conversation.context.clone()
}

/// /context opened: the running turn's session is asked for its context by category, where
/// its CLI can say. Between turns there's no session to ask.
pub(crate) fn ask_context(conversation: &ConversationState, detail: ContextDetail) {
    if let Some(turn) = &conversation.turn {
        turn.live.ask_context(detail);
    }
}

/// How long `/context print` waits for a count before it prints the last one it has.
const PRINT_WAIT: Duration = Duration::from_secs(2);

/// What `/context print` waits for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PrintWait {
    /// The session was asked then, and hasn't answered.
    Asked(Instant),
    Counted,
}

/// `/context print` while the conversation's session is at work on a CLI that can count
/// itself: it is asked as its own /context counts, and the rows wait for the answer. False
/// where there is no one to ask, and the rows print from the last count.
pub(crate) fn count_then_print(ctx: &Ctx, conversation: &mut ConversationState) -> bool {
    let Some(turn) = conversation
        .turn
        .as_ref()
        .filter(|turn| !turn.step.own_session())
    else {
        return false;
    };
    let can_say = crate::commands::adapters_here(Some(&ctx.env), ctx.settings.resolved())
        .ok()
        .and_then(|adapters| {
            adapters
                .into_iter()
                .find(|adapter| adapter.id() == turn.worker.agent)
        })
        .is_some_and(|adapter| adapter.context_usage_line(ContextDetail::Full).is_some());
    if !can_say {
        return false;
    }
    turn.live.ask_context(ContextDetail::Full);
    conversation.print_wait = Some(PrintWait::Asked(Instant::now()));
    true
}

/// Whether the rows `/context print` waits to print should print now: the count is in, the
/// turn is over, or the session took too long to answer. True once.
pub(crate) fn print_is_due(conversation: &mut ConversationState) -> bool {
    let due = match conversation.print_wait {
        Some(PrintWait::Counted) => true,
        Some(PrintWait::Asked(at)) => conversation.turn.is_none() || at.elapsed() >= PRINT_WAIT,
        None => false,
    };
    if due {
        conversation.print_wait = None;
    }
    due
}

/// Something on screen started or stopped showing the conversation's context. The session
/// then asks after each fill, at most every 10 seconds.
pub(crate) fn watch_context(conversation: &mut ConversationState, detail: Option<ContextDetail>) {
    conversation.context_watch = detail;
    if let Some(turn) = &conversation.turn {
        turn.live.watch_context(detail);
    }
}

/// Records the request and runs `Estimate` in the conversation session.
///
/// Captain honours how you ask before it takes the request for itself. A main session that has
/// the start tool is told what your words ask and starts the run itself. One that can't start
/// a run has code read your words instead: words that plainly ask for a workflow hand the
/// request to the crew, and a reviewer you name keeps Captain doing the work, checked in a
/// worktree, with that reviewer reading the change before it lands.
pub(crate) async fn new_request(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    text: String,
) -> anyhow::Result<Vec<Effect>> {
    let code_reads_the_words = conversation.turn.is_none()
        && conversation.place.is_none()
        && !conversation.prefs.plan_first
        && direct::workflow_is_direct(ctx.settings())
        && ctx.settings().captain_or_crew.read_instructions
        && !takes_tools(ctx, conversation);
    if code_reads_the_words && instructions::plainly_asks_for_a_workflow(&text) {
        return start_as_workflow(ctx, conversation, text, "your words ask for a workflow").await;
    }
    if code_reads_the_words && instructions::names_a_reviewer(&text, &Roster::new(ctx)) {
        let text = text.trim().to_owned();
        let effects = name_the_conversation(conversation, &text);
        begin(ctx, conversation, text, |record| TurnStep::Estimate {
            uncommitted_note: record.uncommitted_note(),
            read_instructions: true,
            command: None,
        })?;
        return Ok(effects);
    }
    request(ctx, conversation, text)
}

/// Plain text while a turn runs: into the running session at once where its CLI takes it
/// there, as Claude Code does, and otherwise sent as the next turn.
pub(crate) fn queue_turn(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    text: String,
) -> anyhow::Result<Vec<Effect>> {
    let pictures = &conversation.pictures;
    let joined = conversation.turn.as_mut().is_some_and(|turn| {
        let trimmed = text.trim();
        !turn.step.own_session()
            && !trimmed.is_empty()
            && turn.live.takes_messages()
            && turn.live.send(trimmed.to_owned(), pictures.clone())
    });
    if joined {
        conversation.pictures.clear();
        return Ok(Vec::new());
    }
    request(ctx, conversation, text)
}

/// Enter on a subagent of your conversation's own session with words in the prompt. The
/// subagent has no line of its own when Argonauts Code drives its CLI, so the main agent passes
/// your words on: at once while its turn runs where the CLI takes that, else as the next
/// turn. The conversation shows `@name` and your words, never how to pass them on.
pub(crate) fn message_subagent(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    name: String,
    text: String,
) -> anyhow::Result<Vec<Effect>> {
    let words = text.trim().to_owned();
    if words.is_empty() {
        return Ok(Vec::new());
    }
    let prompt = pass_on(&name, &words);
    if let Some(turn) = conversation.turn.as_mut() {
        let joined = !turn.step.own_session()
            && turn.live.takes_messages()
            && turn
                .live
                .send_for_subagent(prompt, name.clone(), words.clone());
        if !joined {
            conversation.queue(Queued::Subagent { name, text: words });
        }
        return Ok(Vec::new());
    }
    let shown = format!("@{name} {words}");
    let mut effects = name_the_conversation(conversation, &shown);
    if is_the_cli(ctx, conversation) {
        effects.extend(direct::begin_agent(ctx, conversation, shown, prompt)?);
        return Ok(effects);
    }
    begin(ctx, conversation, shown, |_| TurnStep::Cli {
        prompt,
        direct: None,
    })?;
    Ok(effects)
}

/// What the main agent reads, to pass your message on to one of its subagents: your words
/// first, then how to pass them on. Claude Code continues a subagent with its SendMessage
/// tool, by the agent id the subagent's launch returned, and the same call resumes one that
/// finished (checked live on 2.1.292, where a call without `message` was refused, so the note
/// spells the tool's fields out).
fn pass_on(name: &str, words: &str) -> String {
    format!(
        "For your subagent \"{name}\": {words}\n\n(A message from me for your subagent \
         \"{name}\". Pass it on now with the SendMessage tool: `to` is that subagent's agent id, \
         `message` is my words above after the colon, word for word, and `summary` is a few \
         words about them. If the subagent has finished, this resumes it. Then carry on with \
         what you were doing.)"
    )
}

/// `x` on a subagent of your conversation's own session, or Ctrl+X Ctrl+K: the session stops
/// that one and goes on. Between turns there's no session to ask, and none of its subagents
/// is running either.
pub(crate) fn stop_subagent(ctx: &mut Ctx, conversation: &mut ConversationState, id: String) {
    let asked = conversation
        .turn
        .as_mut()
        .is_some_and(|turn| turn.live.stop_subagent(id));
    if !asked {
        ctx.notice(
            NoteLevel::Info,
            "That subagent isn't running, so there's nothing to stop.".to_owned(),
        );
    }
}

/// Ctrl+Enter: what you sent the running turn goes to the model now.
pub(crate) fn send_now(conversation: &mut ConversationState) {
    if let Some(turn) = conversation.turn.as_mut() {
        turn.live.send_now();
    }
}

/// ↑ on your waiting messages: the ones not sent yet come back at once, and the session is
/// asked for the ones the model hasn't read, which come back as it lets them go.
pub(crate) fn take_back(ctx: &mut Ctx, conversation: &mut ConversationState) {
    let texts: Vec<String> = conversation
        .queued
        .iter()
        .filter_map(|(queued, _)| match queued {
            Queued::Request(text, _) | Queued::WorkflowRequest(text, _) => Some(text.clone()),
            _ => None,
        })
        .collect();
    conversation
        .queued
        .retain(|(queued, _)| !matches!(queued, Queued::Request(..) | Queued::WorkflowRequest(..)));
    if let Some(turn) = conversation.turn.as_mut() {
        turn.live.take_back();
    }
    if !texts.is_empty() {
        ctx.send(AppEvent::TakenBack { texts });
    }
}

/// The workflow view of one the conversation's CLI runs itself.
pub(crate) fn workflow_view(
    ctx: &Ctx,
    conversation: &ConversationState,
    workflow: Option<&WorkflowId>,
) {
    mirror::show(ctx, conversation, workflow);
}

/// The view of one agent of a workflow the conversation's CLI runs itself.
pub(crate) fn inner_agent_view(ctx: &Ctx, conversation: &ConversationState, agent: &InnerAgent) {
    mirror::show_agent(ctx, conversation, agent);
}

/// A button above the input, pressed. A press for a session that has ended does nothing.
pub(crate) fn press_notice(
    ctx: &Ctx,
    conversation: &mut ConversationState,
    plugin: &str,
    handle: u64,
) {
    let pressed = conversation
        .turn
        .as_mut()
        .is_some_and(|turn| turn.live.press(plugin, handle));
    if !pressed {
        ctx.hint("Claude Code takes that while it's working on your message.");
    }
}

/// A request you typed `ultracode` in goes through a workflow: the crew
/// workflow in use, Claude Code's own from the main session in Captain, or in Captain on a CLI with
/// none of its own, one Argonauts Code runs on that same model. It waits for a running turn, as
/// any request does.
pub(crate) async fn workflow_request(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    text: String,
) -> anyhow::Result<Vec<Effect>> {
    let text = text.trim().to_owned();
    if text.is_empty() {
        return Ok(Vec::new());
    }
    if conversation.turn.is_some() {
        let pictures = std::mem::take(&mut conversation.pictures);
        conversation.queue(Queued::WorkflowRequest(text, pictures));
        return Ok(Vec::new());
    }
    let direct = direct::workflow_is_direct(ctx.settings()) || conversation.place.is_some();
    // A turn that goes to the main session notes its own use as it starts.
    let route = ultra::route(ctx.settings(), true);
    if matches!(route, ultra::Route::Crew | ultra::Route::SameModel) {
        crate::telemetry::used(&ctx.env.paths, crate::telemetry::Use::Ultracode);
    }
    match route {
        ultra::Route::Crew => start_workflow(ctx, conversation, text).await,
        ultra::Route::SameModel => Ok(same_model_workflow(ctx, conversation, text)),
        ultra::Route::MainSession(cli_workflow) if direct => {
            if let Some(why) = ultra::unavailable(ctx.settings()) {
                ctx.notice(NoteLevel::Info, why);
            }
            let mut effects = name_the_conversation(conversation, &text);
            let words = DirectWords {
                cli_workflow,
                ..DirectWords::default()
            };
            if conversation.prefs.plan_first {
                effects.extend(direct::plan(ctx, conversation, text, words)?);
            } else {
                effects.extend(direct::begin_with(ctx, conversation, text, None, words)?);
            }
            Ok(effects)
        }
        ultra::Route::MainSession(_) | ultra::Route::AsUsual => request(ctx, conversation, text),
    }
}

/// Captain on a CLI with no workflow of its own: Argonauts Code runs the workflow in its place,
/// every agent on that same CLI and model.
fn same_model_workflow(
    ctx: &Ctx,
    conversation: &mut ConversationState,
    text: String,
) -> Vec<Effect> {
    let mut effects = name_the_conversation(conversation, &text);
    let mut run = run_request(ctx, text, RunMode::Crew, None, None);
    run.request_settings
        .extend(ultra::same_model(ctx.settings()));
    effects.push(handover::start(run));
    effects
}

/// Whether the CLI behind this conversation takes Argonauts Code's tools when Argonauts Code drives
/// it: the main agent's, or the agent's whose session the conversation carries on. Never while
/// `main_session.tools` is off.
fn takes_tools(ctx: &Ctx, conversation: &ConversationState) -> bool {
    if !ctx.settings().main_session.tools {
        return false;
    }
    let worker = conversation
        .place
        .as_ref()
        .map_or(&ctx.settings().crew.main, |place| &place.worker);
    ctx.services.workers.takes_session_tools(worker)
}

/// The main session is the CLI you chose: your message goes to it as you typed
/// it, it works in your checkout as it does by itself, and work leaves it only when it calls a
/// tool. True with Captain in use, for an agent's session carried on, and whatever the workflow
/// for a CLI that takes Argonauts Code's tools. A CLI that can't take them, with a crew workflow in
/// use, keeps the verdict that ends each reply, and code reads it.
fn is_the_cli(ctx: &Ctx, conversation: &ConversationState) -> bool {
    direct::workflow_is_direct(ctx.settings())
        || conversation.place.is_some()
        || takes_tools(ctx, conversation)
}

/// A turn at a time: text that arrives while one runs waits for it to end.
fn request(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    text: String,
) -> anyhow::Result<Vec<Effect>> {
    let text = text.trim().to_owned();
    if text.is_empty() {
        return Ok(Vec::new());
    }
    if conversation.turn.is_some() {
        let pictures = std::mem::take(&mut conversation.pictures);
        conversation.queue(Queued::Request(text, pictures));
        return Ok(Vec::new());
    }
    let effects = name_the_conversation(conversation, &text);
    // The main session is the CLI itself: your message goes to it as its CLI takes it, with
    // no estimate first, and it hands work over by calling a tool. With plan first on, the
    // session plans before it changes anything.
    if is_the_cli(ctx, conversation) {
        let mut effects = effects;
        if conversation.prefs.plan_first {
            effects.extend(direct::plan(
                ctx,
                conversation,
                text,
                DirectWords::default(),
            )?);
        } else {
            effects.extend(direct::begin(ctx, conversation, text, None)?);
        }
        return Ok(effects);
    }
    let read_instructions = ctx.settings().captain_or_crew.read_instructions;
    begin(ctx, conversation, text, |record| TurnStep::Estimate {
        uncommitted_note: record.uncommitted_note(),
        read_instructions,
        command: None,
    })?;
    Ok(effects)
}

/// A request whose run plans first and waits for you; `/plan <task>`.
fn request_planned(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    text: String,
) -> anyhow::Result<Vec<Effect>> {
    if conversation.turn.is_some() {
        conversation.queue(Queued::Plan(text));
        return Ok(Vec::new());
    }
    // Direct Captain, and an agent's session carried on, plan in the session that then goes ahead,
    // and change nothing until you say. With a workflow in use it is the workflow that plans
    // first and waits for you.
    if direct::workflow_is_direct(ctx.settings()) || conversation.place.is_some() {
        let text = text.trim().to_owned();
        if text.is_empty() {
            return Ok(Vec::new());
        }
        let mut effects = name_the_conversation(conversation, &text);
        effects.extend(direct::plan(
            ctx,
            conversation,
            text,
            DirectWords::default(),
        )?);
        return Ok(effects);
    }
    let effects = request(ctx, conversation, text)?;
    if let Some(turn) = conversation.turn.as_mut() {
        turn.plan_first = true;
    }
    Ok(effects)
}

/// `/ask`: `ConversationAnswer`, never a run.
pub(crate) async fn ask(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    question: String,
) -> anyhow::Result<Vec<Effect>> {
    let question = question.trim().to_owned();
    if question.is_empty() {
        ctx.notice(NoteLevel::Info, "Put the question after /ask.");
        return Ok(Vec::new());
    }
    if conversation.turn.is_some() {
        conversation.queue(Queued::Ask(question));
        return Ok(Vec::new());
    }
    let run_status = run_status(ctx).await;
    let effects = name_the_conversation(conversation, &question);
    begin(ctx, conversation, question, |_| TurnStep::Ask {
        run_status,
    })?;
    Ok(effects)
}

/// One of the main agent CLI's own commands, sent to its session as `prompt`, the command
/// itself. The conversation shows it as you typed it, and the reply as an answer.
pub(crate) fn cli_command(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    command: &CliCommand,
    args: &str,
    prompt: String,
) -> anyhow::Result<Vec<Effect>> {
    if conversation.turn.is_some() {
        conversation.queue(Queued::Cli {
            name: command.name.clone(),
            args: args.trim().to_owned(),
        });
        return Ok(Vec::new());
    }
    let shown = command.invocation(args);
    let mut effects = name_the_conversation(conversation, &shown);
    // The Captain workflow, or an agent's session carried on: the command works where the
    // conversation does, as in the CLI itself.
    if is_the_cli(ctx, conversation) {
        effects.extend(direct::begin_agent(ctx, conversation, shown, prompt)?);
        return Ok(effects);
    }
    begin(ctx, conversation, shown, |_| TurnStep::Cli {
        prompt,
        direct: None,
    })?;
    Ok(effects)
}

/// [`cli_command`] that only reads, whatever the conversation's mode: `prompt` holds words anyone
/// may have written, so its session gets no tool that writes or runs a command, in your
/// checkout or anywhere, before you approve what comes of it.
pub(crate) fn reading_cli_command(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    command: &CliCommand,
    args: &str,
    prompt: String,
) -> anyhow::Result<Vec<Effect>> {
    if conversation.turn.is_some() {
        conversation.queue(Queued::Cli {
            name: command.name.clone(),
            args: args.trim().to_owned(),
        });
        return Ok(Vec::new());
    }
    let shown = command.invocation(args);
    let effects = name_the_conversation(conversation, &shown);
    begin(ctx, conversation, shown, |_| TurnStep::Cli {
        prompt,
        direct: None,
    })?;
    Ok(effects)
}

pub(crate) fn owns_choice(conversation: &ConversationState, id: &ChoiceId) -> bool {
    offer::owns_choice(conversation, id)
        || keep::owns(conversation, id)
        || talk::owns(conversation, id)
}

/// The `StartWork` choice, or where to keep what a request said "from now on".
pub(crate) async fn answer_choice(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    id: ChoiceId,
    action: AnswerAction,
    text: Option<String>,
) -> anyhow::Result<Vec<Effect>> {
    if keep::owns(conversation, &id) {
        return keep::answer(ctx, conversation, id, action);
    }
    if talk::owns(conversation, &id) {
        return talk::answer(ctx, conversation, id, action);
    }
    offer::answer_choice(ctx, conversation, id, action, text).await
}

pub(crate) fn set_aside(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    id: ChoiceId,
) -> anyhow::Result<Vec<Effect>> {
    if keep::owns(conversation, &id) {
        keep::set_aside(ctx, conversation, id);
        return Ok(Vec::new());
    }
    if talk::owns(conversation, &id) {
        return Ok(talk::set_aside(ctx, conversation, id));
    }
    offer::set_aside(ctx, conversation, id)
}

/// Autopilot came on with questions open, and nothing waits for you there: each question the
/// conversation would not have asked on autopilot gets what autopilot gives a new one, and
/// closes with the line that says so. What your own command opened to pick from, such as
/// `/rewind`'s list, stays yours.
pub(crate) fn autopilot_came_on(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
) -> anyhow::Result<Vec<Effect>> {
    direct::set_approval(conversation, ApprovalMode::Autopilot);
    keep::settle_on_autopilot(ctx, conversation);
    let mut effects = talk::settle_on_autopilot(ctx, conversation);
    effects.extend(direct::settle_on_autopilot(ctx, conversation)?);
    effects.extend(offer::settle_on_autopilot(ctx, conversation));
    Ok(effects)
}

/// `/captain` and `/crew`: a run with no estimate.
pub(crate) async fn start_forced(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    mode: RunMode,
    task: String,
) -> anyhow::Result<Vec<Effect>> {
    let task = task.trim().to_owned();
    if task.is_empty() {
        let command = mode_name(mode);
        ctx.notice(
            NoteLevel::Info,
            format!("Say what to do after /{command}, and a {command} run starts on it."),
        );
        return Ok(Vec::new());
    }
    let direct = mode == RunMode::Captain && direct::captain_is_direct(ctx.settings());
    // Your words are read first when they may say how to run, as a request's are: the command
    // keeps its mode, and the rest of what they say is checked and followed. Words that can't
    // say how leave the command as quick as it always was.
    let reads = ctx.settings().captain_or_crew.read_instructions
        && !(direct && conversation.prefs.plan_first)
        && instructions::might_say_how(&task, &Roster::new(ctx));
    if (direct || reads) && conversation.turn.is_some() {
        conversation.queue(match mode {
            RunMode::Captain => Queued::Captain(task),
            RunMode::Crew => Queued::Crew(task),
        });
        return Ok(Vec::new());
    }
    let mut effects = name_the_conversation(conversation, &task);
    if reads {
        let command = Command {
            mode,
            said: format!("you typed /{}", mode_name(mode)),
            style: None,
        };
        begin(ctx, conversation, task, |record| TurnStep::Estimate {
            uncommitted_note: record.uncommitted_note(),
            read_instructions: true,
            command: Some(command),
        })?;
        return Ok(effects);
    }
    // Captain you choose is a direct turn, unless something asks for the checked worktree. With
    // plan first on, it plans before it changes anything.
    if direct {
        if conversation.prefs.plan_first {
            effects.extend(direct::plan(
                ctx,
                conversation,
                task,
                DirectWords::default(),
            )?);
        } else {
            effects.extend(direct::begin(ctx, conversation, task, None)?);
        }
        return Ok(effects);
    }
    let mut request = run_request(ctx, task, mode, None, None);
    if conversation.prefs.plan_first {
        request = planned(request);
    }
    effects.push(handover::start(request));
    Ok(effects)
}

/// Esc during a turn: cancels the step, which asks the agent process to stop. The turn stays
/// stopping until its step ends. Anything queued behind it goes back into the input box.
///
/// While the turn's CLI runs subagents or workflows of its own, only the main agent's turn
/// stops, as on Claude Code's screen: they go on, and the turn stays for them
/// and for what you say next.
pub(crate) fn cancel_turn(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
) -> anyhow::Result<Vec<Effect>> {
    let spared = conversation
        .turn
        .as_mut()
        .is_some_and(|turn| turn.live.stop_turn_alone());
    if !spared {
        let Some(id) = stop_turn(ctx, conversation) else {
            return Ok(Vec::new());
        };
        settle_mode(ctx, &id);
    }
    let texts: Vec<String> = conversation
        .queued
        .drain(..)
        .map(|(queued, _)| match queued {
            Queued::Request(text, _) | Queued::WorkflowRequest(text, _) => text,
            Queued::Ask(question) => format!("/ask {question}"),
            Queued::Cli { name, args } if args.is_empty() => format!("/{name}"),
            Queued::Cli { name, args } => format!("/{name} {args}"),
            Queued::Side(question) => format!("/btw {question}"),
            Queued::Plan(task) => format!("/plan {task}"),
            Queued::Captain(task) => format!("/captain {task}"),
            Queued::Crew(task) => format!("/crew {task}"),
            Queued::Workflow(task) => task.clone(),
            Queued::Subagent { name, text } => format!("@{name} {text}"),
        })
        .collect();
    if !texts.is_empty() {
        ctx.send(AppEvent::TakenBack { texts });
    }
    Ok(Vec::new())
}

pub(crate) async fn on_update(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    update: TurnUpdate,
) -> anyhow::Result<Vec<Effect>> {
    match update {
        TurnUpdate::Event { turn, event } => {
            on_event(ctx, conversation, &turn, event);
            Ok(Vec::new())
        }
        TurnUpdate::Done { turn, outcome } => on_done(ctx, conversation, &turn, outcome),
        TurnUpdate::Asked { turn, asked } => {
            // The CLI waits on the answer, which the inactivity watchdog mustn't take for a
            // session gone quiet.
            if let Some(current) = conversation.turn.as_ref().filter(|now| now.id == turn) {
                current.live.asking(true);
            }
            direct::on_asked(ctx, conversation, &turn, asked)
        }
        TurnUpdate::Answered { turn } => {
            if let Some(current) = conversation.turn.as_ref().filter(|now| now.id == turn) {
                current.live.asking(false);
            }
            Ok(Vec::new())
        }
        TurnUpdate::Retried { turn } => {
            on_retried(ctx, conversation, &turn);
            Ok(Vec::new())
        }
    }
}

/// A new main agent: on the same CLI the session carries on, and on another the next turn
/// starts a fresh one from what `switch.carry` says. A conversation that carries on an agent's
/// session from a run stays with that agent and its session.
pub(crate) fn fresh_session(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
) -> anyhow::Result<Vec<Effect>> {
    if let Some(place) = &conversation.place {
        let agent = ctx.names.worker(&place.worker);
        let main = ctx.names.worker(&ctx.settings().crew.main);
        ctx.notice(
            NoteLevel::Info,
            format!(
                "This conversation carries on {} with {agent}, so it stays with that agent. \
                 /clear starts one with {main}.",
                place.agent
            ),
        );
        return Ok(Vec::new());
    }
    carry::switched(ctx, conversation)
}

/// The screen is closing: a turn in flight is cancelled. The loop then waits a little for
/// every stopping step to end. A worktree made again for the conversation goes too, unless its
/// agent may still be writing it; the next start removes it then.
pub(crate) fn close(ctx: &mut Ctx, conversation: &mut ConversationState) {
    let in_flight = stop_turn(ctx, conversation).is_some() || !conversation.stopping.is_empty();
    if let Some(place) = conversation
        .place
        .as_ref()
        .filter(|place| place.made_again && !in_flight)
    {
        run_worktree::leave(ctx, place);
    }
    conversation.store.release_if_empty();
}

fn stop_turn(ctx: &mut Ctx, conversation: &mut ConversationState) -> Option<TurnId> {
    let mut turn = conversation.turn.take()?;
    turn.cancel.cancel();
    direct::cancelled(ctx, conversation, &mut turn);
    conversation.stopping.push(turn.id.clone());
    emit(
        ctx,
        conversation,
        ConversationEvent::TurnCancelled {
            turn: turn.id.clone(),
        },
    );
    Some(turn.id)
}

/// The first request names the conversation, and the terminal's title shows it.
fn name_the_conversation(conversation: &mut ConversationState, text: &str) -> Vec<Effect> {
    if !conversation.title.is_empty() {
        return Vec::new();
    }
    conversation.title = title_from_request(text);
    vec![Effect::Status]
}

/// A run you picked, forced like `/captain` and `/crew`.
fn run_request(
    ctx: &Ctx,
    request: String,
    mode: RunMode,
    estimate: Option<ChangeEstimate>,
    conversation_turn: Option<TurnId>,
) -> StartRun {
    let settings = ctx.settings();
    StartRun {
        request,
        mode,
        forced: true,
        estimate,
        captain_or_crew: None,
        conversation_turn,
        continues: None,
        overrides: ctx.settings.run_overrides(),
        include_uncommitted: settings.intake.include_uncommitted,
        main_agent: Some(settings.crew.main.clone()),
        approval_mode: None,
        outside_agent: None,
        instructions: Vec::new(),
        request_settings: Vec::new(),
        required_steps: Vec::new(),
        starts_from: None,
    }
}

/// The same run, planned first: the crew plans, and the plan waits for you whatever the
/// approvals say. A Captain run has no plan, so it becomes a crew run.
fn planned(mut request: StartRun) -> StartRun {
    request.mode = RunMode::Crew;
    request
        .overrides
        .push(format!("{PLAN_MODE_KEY}=\"supervised\""));
    request
}

/// The plan approval's mode, which `/plan` sets for one run.
const PLAN_MODE_KEY: &str = "approvals.plan.mode";

/// `/workflow`'s run is the crew's, whatever the words say. A goal is no kind of run: it comes
/// with the settings every run takes, from `/goal` or the request's own words.
pub(super) fn styled(mut request: StartRun, _style: RunStyle) -> StartRun {
    request.mode = RunMode::Crew;
    request
}

/// `/workflow <task>`: a crew run that's a workflow. Words that may say how to run it are read
/// first, as `/crew` reads them: the command keeps its mode and style, and the rest is checked
/// and followed.
pub(crate) async fn start_workflow(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    task: String,
) -> anyhow::Result<Vec<Effect>> {
    start_as_workflow(ctx, conversation, task, "you typed /workflow").await
}

/// A crew run that's a workflow, which a command or your own words chose and `said` names.
async fn start_as_workflow(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    task: String,
    said: &str,
) -> anyhow::Result<Vec<Effect>> {
    let task = task.trim().to_owned();
    let reads = ctx.settings().captain_or_crew.read_instructions
        && instructions::might_say_how(&task, &Roster::new(ctx));
    if reads && conversation.turn.is_some() {
        conversation.queue(Queued::Workflow(task));
        return Ok(Vec::new());
    }
    let mut effects = name_the_conversation(conversation, &task);
    if reads {
        let command = Command {
            mode: RunMode::Crew,
            said: said.to_owned(),
            style: Some(RunStyle::Workflow),
        };
        begin(ctx, conversation, task, |record| TurnStep::Estimate {
            uncommitted_note: record.uncommitted_note(),
            read_instructions: true,
            command: Some(command),
        })?;
        return Ok(effects);
    }
    let mut request = styled(
        run_request(ctx, task, RunMode::Crew, None, None),
        RunStyle::Workflow,
    );
    if conversation.prefs.plan_first {
        request = planned(request);
    }
    effects.push(handover::start(request));
    Ok(effects)
}

/// `/<name> <request>`: a crew run that follows the workflow of that name, for this request
/// alone. The workflow in use stays as it was.
pub(crate) async fn start_with_workflow(
    ctx: &mut Ctx,
    conversation: &mut ConversationState,
    request: String,
    workflow: &str,
) -> anyhow::Result<Vec<Effect>> {
    let request = request.trim().to_owned();
    let mut effects = name_the_conversation(conversation, &request);
    let mut run = run_request(ctx, request, RunMode::Crew, None, None);
    let named = toml::Value::String(workflow.to_owned()).to_string();
    run.request_settings.push(format!("workflow={named}"));
    effects.push(handover::start(run));
    Ok(effects)
}

/// The run on screen's zero-token summary, for `/ask` during a run.
async fn run_status(ctx: &Ctx) -> Option<String> {
    let run = match &ctx.mode {
        AppMode::Working { run } | AppMode::Pausing { run } | AppMode::Waiting { run, .. } => run,
        AppMode::Idle | AppMode::Answering { .. } => return None,
    };
    match ctx.services.conductor.status(run).await {
        Ok(summary) => Some(argonauts_crew::status::render(&summary)),
        Err(err) => {
            ctx.notice(
                NoteLevel::Warn,
                format!("Asking without the run's status: {err}"),
            );
            None
        }
    }
}

/// Back to `Idle`, unless something else changed the mode meanwhile, such as a run starting.
fn settle_mode(ctx: &mut Ctx, turn: &TurnId) {
    if matches!(&ctx.mode, AppMode::Answering { turn: current } if current == turn) {
        ctx.set_mode(AppMode::Idle);
    }
}

/// What was typed during the turn, handled now as if just sent, so it goes by the mode now.
fn send_queued(conversation: &mut ConversationState) -> Vec<Effect> {
    conversation
        .queued
        .drain(..)
        .map(|(queued, typed_after)| {
            let command = match queued {
                Queued::Request(text, pictures) => UiCommand::Submit {
                    text,
                    workflow_requested: false,
                    pictures,
                },
                Queued::WorkflowRequest(text, pictures) => UiCommand::Submit {
                    text,
                    workflow_requested: true,
                    pictures,
                },
                Queued::Ask(question) => UiCommand::Slash {
                    command: SlashCommand::Ask { question },
                },
                Queued::Cli { name, args } => UiCommand::Slash {
                    command: SlashCommand::Cli { name, args },
                },
                Queued::Side(question) => UiCommand::Slash {
                    command: SlashCommand::Btw {
                        question: Some(question),
                    },
                },
                Queued::Plan(task) => UiCommand::Slash {
                    command: SlashCommand::Plan { task: Some(task) },
                },
                Queued::Captain(task) => UiCommand::Slash {
                    command: SlashCommand::Captain { task },
                },
                Queued::Crew(task) => UiCommand::Slash {
                    command: SlashCommand::Crew { task },
                },
                Queued::Subagent { name, text } => UiCommand::MessageSubagent { name, text },
                Queued::Workflow(task) => UiCommand::Slash {
                    command: SlashCommand::WorkflowRun { task: Some(task) },
                },
            };
            Effect::Queued {
                command: Box::new(command),
                typed_after,
            }
        })
        .collect()
}

/// A slash command you typed goes in the record where you typed it, so the conversation drawn
/// again prints it there.
pub(crate) fn command_typed(ctx: &Ctx, conversation: &mut ConversationState, text: String) {
    let text = text.trim();
    if !text.is_empty() {
        let typed = ConversationEvent::CommandTyped {
            text: text.to_owned(),
            at: Some(time::OffsetDateTime::now_utc()),
        };
        record(ctx, conversation, &typed);
    }
}

/// A line a command you typed printed back goes in the record after the command, so the
/// conversation drawn again prints it under it.
pub(crate) fn command_replied(
    ctx: &Ctx,
    conversation: &mut ConversationState,
    reply: CommandReply,
) {
    if !reply.text.trim().is_empty() {
        record(
            ctx,
            conversation,
            &ConversationEvent::CommandReplied { reply },
        );
    }
}

/// Sends the event and records it, except the streamed text, which the finished turn holds,
/// and side questions, which stay out of the conversation.
fn emit(ctx: &Ctx, conversation: &mut ConversationState, event: ConversationEvent) {
    if event_turn(&event).is_some_and(carry::is_carry_turn) {
        return;
    }
    let passing =
        matches!(&event, ConversationEvent::Agent { event, .. } if live::is_passing(event));
    if !passing {
        record(ctx, conversation, &event);
    }
    ctx.send(AppEvent::Conversation { event });
}

/// Writes the event in the conversation's record, where a side question's never goes.
fn record(ctx: &Ctx, conversation: &mut ConversationState, event: &ConversationEvent) {
    let kept = event_turn(event)
        .is_none_or(|turn| !side::is_side_turn(turn) && !carry::is_carry_turn(turn));
    if kept
        && let Err(err) = conversation.store.append(event)
        && !conversation.journal_warned
    {
        conversation.journal_warned = true;
        ctx.notice(
            NoteLevel::Warn,
            format!(
                "The conversation isn't being recorded in {}: {err:#}",
                ctx.env.display(conversation.store.journal_path())
            ),
        );
    }
}

/// The turn the event belongs to. A command you typed belongs to none.
fn event_turn(event: &ConversationEvent) -> Option<&TurnId> {
    match event {
        ConversationEvent::TurnStarted { turn, .. }
        | ConversationEvent::Agent { turn, .. }
        | ConversationEvent::TurnFinished { turn, .. }
        | ConversationEvent::TurnFailed { turn, .. }
        | ConversationEvent::TurnCancelled { turn }
        | ConversationEvent::TurnStopped { turn, .. }
        | ConversationEvent::FellBack { turn, .. }
        | ConversationEvent::Joined { turn, .. }
        | ConversationEvent::AnswerRetried { turn }
        | ConversationEvent::ContextLeft { turn, .. } => Some(turn),
        ConversationEvent::CommandTyped { .. } | ConversationEvent::CommandReplied { .. } => None,
    }
}