agents-kit 0.0.1

Rust agent control Kit for Codex, OpenCode, and provider-backed Custom runtimes
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
use providers_kit::error::{
    ErrorKind as ProviderErrorKind, ErrorPhase as ProviderErrorPhase,
    Operation as ProviderOperation,
};
use std::future::{Future, pending, poll_fn};
use std::net::{Ipv4Addr, SocketAddr};
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use agents_kit::config::{
    AgentConfig, CodexConfig, CustomAgentConfig, CustomLoopLimits, CustomToolApproval,
};
use agents_kit::conversation::{ConversationInfo, FileRead, NewConversation};
use agents_kit::identity::{AgentId, AgentInfo, ConversationId, NotificationId, WorkspaceId};
use agents_kit::knowledge::KnowledgeSource;
use agents_kit::model::ModelSelector;
use agents_kit::notification::{ConversationNotification, ConversationNotificationKind};
use agents_kit::tool::ToolSelection;
use agents_kit::workspace::{
    DeleteWorkspaceRequest, NewWorkspace, WorkspaceContext, WorkspaceDefaults,
    WorkspaceExecutionPolicy, WorkspaceInfo, WorkspaceNetworkAccess, WorkspaceRoot,
    WorkspaceRootAccess, WorkspaceSandboxMode,
};
use agents_kit::{
    Agent, AgentBackend, AgentBuilder, AgentStorage, Conversation, ConversationBackend, KitError,
    Workspace, WorkspaceBackend,
};
use providers_kit::config::{OllamaConfig, ProviderKind, TransportConfig};
use providers_kit::generation::{GenerateEventStream, GenerateRequest, GenerateResponse};
use providers_kit::models::{ModelBackend, ModelId};
use providers_kit::session::SessionBackend;
use providers_kit::{KitError as ProviderError, Provider, ProviderBackend, ProviderConfig};
use storage_kit::database::config::{DatabaseConfig, LanceConfig};
use storage_kit::database::{
    CapabilitySet,
    catalog::{CatalogRequest, CatalogResponse},
    data::{DataRequest, DataResponse},
    index::{IndexRequest, IndexResponse},
    query::{QueryRequest, QueryResponse},
};
use storage_kit::state::config::{LanceStateStoreConfig, StateStoreConfig};
use storage_kit::state::record::{
    StateKey, StateListRequest, StateListResponse, StateRecord, StateRecordSnapshot,
};
use storage_kit::{
    Database, DatabaseBackend, StateStore, StateStoreBackend, StateStoreError, StateTransaction,
    StateTransactionBackend, StorageError,
};
use tokio::sync::{Semaphore, mpsc};
use tools_kit::declaration::{ToolInfo, ToolName};
use tools_kit::execution::{ToolHandlerInput, ToolHandlerOutput};
use tools_kit::{ToolError, ToolHandlerBackend, ToolsetBackend};

const DEADLINE: Duration = Duration::from_secs(5);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum StoreOperation {
    Load,
    List,
    BeginTransaction,
    TransactionLoad,
}

struct BlockingStateStore {
    operations: mpsc::Sender<StoreOperation>,
}

struct DirectProvider;

struct DirectTools;

struct DirectHandler;

impl ToolsetBackend for DirectTools {
    type Handler = DirectHandler;

    async fn list_tools(&self) -> Result<Vec<ToolInfo>, ToolError> {
        Ok(Vec::new())
    }

    async fn tool_handler(&self, name: ToolName) -> Result<Self::Handler, ToolError> {
        Err(ToolError::UnknownTool { name })
    }

    async fn shutdown(&self) -> Result<(), ToolError> {
        Ok(())
    }
}

impl ToolHandlerBackend for DirectHandler {
    async fn step(&mut self, _input: ToolHandlerInput) -> Result<ToolHandlerOutput, ToolError> {
        Err(ToolError::Unsupported {
            operation: "empty test inventory".to_owned(),
        })
    }
}

#[derive(Clone)]
struct DirectContinuation;

struct DirectModel {
    id: ModelId,
}

impl ProviderBackend for DirectProvider {
    type Model = DirectModel;

    fn model(&self, id: ModelId) -> Result<Self::Model, ProviderError> {
        Ok(DirectModel { id })
    }

    async fn shutdown(&self) -> Result<(), ProviderError> {
        Ok(())
    }
}

impl ModelBackend for DirectModel {
    type Continuation = DirectContinuation;
    type Session = std::convert::Infallible;

    fn model_id(&self) -> &ModelId {
        &self.id
    }

    async fn generate(
        &self,
        _request: &GenerateRequest<DirectContinuation>,
    ) -> Result<GenerateResponse<DirectContinuation>, ProviderError> {
        Err(ProviderError::new(
            ProviderErrorKind::Unsupported,
            ProviderOperation::Generate,
            ProviderErrorPhase::InputValidation,
        ))
    }

    async fn generate_stream(
        &self,
        _request: &GenerateRequest<DirectContinuation>,
    ) -> Result<GenerateEventStream<DirectContinuation>, ProviderError> {
        Err(ProviderError::new(
            ProviderErrorKind::Unsupported,
            ProviderOperation::GenerateStream,
            ProviderErrorPhase::InputValidation,
        ))
    }
}

struct BlockingStateTransaction {
    operations: mpsc::Sender<StoreOperation>,
}
struct StoreProbe {
    backend: Option<BlockingStateStore>,
    operations: mpsc::Receiver<StoreOperation>,
}

impl StoreProbe {
    fn new() -> Self {
        let (operations, observed) = mpsc::channel(8);
        Self {
            backend: Some(BlockingStateStore { operations }),
            operations: observed,
        }
    }
    fn storage(&mut self) -> AgentStorage<BlockingStateStore> {
        AgentStorage::new(
            self.backend
                .take()
                .expect("probe StateStore must transfer exactly once"),
        )
    }
    fn agent_config() -> AgentConfig {
        let codex = CodexConfig::tcp(
            SocketAddr::from((Ipv4Addr::LOCALHOST, 9)),
            Duration::from_secs(1),
            Duration::from_secs(1),
        )
        .expect("probe Codex config must be valid");
        AgentConfig::new(
            codex.into(),
            Duration::from_secs(1),
            NonZeroUsize::new(1).expect("one is nonzero"),
        )
        .expect("probe Agent config must be valid")
    }
    fn missing_agent_id() -> AgentId {
        "agent_00000000000000000000000000000001"
            .parse()
            .expect("canonical AgentId")
    }
    async fn next(&mut self) -> StoreOperation {
        tokio::time::timeout(DEADLINE, self.operations.recv())
            .await
            .expect("StateStore operation must begin before its deadline")
            .expect("StateStore operation channel must remain open")
    }
    async fn expect_missing_lookup(&mut self, storage: &AgentStorage<BlockingStateStore>) {
        let lookup = tokio::spawn({
            let storage = storage.clone();
            async move { storage.agent_info(Self::missing_agent_id()).await }
        });
        assert_eq!(self.next().await, StoreOperation::Load);
        assert!(matches!(tokio::time::timeout(DEADLINE, lookup)
                .await
                .expect("lookup must remain bounded")
                .expect("lookup task must not panic"), Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::NotFound)));
    }
    async fn abort<T>(task: tokio::task::JoinHandle<T>) {
        task.abort();
        let result = tokio::time::timeout(DEADLINE, task)
            .await
            .expect("aborted task must join");
        assert!(matches!(result, Err(error) if error.is_cancelled()));
    }
}

impl StateStoreBackend for BlockingStateStore {
    type Transaction = BlockingStateTransaction;
    async fn load(&self, _key: StateKey) -> Result<Option<StateRecordSnapshot>, StateStoreError> {
        self.operations
            .send(StoreOperation::Load)
            .await
            .expect("probe must remain available");
        Ok(None)
    }
    async fn list(&self, _request: StateListRequest) -> Result<StateListResponse, StateStoreError> {
        self.operations
            .send(StoreOperation::List)
            .await
            .expect("probe must remain available");
        pending().await
    }
    async fn begin_transaction(&self) -> Result<Self::Transaction, StateStoreError> {
        self.operations
            .send(StoreOperation::BeginTransaction)
            .await
            .expect("probe must remain available");
        Ok(BlockingStateTransaction {
            operations: self.operations.clone(),
        })
    }
}

impl StateTransactionBackend for BlockingStateTransaction {
    async fn load(
        &mut self,
        _key: StateKey,
    ) -> Result<Option<StateRecordSnapshot>, StateStoreError> {
        self.operations
            .send(StoreOperation::TransactionLoad)
            .await
            .expect("probe must remain available");
        pending().await
    }
    async fn put(&mut self, _record: StateRecord) -> Result<(), StateStoreError> {
        Ok(())
    }
    async fn delete(&mut self, _key: StateKey) -> Result<(), StateStoreError> {
        Ok(())
    }
    fn commit(self) -> impl Future<Output = Result<(), StateStoreError>> + Send {
        pending()
    }
    fn rollback(self) -> impl Future<Output = Result<(), StateStoreError>> + Send {
        pending()
    }
}

#[derive(Clone, Copy, Eq, PartialEq)]
enum CommitFault {
    None,
    ConflictBeforeCommit,
    UnavailableBeforeCommit,
    UnavailableAfterCommit,
    BlockThenCommit,
    TimedOutBeforeCommit,
    TimedOutAfterCommit,
    BlockThenTimedOutBeforeCommit,
    BlockThenTimedOutAfterCommit,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum CloseStorageOperation {
    Begin,
    Load,
    Put,
    Delete,
    Commit,
    Rollback,
}

enum OperationFaultTarget {
    Transaction(usize),
    Invocation(usize),
}

enum CommitFaultTarget {
    WriteOrdinal(usize),
    Delete(StateKey),
}

struct ArmedCommitFault {
    fault: CommitFault,
    target: CommitFaultTarget,
}

struct CommitFaultState {
    armed: Mutex<Option<ArmedCommitFault>>,
    write_commits: AtomicUsize,
    blocked: Semaphore,
    release: Semaphore,
    operation_fault: Mutex<Option<(CloseStorageOperation, OperationFaultTarget, StateStoreError)>>,
    held_load: Mutex<Option<(usize, bool)>>,
    settled_loads: AtomicUsize,
    load_settled: Semaphore,
    transactions: AtomicUsize,
    injected_timeouts: AtomicUsize,
    injected_faults: AtomicUsize,
}

#[derive(Clone)]
struct FaultStateStore {
    inner: StateStore,
    state: Arc<CommitFaultState>,
}

impl FaultStateStore {
    fn new(inner: StateStore) -> Self {
        Self {
            inner,
            state: Arc::new(CommitFaultState {
                armed: Mutex::new(None),
                write_commits: AtomicUsize::new(0),
                blocked: Semaphore::new(0),
                release: Semaphore::new(0),
                operation_fault: Mutex::new(None),
                held_load: Mutex::new(None),
                settled_loads: AtomicUsize::new(0),
                load_settled: Semaphore::new(0),
                transactions: AtomicUsize::new(0),
                injected_timeouts: AtomicUsize::new(0),
                injected_faults: AtomicUsize::new(0),
            }),
        }
    }
    fn arm(&self, fault: CommitFault, target: usize) {
        assert!(target > 0);
        self.arm_target(fault, CommitFaultTarget::WriteOrdinal(target));
    }
    fn arm_delete(&self, fault: CommitFault, key: StateKey) {
        self.arm_target(fault, CommitFaultTarget::Delete(key));
    }
    fn arm_target(&self, fault: CommitFault, target: CommitFaultTarget) {
        assert!(fault != CommitFault::None);
        self.state.write_commits.store(0, Ordering::SeqCst);
        self.state.transactions.store(0, Ordering::SeqCst);
        self.state.injected_timeouts.store(0, Ordering::SeqCst);
        self.state.injected_faults.store(0, Ordering::SeqCst);
        *self
            .state
            .armed
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) =
            Some(ArmedCommitFault { fault, target });
    }
    fn clear(&self) {
        *self
            .state
            .armed
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner()) = None;
        *self.state.operation_fault.lock().expect("operation fault") = None;
        *self.state.held_load.lock().expect("held load fault") = None;
    }
    fn arm_operation_timeout(&self, operation: CloseStorageOperation, transaction: usize) {
        self.arm_operation_fault(
            operation,
            OperationFaultTarget::Transaction(transaction),
            StateStoreError::timed_out("private-close-timeout", 41),
        );
    }
    fn arm_operation_fault(
        &self,
        operation: CloseStorageOperation,
        target: OperationFaultTarget,
        error: StateStoreError,
    ) {
        self.clear();
        self.state.write_commits.store(0, Ordering::SeqCst);
        self.state.transactions.store(0, Ordering::SeqCst);
        self.state.injected_timeouts.store(0, Ordering::SeqCst);
        self.state.injected_faults.store(0, Ordering::SeqCst);
        *self.state.operation_fault.lock().expect("operation fault") =
            Some((operation, target, error));
    }
    fn arm_held_load(&self, transaction: usize, fail_after_release: bool) {
        self.clear();
        self.state.write_commits.store(0, Ordering::SeqCst);
        self.state.transactions.store(0, Ordering::SeqCst);
        self.state.settled_loads.store(0, Ordering::SeqCst);
        *self.state.held_load.lock().expect("held load fault") =
            Some((transaction, fail_after_release));
    }
    async fn hold_load(&self, transaction: usize) -> Result<(), StateStoreError> {
        let fault = {
            let mut fault = self.state.held_load.lock().expect("held load fault");
            if fault
                .as_ref()
                .is_some_and(|(target, _)| *target == transaction)
            {
                fault.take()
            } else {
                None
            }
        };
        let Some((_, fail_after_release)) = fault else {
            return Ok(());
        };
        self.state.blocked.add_permits(1);
        self.state
            .release
            .acquire()
            .await
            .expect("load release")
            .forget();
        self.state.settled_loads.fetch_add(1, Ordering::SeqCst);
        self.state.load_settled.add_permits(1);
        if fail_after_release {
            Err(StateStoreError::timed_out("private-close-timeout", 41))
        } else {
            Ok(())
        }
    }
    async fn wait_until_load_settled(&self) {
        self.state
            .load_settled
            .acquire()
            .await
            .expect("load outcome")
            .forget();
    }
    fn check_operation_fault(
        &self,
        operation: CloseStorageOperation,
        transaction: usize,
    ) -> Result<(), StateStoreError> {
        let mut fault = self.state.operation_fault.lock().expect("operation fault");
        let selected = match fault.as_mut() {
            Some((selected, target, _)) if *selected == operation => match target {
                OperationFaultTarget::Transaction(target) => *target == transaction,
                OperationFaultTarget::Invocation(remaining) => {
                    *remaining -= 1;
                    *remaining == 0
                }
            },
            Some(_) | None => false,
        };
        if selected {
            let (_, _, error) = fault.take().expect("selected operation fault");
            if matches!(&error, StateStoreError::TimedOut { .. }) {
                self.state.injected_timeouts.fetch_add(1, Ordering::SeqCst);
            }
            self.state.injected_faults.fetch_add(1, Ordering::SeqCst);
            return Err(error);
        }
        Ok(())
    }
    fn transaction_count(&self) -> usize {
        self.state.transactions.load(Ordering::SeqCst)
    }
    fn write_commit_count(&self) -> usize {
        self.state.write_commits.load(Ordering::SeqCst)
    }
    async fn before_commit(&self, wrote: bool, deleted: &[StateKey]) -> CommitFault {
        if !wrote {
            return CommitFault::None;
        }
        let ordinal = self.state.write_commits.fetch_add(1, Ordering::SeqCst) + 1;
        let fault = {
            let mut armed = self
                .state
                .armed
                .lock()
                .unwrap_or_else(|poisoned| poisoned.into_inner());
            let matches = armed.as_ref().is_some_and(|armed| match &armed.target {
                CommitFaultTarget::WriteOrdinal(target) => ordinal == *target,
                CommitFaultTarget::Delete(target) => deleted.contains(target),
            });
            if matches {
                self.state.injected_faults.fetch_add(1, Ordering::SeqCst);
                armed.take().map_or(CommitFault::None, |armed| armed.fault)
            } else {
                CommitFault::None
            }
        };
        if matches!(
            fault,
            CommitFault::BlockThenCommit
                | CommitFault::BlockThenTimedOutBeforeCommit
                | CommitFault::BlockThenTimedOutAfterCommit
        ) {
            self.state.blocked.add_permits(1);
            self.state
                .release
                .acquire()
                .await
                .expect("fault release semaphore must remain open")
                .forget();
        }
        fault
    }
    async fn wait_until_blocked(&self) {
        self.state
            .blocked
            .acquire()
            .await
            .expect("fault block semaphore must remain open")
            .forget();
    }
    fn release(&self) {
        self.state.release.add_permits(1);
    }
}

struct FaultStateTransaction {
    inner: StateTransaction,
    faults: FaultStateStore,
    wrote: bool,
    deleted: Vec<StateKey>,
    ordinal: usize,
}

impl StateStoreBackend for FaultStateStore {
    type Transaction = FaultStateTransaction;
    async fn load(&self, key: StateKey) -> Result<Option<StateRecordSnapshot>, StateStoreError> {
        self.inner.load(key).await
    }
    async fn list(&self, request: StateListRequest) -> Result<StateListResponse, StateStoreError> {
        self.inner.list(request).await
    }
    async fn begin_transaction(&self) -> Result<Self::Transaction, StateStoreError> {
        let ordinal = self.state.transactions.fetch_add(1, Ordering::SeqCst) + 1;
        self.check_operation_fault(CloseStorageOperation::Begin, ordinal)?;
        Ok(FaultStateTransaction {
            inner: self.inner.begin_transaction().await?,
            faults: self.clone(),
            wrote: false,
            deleted: Vec::new(),
            ordinal,
        })
    }
}

impl StateTransactionBackend for FaultStateTransaction {
    async fn load(
        &mut self,
        key: StateKey,
    ) -> Result<Option<StateRecordSnapshot>, StateStoreError> {
        self.faults
            .check_operation_fault(CloseStorageOperation::Load, self.ordinal)?;
        self.faults.hold_load(self.ordinal).await?;
        self.inner.load(key).await
    }
    async fn put(&mut self, record: StateRecord) -> Result<(), StateStoreError> {
        self.faults
            .check_operation_fault(CloseStorageOperation::Put, self.ordinal)?;
        self.wrote = true;
        self.inner.put(record).await
    }
    async fn delete(&mut self, key: StateKey) -> Result<(), StateStoreError> {
        self.faults
            .check_operation_fault(CloseStorageOperation::Delete, self.ordinal)?;
        self.wrote = true;
        self.inner.delete(key.clone()).await?;
        self.deleted.push(key);
        Ok(())
    }
    async fn commit(self) -> Result<(), StateStoreError> {
        match self.faults.before_commit(self.wrote, &self.deleted).await {
            CommitFault::ConflictBeforeCommit => {
                self.inner.rollback().await?;
                Err(StateStoreError::transaction_conflict(
                    "commit",
                    "injected conflict",
                ))
            }
            CommitFault::UnavailableBeforeCommit => {
                self.inner.rollback().await?;
                Err(StateStoreError::unavailable(
                    "commit",
                    "injected unavailability",
                ))
            }
            CommitFault::UnavailableAfterCommit => {
                self.inner.commit().await?;
                Err(StateStoreError::unavailable(
                    "commit",
                    "ambiguous commit response",
                ))
            }
            CommitFault::TimedOutBeforeCommit | CommitFault::BlockThenTimedOutBeforeCommit => {
                self.inner.rollback().await?;
                self.faults
                    .state
                    .injected_timeouts
                    .fetch_add(1, Ordering::SeqCst);
                Err(StateStoreError::timed_out("private-close-timeout", 41))
            }
            CommitFault::TimedOutAfterCommit | CommitFault::BlockThenTimedOutAfterCommit => {
                self.inner.commit().await?;
                self.faults
                    .state
                    .injected_timeouts
                    .fetch_add(1, Ordering::SeqCst);
                Err(StateStoreError::timed_out("private-close-timeout", 41))
            }
            CommitFault::None | CommitFault::BlockThenCommit => self.inner.commit().await,
        }
    }
    async fn rollback(self) -> Result<(), StateStoreError> {
        self.inner.rollback().await?;
        self.faults
            .check_operation_fault(CloseStorageOperation::Rollback, self.ordinal)
    }
}

struct KnowledgeDeleteProbe {
    database: Database,
    gate: Arc<KnowledgeDeleteGate>,
}

struct KnowledgeDeleteControl {
    gate: Arc<KnowledgeDeleteGate>,
}

struct KnowledgeDeleteGate {
    armed: AtomicBool,
    started: AtomicBool,
    observed: Semaphore,
    release: Semaphore,
    fail_after_release: AtomicBool,
    calls: AtomicUsize,
    settled: AtomicUsize,
    interrupted: AtomicUsize,
    outcome: Semaphore,
}

struct KnowledgeDeleteGuard<'a> {
    gate: &'a KnowledgeDeleteGate,
    settled: bool,
}

impl KnowledgeDeleteGuard<'_> {
    fn complete(mut self) {
        self.settled = true;
        self.gate.settled.fetch_add(1, Ordering::SeqCst);
        self.gate.outcome.add_permits(1);
    }
}

impl Drop for KnowledgeDeleteGuard<'_> {
    fn drop(&mut self) {
        if !self.settled {
            self.gate.interrupted.fetch_add(1, Ordering::SeqCst);
            self.gate.outcome.add_permits(1);
        }
    }
}

impl KnowledgeDeleteProbe {
    fn new(database: Database) -> (Self, KnowledgeDeleteControl) {
        let gate = Arc::new(KnowledgeDeleteGate {
            armed: AtomicBool::new(false),
            started: AtomicBool::new(false),
            observed: Semaphore::new(0),
            release: Semaphore::new(0),
            fail_after_release: AtomicBool::new(false),
            calls: AtomicUsize::new(0),
            settled: AtomicUsize::new(0),
            interrupted: AtomicUsize::new(0),
            outcome: Semaphore::new(0),
        });
        (
            Self {
                database,
                gate: Arc::clone(&gate),
            },
            KnowledgeDeleteControl { gate },
        )
    }
}

impl KnowledgeDeleteControl {
    fn fail_after_release(&self) {
        self.gate.fail_after_release.store(true, Ordering::SeqCst);
    }

    fn calls(&self) -> usize {
        self.gate.calls.load(Ordering::SeqCst)
    }

    fn arm(&self) {
        self.gate.calls.store(0, Ordering::SeqCst);
        assert!(
            !self.gate.armed.swap(true, Ordering::SeqCst),
            "knowledge delete probe must not already be armed"
        );
        assert!(
            !self.gate.started.load(Ordering::SeqCst),
            "knowledge delete probe supports one exact deletion"
        );
    }

    async fn wait_for_outcome(&self) {
        self.gate
            .outcome
            .acquire()
            .await
            .expect("delete outcome")
            .forget();
    }

    fn deletion_started(&self) -> bool {
        self.gate.started.load(Ordering::SeqCst)
    }

    async fn wait_until_blocked(&self) {
        self.gate
            .observed
            .acquire()
            .await
            .expect("knowledge delete observation must remain open")
            .forget();
    }

    fn release(&self) {
        self.gate.release.add_permits(1);
    }
}

impl DatabaseBackend for KnowledgeDeleteProbe {
    fn capabilities(&self) -> CapabilitySet {
        self.database.capabilities()
    }

    async fn catalog(&self, request: CatalogRequest) -> Result<CatalogResponse, StorageError> {
        self.database.catalog(request).await
    }

    async fn data(&self, request: DataRequest) -> Result<DataResponse, StorageError> {
        if matches!(&request, DataRequest::Delete(_)) {
            self.gate.calls.fetch_add(1, Ordering::SeqCst);
        }
        if matches!(&request, DataRequest::Delete(_))
            && self.gate.armed.swap(false, Ordering::SeqCst)
        {
            let guard = KnowledgeDeleteGuard {
                gate: &self.gate,
                settled: false,
            };
            self.gate.started.store(true, Ordering::SeqCst);
            self.gate.observed.add_permits(1);
            self.gate
                .release
                .acquire()
                .await
                .expect("knowledge delete release must remain open")
                .forget();
            let result = if self.gate.fail_after_release.load(Ordering::SeqCst) {
                Err(StorageError::backend_op(
                    "delete",
                    "private knowledge failure",
                ))
            } else {
                self.database.data(request).await
            };
            guard.complete();
            return result;
        }
        self.database.data(request).await
    }

    async fn query(&self, request: QueryRequest) -> Result<QueryResponse, StorageError> {
        self.database.query(request).await
    }

    async fn index(&self, request: IndexRequest) -> Result<IndexResponse, StorageError> {
        self.database.index(request).await
    }
}

struct CustomFixture<D: DatabaseBackend = Database> {
    faults: FaultStateStore,
    agent: Agent<FaultStateStore, D>,
    root: PathBuf,
}

impl<D: DatabaseBackend> CustomFixture<D> {
    fn config(lifecycle_timeout: Duration) -> AgentConfig {
        let custom = CustomAgentConfig::new(
            ModelSelector::new("qwen3").expect("fixture model selector must be valid"),
            ToolSelection::None,
            CustomLoopLimits::new(2, 0, 1).expect("fixture loop limits must be valid"),
            CustomToolApproval::Deny,
        )
        .expect("fixture Custom config must be valid");
        AgentConfig::new(
            custom.into(),
            lifecycle_timeout,
            NonZeroUsize::new(1).expect("one is nonzero"),
        )
        .expect("fixture Agent config must be valid")
    }
    fn provider() -> Provider {
        Provider::from_config(ProviderConfig::new(
            ProviderKind::Ollama(OllamaConfig::local()),
            TransportConfig::default(),
        ))
        .expect("fixture Provider must build without network access")
    }
    async fn build_with_knowledge_database(database: D) -> Self {
        let store = StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory()))
            .await
            .expect("fixture Lance StateStore must build");
        let faults = FaultStateStore::new(store);
        let root = std::env::current_dir().expect("fixture cwd must be available");
        let agent = AgentBuilder::create(
            Self::config(Duration::from_secs(2)),
            AgentStorage::new(faults.clone()),
        )
        .with_provider(Self::provider())
        .with_knowledge_database(database)
        .build()
        .await
        .expect("fixture Custom Agent must build");
        Self {
            faults,
            agent,
            root,
        }
    }

    fn workspace_request(&self, name: &str) -> NewWorkspace {
        let path = self.root.clone();
        let context = WorkspaceContext::new(
            path.clone(),
            vec![
                WorkspaceRoot::new(path, WorkspaceRootAccess::ReadWrite)
                    .expect("fixture workspace root must be valid"),
            ],
            WorkspaceExecutionPolicy::new(
                WorkspaceSandboxMode::FullAccess,
                WorkspaceNetworkAccess::Disabled,
            ),
        )
        .expect("fixture workspace context must be valid");
        NewWorkspace::new(name, context, WorkspaceDefaults::empty())
            .expect("fixture workspace request must be valid")
    }
}

impl CustomFixture<Database> {
    async fn build() -> Self {
        Self::build_with_lifecycle_timeout(Duration::from_secs(2)).await
    }

    async fn build_with_lifecycle_timeout(lifecycle_timeout: Duration) -> Self {
        let store = StateStore::connect(StateStoreConfig::Lance(LanceStateStoreConfig::memory()))
            .await
            .expect("fixture Lance StateStore must build");
        let faults = FaultStateStore::new(store);
        let root = std::env::current_dir().expect("fixture cwd must be available");
        let agent = AgentBuilder::create(
            Self::config(lifecycle_timeout),
            AgentStorage::new(faults.clone()),
        )
        .with_provider(Self::provider())
        .build()
        .await
        .expect("fixture Custom Agent must build");
        Self {
            faults,
            agent,
            root,
        }
    }

    async fn reopen(faults: &FaultStateStore, id: AgentId) -> Agent<FaultStateStore> {
        AgentBuilder::reopen(
            Self::config(Duration::from_secs(2)),
            AgentStorage::new(faults.clone()),
            id,
        )
        .with_provider(Self::provider())
        .build()
        .await
        .expect("fixture Custom Agent must reopen")
    }

    fn conversation_request() -> NewConversation {
        NewConversation::new(None, None, Vec::new()).expect("valid conversation request")
    }
}

#[tokio::test]
async fn agent_storage_reads_progress_behind_reads_and_associated_transactions() {
    let mut probe = StoreProbe::new();
    let storage = probe.storage();
    let blocked_list = tokio::spawn({
        let storage = storage.clone();
        async move { storage.list_agents().await }
    });
    assert_eq!(probe.next().await, StoreOperation::List);
    probe.expect_missing_lookup(&storage).await;
    assert!(!blocked_list.is_finished());
    StoreProbe::abort(blocked_list).await;

    let blocked_build = tokio::spawn({
        let storage = storage.clone();
        async move {
            AgentBuilder::create_for_backends::<DirectProvider, DirectTools>(
                StoreProbe::agent_config(),
                storage,
            )
            .build()
            .await
        }
    });
    assert_eq!(probe.next().await, StoreOperation::BeginTransaction);
    assert_eq!(probe.next().await, StoreOperation::TransactionLoad);
    probe.expect_missing_lookup(&storage).await;
    assert!(!blocked_build.is_finished());
    StoreProbe::abort(blocked_build).await;
}

#[test]
fn static_integration_types_preserve_associated_children_without_clone_bounds() {
    fn assert_clone<T: Clone>() {}
    fn assert_transaction<T: StateTransactionBackend>() {}
    fn assert_session<T: SessionBackend>() {}
    fn assert_direct_model<T>()
    where
        T: ModelBackend<Continuation = DirectContinuation, Session = std::convert::Infallible>,
    {
    }
    fn assert_hierarchy<S, D, P, T>()
    where
        S: StateStoreBackend,
        D: DatabaseBackend,
        P: ProviderBackend,
        T: ToolsetBackend,
        Agent<S, D, P, T>: AgentBackend<Workspace = Workspace<S, D, P, T>>,
        Workspace<S, D, P, T>: WorkspaceBackend<Conversation = Conversation<S, D, P, T>>,
        Conversation<S, D, P, T>: ConversationBackend,
    {
        assert_transaction::<S::Transaction>();
        assert_session::<<<P as ProviderBackend>::Model as ModelBackend>::Session>();
    }

    fn replace_database(
        builder: AgentBuilder<StateStore, Database, DirectProvider, DirectTools>,
        database: KnowledgeDeleteProbe,
    ) -> AgentBuilder<StateStore, KnowledgeDeleteProbe, DirectProvider, DirectTools> {
        builder.with_knowledge_database(database)
    }

    assert_clone::<AgentStorage<BlockingStateStore>>();
    assert_direct_model::<DirectModel>();
    assert_hierarchy::<BlockingStateStore, KnowledgeDeleteProbe, DirectProvider, DirectTools>();
    assert_hierarchy::<StateStore, Database, Provider, tools_kit::Toolset>();

    let mut probe = StoreProbe::new();
    let storage = probe.storage();
    let _: AgentBuilder<BlockingStateStore, Database, DirectProvider, DirectTools> =
        AgentBuilder::<BlockingStateStore>::create_for_backends::<DirectProvider, DirectTools>(
            StoreProbe::agent_config(),
            storage.clone(),
        );
    let _: AgentBuilder<BlockingStateStore, Database, DirectProvider, DirectTools> =
        AgentBuilder::<BlockingStateStore>::reopen_for_backends::<DirectProvider, DirectTools>(
            StoreProbe::agent_config(),
            storage,
            StoreProbe::missing_agent_id(),
        );
    let default_create: fn(
        AgentConfig,
        AgentStorage<BlockingStateStore>,
    ) -> AgentBuilder<BlockingStateStore> = AgentBuilder::create;
    let default_reopen: fn(
        AgentConfig,
        AgentStorage<BlockingStateStore>,
        AgentId,
    ) -> AgentBuilder<BlockingStateStore> = AgentBuilder::reopen;
    let select_direct_provider: fn(
        AgentBuilder,
    ) -> AgentBuilder<StateStore, Database, DirectProvider> =
        |builder| builder.with_provider(DirectProvider);
    let _ = (
        default_create,
        default_reopen,
        replace_database,
        select_direct_provider,
    );
}

#[tokio::test]
async fn custom_close_propagates_settled_storage_timeouts_without_restarting_a_phase() {
    use agents_kit::error::{DeadlineKind, ErrorDetail, ErrorKind, ErrorPhase};
    // With no active prompt, cancellation preserves the continuation and must
    // finish its read transaction through rollback. Close admission and terminal
    // close still mutate state; pending interactions have a separate read.
    for (target, completion) in [
        (1, CloseStorageOperation::Put),
        (2, CloseStorageOperation::Rollback),
        (4, CloseStorageOperation::Put),
    ] {
        for operation in [
            CloseStorageOperation::Begin,
            CloseStorageOperation::Load,
            completion,
        ] {
            let fixture = CustomFixture::build().await;
            let workspace = fixture
                .agent
                .new_workspace(fixture.workspace_request("close-operation-timeout"))
                .await
                .expect("workspace");
            let conversation = workspace
                .new_conversation(CustomFixture::conversation_request())
                .await
                .expect("conversation");
            let receiver = conversation
                .notification_stream()
                .expect("notification receiver");
            fixture.faults.arm_operation_timeout(operation, target);
            let result = tokio::time::timeout(DEADLINE, conversation.close()).await;
            let attempts = fixture.faults.transaction_count();
            let injected = fixture
                .faults
                .state
                .injected_timeouts
                .load(Ordering::SeqCst);
            fixture.faults.clear();
            drop(receiver);
            drop(workspace);
            let shutdown = tokio::time::timeout(DEADLINE, fixture.agent.shutdown()).await;

            shutdown
                .expect("shutdown remains bounded")
                .expect("shutdown cleans owned work");
            let error = match result.expect("close remains bounded") {
                Err(error) => error,
                Ok(()) => panic!(
                    "settled storage timeout must end close: target={target} operation={operation:?} injected={injected} attempts={attempts}"
                ),
            };
            assert_eq!(
                error.kind(),
                ErrorKind::Timeout,
                "target={target} operation={operation:?}"
            );
            assert_eq!(
                error.context().phase,
                match operation {
                    CloseStorageOperation::Load => ErrorPhase::StateRead,
                    CloseStorageOperation::Begin
                    | CloseStorageOperation::Put
                    | CloseStorageOperation::Delete => ErrorPhase::StateWrite,
                    CloseStorageOperation::Commit => ErrorPhase::StateCommit,
                    CloseStorageOperation::Rollback => ErrorPhase::Cleanup,
                }
            );
            assert_eq!(
                error.detail(),
                Some(&ErrorDetail::DeadlineElapsed {
                    kind: DeadlineKind::Operation,
                    timeout: Some(Duration::from_millis(41)),
                })
            );
            assert_eq!(injected, 1);
            assert_eq!(
                attempts, target,
                "no retry or later close phase may start after failure"
            );
            assert!(!format!("{error:?}").contains("private-close-timeout"));
        }
    }
}

#[tokio::test]
async fn custom_close_commit_timeouts_preserve_exact_commit_authority() {
    use agents_kit::error::{DeadlineKind, ErrorDetail, ErrorKind, ErrorPhase};
    for target in [1, 2] {
        for commit_first in [false, true] {
            let fixture = CustomFixture::build().await;
            let workspace = fixture
                .agent
                .new_workspace(fixture.workspace_request("close-commit-timeout"))
                .await
                .expect("workspace");
            let conversation = workspace
                .new_conversation(CustomFixture::conversation_request())
                .await
                .expect("conversation");
            let receiver = conversation
                .notification_stream()
                .expect("notification receiver");
            fixture.faults.arm(
                if commit_first {
                    CommitFault::TimedOutAfterCommit
                } else {
                    CommitFault::TimedOutBeforeCommit
                },
                target,
            );
            let result = tokio::time::timeout(DEADLINE, conversation.close()).await;
            let attempts = fixture.faults.transaction_count();
            let writes = fixture.faults.write_commit_count();
            let injected = fixture
                .faults
                .state
                .injected_timeouts
                .load(Ordering::SeqCst);
            fixture.faults.clear();
            drop(receiver);
            drop(workspace);
            let shutdown = tokio::time::timeout(DEADLINE, fixture.agent.shutdown()).await;

            shutdown
                .expect("shutdown remains bounded")
                .expect("shutdown cleans owned work");
            let result = result.expect("close remains bounded");
            if commit_first {
                result.expect("an exactly observed commit is authoritative");
                assert_eq!(
                    attempts, 4,
                    "two close mutations plus cancellation and interaction reads; no replay"
                );
                assert_eq!(writes, 2);
            } else {
                let error = result.expect_err("an uncommitted close mutation must fail");
                assert_eq!(error.kind(), ErrorKind::Timeout);
                assert_eq!(error.context().phase, ErrorPhase::StateCommit);
                assert_eq!(
                    error.detail(),
                    Some(&ErrorDetail::DeadlineElapsed {
                        kind: DeadlineKind::Operation,
                        timeout: Some(Duration::from_millis(41)),
                    })
                );
                assert_eq!(
                    attempts,
                    if target == 1 { 1 } else { 4 },
                    "a failed commit cannot restart its phase"
                );
                assert_eq!(writes, target);
            }
            assert_eq!(injected, 1);
        }
    }
}

#[tokio::test]
async fn custom_close_retains_one_held_mutation_after_waiter_expiry_or_caller_loss() {
    use agents_kit::error::ErrorKind;
    let lifecycle_timeout = Duration::from_millis(300);
    for target in [1, 2] {
        for commit_first in [false, true] {
            for caller_loss in [false, true] {
                let fixture = CustomFixture::build_with_lifecycle_timeout(lifecycle_timeout).await;
                let workspace = fixture
                    .agent
                    .new_workspace(fixture.workspace_request("held-close-mutation"))
                    .await
                    .expect("workspace");
                let conversation = workspace
                    .new_conversation(CustomFixture::conversation_request())
                    .await
                    .expect("conversation");
                let mut receiver = conversation
                    .notification_stream()
                    .expect("notification receiver");
                fixture.faults.arm(
                    if commit_first {
                        CommitFault::BlockThenTimedOutAfterCommit
                    } else {
                        CommitFault::BlockThenTimedOutBeforeCommit
                    },
                    target,
                );
                let mut close = tokio::spawn(async move { conversation.close().await });
                let blocked =
                    tokio::time::timeout(DEADLINE, fixture.faults.wait_until_blocked()).await;
                if caller_loss {
                    close.abort();
                }
                let observation = tokio::time::timeout(DEADLINE, &mut close).await;
                // The admitted transaction stays held beyond both observation modes' phase budget.
                tokio::time::sleep(lifecycle_timeout + Duration::from_millis(25)).await;
                let held_writes = fixture.faults.write_commit_count();
                fixture.faults.clear();
                fixture.faults.release();
                let joined_after_release = if observation.is_err() {
                    Some(tokio::time::timeout(DEADLINE, &mut close).await)
                } else {
                    None
                };
                let drained = if commit_first {
                    Some(tokio::time::timeout(DEADLINE, receiver.recv()).await)
                } else {
                    None
                };
                drop(workspace);
                let shutdown = tokio::time::timeout(DEADLINE, fixture.agent.shutdown()).await;
                let writes = fixture.faults.write_commit_count();
                let injected = fixture
                    .faults
                    .state
                    .injected_timeouts
                    .load(Ordering::SeqCst);
                drop(receiver);

                blocked.expect("the selected close mutation reached its gate");
                shutdown
                    .expect("shutdown joins released close work within its budget")
                    .expect("shutdown cleans the remaining runtime");
                assert!(
                    joined_after_release.is_none(),
                    "the public close observer must settle before release"
                );
                let observation = observation.expect("public close observation stays bounded");
                if caller_loss {
                    assert!(observation.expect_err("aborted observer").is_cancelled());
                } else {
                    assert_eq!(
                        observation
                            .expect("close observer task")
                            .expect_err("held mutation outlives the observer")
                            .kind(),
                        ErrorKind::Timeout
                    );
                }
                assert_eq!(
                    held_writes, target,
                    "waiting cannot dispatch another mutation"
                );
                assert_eq!(injected, 1);
                if commit_first {
                    assert!(
                        drained
                            .expect("drain observation")
                            .expect("late close seals delivery")
                            .is_none()
                    );
                    assert_eq!(
                        writes, 4,
                        "two close mutations plus notification and conversation reclamation; no replay"
                    );
                } else {
                    assert_eq!(
                        writes, target,
                        "a late failed result must stop its original phase"
                    );
                }
            }
        }
    }
}

#[tokio::test]
async fn custom_close_retains_pending_interaction_cancellation_receipt() {
    use agents_kit::error::ErrorKind;
    for fail_after_release in [false, true] {
        let fixture = CustomFixture::build_with_lifecycle_timeout(Duration::from_millis(300)).await;
        let workspace = fixture
            .agent
            .new_workspace(fixture.workspace_request("held-close-cancellation"))
            .await
            .expect("workspace");
        let conversation = workspace
            .new_conversation(CustomFixture::conversation_request())
            .await
            .expect("conversation");
        let mut receiver = conversation
            .notification_stream()
            .expect("notification receiver");
        // Pending interactions are read after close admission and the unchanged
        // cancellation continuation has settled its read transaction.
        fixture.faults.arm_held_load(3, fail_after_release);
        let mut close = tokio::spawn(async move { conversation.close().await });
        let blocked = tokio::time::timeout(DEADLINE, fixture.faults.wait_until_blocked()).await;
        let observation = tokio::time::timeout(DEADLINE, &mut close).await;
        let settled_before_release = tokio::time::timeout(
            Duration::from_millis(325),
            fixture.faults.wait_until_load_settled(),
        )
        .await;
        let held_transactions = fixture.faults.transaction_count();
        fixture.faults.clear();
        fixture.faults.release();
        let joined_after_release = if observation.is_err() {
            Some(tokio::time::timeout(DEADLINE, &mut close).await)
        } else {
            None
        };
        let drained = if fail_after_release {
            None
        } else {
            Some(tokio::time::timeout(DEADLINE, receiver.recv()).await)
        };
        drop(workspace);
        let shutdown = tokio::time::timeout(DEADLINE, fixture.agent.shutdown()).await;
        let writes = fixture.faults.write_commit_count();
        let settled_loads = fixture.faults.state.settled_loads.load(Ordering::SeqCst);
        drop(receiver);

        blocked.expect("pending interaction cancellation reaches its gate");
        shutdown
            .expect("shutdown remains bounded")
            .expect("shutdown joins cancellation");
        assert!(joined_after_release.is_none());
        assert!(
            settled_before_release.is_err(),
            "the original read remains held until release"
        );
        assert_eq!(
            observation
                .expect("close bounded")
                .expect("close task")
                .expect_err("held cancellation outlives observation")
                .kind(),
            ErrorKind::Timeout
        );
        assert_eq!(
            held_transactions, 3,
            "no later close mutation starts while cancellation is held"
        );
        assert_eq!(settled_loads, 1, "the admitted read settles once");
        if fail_after_release {
            assert_eq!(writes, 1, "late failed cancellation stops the phase");
        } else {
            assert!(
                drained
                    .expect("drain observation")
                    .expect("late successful cancellation must complete the original close")
                    .is_none()
            );
            assert_eq!(
                writes, 4,
                "two close mutations plus two receiver reclamations; no replay"
            );
        }
    }
}

#[tokio::test]
async fn custom_close_final_delete_timeout_remains_recoverable_without_replay() {
    use agents_kit::error::{DeadlineKind, ErrorDetail, ErrorKind, ErrorPhase};
    let fixture = CustomFixture::build().await;
    let agent_id = fixture.agent.id();
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("close-delete-timeout"))
        .await
        .expect("workspace");
    let workspace_id = workspace.id();
    let conversation = workspace
        .new_conversation(CustomFixture::conversation_request())
        .await
        .expect("conversation");
    let key = StateKey::new(
        "agents_kit_conversations_v1",
        format!("{workspace_id}:{}", conversation.id()),
    )
    .expect("conversation key");
    fixture
        .faults
        .arm_delete(CommitFault::TimedOutBeforeCommit, key.clone());
    let result = tokio::time::timeout(DEADLINE, conversation.close()).await;
    let writes = fixture.faults.write_commit_count();
    let stored = fixture.faults.load(key).await;
    fixture.faults.clear();
    drop(workspace);
    let shutdown = tokio::time::timeout(DEADLINE, fixture.agent.shutdown()).await;
    let reopened = CustomFixture::reopen(&fixture.faults, agent_id).await;
    let workspace = reopened
        .get_workspace(workspace_id)
        .await
        .expect("reopened workspace");
    let recovered = workspace.list_conversations().await;
    let workspace_cleanup = workspace.close().await;
    let reopened_cleanup = reopened.shutdown().await;

    shutdown
        .expect("original shutdown bounded")
        .expect("original shutdown");
    workspace_cleanup.expect("reopened workspace cleanup");
    reopened_cleanup.expect("reopened Agent cleanup");
    let error = result
        .expect("close bounded")
        .expect_err("failed final deletion cannot claim success");
    assert_eq!(error.kind(), ErrorKind::Timeout);
    assert_eq!(error.context().phase, ErrorPhase::StateCommit);
    assert_eq!(
        error.detail(),
        Some(&ErrorDetail::DeadlineElapsed {
            kind: DeadlineKind::Operation,
            timeout: Some(Duration::from_millis(41)),
        })
    );
    assert_eq!(writes, 3, "the final removal is attempted once");
    assert!(stored.expect("stored conversation readable").is_some());
    assert!(recovered.expect("recovered inventory").is_empty());
}

#[tokio::test]
async fn custom_close_retains_knowledge_cleanup_observation_without_restarting_deletion() {
    use agents_kit::error::ErrorKind;
    for fail_after_release in [false, true] {
        let database = Database::connect(DatabaseConfig::lance(LanceConfig::embedded_path(
            "memory://",
        )))
        .await
        .expect("knowledge database");
        let (knowledge, control) = KnowledgeDeleteProbe::new(database);
        let fixture = CustomFixture::build_with_knowledge_database(knowledge).await;
        let workspace = fixture
            .agent
            .new_workspace(fixture.workspace_request("held-close-knowledge"))
            .await
            .expect("workspace");
        let source = KnowledgeSource::text(
            "close-source",
            "Close source",
            "Retain knowledge cleanup ownership.",
        )
        .expect("knowledge source");
        let conversation = workspace
            .new_conversation(
                NewConversation::new(None, None, vec![source]).expect("conversation request"),
            )
            .await
            .expect("conversation");
        let conversation_key = StateKey::new(
            "agents_kit_conversations_v1",
            format!("{}:{}", workspace.id(), conversation.id()),
        )
        .expect("conversation key");
        let obligation_key = StateKey::new(
            "agents_kit_knowledge_forget_v1",
            format!(
                "{}:conversation:{}:{}",
                fixture.agent.id(),
                workspace.id(),
                conversation.id()
            ),
        )
        .expect("forget obligation key");
        control.arm();
        if fail_after_release {
            control.fail_after_release();
        }
        let mut close = tokio::spawn(async move { conversation.close().await });
        let blocked = tokio::time::timeout(DEADLINE, control.wait_until_blocked()).await;
        let admitted_transactions = fixture.faults.transaction_count();
        let obligation_before = fixture.faults.load(obligation_key.clone()).await;
        let conversation_before = fixture.faults.load(conversation_key.clone()).await;
        let observation = tokio::time::timeout(DEADLINE, &mut close).await;
        let outcome_before_release =
            tokio::time::timeout(Duration::from_millis(100), control.wait_for_outcome()).await;
        let held_transactions = fixture.faults.transaction_count();
        control.release();
        let joined_after_release = if observation.is_err() {
            Some(tokio::time::timeout(DEADLINE, &mut close).await)
        } else {
            None
        };
        drop(workspace);
        let shutdown = tokio::time::timeout(DEADLINE, fixture.agent.shutdown()).await;
        let obligation_after = fixture.faults.load(obligation_key).await;
        let conversation_after = fixture.faults.load(conversation_key).await;
        let calls = control.calls();
        let settled = control.gate.settled.load(Ordering::SeqCst);
        let interrupted = control.gate.interrupted.load(Ordering::SeqCst);

        blocked.expect("knowledge deletion reached its gate");
        shutdown
            .expect("shutdown remains bounded after knowledge release")
            .expect("shutdown joins the original knowledge operation");
        assert!(joined_after_release.is_none());
        assert_eq!(
            observation
                .expect("close observation bounded")
                .expect("close task")
                .expect_err("held knowledge cleanup outlives observation")
                .kind(),
            ErrorKind::Timeout
        );
        assert_eq!(
            held_transactions, admitted_transactions,
            "expiry must not restart the final state mutation while knowledge deletion is held"
        );
        assert!(
            outcome_before_release.is_err(),
            "accepted deletion must remain held until release"
        );
        assert_eq!(
            interrupted, 0,
            "accepted database deletion must not be dropped"
        );
        assert_eq!(settled, 1, "the original deletion reaches its late outcome");
        let obligation_before = obligation_before
            .expect("held obligation is readable")
            .expect("staged forget obligation exists");
        let conversation_before = conversation_before
            .expect("held conversation is readable")
            .expect("staged conversation exists");
        assert_eq!(
            obligation_after.expect("settled obligation is readable"),
            Some(obligation_before),
            "expiry preserves the durable forget obligation for recovery"
        );
        assert_eq!(
            conversation_after.expect("settled conversation is readable"),
            Some(conversation_before),
            "expiry cannot admit the final conversation removal"
        );
        assert_eq!(
            calls, 1,
            "the accepted knowledge deletion must never replay"
        );
    }
}

#[derive(Clone, Copy, Debug)]
enum KnowledgeCleanupFailure {
    Unavailable,
    TimedOut,
    Conflict,
    CommittedTimeout,
}

impl KnowledgeCleanupFailure {
    fn arm(self, store: &FaultStateStore, operation: CloseStorageOperation, invocation: usize) {
        match operation {
            CloseStorageOperation::Commit => store.arm(
                match self {
                    Self::Unavailable => CommitFault::UnavailableBeforeCommit,
                    Self::TimedOut => CommitFault::TimedOutBeforeCommit,
                    Self::Conflict => CommitFault::ConflictBeforeCommit,
                    Self::CommittedTimeout => CommitFault::TimedOutAfterCommit,
                },
                invocation,
            ),
            CloseStorageOperation::Begin
            | CloseStorageOperation::Load
            | CloseStorageOperation::Put
            | CloseStorageOperation::Delete
            | CloseStorageOperation::Rollback => store.arm_operation_fault(
                operation,
                OperationFaultTarget::Invocation(invocation),
                match self {
                    Self::Unavailable => StateStoreError::unavailable(
                        "private-knowledge-operation",
                        "private knowledge dependency failure",
                    ),
                    Self::TimedOut => StateStoreError::timed_out("private-knowledge-operation", 41),
                    Self::Conflict => StateStoreError::transaction_conflict(
                        "private-knowledge-operation",
                        "private knowledge conflict",
                    ),
                    Self::CommittedTimeout => panic!("committed fault requires commit"),
                },
            ),
        }
    }

    fn assert_error(self, error: &KitError, operation: CloseStorageOperation) {
        use agents_kit::error::{DeadlineKind, ErrorDetail, ErrorKind, ErrorPhase};
        assert_eq!(
            error.context().phase,
            match operation {
                CloseStorageOperation::Load => ErrorPhase::StateRead,
                CloseStorageOperation::Commit => ErrorPhase::StateCommit,
                CloseStorageOperation::Rollback => ErrorPhase::Cleanup,
                CloseStorageOperation::Begin
                | CloseStorageOperation::Put
                | CloseStorageOperation::Delete => ErrorPhase::StateWrite,
            },
            "failure={self:?}, method={operation:?}: {error:?}",
        );
        let (kind, detail) = match self {
            Self::Unavailable => (ErrorKind::Integration, ErrorDetail::ServiceUnavailable),
            Self::TimedOut | Self::CommittedTimeout => (
                ErrorKind::Timeout,
                ErrorDetail::DeadlineElapsed {
                    kind: DeadlineKind::Operation,
                    timeout: Some(Duration::from_millis(41)),
                },
            ),
            Self::Conflict => (ErrorKind::Conflict, ErrorDetail::TransactionConflict),
        };
        assert_eq!(error.kind(), kind);
        assert_eq!(error.detail(), Some(&detail));
        let mut source: Option<&(dyn std::error::Error + 'static)> = Some(error);
        while let Some(error) = source {
            for rendered in [format!("{error}"), format!("{error:?}")] {
                for private in [
                    "private-knowledge-operation",
                    "private knowledge dependency failure",
                    "private knowledge conflict",
                    "private-close-timeout",
                    "injected unavailability",
                    "injected conflict",
                ] {
                    assert!(
                        !rendered.contains(private),
                        "lower error escaped: {rendered}"
                    );
                }
            }
            source = error.source();
        }
    }

    async fn sweep(self, operation: CloseStorageOperation) {
        // Each position is an independent fixture and one public close attempt.
        // Exhaust the selected public storage method without fixing its count,
        // transaction order, private namespace, or record encoding.
        let mut surfaced_after_delete = false;
        for invocation in 1..=64 {
            let database = Database::connect(DatabaseConfig::lance(LanceConfig::embedded_path(
                "memory://",
            )))
            .await
            .expect("knowledge database");
            let (knowledge, control) = KnowledgeDeleteProbe::new(database.clone());
            let fixture = CustomFixture::build_with_knowledge_database(knowledge).await;
            let agent_id = fixture.agent.id();
            let workspace = fixture
                .agent
                .new_workspace(fixture.workspace_request("knowledge-error-phase"))
                .await
                .expect("workspace");
            let workspace_id = workspace.id();
            let conversation = workspace
                .new_conversation(
                    NewConversation::new(
                        None,
                        None,
                        vec![
                            KnowledgeSource::text(
                                "phase-source",
                                "Phase source",
                                "Knowledge deletion preserves its detecting storage phase.",
                            )
                            .expect("source"),
                        ],
                    )
                    .expect("conversation request"),
                )
                .await
                .expect("conversation");
            let conversation_id = conversation.id();
            self.arm(&fixture.faults, operation, invocation);
            control.arm();
            control.release();
            let result = tokio::time::timeout(DEADLINE, conversation.close()).await;
            let injected = fixture.faults.state.injected_faults.load(Ordering::SeqCst);
            let deletion_started = control.deletion_started();
            let deletions = control.calls();
            fixture.faults.clear();
            control.release();
            drop(workspace);
            let shutdown = tokio::time::timeout(DEADLINE, fixture.agent.shutdown()).await;

            // Recovery uses the same caller-owned storage/database domains. It
            // is not another faulted close attempt or an implicit Kit retry.
            let (knowledge, _) = KnowledgeDeleteProbe::new(database);
            let reopened = AgentBuilder::reopen(
                CustomFixture::<KnowledgeDeleteProbe>::config(Duration::from_secs(2)),
                AgentStorage::new(fixture.faults.clone()),
                agent_id,
            )
            .with_provider(CustomFixture::<KnowledgeDeleteProbe>::provider())
            .with_knowledge_database(knowledge)
            .build()
            .await;
            let recovery = match reopened {
                Ok(agent) => {
                    let recovery = async {
                        let workspace = agent.get_workspace(workspace_id).await?;
                        let observed = async {
                            let before = workspace.list_conversations().await?;
                            if before.iter().any(|info| info.id() == &conversation_id) {
                                workspace
                                    .get_conversation(conversation_id.clone())
                                    .await?
                                    .close()
                                    .await?;
                            }
                            let after = workspace.list_conversations().await?;
                            Ok::<_, KitError>((before, after))
                        }
                        .await;
                        let cleanup = workspace.close().await;
                        match (observed, cleanup) {
                            (result, Ok(())) => result,
                            (Ok(_), Err(error)) => Err(error),
                            (Err(primary), Err(cleanup)) => {
                                Err(primary.with_supplementary(cleanup))
                            }
                        }
                    }
                    .await;
                    let cleanup = agent.shutdown().await;
                    match (recovery, cleanup) {
                        (result, Ok(())) => result,
                        (Ok(_), Err(error)) => Err(error),
                        (Err(primary), Err(cleanup)) => Err(primary.with_supplementary(cleanup)),
                    }
                }
                Err(error) => Err(error),
            };

            // Check results only after all owned fixtures have been cleaned up.
            let shutdown = shutdown.expect("shutdown bounded");
            let (before, after) = recovery.expect("unfaulted reopen reconciles cleanup");
            assert!(before.iter().all(|info| info.id() == &conversation_id));
            assert!(after.is_empty(), "recovery leaves no closed conversation");
            if deletion_started {
                assert!(
                    before.is_empty(),
                    "durable closing state must recover to deletion"
                );
            }
            assert!(
                deletions <= 1,
                "one close must not replay Database deletion"
            );
            let result = result.expect("close bounded");
            if let Err(error) = &result {
                assert_eq!(
                    injected, 1,
                    "a returned failure must match the injected fault"
                );
                assert_eq!(
                    error.context().operation,
                    agents_kit::error::Operation::CloseConversation,
                );
                self.assert_error(error, operation);
                surfaced_after_delete |= deletion_started;
            }
            if let Err(error) = &shutdown {
                // Notification reclamation can retain the same dependency
                // failure for shutdown. Disarming the injector does not erase
                // that already-observed failure or require replaying cleanup.
                assert_eq!(injected, 1, "unfaulted shutdown must succeed");
                assert_eq!(
                    error.context().operation,
                    agents_kit::error::Operation::Shutdown,
                );
                self.assert_error(error, operation);
            }
            if result.is_ok() {
                assert!(
                    injected == 0
                        || shutdown.is_err()
                        || matches!(self, Self::Conflict | Self::CommittedTimeout)
                        || operation == CloseStorageOperation::Commit,
                    "an injected non-conflict failure must be reported by its owning operation",
                );
                assert!(before.is_empty(), "confirmed close remains terminal");
            }
            if injected == 0 {
                result.expect("untriggered control closes successfully");
                assert!(
                    surfaced_after_delete,
                    "sweep must cover post-Database storage failures"
                );
                return;
            }
            assert_eq!(injected, 1, "each fixture injects one fault");
        }
        panic!("knowledge cleanup fault sweep exceeded its finite bound");
    }
}

#[tokio::test]
async fn custom_knowledge_cleanup_load_unavailability_keeps_read_phase() {
    KnowledgeCleanupFailure::Unavailable
        .sweep(CloseStorageOperation::Load)
        .await;
}

#[tokio::test]
async fn custom_knowledge_cleanup_commit_unavailability_keeps_commit_phase() {
    KnowledgeCleanupFailure::Unavailable
        .sweep(CloseStorageOperation::Commit)
        .await;
}

#[tokio::test]
async fn custom_knowledge_cleanup_timeouts_keep_phase_and_duration() {
    for operation in [
        CloseStorageOperation::Begin,
        CloseStorageOperation::Load,
        CloseStorageOperation::Delete,
        CloseStorageOperation::Commit,
    ] {
        KnowledgeCleanupFailure::TimedOut.sweep(operation).await;
    }
}

#[tokio::test]
async fn custom_knowledge_cleanup_conflicts_keep_transaction_detail() {
    for operation in [CloseStorageOperation::Load, CloseStorageOperation::Commit] {
        KnowledgeCleanupFailure::Conflict.sweep(operation).await;
    }
}

#[tokio::test]
async fn custom_knowledge_cleanup_ambiguous_commit_keeps_recovery_authority() {
    KnowledgeCleanupFailure::CommittedTimeout
        .sweep(CloseStorageOperation::Commit)
        .await;
}

#[tokio::test]
async fn custom_state_classifies_conflicts_and_reconciles_commit_observations() {
    let fixture = CustomFixture::build().await;
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("state-contract-workspace"))
        .await
        .expect("fixture workspace must be created");
    let before = workspace.info().await.expect("workspace info must load");

    fixture.faults.arm(CommitFault::ConflictBeforeCommit, 1);
    let request = DeleteWorkspaceRequest::new(before.id().clone(), before.revision())
        .expect("delete request must be valid");
    assert!(
        matches!(fixture.agent.delete_workspace(request).await, Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::Conflict))
    );
    assert_eq!(fixture.faults.write_commit_count(), 1);
    assert_eq!(
        workspace.info().await.expect("workspace must survive"),
        before
    );

    fixture.faults.arm(CommitFault::UnavailableBeforeCommit, 1);
    let request = DeleteWorkspaceRequest::new(before.id().clone(), before.revision())
        .expect("delete request must be valid");
    assert!(
        matches!(fixture.agent.delete_workspace(request).await, Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::Integration))
    );
    assert_eq!(
        fixture.faults.write_commit_count(),
        1,
        "non-conflicts must not enter the conflict retry path"
    );
    assert_eq!(
        workspace.info().await.expect("workspace must survive"),
        before
    );

    fixture.faults.arm(CommitFault::ConflictBeforeCommit, 2);
    let conversation = tokio::time::timeout(
        DEADLINE,
        workspace.new_conversation(CustomFixture::conversation_request()),
    )
    .await
    .expect("conflict retry must remain bounded")
    .expect("a definite promotion conflict must be retried");
    assert_eq!(
        fixture.faults.write_commit_count(),
        3,
        "creation, conflicted promotion, and one retry are the only writes"
    );
    fixture.faults.clear();
    conversation
        .close()
        .await
        .expect("conflict fixture conversation must close");

    fixture.faults.arm(CommitFault::UnavailableAfterCommit, 2);
    let conversation = tokio::time::timeout(
        DEADLINE,
        workspace.new_conversation(CustomFixture::conversation_request()),
    )
    .await
    .expect("ambiguous commit reconciliation must remain bounded")
    .expect("the durably observed promoted value must win");
    assert_eq!(
        fixture.faults.write_commit_count(),
        2,
        "creation and the ambiguously successful promotion are the only writes"
    );
    fixture.faults.clear();
    conversation
        .close()
        .await
        .expect("reconciled fixture conversation must close");

    let info = workspace.info().await.expect("workspace info must reload");
    drop(workspace);
    fixture
        .agent
        .delete_workspace(
            DeleteWorkspaceRequest::new(info.id().clone(), info.revision())
                .expect("cleanup delete request must be valid"),
        )
        .await
        .expect("fixture workspace cleanup must succeed");
    fixture
        .agent
        .shutdown()
        .await
        .expect("fixture Agent must shut down");
}

#[tokio::test]
async fn custom_workspace_close_preserves_reopen_without_reviving_old_descendants() {
    let fixture = CustomFixture::build().await;
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("workspace-durable-reopen"))
        .await
        .expect("fixture workspace must be created");
    let workspace_id = workspace.id();
    let conversation = workspace
        .new_conversation(CustomFixture::conversation_request())
        .await
        .expect("fixture conversation must be created");
    let conversation_id = conversation.id();

    workspace
        .close()
        .await
        .expect("workspace close must preserve durable descendants");
    assert!(
        matches!(conversation.info().await, Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::Closed))
    );

    let reopened_workspace = fixture
        .agent
        .get_workspace(workspace_id.clone())
        .await
        .expect("durable workspace must reopen in the same Agent runtime");
    reopened_workspace
        .info()
        .await
        .expect("the reopened workspace handle must own the new runtime epoch");
    let reopened_conversation = reopened_workspace
        .get_conversation(conversation_id)
        .await
        .expect("durable conversation must reopen under the reopened workspace");
    assert!(
        matches!(conversation.info().await, Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::Closed)),
        "the pre-close descendant must not revive after durable reopen"
    );

    reopened_conversation
        .close()
        .await
        .expect("reopened conversation must close");
    reopened_workspace
        .close()
        .await
        .expect("reopened workspace must close");
    let durable = fixture
        .agent
        .workspace_info(workspace_id.clone())
        .await
        .expect("closed workspace metadata must remain readable");
    fixture
        .agent
        .delete_workspace(
            DeleteWorkspaceRequest::new(workspace_id, durable.revision())
                .expect("durable workspace deletion must be valid"),
        )
        .await
        .expect("closed durable workspace must remain deletable");
    fixture
        .agent
        .shutdown()
        .await
        .expect("fixture Agent must shut down");
}

#[tokio::test]
async fn custom_receiver_holds_knowledge_until_notification_reclamation() {
    let database = Database::connect(DatabaseConfig::lance(LanceConfig::embedded_path(
        "memory://",
    )))
    .await
    .expect("fixture Lance Database must build");
    let (knowledge, control) = KnowledgeDeleteProbe::new(database);
    let fixture = CustomFixture::build_with_knowledge_database(knowledge).await;
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("knowledge-terminal-drain-workspace"))
        .await
        .expect("fixture workspace must be created");
    let workspace_id = workspace.id();
    let source = KnowledgeSource::text(
        "terminal-drain-source",
        "Terminal drain source",
        "Knowledge must remain durable until notification delivery is reclaimed.",
    )
    .expect("fixture knowledge source must be valid");
    let conversation = workspace
        .new_conversation(
            NewConversation::new(None, None, vec![source])
                .expect("knowledge fixture conversation request must be valid"),
        )
        .await
        .expect("knowledge fixture conversation must be created");
    let conversation_key = StateKey::new(
        "agents_kit_conversations_v1",
        format!("{workspace_id}:{}", conversation.id()),
    )
    .expect("terminal conversation key must be valid");
    let receiver = conversation
        .notification_stream()
        .expect("fixture receiver must be acquired");

    control.arm();
    tokio::time::timeout(DEADLINE, conversation.close())
        .await
        .expect("conversation close must not wait for receiver-owned cleanup")
        .expect("fixture conversation must enter terminal drain");
    assert!(
        !control.deletion_started(),
        "knowledge deletion must not start while notification delivery is retained"
    );

    drop(receiver);
    tokio::time::timeout(DEADLINE, control.wait_until_blocked())
        .await
        .expect("receiver drop must hand off knowledge cleanup");
    assert!(
        fixture
            .faults
            .load(conversation_key.clone())
            .await
            .expect("terminal conversation record must remain readable")
            .is_some(),
        "conversation deletion must wait for knowledge settlement"
    );

    control.release();
    tokio::time::timeout(DEADLINE, async {
        loop {
            if fixture
                .faults
                .load(conversation_key.clone())
                .await
                .expect("terminal conversation record must remain readable")
                .is_none()
            {
                break;
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("terminal conversation deletion must follow knowledge settlement");

    workspace
        .close()
        .await
        .expect("fixture workspace must close");
    let durable = fixture
        .agent
        .workspace_info(workspace_id.clone())
        .await
        .expect("closed workspace metadata must remain readable");
    fixture
        .agent
        .delete_workspace(
            DeleteWorkspaceRequest::new(workspace_id, durable.revision())
                .expect("workspace deletion must be valid"),
        )
        .await
        .expect("fixture workspace must delete");
    fixture
        .agent
        .shutdown()
        .await
        .expect("fixture Agent must shut down");
}

#[tokio::test]
async fn custom_held_receivers_start_terminal_cleanup_time_at_handoff() {
    let lifecycle_timeout = Duration::from_millis(200);
    let fixture = CustomFixture::build_with_lifecycle_timeout(lifecycle_timeout).await;
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("terminal-drain-workspace"))
        .await
        .expect("fixture workspace must be created");
    let workspace_id = workspace.id();
    let dropped_conversation = workspace
        .new_conversation(CustomFixture::conversation_request())
        .await
        .expect("fixture conversation must be created");
    let dropped_receiver = dropped_conversation
        .notification_stream()
        .expect("fixture receiver must be acquired");
    let received_conversation = workspace
        .new_conversation(CustomFixture::conversation_request())
        .await
        .expect("second fixture conversation must be created");
    let mut received_receiver = received_conversation
        .notification_stream()
        .expect("second fixture receiver must be acquired");

    tokio::time::timeout(
        lifecycle_timeout + Duration::from_millis(100),
        pending::<()>(),
    )
    .await
    .expect_err("receiver hold must outlast the cleanup lifecycle budget");

    tokio::time::timeout(DEADLINE, dropped_conversation.close())
        .await
        .expect("conversation close must not wait for caller receipt")
        .expect("fixture conversation must close");
    drop(dropped_receiver);
    tokio::time::timeout(DEADLINE, received_conversation.close())
        .await
        .expect("second conversation close must not wait for caller receipt")
        .expect("second fixture conversation must close");
    assert!(
        tokio::time::timeout(DEADLINE, received_receiver.recv())
            .await
            .expect("terminal receive must remain bounded")
            .is_none(),
        "receiver lifetime must not consume its later cleanup deadline"
    );

    tokio::time::timeout(DEADLINE, async {
        loop {
            if workspace
                .list_conversations()
                .await
                .expect("conversation inventory must remain readable")
                .is_empty()
            {
                break;
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("receiver drop must drive terminal durable reclamation");

    workspace
        .close()
        .await
        .expect("fixture workspace must close");
    let durable = fixture
        .agent
        .workspace_info(workspace_id.clone())
        .await
        .expect("closed workspace metadata must remain readable");
    fixture
        .agent
        .delete_workspace(
            DeleteWorkspaceRequest::new(workspace_id, durable.revision())
                .expect("workspace deletion must be valid"),
        )
        .await
        .expect("fixture workspace must delete");
    fixture
        .agent
        .shutdown()
        .await
        .expect("fixture Agent must shut down");
}

#[tokio::test]
async fn custom_reopen_finishes_a_durably_staged_terminal_close() {
    let fixture = CustomFixture::build().await;
    let agent_id = fixture.agent.id();
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("terminal-recovery-workspace"))
        .await
        .expect("fixture workspace must be created");
    let workspace_id = workspace.id();
    let conversation = workspace
        .new_conversation(CustomFixture::conversation_request())
        .await
        .expect("fixture conversation must be created");
    let conversation_key = StateKey::new(
        "agents_kit_conversations_v1",
        format!("{workspace_id}:{}", conversation.id()),
    )
    .expect("terminal conversation key must be valid");

    fixture
        .faults
        .arm_delete(CommitFault::UnavailableBeforeCommit, conversation_key);
    let error = conversation
        .close()
        .await
        .expect_err("final terminal deletion must fail before commit");
    assert!(matches!(
        (error).kind(),
        agents_kit::error::ErrorKind::Integration
    ));
    fixture.faults.clear();
    drop(workspace);

    let CustomFixture {
        faults,
        agent,
        root: _,
    } = fixture;
    agent
        .shutdown()
        .await
        .expect("fixture Agent must shut down with staged terminal state");
    let reopened = CustomFixture::reopen(&faults, agent_id).await;
    let reopened_workspace = reopened
        .get_workspace(workspace_id.clone())
        .await
        .expect("fixture workspace must reopen");
    assert!(
        reopened_workspace
            .list_conversations()
            .await
            .expect("reopened conversation inventory must load")
            .is_empty(),
        "Custom reopen must finish the staged terminal deletion"
    );
    reopened_workspace
        .close()
        .await
        .expect("reopened workspace must close");
    let durable = reopened
        .workspace_info(workspace_id.clone())
        .await
        .expect("closed workspace metadata must remain readable");
    reopened
        .delete_workspace(
            DeleteWorkspaceRequest::new(workspace_id, durable.revision())
                .expect("workspace deletion must be valid"),
        )
        .await
        .expect("fixture workspace must delete");
    reopened
        .shutdown()
        .await
        .expect("reopened Agent must shut down");
}

#[tokio::test]
async fn custom_shutdown_does_not_extend_a_terminal_cleanup_deadline() {
    let lifecycle_timeout = Duration::from_millis(500);
    let fixture = CustomFixture::build_with_lifecycle_timeout(lifecycle_timeout).await;
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("terminal-shutdown-workspace"))
        .await
        .expect("fixture workspace must be created");
    let conversation = workspace
        .new_conversation(CustomFixture::conversation_request())
        .await
        .expect("fixture conversation must be created");
    let conversation_key = StateKey::new(
        "agents_kit_conversations_v1",
        format!("{}:{}", workspace.id(), conversation.id()),
    )
    .expect("terminal conversation key must be valid");
    let receiver = conversation
        .notification_stream()
        .expect("fixture receiver must be acquired");
    conversation
        .close()
        .await
        .expect("fixture conversation must enter terminal drain");

    fixture
        .faults
        .arm_delete(CommitFault::BlockThenCommit, conversation_key.clone());
    drop(receiver);
    tokio::time::timeout(DEADLINE, fixture.faults.wait_until_blocked())
        .await
        .expect("terminal conversation deletion must reach its injected block");
    let writes_at_delete_block = fixture.faults.write_commit_count();
    tokio::time::timeout(lifecycle_timeout * 3 / 4, pending::<()>())
        .await
        .expect_err("terminal delete must consume most of its original cleanup budget");
    drop(workspace);

    let CustomFixture {
        faults,
        agent,
        root: _,
    } = fixture;
    let result = tokio::time::timeout(lifecycle_timeout * 3 / 4, agent.shutdown())
        .await
        .expect("shutdown must finish within the remaining original cleanup episode");
    assert!(
        matches!(result, Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::Timeout))
    );
    assert_eq!(
        faults.write_commit_count(),
        writes_at_delete_block,
        "shutdown must join the expired delete instead of redispatching it"
    );
    faults.release();
    assert!(
        faults
            .load(conversation_key)
            .await
            .expect("terminal conversation record must remain readable")
            .is_some(),
        "an aborted terminal delete must not commit after shutdown"
    );
}

#[tokio::test]
async fn custom_shutdown_settles_accepted_work_after_waiter_loss() {
    let fixture = CustomFixture::build().await;
    let workspace = fixture
        .agent
        .new_workspace(fixture.workspace_request("waiter-loss-workspace"))
        .await
        .expect("fixture workspace must be created");
    fixture.faults.arm(CommitFault::BlockThenCommit, 2);

    let mut creation = Box::pin(workspace.new_conversation(CustomFixture::conversation_request()));
    tokio::time::timeout(DEADLINE, async {
        tokio::select! {
            () = fixture.faults.wait_until_blocked() => {}
            _ = &mut creation => {
                panic!("accepted creation completed before its injected block");
            }
        }
    })
    .await
    .expect("promotion must block before caller loss");
    drop(creation);
    drop(workspace);

    let CustomFixture {
        faults,
        agent,
        root: _,
    } = fixture;
    let mut shutdown = Box::pin(agent.shutdown());
    let early = poll_fn(|context| match shutdown.as_mut().poll(context) {
        std::task::Poll::Ready(result) => std::task::Poll::Ready(Some(result)),
        std::task::Poll::Pending => std::task::Poll::Ready(None),
    })
    .await;
    assert!(
        early.is_none(),
        "shutdown must retain the accepted owner after waiter loss: {early:?}"
    );

    faults.release();
    tokio::time::timeout(DEADLINE, shutdown)
        .await
        .expect("shutdown must settle retained cleanup before its deadline")
        .expect("shutdown must reconcile accepted work");
}

#[test]
fn public_result_constructors_enforce_portable_invariants() {
    let agent_id: AgentId = "agent_00000000000000000000000000000011"
        .parse()
        .expect("canonical AgentId");
    let workspace_id: WorkspaceId = "workspace_00000000000000000000000000000012"
        .parse()
        .expect("canonical WorkspaceId");
    let conversation_id: ConversationId = "conversation_00000000000000000000000000000013"
        .parse()
        .expect("canonical ConversationId");
    let notification_id: NotificationId = "notification_00000000000000000000000000000014"
        .parse()
        .expect("canonical NotificationId");
    AgentInfo::new(agent_id.clone(), Some("Example agent".to_owned())).expect("valid AgentInfo");
    assert!(
        matches!(AgentInfo::new(agent_id, Some(String::new())), Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::InvalidInput))
    );

    let root = std::env::current_dir().expect("fixture cwd must be available");
    let context = WorkspaceContext::new(
        root.clone(),
        vec![WorkspaceRoot::new(root, WorkspaceRootAccess::ReadWrite).expect("valid root")],
        WorkspaceExecutionPolicy::new(
            WorkspaceSandboxMode::FullAccess,
            WorkspaceNetworkAccess::Disabled,
        ),
    )
    .expect("valid context");
    WorkspaceInfo::new(
        workspace_id.clone(),
        "Example workspace".to_owned(),
        context.clone(),
        WorkspaceDefaults::empty(),
        1,
    )
    .expect("valid WorkspaceInfo");
    assert!(matches!(WorkspaceInfo::new(
            workspace_id.clone(),
            "Example workspace".to_owned(),
            context,
            WorkspaceDefaults::empty(),
            0,
        ), Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::InvalidState)));
    let conversation = ConversationInfo::new(
        conversation_id,
        workspace_id,
        Some("Example conversation".to_owned()),
    )
    .expect("valid ConversationInfo");
    assert_eq!(conversation.title(), Some("Example conversation"));
    assert!(matches!(ConversationInfo::new(
            conversation.id().clone(),
            conversation.workspace_id().clone(),
            Some(" padded".to_owned()),
        ), Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::InvalidInput)));
    let inserted_at = SystemTime::now();
    ConversationNotification::new(
        notification_id.clone(),
        inserted_at,
        ConversationNotificationKind::Message {
            text: "exact message".to_owned(),
        },
    )
    .expect("valid notification");
    assert!(matches!(ConversationNotification::new(
            notification_id,
            inserted_at,
            ConversationNotificationKind::Message {
                text: " \n".to_owned(),
            },
        ), Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::InvalidInput)));
    let text = FileRead::from_bytes(PathBuf::from("notes.txt"), b"ready\n".to_vec())
        .expect("valid FileRead");
    assert_eq!(text.text(), Some("ready\n"));
    assert!(
        matches!(FileRead::from_bytes(PathBuf::from("../escape"), Vec::new()), Err(error) if matches!(error.kind(), agents_kit::error::ErrorKind::InvalidInput))
    );
}

#[test]
fn every_portable_error_category_has_a_public_safe_constructor() {
    let errors = [
        (KitError::invalid_config("invalid config"), "invalid config"),
        (KitError::invalid_input("invalid input"), "invalid input"),
        (KitError::not_found("not found"), "not found"),
        (KitError::conflict("conflict"), "conflict"),
        (KitError::closed("closed"), "closed"),
        (KitError::busy("busy"), "busy"),
        (KitError::cancelled("cancelled"), "cancelled"),
        (KitError::timeout("timeout"), "timeout"),
        (KitError::unsupported("unsupported"), "unsupported"),
        (
            KitError::not_yet_implemented("not yet implemented"),
            "not yet implemented",
        ),
        (KitError::protocol("protocol"), "protocol"),
        (KitError::invalid_state("invalid state"), "invalid state"),
        (KitError::integration("integration"), "integration"),
    ];

    for (error, message) in errors {
        assert_eq!(error.message(), message);
    }
}