artifactize-core 0.26.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
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
//! Artifactize's worktree-bound coding conversation.
//!
//! Unifies agentic CLI backends on ACP (`ChatTarget::Acp`) alongside direct HTTP API providers
//! (`ChatTarget::Direct`), while preserving read-only historical access for retired
//! `LegacyClaudeAgent` sessions (`C-CHAT-ACP-UNIFIED`, `C-TRUST-BEFORE-WORKTREE`,
//! `C-ONE-TURN-OWNERSHIP`, `C-ACP-EDGE-ISOLATION`).

mod details;
#[cfg(test)]
mod tests;
pub(crate) mod tools;
mod transcript;

use std::{
    collections::{BTreeMap, btree_map::Entry as Slot},
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
    time::Duration,
};

use serde::{Deserialize, Serialize};
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;

use crate::{
    acp::{
        self, AcpBinding, AgentId, ConfigCategory, ConfigOption, ConfigSelection,
        VerifyBridgeState, WarmOwner,
        catalog::{self, AgentAvailability},
        engine::{AcpTurnInput, VerifiedAcpLaunch},
        pool::{IDLE_TTL, Pool},
        trust::{TrustRecord, TrustRequirement, TrustScope},
    },
    agent::{
        self,
        session::{Answer, Conversation, Kind, Recorder, Send},
    },
    auth::providers,
    config::{CustomBackendName, DirectBackend, HttpProvider, ModelId, Provider},
    llm,
    monitor::live::{self, Branch, Place},
    repository, runs,
    store::{self, Receipts},
    types::{SessionId, Timestamp},
};

pub use crate::store::chat::ChatWatermark;
pub use details::ToolKind;
pub use transcript::{Block, RunCard, ToolResult, Turn, TurnState};

pub(crate) fn recorded_outcome(state: &Path, id: &SessionId) -> Option<Outcome> {
    Conversation::load(&path(state, id))
        .ok()
        .flatten()
        .as_ref()
        .and_then(transcript::last_outcome)
}

/// The Chat branch choices and the sessions observed while discovering them.
pub struct BranchListing {
    pub branches: Vec<Branch>,
    pub sessions: Vec<Session>,
}

/// A description comes from the first nonempty prompt line, not another provider call.
const TITLE_CHARS: usize = 72;
/// Limit one prompt so a caller cannot overwhelm the transcript and model request at once.
const PROMPT_BYTES: usize = 256 * 1024;
/// A turn checks for a durable stop from another app even when the provider sends no data.
const STOP_CHECK: Duration = Duration::from_millis(250);
/// Extra slack after `IDLE_TTL` before the background warm-pool timer reaps an idle child.
const IDLE_REAP_MARGIN: Duration = Duration::from_secs(1);

/// Direct HTTP provider selection used by `ChatTarget::Direct` and `agent::chat::turn`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DirectSelection {
    pub backend: DirectBackend,
    pub model: ModelId,
    #[serde(default)]
    pub effort: Option<String>,
}

/// The execution target chosen for a Chat session (`T-ILLEGAL-STATES`).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "engine", rename_all = "camelCase")]
pub enum ChatTarget {
    /// Direct HTTP API provider (Codex OAuth, OpenAI, Anthropic, Gemini, or custom HTTP).
    Direct {
        backend: DirectBackend,
        model: ModelId,
        #[serde(default)]
        effort: Option<String>,
    },
    /// Unified ACP agentic CLI backend (built-in preset, Registry-installed, or custom).
    Acp {
        agent: AgentId,
        #[serde(default)]
        config: Vec<ConfigSelection>,
    },
    /// Retired `claude-agent` Chat target kept readable for historical sessions; cannot run new turns.
    LegacyClaudeAgent {
        model: ModelId,
        #[serde(default)]
        effort: Option<String>,
    },
}

impl ChatTarget {
    pub fn direct(selection: DirectSelection) -> Self {
        Self::Direct {
            backend: selection.backend,
            model: selection.model,
            effort: selection.effort,
        }
    }

    pub fn acp(agent: AgentId, config: Vec<ConfigSelection>) -> Self {
        Self::Acp { agent, config }
    }

    pub fn is_legacy_claude_agent(&self) -> bool {
        matches!(self, Self::LegacyClaudeAgent { .. })
    }

    pub fn acp_agent(&self) -> Option<&AgentId> {
        match self {
            Self::Acp { agent, .. } => Some(agent),
            _ => None,
        }
    }

    pub fn engine_label(&self) -> String {
        match self {
            Self::Direct { backend, model, .. } => format!("Direct ({backend} / {model})"),
            Self::Acp { agent, .. } => format!("ACP ({agent})"),
            Self::LegacyClaudeAgent { model, .. } => format!("Legacy Claude Agent ({model})"),
        }
    }
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum SendError {
    #[error("Choose a Chat agent or model before sending a Chat message.")]
    NoModel,
    #[error("Workspace trust is required before launching {}.", .0.agent_name)]
    TrustRequired(Box<TrustRequirement>),
    #[error("{0}")]
    LegacyRetired(String),
    #[error("{0}")]
    Failed(String),
}

impl From<String> for SendError {
    fn from(message: String) -> Self {
        Self::Failed(message)
    }
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum MoveError {
    #[error("Not an available Chat worktree: {0}")]
    Invalid(String),
    #[error("Stop the running turn first")]
    Busy,
    #[error("{0}")]
    Failed(String),
}

/// Whether an accepted quit could read active ownership and deliver Stop to those turns.
#[derive(Debug)]
pub enum StopRunningError {
    Unreadable(String),
    Turns(Vec<String>),
}

/// The state of the latest accepted turn.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Outcome {
    Running,
    Completed,
    Failed,
    Stopped,
    Interrupted,
}

/// The workspace view last used in this session, independent of its turn activity.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SessionView {
    #[default]
    Conversation,
    Graph,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
struct Activity {
    outcome: Outcome,
    unseen: bool,
}

/// Edge deserialization wire representation for `Session` (`T-PARSE-AT-EDGE`).
///
/// Decodes legacy `model` / `selection` fields as raw strings bypassing `Backend::from_str`
/// so historical sessions pointing at `"antigravity"` or `"claude-agent"` always open cleanly.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SessionWire {
    id: SessionId,
    title: String,
    #[serde(with = "crate::platform::path_serde")]
    worktree: PathBuf,
    #[serde(default)]
    repository: Option<repository::Identity>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    target: Option<ChatTarget>,
    #[serde(default, alias = "selection", skip_serializing)]
    model: Option<LegacyModelWire>,
    #[serde(default)]
    view: SessionView,
    created_at: Timestamp,
    updated_at: Timestamp,
    #[serde(default)]
    activity: Option<Activity>,
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "crate::platform::path_serde::option"
    )]
    moved_from: Option<PathBuf>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    acp_binding: Option<AcpBinding>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct LegacyModelWire {
    backend: String,
    model: String,
    #[serde(default)]
    effort: Option<String>,
}

impl LegacyModelWire {
    fn migrate(self) -> Option<ChatTarget> {
        let backend = self.backend.trim();
        let effort = self
            .effort
            .map(|value| value.trim().to_owned())
            .filter(|value| !value.is_empty());
        if backend == "antigravity" {
            let mut config = Vec::new();
            let model = self.model.trim();
            if !model.is_empty() {
                config.push(ConfigSelection {
                    id: "model".into(),
                    value: model.to_owned(),
                    category: Some(ConfigCategory::Model),
                });
            }
            if let Some(level) = effort {
                config.push(ConfigSelection {
                    id: "thought_level".into(),
                    value: level,
                    category: Some(ConfigCategory::ThoughtLevel),
                });
            }
            return Some(ChatTarget::Acp {
                agent: "antigravity"
                    .parse()
                    .expect("antigravity is a valid AgentId"),
                config,
            });
        }
        if backend == "claude-agent" {
            let model: ModelId = self.model.parse().ok()?;
            return Some(ChatTarget::LegacyClaudeAgent { model, effort });
        }
        if let Some(provider) = HttpProvider::named(backend) {
            let model: ModelId = self.model.parse().ok()?;
            return Some(ChatTarget::Direct {
                backend: DirectBackend::Builtin { provider },
                model,
                effort,
            });
        }
        if let Ok(name) = backend.parse::<CustomBackendName>() {
            let model: ModelId = self.model.parse().ok()?;
            return Some(ChatTarget::Direct {
                backend: DirectBackend::Custom { name },
                model,
                effort,
            });
        }
        None
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(from = "SessionWire", into = "SessionWire")]
pub struct Session {
    pub id: SessionId,
    pub title: String,
    pub worktree: PathBuf,
    pub repository: Option<repository::Identity>,
    pub target: Option<ChatTarget>,
    pub view: SessionView,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
    activity: Option<Activity>,
    pub(crate) moved_from: Option<PathBuf>,
    pub acp_binding: Option<AcpBinding>,
    /// Live ownership of a turn, read separately from the saved session.
    pub running: bool,
    /// Derived live from `running && has_pending_permission`; never persisted in `Activity`.
    pub waiting_permission: bool,
    /// Live ACP `configOptions` snapshot while a turn is running.
    pub live_config_options: Vec<ConfigOption>,
    /// Live Verify MCP bridge state while an ACP turn is running.
    pub verify_bridge_state: VerifyBridgeState,
    /// True when this historical session was saved on a legacy engine (`claude-agent` or legacy
    /// `antigravity`) so the UI and handoff surface the transition explicitly (`C-NO-SILENT-REGRESSION`).
    pub legacy_migration_notice: bool,
    pub unavailable: Option<String>,
}

impl From<SessionWire> for Session {
    fn from(wire: SessionWire) -> Self {
        let migrated_from_legacy_antigravity = wire.target.is_none()
            && wire
                .model
                .as_ref()
                .is_some_and(|legacy| legacy.backend.trim() == "antigravity");
        let target = wire
            .target
            .or_else(|| wire.model.and_then(LegacyModelWire::migrate));
        let legacy_migration_notice = target
            .as_ref()
            .is_some_and(ChatTarget::is_legacy_claude_agent)
            || migrated_from_legacy_antigravity;
        Self {
            id: wire.id,
            title: wire.title,
            worktree: wire.worktree,
            repository: wire.repository,
            target,
            view: wire.view,
            created_at: wire.created_at,
            updated_at: wire.updated_at,
            activity: wire.activity,
            moved_from: wire.moved_from,
            acp_binding: wire.acp_binding,
            running: false,
            waiting_permission: false,
            live_config_options: Vec::new(),
            verify_bridge_state: VerifyBridgeState::NotConnected,
            legacy_migration_notice,
            unavailable: None,
        }
    }
}

impl From<Session> for SessionWire {
    fn from(session: Session) -> Self {
        Self {
            id: session.id,
            title: session.title,
            worktree: session.worktree,
            repository: session.repository,
            target: session.target,
            model: None,
            view: session.view,
            created_at: session.created_at,
            updated_at: session.updated_at,
            activity: session.activity,
            moved_from: session.moved_from,
            acp_binding: session.acp_binding,
        }
    }
}

impl Session {
    /// Stable across branch switches or removal of the saved checkout.
    pub fn branch(&self) -> Place {
        Place::for_checkout(
            &self.worktree,
            self.repository
                .as_ref()
                .unwrap_or(&repository::Identity::default()),
        )
    }

    pub fn label(&self) -> crate::monitor::live::Label {
        crate::monitor::live::Label::for_identity(
            &self.worktree,
            &self.repository.clone().unwrap_or_default(),
        )
    }

    pub fn outcome(&self) -> Option<Outcome> {
        self.activity.map(|activity| activity.outcome)
    }

    pub fn unseen(&self) -> bool {
        self.activity.is_some_and(|activity| activity.unseen)
    }

    pub(crate) fn needs_activity(&self) -> bool {
        self.activity.is_none() || (self.outcome() == Some(Outcome::Running) && !self.running)
    }

    pub(crate) fn refresh_activity(&mut self, outcome: Option<Outcome>) {
        let owner_gone = self.outcome() == Some(Outcome::Running) && !self.running;
        if !self.needs_activity() {
            return;
        }
        // Do not present legacy history as new. If an owner died after flushing its
        // answer and before updating the catalog, prefer that answer over Interrupted.
        self.activity = if owner_gone {
            Some(Activity {
                outcome: outcome.unwrap_or(Outcome::Interrupted),
                unseen: true,
            })
        } else {
            outcome.map(|outcome| Activity {
                outcome: if self.running {
                    Outcome::Running
                } else {
                    outcome
                },
                unseen: false,
            })
        };
    }

    pub(crate) fn settle_from(&mut self, turn: &Self) {
        self.activity = turn.activity;
        self.updated_at = self.updated_at.max(turn.updated_at);
    }

    pub(crate) fn mark_seen(&mut self) -> bool {
        self.activity
            .as_mut()
            .is_some_and(|activity| std::mem::take(&mut activity.unseen))
    }

    fn matches(&self, words: &[String]) -> bool {
        if words.is_empty() {
            return true;
        }
        let label = self.label();
        let fields = [
            self.title.to_lowercase(),
            label.project.to_lowercase(),
            label.branch.to_lowercase(),
        ];
        words
            .iter()
            .all(|word| fields.iter().any(|field| field.contains(word)))
    }

    fn check_checkout(&self) -> Result<(), String> {
        if !self.worktree.is_dir() {
            return Err("This Chat's worktree no longer exists.".into());
        }
        verify_checkout(&self.worktree, self.repository.as_ref())
    }

    fn with_checkout(mut self) -> Self {
        self.unavailable = self.check_checkout().err();
        self.legacy_migration_notice = self.legacy_migration_notice
            || self
                .target
                .as_ref()
                .is_some_and(ChatTarget::is_legacy_claude_agent);
        self
    }
}

async fn checkout_for(
    worktree: &Path,
    expected: Option<&Place>,
) -> Result<(PathBuf, repository::Identity), String> {
    let worktree =
        crate::workspace::canonical_target(worktree).map_err(|error| error.to_string())?;
    let expected = expected.cloned();
    crate::task::blocking(move || {
        let identity = repository::identify(&worktree);
        if expected.is_some_and(|expected| expected != Place::for_checkout(&worktree, &identity)) {
            return Err("This branch is no longer checked out in the worktree.".into());
        }
        Ok((worktree, identity))
    })
    .await?
}

fn verify_checkout(worktree: &Path, saved: Option<&repository::Identity>) -> Result<(), String> {
    checkout_refusal(saved, &repository::identify(worktree)).map_or(Ok(()), Err)
}

fn checkout_refusal(
    saved: Option<&repository::Identity>,
    current: &repository::Identity,
) -> Option<String> {
    match saved {
        Some(saved) if saved == current => None,
        Some(saved) => Some(match &saved.branch {
            Some(branch) => format!("Check out {branch} in this worktree to continue this Chat."),
            None => "Restore this Chat's original checkout to continue.".into(),
        }),
        None if *current == repository::Identity::default() => None,
        None => {
            Some("This Chat was saved without a branch. Start a new Chat for this checkout.".into())
        }
    }
}

#[derive(Debug, Clone)]
pub struct View {
    pub session: Session,
    pub running: bool,
    pub waiting_permission: bool,
    pub turns: Vec<Turn>,
    pub live_config_options: Vec<ConfigOption>,
    pub verify_bridge_state: VerifyBridgeState,
}

#[derive(Clone)]
pub struct Chat {
    state: PathBuf,
    executable: Option<PathBuf>,
    active: Arc<ActiveTurns>,
    pool: Arc<Pool>,
    permission_notify: Arc<Notify>,
}

#[derive(Default)]
struct ActiveTurns {
    state: Mutex<Turns>,
    settled: Notify,
}

#[derive(Default)]
struct Turns {
    closed: bool,
    turns: BTreeMap<SessionId, ActiveEntry>,
}

#[derive(Clone)]
struct ActiveEntry {
    cancel: CancellationToken,
    owner_token: Option<String>,
    worktree: Option<PathBuf>,
}

/// Resolved execution engine for a single accepted Chat turn (`T-ILLEGAL-STATES`).
enum ChatEngine {
    Direct {
        selection: DirectSelection,
        engine_label: String,
        previous_engine_label: Option<String>,
        prior_turns_count: usize,
    },
    Acp {
        config: Vec<ConfigSelection>,
        launch: Box<VerifiedAcpLaunch>,
        previous_engine_label: Option<String>,
        prior_turns: Vec<Turn>,
        last_outcome: Option<Outcome>,
    },
}

impl Chat {
    pub fn new(state: PathBuf, executable: PathBuf) -> Self {
        Self {
            state,
            executable: Some(executable),
            active: Arc::default(),
            pool: Arc::new(Pool::default()),
            permission_notify: Arc::new(Notify::new()),
        }
    }

    /// Desktop discovers the packaged CLI when a session needs it, not at app startup.
    pub fn discover(state: PathBuf) -> Self {
        Self {
            state,
            executable: None,
            active: Arc::default(),
            pool: Arc::new(Pool::default()),
            permission_notify: Arc::new(Notify::new()),
        }
    }

    fn context(&self, worktree: &Path) -> Result<runs::Context, String> {
        match &self.executable {
            Some(executable) => runs::Context::new(worktree, Some(&self.state), executable),
            None => runs::Context::discover(worktree, Some(&self.state)),
        }
    }

    /// Stop accepting work, commit durable Stop in SQLite before cancelling owned turns, wait for
    /// their saved outcomes and ownership release, and drain all warm ACP children.
    /// Verify Runs already admitted remain independent of Chat.
    pub fn close(&self) -> impl std::future::Future<Output = ()> + use<> {
        let entries: Vec<(SessionId, ActiveEntry)> = {
            let mut active = self.active.state.lock().expect("Chat active turns lock");
            active.closed = true;
            active
                .turns
                .iter()
                .map(|(id, entry)| (id.clone(), entry.clone()))
                .collect()
        };
        let state = self.state.clone();
        let active = self.active.clone();
        let pool = self.pool.clone();
        let permission_notify = self.permission_notify.clone();
        async move {
            // Uniform Stop path (`C-ONE-TURN-OWNERSHIP`): commit SQLite stop_requested +
            // permission cancellation FIRST, and only fire the in-memory CancellationToken for
            // turns whose Stop transaction succeeded (or that have no SQLite active turn yet).
            let mut stopped_ids = std::collections::BTreeSet::new();
            for (id, entry) in &entries {
                if let (Some(token), Some(worktree)) = (&entry.owner_token, &entry.worktree) {
                    let mut committed = false;
                    for attempt in 0..5 {
                        if let Ok(receipts) = Receipts::open(&state, worktree).await {
                            let stopped = receipts.stop_chat_by_token(id, token).await;
                            let closed = receipts.close().await;
                            if stopped.and(closed).is_ok() {
                                committed = true;
                                break;
                            }
                        }
                        if attempt < 4 {
                            tokio::time::sleep(Duration::from_millis(20)).await;
                        }
                    }
                    if committed {
                        stopped_ids.insert(id.clone());
                        entry.cancel.cancel();
                    }
                } else {
                    stopped_ids.insert(id.clone());
                    entry.cancel.cancel();
                }
            }
            permission_notify.notify_waiters();
            loop {
                let settled = active.settled.notified();
                tokio::pin!(settled);
                settled.as_mut().enable();
                if active
                    .state
                    .lock()
                    .expect("Chat active turns lock")
                    .turns
                    .keys()
                    .all(|id| !stopped_ids.contains(id))
                {
                    break;
                }
                settled.await;
            }
            // Terminate all warm ACP children and clear their warm_owner references.
            let drained = pool.drain_all();
            let current_owner = WarmOwner::current().ok();
            for child in drained {
                let session_id = child.session_id.clone();
                let generation = child.generation;
                let _ = child.terminate().await;
                if let Some(warm_owner) = current_owner
                    && let Ok(session) = store::chat::load(&state, &session_id).await
                    && let Ok(receipts) = Receipts::open(&state, &session.worktree).await
                {
                    let _ = receipts
                        .clear_acp_warm_owner(&session_id, generation, warm_owner)
                        .await;
                    let _ = receipts.close().await;
                }
            }
        }
    }

    /// Reconcile exact turn ownership after a process dies without a final change hint.
    /// Returns only sessions this observer actually reconciled.
    pub async fn reconcile_interrupted(&self) -> Result<Vec<SessionId>, String> {
        let mut changed = Vec::new();
        for (session, owner) in store::chat::active_turns(&self.state, false).await? {
            let receipts = Receipts::open(&self.state, &session.worktree).await?;
            let settled = receipts
                .finish_chat(&session.id, &owner, Some(&session), None)
                .await;
            let closed = receipts.close().await;
            if settled.and_then(|settled| closed.map(|()| settled))? {
                changed.push(session.id);
            }
        }
        let publisher = crate::changes::Publisher::new(&self.state);
        for session in &changed {
            publisher.notify(crate::changes::Change::SessionInvalidated(session.clone()));
        }
        Ok(changed)
    }

    /// The starting target for a new session: the worktree's last available choice, or the first
    /// ready ACP Chat agent, or the first available direct HTTP provider model.
    pub async fn default_target(&self, worktree: &Path) -> Result<Option<ChatTarget>, String> {
        self.default_target_for(worktree, None).await
    }

    pub async fn default_target_for(
        &self,
        worktree: &Path,
        branch: Option<&Place>,
    ) -> Result<Option<ChatTarget>, String> {
        let (worktree, identity) = checkout_for(worktree, branch).await?;
        self.starting_target(&worktree, &identity).await
    }

    async fn starting_target(
        &self,
        worktree: &Path,
        identity: &repository::Identity,
    ) -> Result<Option<ChatTarget>, String> {
        if let Some(target) = self.last_target(worktree, identity).await?
            && !target.is_legacy_claude_agent()
            && let Ok(resolved) = self.resolve_target(worktree, target, None).await
        {
            return Ok(Some(resolved));
        }

        // Prefer ready ACP Chat agents in catalog order.
        if let Ok(entries) = catalog::list_catalog(Some(&self.state), Some(worktree)) {
            let probes = futures_util::future::join_all(entries.iter().map(|entry| {
                catalog::probe_agent(&entry.id, Some(&self.state), Some(worktree), false)
            }))
            .await;
            for (entry, availability) in entries.into_iter().zip(probes) {
                if let AgentAvailability::Ready { config_options, .. } = availability {
                    let config = default_config_selections(&config_options);
                    return Ok(Some(ChatTarget::Acp {
                        agent: entry.id,
                        config,
                    }));
                }
            }
        }

        // Fall back to direct HTTP providers (Codex, OpenAI, Anthropic, Gemini + HTTP custom).
        let providers = providers::list(Some(&self.state), Some(worktree))?;
        let mut direct_backends: Vec<DirectBackend> = HttpProvider::ALL
            .into_iter()
            .map(|provider| DirectBackend::Builtin { provider })
            .collect();
        for (name, view) in providers.custom {
            if matches!(
                view,
                providers::CustomView::Alias {
                    provider: Provider::ClaudeAgent
                }
            ) {
                continue;
            }
            if let Ok(custom) = name.as_str().parse::<CustomBackendName>() {
                direct_backends.push(DirectBackend::Custom { name: custom });
            }
        }
        let listings = futures_util::future::join_all(direct_backends.iter().map(|backend| {
            llm::models::list(backend.to_backend(), Some(&self.state), Some(worktree))
        }))
        .await;
        Ok(direct_backends
            .into_iter()
            .zip(listings)
            .find_map(|(backend, listing)| {
                let listing = listing.ok()?;
                listing
                    .models
                    .into_iter()
                    .next()
                    .map(|model| ChatTarget::Direct {
                        backend,
                        model: model.slug,
                        effort: model.default_effort,
                    })
            }))
    }

    async fn last_target(
        &self,
        workspace: &Path,
        checkout: &repository::Identity,
    ) -> Result<Option<ChatTarget>, String> {
        let branch = Place::for_checkout(workspace, checkout);
        Ok(store::chat::for_branch(&self.state, &branch)
            .await?
            .into_iter()
            .find_map(|session| {
                session
                    .target
                    .filter(|target| !target.is_legacy_claude_agent())
            }))
    }

    /// Persist an empty session, with no target unless one is explicitly selected.
    pub async fn create(
        &self,
        worktree: &Path,
        selection: Option<ChatTarget>,
    ) -> Result<Session, String> {
        self.create_for_branch(worktree, None, selection).await
    }

    /// Create only on the checkout the person selected, not a branch switched in meanwhile.
    pub async fn create_for_branch(
        &self,
        workspace: &Path,
        branch: Option<&Place>,
        selection: Option<ChatTarget>,
    ) -> Result<Session, String> {
        let (workspace, repository) = checkout_for(workspace, branch).await?;
        let worktree = workspace.as_path();
        crate::config::read_workspace_config(worktree).map_err(|error| error.to_string())?;
        let target = if let Some(selection) = selection {
            let previous = self.last_target(worktree, &repository).await?;
            Some(
                self.resolve_target(worktree, selection, previous.as_ref())
                    .await?,
            )
        } else {
            None
        };
        let now = crate::broker::now();
        let legacy_migration_notice = target
            .as_ref()
            .is_some_and(ChatTarget::is_legacy_claude_agent);
        let session = Session {
            id: format!("chat-{}", agent::uuid()?).parse()?,
            title: "New chat".into(),
            worktree: worktree.into(),
            repository: Some(repository),
            target,
            view: SessionView::default(),
            created_at: now,
            updated_at: now,
            activity: None,
            moved_from: None,
            acp_binding: None,
            running: false,
            waiting_permission: false,
            live_config_options: Vec::new(),
            verify_bridge_state: VerifyBridgeState::NotConnected,
            legacy_migration_notice,
            unavailable: None,
        };
        let session = crate::task::blocking(move || session.with_checkout()).await?;
        if let Some(reason) = &session.unavailable {
            return Err(reason.clone());
        }
        let receipts = Receipts::open(&self.state, worktree).await?;
        let result = receipts.create_chat(&session).await;
        let closed = receipts.close().await;
        result.and(closed)?;
        Ok(session)
    }

    /// Discover the live choices used by the Chat branch picker, independently of Verify search.
    pub async fn branches(&self, launch: Option<&Path>) -> Result<BranchListing, String> {
        let runs = store::read_catalog(&self.state).await?;
        let sessions = self.list(None, None).await?;
        let launch = launch.map(Path::to_owned);
        crate::task::blocking(move || {
            let mut branches =
                live::branches(&runs, launch.as_deref(), &mut live::Discovery::default()).branches;
            for session in sessions
                .iter()
                .filter(|session| session.unavailable.is_none())
            {
                let place = session.branch();
                if !branches.iter().any(|branch| branch.place == place) {
                    let label = session.label();
                    branches.push(Branch {
                        place,
                        project: label.project,
                        name: label.branch,
                        workspace: session.worktree.clone(),
                        runs: Vec::new(),
                    });
                }
            }
            BranchListing { branches, sessions }
        })
        .await
    }

    /// Rebind the saved conversation to a live choice, excluding active turns in any process.
    /// Evicts any warm ACP child owned by this process for the moved session.
    pub async fn set_worktree(
        &self,
        id: &SessionId,
        worktree: &Path,
        launch: Option<&Path>,
    ) -> Result<Session, MoveError> {
        let path = crate::platform::path_text(worktree);
        let invalid = || MoveError::Invalid(path.clone());
        let branch = self
            .branches(launch)
            .await
            .map_err(MoveError::Failed)?
            .branches
            .into_iter()
            .find(|branch| crate::platform::path_text(&branch.workspace) == path)
            .ok_or_else(invalid)?;
        let (workspace, repository) = checkout_for(&branch.workspace, Some(&branch.place))
            .await
            .map_err(|_| invalid())?;
        let receipts = Receipts::open(&self.state, &workspace)
            .await
            .map_err(MoveError::Failed)?;
        let result = receipts
            .set_chat_worktree(&self.state, id, workspace, repository)
            .await;
        let closed = receipts.close().await;
        let (saved, changed) = result
            .and_then(|saved| closed.map(|()| saved))
            .map_err(MoveError::Failed)?
            .ok_or(MoveError::Busy)?;
        if changed {
            if let Some(child) = self.pool.take_any(id) {
                let _ = child.terminate().await;
            }
            crate::changes::Publisher::new(&self.state)
                .notify(crate::changes::Change::SessionInvalidated(id.clone()));
        }
        crate::task::blocking(move || saved.with_checkout())
            .await
            .map_err(MoveError::Failed)
    }

    /// Keep the catalog's activity order while matching every word across its displayed labels.
    pub async fn list(
        &self,
        worktree: Option<&Path>,
        query: Option<&str>,
    ) -> Result<Vec<Session>, String> {
        let words = query
            .unwrap_or_default()
            .split_whitespace()
            .map(str::to_lowercase)
            .collect::<Vec<_>>();
        let sessions = store::chat::list(&self.state, worktree).await?;
        crate::task::blocking(move || {
            let mut identities = BTreeMap::new();
            sessions
                .into_iter()
                .filter(|session| session.matches(&words))
                .map(|mut session| {
                    let checkout =
                        identities
                            .entry(session.worktree.clone())
                            .or_insert_with(|| {
                                session
                                    .worktree
                                    .is_dir()
                                    .then(|| repository::identify(&session.worktree))
                            });
                    session.unavailable = match checkout {
                        Some(current) => checkout_refusal(session.repository.as_ref(), current),
                        None => Some("This Chat's worktree no longer exists.".into()),
                    };
                    session.legacy_migration_notice = session.legacy_migration_notice
                        || session
                            .target
                            .as_ref()
                            .is_some_and(ChatTarget::is_legacy_claude_agent);
                    session
                })
                .collect()
        })
        .await
    }

    /// Find a conversation by its former checkout, including a workspace within the worktree.
    pub async fn latest_for_unavailable_checkout(
        &self,
        workspace: &Path,
    ) -> Result<Option<Session>, String> {
        let workspace = workspace.to_path_buf();
        let workspace = crate::task::blocking(move || {
            let workspace = crate::workspace::canonical_target(&workspace)
                .map_err(|error| error.to_string())?;
            Ok::<_, String>(
                crate::repository::identify(&workspace)
                    .worktree_path
                    .is_none()
                    .then_some(workspace),
            )
        })
        .await??;
        let Some(workspace) = workspace else {
            return Ok(None);
        };
        let sessions = store::chat::list(&self.state, None).await?;
        crate::task::blocking(move || {
            sessions
                .into_iter()
                .filter(|session| {
                    let recorded = session
                        .repository
                        .as_ref()
                        .and_then(|repo| repo.worktree_path.as_deref())
                        .unwrap_or(&session.worktree);
                    crate::platform::paths::relative_beneath(&workspace, recorded).is_some()
                        || crate::platform::paths_equal(&workspace, recorded)
                })
                .map(Session::with_checkout)
                .find(|session| session.unavailable.is_some())
        })
        .await
    }

    /// The most recently active saved session on this branch, even if its checkout is gone.
    pub async fn latest_for_branch(&self, branch: &Place) -> Result<Option<Session>, String> {
        let mut sessions = store::chat::for_branch(&self.state, branch)
            .await?
            .into_iter();
        crate::task::blocking(move || sessions.next().map(Session::with_checkout)).await
    }

    /// Prefer the newest usable session for branch navigation. If the checkout moved and none
    /// can work on it, keep the newest saved conversation reachable with its diagnostic.
    pub async fn latest_for_navigation(&self, branch: &Place) -> Result<Option<Session>, String> {
        let sessions = store::chat::for_branch(&self.state, branch).await?;
        crate::task::blocking(move || {
            let mut fallback = None;
            for session in sessions {
                let session = session.with_checkout();
                if session.unavailable.is_none() {
                    return Some(session);
                }
                if fallback.is_none() {
                    fallback = Some(session);
                }
            }
            fallback
        })
        .await
    }

    /// The latest session whose exact checkout can be worked on now.
    pub async fn latest_available_for_branch(
        &self,
        branch: &Place,
    ) -> Result<Option<Session>, String> {
        let sessions = store::chat::for_branch(&self.state, branch).await?;
        crate::task::blocking(move || {
            sessions
                .into_iter()
                .find(|session| session.check_checkout().is_ok())
        })
        .await
    }

    /// The oldest usable checkout anchors Current when there is no Verify workspace.
    /// Activity in a newer session must not redirect scans, edits or tools.
    pub async fn current_worktree(&self, branch: &Place) -> Result<Option<PathBuf>, String> {
        let sessions = store::chat::for_branch(&self.state, branch).await?;
        crate::task::blocking(move || {
            sessions
                .into_iter()
                .filter(|session| session.check_checkout().is_ok())
                .min_by(|a, b| (a.created_at, &a.id).cmp(&(b.created_at, &b.id)))
                .map(|session| session.worktree)
        })
        .await
    }

    /// Save a view without changing the session's activity order or requiring a live checkout.
    pub async fn set_view(&self, id: &SessionId, view: SessionView) -> Result<Session, String> {
        let session = store::chat::load(&self.state, id).await?;
        let receipts = Receipts::open(&self.state, &session.worktree).await?;
        let result = receipts.set_chat_view(&self.state, id, view).await;
        let closed = receipts.close().await;
        let (saved, changed) = result.and_then(|saved| closed.map(|()| saved))?;
        if changed {
            crate::changes::Publisher::new(&self.state)
                .notify(crate::changes::Change::SessionInvalidated(id.clone()));
        }
        crate::task::blocking(move || saved.with_checkout()).await
    }

    /// Acknowledge the latest settled outcome without changing the session's activity order.
    /// A turn still running will become unseen when it settles, even if this is called now.
    pub async fn mark_seen(&self, id: &SessionId) -> Result<(), String> {
        let session = store::chat::load(&self.state, id).await?;
        if !session.unseen() {
            return Ok(());
        }
        let receipts = Receipts::open(&self.state, &session.worktree).await?;
        let result = receipts.mark_chat_seen(&self.state, id).await;
        let closed = receipts.close().await;
        let changed = result.and_then(|changed| closed.map(|()| changed))?;
        if changed {
            crate::changes::Publisher::new(&self.state)
                .notify(crate::changes::Change::SessionInvalidated(id.clone()));
        }
        Ok(())
    }

    /// Chat records are append-only. Readers can detect a missed hint without parsing a
    /// transcript, with `None` covering a session that has not yet accepted a turn.
    pub fn transcript_size(&self, id: &SessionId) -> Option<u64> {
        crate::platform::open_regular(&path(&self.state, id))
            .and_then(|file| file.metadata())
            .ok()
            .map(|file| file.len())
    }

    /// Lightweight SQLite watermark across `(chat_sessions, chat_active_turns,
    /// chat_pending_permissions)` tracked by Desktop `watch.rs` alongside `transcript_size`.
    pub async fn watermark(&self, id: &SessionId) -> Result<Option<ChatWatermark>, String> {
        store::chat::watermark(&self.state, id).await
    }

    /// Evict any local warm ACP child whose committed `acp_binding.generation` has advanced
    /// (called on `Change::SessionInvalidated` so another process's move/switch terminates stale
    /// local children promptly).
    pub async fn evict_warm_if_stale(&self, id: &SessionId) {
        let committed_gen = store::chat::load(&self.state, id)
            .await
            .ok()
            .and_then(|session| session.acp_binding)
            .map(|binding| binding.generation);
        if let Some(child) = self.pool.take_if_generation_stale(id, committed_gen) {
            let _ = child.terminate().await;
        }
    }

    /// The model/agent's streamed text, rich ACP blocks, live permissions, and Run cards.
    pub async fn open(&self, id: &SessionId) -> Result<View, String> {
        let session = store::chat::load(&self.state, id).await?;
        let session = crate::task::blocking(move || session.with_checkout()).await?;
        let running = session.running;
        let waiting_permission = session.waiting_permission;
        let live_config_options = session.live_config_options.clone();
        let verify_bridge_state = session.verify_bridge_state;
        let live_permissions = store::chat::list_permissions(&self.state, id).await?;
        let conversation = Conversation::load(&path(&self.state, id))?;
        let mut turns = transcript::turns(conversation.as_ref(), running, &live_permissions);
        transcript::update_runs(&self.state, &session.worktree, id, &mut turns).await;
        Ok(View {
            session,
            running,
            waiting_permission,
            turns,
            live_config_options,
            verify_bridge_state,
        })
    }

    /// Save a Chat target for the next turn without adding a message. An accepted turn keeps
    /// the target it started with, even if the user changes the picker while it streams.
    pub async fn set_target(
        &self,
        id: &SessionId,
        selection: ChatTarget,
    ) -> Result<Session, String> {
        let session = self.available(id).await?;
        let selection = self
            .resolve_target(&session.worktree, selection, session.target.as_ref())
            .await?;
        let receipts = Receipts::open(&self.state, &session.worktree).await?;
        let saved = receipts.set_chat_target(&self.state, id, selection).await;
        let closed = receipts.close().await;
        let (saved, agent_changed) = saved.and_then(|result| closed.map(|()| result))?;
        if agent_changed && let Some(child) = self.pool.take_any(id) {
            let _ = child.terminate().await;
        }
        crate::changes::Publisher::new(&self.state)
            .notify(crate::changes::Change::SessionInvalidated(id.clone()));
        crate::task::blocking(move || saved.with_checkout()).await
    }

    /// Respond to a pending ACP permission card (`PENDING -> SELECTED` in one `IMMEDIATE`
    /// SQLite transaction) and wake any local owning turn immediately.
    pub async fn respond_permission(
        &self,
        id: &SessionId,
        permission_id: &str,
        option_id: &str,
    ) -> Result<bool, String> {
        let session = store::chat::load(&self.state, id).await?;
        let receipts = Receipts::open(&self.state, &session.worktree).await?;
        let updated = receipts
            .respond_chat_permission(id, permission_id, option_id)
            .await;
        let closed = receipts.close().await;
        let updated = updated.and_then(|changed| closed.map(|()| changed))?;
        if updated {
            self.permission_notify.notify_waiters();
            crate::changes::Publisher::new(&self.state)
                .notify(crate::changes::Change::SessionInvalidated(id.clone()));
        }
        Ok(updated)
    }

    /// Record explicit machine-local Workspace Trust for an ACP agent on a worktree/repo scope.
    pub async fn trust_workspace(
        &self,
        worktree: &Path,
        agent_id: &AgentId,
    ) -> Result<TrustRecord, String> {
        let worktree =
            crate::workspace::canonical_target(worktree).map_err(|error| error.to_string())?;
        let state = self.state.clone();
        let agent_id = agent_id.clone();
        let (spec, scope, fingerprint) = crate::task::blocking(move || {
            let identity = repository::identify(&worktree);
            let spec = catalog::resolve_spec(&agent_id, Some(&state), Some(&worktree))?;
            let resolved = acp::trust::resolve_executable(
                &spec.program,
                Some(&worktree),
                spec.reject_worktree_executable,
            )?;
            let fingerprint = acp::trust::LauncherFingerprint::compute(&resolved, &spec.args)?;
            let scope = TrustScope::for_worktree(&worktree, &identity)?;
            Ok::<_, String>((spec, scope, fingerprint))
        })
        .await??;
        acp::trust::grant_trust(Some(&self.state), None, spec.id, scope, fingerprint).await
    }

    /// List all saved machine-local ACP Workspace Trust records.
    pub fn list_trust(&self) -> Result<Vec<TrustRecord>, String> {
        acp::trust::list_trust(Some(&self.state), None)
    }

    /// Revoke a saved Workspace Trust record and terminate any matching warm ACP children.
    pub async fn revoke_trust(
        &self,
        agent_id: &AgentId,
        scope: &TrustScope,
    ) -> Result<bool, String> {
        let removed = acp::trust::revoke_trust(Some(&self.state), None, agent_id, scope).await?;
        if removed {
            let drained = self.pool.drain_all();
            let current_owner = WarmOwner::current().ok();
            for child in drained {
                if &child.agent_id == agent_id {
                    let session_id = child.session_id.clone();
                    let generation = child.generation;
                    let _ = child.terminate().await;
                    if let Some(warm_owner) = current_owner
                        && let Ok(session) = store::chat::load(&self.state, &session_id).await
                        && let Ok(receipts) = Receipts::open(&self.state, &session.worktree).await
                    {
                        let _ = receipts
                            .clear_acp_warm_owner(&session_id, generation, warm_owner)
                            .await;
                        let _ = receipts.close().await;
                    }
                } else {
                    self.pool.park(child);
                }
            }
        }
        Ok(removed)
    }

    async fn available(&self, id: &SessionId) -> Result<Session, String> {
        let session = store::chat::load(&self.state, id).await?;
        let mut session = crate::task::blocking(move || session.with_checkout()).await?;
        match session.unavailable.take() {
            Some(reason) => Err(reason),
            None => Ok(session),
        }
    }

    /// Start a durable turn and return as soon as it has an accepted user message. The recorder
    /// notifies `SessionInvalidated(id)` as text, tool outcomes, permissions and Run references arrive.
    pub async fn send(
        &self,
        id: &SessionId,
        text: String,
        selection: Option<ChatTarget>,
    ) -> Result<(), SendError> {
        if text.trim().is_empty() || text.len() > PROMPT_BYTES {
            return Err(SendError::Failed(format!(
                "Chat messages must have text and be at most {PROMPT_BYTES} bytes."
            )));
        }
        let active = self.claim(id)?;
        let chat = self.clone();
        let id = id.clone();
        let (accepted, acknowledgement) = tokio::sync::oneshot::channel();
        // Once queued, the library owns preparation and cleanup independently of the IPC caller.
        tokio::spawn(async move {
            match chat.prepare(&id, &text, selection, &active.cancel).await {
                Ok(prepared) => {
                    let _ = accepted.send(Ok(()));
                    chat.finish(prepared, text, active).await;
                }
                Err(error) => {
                    drop(active);
                    let _ = accepted.send(Err(error));
                }
            }
        });
        acknowledgement
            .await
            .map_err(|_| SendError::Failed("The queued Chat turn did not start.".into()))?
    }

    async fn prepare(
        &self,
        id: &SessionId,
        text: &str,
        selection: Option<ChatTarget>,
        cancel: &CancellationToken,
    ) -> Result<Prepared, SendError> {
        let saved = store::chat::load(&self.state, id).await?;
        let file = path(&self.state, id);
        let owner = store::chat::Owner::current()?;
        let receipts = Receipts::open(&self.state, &saved.worktree).await?;
        let prepared = async {
            // Claim and read the checkout in one transaction. If a move won the race, this
            // turn starts on the newly saved workspace, never the stale path read above.
            let session = receipts.claim_chat(&self.state, id, &owner).await?;
            self.record_owner(id, owner.token.clone(), session.worktree.clone());
            let prepared = async {
                let mut session = crate::task::blocking(move || session.with_checkout()).await?;
                if let Some(reason) = session.unavailable.take() {
                    return Err(SendError::Failed(reason));
                }
                let previous_target = session.target.clone();
                let context = self.context(&session.worktree)?;
                let explicit_selection = if let Some(selection) = selection {
                    Some(tokio::select! {
                        _ = cancel.cancelled() => return Err(SendError::Failed("Chat stopped before accepting the turn.".into())),
                        resolved = self.resolve_target(context.repo(), selection, previous_target.as_ref()) => resolved?,
                    })
                } else {
                    None
                };
                let effective_target = explicit_selection
                    .clone()
                    .or(previous_target.clone())
                    .ok_or(SendError::NoModel)?;

                // Validate target and enforce Workspace Trust BEFORE recording Kind::Send into JSONL.
                let conversation = Conversation::load(&file)?;
                let send = conversation.as_ref().map(Conversation::sends).unwrap_or(0) + 1;
                let history = conversation
                    .as_ref()
                    .map(Conversation::history)
                    .transpose()?
                    .unwrap_or_default();

                let engine = match &effective_target {
                    ChatTarget::LegacyClaudeAgent { .. } => {
                        return Err(SendError::LegacyRetired(
                            "Legacy claude-agent Chat execution has been retired. Switch this session to Claude ACP or a direct API provider to continue."
                                .into(),
                        ));
                    }
                    ChatTarget::Direct {
                        backend,
                        model,
                        effort,
                    } => {
                        let engine_label = effective_target.engine_label();
                        let previous_label = previous_target
                            .as_ref()
                            .map(ChatTarget::engine_label)
                            .filter(|prev| prev != &engine_label)
                            .or_else(|| {
                                session
                                    .acp_binding
                                    .as_ref()
                                    .map(|b| format!("ACP ({})", b.agent_id))
                            });
                        let prior_turns_count =
                            conversation.as_ref().map(Conversation::sends).unwrap_or(0);
                        ChatEngine::Direct {
                            selection: DirectSelection {
                                backend: backend.clone(),
                                model: model.clone(),
                                effort: effort.clone(),
                            },
                            engine_label,
                            previous_engine_label: previous_label,
                            prior_turns_count,
                        }
                    }
                    ChatTarget::Acp { agent, config } => {
                        let state = self.state.clone();
                        let worktree = session.worktree.clone();
                        let identity = session.repository.clone().unwrap_or_default();
                        let agent_id = agent.clone();
                        let trust_result = crate::task::blocking(move || {
                            acp::engine::verify_launch(&state, &worktree, &identity, &agent_id)
                        })
                        .await
                        .map_err(SendError::Failed)?
                        .map_err(SendError::Failed)?;
                        let launch = match trust_result {
                            Ok(launch) => launch,
                            Err(requirement) => {
                                return Err(SendError::TrustRequired(Box::new(requirement)));
                            }
                        };
                        let prior_turns = transcript::turns(conversation.as_ref(), false, &[]);
                        let last_outcome = conversation.as_ref().and_then(transcript::last_outcome);
                        let previous_engine_label = previous_engine_label(
                            previous_target.as_ref(),
                            session.acp_binding.as_ref(),
                            session.legacy_migration_notice,
                            agent,
                        );
                        ChatEngine::Acp {
                            config: config.clone(),
                            launch: Box::new(launch),
                            previous_engine_label,
                            prior_turns,
                            last_outcome,
                        }
                    }
                };

                if cancel.is_cancelled() {
                    return Err(SendError::Failed(
                        "Chat stopped before accepting the turn.".into(),
                    ));
                }

                if send == 1 {
                    session.title = text
                        .lines()
                        .find_map(|line| {
                            let line = line.trim();
                            (!line.is_empty()).then(|| line.chars().take(TITLE_CHARS).collect())
                        })
                        .expect("nonblank prompt contains a title");
                }
                let mut recorder = Recorder::chat(&self.state, &file, id, send)?;
                recorder.event(Kind::Send(Send {
                    text: Some(text.to_owned()),
                    framing: None,
                    files_changed: None,
                    tools: None,
                }));
                let prompt = rig_core::message::Message::user(text.to_owned());
                recorder.message(1, &prompt, false);
                recorder.check_saved()?;
                session.updated_at = crate::broker::now();
                session.activity = Some(Activity {
                    outcome: Outcome::Running,
                    unseen: false,
                });
                let (_target, acp_binding) = receipts
                    .accept_chat(&session, Some(effective_target))
                    .await?;
                Ok::<_, SendError>((session, recorder, send, history, engine, acp_binding, context))
            }
            .await;
            if prepared.is_err() {
                let _ = receipts.finish_chat(id, &owner, None, None).await;
            }
            prepared
        }
        .await;
        let (session, recorder, send, history, engine, acp_binding, context) = match prepared {
            Ok(prepared) => prepared,
            Err(error) => {
                let _ = receipts.close().await;
                return Err(error);
            }
        };
        Ok(Prepared {
            session,
            send,
            engine,
            acp_binding,
            context,
            receipts,
            owner,
            recorder,
            history,
        })
    }

    async fn finish(&self, prepared: Prepared, text: String, active: Active) {
        let Prepared {
            mut session,
            send,
            engine,
            acp_binding,
            context,
            receipts,
            owner,
            mut recorder,
            history,
        } = prepared;
        let context = context.with_origin(runs::Origin::Chat {
            session_id: session.id.clone(),
            title: session.title.clone(),
            send: Some(send),
        });
        let directory = self.state.join("chat-output");
        let output = crate::workspace::prepare_directory(&directory, &session.worktree)
            .map_err(|error| agent::error::Failure::from(error.to_string()))
            .and_then(|directory| {
                crate::platform::private_tempdir_in("turn-", &directory)
                    .map_err(|error| agent::error::Failure::from(error.to_string()))
            });
        let mut settled_acp: Option<(u64, Option<usize>, Option<WarmOwner>)> = None;
        let mut park_reaper = false;
        let result = match output {
            Ok(output) => {
                let tools = tools::Tools::new(&context, output.path())
                    .guard_checkout(session.repository.as_ref());
                match engine {
                    ChatEngine::Direct {
                        selection,
                        engine_label,
                        previous_engine_label,
                        prior_turns_count,
                    } => {
                        if let Some(prev_label) = previous_engine_label
                            && prior_turns_count > 0
                        {
                            recorder.event(Kind::Handoff(crate::agent::session::HandoffEvent {
                                from: prev_label,
                                to: engine_label.clone(),
                                summary: format!(
                                    "Continued conversation in {engine_label} carrying {prior_turns_count} prior turn(s)."
                                ),
                            }));
                        }
                        let turn = agent::chat_turn(
                            &selection,
                            &context,
                            &tools,
                            agent::ChatPrompt::new(
                                history,
                                text,
                                session.moved_from.as_deref(),
                                &session.worktree,
                            ),
                            &mut recorder,
                            &active.cancel,
                        );
                        let requested = async {
                            let mut check = tokio::time::interval(STOP_CHECK);
                            check.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
                            loop {
                                check.tick().await;
                                if receipts.chat_stop_requested(&session.id, &owner).await
                                    == Ok(true)
                                {
                                    active.cancel.cancel();
                                    break;
                                }
                            }
                        };
                        tokio::pin!(turn);
                        tokio::select! {
                            answer = &mut turn => answer,
                            () = requested => turn.await,
                        }
                    }
                    ChatEngine::Acp {
                        config,
                        launch,
                        previous_engine_label,
                        prior_turns,
                        last_outcome,
                    } => {
                        let identity = session.repository.clone().unwrap_or_default();
                        let outcome = acp::engine::run_turn(
                            AcpTurnInput {
                                session_id: &session.id,
                                worktree: &session.worktree,
                                identity: &identity,
                                moved_from: session.moved_from.as_deref(),
                                send,
                                prior_turns: &prior_turns,
                                last_outcome,
                                previous_engine_label: previous_engine_label.as_deref(),
                                user_text: &text,
                                requested_config: &config,
                                binding: acp_binding,
                                launch: *launch,
                                context: &context,
                                tools: &tools,
                                receipts: &receipts,
                                owner: &owner,
                                pool: &self.pool,
                                permission_notify: &self.permission_notify,
                                turn_cancel: &active.cancel,
                            },
                            &mut recorder,
                        )
                        .await;
                        let completed_send = outcome.result.is_ok().then_some(send);
                        if outcome.warm_owner.is_some() {
                            park_reaper = true;
                        }
                        settled_acp =
                            Some((outcome.generation, completed_send, outcome.warm_owner));
                        outcome.result
                    }
                }
            }
            Err(error) => Err(error),
        };
        let answer = match result {
            Ok(text) => Answer::Completed(text),
            Err(failure) => Answer::Failed {
                code: Some(failure.code),
                message: Some(failure.message),
            },
        };
        session.activity = Some(Activity {
            outcome: transcript::outcome(&answer),
            unseen: true,
        });
        session.updated_at = crate::broker::now().max(session.updated_at);
        recorder.event(Kind::Answer(answer));
        let flushed = recorder.check_saved().is_ok();
        let settled_acp = if flushed {
            settled_acp
        } else {
            settled_acp.map(|(generation, _, warm_owner)| (generation, None, warm_owner))
        };
        // Publish the settled outcome, release ownership, and advance ACP settled_send together.
        let _ = receipts
            .finish_chat(&session.id, &owner, Some(&session), settled_acp)
            .await;
        let _ = receipts.close().await;
        if park_reaper {
            self.schedule_idle_reaper();
        }
        drop(active);
        recorder.notify();
    }

    fn schedule_idle_reaper(&self) {
        let pool = self.pool.clone();
        let state = self.state.clone();
        tokio::spawn(async move {
            tokio::time::sleep(IDLE_TTL + IDLE_REAP_MARGIN).await;
            let expired = pool.take_expired();
            let current_owner = WarmOwner::current().ok();
            for child in expired {
                let session_id = child.session_id.clone();
                let generation = child.generation;
                let _ = child.terminate().await;
                if let Some(warm_owner) = current_owner
                    && let Ok(session) = store::chat::load(&state, &session_id).await
                    && let Ok(receipts) = Receipts::open(&state, &session.worktree).await
                {
                    let _ = receipts
                        .clear_acp_warm_owner(&session_id, generation, warm_owner)
                        .await;
                    let _ = receipts.close().await;
                }
            }
        });
    }

    /// Stop the active turn, even if another app owns it. Accepted Verify Runs are independent.
    /// Always commits the SQLite Stop transaction (`stop_requested = true` + permission
    /// cancellation) BEFORE firing the in-memory CancellationToken.
    pub async fn stop(&self, id: &SessionId) -> Result<bool, String> {
        let local = self
            .active
            .state
            .lock()
            .expect("Chat active turns lock")
            .turns
            .get(id)
            .cloned();
        if let Some(entry) = local {
            if let (Some(token), Some(worktree)) = (&entry.owner_token, &entry.worktree) {
                let receipts = Receipts::open(&self.state, worktree).await?;
                let stopped = receipts.stop_chat_by_token(id, token).await;
                let closed = receipts.close().await;
                let _ = stopped.and_then(|value| closed.map(|()| value))?;
            }
            self.permission_notify.notify_waiters();
            entry.cancel.cancel();
            crate::changes::Publisher::new(&self.state)
                .notify(crate::changes::Change::SessionInvalidated(id.clone()));
            return Ok(true);
        }
        let Some(owner) = store::chat::turn_owner(&self.state, id).await? else {
            return Ok(false);
        };
        let session = store::chat::load(&self.state, id).await?;
        let stopped = self.stop_exact(&session, owner).await?;
        if stopped {
            crate::changes::Publisher::new(&self.state)
                .notify(crate::changes::Change::SessionInvalidated(id.clone()));
        }
        Ok(stopped)
    }

    /// The desktop's quit prompt names every running Chat. Deliver the same Stop to those
    /// turns even when another process owns them, before the desktop window closes.
    pub async fn stop_running(&self) -> Result<(), StopRunningError> {
        let local: Vec<(SessionId, ActiveEntry)> = {
            let active = self.active.state.lock().expect("Chat active turns lock");
            active
                .turns
                .iter()
                .map(|(id, entry)| (id.clone(), entry.clone()))
                .collect()
        };
        let mut failed = Vec::new();
        let mut local_ids = std::collections::BTreeSet::new();
        for (id, entry) in local {
            local_ids.insert(id.clone());
            if let (Some(token), Some(worktree)) = (&entry.owner_token, &entry.worktree) {
                match Receipts::open(&self.state, worktree).await {
                    Ok(receipts) => {
                        let stopped = receipts.stop_chat_by_token(&id, token).await;
                        let closed = receipts.close().await;
                        match stopped.and(closed) {
                            Ok(()) => entry.cancel.cancel(),
                            Err(error) => failed.push(format!("{id}: {error}")),
                        }
                    }
                    Err(error) => failed.push(format!("{id}: {error}")),
                }
            } else {
                entry.cancel.cancel();
            }
        }
        self.permission_notify.notify_waiters();
        let turns = store::chat::active_turns(&self.state, true)
            .await
            .map_err(StopRunningError::Unreadable)?;
        for (session, owner) in turns {
            if owner.in_current_process() && local_ids.contains(&session.id) {
                continue;
            }
            if let Err(error) = self.stop_exact(&session, owner).await {
                failed.push(format!("{}: {error}", session.id));
            }
        }
        if failed.is_empty() {
            Ok(())
        } else {
            Err(StopRunningError::Turns(failed))
        }
    }

    async fn stop_exact(
        &self,
        session: &Session,
        owner: store::chat::Owner,
    ) -> Result<bool, String> {
        let receipts = Receipts::open(&self.state, &session.worktree).await?;
        let stopped = receipts.stop_chat(&session.id, owner).await;
        let closed = receipts.close().await;
        stopped.and_then(|stopped| closed.map(|()| stopped))
    }

    async fn resolve_target(
        &self,
        worktree: &Path,
        selection: ChatTarget,
        previous: Option<&ChatTarget>,
    ) -> Result<ChatTarget, String> {
        match selection {
            ChatTarget::LegacyClaudeAgent { .. } => Err(
                "Legacy claude-agent Chat execution has been retired. Choose an ACP Chat agent or a direct API provider."
                    .into(),
            ),
            ChatTarget::Direct {
                backend,
                model,
                effort,
            } => {
                let prev = match previous {
                    Some(ChatTarget::Direct {
                        backend,
                        model,
                        effort,
                    }) => Some(DirectSelection {
                        backend: backend.clone(),
                        model: model.clone(),
                        effort: effort.clone(),
                    }),
                    _ => None,
                };
                let resolved = self
                    .resolve_direct(
                        worktree,
                        DirectSelection {
                            backend,
                            model,
                            effort,
                        },
                        prev.as_ref(),
                    )
                    .await?;
                Ok(ChatTarget::direct(resolved))
            }
            ChatTarget::Acp { agent, config } => {
                let _spec = catalog::resolve_spec(&agent, Some(&self.state), Some(worktree))?;
                Ok(ChatTarget::Acp { agent, config })
            }
        }
    }

    async fn resolve_direct(
        &self,
        worktree: &Path,
        mut selection: DirectSelection,
        previous: Option<&DirectSelection>,
    ) -> Result<DirectSelection, String> {
        let backend = selection.backend.to_backend();
        let route = llm::route(
            &backend,
            Some(&self.state),
            Some(worktree),
            "for this Chat model",
        )
        .map_err(|failure| failure.message)?;
        if !matches!(route, llm::Route::Http(_)) {
            return Err(format!(
                "{} is not a direct HTTP provider for Chat.",
                selection.backend
            ));
        }
        let listing = llm::models::list(backend, Some(&self.state), Some(worktree)).await?;
        let model = listing
            .models
            .into_iter()
            .find(|model| model.slug == selection.model)
            .ok_or_else(|| {
                format!(
                    "{} does not list model {}.",
                    selection.backend, selection.model
                )
            })?;
        if let Some(effort) = &selection.effort {
            if !model.efforts.contains(effort) {
                let allowed = if model.efforts.is_empty() {
                    "none".to_owned()
                } else {
                    model.efforts.join(", ")
                };
                return Err(format!(
                    "{} model {} does not offer effort {effort:?}. Available efforts: {allowed}.",
                    selection.backend, selection.model
                ));
            }
        } else {
            selection.effort = previous
                .and_then(|previous| previous.effort.as_ref())
                .filter(|effort| model.efforts.contains(effort))
                .cloned()
                .or(model.default_effort);
        }
        Ok(selection)
    }

    fn claim(&self, id: &SessionId) -> Result<Active, String> {
        let mut active = self.active.state.lock().expect("Chat active turns lock");
        if active.closed {
            return Err("Chat is closing.".into());
        }
        match active.turns.entry(id.clone()) {
            Slot::Occupied(_) => Err("This Chat session already has an active turn.".into()),
            Slot::Vacant(entry) => {
                let cancel = CancellationToken::new();
                entry.insert(ActiveEntry {
                    cancel: cancel.clone(),
                    owner_token: None,
                    worktree: None,
                });
                Ok(Active {
                    chat: self.clone(),
                    id: id.clone(),
                    cancel,
                })
            }
        }
    }

    fn record_owner(&self, id: &SessionId, token: String, worktree: PathBuf) {
        if let Some(entry) = self
            .active
            .state
            .lock()
            .expect("Chat active turns lock")
            .turns
            .get_mut(id)
        {
            entry.owner_token = Some(token);
            entry.worktree = Some(worktree);
        }
    }
}

fn default_config_selections(options: &[ConfigOption]) -> Vec<ConfigSelection> {
    options
        .iter()
        .filter(|opt| !opt.current_value.is_empty())
        .map(|opt| ConfigSelection {
            id: opt.id.clone(),
            value: opt.current_value.clone(),
            category: Some(opt.category),
        })
        .collect()
}

fn previous_engine_label(
    previous_target: Option<&ChatTarget>,
    binding: Option<&AcpBinding>,
    legacy_migration_notice: bool,
    next_agent: &AgentId,
) -> Option<String> {
    if let Some(prev) = previous_target {
        match prev {
            ChatTarget::Acp { agent, .. } if agent == next_agent => {
                if legacy_migration_notice && binding.is_none() {
                    return Some("Legacy Antigravity".into());
                }
            }
            other => return Some(other.engine_label()),
        }
    }
    if let Some(binding) = binding
        && &binding.agent_id != next_agent
    {
        return Some(format!("ACP ({})", binding.agent_id));
    }
    None
}

struct Prepared {
    session: Session,
    send: usize,
    engine: ChatEngine,
    acp_binding: Option<AcpBinding>,
    context: runs::Context,
    receipts: Receipts,
    owner: store::chat::Owner,
    recorder: Recorder,
    history: Vec<rig_core::message::Message>,
}

struct Active {
    chat: Chat,
    id: SessionId,
    cancel: CancellationToken,
}

impl Drop for Active {
    fn drop(&mut self) {
        self.chat
            .active
            .state
            .lock()
            .expect("Chat active turns lock")
            .turns
            .remove(&self.id);
        self.chat.active.settled.notify_waiters();
    }
}

fn path(state: &Path, id: &SessionId) -> PathBuf {
    state.join("chat-sessions").join(format!("{id}.jsonl"))
}