summaryrefslogtreecommitdiffhomepage
path: root/packages/api/tests/agent-manager.test.ts
blob: 7d8342ef4385c50c7b1f5bb3c407c11217086a33 (plain)
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
import type { AgentEvent, ToolDefinition } from "@dispatch/core";
import { beforeEach, describe, expect, it, vi } from "vitest";

// Spy on appendEventToChunks so we can assert persistence calls
const appendEventToChunksSpy = vi.fn((_chunks: unknown[], _event: unknown) => {
	// no-op; we inspect calls in tests
});

// Configurable stub for `getMessagesForTab`. Tests can push rows
// before invoking `processMessage` to simulate prior conversation
// history persisted in the DB (model-switch / history-replay path).
interface FakeMessageRow {
	id: string;
	tabId: string;
	seq: number;
	role: "user" | "assistant" | "system";
	chunks: unknown[];
	createdAt: number;
}
const fakeMessagesByTab = new Map<string, FakeMessageRow[]>();
function resetFakeMessages(): void {
	fakeMessagesByTab.clear();
}
function setFakeMessages(tabId: string, rows: FakeMessageRow[]): void {
	fakeMessagesByTab.set(tabId, rows);
}

// Configurable stub for the tabs DB (getTab / listOpenTabs). Tests can seed
// rows to exercise deliverMessage cold-hydration and handle resolution.
interface FakeTabRow {
	id: string;
	title: string;
	keyId: string | null;
	modelId: string | null;
	parentTabId: string | null;
	status: string;
	isOpen: boolean;
	position: number;
	createdAt: number;
	updatedAt: number;
}
const fakeTabs = new Map<string, FakeTabRow>();
function resetFakeTabs(): void {
	fakeTabs.clear();
}
function setFakeTab(row: Partial<FakeTabRow> & { id: string }): void {
	fakeTabs.set(row.id, {
		title: "Tab",
		keyId: null,
		modelId: null,
		parentTabId: null,
		status: "idle",
		isOpen: true,
		position: 0,
		createdAt: 0,
		updatedAt: 0,
		...row,
	});
}
function makeRow(
	tabId: string,
	seq: number,
	role: "user" | "assistant" | "system",
	chunks: unknown[],
): FakeMessageRow {
	return { id: `msg-${tabId}-${seq}`, tabId, seq, role, chunks, createdAt: seq };
}

// Hook into Agent construction so tests can assert what
// `messages` was pre-populated with at the moment `run()` was
// called (after the post-construction pre-populate step in
// `getOrCreateAgentForTab` has had a chance to assign).
//
// We snapshot at `run()` invocation rather than at construction
// because the production code reassigns `agent.messages =
// rows.slice(...)` AFTER `new Agent()` returns — capturing a
// reference at construction would yield a stale empty array.
const constructedAgents: Array<{
	initialMessages: unknown[];
	toolNames: string[];
	systemPrompt: string;
}> = [];
function resetConstructedAgents(): void {
	constructedAgents.length = 0;
}

// Capture the per-call `run()` options (notably reasoningEffort) so tests can
// assert the per-model → per-tab → default effort resolution.
const capturedRunOptions: Array<{ reasoningEffort?: string } | undefined> = [];
function resetCapturedRunOptions(): void {
	capturedRunOptions.length = 0;
}

// Capture every warmCache(history) call so tests can assert the warming replay
// receives the genuine (FULL) history and returns its usage unmodified.
const capturedWarmHistories: unknown[][] = [];
function resetCapturedWarmHistories(): void {
	capturedWarmHistories.length = 0;
}

// Configurable settings store so tests can toggle tool permissions
// (perm_send_to_tab / perm_read_tab / ...) and assert which tools the
// constructed Agent receives. Defaults to empty (getSetting → null).
const fakeSettings = new Map<string, string>();
function resetFakeSettings(): void {
	fakeSettings.clear();
}
function setFakeSetting(key: string, value: string): void {
	fakeSettings.set(key, value);
}

// Capture every appendChunks(tabId, drafts) call so tests can assert what got
// persisted (e.g. usage side-channel rows). The real explodeTurn is mocked to
// return [], so content drafts are empty here; usage rows are pushed directly
// by processMessage's flushAssistant, making them the visible drafts.
interface AppendChunksCall {
	tabId: string;
	drafts: Array<{ turnId: string; step: number; role: string; type: string; data: unknown }>;
}
const appendChunksCalls: AppendChunksCall[] = [];
function resetAppendChunksCalls(): void {
	appendChunksCalls.length = 0;
}

// ── Compaction test scaffolding ────────────────────────────────────
// Fake chunk store (per tab) feeding getChunksForTab / rekeyChunks.
const fakeChunksByTab = new Map<string, Array<{ tabId: string }>>();
// Records of createTab(id, title, opts) and rekeyChunks(from, to) calls.
const createTabCalls: Array<{ id: string; title: string }> = [];
const rekeyCalls: Array<{ from: string; to: string }> = [];
// Configurable buildCompactionRequest result per source tab. Default: a
// compactable conversation (non-empty prompt + a one-message tail).
const fakeCompactionByTab = new Map<
	string,
	{ prompt?: string; tail: Array<{ turnId: string; role: string; chunks: unknown[] }> }
>();
// Configurable registry keys + env-key resolution so resolveConnection can
// succeed in compaction tests. Default empty (preserves existing behaviour).
const fakeRegistryKeys: Array<{
	id: string;
	provider: string;
	base_url: string;
	env?: string;
}> = [];
const fakeApiKeys = new Map<string, string>();
const fakeConfigKeys: Array<{ id: string; provider: string; base_url: string; env?: string }> = [];
function resetCompactionScaffolding(): void {
	fakeRegistryKeys.length = 0;
	fakeApiKeys.clear();
	fakeConfigKeys.length = 0;
	fakeChunksByTab.clear();
	createTabCalls.length = 0;
	rekeyCalls.length = 0;
	fakeCompactionByTab.clear();
}

// Seedable return value for the mocked getUsageStatsForTab — what the backend
// reads (post-write) to attach to the `turn-sealed` event.
const fakeUsageStatsByTab = new Map<string, unknown>();
function resetFakeUsageStats(): void {
	fakeUsageStatsByTab.clear();
}

// Allow tests to swap in a custom `run` generator (e.g. to simulate
// a fallback failure mid-stream). Returning to undefined restores
// the default.
type RunGen = (msg: string) => AsyncGenerator<unknown>;
let runImpl: RunGen | null = null;
function setRunImpl(impl: RunGen | null): void {
	runImpl = impl;
}
async function* defaultRun(_message: string): AsyncGenerator<unknown> {
	yield { type: "status", status: "running" } as const;
	await new Promise<void>((r) => setTimeout(r, 10));
	yield { type: "reasoning-delta", delta: "thinking about it" } as const;
	yield {
		type: "reasoning-end",
		metadata: { anthropic: { signature: "mock-sig" } },
	} as const;
	yield { type: "text-delta", delta: "Hello " } as const;
	yield { type: "text-delta", delta: "world" } as const;
	yield {
		type: "done",
		message: {
			role: "assistant",
			chunks: [
				{
					type: "thinking",
					text: "thinking about it",
					metadata: { anthropic: { signature: "mock-sig" } },
				},
				{ type: "text", text: "Hello world" },
			],
		},
	} as const;
	yield { type: "status", status: "idle" } as const;
}

// Mock @dispatch/core's Agent to avoid real LLM calls
vi.mock("@dispatch/core", () => ({
	Agent: class MockAgent {
		status = "idle";
		messages: unknown[] = [];
		toolNames: string[] = [];
		systemPrompt = "";
		constructor(config: { tools?: Array<{ name: string }>; systemPrompt?: string }) {
			this.toolNames = (config?.tools ?? []).map((t) => t.name);
			this.systemPrompt = config?.systemPrompt ?? "";
		}
		async *run(message: string, options?: { reasoningEffort?: string }): AsyncGenerator<unknown> {
			// Snapshot the post-construction pre-populated message list
			// the first thing `run()` does, before the real `Agent.run`
			// would push the current user message at line 546. Tests
			// inspect this to verify history was loaded correctly.
			constructedAgents.push({
				initialMessages: [...this.messages],
				toolNames: [...this.toolNames],
				systemPrompt: this.systemPrompt,
			});
			capturedRunOptions.push(options);
			if (runImpl) {
				for await (const ev of runImpl(message)) yield ev;
				return;
			}
			for await (const ev of defaultRun(message)) yield ev;
		}
		async warmCache(history: unknown[]) {
			capturedWarmHistories.push([...history]);
			return {
				inputTokens: 1200,
				outputTokens: 1,
				cacheReadTokens: 1100,
				cacheWriteTokens: 0,
			};
		}
	},
	PermissionService: class MockPermissionService {
		ask(_request: unknown, _rulesets: unknown[]) {
			return Promise.resolve("once");
		}
		reply(_id: string, _reply: unknown) {}
		getPending() {
			return [];
		}
	},
	createReadFileTool(_wd: string): ToolDefinition {
		return {
			name: "read_file",
			description: "read a file",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => "mock file content",
		};
	},
	createReadFileSliceTool(_wd: string): ToolDefinition {
		return {
			name: "read_file_slice",
			description: "read a char slice of a single line",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => "mock slice",
		};
	},
	clearSpillForTab(_tabId: string) {},
	createWriteFileTool(_wd: string): ToolDefinition {
		return {
			name: "write_file",
			description: "write a file",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => true,
		};
	},
	createListFilesTool(_wd: string): ToolDefinition {
		return {
			name: "list_files",
			description: "list files",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => ["file1.ts"],
		};
	},
	createLspTool(_getContext: unknown): ToolDefinition {
		return {
			name: "lsp",
			description: "query the language server",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => "mock lsp",
		};
	},
	LspManager: class MockLspManager {
		hasServerForFile() {
			return false;
		}
		async getClients() {
			return [];
		}
		async touchFile() {}
		getDiagnostics() {
			return {};
		}
		async request() {
			return [];
		}
		async shutdownAll() {}
	},
	resolveServersFromConfig(_lsp: unknown) {
		return [];
	},
	reportDiagnostics(_file: string, _issues: unknown) {
		return "";
	},
	createRunShellTool(_wd: string): ToolDefinition {
		return {
			name: "run_shell",
			description: "run shell command",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => ({ stdout: "", stderr: "", exitCode: 0 }),
		};
	},
	loadConfig(_dir: string) {
		return fakeConfigKeys.length > 0
			? { permissions: {}, keys: [...fakeConfigKeys] }
			: { permissions: {} };
	},
	configToRuleset(_config: unknown) {
		return [];
	},
	validateConfig(_config: unknown) {
		return { config: _config, errors: [] };
	},
	createConfigWatcher(_dir: string, _onChange: unknown) {
		return { close() {} };
	},
	watchDirConfig(_dir: string, _onChange: unknown) {
		return { close() {} };
	},
	loadSkills(_dir: string) {
		return { skills: [], mappings: [] };
	},
	createSkillsWatcher(_dir: string, _onChange: unknown) {
		return { close() {} };
	},
	ModelRegistry: class MockModelRegistry {
		getModels() {
			return [];
		}
		getKeys() {
			return fakeRegistryKeys.map((k) => ({ definition: k, status: "active" }));
		}
		getModelsByTag(_tag: string) {
			return [];
		}
		getAllTags() {
			return [];
		}
		hasAvailableKey(_provider: string) {
			return false;
		}
		allKeysExhausted() {
			return true;
		}
		markKeyExhausted() {}
		markKeyActive() {}
		updateConfig() {}
	},
	ModelResolver: class MockModelResolver {
		resolve(_tag: string) {
			return null;
		}
		waitForKey() {
			return Promise.resolve(null);
		}
	},
	TaskList: class MockTaskList {
		private tasks: Array<{ id: string; content: string; status: string }> = [];
		getTasks() {
			return this.tasks.map((t) => ({ ...t }));
		}
		setTasks(items: Array<{ content: string; status?: string }>) {
			this.tasks = items.map((item, i) => ({
				id: `task-${i + 1}`,
				content: item.content,
				status: item.status ?? "pending",
			}));
			return this.getTasks();
		}
		onChange(_cb: unknown) {
			return () => {};
		}
	},
	createTaskListTool(_taskList: unknown) {
		return {
			name: "todo",
			description: "todo",
			parameters: { _type: "z.ZodObject", shape: {} },
			execute: async () => "mock",
		};
	},
	createSummonTool(_wd: string, _callbacks: unknown) {
		return {
			name: "summon",
			description: "summon",
			parameters: { _type: "z.ZodObject", shape: {} },
			execute: async () => "mock",
		};
	},
	createRetrieveTool(_callbacks: unknown) {
		return {
			name: "retrieve",
			description: "retrieve",
			parameters: { _type: "z.ZodObject", shape: {} },
			execute: async () => "mock",
		};
	},
	// Summon parent-path dependencies. The real implementations load agent
	// definitions from disk; tests only need the summon/retrieve tool entries
	// to appear, so these return empty projections.
	loadAgents() {
		return [];
	},
	toAvailableSubagents() {
		return [];
	},
	toAvailableUserAgents() {
		return [];
	},
	getAgentDirPaths() {
		return [];
	},
	GLOBAL_AGENTS_DIR: "/tmp/global-agents",
	createTab(id: string, title: string) {
		createTabCalls.push({ id, title });
		return { id, title };
	},
	getTab(id: string) {
		return fakeTabs.get(id) ?? null;
	},
	listOpenTabs() {
		return [...fakeTabs.values()].filter((t) => t.isOpen);
	},
	resolveTabPrefix(prefix: string) {
		const sanitized = (prefix ?? "").toLowerCase().replace(/[^0-9a-f-]/g, "");
		if (sanitized.length < 4) return { status: "none" };
		const matches = [...fakeTabs.values()].filter(
			(t) => t.isOpen && t.id.toLowerCase().startsWith(sanitized),
		);
		if (matches.length === 0) return { status: "none" };
		if (matches.length === 1) return { status: "ok", tab: matches[0] };
		return { status: "ambiguous", matches };
	},
	shortestUniquePrefix(id: string) {
		return (id ?? "").slice(0, 4);
	},
	createSendToTabTool(_callbacks: unknown): ToolDefinition {
		return {
			name: "send_to_tab",
			description: "send to tab",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => "mock",
		};
	},
	createReadTabTool(_callbacks: unknown): ToolDefinition {
		return {
			name: "read_tab",
			description: "read tab",
			parameters: { _type: "z.ZodObject", shape: {} } as unknown as ToolDefinition["parameters"],
			execute: async () => "mock",
		};
	},
	getClaudeAccountsFromDB() {
		return [];
	},
	refreshAccountCredentials() {
		return null;
	},
	refreshAccountCredentialsAsync() {
		return Promise.resolve(null);
	},
	resolveApiKey(keyId: string) {
		return fakeApiKeys.get(keyId) ?? null;
	},
	getSetting(key: string) {
		return fakeSettings.get(key) ?? null;
	},
	isReasoningEffort(value: unknown) {
		return (
			typeof value === "string" && ["none", "low", "medium", "high", "xhigh", "max"].includes(value)
		);
	},
	appendChunks(tabId: string, drafts: AppendChunksCall["drafts"]) {
		appendChunksCalls.push({ tabId, drafts: [...drafts] });
		return [];
	},
	explodeUserText() {
		return [];
	},
	explodeTurn() {
		return [{ turnId: "t", step: 0, role: "assistant", type: "text", data: { text: "" } }];
	},
	getChunksForTab(tabId: string) {
		return fakeChunksByTab.get(tabId) ?? [];
	},
	groupRowsToMessages(rows: Array<{ tabId: string }>) {
		return rows;
	},
	rekeyChunks(from: string, to: string) {
		rekeyCalls.push({ from, to });
		const rows = fakeChunksByTab.get(from) ?? [];
		fakeChunksByTab.set(to, rows);
		fakeChunksByTab.delete(from);
		return rows.length;
	},
	buildCompactionRequest(input: { messages: unknown[] }) {
		// Resolve the seeded result by matching the first row's tabId.
		const first = (input.messages as Array<{ tabId?: string }>)[0];
		const tabId = first?.tabId ?? "";
		const seeded = fakeCompactionByTab.get(tabId);
		if (seeded) return { head: [], tail: seeded.tail, prompt: seeded.prompt };
		return {
			head: [],
			tail: [{ turnId: "tail-turn", role: "user", chunks: [{ type: "text", text: "recent" }] }],
			prompt: "SUMMARY PROMPT",
		};
	},
	buildSummaryTurnText(summary: string) {
		return `[CONVERSATION SUMMARY]\n\n${summary}`;
	},
	getMessagesForTab(tabId: string) {
		return fakeMessagesByTab.get(tabId) ?? [];
	},
	getUsageStatsForTab(tabId: string) {
		return fakeUsageStatsByTab.get(tabId) ?? null;
	},
	appendEventToChunks: appendEventToChunksSpy,
	applySystemEvent(_messages: unknown[], _event: unknown) {
		return { messageId: "mock-system-msg" };
	},
	BackgroundShellStore: class MockBackgroundShellStore {
		has() {
			return false;
		}
		getResult() {
			return Promise.resolve({ status: "error", error: "not found" });
		}
	},
	BackgroundTranscriptStore: class MockBackgroundTranscriptStore {
		has() {
			return false;
		}
		getResult() {
			return Promise.resolve({ status: "error", error: "not found" });
		}
	},
	createWebSearchTool() {
		return {
			name: "web_search",
			description: "web search",
			parameters: { _type: "z.ZodObject", shape: {} },
			execute: async () => "mock",
		};
	},
	createKeyUsageTool(_callbacks: unknown) {
		return {
			name: "key_usage",
			description: "key usage",
			parameters: { _type: "z.ZodObject", shape: {} },
			execute: async () => "mock",
		};
	},
	createSearchCodeTool(_wd: string) {
		return {
			name: "search_code",
			description: "search code",
			parameters: { _type: "z.ZodObject", shape: {} },
			execute: async () => "mock",
		};
	},
	createYoutubeTranscribeTool() {
		return {
			name: "youtube_transcribe",
			description: "youtube transcribe",
			parameters: { _type: "z.ZodObject", shape: {} },
			execute: async () => "mock",
		};
	},
}));

// Import after mock is defined (Vitest hoists vi.mock automatically)
const { AgentManager } = await import("../src/agent-manager.js");

describe("AgentManager", () => {
	beforeEach(() => {
		resetFakeMessages();
		resetConstructedAgents();
		resetCapturedRunOptions();
		resetFakeTabs();
		resetFakeSettings();
		setRunImpl(null);
		appendEventToChunksSpy.mockClear();
		resetAppendChunksCalls();
		resetFakeUsageStats();
		resetCapturedWarmHistories();
		resetCompactionScaffolding();
	});

	it("initial status is idle", () => {
		const manager = new AgentManager();
		expect(manager.getStatus()).toBe("idle");
	});

	it("initial messageCount is 0", () => {
		const manager = new AgentManager();
		expect(manager.getMessageCount()).toBe(0);
	});

	it("event listeners receive events during processMessage", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((event) => {
			events.push(event);
		});

		await manager.processMessage("tab-1", "test");

		expect(events.length).toBeGreaterThan(0);
		// A turn now opens with `turn-start`, immediately followed by the
		// agent's `status: running`.
		expect(events[0]).toMatchObject({ type: "turn-start" });
		expect(events[1]).toMatchObject({ type: "status", status: "running" });

		// A turn now closes with `turn-sealed` (emitted after the DB write, which
		// is after the agent's final `status: idle`).
		const lastEvent = events[events.length - 1];
		expect(lastEvent).toMatchObject({ type: "turn-sealed" });
		expect(events.some((e) => e.type === "status" && e.status === "idle")).toBe(true);

		const doneEvent = events.find((e) => e.type === "done");
		expect(doneEvent).toBeDefined();
	});

	it("emits a turn-start with a turnId before any content event", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((event) => {
			events.push(event);
		});

		await manager.processMessage("tab-turnstart", "go");

		const turnStartIdx = events.findIndex((e) => e.type === "turn-start");
		expect(turnStartIdx).toBeGreaterThanOrEqual(0);
		const turnStart = events[turnStartIdx] as Extract<AgentEvent, { type: "turn-start" }>;
		expect(typeof turnStart.turnId).toBe("string");
		expect(turnStart.turnId.length).toBeGreaterThan(0);

		// Must precede the first content delta.
		const firstContentIdx = events.findIndex(
			(e) => e.type === "text-delta" || e.type === "reasoning-delta",
		);
		expect(firstContentIdx).toBeGreaterThan(turnStartIdx);
	});

	it("emits text-delta events during processMessage", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((event) => {
			events.push(event);
		});

		await manager.processMessage("tab-1", "hello");

		const textDeltas = events.filter((e) => e.type === "text-delta");
		expect(textDeltas.length).toBeGreaterThan(0);
	});

	it("messageCount increments after processMessage", async () => {
		const manager = new AgentManager();
		await manager.processMessage("tab-1", "hello");
		expect(manager.getMessageCount()).toBe(1);
		await manager.processMessage("tab-1", "world");
		expect(manager.getMessageCount()).toBe(2);
	});

	it("status returns to idle after processMessage completes", async () => {
		const manager = new AgentManager();
		await manager.processMessage("tab-1", "test");
		expect(manager.getStatus()).toBe("idle");
	});

	it("unsubscribe removes listener", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		const unsubscribe = manager.onEvent((event) => {
			events.push(event);
		});

		unsubscribe();
		await manager.processMessage("tab-1", "test");

		expect(events.length).toBe(0);
	});

	it("multiple listeners all receive events", async () => {
		const manager = new AgentManager();
		const listener1 = vi.fn();
		const listener2 = vi.fn();

		manager.onEvent(listener1);
		manager.onEvent(listener2);

		await manager.processMessage("tab-1", "test");

		expect(listener1).toHaveBeenCalled();
		expect(listener2).toHaveBeenCalled();
	});

	// ─── per-model reasoning effort precedence ───────────────────────

	describe("reasoning effort precedence (per-model → per-tab → default)", () => {
		it("uses the per-model effort over the per-tab selector for that fallback entry", async () => {
			const manager = new AgentManager();
			// Agent definition supplies a fallback chain where each entry has its
			// own configured effort; the per-tab selector ("low") must NOT win.
			await manager.processMessage(
				"tab-effort-permodel",
				"go",
				"key-a",
				"model-a",
				"low",
				undefined,
				[{ key_id: "key-a", model_id: "model-a", effort: "xhigh" }],
			);
			expect(capturedRunOptions.at(-1)?.reasoningEffort).toBe("xhigh");
		});

		it("falls back to the per-tab selector when the model entry has no effort", async () => {
			const manager = new AgentManager();
			await manager.processMessage(
				"tab-effort-tab",
				"go",
				"key-a",
				"model-a",
				"medium",
				undefined,
				[{ key_id: "key-a", model_id: "model-a" }],
			);
			expect(capturedRunOptions.at(-1)?.reasoningEffort).toBe("medium");
		});

		it("passes no effort (Agent applies its default) when neither is set", async () => {
			const manager = new AgentManager();
			await manager.processMessage(
				"tab-effort-default",
				"go",
				"key-a",
				"model-a",
				undefined,
				undefined,
				[{ key_id: "key-a", model_id: "model-a" }],
			);
			expect(capturedRunOptions.at(-1)?.reasoningEffort).toBeUndefined();
		});
	});

	// ─── v6 reasoning-end tests ───────────────────────────────────────

	it("reasoning-end event is broadcast to WS listeners", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((event) => {
			events.push(event);
		});

		await manager.processMessage("tab-reasoning", "think please");

		const reasoningEndEvents = events.filter((e) => e.type === "reasoning-end");
		expect(reasoningEndEvents.length).toBeGreaterThan(0);
		expect(reasoningEndEvents[0]).toMatchObject({
			type: "reasoning-end",
			metadata: { anthropic: { signature: "mock-sig" } },
		});
	});

	it("reasoning-end is passed to appendEventToChunks for persistence", async () => {
		appendEventToChunksSpy.mockClear();
		const manager = new AgentManager();

		await manager.processMessage("tab-persist", "think and persist");

		// Find all calls to appendEventToChunks that received a reasoning-end event
		const reasoningEndCalls = appendEventToChunksSpy.mock.calls.filter(
			([_chunks, event]) => (event as AgentEvent).type === "reasoning-end",
		);
		expect(reasoningEndCalls.length).toBeGreaterThan(0);

		// The event should carry the metadata blob
		const [, reasoningEndEvent] = reasoningEndCalls[0] as [unknown[], AgentEvent];
		expect(reasoningEndEvent).toMatchObject({
			type: "reasoning-end",
			metadata: { anthropic: { signature: "mock-sig" } },
		});
	});

	it("reasoning-end follows reasoning-delta in broadcast order (chunk accumulator ordering)", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((event) => {
			events.push(event);
		});

		await manager.processMessage("tab-ordering", "think in order");

		const types = events.map((e) => e.type);
		const deltaIdx = types.indexOf("reasoning-delta");
		const endIdx = types.indexOf("reasoning-end");

		// Both must be present
		expect(deltaIdx).toBeGreaterThanOrEqual(0);
		expect(endIdx).toBeGreaterThanOrEqual(0);

		// reasoning-end must come AFTER reasoning-delta
		expect(endIdx).toBeGreaterThan(deltaIdx);

		// reasoning-end must come BEFORE any text-delta (reasoning precedes text)
		const textDeltaIdx = types.indexOf("text-delta");
		if (textDeltaIdx >= 0) {
			expect(endIdx).toBeLessThan(textDeltaIdx);
		}
	});

	it("done event includes a thinking chunk with metadata in its message", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((event) => {
			events.push(event);
		});

		await manager.processMessage("tab-done-chunks", "think and respond");

		const doneEvent = events.find((e) => e.type === "done") as
			| Extract<AgentEvent, { type: "done" }>
			| undefined;
		expect(doneEvent).toBeDefined();

		const thinkingChunk = doneEvent?.message.chunks.find((c) => c.type === "thinking");
		expect(thinkingChunk).toBeDefined();
		expect(thinkingChunk).toMatchObject({
			type: "thinking",
			text: "thinking about it",
			metadata: { anthropic: { signature: "mock-sig" } },
		});
	});

	// ─── History pre-population on Agent (re)construction ────────────
	//
	// These tests guard the fix that prior conversation turns survive
	// switching models mid-conversation via the sidebar slider. Without
	// it, a fresh `Agent` is constructed with `messages: []` and the
	// next LLM call sees zero prior context.

	it("pre-populates Agent.messages from DB history when constructing a fresh Agent", async () => {
		const manager = new AgentManager();
		const tabId = "tab-history";

		// Simulate prior conversation in the DB:
		//   u1, a1, u_current
		// (the current turn's user message has already been appended
		//  by `processMessage` before `getOrCreateAgentForTab` runs)
		setFakeMessages(tabId, [
			makeRow(tabId, 0, "user", [{ type: "text", text: "first question" }]),
			makeRow(tabId, 1, "assistant", [{ type: "text", text: "first answer" }]),
			makeRow(tabId, 2, "user", [{ type: "text", text: "follow-up" }]),
		]);

		await manager.processMessage(tabId, "follow-up");

		// Exactly one Agent should have been constructed for this tab,
		// and its messages must be the prior two rows (excluding the
		// current user message — `Agent.run()` pushes that itself).
		expect(constructedAgents.length).toBe(1);
		const inst = constructedAgents[0];
		expect(inst).toBeDefined();
		if (!inst) return;
		const init = inst.initialMessages as Array<{ role: string; chunks: unknown[] }>;
		expect(init.length).toBe(2);
		expect(init[0]).toMatchObject({
			role: "user",
			chunks: [{ type: "text", text: "first question" }],
		});
		expect(init[1]).toMatchObject({
			role: "assistant",
			chunks: [{ type: "text", text: "first answer" }],
		});
	});

	it("leaves messages empty when the DB has only the current turn's user message (first turn)", async () => {
		const manager = new AgentManager();
		const tabId = "tab-first-turn";

		// First-ever turn: DB has only the just-appended user message.
		setFakeMessages(tabId, [makeRow(tabId, 0, "user", [{ type: "text", text: "hello" }])]);

		await manager.processMessage(tabId, "hello");

		expect(constructedAgents.length).toBe(1);
		const inst = constructedAgents[0];
		expect(inst).toBeDefined();
		if (!inst) return;
		// The user message at idx 0 is the current turn — must be excluded.
		expect((inst.initialMessages as unknown[]).length).toBe(0);
	});

	it("excludes a partial assistant trail from a prior fallback attempt", async () => {
		const manager = new AgentManager();
		const tabId = "tab-fallback-partial";

		// Scenario: the agent-mode fallback path. Attempt 1 (Opus) errored
		// mid-stream after flushing some chunks; attempt 2 (DeepSeek) is
		// about to start. DB looks like:
		//   u1, a1, u_current, partial_a_attempt1
		// The fresh Agent for attempt 2 must see [u1, a1] — not the
		// current user message and not the failed attempt's partial.
		setFakeMessages(tabId, [
			makeRow(tabId, 0, "user", [{ type: "text", text: "q1" }]),
			makeRow(tabId, 1, "assistant", [{ type: "text", text: "a1" }]),
			makeRow(tabId, 2, "user", [{ type: "text", text: "q2" }]),
			makeRow(tabId, 3, "assistant", [{ type: "text", text: "half-baked..." }]),
		]);

		await manager.processMessage(tabId, "q2");

		expect(constructedAgents.length).toBe(1);
		const inst = constructedAgents[0];
		expect(inst).toBeDefined();
		if (!inst) return;
		const init = inst.initialMessages as Array<{ role: string; chunks: unknown[] }>;
		expect(init.length).toBe(2);
		expect(init[0]).toMatchObject({ role: "user", chunks: [{ type: "text", text: "q1" }] });
		expect(init[1]).toMatchObject({ role: "assistant", chunks: [{ type: "text", text: "a1" }] });
	});

	it("preserves system-role rows in pre-populated history (toModelMessages filters them later)", async () => {
		const manager = new AgentManager();
		const tabId = "tab-with-system-rows";

		setFakeMessages(tabId, [
			makeRow(tabId, 0, "user", [{ type: "text", text: "q1" }]),
			makeRow(tabId, 1, "assistant", [{ type: "text", text: "a1" }]),
			makeRow(tabId, 2, "system", [
				{ type: "system", kind: "config-reload", text: "Configuration reloaded" },
			]),
			makeRow(tabId, 3, "user", [{ type: "text", text: "q2" }]),
		]);

		await manager.processMessage(tabId, "q2");

		expect(constructedAgents.length).toBe(1);
		const inst = constructedAgents[0];
		expect(inst).toBeDefined();
		if (!inst) return;
		const init = inst.initialMessages as Array<{ role: string; chunks: unknown[] }>;
		// All three prior rows (user/assistant/system) preserved; the
		// LLM-facing `toModelMessages` strips the system row later.
		expect(init.length).toBe(3);
		expect(init[2]).toMatchObject({ role: "system" });
	});

	it("survives a getMessagesForTab failure without crashing (messages stays empty)", async () => {
		const manager = new AgentManager();
		const tabId = "tab-db-error";

		// Simulate DB error by stubbing the fake-store getter to throw
		// for this specific tab. We use a Proxy on the Map's get method
		// for the duration of one call.
		const realGet = fakeMessagesByTab.get.bind(fakeMessagesByTab);
		fakeMessagesByTab.get = ((key: string) => {
			if (key === tabId) throw new Error("simulated DB error");
			return realGet(key);
		}) as typeof fakeMessagesByTab.get;

		try {
			await expect(manager.processMessage(tabId, "anything")).resolves.toBeUndefined();
		} finally {
			fakeMessagesByTab.get = realGet;
		}

		// Agent still constructed, just with empty messages.
		expect(constructedAgents.length).toBe(1);
		const inst = constructedAgents[0];
		expect(inst).toBeDefined();
		if (!inst) return;
		expect((inst.initialMessages as unknown[]).length).toBe(0);
	});

	it("reloads history on every Agent reconstruction (simulated model switch)", async () => {
		const manager = new AgentManager();
		const tabId = "tab-model-switch";

		// Turn 1: empty DB → just the first user message.
		setFakeMessages(tabId, [makeRow(tabId, 0, "user", [{ type: "text", text: "q1" }])]);
		await manager.processMessage(tabId, "q1", "key-opus", "claude-opus-4-7");

		// Turn 2: DB now has the full prior turn + new user message.
		// User has switched models via the sidebar slider — different
		// (keyId, modelId) triggers Agent invalidation and reconstruction.
		setFakeMessages(tabId, [
			makeRow(tabId, 0, "user", [{ type: "text", text: "q1" }]),
			makeRow(tabId, 1, "assistant", [{ type: "text", text: "a1" }]),
			makeRow(tabId, 2, "user", [{ type: "text", text: "q2" }]),
		]);
		await manager.processMessage(tabId, "q2", "key-deepseek", "deepseek-v3");

		// Exactly two Agents constructed across the two turns (the
		// invalidation gate fires when keyId/modelId change).
		expect(constructedAgents.length).toBe(2);

		// Second Agent (the DeepSeek one) was pre-populated with the
		// completed first turn — not empty, not duplicating q2.
		const second = constructedAgents[1];
		expect(second).toBeDefined();
		if (!second) return;
		const init = second.initialMessages as Array<{ role: string; chunks: unknown[] }>;
		expect(init.length).toBe(2);
		expect(init[0]).toMatchObject({ role: "user", chunks: [{ type: "text", text: "q1" }] });
		expect(init[1]).toMatchObject({ role: "assistant", chunks: [{ type: "text", text: "a1" }] });
	});

	// ─── getAllStatuses snapshot shape (for browser-reopen restore) ────
	//
	// The snapshot enriches the legacy `Record<string, AgentStatus>` shape
	// with per-tab in-flight context so a fresh frontend can render the
	// streaming assistant message correctly after a reload.

	it("getAllStatuses returns an empty record when no tabs are tracked", () => {
		const manager = new AgentManager();
		expect(manager.getAllStatuses()).toEqual({});
	});

	it("getAllStatuses returns { status } for an idle tab (no currentChunks/currentAssistantId)", async () => {
		const manager = new AgentManager();
		// Drive a full turn so the tab gets registered; default mock run
		// settles back to idle by the time `await` resolves.
		await manager.processMessage("tab-idle", "hi");
		const snap = manager.getAllStatuses();
		expect(snap["tab-idle"]).toBeDefined();
		expect(snap["tab-idle"]?.status).toBe("idle");
		expect(snap["tab-idle"]).not.toHaveProperty("currentChunks");
		expect(snap["tab-idle"]).not.toHaveProperty("currentAssistantId");
	});

	it("getAllStatuses includes currentChunks and currentAssistantId for a running tab", () => {
		const manager = new AgentManager();
		// Reach into the private map to set up a synthetic running state.
		// Justification: there is no public API to enter a sustained
		// "running" state without actually streaming, and we want to
		// assert the snapshot shape — not the streaming pipeline.
		const inner = manager as unknown as {
			tabAgents: Map<
				string,
				{
					agent: null;
					status: "running" | "idle" | "error";
					keyId: null;
					modelId: null;
					taskList: { onChange: (cb: unknown) => void; getTasks: () => unknown[] };
					messageQueue: unknown[];
					queueListeners: unknown[];
					shellStore: unknown;
					transcriptStore: unknown;
					currentChunks: Array<{ type: string; text?: string }> | null;
					currentAssistantId: string | null;
				}
			>;
		};
		inner.tabAgents.set("tab-running", {
			agent: null,
			status: "running",
			keyId: null,
			modelId: null,
			taskList: { onChange: () => {}, getTasks: () => [] },
			messageQueue: [],
			queueListeners: [],
			shellStore: {},
			transcriptStore: {},
			currentChunks: [
				{ type: "thinking", text: "let me think" },
				{ type: "text", text: "partial answer" },
			],
			currentAssistantId: "assistant-msg-id-7",
		});

		const snap = manager.getAllStatuses();
		expect(snap["tab-running"]).toBeDefined();
		expect(snap["tab-running"]?.status).toBe("running");
		expect(snap["tab-running"]?.currentAssistantId).toBe("assistant-msg-id-7");
		expect(snap["tab-running"]?.currentChunks).toEqual([
			{ type: "thinking", text: "let me think" },
			{ type: "text", text: "partial answer" },
		]);
	});

	it("getAllStatuses defensively copies currentChunks (mutating the snapshot doesn't affect the live array)", () => {
		const manager = new AgentManager();
		const inner = manager as unknown as {
			tabAgents: Map<
				string,
				{
					agent: null;
					status: "running";
					keyId: null;
					modelId: null;
					taskList: { onChange: (cb: unknown) => void; getTasks: () => unknown[] };
					messageQueue: unknown[];
					queueListeners: unknown[];
					shellStore: unknown;
					transcriptStore: unknown;
					currentChunks: Array<{ type: string; text?: string }>;
					currentAssistantId: string;
				}
			>;
		};
		const liveChunks = [{ type: "text", text: "live" }];
		inner.tabAgents.set("tab-copy", {
			agent: null,
			status: "running",
			keyId: null,
			modelId: null,
			taskList: { onChange: () => {}, getTasks: () => [] },
			messageQueue: [],
			queueListeners: [],
			shellStore: {},
			transcriptStore: {},
			currentChunks: liveChunks,
			currentAssistantId: "msg-x",
		});

		const snap = manager.getAllStatuses();
		// Mutate the snapshot's array
		snap["tab-copy"]?.currentChunks?.push({ type: "text", text: "polluted" });
		// Live array must be untouched
		expect(liveChunks).toEqual([{ type: "text", text: "live" }]);
	});

	it("getAllStatuses omits currentChunks when a running tab has none yet", () => {
		const manager = new AgentManager();
		const inner = manager as unknown as {
			tabAgents: Map<
				string,
				{
					agent: null;
					status: "running";
					keyId: null;
					modelId: null;
					taskList: { onChange: (cb: unknown) => void; getTasks: () => unknown[] };
					messageQueue: unknown[];
					queueListeners: unknown[];
					shellStore: unknown;
					transcriptStore: unknown;
					currentChunks: null;
					currentAssistantId: null;
				}
			>;
		};
		inner.tabAgents.set("tab-early", {
			agent: null,
			status: "running",
			keyId: null,
			modelId: null,
			taskList: { onChange: () => {}, getTasks: () => [] },
			messageQueue: [],
			queueListeners: [],
			shellStore: {},
			transcriptStore: {},
			currentChunks: null,
			currentAssistantId: null,
		});

		const snap = manager.getAllStatuses();
		expect(snap["tab-early"]?.status).toBe("running");
		expect(snap["tab-early"]).not.toHaveProperty("currentChunks");
		expect(snap["tab-early"]).not.toHaveProperty("currentAssistantId");
	});

	it("getAllStatuses includes a tab's todo list (for reload rehydration)", () => {
		const manager = new AgentManager();
		// Public API: getTaskList creates+returns the tab's list. setTasks is
		// the declarative whole-list write.
		const list = manager.getTaskList("tab-todos");
		list.setTasks([
			{ content: "plan", status: "completed" },
			{ content: "build", status: "in_progress" },
		]);
		const snap = manager.getAllStatuses();
		expect(snap["tab-todos"]?.tasks).toEqual([
			{ id: "task-1", content: "plan", status: "completed" },
			{ id: "task-2", content: "build", status: "in_progress" },
		]);
	});

	it("getAllStatuses omits tasks for a tab with an empty todo list", () => {
		const manager = new AgentManager();
		manager.getTaskList("tab-empty");
		const snap = manager.getAllStatuses();
		expect(snap["tab-empty"]).toBeDefined();
		expect(snap["tab-empty"]).not.toHaveProperty("tasks");
	});

	// ─── Tab-to-tab communication ─────────────────────────────────

	describe("deliverMessage", () => {
		it("starts a new turn when the target tab is idle", async () => {
			const manager = new AgentManager();
			const events: AgentEvent[] = [];
			manager.onEvent((e) => events.push(e));

			const outcome = manager.deliverMessage("tab-idle", "wake up");
			expect(outcome.status).toBe("started");

			// Let the background turn run to completion.
			await new Promise<void>((r) => setTimeout(r, 60));
			expect(events.some((e) => e.type === "text-delta")).toBe(true);
			expect(manager.getTabStatus("tab-idle")).toBe("idle");
		});

		it("queues the message when the target tab is running", () => {
			const manager = new AgentManager();
			const inner = manager as unknown as {
				tabAgents: Map<string, Record<string, unknown>>;
			};
			// Seed a running tab agent directly.
			inner.tabAgents.set("tab-busy", {
				agent: null,
				status: "running",
				keyId: null,
				modelId: null,
				taskList: { onChange: () => {}, getTasks: () => [] },
				messageQueue: [],
				queueListeners: [],
				shellStore: {},
				transcriptStore: {},
				currentChunks: null,
				currentAssistantId: null,
				currentTurnId: null,
			});

			const outcome = manager.deliverMessage("tab-busy", "queued msg");
			expect(outcome.status).toBe("queued");
			if (outcome.status === "queued") {
				expect(typeof outcome.messageId).toBe("string");
			}
			// The message landed on the running tab's queue.
			const agent = inner.tabAgents.get("tab-busy") as { messageQueue: unknown[] };
			expect(agent.messageQueue).toHaveLength(1);
		});

		it("hydrates key/model from the persisted tab row for a cold wake", () => {
			const manager = new AgentManager();
			setFakeTab({ id: "tab-cold", keyId: "persisted-key", modelId: "persisted-model" });

			// Spy on processMessage to capture the key/model deliverMessage
			// forwarded — asserting the hydration decision directly rather than
			// downstream tabAgent state (which the mocked ModelRegistry rewrites).
			const spy = vi.spyOn(manager, "processMessage").mockResolvedValue(undefined);

			const outcome = manager.deliverMessage("tab-cold", "hello");
			expect(outcome.status).toBe("started");

			expect(spy).toHaveBeenCalledTimes(1);
			const args = spy.mock.calls[0] ?? [];
			expect(args[0]).toBe("tab-cold"); // tabId
			expect(args[1]).toBe("hello"); // message
			expect(args[2]).toBe("persisted-key"); // keyId hydrated from row
			expect(args[3]).toBe("persisted-model"); // modelId hydrated from row
		});

		it("prefers explicit opts over the persisted row on a cold wake", () => {
			const manager = new AgentManager();
			setFakeTab({ id: "tab-cold2", keyId: "row-key", modelId: "row-model" });
			const spy = vi.spyOn(manager, "processMessage").mockResolvedValue(undefined);

			manager.deliverMessage("tab-cold2", "hello", {
				keyId: "explicit-key",
				modelId: "explicit-model",
			});

			const args = spy.mock.calls[0] ?? [];
			expect(args[2]).toBe("explicit-key");
			expect(args[3]).toBe("explicit-model");
		});
	});

	describe("deliverMessage — agent auto-wake budget", () => {
		it("allows up to 6 consecutive agent wakes, then suppresses further ones", () => {
			const manager = new AgentManager();
			const spy = vi.spyOn(manager, "processMessage").mockResolvedValue(undefined);

			// 6 agent-originated wakes of an idle tab should all start turns.
			for (let i = 0; i < 6; i++) {
				const outcome = manager.deliverMessage("tab-pp", `msg ${i}`, { origin: "agent" });
				expect(outcome.status).toBe("started");
			}
			expect(spy).toHaveBeenCalledTimes(6);

			// The 7th is suppressed: no new turn, message preserved on the queue.
			const seventh = manager.deliverMessage("tab-pp", "msg 7", { origin: "agent" });
			expect(seventh.status).toBe("suppressed");
			expect(spy).toHaveBeenCalledTimes(6); // unchanged — no wake

			const inner = manager as unknown as {
				tabAgents: Map<string, { messageQueue: unknown[]; autoWakeBudget: number }>;
			};
			const agent = inner.tabAgents.get("tab-pp");
			expect(agent?.autoWakeBudget).toBe(0);
			// Suppressed message is queued, not dropped.
			expect(agent?.messageQueue).toHaveLength(1);
		});

		it("a human message refills the budget and re-enables agent wakes", () => {
			const manager = new AgentManager();
			vi.spyOn(manager, "processMessage").mockResolvedValue(undefined);

			// Exhaust the budget with agent wakes.
			for (let i = 0; i < 6; i++) {
				manager.deliverMessage("tab-refill", `a${i}`, { origin: "agent" });
			}
			expect(manager.deliverMessage("tab-refill", "blocked", { origin: "agent" }).status).toBe(
				"suppressed",
			);

			// A human message refills the budget...
			const humanOutcome = manager.deliverMessage("tab-refill", "human here", {
				origin: "human",
			});
			expect(humanOutcome.status).toBe("started");

			const inner = manager as unknown as {
				tabAgents: Map<string, { autoWakeBudget: number }>;
			};
			expect(inner.tabAgents.get("tab-refill")?.autoWakeBudget).toBe(6);

			// ...so an agent can wake it again.
			expect(manager.deliverMessage("tab-refill", "again", { origin: "agent" }).status).toBe(
				"started",
			);
		});

		it("does not consume budget when the message is merely queued (busy target)", () => {
			const manager = new AgentManager();
			const inner = manager as unknown as {
				tabAgents: Map<string, Record<string, unknown>>;
			};
			inner.tabAgents.set("tab-busy-budget", {
				agent: null,
				status: "running",
				keyId: null,
				modelId: null,
				taskList: { onChange: () => {}, getTasks: () => [] },
				messageQueue: [],
				queueListeners: [],
				shellStore: {},
				transcriptStore: {},
				currentChunks: null,
				currentAssistantId: null,
				currentTurnId: null,
				autoWakeBudget: 6,
			});

			const outcome = manager.deliverMessage("tab-busy-budget", "queued one", {
				origin: "agent",
			});
			expect(outcome.status).toBe("queued");
			// Budget untouched — queuing can't drive a runaway loop.
			const agent = inner.tabAgents.get("tab-busy-budget") as { autoWakeBudget: number };
			expect(agent.autoWakeBudget).toBe(6);
		});

		it("human-originated wakes are never throttled", () => {
			const manager = new AgentManager();
			const spy = vi.spyOn(manager, "processMessage").mockResolvedValue(undefined);

			// Far more than the budget, all human-originated → all start turns.
			for (let i = 0; i < 10; i++) {
				const outcome = manager.deliverMessage("tab-human", `h${i}`, { origin: "human" });
				expect(outcome.status).toBe("started");
			}
			expect(spy).toHaveBeenCalledTimes(10);
		});

		it("defaults origin to human when unspecified (POST /chat path)", () => {
			const manager = new AgentManager();
			const spy = vi.spyOn(manager, "processMessage").mockResolvedValue(undefined);
			for (let i = 0; i < 8; i++) {
				expect(manager.deliverMessage("tab-default", `d${i}`).status).toBe("started");
			}
			expect(spy).toHaveBeenCalledTimes(8);
		});
	});

	describe("queue continuation after a turn ends", () => {
		// A run generator that enqueues `msg` (as if a user/agent sent it mid-turn)
		// exactly once, then streams a normal short reply. Used to simulate a
		// message landing on the queue while the agent is busy.
		function runThatEnqueues(manager: AgentManager, tabId: string, msg: string): RunGen {
			let enqueued = false;
			return async function* () {
				yield { type: "status", status: "running" } as const;
				if (!enqueued) {
					enqueued = true;
					manager.queueMessage(tabId, msg);
				}
				yield { type: "text-delta", delta: "reply" } as const;
				yield {
					type: "done",
					message: { role: "assistant", chunks: [{ type: "text", text: "reply" }] },
				} as const;
				yield { type: "status", status: "idle" } as const;
			};
		}

		it("starts a NEW turn for a message queued during the turn (the bug fix)", async () => {
			const manager = new AgentManager();
			const processSpy = vi.spyOn(manager, "processMessage");
			setRunImpl(runThatEnqueues(manager, "tab-cont", "follow-up question"));

			await manager.processMessage("tab-cont", "first");
			// Let the fire-and-forget continuation turn run to completion.
			await new Promise<void>((r) => setTimeout(r, 50));

			// processMessage called twice: the original turn + the continuation.
			expect(processSpy).toHaveBeenCalledTimes(2);
			expect(processSpy.mock.calls[1]?.[0]).toBe("tab-cont");
			expect(processSpy.mock.calls[1]?.[1]).toBe("follow-up question");

			// Queue is drained and the tab is idle again.
			const inner = manager as unknown as {
				tabAgents: Map<string, { messageQueue: unknown[] }>;
			};
			expect(inner.tabAgents.get("tab-cont")?.messageQueue).toHaveLength(0);
			expect(manager.getTabStatus("tab-cont")).toBe("idle");
		});

		it('emits message-consumed with reason "continuation" when draining between turns', async () => {
			const manager = new AgentManager();
			const events: AgentEvent[] = [];
			manager.onEvent((e) => events.push(e));
			setRunImpl(runThatEnqueues(manager, "tab-evt", "next"));

			await manager.processMessage("tab-evt", "first");
			await new Promise<void>((r) => setTimeout(r, 50));

			const consumed = events.find((e) => e.type === "message-consumed") as
				| (AgentEvent & { reason?: string })
				| undefined;
			expect(consumed).toBeDefined();
			expect(consumed?.reason).toBe("continuation");
		});

		it("does NOT continue when the queue is empty after a clean turn", async () => {
			const manager = new AgentManager();
			const processSpy = vi.spyOn(manager, "processMessage");

			await manager.processMessage("tab-noqueue", "only message");
			await new Promise<void>((r) => setTimeout(r, 30));

			expect(processSpy).toHaveBeenCalledTimes(1); // no continuation
		});

		it("does NOT continue a turn the user stopped (queue is preserved)", async () => {
			const manager = new AgentManager();
			const processSpy = vi.spyOn(manager, "processMessage");
			// Run that enqueues then aborts itself via stopTab to mimic a user stop.
			setRunImpl(async function* () {
				yield { type: "status", status: "running" } as const;
				manager.queueMessage("tab-stop", "should wait");
				manager.stopTab("tab-stop");
				yield {
					type: "done",
					message: { role: "assistant", chunks: [] },
				} as const;
			});

			await manager.processMessage("tab-stop", "go");
			await new Promise<void>((r) => setTimeout(r, 30));

			// Only the original turn ran; the queued message is preserved, unanswered.
			expect(processSpy).toHaveBeenCalledTimes(1);
			const inner = manager as unknown as {
				tabAgents: Map<string, { messageQueue: unknown[] }>;
			};
			expect(inner.tabAgents.get("tab-stop")?.messageQueue).toHaveLength(1);
		});

		it("bounds runaway agent<->agent continuation via the auto-wake budget", async () => {
			const manager = new AgentManager();
			// A run that ALWAYS enqueues another message → would loop forever
			// without the budget cap.
			setRunImpl(async function* () {
				yield { type: "status", status: "running" } as const;
				manager.queueMessage("tab-loop", "again and again");
				yield {
					type: "done",
					message: { role: "assistant", chunks: [{ type: "text", text: "r" }] },
				} as const;
				yield { type: "status", status: "idle" } as const;
			});
			const processSpy = vi.spyOn(manager, "processMessage");

			await manager.processMessage("tab-loop", "kick off");
			await new Promise<void>((r) => setTimeout(r, 120));

			// 1 original + at most MAX_AGENT_AUTO_WAKES (6) continuations = 7.
			// Crucially BOUNDED, not infinite.
			expect(processSpy.mock.calls.length).toBeLessThanOrEqual(7);
			expect(processSpy.mock.calls.length).toBeGreaterThan(1);
			// Budget spent; the last queued message is held, not answered.
			const inner = manager as unknown as {
				tabAgents: Map<string, { autoWakeBudget: number; messageQueue: unknown[] }>;
			};
			expect(inner.tabAgents.get("tab-loop")?.autoWakeBudget).toBe(0);
			expect(inner.tabAgents.get("tab-loop")?.messageQueue.length).toBeGreaterThan(0);
		});
	});

	describe("getLastTabResponse", () => {
		it("returns the most recent assistant turn's text and current status", () => {
			const manager = new AgentManager();
			setFakeMessages("tab-hist", [
				makeRow("tab-hist", 1, "user", [{ type: "text", text: "hi" }]),
				makeRow("tab-hist", 2, "assistant", [{ type: "text", text: "first answer" }]),
				makeRow("tab-hist", 3, "user", [{ type: "text", text: "again" }]),
				makeRow("tab-hist", 4, "assistant", [
					{ type: "text", text: "second " },
					{ type: "text", text: "answer" },
				]),
			]);

			const res = manager.getLastTabResponse("tab-hist");
			expect(res.text).toBe("second answer");
			expect(res.status).toBe("idle");
		});

		it("returns null text when the tab has no assistant turn yet", () => {
			const manager = new AgentManager();
			setFakeMessages("tab-empty", [
				makeRow("tab-empty", 1, "user", [{ type: "text", text: "hi" }]),
			]);
			const res = manager.getLastTabResponse("tab-empty");
			expect(res.text).toBeNull();
		});

		it("skips assistant turns that contain no text chunks", () => {
			const manager = new AgentManager();
			setFakeMessages("tab-toolonly", [
				makeRow("tab-toolonly", 1, "assistant", [{ type: "text", text: "real answer" }]),
				// A later assistant turn with only non-text chunks should be skipped.
				makeRow("tab-toolonly", 2, "assistant", [{ type: "thinking", text: "hmm" }]),
			]);
			const res = manager.getLastTabResponse("tab-toolonly");
			expect(res.text).toBe("real answer");
		});
	});

	describe("send_to_tab / read_tab permission split", () => {
		// Drives the real parent-path tool construction in getOrCreateAgentForTab
		// by toggling the new split permissions and inspecting which tools the
		// constructed Agent received.
		async function toolsForPerms(tabId: string, perms: Record<string, string>): Promise<string[]> {
			for (const [k, v] of Object.entries(perms)) setFakeSetting(k, v);
			const manager = new AgentManager();
			await manager.processMessage(tabId, "go");
			return constructedAgents.at(-1)?.toolNames ?? [];
		}

		it("grants only send_to_tab when only perm_send_to_tab is allowed", async () => {
			const tools = await toolsForPerms("tab-send-only", { perm_send_to_tab: "allow" });
			expect(tools).toContain("send_to_tab");
			expect(tools).not.toContain("read_tab");
		});

		it("grants only read_tab when only perm_read_tab is allowed", async () => {
			const tools = await toolsForPerms("tab-read-only", { perm_read_tab: "allow" });
			expect(tools).toContain("read_tab");
			expect(tools).not.toContain("send_to_tab");
		});

		it("grants both when both permissions are allowed", async () => {
			const tools = await toolsForPerms("tab-both", {
				perm_send_to_tab: "allow",
				perm_read_tab: "allow",
			});
			expect(tools).toContain("send_to_tab");
			expect(tools).toContain("read_tab");
		});

		it("grants neither when both permissions are off", async () => {
			const tools = await toolsForPerms("tab-neither", {});
			expect(tools).not.toContain("send_to_tab");
			expect(tools).not.toContain("read_tab");
		});
	});

	describe("search_code permission gating", () => {
		// Reuses the parent-path tool construction to confirm the perm flag wires
		// the search_code tool on/off correctly.
		async function toolsForPerms(tabId: string, perms: Record<string, string>): Promise<string[]> {
			for (const [k, v] of Object.entries(perms)) setFakeSetting(k, v);
			const manager = new AgentManager();
			await manager.processMessage(tabId, "go");
			return constructedAgents.at(-1)?.toolNames ?? [];
		}

		it("grants search_code when perm_search_code is allowed", async () => {
			const tools = await toolsForPerms("tab-cs-on", { perm_search_code: "allow" });
			expect(tools).toContain("search_code");
		});

		it("omits search_code when perm_search_code is not allowed", async () => {
			const tools = await toolsForPerms("tab-cs-off", {});
			expect(tools).not.toContain("search_code");
		});
	});

	describe("summon / user_agent permission split", () => {
		// Drives the real parent-path tool construction in
		// getOrCreateAgentForTab by toggling perm_summon and perm_user_agent
		// independently, then inspecting which tools the constructed Agent
		// received. The summon tool must be registered when EITHER permission
		// is granted; `retrieve` rides with the subagent permission only
		// (user agents are fire-and-forget).
		async function toolsForPerms(tabId: string, perms: Record<string, string>): Promise<string[]> {
			for (const [k, v] of Object.entries(perms)) setFakeSetting(k, v);
			const manager = new AgentManager();
			await manager.processMessage(tabId, "go");
			return constructedAgents.at(-1)?.toolNames ?? [];
		}

		it("grants summon + retrieve when only perm_summon is allowed", async () => {
			const tools = await toolsForPerms("tab-summon-only", { perm_summon: "allow" });
			expect(tools).toContain("summon");
			expect(tools).toContain("retrieve");
		});

		it("grants summon WITHOUT retrieve when only perm_user_agent is allowed", async () => {
			// Regression: granting only the user-agent permission used to leave
			// the agent unable to summon user agents because the whole summon
			// tool was gated behind perm_summon.
			const tools = await toolsForPerms("tab-user-agent-only", { perm_user_agent: "allow" });
			expect(tools).toContain("summon");
			expect(tools).not.toContain("retrieve");
		});

		it("grants summon + retrieve when both permissions are allowed", async () => {
			const tools = await toolsForPerms("tab-summon-both", {
				perm_summon: "allow",
				perm_user_agent: "allow",
			});
			expect(tools).toContain("summon");
			expect(tools).toContain("retrieve");
		});

		it("grants neither summon nor retrieve when both permissions are off", async () => {
			const tools = await toolsForPerms("tab-summon-neither", {});
			expect(tools).not.toContain("summon");
			expect(tools).not.toContain("retrieve");
		});
	});

	describe("key_usage permission gate", () => {
		// The key_usage tool is conditionally useful, so it must be COMPLETELY
		// absent from the toolset (and thus the model's context) unless
		// perm_key_usage is explicitly allowed.
		async function toolsForPerms(tabId: string, perms: Record<string, string>): Promise<string[]> {
			for (const [k, v] of Object.entries(perms)) setFakeSetting(k, v);
			const manager = new AgentManager();
			await manager.processMessage(tabId, "go");
			return constructedAgents.at(-1)?.toolNames ?? [];
		}

		it("registers key_usage when perm_key_usage is allowed", async () => {
			const tools = await toolsForPerms("tab-key-usage-on", { perm_key_usage: "allow" });
			expect(tools).toContain("key_usage");
		});

		it("omits key_usage when perm_key_usage is not allowed", async () => {
			const tools = await toolsForPerms("tab-key-usage-off", {});
			expect(tools).not.toContain("key_usage");
		});
	});

	// Regression: granted tab-messaging tools must also be ADVERTISED in the
	// agent's system prompt. The tools were registered in the API tool payload
	// but `buildSystemPrompt` filtered its "You have access to the following
	// tools" list through TOOL_DESCRIPTIONS, which lacked send_to_tab/read_tab
	// — so the model was told it didn't have them and refused to use them. This
	// locks the prompt's capability list to the granted toolset.
	describe("send_to_tab / read_tab system-prompt advertisement", () => {
		async function promptForPerms(tabId: string, perms: Record<string, string>): Promise<string> {
			for (const [k, v] of Object.entries(perms)) setFakeSetting(k, v);
			const manager = new AgentManager();
			await manager.processMessage(tabId, "go");
			return constructedAgents.at(-1)?.systemPrompt ?? "";
		}

		it("lists send_to_tab in the system prompt when granted", async () => {
			const prompt = await promptForPerms("tab-prompt-send", { perm_send_to_tab: "allow" });
			expect(prompt).toContain("- send_to_tab:");
			expect(prompt).not.toContain("- read_tab:");
		});

		it("lists read_tab in the system prompt when granted", async () => {
			const prompt = await promptForPerms("tab-prompt-read", { perm_read_tab: "allow" });
			expect(prompt).toContain("- read_tab:");
			expect(prompt).not.toContain("- send_to_tab:");
		});

		it("lists both tab-messaging tools when both are granted", async () => {
			const prompt = await promptForPerms("tab-prompt-both", {
				perm_send_to_tab: "allow",
				perm_read_tab: "allow",
			});
			expect(prompt).toContain("- send_to_tab:");
			expect(prompt).toContain("- read_tab:");
		});

		it("omits both from the system prompt when neither is granted", async () => {
			const prompt = await promptForPerms("tab-prompt-neither", {});
			expect(prompt).not.toContain("- send_to_tab:");
			expect(prompt).not.toContain("- read_tab:");
		});

		it("advertises exactly the granted tab tools (prompt list matches schema)", async () => {
			for (const [k, v] of Object.entries({
				perm_send_to_tab: "allow",
				perm_read_tab: "allow",
			})) {
				setFakeSetting(k, v);
			}
			const manager = new AgentManager();
			await manager.processMessage("tab-prompt-match", "go");
			const inst = constructedAgents.at(-1);
			// Every granted tab-messaging tool surfaced in the schema must also be
			// advertised in the prompt, so the model never believes it lacks one.
			for (const name of ["send_to_tab", "read_tab"]) {
				expect(inst?.toolNames).toContain(name);
				expect(inst?.systemPrompt).toContain(`- ${name}:`);
			}
		});
	});

	// ─── Usage side-channel persistence ──────────────────────────────
	//
	// `usage` AgentEvents (one per LLM round-trip) are persisted as invisible
	// `type:"usage"` chunk rows so per-tab token/cache telemetry survives a
	// reload. They ride the SAME atomic appendChunks call as the turn's content
	// rows (one fsync, contiguous seqs). A superseded fallback attempt's usage is
	// discarded with its `chunks` (per-attempt accumulator).
	describe("usage persistence", () => {
		it("writes one usage row per usage event emitted during a turn", async () => {
			const manager = new AgentManager();
			setRunImpl(async function* () {
				yield { type: "status", status: "running" } as const;
				yield {
					type: "usage",
					usage: { inputTokens: 1000, outputTokens: 40, cacheReadTokens: 0, cacheWriteTokens: 900 },
				} as const;
				yield { type: "text-delta", delta: "step two" } as const;
				yield {
					type: "usage",
					usage: {
						inputTokens: 1200,
						outputTokens: 60,
						cacheReadTokens: 1000,
						cacheWriteTokens: 100,
					},
				} as const;
				yield {
					type: "done",
					message: { role: "assistant", chunks: [{ type: "text", text: "step two" }] },
				} as const;
				yield { type: "status", status: "idle" } as const;
			});

			await manager.processMessage("tab-usage-rows", "go");

			const usageDrafts = appendChunksCalls
				.flatMap((c) => c.drafts)
				.filter((d) => d.type === "usage");
			expect(usageDrafts).toHaveLength(2);
			// One row per event, role=assistant, step cosmetic (0).
			expect(usageDrafts.every((d) => d.role === "assistant" && d.step === 0)).toBe(true);
			expect(usageDrafts[0]?.data).toEqual({
				inputTokens: 1000,
				outputTokens: 40,
				cacheReadTokens: 0,
				cacheWriteTokens: 900,
			});
			expect(usageDrafts[1]?.data).toEqual({
				inputTokens: 1200,
				outputTokens: 60,
				cacheReadTokens: 1000,
				cacheWriteTokens: 100,
			});
		});

		it("attaches the DB usage aggregate to the turn-sealed event for live reconciliation", async () => {
			const manager = new AgentManager();
			const aggregate = {
				inputTokens: 222,
				outputTokens: 22,
				cacheReadTokens: 100,
				cacheWriteTokens: 5,
				requests: 1,
				last: { inputTokens: 222, outputTokens: 22, cacheReadTokens: 100, cacheWriteTokens: 5 },
			};
			fakeUsageStatsByTab.set("tab-sealed-usage", aggregate);

			const events: AgentEvent[] = [];
			manager.onEvent((event) => {
				events.push(event);
			});

			await manager.processMessage("tab-sealed-usage", "go");

			const sealed = events.find((e) => e.type === "turn-sealed") as
				| Extract<AgentEvent, { type: "turn-sealed" }>
				| undefined;
			expect(sealed).toBeDefined();
			// The aggregate read AFTER the write is carried on the event so the
			// frontend can REPLACE its live cacheStats with the DB truth.
			expect(sealed?.usageStats).toEqual(aggregate);
		});

		it("emits usage rows in the SAME appendChunks call as the turn's content (one atomic write)", async () => {
			const manager = new AgentManager();
			setRunImpl(async function* () {
				yield { type: "status", status: "running" } as const;
				yield { type: "text-delta", delta: "hi" } as const;
				yield {
					type: "usage",
					usage: { inputTokens: 5, outputTokens: 1, cacheReadTokens: 2, cacheWriteTokens: 3 },
				} as const;
				yield {
					type: "done",
					message: { role: "assistant", chunks: [{ type: "text", text: "hi" }] },
				} as const;
				yield { type: "status", status: "idle" } as const;
			});

			await manager.processMessage("tab-usage-atomic", "go");

			// Exactly one appendChunks call carries the usage draft (the flush). The
			// user-message append and any system-row appends carry no usage rows.
			const callsWithUsage = appendChunksCalls.filter((c) =>
				c.drafts.some((d) => d.type === "usage"),
			);
			expect(callsWithUsage).toHaveLength(1);
			expect(callsWithUsage[0]?.tabId).toBe("tab-usage-atomic");
		});

		it("discards a superseded (rate-limited) attempt's usage on fallback", async () => {
			const manager = new AgentManager();
			// Inject a minimal model registry so the rate-limit fallback path is
			// taken (real `processMessage` requires modelRegistry + a resolved
			// keyId + a next fallback entry to retry).
			const markKeyExhausted = vi.fn();
			(
				manager as unknown as {
					modelRegistry: {
						getKeys(): Array<{ definition: Record<string, unknown> }>;
						markKeyExhausted(): void;
					};
				}
			).modelRegistry = {
				getKeys: () => [
					{
						definition: {
							id: "k1",
							provider: "openai-compatible",
							env: "ENV1",
							base_url: "http://x",
						},
					},
					{
						definition: {
							id: "k2",
							provider: "openai-compatible",
							env: "ENV2",
							base_url: "http://y",
						},
					},
				],
				markKeyExhausted,
			};

			let attempt = 0;
			setRunImpl(async function* () {
				attempt++;
				yield { type: "status", status: "running" } as const;
				if (attempt === 1) {
					// Attempt 1 emits usage then rate-limits — its usage must be dropped.
					yield {
						type: "usage",
						usage: { inputTokens: 999, outputTokens: 9, cacheReadTokens: 0, cacheWriteTokens: 0 },
					} as const;
					yield { type: "error", error: "rate limit exceeded (status=429)" } as const;
					return;
				}
				// Attempt 2 succeeds — only its usage should persist.
				yield {
					type: "usage",
					usage: { inputTokens: 222, outputTokens: 22, cacheReadTokens: 100, cacheWriteTokens: 5 },
				} as const;
				yield {
					type: "done",
					message: { role: "assistant", chunks: [{ type: "text", text: "recovered" }] },
				} as const;
				yield { type: "status", status: "idle" } as const;
			});

			const agentModels = [
				{ key_id: "k1", model_id: "m1" },
				{ key_id: "k2", model_id: "m2" },
			];
			await manager.processMessage(
				"tab-usage-fallback",
				"go",
				undefined,
				undefined,
				undefined,
				undefined,
				agentModels,
			);

			expect(attempt).toBe(2); // confirm the fallback retry actually happened
			expect(markKeyExhausted).toHaveBeenCalled();

			const usageDrafts = appendChunksCalls
				.flatMap((c) => c.drafts)
				.filter((d) => d.type === "usage");
			// Only attempt 2's usage survives.
			expect(usageDrafts).toHaveLength(1);
			expect(usageDrafts[0]?.data).toEqual({
				inputTokens: 222,
				outputTokens: 22,
				cacheReadTokens: 100,
				cacheWriteTokens: 5,
			});
		});
	});

	describe("warmCacheForTab (prompt-cache warming)", () => {
		it("returns the warm request usage and forwards the FULL genuine history", async () => {
			const manager = new AgentManager();
			setFakeMessages("tab-warm", [
				makeRow("tab-warm", 1, "user", [{ type: "text", text: "hello" }]),
				makeRow("tab-warm", 2, "assistant", [{ type: "text", text: "hi" }]),
			]);

			const result = await manager.warmCacheForTab("tab-warm");
			expect(result.ok).toBe(true);
			if (result.ok) {
				expect(result.usage).toEqual({
					inputTokens: 1200,
					outputTokens: 1,
					cacheReadTokens: 1100,
					cacheWriteTokens: 0,
				});
			}
			// The genuine history is forwarded UNTRIMMED (both turns), so the
			// replayed prefix matches the next real turn exactly.
			expect(capturedWarmHistories).toHaveLength(1);
			expect(capturedWarmHistories[0]).toHaveLength(2);
		});

		it("does NOT persist anything (no appendChunks for the warm request)", async () => {
			const manager = new AgentManager();
			setFakeMessages("tab-warm-2", [
				makeRow("tab-warm-2", 1, "user", [{ type: "text", text: "hello" }]),
			]);
			await manager.warmCacheForTab("tab-warm-2");
			// Warming must never write chunk rows (history / usage / anything).
			expect(appendChunksCalls).toHaveLength(0);
		});

		it("refuses to warm while the tab is generating", async () => {
			const manager = new AgentManager();
			// Start a turn (status flips to running) but don't await it.
			const running = manager.processMessage("tab-warm-busy", "go");
			// Let the mock run() yield its first running status.
			await new Promise<void>((r) => setTimeout(r, 1));
			const result = await manager.warmCacheForTab("tab-warm-busy");
			expect(result.ok).toBe(false);
			if (!result.ok) expect(result.error).toBe("tab is generating");
			await running;
		});
	});
});

describe("AgentManager.compactTab", () => {
	beforeEach(() => {
		resetFakeMessages();
		resetConstructedAgents();
		resetCapturedRunOptions();
		resetFakeTabs();
		resetFakeSettings();
		setRunImpl(null);
		appendEventToChunksSpy.mockClear();
		resetAppendChunksCalls();
		resetFakeUsageStats();
		resetCompactionScaffolding();
	});

	/** Seed a usable compaction model (config key + registry key + env key). */
	function seedCompactorModel(keyId = "k1", modelId = "m1"): void {
		fakeConfigKeys.push({ id: keyId, provider: "opencode-go", base_url: "https://x", env: "K1" });
		fakeRegistryKeys.push({ id: keyId, provider: "opencode-go", base_url: "https://x", env: "K1" });
		fakeApiKeys.set(keyId, "secret");
		setFakeSetting("compaction_model_key_id", keyId);
		setFakeSetting("compaction_model_id", modelId);
	}

	it("errors when the conversation is too short to compact (empty prompt)", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((e) => events.push(e));

		setFakeTab({ id: "src", title: "Chat" });
		fakeChunksByTab.set("src", [{ tabId: "src" }]);
		// No prompt → nothing to compact.
		fakeCompactionByTab.set("src", { tail: [], prompt: undefined });

		await manager.compactTab("temp", "src");

		const err = events.find((e) => e.type === "compaction-error");
		expect(err).toMatchObject({ type: "compaction-error", tempTabId: "temp", sourceTabId: "src" });
		// No backup tab created, no rekey.
		expect(createTabCalls).toHaveLength(0);
		expect(rekeyCalls).toHaveLength(0);
	});

	it("errors when no compaction model can be resolved", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((e) => events.push(e));

		setFakeTab({ id: "src", title: "Chat", keyId: null, modelId: null });
		fakeChunksByTab.set("src", [{ tabId: "src" }]);
		// Compactable, but no configured model and the tab has no key/model.
		// (no seedCompactorModel call)

		await manager.compactTab("temp", "src");

		expect(events.some((e) => e.type === "compaction-error")).toBe(true);
		expect(rekeyCalls).toHaveLength(0);
	});

	it("refuses to compact while the source tab is running", async () => {
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((e) => events.push(e));

		setFakeTab({ id: "src", title: "Chat" });
		fakeChunksByTab.set("src", [{ tabId: "src" }]);
		// Start a (never-finishing) turn so the tab is "running".
		setRunImpl(async function* () {
			yield { type: "status", status: "running" } as const;
			await new Promise<void>((r) => setTimeout(r, 50));
			yield { type: "status", status: "idle" } as const;
		});
		void manager.processMessage("src", "hi");
		// Give processMessage a tick to flip status to running.
		await new Promise<void>((r) => setTimeout(r, 5));

		await manager.compactTab("temp", "src");
		const err = events.find((e) => e.type === "compaction-error");
		expect(err).toBeDefined();
		expect(rekeyCalls).toHaveLength(0);
	});

	it("happy path: summarizes, relocates history to a backup, re-seeds the source", async () => {
		seedCompactorModel();
		const manager = new AgentManager();
		const events: AgentEvent[] = [];
		manager.onEvent((e) => events.push(e));

		setFakeTab({ id: "src", title: "My Chat", keyId: "k1", modelId: "m1" });
		fakeChunksByTab.set("src", [{ tabId: "src" }, { tabId: "src" }]);
		fakeCompactionByTab.set("src", {
			prompt: "SUMMARY PROMPT",
			tail: [
				{ turnId: "tail-u", role: "user", chunks: [{ type: "text", text: "recent q" }] },
				{ turnId: "tail-a", role: "assistant", chunks: [{ type: "text", text: "recent a" }] },
			],
		});

		await manager.compactTab("temp", "src");

		// started + complete emitted; no error.
		expect(events.some((e) => e.type === "compaction-started")).toBe(true);
		const complete = events.find((e) => e.type === "compaction-complete") as
			| (AgentEvent & { backupTabId: string; backupTitle: string })
			| undefined;
		expect(complete).toBeDefined();
		expect(events.some((e) => e.type === "compaction-error")).toBe(false);

		// A backup tab was created and history was relocated onto it.
		expect(createTabCalls).toHaveLength(1);
		expect(rekeyCalls).toHaveLength(1);
		expect(rekeyCalls[0]).toMatchObject({ from: "src", to: createTabCalls[0]?.id });
		expect(complete?.backupTabId).toBe(createTabCalls[0]?.id);
		expect(complete?.backupTitle).toContain("pre-compaction");

		// The source was re-seeded: appendChunks was called for "src" after rekey
		// (summary turn + preserved tail).
		const srcAppends = appendChunksCalls.filter((c) => c.tabId === "src");
		expect(srcAppends.length).toBeGreaterThanOrEqual(1);
	});

	it("queues messages sent to the source while compaction is in flight", async () => {
		seedCompactorModel();
		const manager = new AgentManager();
		setFakeTab({ id: "src", title: "Chat", keyId: "k1", modelId: "m1" });
		fakeChunksByTab.set("src", [{ tabId: "src" }]);
		fakeCompactionByTab.set("src", {
			prompt: "SUMMARY PROMPT",
			tail: [{ turnId: "t", role: "user", chunks: [{ type: "text", text: "recent" }] }],
		});

		// Make the summary generation slow so we can deliver mid-compaction.
		setRunImpl(async function* () {
			yield { type: "status", status: "running" } as const;
			await new Promise<void>((r) => setTimeout(r, 40));
			yield { type: "text-delta", delta: "summary text" } as const;
			yield { type: "status", status: "idle" } as const;
		});

		const compaction = manager.compactTab("temp", "src");
		await new Promise<void>((r) => setTimeout(r, 10));
		// While compacting, a human message should be QUEUED, not started.
		const result = manager.deliverMessage("src", "hello during compaction");
		expect(result.status).toBe("queued");

		await compaction;
	});
});