navi-core 0.3.7

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

#[cfg(test)]
mod tests;

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use tokio::sync::{broadcast, mpsc, oneshot};

use crate::cancel::CancelToken;
use crate::config::{LoadedConfig, PermissionMode, SecurityConfig};
use crate::context::ContextPacket;
use crate::event::{
    AgentEvent, ApprovalDecision, PlanReviewResponse, QuestionResponse, RuntimeEvent,
    RuntimeEventKind, SudoPasswordResponse,
};
use crate::goal::{
    CreateGoalTool, GetGoalTool, GoalExtension, GoalRuntimeHandle, GoalService, UpdateGoalTool,
};
use crate::harness::select_harness_policy;
use crate::model::{ModelMessage, ModelProvider, ModelResponse};
use crate::runtime_components::RuntimeComponents;
use crate::security::SecurityPolicy;
use crate::session::{SessionId, SessionStore, current_unix_timestamp};
use crate::session_title::SessionTitleHandle;
use crate::skills::{
    SkillManifest, SkillPool, active_skills, discover_catalog_entries, discover_configured_skills,
};
use crate::tool::builtin::{RepoExploreTool, SubagentTool};
use crate::tool::{Tool, ToolExecutor};
use crate::trace::{TraceStore, turn_traces_from_events};
use crate::{
    ModelOption, SessionSnapshot, available_model_options, canonical_provider_id,
    provider_request_model_name,
};
use anyhow::Result;

pub use event_bus::EventBus;
pub use session_state::SessionState;

type PendingApprovals = Arc<std::sync::Mutex<HashMap<String, oneshot::Sender<ApprovalDecision>>>>;
type PendingQuestions = Arc<std::sync::Mutex<HashMap<String, oneshot::Sender<QuestionResponse>>>>;
type PendingPlanReviews =
    Arc<std::sync::Mutex<HashMap<String, oneshot::Sender<PlanReviewResponse>>>>;
type PendingSudoPasswords =
    Arc<std::sync::Mutex<HashMap<String, oneshot::Sender<SudoPasswordResponse>>>>;

/// Resolves pending tool approvals by matching decision ids to waiting
/// receivers. Cloneable so it can be handed to the UI layer.
#[derive(Clone)]
pub struct ApprovalResolver {
    pending_approvals: PendingApprovals,
    runtime_events_tx: broadcast::Sender<RuntimeEvent>,
}

/// Resolves pending interactive questions by matching response ids to waiting
/// receivers. Cloneable so it can be handed to the UI layer.
#[derive(Clone)]
pub struct QuestionResolver {
    pending_questions: PendingQuestions,
    runtime_events_tx: broadcast::Sender<RuntimeEvent>,
}

/// Resolves pending plan reviews (blocks `plan` create until the user finishes).
#[derive(Clone)]
pub struct PlanReviewResolver {
    pending_reviews: PendingPlanReviews,
    runtime_events_tx: broadcast::Sender<RuntimeEvent>,
}

/// Resolves sudo password prompts without putting secrets on the event bus.
#[derive(Clone)]
pub struct SudoPasswordResolver {
    pending: PendingSudoPasswords,
}

impl SudoPasswordResolver {
    #[cfg(test)]
    pub fn new_for_test() -> Self {
        Self {
            pending: Arc::new(std::sync::Mutex::new(HashMap::new())),
        }
    }

    pub(crate) fn new_standalone() -> Self {
        Self {
            pending: Arc::new(std::sync::Mutex::new(HashMap::new())),
        }
    }

    pub fn register(&self, id: String) -> oneshot::Receiver<SudoPasswordResponse> {
        let (tx, rx) = oneshot::channel();
        self.pending
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .insert(id, tx);
        rx
    }

    /// Deliver the user's response. The password (if any) never goes on the runtime event bus.
    pub fn resolve(&self, response: SudoPasswordResponse) -> bool {
        let id = response.id().to_string();
        if let Some(tx) = self
            .pending
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .remove(&id)
        {
            let _ = tx.send(response);
            true
        } else {
            false
        }
    }
}

impl PlanReviewResolver {
    #[cfg(test)]
    pub fn new_for_test() -> Self {
        let (tx, _) = broadcast::channel(16);
        Self {
            pending_reviews: Arc::new(std::sync::Mutex::new(HashMap::new())),
            runtime_events_tx: tx,
        }
    }

    pub(crate) fn new_standalone() -> Self {
        let (tx, _) = broadcast::channel(16);
        Self {
            pending_reviews: Arc::new(std::sync::Mutex::new(HashMap::new())),
            runtime_events_tx: tx,
        }
    }

    pub fn register(&self, id: String) -> oneshot::Receiver<PlanReviewResponse> {
        let (tx, rx) = oneshot::channel();
        self.pending_reviews
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .insert(id, tx);
        rx
    }

    pub fn resolve(&self, response: PlanReviewResponse) -> bool {
        let id = response.id.clone();
        if let Some(tx) = self
            .pending_reviews
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .remove(&id)
        {
            let _ = tx.send(response.clone());
            let _ = self.runtime_events_tx.send(RuntimeEvent::new(
                RuntimeEventKind::PlanReviewResolved(response),
            ));
            true
        } else {
            false
        }
    }
}

impl QuestionResolver {
    #[cfg(test)]
    pub fn new_for_test() -> Self {
        let (tx, _) = broadcast::channel(16);
        Self {
            pending_questions: Arc::new(std::sync::Mutex::new(HashMap::new())),
            runtime_events_tx: tx,
        }
    }

    pub(crate) fn new_standalone() -> Self {
        let (tx, _) = broadcast::channel(16);
        Self {
            pending_questions: Arc::new(std::sync::Mutex::new(HashMap::new())),
            runtime_events_tx: tx,
        }
    }

    /// Register a pending question, returning the receiver for the response.
    pub fn register(&self, id: String) -> oneshot::Receiver<QuestionResponse> {
        let (tx, rx) = oneshot::channel();
        self.pending_questions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .insert(id, tx);
        rx
    }

    /// Resolves a pending question by id. Returns `true` if a matching request
    /// was found and resolved.
    pub fn resolve(&self, response: QuestionResponse) -> bool {
        let id = response.id().to_string();
        if let Some(tx) = self
            .pending_questions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .remove(&id)
        {
            let _ = tx.send(response.clone());
            let _ =
                self.runtime_events_tx
                    .send(RuntimeEvent::new(RuntimeEventKind::QuestionResolved(
                        response,
                    )));
            true
        } else {
            false
        }
    }
}

impl ApprovalResolver {
    #[cfg(test)]
    pub fn new_for_test() -> Self {
        let (tx, _) = broadcast::channel(16);
        Self {
            pending_approvals: Arc::new(std::sync::Mutex::new(HashMap::new())),
            runtime_events_tx: tx,
        }
    }

    pub(crate) fn new_standalone() -> Self {
        let (tx, _) = broadcast::channel(16);
        Self {
            pending_approvals: Arc::new(std::sync::Mutex::new(HashMap::new())),
            runtime_events_tx: tx,
        }
    }

    /// Register a pending approval, returning the receiver for the decision.
    pub fn register(&self, id: String) -> oneshot::Receiver<ApprovalDecision> {
        let (tx, rx) = oneshot::channel();
        self.pending_approvals
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .insert(id, tx);
        rx
    }

    /// Resolves a pending approval by id. Returns `true` if a matching
    /// pending request was found and resolved.
    pub fn resolve(&self, decision: ApprovalDecision) -> bool {
        let id = match &decision {
            ApprovalDecision::Approved { id } => id,
            ApprovalDecision::Denied { id } => id,
        };
        if let Some(tx) = self
            .pending_approvals
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .remove(id)
        {
            let _ = tx.send(decision.clone());
            let _ =
                self.runtime_events_tx
                    .send(RuntimeEvent::new(RuntimeEventKind::ApprovalResolved(
                        decision,
                    )));
            true
        } else {
            false
        }
    }
}

/// A lightweight handle that cancels the current turn when dropped or called.
/// Cloneable so it can be handed to the UI layer.
#[derive(Clone)]
pub struct TurnCanceller {
    inner: CancelToken,
}

impl TurnCanceller {
    /// Cancels the current turn.
    pub fn cancel(&self) {
        self.inner.cancel();
    }

    /// Returns `true` if cancellation has been requested.
    pub fn is_cancelled(&self) -> bool {
        self.inner.is_requested()
    }
}

/// Options for constructing an [`AgentRuntime`].
pub struct AgentRuntimeOptions {
    /// Loaded and merged configuration.
    pub loaded_config: LoadedConfig,
    /// The model provider implementation.
    pub model_provider: Arc<dyn ModelProvider>,
    /// Project root directory.
    pub project_dir: PathBuf,
    /// Optional custom tool executor (defaults to built-in tools).
    pub tool_executor: Option<Arc<ToolExecutor>>,
    /// Context packets to inject into the session.
    pub context_packets: Vec<ContextPacket>,
    /// Active skill names for this session.
    pub active_skills: Vec<String>,
    /// Seed messages for restoring a session.
    pub initial_messages: Vec<ModelMessage>,
    /// Seed events for restoring a persisted session without losing history.
    pub initial_events: Vec<AgentEvent>,
    /// Original creation timestamp for restored sessions.
    pub initial_created_at: Option<u64>,
    /// Original update timestamp for restored sessions.
    pub initial_updated_at: Option<u64>,
    /// Goal restored from a persisted session snapshot.
    pub initial_goal: Option<crate::goal::types::SessionGoal>,
    /// Session id for restoring an existing session.
    pub session_id: Option<SessionId>,
    /// Optional channel for forwarding agent events outside the runtime.
    pub event_tx: Option<tokio::sync::mpsc::UnboundedSender<AgentEvent>>,
    /// Replaceable runtime components. Defaults preserve NAVI's code-agent behavior.
    pub runtime_components: Option<RuntimeComponents>,
    /// Session-local title state written by the `set_session_title` tool.
    /// When omitted, direct runtime users simply do not get agent-managed titles.
    pub session_title_handle: Option<SessionTitleHandle>,
    /// Explicit model for automatic durable-memory extraction. `None` means
    /// extraction is disabled rather than silently billing the chat model.
    pub memory_extraction_model: Option<MemoryExtractionModel>,
    /// When true, skip auto-registering goal tools and skill loaders on the
    /// provided tool executor. Used by host tool profiles (`chat_only`,
    /// `host_tools_only`) so the host's filtered tool set is preserved.
    pub skip_auto_tool_bootstrap: bool,
}

/// Provider selection for the opt-in per-turn memory extractor.
#[derive(Clone)]
pub struct MemoryExtractionModel {
    pub provider: Arc<dyn ModelProvider>,
    pub model_name: String,
}

/// The core agent runtime that manages sessions, turns, approvals, and events.
pub struct AgentRuntime {
    loaded_config: LoadedConfig,
    model_provider: Arc<dyn ModelProvider>,
    shared_model_provider: Arc<RwLock<Arc<dyn ModelProvider>>>,
    shared_model_name: Arc<RwLock<String>>,
    shared_config: Arc<RwLock<crate::config::NaviConfig>>,
    project_dir: PathBuf,
    tool_executor: Option<Arc<ToolExecutor>>,
    session_store: SessionStore,
    context_packets: Vec<ContextPacket>,
    shared_context_packets: Arc<std::sync::Mutex<Vec<ContextPacket>>>,
    active_skills: Vec<String>,
    shared_available_skills: Arc<std::sync::Mutex<Vec<crate::skills::SkillManifest>>>,
    shared_skill_pools: Arc<std::sync::Mutex<Vec<crate::skills::SkillPool>>>,
    shared_active_skills: Arc<std::sync::Mutex<Vec<crate::skills::SkillManifest>>>,
    prompt_cache: Arc<crate::prompt::PromptCache>,
    runtime_components: RuntimeComponents,
    initial_messages: Vec<ModelMessage>,
    event_tx: Option<mpsc::UnboundedSender<AgentEvent>>,
    cancel_token: CancelToken,
    pending_approvals: PendingApprovals,
    pending_questions: PendingQuestions,
    pending_plan_reviews: PendingPlanReviews,
    pending_sudo_passwords: PendingSudoPasswords,
    event_bus: EventBus,
    session: SessionState,
    /// Goal runtime handle for the current session.
    goal_runtime: Arc<GoalRuntimeHandle>,
    /// Goal extension providing lifecycle hooks.
    goal_extension: GoalExtension,
    /// Whether the model used the `memory` tool with `write` action during the current turn.
    /// Used for mutual exclusion with background extractMemories.
    turn_used_memory_write: bool,
    /// Last user task text — used for extractMemories context.
    last_user_task: String,
    /// Session title assigned by the chat model through `set_session_title`.
    session_title_handle: SessionTitleHandle,
    /// User-selected model for asynchronous automatic memory extraction.
    memory_extraction_model: Option<MemoryExtractionModel>,
    /// When true, do not auto-register goal/skill tools on the provided executor.
    skip_auto_tool_bootstrap: bool,
    /// Whether a user message is pending (set when send_turn is called
    /// while the runtime is busy with auto-continuation).
    pending_user_input: std::sync::atomic::AtomicBool,
    /// Current agent mode (Default or Plan). In Plan mode, only read-only
    /// tools are available and the model is instructed to propose a plan.
    agent_mode: std::sync::RwLock<crate::plan_mode::AgentMode>,
    /// Parser for `<proposed_plan>` tags in streaming text.
    plan_parser: std::sync::Mutex<crate::plan_mode::ProposedPlanParser>,
    /// Session-scoped memory manager shared with TurnContext (open once).
    memory_manager: Arc<std::sync::Mutex<Option<Arc<crate::memory::MemoryManager>>>>,
    /// Per-turn file snapshots for rewind / Revert.
    rewind_store: Arc<std::sync::Mutex<crate::rewind::RewindStore>>,
    /// From active harness packs: override for goals.max_auto_continue_turns.
    harness_max_auto_continue: Option<u32>,
    /// From active harness packs: preferred token budget when setting a goal.
    harness_token_budget: Option<i64>,
    /// Developer-context harness card for active packs.
    harness_card: Option<String>,
    /// Soft graph + skill allowlist merged (None = no extra lock).
    harness_allow_tools: Option<Vec<String>>,
}

impl AgentRuntime {
    /// Creates a new runtime from the given options.
    pub fn new(options: AgentRuntimeOptions) -> Self {
        let session_store = SessionStore::with_redaction(
            options.loaded_config.data_dir.clone(),
            options
                .loaded_config
                .config
                .security
                .redact_secrets_in_sessions,
        );

        let shared_context_packets =
            Arc::new(std::sync::Mutex::new(options.context_packets.clone()));
        let shared_available_skills = Arc::new(std::sync::Mutex::new(Vec::new()));
        let shared_skill_pools = Arc::new(std::sync::Mutex::new(Vec::new()));
        let shared_active_skills = Arc::new(std::sync::Mutex::new(Vec::new()));
        let shared_model_provider = Arc::new(RwLock::new(options.model_provider.clone()));
        let shared_model_name = Arc::new(RwLock::new(provider_request_model_name(
            &options.loaded_config.config.model.provider,
            &options.loaded_config.config.model.name,
        )));
        let shared_config = Arc::new(RwLock::new(options.loaded_config.config.clone()));
        let prompt_cache = Arc::new(crate::prompt::PromptCache::new());
        let runtime_components = options.runtime_components.unwrap_or_default();
        let goal_service = Arc::new(GoalService::new());
        let goal_runtime = Arc::new(GoalRuntimeHandle::new(options.initial_goal.clone()));
        let goal_extension = GoalExtension::new(goal_service.clone(), goal_runtime.clone());

        let rewind_sid = options
            .session_id
            .as_ref()
            .map(|s| s.as_str().to_string())
            .unwrap_or_else(|| "pending".to_string());
        let rewind_store = Arc::new(std::sync::Mutex::new(crate::rewind::RewindStore::new(
            &options.loaded_config.data_dir,
            &rewind_sid,
            &options.project_dir,
        )));

        Self {
            loaded_config: options.loaded_config,
            model_provider: options.model_provider,
            shared_model_provider,
            shared_model_name,
            shared_config,
            project_dir: options.project_dir,
            tool_executor: options.tool_executor,
            session_store,
            context_packets: options.context_packets,
            shared_context_packets,
            active_skills: options.active_skills,
            shared_available_skills,
            shared_skill_pools,
            shared_active_skills,
            prompt_cache,
            runtime_components,
            initial_messages: options.initial_messages,
            event_tx: options.event_tx,
            cancel_token: CancelToken::new(),
            pending_approvals: Arc::new(std::sync::Mutex::new(HashMap::new())),
            pending_questions: Arc::new(std::sync::Mutex::new(HashMap::new())),
            pending_plan_reviews: Arc::new(std::sync::Mutex::new(HashMap::new())),
            pending_sudo_passwords: Arc::new(std::sync::Mutex::new(HashMap::new())),
            event_bus: EventBus::new(),
            session: SessionState::new_with_history(
                options.session_id,
                options.initial_events,
                options.initial_created_at,
                options.initial_updated_at,
            ),
            goal_runtime,
            goal_extension,
            turn_used_memory_write: false,
            last_user_task: String::new(),
            session_title_handle: options.session_title_handle.unwrap_or_default(),
            memory_extraction_model: options.memory_extraction_model,
            skip_auto_tool_bootstrap: options.skip_auto_tool_bootstrap,
            pending_user_input: std::sync::atomic::AtomicBool::new(false),
            agent_mode: std::sync::RwLock::new(crate::plan_mode::AgentMode::Default),
            plan_parser: std::sync::Mutex::new(crate::plan_mode::ProposedPlanParser::new()),
            memory_manager: Arc::new(std::sync::Mutex::new(None)),
            rewind_store,
            harness_max_auto_continue: None,
            harness_token_budget: None,
            harness_card: None,
            harness_allow_tools: None,
        }
    }

    /// Shared rewind store (write tools note dirty paths through this handle).
    pub fn rewind_store_handle(&self) -> Arc<std::sync::Mutex<crate::rewind::RewindStore>> {
        self.rewind_store.clone()
    }

    /// Rebind rewind store to the live session id (after start_session).
    pub fn rebind_rewind_store(&self) {
        let sid = self.session.id().as_str().to_string();
        let mut store = self.rewind_store.lock().unwrap_or_else(|e| e.into_inner());
        *store =
            crate::rewind::RewindStore::new(&self.loaded_config.data_dir, &sid, &self.project_dir);
    }

    /// Returns all agent events recorded so far.
    /// Returns the current session goal, if any.
    pub fn get_goal(&self) -> Option<crate::goal::types::SessionGoal> {
        self.goal_runtime.get_goal()
    }

    /// Sets or updates the session goal and notifies live clients.
    pub fn set_goal(
        &self,
        objective: String,
        token_budget: Option<i64>,
    ) -> crate::goal::types::SessionGoal {
        let goal = self.goal_runtime.set_objective(objective, token_budget);
        self.goal_runtime.set_auto_continue(true);
        self.publish_goal_updated(&goal);
        goal
    }

    /// Sets or updates the session goal with a compact UI label.
    pub fn set_goal_with_short_description(
        &self,
        objective: String,
        short_description: Option<String>,
        token_budget: Option<i64>,
    ) -> crate::goal::types::SessionGoal {
        let goal = self.goal_runtime.set_objective_with_short_description(
            objective,
            short_description,
            token_budget,
        );
        self.goal_runtime.set_auto_continue(true);
        self.publish_goal_updated(&goal);
        goal
    }

    /// Clears the current session goal.
    ///
    /// Live UIs typically clear their chip optimistically when the user
    /// requests clear; no terminal GoalUpdated is published (that would look
    /// like a successful complete).
    pub fn clear_goal(&self) {
        self.goal_runtime.clear_goal();
    }

    /// Updates the stored goal (used after status transitions) and notifies clients.
    pub fn update_goal(&self, goal: crate::goal::types::SessionGoal) {
        self.goal_runtime.update_goal(goal.clone());
        self.publish_goal_updated(&goal);
    }

    fn publish_goal_updated(&self, goal: &crate::goal::types::SessionGoal) {
        self.event_bus.publish(RuntimeEventKind::GoalUpdated {
            session_id: goal.session_id.clone(),
            goal_id: goal.goal_id.as_str().to_string(),
            objective: goal.objective.clone(),
            short_description: goal.short_description.clone(),
            status: goal.status,
            tokens_used: goal.tokens_used,
            token_budget: goal.token_budget,
        });
    }

    /// Updates the goal checklist (replaces all tasks).
    pub fn update_goal_checklist(
        &self,
        tasks: Vec<crate::goal::types::GoalTask>,
    ) -> Option<crate::goal::types::SessionGoal> {
        self.goal_runtime.update_checklist(tasks)
    }

    /// Updates a single task's status in the goal checklist.
    pub fn update_goal_task_status(
        &self,
        task_id: usize,
        status: crate::goal::types::TaskStatus,
    ) -> Option<crate::goal::types::SessionGoal> {
        self.goal_runtime.update_task_status(task_id, status)
    }

    /// Returns a continuation steering prompt if the goal is active and should auto-continue.
    pub fn goal_idle_prompt(&self) -> Option<String> {
        // In Plan mode, don't auto-continue — the user needs to confirm the plan first.
        if self
            .agent_mode
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .restricts_tools()
        {
            return None;
        }
        if !self.loaded_config.config.goals.enabled {
            return None;
        }
        self.goal_extension.on_idle()
    }

    /// Returns the goals configuration.
    pub fn goals_config(&self) -> crate::config::GoalsConfig {
        self.loaded_config.config.goals.clone()
    }

    /// Returns a reference to the goal runtime handle.
    pub fn goal_runtime(&self) -> &Arc<GoalRuntimeHandle> {
        &self.goal_runtime
    }

    // ── Plan Mode ──────────────────────────────────────────────

    /// Returns the current agent mode.
    pub fn agent_mode(&self) -> crate::plan_mode::AgentMode {
        *self.agent_mode.read().unwrap_or_else(|e| e.into_inner())
    }

    /// Enters Plan mode. Exploration tools + the session plan markdown file are
    /// available; project writes and commands are blocked.
    pub fn enter_plan_mode(&self) {
        *self.agent_mode.write().unwrap_or_else(|e| e.into_inner()) =
            crate::plan_mode::AgentMode::Plan;
        *self.plan_parser.lock().unwrap_or_else(|e| e.into_inner()) =
            crate::plan_mode::ProposedPlanParser::new();

        let plan_path = crate::plan_store::session_plan_file_path(
            &self.loaded_config.data_dir,
            self.session.id().as_str(),
        );
        if let Some(parent) = plan_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }
        if let Some(exec) = self.tool_executor.as_ref() {
            exec.policy().set_plan_mode(true, Some(plan_path.clone()));
        }

        self.event_bus.publish(RuntimeEventKind::AgentModeChanged {
            session_id: self.session.id().as_str().to_string(),
            mode: crate::plan_mode::AgentMode::Plan,
        });
    }

    /// Exits Plan mode and returns to normal execution.
    pub fn exit_plan_mode(&self) {
        *self.agent_mode.write().unwrap_or_else(|e| e.into_inner()) =
            crate::plan_mode::AgentMode::Default;
        if let Some(exec) = self.tool_executor.as_ref() {
            exec.policy().set_plan_mode(false, None);
        }
        self.event_bus.publish(RuntimeEventKind::AgentModeChanged {
            session_id: self.session.id().as_str().to_string(),
            mode: crate::plan_mode::AgentMode::Default,
        });
    }

    /// Feeds a text delta into the plan parser. Returns any completed plans.
    pub fn feed_plan_text(&self, text: &str) -> Vec<crate::plan_mode::ProposedPlan> {
        self.plan_parser
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .push_text(text)
    }

    /// Drains any pending plans from the parser (call at end of turn).
    pub fn drain_plans(&self) -> Vec<crate::plan_mode::ProposedPlan> {
        self.plan_parser
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .drain()
    }

    /// Returns true if the parser is currently inside a `<proposed_plan>` block.
    pub fn is_parsing_plan(&self) -> bool {
        self.plan_parser
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .is_in_plan()
    }

    /// Returns `true` if a user message is pending (set when `send_turn` is
    /// called while the runtime is busy with auto-continuation).
    pub fn has_pending_user_input(&self) -> bool {
        self.pending_user_input
            .load(std::sync::atomic::Ordering::SeqCst)
    }

    /// Marks that a user message is pending.
    pub fn set_pending_user_input(&self, pending: bool) {
        self.pending_user_input
            .store(pending, std::sync::atomic::Ordering::SeqCst);
    }

    /// Returns all agent events recorded so far.
    pub fn events(&self) -> &[AgentEvent] {
        self.session.events()
    }

    /// Returns the current session id.
    pub fn session_id(&self) -> &SessionId {
        self.session.id()
    }

    /// Returns the session title, if one has been derived.
    pub fn session_title(&self) -> Option<&str> {
        self.session.title()
    }

    /// Adds a context packet to the session and emits a `ContextUpdated` event.
    pub fn add_context_packet(&mut self, packet: ContextPacket) {
        self.context_packets.push(packet.clone());
        self.shared_context_packets
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .push(packet);
        self.event_bus.publish(RuntimeEventKind::ContextUpdated);
    }

    /// Clears all context packets and emits a `ContextUpdated` event.
    pub fn clear_context_packets(&mut self) {
        self.context_packets.clear();
        self.shared_context_packets
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .clear();
        self.event_bus.publish(RuntimeEventKind::ContextUpdated);
    }

    /// Returns the current context packets.
    pub fn context_packets(&self) -> &[ContextPacket] {
        &self.context_packets
    }

    /// Sets the session catalog filter for skills.
    ///
    /// Empty `skills` means **all** discovered skills are catalog-active (default).
    /// Non-empty restricts the Available Skills catalog to those ids/names.
    /// Never injects skill instruction bodies into the prompt.
    ///
    /// When a skill has a harness pack under `{data_dir}/harnesses/<id>/`, applies
    /// soft graph allowlists, loop max_turns / token_budget, and harness card text.
    pub fn set_active_skills(&mut self, skills: Vec<String>) {
        self.active_skills = skills;
        let catalog = self.load_catalog_skills();
        let pools = self.load_catalog_pools();
        *self
            .shared_available_skills
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = catalog.clone();
        *self
            .shared_skill_pools
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = pools;
        // Soft harness applies only to session-active skills, never the full catalog.
        let harness_skills = self.session_active_skill_manifests();
        *self
            .shared_active_skills
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = harness_skills.clone();
        self.apply_harness_packs_for_active(&harness_skills);
        self.event_bus.publish(RuntimeEventKind::ContextUpdated);
    }

    /// Skill manifests selected for **session** activation (CLI `--skill`, host
    /// `set_active_skills`, config `skills.active` when session list is empty).
    /// Empty selection → no soft harness allowlist.
    fn session_active_skill_manifests(&self) -> Vec<crate::skills::SkillManifest> {
        let available = self.discover_available_skills().unwrap_or_default();
        let session = &self.active_skills;
        let configured = &self.loaded_config.config.skills.active;
        // Unlike catalog `active_skills`, an empty selection means **no**
        // harness soft-apply (do not default to every discovered skill).
        if session.is_empty() && configured.is_empty() {
            return Vec::new();
        }
        crate::skills::active_skills(&available, configured, session)
    }

    /// Recompute harness soft-apply state from currently active skill manifests.
    fn apply_harness_packs_for_active(&mut self, manifests: &[crate::skills::SkillManifest]) {
        let applied =
            crate::harness_pack::apply_harness_for_skills(&self.loaded_config.data_dir, manifests);
        self.harness_max_auto_continue = applied.max_auto_continue_turns;
        self.harness_token_budget = applied.token_budget;
        self.harness_card = if applied.harness_card.is_empty() {
            None
        } else {
            Some(applied.harness_card)
        };
        self.harness_allow_tools = applied.allow_tools;
        // If a harness sets a token budget and no goal exists yet, leave budget
        // for host/model create_goal; if a goal is Active, optionally annotate budget.
        if let Some(budget) = applied.token_budget {
            if let Some(mut goal) = self.goal_runtime.get_goal() {
                if goal.token_budget.is_none() && goal.status.should_auto_continue() {
                    goal.token_budget = Some(budget);
                    self.goal_runtime.update_goal(goal);
                }
            }
        }
    }

    /// Effective max auto-continue turns (harness pack overrides goals config when set).
    pub fn effective_max_auto_continue_turns(&self) -> u32 {
        let cfg = self.goals_config().max_auto_continue_turns;
        match self.harness_max_auto_continue {
            Some(h) if h > 0 => {
                if cfg > 0 {
                    cfg.min(h)
                } else {
                    h
                }
            }
            _ => cfg,
        }
    }

    /// Developer harness card for the active pack(s), if any.
    pub fn harness_card(&self) -> Option<&str> {
        self.harness_card.as_deref()
    }

    /// Lists available model options from the loaded configuration.
    pub fn list_models(&self) -> Vec<ModelOption> {
        available_model_options(&self.loaded_config.config)
    }

    /// Current `(provider_id, model_name)` selection for this runtime.
    pub fn model_selection(&self) -> (&str, &str) {
        (
            self.loaded_config.config.model.provider.as_str(),
            self.loaded_config.config.model.name.as_str(),
        )
    }

    /// Changes the selected model and emits a `ContextUpdated` event.
    pub fn set_model(&mut self, provider: impl Into<String>, model: impl Into<String>) {
        self.loaded_config.config.model.provider =
            canonical_provider_id(&provider.into()).to_string();
        self.loaded_config.config.model.name = model.into();
        self.update_shared_model_state();
        self.event_bus.publish(RuntimeEventKind::ContextUpdated);
    }

    /// Replaces the runtime configuration and provider used by subsequent turns.
    pub fn set_model_provider(
        &mut self,
        loaded_config: LoadedConfig,
        model_provider: Arc<dyn ModelProvider>,
    ) {
        self.loaded_config = loaded_config;
        self.model_provider = model_provider;
        self.update_shared_model_state();
        self.event_bus.publish(RuntimeEventKind::ContextUpdated);
    }

    /// Registers a host-provided tool with the runtime's tool executor.
    /// Creates a default executor if none exists yet.
    pub fn register_host_tool(&mut self, tool: Arc<dyn Tool>) -> Result<()> {
        if self.tool_executor.is_none() {
            let security_policy = SecurityPolicy::new(
                self.project_dir.clone(),
                self.loaded_config.data_dir.clone(),
                self.loaded_config.config.effective_security_config(),
            )?;
            self.tool_executor = Some(Arc::new(ToolExecutor::with_security_policy(
                security_policy,
                self.runtime_components.security.clone(),
            )));
        }

        let Some(executor) = self.tool_executor.as_mut() else {
            return Err(anyhow::anyhow!("tool executor unavailable"));
        };
        let Some(executor) = Arc::get_mut(executor) else {
            return Err(anyhow::anyhow!(
                "cannot register host tool while tool executor is shared"
            ));
        };
        executor.register_tool(tool);
        self.event_bus.publish(RuntimeEventKind::ContextUpdated);
        Ok(())
    }

    /// Returns a broadcast receiver for [`RuntimeEvent`]s.
    pub fn stream_events(&self) -> broadcast::Receiver<RuntimeEvent> {
        self.event_bus.stream_events()
    }

    /// Cancels the currently running turn.
    pub fn cancel_turn(&self) {
        self.turn_canceller().cancel();
    }

    /// Resolves a pending approval by id. Returns `true` if found.
    pub fn resolve_approval(&self, decision: ApprovalDecision) -> bool {
        self.approval_resolver().resolve(decision)
    }

    /// Resolves a pending interactive question by id. Returns `true` if found.
    pub fn resolve_question(&self, response: QuestionResponse) -> bool {
        self.question_resolver().resolve(response)
    }

    /// Resolves a pending plan review by invocation id. Returns `true` if found.
    pub fn resolve_plan_review(&self, response: PlanReviewResponse) -> bool {
        self.plan_review_resolver().resolve(response)
    }

    /// Returns an [`ApprovalResolver`] handle for external approval resolution.
    pub fn approval_resolver(&self) -> ApprovalResolver {
        ApprovalResolver {
            pending_approvals: self.pending_approvals.clone(),
            runtime_events_tx: self.event_bus.sender(),
        }
    }

    /// Returns a [`QuestionResolver`] handle for external question resolution.
    pub fn question_resolver(&self) -> QuestionResolver {
        QuestionResolver {
            pending_questions: self.pending_questions.clone(),
            runtime_events_tx: self.event_bus.sender(),
        }
    }

    /// Returns a [`PlanReviewResolver`] handle for external plan-review resolution.
    pub fn plan_review_resolver(&self) -> PlanReviewResolver {
        PlanReviewResolver {
            pending_reviews: self.pending_plan_reviews.clone(),
            runtime_events_tx: self.event_bus.sender(),
        }
    }

    /// Returns a [`SudoPasswordResolver`] for interactive sudo prompts.
    pub fn sudo_password_resolver(&self) -> SudoPasswordResolver {
        SudoPasswordResolver {
            pending: self.pending_sudo_passwords.clone(),
        }
    }

    pub fn resolve_sudo_password(&self, response: SudoPasswordResponse) -> bool {
        self.sudo_password_resolver().resolve(response)
    }

    /// Returns a [`TurnCanceller`] handle for external cancellation.
    pub fn turn_canceller(&self) -> TurnCanceller {
        TurnCanceller {
            inner: self.cancel_token.clone(),
        }
    }

    /// Starts a new session (or restarts if one is already active).
    /// Returns the session id.
    pub fn start_session(&mut self) -> Result<SessionId> {
        if self.session.started() {
            self.goal_extension
                .on_session_end(self.session.id().as_str());
            self.runtime_components
                .hooks
                .on_session_end(self.session.id().as_str());

            // Light auto-memory consolidation on session end (stale + dedup, no model needed)
            let _ = self.consolidate_auto_memory();

            self.event_bus.publish(RuntimeEventKind::SessionFinished {
                session_id: self.session.id().as_str().to_string(),
            });
        }
        self.cancel_token.reset();
        self.pending_approvals = Arc::new(std::sync::Mutex::new(HashMap::new()));
        self.pending_questions = Arc::new(std::sync::Mutex::new(HashMap::new()));
        self.pending_plan_reviews = Arc::new(std::sync::Mutex::new(HashMap::new()));
        self.pending_sudo_passwords = Arc::new(std::sync::Mutex::new(HashMap::new()));
        self.session.start();

        let (session_runtime, event_rx) = self.build_session_runtime()?;
        self.session.set_runtime(session_runtime, event_rx);

        let id = self.session.id().clone();
        // Bind rewind store to this session id and attach it to the tool executor.
        self.rebind_rewind_store();
        if let Ok(_) = self.ensure_tool_executor() {
            if let Some(stored) = self.tool_executor.as_mut() {
                if let Some(exec) = Arc::get_mut(stored) {
                    exec.set_rewind_store(Some(self.rewind_store.clone()));
                }
            }
        }
        // Goal lifecycle: session start + register runtime
        self.goal_extension.on_session_start(id.as_str());
        self.runtime_components.hooks.on_session_start(id.as_str());
        self.event_bus.publish(RuntimeEventKind::SessionStarted {
            session_id: id.as_str().to_string(),
        });

        Ok(id)
    }

    /// Sends a user task to the agent and waits for the full response.
    /// Starts a session automatically if one is not active.
    /// Sends a user turn with optional multimodal content parts.
    ///
    /// When `content_parts` is non-empty, the message is created as a
    /// multimodal user message containing both text and images.
    ///
    /// After a successful turn, if a thread goal is still active, runs automatic
    /// continuation turns (idle lifecycle) until the goal stops, the auto-continue
    /// limit is hit, plan mode is on, or the user has pending input.
    pub async fn send_turn_with_parts(
        &mut self,
        task: String,
        content_parts: Vec<crate::model::ContentPart>,
        thinking_override: Option<crate::model::ThinkingConfig>,
    ) -> Result<ModelResponse> {
        let mut response = self
            .send_turn_once(task, content_parts, thinking_override)
            .await?;

        let goals_config = self.goals_config();
        let max_auto = self.effective_max_auto_continue_turns();
        let mut auto_continue_count = 0u32;
        loop {
            if !goals_config.enabled {
                break;
            }
            if max_auto > 0 && auto_continue_count >= max_auto {
                self.goal_runtime
                    .record_blocked_turn("auto-continuation limit reached");
                if let Some(goal) = self.goal_runtime.get_goal() {
                    self.publish_goal_updated(&goal);
                }
                break;
            }
            if self.has_pending_user_input() {
                break;
            }
            let Some(prompt) = self.goal_idle_prompt() else {
                break;
            };
            auto_continue_count += 1;
            tracing::info!(
                auto_continue_count,
                "starting automatic goal continuation turn"
            );
            // Continuation is injected as a model-visible steering message (not a
            // host/user chat turn). Content matches the goal steering template.
            response = self.send_turn_once(prompt, Vec::new(), None).await?;
        }

        Ok(response)
    }

    /// Single turn without goal auto-continuation.
    async fn send_turn_once(
        &mut self,
        task: String,
        content_parts: Vec<crate::model::ContentPart>,
        thinking_override: Option<crate::model::ThinkingConfig>,
    ) -> Result<ModelResponse> {
        // Mark that user input is being processed.
        self.pending_user_input
            .store(false, std::sync::atomic::Ordering::SeqCst);

        if !self.session.started() || self.session.runtime().is_none() {
            self.start_session()?;
        }

        // Apply per-turn thinking override before the turn runs so
        // build_model_request picks it up from the shared config.
        if let Some(thinking) = thinking_override {
            let level_str = thinking.as_config_str();
            // NOTE: we only update shared_config, NOT loaded_config. Mutating
            // loaded_config would permanently corrupt the original config and
            // leak the last override into future turns that pass thinking: None.
            self.shared_config
                .write()
                .unwrap_or_else(|e| e.into_inner())
                .tui
                .thinking_level = level_str.to_string();
        }

        let submission_tx = self
            .session
            .runtime()
            .ok_or_else(|| anyhow::anyhow!("session not started"))?
            .submission_tx
            .clone();

        let mut event_rx = self
            .session
            .take_event_rx()
            .ok_or_else(|| anyhow::anyhow!("session event stream unavailable"))?;

        self.cancel_token.reset();

        let turn_id = self.session.next_turn_id();
        tracing::info!(
            project = %self.project_dir.display(),
            provider = %self.loaded_config.config.model.provider,
            model = %self.loaded_config.config.model.name,
            "agent task submitted"
        );
        let session_id = self.session.id().as_str().to_string();
        self.runtime_components
            .hooks
            .on_turn_start(&session_id, &task);
        self.goal_extension.on_turn_start(&session_id, &task);
        self.record_event(AgentEvent::UserTaskSubmitted {
            text: task.clone(),
            content_parts: content_parts.clone(),
            submitted_at: Some(crate::session::current_unix_timestamp()),
        });
        self.last_user_task = task.clone();

        // Pre-turn file snapshot (dirty paths only).
        let prompt_index = self
            .session
            .events()
            .iter()
            .filter(|e| matches!(e, AgentEvent::UserTaskSubmitted { .. }))
            .count()
            .saturating_sub(1);
        let captured_index = {
            let mut store = self.rewind_store.lock().unwrap_or_else(|e| e.into_inner());
            // Ensure store is keyed to live session id.
            if store.root().to_string_lossy().contains("pending") {
                *store = crate::rewind::RewindStore::new(
                    &self.loaded_config.data_dir,
                    self.session.id().as_str(),
                    &self.project_dir,
                );
            }
            // New turn: only count writes that happen after this capture.
            store.clear_turn_written();
            match store.capture_point(
                prompt_index,
                &task,
                crate::session::current_unix_timestamp(),
            ) {
                Ok(p) => Some(p.prompt_index),
                Err(e) => {
                    tracing::warn!(error = %e, "rewind: failed to capture pre-turn snapshot");
                    None
                }
            }
        };

        // Persist immediately so sidebars list the session while the turn is
        // still running. The chat model supplies its title through the tool.
        self.persist_submitted_session();
        self.event_bus.publish(RuntimeEventKind::TurnStarted {
            turn_id: turn_id.clone(),
        });

        let (response_tx, response_rx) = tokio::sync::oneshot::channel();
        if let Err(e) = submission_tx.send(crate::session::SessionCommand::Turn(
            crate::session::Submission {
                task,
                content_parts,
                response_tx,
            },
        )) {
            return Err(anyhow::anyhow!("failed to send submission: {}", e));
        }

        let mut response_rx = response_rx;
        let result: Result<String> = loop {
            tokio::select! {
                res = &mut response_rx => {
                    break match res {
                        Ok(Ok(text)) => Ok(text),
                        Ok(Err(err)) => Err(anyhow::anyhow!("turn failed: {err}")),
                        Err(_) => Err(anyhow::anyhow!("turn cancelled or panicked")),
                    };
                }
                Some(event) = event_rx.recv() => {
                    self.record_event(event);
                    // Apply + persist as soon as `set_session_title` completes so
                    // live UIs and session lists do not wait for the whole turn.
                    if self.apply_pending_session_title() {
                        if let Err(err) = self.session.snapshot(
                            &self.project_dir,
                            &self.session_store,
                            &self.event_bus,
                            self.goal_runtime.get_goal(),
                        ) {
                            tracing::debug!(error = %err, "early title snapshot failed");
                        }
                    }
                }
            }
        };

        while let Ok(event) = event_rx.try_recv() {
            self.record_event(event);
        }
        drop(event_rx);
        self.session.set_updated_at(current_unix_timestamp());
        let _ = self.apply_pending_session_title();

        // Record paths created this turn so restore can delete them.
        if let Some(idx) = captured_index {
            let mut store = self.rewind_store.lock().unwrap_or_else(|e| e.into_inner());
            if let Err(e) = store.finalize_turn_created_paths(idx) {
                tracing::debug!(error = %e, prompt_index = idx, "rewind: finalize created_paths failed");
            }
        }

        match &result {
            Ok(text) => {
                self.goal_extension.on_turn_end(&session_id);
                self.runtime_components
                    .hooks
                    .on_turn_end(self.session.id().as_str(), text);
                self.event_bus.publish(RuntimeEventKind::TurnCompleted {
                    turn_id,
                    text: text.clone(),
                });

                // extractMemories: background extraction per turn (fire-and-forget)
                // Skip if the model already wrote memories during this turn
                let model_wrote_memory = self.turn_used_memory_write;

                if !model_wrote_memory {
                    // Build conversation snippet from user task + assistant response
                    let user_task = self.last_user_task.clone();
                    let conversation = if user_task.is_empty() {
                        format!("Assistant: {}", text)
                    } else {
                        format!("User: {}\n\nAssistant: {}", user_task, text)
                    };
                    self.try_extract_memories(&session_id, &conversation);
                }

                // Reset per-turn flag
                self.turn_used_memory_write = false;

                // Auto-dream: fire-and-forget check after each turn
                self.try_auto_dream();

                // Auto-distill: fire-and-forget check after each turn
                self.try_auto_distill();
            }
            Err(err) => {
                self.goal_extension.on_turn_error(&err.to_string());
                // Coalesce streamed deltas into a durable ModelOutput so a mid-turn
                // failure / crash does not erase what the model already produced.
                self.flush_partial_model_output_from_events();
                self.record_event(AgentEvent::Error {
                    message: err.to_string(),
                });
                // Best-effort persist immediately — Desktop/TUI also snapshot after.
                if let Err(snap_err) = self.snapshot_session() {
                    tracing::warn!(
                        error = %snap_err,
                        "failed to snapshot session after turn error"
                    );
                }
            }
        }

        result.map(|text| {
            tracing::info!(chars = text.len(), "agent task completed");
            ModelResponse { text }
        })
    }

    /// If the current turn streamed text/thinking but never emitted `ModelOutput`
    /// (e.g. provider error mid-stream), write a `ModelOutput` from the deltas so
    /// session JSON reload keeps the partial answer.
    fn flush_partial_model_output_from_events(&mut self) {
        let events = self.session.events();
        let mut last_user = None;
        for (i, event) in events.iter().enumerate() {
            if matches!(event, AgentEvent::UserTaskSubmitted { .. }) {
                last_user = Some(i);
            }
        }
        let start = last_user.map(|i| i + 1).unwrap_or(0);
        let mut text = String::new();
        let mut thinking = String::new();
        let mut saw_output = false;
        for event in &events[start..] {
            match event {
                AgentEvent::ModelOutput { .. } => {
                    saw_output = true;
                    break;
                }
                AgentEvent::ModelDelta { text: delta } => text.push_str(delta),
                AgentEvent::ModelThinkingDelta { text: delta } => thinking.push_str(delta),
                _ => {}
            }
        }
        if saw_output {
            return;
        }
        if text.is_empty() && thinking.is_empty() {
            return;
        }
        self.record_event(AgentEvent::ModelOutput {
            text,
            thinking: if thinking.is_empty() {
                None
            } else {
                Some(thinking)
            },
        });
    }

    pub async fn submit_task(&mut self, task: String) -> Result<ModelResponse> {
        self.send_turn_with_parts(task, Vec::new(), None).await
    }

    /// Sends a plain text user turn (no images).
    pub async fn send_turn(&mut self, task: String) -> Result<ModelResponse> {
        self.send_turn_with_parts(task, Vec::new(), None).await
    }

    /// Force-compact live conversation history using the session's own model.
    ///
    /// Summarizes older turns, replaces the live message list, and emits
    /// `AutoCompactStarted` / `AutoCompactCompleted` (or Failed) events.
    pub async fn compact_now(&mut self) -> Result<crate::compact::CompactOutcome> {
        if !self.session.started() || self.session.runtime().is_none() {
            // No live session loop yet — compact the seed history in place.
            self.record_event(AgentEvent::AutoCompactStarted);
            let provider = self
                .shared_model_provider
                .read()
                .unwrap_or_else(|e| e.into_inner())
                .clone();
            let model = self
                .shared_model_name
                .read()
                .unwrap_or_else(|e| e.into_inner())
                .clone();
            let harness = self.loaded_config.config.harness.clone();
            let mut state = crate::compact::CompactState::new(
                crate::config::effective_context_window(&self.loaded_config.config),
            );
            match state
                .force_compact(
                    &mut self.initial_messages,
                    provider.as_ref(),
                    &model,
                    &harness,
                )
                .await
            {
                Ok(Some(outcome)) => {
                    self.collapse_events_after_compact(&outcome);
                    // collapse already includes AutoCompactCompleted; also
                    // publish for live subscribers if session wasn't recording.
                    self.event_bus
                        .publish(RuntimeEventKind::AutoCompactCompleted {
                            tokens_saved: outcome.tokens_saved,
                            summary: outcome.summary.clone(),
                            kept_recent_messages: outcome.kept_recent_messages,
                        });
                    Ok(outcome)
                }
                Ok(None) => {
                    let reason = "nothing to compact".to_string();
                    self.record_event(AgentEvent::AutoCompactFailed {
                        reason: reason.clone(),
                    });
                    Err(anyhow::anyhow!(reason))
                }
                Err(e) => {
                    self.record_event(AgentEvent::AutoCompactFailed {
                        reason: e.to_string(),
                    });
                    Err(e)
                }
            }
        } else {
            let submission_tx = self
                .session
                .runtime()
                .ok_or_else(|| anyhow::anyhow!("session not started"))?
                .submission_tx
                .clone();

            // Drain compact events from the session loop through the normal
            // event channel so subscribers (TUI) see them live.
            let mut event_rx = self
                .session
                .take_event_rx()
                .ok_or_else(|| anyhow::anyhow!("session event stream unavailable"))?;

            let (response_tx, response_rx) = tokio::sync::oneshot::channel();
            if let Err(e) =
                submission_tx.send(crate::session::SessionCommand::Compact { response_tx })
            {
                return Err(anyhow::anyhow!("failed to send compact command: {}", e));
            }

            let mut response_rx = response_rx;
            let result: Result<crate::compact::CompactOutcome> = loop {
                tokio::select! {
                    res = &mut response_rx => {
                        break match res {
                            Ok(Ok(outcome)) => Ok(outcome),
                            Ok(Err(err)) => Err(anyhow::anyhow!("compact failed: {err}")),
                            Err(_) => Err(anyhow::anyhow!("compact cancelled or session loop exited")),
                        };
                    }
                    Some(event) = event_rx.recv() => {
                        self.record_event(event);
                    }
                }
            };

            while let Ok(event) = event_rx.try_recv() {
                self.record_event(event);
            }
            // SessionEventReceiver::Drop returns the channel to the session slot.

            if let Ok(ref outcome) = result {
                self.collapse_events_after_compact(outcome);
                self.sync_initial_messages_from_compact(outcome);
                if let Err(err) = self.snapshot_session() {
                    tracing::warn!(error = %err, "failed to snapshot session after compact");
                }
            }

            result
        }
    }

    fn collapse_events_after_compact(&mut self, outcome: &crate::compact::CompactOutcome) {
        // Rebuild a minimal event log: summary as the sole prior user turn.
        // Keeps session reloads from resurrecting the pre-compact history.
        let summary_event = AgentEvent::UserTaskSubmitted {
            text: format!(
                "Here is a summary of the conversation so far:\n\n{}",
                outcome.summary
            ),
            content_parts: Vec::new(),
            submitted_at: Some(crate::session::current_unix_timestamp()),
        };
        let completed = AgentEvent::AutoCompactCompleted {
            tokens_saved: outcome.tokens_saved,
            summary: outcome.summary.clone(),
            // Event log is fully replaced — recent turns are not reconstructed here.
            kept_recent_messages: 0,
        };
        self.session.replace_events(vec![summary_event, completed]);
    }

    fn sync_initial_messages_from_compact(&mut self, outcome: &crate::compact::CompactOutcome) {
        // Preserve any system/developer prefix already in initial_messages.
        let prefix: Vec<_> = self
            .initial_messages
            .iter()
            .filter(|m| {
                matches!(
                    m.role,
                    crate::model::ModelRole::System | crate::model::ModelRole::Developer
                )
            })
            .cloned()
            .collect();
        self.initial_messages = prefix;
        self.initial_messages
            .push(crate::compact::compact_summary_user_message(
                &outcome.summary,
            ));
    }

    /// Rewind live conversation history for an edited past user message.
    ///
    /// Keeps the first `keep_user_turns` user turns (and their assistant/tool
    /// follow-ups), drops everything after, and truncates recorded session
    /// events the same way. Also restores project files to the pre-turn state
    /// of the first dropped user prompt (session rewind).
    ///
    /// Caller should then `send_turn` with the new text (or leave the prompt in
    /// the input for the user to edit).
    ///
    /// `keep_user_turns = 0` keeps only system/developer preamble.
    pub async fn rewind_to_user_turns(&mut self, keep_user_turns: usize) -> Result<usize> {
        // Restore filesystem to state before user turn `keep_user_turns` (and
        // drop checkpoints at/after that index). Best-effort; history still rewinds.
        let fs_summary = {
            let mut store = self.rewind_store.lock().unwrap_or_else(|e| e.into_inner());
            store.restore_to(keep_user_turns)
        };
        if !fs_summary.errors.is_empty() {
            for err in &fs_summary.errors {
                tracing::warn!(error = %err, "rewind: filesystem restore issue");
            }
        }
        if fs_summary.total_changes() > 0 {
            tracing::info!(
                restored = fs_summary.restored,
                deleted = fs_summary.deleted,
                keep_user_turns,
                "rewind: restored project files"
            );
        }

        if !self.session.started() || self.session.runtime().is_none() {
            // Nothing live yet — just reset seed messages/events for a clean start.
            self.session.truncate_events_to_user_turns(keep_user_turns);
            // Seed messages for next start_session: drop user turns past keep.
            crate::session::truncate_messages_to_user_turns(
                &mut self.initial_messages,
                keep_user_turns,
            );
            return Ok(self.initial_messages.len());
        }

        let submission_tx = self
            .session
            .runtime()
            .ok_or_else(|| anyhow::anyhow!("session not started"))?
            .submission_tx
            .clone();

        let (response_tx, response_rx) = tokio::sync::oneshot::channel();
        if let Err(e) = submission_tx.send(crate::session::SessionCommand::TruncateToUserTurns {
            keep_user_turns,
            response_tx,
        }) {
            return Err(anyhow::anyhow!("failed to send rewind command: {}", e));
        }

        let remaining = response_rx
            .await
            .map_err(|_| anyhow::anyhow!("rewind cancelled or session loop exited"))??;

        self.session.truncate_events_to_user_turns(keep_user_turns);
        // Keep initial_messages in sync if the session is later restarted.
        crate::session::truncate_messages_to_user_turns(
            &mut self.initial_messages,
            keep_user_turns,
        );

        // Persist truncated history so reload does not resurrect dropped turns.
        if let Err(err) = self.snapshot_session() {
            tracing::warn!(error = %err, "failed to snapshot session after rewind");
        }

        Ok(remaining)
    }

    /// List rewind checkpoints (user-turn snapshots) for the live session.
    pub fn list_rewind_points(&self) -> Vec<crate::rewind::RewindPointMeta> {
        self.rewind_store
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .load_points()
    }

    /// Filesystem restore summary from the last `restore_to` is not retained;
    /// use [`Self::rewind_to_user_turns`] which restores as a side effect.
    pub fn rewind_store_points_path(&self) -> std::path::PathBuf {
        self.rewind_store
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .root()
            .to_path_buf()
    }

    /// Applies a title set by the current chat model and informs live clients.
    ///
    /// Returns `true` when a new title was applied so callers can decide whether
    /// to persist immediately (mid-turn) or fold the change into a later snapshot.
    fn apply_pending_session_title(&mut self) -> bool {
        let Some(title) = self.session_title_handle.take() else {
            return false;
        };
        self.session.set_title(Some(title.clone()));
        self.event_bus
            .publish(RuntimeEventKind::SessionTitleUpdated {
                session_id: self.session.id().as_str().to_string(),
                title,
            });
        true
    }

    /// Creates a [`SessionSnapshot`] of the current session state for persistence.
    pub fn snapshot_session(&mut self) -> Result<SessionSnapshot> {
        self.session.set_updated_at(current_unix_timestamp());
        let _ = self.apply_pending_session_title();
        let snapshot = self.session.snapshot(
            &self.project_dir,
            &self.session_store,
            &self.event_bus,
            self.goal_runtime.get_goal(),
        )?;
        self.save_trace_snapshot(snapshot.id.as_str());
        Ok(snapshot)
    }

    /// Creates and persists a session snapshot without blocking the async runtime.
    pub async fn snapshot_session_async(&mut self) -> Result<SessionSnapshot> {
        self.session.set_updated_at(current_unix_timestamp());
        let _ = self.apply_pending_session_title();
        let snapshot = self
            .session
            .snapshot_async(
                &self.project_dir,
                &self.session_store,
                &self.event_bus,
                self.goal_runtime.get_goal(),
            )
            .await?;
        self.save_trace_snapshot(snapshot.id.as_str());
        Ok(snapshot)
    }

    fn save_trace_snapshot(&self, session_id: &str) {
        let traces = turn_traces_from_events(
            session_id,
            &self.loaded_config.config.model.provider,
            &self.loaded_config.config.model.name,
            self.session.events(),
        );
        if traces.is_empty() {
            return;
        }
        let store = TraceStore::new(&self.loaded_config.data_dir);
        if let Err(err) = store.save_session_traces(session_id, &traces) {
            tracing::warn!(error = %err, session_id, "failed to save turn traces");
        }
    }

    /// Test-only accessor; production code uses `self.session_store` directly.
    #[cfg(test)]
    pub(crate) fn session_store(&self) -> &SessionStore {
        &self.session_store
    }

    fn ensure_tool_executor(&mut self) -> Result<Arc<ToolExecutor>> {
        if let Some(executor) = self.tool_executor.as_mut() {
            if let Some(executor) = Arc::get_mut(executor) {
                if !self.skip_auto_tool_bootstrap {
                    Self::register_goal_tools_on_executor(
                        executor,
                        self.goal_runtime.clone(),
                        self.loaded_config.config.goals.enabled,
                    );
                    executor.register_skill_loader(
                        self.project_dir.clone(),
                        self.loaded_config.data_dir.clone(),
                        self.shared_config.clone(),
                    );
                }
                executor.set_rewind_store(Some(self.rewind_store.clone()));
            } else {
                tracing::warn!(
                    "tool executor Arc is shared; cannot install goal tools in place \
                     (strong_count={})",
                    Arc::strong_count(executor)
                );
            }
            return Ok(executor.clone());
        }

        let security_policy = SecurityPolicy::new(
            self.project_dir.clone(),
            self.loaded_config.data_dir.clone(),
            self.loaded_config.config.effective_security_config(),
        )?;
        let harness_policy = crate::harness::select_harness_policy(&self.loaded_config.config);
        let profile_name = format!("{:?}", harness_policy.profile).to_lowercase();
        let mut executor = ToolExecutor::with_security_policy(
            security_policy,
            self.runtime_components.security.clone(),
        );
        executor.set_harness_profile(profile_name);
        Self::register_goal_tools_on_executor(
            &mut executor,
            self.goal_runtime.clone(),
            self.loaded_config.config.goals.enabled,
        );
        executor.register_skill_loader(
            self.project_dir.clone(),
            self.loaded_config.data_dir.clone(),
            self.shared_config.clone(),
        );
        executor.set_rewind_store(Some(self.rewind_store.clone()));

        let workflow_config = self.loaded_config.config.workflow.clone();
        let workflow_policy = SecurityPolicy::new(
            self.project_dir.clone(),
            self.loaded_config.data_dir.clone(),
            self.loaded_config.config.security.clone(),
        )
        .unwrap_or_else(|_| {
            SecurityPolicy::new(
                self.project_dir.clone(),
                self.loaded_config.data_dir.clone(),
                crate::config::SecurityConfig::default(),
            )
            .expect("default security policy")
        });
        let executor = Arc::new_cyclic(|executor_weak| {
            let subagent = SubagentTool::new(
                executor_weak.clone(),
                self.shared_model_provider.clone(),
                self.project_dir.clone(),
                self.loaded_config.data_dir.clone(),
                self.shared_model_name.clone(),
                self.loaded_config.config.harness.clone(),
                self.shared_config.clone(),
                self.prompt_cache.clone(),
                self.runtime_components.clone(),
            );
            executor.register_tool(Arc::new(subagent));
            // BM25 + symbol index — cheap, no nested agent.
            executor.register_tool(Arc::new(RepoExploreTool::new(self.project_dir.clone())));
            // Production workflow: each agent() is a real nested subagent turn
            // with policy-intersected tool allowlists (own max_parallel semaphore).
            executor.register_tool(Arc::new(crate::tool::WorkflowTool::with_subagent_bridge(
                workflow_policy.clone(),
                workflow_config.clone(),
                executor_weak.clone(),
            )));
            executor
        });
        // If plan mode was entered before the executor existed, re-apply the gate.
        if self.agent_mode() == crate::plan_mode::AgentMode::Plan {
            let plan_path = crate::plan_store::session_plan_file_path(
                &self.loaded_config.data_dir,
                self.session.id().as_str(),
            );
            executor.policy().set_plan_mode(true, Some(plan_path));
        }

        self.tool_executor = Some(executor.clone());
        Ok(executor)
    }

    fn register_goal_tools_on_executor(
        executor: &mut ToolExecutor,
        goal_runtime: Arc<GoalRuntimeHandle>,
        goals_enabled: bool,
    ) {
        if !goals_enabled {
            return;
        }
        // Model-facing goal surface: get / create / update only.
        // Host checklist mutations stay on the SDK/server API, not the model schema.
        executor.register_tool(Arc::new(GetGoalTool::new(goal_runtime.clone())));
        executor.register_tool(Arc::new(CreateGoalTool::new(goal_runtime.clone())));
        executor.register_tool(Arc::new(UpdateGoalTool::new(goal_runtime)));
    }

    /// Returns the configured tool executor, if any.
    pub fn tool_executor(&self) -> Option<Arc<ToolExecutor>> {
        self.tool_executor.clone()
    }

    /// Keep only tools whose names satisfy `pred` on the live session executor.
    ///
    /// Used by host tool profiles after start (goal/skill tools may re-register).
    /// No-ops when the executor Arc is shared.
    pub fn retain_tools<F>(&mut self, pred: F)
    where
        F: FnMut(&str) -> bool,
    {
        if let Some(exec) = self.tool_executor.as_mut() {
            if let Some(e) = Arc::get_mut(exec) {
                e.retain_tools(pred);
            } else {
                tracing::warn!(
                    strong_count = Arc::strong_count(exec),
                    "cannot retain tools; tool executor Arc is shared"
                );
            }
        }
    }

    /// Returns the session-local handle used by the title tool. SDK tooling
    /// reloads use this to keep the built-in title capability installed.
    pub fn session_title_handle(&self) -> SessionTitleHandle {
        self.session_title_handle.clone()
    }

    /// Replaces the session tool executor (e.g. after installing WASM plugins).
    pub fn set_tool_executor(&mut self, executor: Arc<ToolExecutor>) {
        self.tool_executor = Some(executor);
    }

    /// Updates the security configuration used by the tool executor and the
    /// runtime's loaded config. No-op when no executor has been created yet.
    pub fn set_security_config(&mut self, security: SecurityConfig) -> Result<()> {
        self.loaded_config.config.security = security.clone();
        self.loaded_config.config.tui.yolo_mode =
            matches!(security.permission_mode, PermissionMode::Yolo);

        let Some(executor) = self.tool_executor.as_mut() else {
            return Ok(());
        };
        let Some(executor) = Arc::get_mut(executor) else {
            return Err(anyhow::anyhow!(
                "cannot update security policy while tool executor is shared"
            ));
        };
        let mut policy = executor.security_policy().clone();
        policy.set_config(security);
        executor.set_security_policy(policy);
        Ok(())
    }

    /// Returns a shared [`MemoryManager`], opening SQLite stores at most once.
    fn get_or_init_memory_manager(&self) -> Result<Option<Arc<crate::memory::MemoryManager>>> {
        let memory_config = &self.loaded_config.config.memory;
        if !memory_config.enabled {
            return Ok(None);
        }
        let mut guard = self
            .memory_manager
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        if let Some(manager) = guard.as_ref() {
            return Ok(Some(manager.clone()));
        }
        let manager = Arc::new(crate::memory::MemoryManager::new(
            self.project_dir.clone(),
            self.loaded_config.data_dir.clone(),
            memory_config,
        )?);
        *guard = Some(manager.clone());
        Ok(Some(manager))
    }

    /// Runs a lightweight auto-memory consolidation (stale detection + dedup).
    /// Called on session end. Does not require a model provider.
    fn consolidate_auto_memory(&self) -> Result<()> {
        let Some(manager) = self.get_or_init_memory_manager()? else {
            return Ok(());
        };

        let db_path = manager.auto_memory.db_path.clone();
        if !db_path.exists() {
            return Ok(());
        }

        // Reuse the already-open store rather than reopening the DB.
        let report = manager.auto_memory.consolidate(30)?;

        if report.marked_stale > 0 || report.duplicates_merged > 0 {
            tracing::info!(
                "auto-memory consolidation on session end: {} stale, {} duplicates, {} active",
                report.marked_stale,
                report.duplicates_merged,
                report.remaining_active
            );
        }

        Ok(())
    }

    /// Persist a newly submitted session so it appears in saved-session lists.
    /// Naming is deliberately not done here: a second completion would both cost
    /// credits and create an unrelated request that harms provider cache affinity.
    fn persist_submitted_session(&mut self) {
        // Always snapshot on user submit so live sessions appear in list_saved
        // while the agent is still working.
        if let Err(err) = self.snapshot_session() {
            tracing::warn!(error = %err, "early session snapshot after user message failed");
        }
    }

    /// extractMemories: background per-turn memory extraction.
    /// Spawns a tokio task that calls the model to extract durable memories
    /// from the completed turn. Fire-and-forget — does not block the agent loop.
    fn try_extract_memories(&self, _session_id: &str, conversation: &str) {
        // Extraction never borrows the interactive chat model. It only runs
        // after the user explicitly configures a dedicated background model.
        let Some(extraction_model) = &self.memory_extraction_model else {
            tracing::debug!("extract-memories skipped: no memory extraction model configured");
            return;
        };
        let provider = extraction_model.provider.clone();
        let model_name = extraction_model.model_name.clone();
        let conversation = conversation.to_string();

        let manager = match self.get_or_init_memory_manager() {
            Ok(Some(m)) => m,
            Ok(None) => return,
            Err(e) => {
                tracing::debug!("extract-memories: failed to init memory manager: {}", e);
                return;
            }
        };

        let db_path = manager.auto_memory.db_path.clone();
        if !db_path.exists() {
            return;
        }

        let store = manager.auto_memory.clone();

        // Fire-and-forget
        tokio::spawn(async move {
            match crate::memory::extract::extract_memories(
                &conversation,
                provider.as_ref(),
                &model_name,
                &store,
            )
            .await
            {
                Ok(n) => {
                    if n > 0 {
                        tracing::info!("extract-memories: saved {} memories from turn", n);
                    }
                }
                Err(e) => {
                    tracing::debug!("extract-memories failed: {}", e);
                }
            }
        });
    }

    /// Auto-dream: checks 3 gates after each turn and spawns consolidation if all pass.
    /// Fire-and-forget — does not block the agent loop.
    fn try_auto_dream(&self) {
        let memory_config = &self.loaded_config.config.memory;
        let manager = match self.get_or_init_memory_manager() {
            Ok(Some(m)) => m,
            Ok(None) => return,
            Err(e) => {
                tracing::debug!("auto-dream: failed to init memory manager: {}", e);
                return;
            }
        };

        let interval_hours = memory_config.dream_interval_days * 24;
        let state =
            crate::memory::auto_dream::AutoDreamState::new(manager.store.memory_root.clone())
                .with_interval(interval_hours.max(1));

        let history = manager.history.clone();

        let last_dream = state.read_last_dream_at();
        if !state.should_dream(&history) {
            return;
        }

        let db_path = manager.auto_memory.db_path.clone();
        let hours_since = if last_dream > 0 {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_secs())
                .unwrap_or(0);
            (now.saturating_sub(last_dream)) / 3600
        } else {
            0
        };
        let sessions_count = history.list_sessions().map(|s| s.len()).unwrap_or(0);

        tracing::info!(
            "auto-dream triggered: {}h since last, {} sessions",
            hours_since,
            sessions_count
        );

        self.event_bus.publish(RuntimeEventKind::AutoDreamStarted {
            hours_since_last: hours_since,
            sessions_reviewed: sessions_count,
        });

        let memory_root = manager.store.memory_root.clone();
        let event_sender = self.event_bus.sender();
        tokio::spawn(async move {
            let result = run_auto_dream_consolidation(&db_path).await;

            let dream_state = crate::memory::auto_dream::AutoDreamState::new(memory_root);
            match result {
                Ok(report) => {
                    tracing::info!(
                        "auto-dream completed: {} stale, {} duplicates, {} active",
                        report.marked_stale,
                        report.duplicates_merged,
                        report.remaining_active
                    );
                    dream_state.mark_completed();
                    let _ = event_sender.send(RuntimeEvent::new(
                        RuntimeEventKind::AutoDreamCompleted {
                            marked_stale: report.marked_stale,
                            duplicates_merged: report.duplicates_merged,
                            active_count: report.remaining_active,
                        },
                    ));
                }
                Err(e) => {
                    tracing::warn!("auto-dream failed: {}", e);
                    dream_state.release();
                    let _ =
                        event_sender.send(RuntimeEvent::new(RuntimeEventKind::AutoDreamFailed {
                            reason: e.to_string(),
                        }));
                }
            }
        });
    }

    /// Auto-distill: checks time gate after each turn and spawns distill if enough time passed.
    /// Fire-and-forget — does not block the agent loop.
    fn try_auto_distill(&self) {
        let memory_config = &self.loaded_config.config.memory;
        if memory_config.distill_interval_days == 0 {
            return;
        }

        let manager = match self.get_or_init_memory_manager() {
            Ok(Some(m)) => m,
            _ => return,
        };

        let interval_hours = memory_config.distill_interval_days * 24;
        let state = crate::memory::auto_dream::AutoDreamState::new(
            manager.store.memory_root.join("distill"),
        )
        .with_interval(interval_hours)
        .with_min_sessions(3);

        let history = manager.history.clone();
        if !state.should_dream(&history) {
            return;
        }

        tracing::info!("auto-distill triggered");

        let auto_memory = manager.auto_memory.clone();
        tokio::spawn(async move {
            // Distill only does stale + dedup (no model-based SOP extraction in auto mode)
            match auto_memory.consolidate(60) {
                Ok(report) => {
                    tracing::info!(
                        "auto-distill completed: {} stale, {} duplicates, {} active",
                        report.marked_stale,
                        report.duplicates_merged,
                        report.remaining_active
                    );
                    state.mark_completed();
                }
                Err(e) => {
                    tracing::warn!("auto-distill failed: {}", e);
                    state.release();
                }
            }
        });
    }

    fn build_session_runtime(
        &mut self,
    ) -> Result<(
        crate::session::SessionRuntime,
        mpsc::UnboundedReceiver<AgentEvent>,
    )> {
        let tool_executor = self.ensure_tool_executor()?;
        let (event_tx, event_rx) = mpsc::unbounded_channel();
        let memory_injection =
            self.session_store
                .load_memory(&self.project_dir)
                .and_then(|memory| {
                    memory.format_injection(self.loaded_config.config.memory.max_memory_entries)
                });

        // Catalog: root skills + pools (metadata only). Bodies via load_skill.
        let root = self.load_catalog_skills();
        let pools = self.load_catalog_pools();
        *self
            .shared_available_skills
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = root.clone();
        *self
            .shared_skill_pools
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = pools;
        // Soft harness only for session-active skills (not every discovered skill).
        let harness_skills = self.session_active_skill_manifests();
        *self
            .shared_active_skills
            .lock()
            .unwrap_or_else(|e| e.into_inner()) = harness_skills.clone();
        self.apply_harness_packs_for_active(&harness_skills);

        let ctx = Arc::new(crate::turn::TurnContext {
            model_provider: self.shared_model_provider.clone(),
            tool_executor,
            project_dir: self.project_dir.clone(),
            data_dir: self.loaded_config.data_dir.clone(),
            model_name: self.shared_model_name.clone(),
            event_tx: Some(event_tx),
            approval_resolver: self.approval_resolver(),
            question_resolver: self.question_resolver(),
            plan_review_resolver: self.plan_review_resolver(),
            sudo_password_resolver: self.sudo_password_resolver(),
            compact_state: Arc::new(tokio::sync::Mutex::new(crate::compact::CompactState::new(
                crate::config::effective_context_window(&self.loaded_config.config),
            ))),
            harness_config: self.loaded_config.config.harness.clone(),
            include_tool_prompt_manifest: crate::config::effective_tool_prompt_manifest(
                &self.loaded_config.config,
            ),
            context_packets: self.shared_context_packets.clone(),
            available_skills: self.shared_available_skills.clone(),
            skill_pools: self.shared_skill_pools.clone(),
            // Prompt path uses available_skills + skill_pools; no instruction bodies.
            active_skills: Arc::new(std::sync::Mutex::new(Vec::new())),
            prompt_cache: self.prompt_cache.clone(),
            instructions: std::sync::Arc::new(std::sync::RwLock::new(None)),
            prompt_prefix: std::sync::Arc::new(std::sync::Mutex::new(None)),
            components: self.runtime_components.clone(),
            cancel_token: self.cancel_token.clone(),
            config: self.shared_config.clone(),
            memory_injection: memory_injection.clone(),
            compaction_provider: None,
            agent_mode: self.agent_mode(),
            compaction_model_name: None,
            session_id: self.session.id().as_str().to_string(),
            // Catalog-active skills do not lock tools. Only session-active harness
            // packs / harness-flagged skills may contribute an allowlist.
            allowed_tool_names: self.harness_allow_tools.clone(),
            is_subagent: false,
            memory_manager: self.memory_manager.clone(),
            harness_card: self.harness_card.clone(),
        });

        let policy = select_harness_policy(&self.loaded_config.config);
        let session_runtime = crate::session::SessionRuntime::spawn(
            ctx,
            policy,
            self.initial_messages.clone(),
            memory_injection,
        );

        Ok((session_runtime, event_rx))
    }

    fn record_event(&mut self, event: AgentEvent) {
        // ── Goal accounting driven by agent events ────────────────
        match &event {
            AgentEvent::ToolCompleted(_result) => {
                // Track if the model used the memory tool with write action during this turn
                if _result.ok {
                    if let Some(output) = _result.output.as_object() {
                        if output.get("tool_name").and_then(|v| v.as_str()) == Some("memory") {
                            self.turn_used_memory_write = true;
                        }
                    }
                }
                let budget_prompt = self.goal_extension.on_tool_complete();
                if budget_prompt.is_some() {
                    if let Some(goal) = self.goal_runtime.get_goal() {
                        self.event_bus.publish(RuntimeEventKind::GoalUpdated {
                            session_id: goal.session_id.clone(),
                            goal_id: goal.goal_id.as_str().to_string(),
                            objective: goal.objective.clone(),
                            short_description: goal.short_description.clone(),
                            status: goal.status,
                            tokens_used: goal.tokens_used,
                            token_budget: goal.token_budget,
                        });
                    }
                }
            }
            AgentEvent::UsageReported {
                input_tokens,
                output_tokens,
                ..
            } => {
                let exceeded = self
                    .goal_extension
                    .on_token_usage(*input_tokens, *output_tokens);
                if exceeded {
                    if let Some(goal) = self.goal_runtime.get_goal() {
                        self.event_bus.publish(RuntimeEventKind::GoalUpdated {
                            session_id: goal.session_id.clone(),
                            goal_id: goal.goal_id.as_str().to_string(),
                            objective: goal.objective.clone(),
                            short_description: goal.short_description.clone(),
                            status: goal.status,
                            tokens_used: goal.tokens_used,
                            token_budget: goal.token_budget,
                        });
                    }
                }
            }
            AgentEvent::SetGoalRequested {
                objective,
                short_description,
                token_budget,
            } => {
                let goal = self.goal_runtime.set_objective_with_short_description(
                    objective.clone(),
                    short_description.clone(),
                    *token_budget,
                );
                self.goal_runtime.set_auto_continue(true);
                self.publish_goal_updated(&goal);
            }
            AgentEvent::GoalUpdated {
                session_id,
                goal_id,
                objective,
                short_description,
                status,
                tokens_used,
                token_budget,
            } => {
                // Tool-side goal tools already mutated GoalRuntimeHandle; fan out
                // to the runtime event bus so TUI/SDK subscribers stay in sync.
                self.event_bus.publish(RuntimeEventKind::GoalUpdated {
                    session_id: session_id.clone(),
                    goal_id: goal_id.clone(),
                    objective: objective.clone(),
                    short_description: short_description.clone(),
                    status: *status,
                    tokens_used: *tokens_used,
                    token_budget: *token_budget,
                });
            }
            AgentEvent::ModelDelta { text } => {
                // Feed text deltas to plan parser when in Plan mode.
                if self.agent_mode() == crate::plan_mode::AgentMode::Plan {
                    let plans = self.feed_plan_text(&text);
                    for plan in plans {
                        self.event_bus.publish(RuntimeEventKind::PlanProposed {
                            session_id: self.session.id().as_str().to_string(),
                            title: plan.title,
                            steps: plan.steps,
                        });
                    }
                }
            }
            AgentEvent::ModelOutput { text, .. } => {
                // Feed final model output to plan parser (catches plans at end of turn).
                if self.agent_mode() == crate::plan_mode::AgentMode::Plan {
                    let plans = self.feed_plan_text(text);
                    for plan in plans {
                        self.event_bus.publish(RuntimeEventKind::PlanProposed {
                            session_id: self.session.id().as_str().to_string(),
                            title: plan.title,
                            steps: plan.steps,
                        });
                    }
                    // Also drain any unclosed plans at end of turn.
                    let remaining = self.drain_plans();
                    for plan in remaining {
                        self.event_bus.publish(RuntimeEventKind::PlanProposed {
                            session_id: self.session.id().as_str().to_string(),
                            title: plan.title,
                            steps: plan.steps,
                        });
                    }
                }
            }
            AgentEvent::PlanProposed { .. } => {}
            AgentEvent::PlanReviewRequested(_) | AgentEvent::PlanReviewResolved(_) => {}
            AgentEvent::SudoPasswordRequested(_) => {}
            AgentEvent::AgentModeChanged { .. } => {}
            _ => {}
        }

        if let Some(tx) = &self.event_tx {
            let _ = tx.send(event.clone());
        }
        // Stream deltas are live-only: keep publishing to subscribers / event bus
        // but do not persist them in the session event log (RAM + disk blowup).
        let transient = matches!(
            event,
            AgentEvent::SubagentActivity { .. }
                | AgentEvent::SubagentTranscript { .. }
                | AgentEvent::ModelDelta { .. }
                | AgentEvent::ModelThinkingDelta { .. }
                | AgentEvent::ToolCallStreaming { .. }
                | AgentEvent::StreamResuming { .. }
        );
        if let Some(kind) = runtime_event_kind_from_agent_event(&event) {
            self.event_bus.publish(kind);
        }
        if !transient {
            self.session.push_event(event);
        }
    }

    fn update_shared_model_state(&self) {
        *self
            .shared_model_provider
            .write()
            .unwrap_or_else(|e| e.into_inner()) = self.model_provider.clone();
        *self
            .shared_model_name
            .write()
            .unwrap_or_else(|e| e.into_inner()) = provider_request_model_name(
            &self.loaded_config.config.model.provider,
            &self.loaded_config.config.model.name,
        );
        *self
            .shared_config
            .write()
            .unwrap_or_else(|e| e.into_inner()) = self.loaded_config.config.clone();
    }

    /// Root-level catalog skills (metadata only). Pool members are not included.
    fn load_catalog_skills(&self) -> Vec<SkillManifest> {
        match discover_catalog_entries(
            &self.loaded_config.config.skills,
            &self.project_dir,
            &self.loaded_config.data_dir,
        ) {
            Ok(catalog) => active_skills(
                &catalog.root_skills,
                &self.loaded_config.config.skills.active,
                &self.active_skills,
            ),
            Err(err) => {
                tracing::warn!(error = %err, "failed to load catalog skills");
                Vec::new()
            }
        }
    }

    fn load_catalog_pools(&self) -> Vec<SkillPool> {
        match discover_catalog_entries(
            &self.loaded_config.config.skills,
            &self.project_dir,
            &self.loaded_config.data_dir,
        ) {
            Ok(catalog) => catalog.pools,
            Err(err) => {
                tracing::warn!(error = %err, "failed to load skill pools");
                Vec::new()
            }
        }
    }

    fn load_active_skills(&self) -> Vec<SkillManifest> {
        self.load_catalog_skills()
    }

    fn load_available_skills(&self) -> Vec<SkillManifest> {
        self.load_catalog_skills()
    }

    fn discover_available_skills(&self) -> Result<Vec<SkillManifest>> {
        // Full discovery (root + pool members) for load_skill / harness.
        discover_configured_skills(
            &self.loaded_config.config.skills,
            &self.project_dir,
            &self.loaded_config.data_dir,
        )
    }
}

fn runtime_event_kind_from_agent_event(event: &AgentEvent) -> Option<RuntimeEventKind> {
    match event {
        AgentEvent::ModelDelta { text } => {
            Some(RuntimeEventKind::AssistantDelta { text: text.clone() })
        }
        AgentEvent::ModelThinkingDelta { text } => {
            Some(RuntimeEventKind::AssistantThinkingDelta { text: text.clone() })
        }
        AgentEvent::ToolRequested(invocation) => {
            Some(RuntimeEventKind::ToolRequested(invocation.clone()))
        }
        AgentEvent::ToolCompleted(result) => Some(RuntimeEventKind::ToolCompleted(result.clone())),
        AgentEvent::SubagentActivity {
            invocation_id,
            message,
        } => Some(RuntimeEventKind::SubagentActivity {
            invocation_id: invocation_id.clone(),
            message: message.clone(),
        }),
        AgentEvent::SubagentTranscript {
            invocation_id,
            item,
        } => Some(RuntimeEventKind::SubagentTranscript {
            invocation_id: invocation_id.clone(),
            item: item.clone(),
        }),
        AgentEvent::ApprovalRequested(request) => {
            Some(RuntimeEventKind::ApprovalRequired(request.clone()))
        }
        AgentEvent::UsageReported {
            input_tokens,
            output_tokens,
            cache_creation_tokens,
            cache_read_tokens,
        } => Some(RuntimeEventKind::TokensUpdated {
            input_tokens: *input_tokens,
            output_tokens: *output_tokens,
            cache_creation_tokens: *cache_creation_tokens,
            cache_read_tokens: *cache_read_tokens,
        }),
        AgentEvent::Error { message } => Some(RuntimeEventKind::Error {
            message: message.clone(),
        }),
        AgentEvent::ApprovalResolved(decision) => {
            Some(RuntimeEventKind::ApprovalResolved(decision.clone()))
        }
        AgentEvent::CapabilityRecorded(entry) => {
            Some(RuntimeEventKind::CapabilityRecorded(entry.clone()))
        }
        AgentEvent::QuestionRequested(request) => {
            Some(RuntimeEventKind::QuestionRequired(request.clone()))
        }
        AgentEvent::QuestionResolved(response) => {
            Some(RuntimeEventKind::QuestionResolved(response.clone()))
        }
        AgentEvent::PlanReviewRequested(request) => {
            Some(RuntimeEventKind::PlanReviewRequired(request.clone()))
        }
        AgentEvent::PlanReviewResolved(response) => {
            Some(RuntimeEventKind::PlanReviewResolved(response.clone()))
        }
        AgentEvent::SudoPasswordRequested(request) => {
            Some(RuntimeEventKind::SudoPasswordRequired(request.clone()))
        }
        AgentEvent::HarnessTrace(value) => Some(RuntimeEventKind::HarnessTrace(value.clone())),
        AgentEvent::HarnessStopped {
            reason,
            message,
            tool_name,
        } => Some(RuntimeEventKind::HarnessStopped {
            reason: reason.clone(),
            message: message.clone(),
            tool_name: tool_name.clone(),
        }),
        AgentEvent::PatchProposed(patch) => Some(RuntimeEventKind::PatchProposed(patch.clone())),
        AgentEvent::MicroCompactApplied { messages_cleared } => {
            Some(RuntimeEventKind::MicroCompactApplied {
                messages_cleared: *messages_cleared,
            })
        }
        AgentEvent::AutoCompactStarted => Some(RuntimeEventKind::AutoCompactStarted),
        AgentEvent::AutoCompactCompleted {
            tokens_saved,
            summary,
            kept_recent_messages,
        } => Some(RuntimeEventKind::AutoCompactCompleted {
            tokens_saved: *tokens_saved,
            summary: summary.clone(),
            kept_recent_messages: *kept_recent_messages,
        }),
        AgentEvent::AutoCompactFailed { reason } => Some(RuntimeEventKind::AutoCompactFailed {
            reason: reason.clone(),
        }),
        AgentEvent::UserTaskSubmitted { .. } | AgentEvent::ModelOutput { .. } => None,
        AgentEvent::RepeatedToolCallWarning { .. } => None,
        AgentEvent::RepetitionDetected { .. } => None,
        AgentEvent::GoalUpdated { .. } => None,
        AgentEvent::SetGoalRequested {
            objective,
            short_description,
            token_budget,
        } => Some(RuntimeEventKind::SetGoalRequested {
            objective: objective.clone(),
            short_description: short_description.clone(),
            token_budget: *token_budget,
        }),
        AgentEvent::AutoDreamStarted {
            hours_since_last,
            sessions_reviewed,
        } => Some(RuntimeEventKind::AutoDreamStarted {
            hours_since_last: *hours_since_last,
            sessions_reviewed: *sessions_reviewed,
        }),
        AgentEvent::AutoDreamCompleted {
            marked_stale,
            duplicates_merged,
            active_count,
        } => Some(RuntimeEventKind::AutoDreamCompleted {
            marked_stale: *marked_stale,
            duplicates_merged: *duplicates_merged,
            active_count: *active_count,
        }),
        AgentEvent::AutoDreamFailed { reason } => Some(RuntimeEventKind::AutoDreamFailed {
            reason: reason.clone(),
        }),
        AgentEvent::PlanProposed { .. } => None,
        // Also mapped above via dedicated arms; keep fallthrough safe.
        AgentEvent::AgentModeChanged { .. } => None,
        AgentEvent::SessionRecap { .. } => None,
        // Transient mid-stream resume signal; live UI only, not a runtime kind.
        AgentEvent::StreamResuming { .. } => None,
        // Live-only tool-call argument stream; UI progress, not a runtime kind.
        AgentEvent::ToolCallStreaming { .. } => None,
        // Host-facing UI events (not replayed as runtime kinds).
        AgentEvent::NotificationRequested { .. } | AgentEvent::UpdateAvailable { .. } => None,
    }
}

/// Runs the auto-dream consolidation pass (stale + dedup) on the auto-memory SQLite store.
/// Called from a tokio::spawn background task — must not access AgentRuntime state.
async fn run_auto_dream_consolidation(
    db_path: &std::path::Path,
) -> anyhow::Result<crate::memory::ConsolidationReport> {
    let store = crate::memory::AutoMemoryStore::open(db_path)?;
    store.consolidate(30)
}