agent-framework-core 0.3.0

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

use std::sync::Arc;

use async_trait::async_trait;

use crate::error::Result;
use crate::memory::{ContextProvider, SessionContext};
use crate::types::{Content, Message, Role};

/// Counts tokens for a piece of text. Rust equivalent of upstream
/// `TokenizerProtocol`.
pub trait Tokenizer: Send + Sync {
    /// Count the tokens represented by `text`.
    fn count_tokens(&self, text: &str) -> usize;
}

/// A dependency-free default tokenizer using a ~4-characters-per-token
/// heuristic. Mirrors upstream's `CharacterEstimatorTokenizer`.
#[derive(Debug, Clone, Copy, Default)]
pub struct ApproxTokenizer;

impl Tokenizer for ApproxTokenizer {
    fn count_tokens(&self, text: &str) -> usize {
        text.chars().count().div_ceil(4)
    }
}

/// Sum the token counts of a message's text content (text and reasoning
/// content items) using `tokenizer`.
pub fn count_message_tokens(tokenizer: &dyn Tokenizer, message: &Message) -> usize {
    message
        .contents
        .iter()
        .map(|content| count_content_tokens(tokenizer, content))
        .sum()
}

/// Token cost of a single content item.
///
/// Tool payloads count. They were previously free, because the estimate went
/// through `Content::as_text`, which exposes only text and reasoning — so a
/// completed exchange carrying a megabyte of JSON added nothing to the total
/// and any number of them survived [`TokenBudget`], which exists precisely to
/// keep the request inside the context window. Tool arguments and results are
/// literal text in the request and are counted as such.
///
/// Media is still not counted: an image's cost is provider-specific and is not
/// its base64 length, so charging the data URI would be worse than charging
/// nothing.
fn count_content_tokens(tokenizer: &dyn Tokenizer, content: &Content) -> usize {
    fn value_text(value: &serde_json::Value) -> String {
        match value {
            serde_json::Value::String(s) => s.clone(),
            other => other.to_string(),
        }
    }

    match content {
        Content::Text(t) => tokenizer.count_tokens(&t.text),
        Content::TextReasoning(t) => tokenizer.count_tokens(&t.text),
        Content::FunctionCall(fc) => {
            let arguments = match &fc.arguments {
                Some(crate::types::FunctionArguments::Raw(raw)) => raw.clone(),
                Some(crate::types::FunctionArguments::Object(map)) => {
                    serde_json::to_string(map).unwrap_or_default()
                }
                None => String::new(),
            };
            tokenizer.count_tokens(&fc.name) + tokenizer.count_tokens(&arguments)
        }
        Content::FunctionResult(fr) => {
            let mut total = fr
                .result
                .as_ref()
                .map_or(0, |v| tokenizer.count_tokens(&value_text(v)));
            if let Some(exception) = &fr.exception {
                total += tokenizer.count_tokens(exception);
            }
            total
        }
        _ => 0,
    }
}

/// A strategy that reduces a message list to fit some constraint.
///
/// Compaction never errors on content — it always returns *some* retained
/// subset of `messages`, in original order. Rust equivalent of upstream
/// `CompactionStrategy`.
pub trait CompactionStrategy: Send + Sync {
    /// Return the retained messages (in original order) after compaction.
    fn compact(&self, messages: &[Message], tokenizer: &dyn Tokenizer) -> Vec<Message>;
}

/// Returns the number of leading messages with `Role::system()`.
fn leading_system_count(messages: &[Message]) -> usize {
    messages
        .iter()
        .take_while(|m| m.role == Role::system())
        .count()
}

/// Keep the most recent `max_messages`, always preserving any leading system
/// message(s) at the front. Mirrors upstream's `Truncation` strategy.
#[derive(Debug, Clone, Copy)]
pub struct Truncation {
    pub max_messages: usize,
}

impl Truncation {
    pub fn new(max_messages: usize) -> Self {
        Self { max_messages }
    }
}

impl CompactionStrategy for Truncation {
    fn compact(&self, messages: &[Message], _tokenizer: &dyn Tokenizer) -> Vec<Message> {
        if messages.len() <= self.max_messages {
            return messages.to_vec();
        }
        let sys_count = leading_system_count(messages);
        let mut out: Vec<Message> = messages[..sys_count].to_vec();

        if sys_count >= self.max_messages {
            // The system prefix alone already fills (or exceeds) the budget;
            // keep just the system prefix, truncated to the budget.
            out.truncate(self.max_messages);
            return out;
        }

        let remaining_budget = self.max_messages - sys_count;
        let rest = &messages[sys_count..];
        let start = rest.len().saturating_sub(remaining_budget);
        out.extend_from_slice(&rest[start..]);
        out
    }
}

/// Keep leading system message(s) + the last `window` non-system messages.
/// Mirrors upstream's `SlidingWindow` strategy.
#[derive(Debug, Clone, Copy)]
pub struct SlidingWindow {
    pub window: usize,
}

impl SlidingWindow {
    pub fn new(window: usize) -> Self {
        Self { window }
    }
}

impl CompactionStrategy for SlidingWindow {
    fn compact(&self, messages: &[Message], _tokenizer: &dyn Tokenizer) -> Vec<Message> {
        let sys_count = leading_system_count(messages);
        let mut out: Vec<Message> = messages[..sys_count].to_vec();
        let rest = &messages[sys_count..];
        let start = rest.len().saturating_sub(self.window);
        out.extend_from_slice(&rest[start..]);
        out
    }
}

/// Keep leading system message(s), then walk from the newest message
/// backward accumulating token counts, keeping messages until adding the
/// next would exceed `max_tokens`. Returns the kept messages in original
/// order. Mirrors upstream's `ContextWindow`/token-budget strategy.
#[derive(Debug, Clone, Copy)]
pub struct TokenBudget {
    pub max_tokens: usize,
}

impl TokenBudget {
    pub fn new(max_tokens: usize) -> Self {
        Self { max_tokens }
    }
}

impl CompactionStrategy for TokenBudget {
    fn compact(&self, messages: &[Message], tokenizer: &dyn Tokenizer) -> Vec<Message> {
        let sys_count = leading_system_count(messages);
        let system_prefix = &messages[..sys_count];
        let rest = &messages[sys_count..];

        let mut used: usize = system_prefix
            .iter()
            .map(|m| count_message_tokens(tokenizer, m))
            .sum();

        // Walk from newest to oldest over the non-system tail, keeping
        // messages until adding the next would exceed the budget. The
        // newest non-system message is always kept, even if it alone (plus
        // the system prefix) exceeds the budget — compaction never reduces
        // a non-empty tail to nothing.
        let mut kept_rest: Vec<&Message> = Vec::new();
        for message in rest.iter().rev() {
            let cost = count_message_tokens(tokenizer, message);
            if !kept_rest.is_empty() && used + cost > self.max_tokens {
                break;
            }
            used += cost;
            kept_rest.push(message);
        }
        kept_rest.reverse();

        let mut out: Vec<Message> = system_prefix.to_vec();
        out.extend(kept_rest.into_iter().cloned());
        out
    }
}

/// Whether a message carries any `Content::FunctionResult` (tool-result)
/// content.
fn has_tool_result(message: &Message) -> bool {
    message
        .contents
        .iter()
        .any(|c| matches!(c, Content::FunctionResult(_)))
}

/// Replace the payload of `Content::FunctionResult` (tool-result) content in
/// all but the last `keep_last` messages that carry tool results — they are the
/// bulkiest and least useful once stale. Text and other content is left intact.
///
/// The result content itself is **kept**, with its payload swapped for
/// [`OMITTED_TOOL_RESULT`] — on `result` and, for a failed call, on
/// `exception` too, since provider converters render `exception` *instead of*
/// `result` and leaving it would send the original error verbatim — rather
/// than deleted. Deleting it would leave the
/// matching assistant `tool_calls` entry unanswered, which providers reject
/// outright — so the size win would come at the cost of a 400 on the next
/// request. Mirrors the intent of upstream's `ToolResultCompactionStrategy`,
/// which likewise *replaces* stale tool groups with a compact stand-in instead
/// of removing them; upstream summarizes the group with an LLM, while this
/// port (which has no summarizing strategy) substitutes a fixed marker.
#[derive(Debug, Clone, Copy)]
pub struct SelectiveToolResult {
    pub keep_last: usize,
}

impl SelectiveToolResult {
    pub fn new(keep_last: usize) -> Self {
        Self { keep_last }
    }
}

/// Stand-in payload left in place of a tool result this strategy compacts.
///
/// The result *content* is kept (only its payload is replaced) so the exchange
/// stays paired with its function call — see [`SelectiveToolResult`].
pub const OMITTED_TOOL_RESULT: &str = "[tool result omitted by compaction]";

impl CompactionStrategy for SelectiveToolResult {
    fn compact(&self, messages: &[Message], _tokenizer: &dyn Tokenizer) -> Vec<Message> {
        let tool_result_count = messages.iter().filter(|m| has_tool_result(m)).count();
        let mut strip_budget = tool_result_count.saturating_sub(self.keep_last);

        let mut out = Vec::with_capacity(messages.len());
        for message in messages {
            if has_tool_result(message) && strip_budget > 0 {
                strip_budget -= 1;
                let mut compacted = message.clone();
                for content in &mut compacted.contents {
                    if let Content::FunctionResult(fr) = content {
                        fr.result =
                            Some(serde_json::Value::String(OMITTED_TOOL_RESULT.to_string()));
                        // A failed call carries its payload — often a stack
                        // trace, the bulkiest thing here — in `exception`, and
                        // every provider converter renders `exception` *instead
                        // of* `result`. Replacing only `result` would leave the
                        // original error sent verbatim and the marker ignored,
                        // making this a no-op for exactly the results most worth
                        // compacting. Replaced rather than cleared, so the model
                        // still sees that the call failed.
                        if fr.exception.is_some() {
                            fr.exception = Some(OMITTED_TOOL_RESULT.to_string());
                        }
                    }
                }
                out.push(compacted);
            } else {
                out.push(message.clone());
            }
        }
        out
    }
}

/// Strip function-call / function-result contents that lost their counterpart,
/// dropping any message left empty by the strip.
///
/// Compaction cuts a message list at an arbitrary point, so a strategy can
/// easily retain one half of a tool exchange: [`TokenBudget`] drops an
/// expensive call-bearing message while keeping its cheap result, and
/// [`SelectiveToolResult`] strips old results while their calls stay put.
/// Either half alone is not merely wasteful — it is *invalid*. Providers
/// require every assistant `tool_calls` entry to be answered by a matching
/// tool message and reject a tool message that answers nothing, so an orphan
/// turns the next request into a 400 rather than a slightly worse completion.
///
/// Mirrors the invariant upstream added in "Keep call and result occurrences
/// atomic in compaction" (#7406). Upstream enforces it by linking call and
/// result into one indivisible span before any strategy runs; this port has no
/// span/group model, so it enforces the same invariant as a repair pass over
/// the retained set — the observable guarantee (never emit a half-exchange) is
/// identical.
///
/// Note this *drops* an unmatched call rather than replacing it with a summary
/// the way upstream's `ToolResultCompactionStrategy` does; this port has no
/// LLM-summarizing compaction strategy to build that summary with.
fn drop_orphaned_tool_exchanges(
    messages: &mut Vec<Message>,
    origins: &mut Vec<Option<usize>>,
    pending: &[Message],
) {
    use std::collections::HashSet;

    // Pair by *occurrence*, not by id membership. Call ids are not guaranteed
    // unique across a conversation — providers may reuse one for a later
    // invocation, and some surfaces derive the id from the tool name — so
    // comparing sets of ids would call `[call(c1), result(c1), call(c1)]`
    // balanced and leave the second call unanswered. Each result is instead
    // matched to the oldest still-unanswered call sharing its id; whatever is
    // left unmatched on either side is an orphan.
    let paired: HashSet<Site> = {
        // `pending` is walked as a continuation of `messages` (its indices start
        // past the end) so a call here can pair with a result that only arrives
        // later, but it is never itself modified.
        let (pairs, _) = pair_tool_exchanges(messages.iter().chain(pending.iter()).enumerate());
        pairs
            .into_iter()
            .flat_map(|(call_site, result_site)| [call_site, result_site])
            .collect()
    };

    // Fast path: nothing is orphaned, so the (common) balanced conversation is
    // returned without rebuilding it.
    let has_orphan = messages.iter().enumerate().any(|(mi, message)| {
        message.contents.iter().enumerate().any(|(ci, content)| {
            matches!(
                content,
                Content::FunctionCall(_) | Content::FunctionResult(_)
            ) && !paired.contains(&(mi, ci))
        })
    });
    if !has_orphan {
        return;
    }

    // Rebuilt in place, with `origins` kept index-aligned: a dropped message
    // must drop its recovered position too, or every later ordering decision
    // reads the wrong one.
    let mut mi = 0;
    let mut kept = 0;
    messages.retain_mut(|message| {
        let this = mi;
        mi += 1;
        let had_contents = !message.contents.is_empty();
        let mut ci = 0;
        message.contents.retain(|content| {
            let keep = !matches!(
                content,
                Content::FunctionCall(_) | Content::FunctionResult(_)
            ) || paired.contains(&(this, ci));
            ci += 1;
            keep
        });
        // A message that carried only an orphaned half is dropped outright; one
        // that was already empty is left alone (not this pass's business).
        let keep_message = !(had_contents && message.contents.is_empty());
        if keep_message {
            origins.swap(kept, this);
            kept += 1;
        }
        keep_message
    });
    origins.truncate(kept);
}

/// Ensure compaction never reduces a conversation to system messages alone.
///
/// A budget smaller than the system prefix leaves [`Truncation`] and
/// [`SlidingWindow`] returning only system messages — a "conversation" with no
/// turn for the model to answer, which is useless rather than merely short.
/// Mirrors upstream's `_minimum_retained_group_ids` (#7219): the most recent
/// non-system message is retained even when that pushes the result back over
/// the limit.
///
/// The fallback is stripped of function-call/result contents before being
/// reinstated, since its counterpart is by definition not in the retained set —
/// so this can never manufacture the orphan
/// [`drop_orphaned_tool_exchanges`] exists to remove.
fn ensure_non_system_message(
    original: &[Message],
    mut retained: Vec<Message>,
    origins: &[Option<usize>],
) -> Vec<Message> {
    // A non-system message that renders nothing does not satisfy this: the
    // orphan repair can strip a message down to reasoning alone, which reaches
    // the model as nothing and leaves the request effectively system-only.
    if retained
        .iter()
        .any(|m| m.role != Role::system() && message_renders_on_every_provider(m))
    {
        return retained;
    }
    // Prefer ordinary content: the latest non-system message with something in
    // it besides a tool exchange. Its call/result contents are stripped, since
    // their counterparts are by definition not in the retained set — so this
    // can never manufacture the orphan `drop_orphaned_tool_exchanges` removes.
    let plain = original
        .iter()
        .enumerate()
        .rev()
        // A tool-role message is never standalone content: its text belongs to
        // the exchange. Stripping its result and keeping the text yields a tool
        // message with no `tool_call_id`, which the OpenAI converter drops
        // entirely (leaving the conversation system-only after all) and which
        // Gemini emits as a bare text part under its `function` role. The
        // complete-exchange fallback below handles these properly.
        .filter(|(_, m)| m.role != Role::system() && m.role != Role::tool())
        .find_map(|(index, m)| {
            let mut candidate = m.clone();
            // Reasoning is dropped alongside the exchange halves, not kept as
            // standalone content. It renders nothing on its own — Gemini's
            // request builder deliberately skips reasoning parts, and the
            // OpenAI converter has no mapping for it — so a message reduced to
            // reasoning alone would qualify here, return early, and leave the
            // request effectively system-only while bypassing the
            // complete-exchange fallback below.
            let role = candidate.role.clone();
            candidate.contents.retain(|c| {
                renders_on_every_provider(c)
                    && !matches!(c, Content::FunctionCall(_) | Content::FunctionResult(_))
                    // Same role qualifier as message_renders_on_every_provider:
                    // an assistant image renders nowhere, so it must not make
                    // this candidate look substantive either.
                    && !(matches!(c, Content::Data(_)) && role == Role::assistant())
            });
            (!candidate.contents.is_empty()).then_some((index, candidate))
        });
    if let Some((index, plain)) = plain {
        let position = chronological_position(origins, index);
        retained.insert(position, plain);
        return retained;
    }
    // Nothing but a tool exchange, then — a conversation whose only non-system
    // content is a call and its result. Stripping both halves (as the branch
    // above does) would leave the result system-only after all, so retain the
    // latest *complete* exchange instead: both halves together are valid, and
    // they give the model something to answer.
    if let Some((origin_index, exchange)) = latest_complete_tool_exchange(original) {
        let position = chronological_position(origins, origin_index);
        for (offset, message) in exchange.into_iter().enumerate() {
            retained.insert(position + offset, message);
        }
    }
    retained
}

/// A `(message index, content index)` position within a message list.
type Site = (usize, usize);

/// A call site paired with the result site that answers it.
type ExchangePair = (Site, Site);

/// A call site left unanswered, tagged with its call id.
type UnansweredCall<'a> = (Site, &'a str);

/// Pair each function result with the **nearest preceding** unanswered call
/// sharing its id, walking `messages` in order.
///
/// Returns the paired couples (in result order) and the call sites left
/// unanswered, tagged with their id. Every pairing decision in this module
/// comes from here. There were previously three separate walks of this shape;
/// two took the nearest preceding call and one took the oldest, and they
/// disagreed about which occurrence a result answers when an id is reused.
fn pair_tool_exchanges<'a>(
    messages: impl Iterator<Item = (usize, &'a Message)>,
) -> (Vec<ExchangePair>, Vec<UnansweredCall<'a>>) {
    use std::collections::{HashMap, VecDeque};

    let mut unanswered: HashMap<&str, VecDeque<Site>> = HashMap::new();
    let mut pairs: Vec<ExchangePair> = Vec::new();
    for (mi, message) in messages {
        for (ci, content) in message.contents.iter().enumerate() {
            match content {
                Content::FunctionCall(fc) if !fc.call_id.is_empty() => {
                    unanswered
                        .entry(fc.call_id.as_str())
                        .or_default()
                        .push_back((mi, ci));
                }
                Content::FunctionResult(fr) if !fr.call_id.is_empty() => {
                    if let Some(call_site) = unanswered
                        .get_mut(fr.call_id.as_str())
                        .and_then(VecDeque::pop_back)
                    {
                        pairs.push((call_site, (mi, ci)));
                    }
                }
                _ => {}
            }
        }
    }
    let mut leftover: Vec<UnansweredCall<'_>> = unanswered
        .into_iter()
        .flat_map(|(id, sites)| sites.into_iter().map(move |site| (site, id)))
        .collect();
    leftover.sort_unstable_by_key(|(site, _)| *site);
    (pairs, leftover)
}

/// Recover each retained message's position in `original`.
///
/// Called **before** anything mutates the retained values. The orphan repair
/// strips contents from a message, after which it no longer equals its
/// original, so recovering positions afterwards silently fails to match and
/// every downstream ordering decision falls back to appending.
///
/// Matching runs from the end backwards. Equal messages recur — two identical
/// reasoning steps around an older user turn, say — and a forward greedy match
/// aliases a retained *later* occurrence to the earlier duplicate. Matching from
/// the end assigns the latest position consistent with order, which is the one a
/// suffix-retaining strategy actually kept. A strategy that rewrites messages
/// rather than selecting them matches nothing, and those entries stay `None`.
fn origin_indices(retained: &[Message], original: &[Message]) -> Vec<Option<usize>> {
    let mut assigned: Vec<Option<usize>> = vec![None; retained.len()];
    let mut limit = original.len();
    for index in (0..retained.len()).rev() {
        match original[..limit]
            .iter()
            .rposition(|candidate| *candidate == retained[index])
        {
            Some(found) => {
                assigned[index] = Some(found);
                limit = found;
            }
            None => break,
        }
    }
    assigned
}

/// The index at which content originating at `origin_index` belongs
/// chronologically, given the retained set's recovered `origins`.
///
/// Appending unconditionally reverses the conversation when the retained set
/// holds a *newer* message that failed the rendering check: with
/// `[user("old"), assistant(reasoning)]` the reasoning is kept and the old user
/// turn lands after it, so a provider that does render reasoning (Anthropic)
/// sees a stale request as the latest turn and can answer it a second time.
///
/// Both fallback paths go through this; they previously did not.
fn chronological_position(origins: &[Option<usize>], origin_index: usize) -> usize {
    origins
        .iter()
        .position(|slot| matches!(slot, Some(index) if *index > origin_index))
        .unwrap_or(origins.len())
}

/// The most recent complete call/result pair, as one or two messages reduced to
/// just that pair's contents (one when both halves share a message).
///
/// Returns empty when no result has a preceding unanswered call — there is no
/// complete exchange to reinstate, and a half is worse than nothing.
fn latest_complete_tool_exchange(messages: &[Message]) -> Option<(usize, Vec<Message>)> {
    let (pairs, _) = pair_tool_exchanges(
        messages
            .iter()
            .enumerate()
            .filter(|(_, m)| m.role != Role::system()),
    );
    let ((call_mi, call_ci), (result_mi, result_ci)) = pairs.last().copied()?;
    // A result always pairs with a call that came before it, so emitting the
    // call's message first preserves the original order.
    let messages = if call_mi == result_mi {
        vec![reduced_call_with_signature(
            &messages[call_mi],
            call_ci,
            &[result_ci],
        )]
    } else {
        vec![
            reduced_call_with_signature(&messages[call_mi], call_ci, &[]),
            reduced_to(&messages[result_mi], &[result_ci]),
        ]
    };
    Some((call_mi, messages))
}

/// See [`Content::renders_on_every_provider`] — the contract lives on
/// `Content`, next to the type the converters consume, and is pinned by
/// contract tests in the provider crates. This module used to define it
/// locally and mis-derived it three times (variant-level, then
/// media-type-level, then trusting a declared type over the payload); it now
/// only asks.
fn renders_on_every_provider(content: &Content) -> bool {
    content.renders_on_every_provider()
}

/// Whether `message` carries anything that reaches the model on every
/// provider, **in this message's role**.
///
/// The role qualifier exists for images: providers accept image blocks only in
/// user turns (Bedrock's Converse and Anthropic both reject an assistant image
/// block outright, and the Bedrock converter accordingly skips them), so an
/// assistant message whose only universal content is image data renders
/// nowhere and must not satisfy retention.
fn message_renders_on_every_provider(message: &Message) -> bool {
    message.contents.iter().any(|content| {
        content.renders_on_every_provider()
            && !(matches!(content, Content::Data(_)) && message.role == Role::assistant())
    })
}

/// Clone `message` keeping the function call at `call_ci` (plus any
/// `also_keep` indices), carrying the call's replay signature across.
///
/// Reducing to the call alone drops a Gemini signature sitting on the preceding
/// reasoning content — the supported fallback placement — and that reasoning is
/// not retained either, so the request builder would have nothing to backfill
/// from. Shared by both paths that resurrect a call, which otherwise learn this
/// separately (and one of them hadn't).
fn reduced_call_with_signature(message: &Message, call_ci: usize, also_keep: &[usize]) -> Message {
    let mut keep = vec![call_ci];
    keep.extend_from_slice(also_keep);
    keep.sort_unstable();
    let mut out = reduced_to(message, &keep);
    if let Some(Content::FunctionCall(fc)) = out
        .contents
        .iter_mut()
        .find(|c| matches!(c, Content::FunctionCall(_)))
    {
        if fc.protected_data.is_none() {
            fc.protected_data = preceding_reasoning_signature(message, call_ci);
        }
    }
    out
}

/// Clone `message` keeping only the contents at `keep` (content indices).
fn reduced_to(message: &Message, keep: &[usize]) -> Message {
    let mut out = message.clone();
    let mut index = 0;
    out.contents.retain(|_| {
        let keep_this = keep.contains(&index);
        index += 1;
        keep_this
    });
    out
}

/// Put back a call that the *strategy* dropped but that an incoming result
/// answers.
///
/// Pairing against `pending` stops the repair from stripping a **retained**
/// call, but the strategy runs first and may have excluded the call from
/// `retained` altogether — `SlidingWindow::new(0)` over a history holding
/// `call(c1)` while the run's input holds `result(c1)`, for instance. Nothing
/// downstream can fix that: the call is gone from the retained set, and
/// `pending` is the caller's input, which is not ours to edit. The request
/// would go out with a result answering nothing.
///
/// So the call is reinstated from `original` — reduced to just that content, so
/// no unrelated payload rides back in with it — and appended after the retained
/// messages, which keeps it ahead of the `pending` result that follows.
fn reinstate_calls_answered_by_pending(
    original: &[Message],
    retained: &mut Vec<Message>,
    origins: &mut Vec<Option<usize>>,
    pending: &[Message],
) {
    // Only results that `pending` does not answer *itself*. A run's input can
    // carry a complete exchange of its own, and it may reuse an id that an
    // older unanswered call also used. Counting every result would reinstate
    // that stale historical call, and the FIFO repair would then pair the
    // pending result with it and leave the pending call unanswered — an
    // invalid exchange assembled out of two valid halves.
    let mut wanted = unanswered_result_counts(pending);
    if wanted.is_empty() {
        return;
    }
    // Which of the original's unanswered calls does `retained` already supply?
    // Compared by *content*, not counted by id: a strategy may retain an older
    // unanswered occurrence while dropping the newer one an incoming result
    // answers, and counting by id would treat the old one as sufficient —
    // reinstating nothing, and letting the nearest-preceding pairing attach the
    // result to the wrong call's name and arguments.
    let (_, retained_unanswered) = pair_tool_exchanges(retained.iter().enumerate());
    let mut supplied: Vec<&crate::types::FunctionCallContent> = retained_unanswered
        .iter()
        .filter_map(|((mi, ci), _)| retained[*mi].contents[*ci].as_function_call())
        .collect();

    // Candidates newest-first: the most recent unanswered occurrence is the one
    // an incoming result answers.
    let (_, original_unanswered) = pair_tool_exchanges(original.iter().enumerate());
    let mut chosen: Vec<Site> = Vec::new();
    for (site, call_id) in original_unanswered.into_iter().rev() {
        let Some(call) = original[site.0].contents[site.1].as_function_call() else {
            continue;
        };
        if let Some(position) = supplied.iter().position(|supplied| *supplied == call) {
            // Already present in the retained set; consume it so a second
            // identical occurrence is still eligible.
            supplied.remove(position);
            continue;
        }
        let Some(count) = wanted.get_mut(call_id) else {
            continue;
        };
        if *count == 0 {
            continue;
        }
        *count -= 1;
        chosen.push(site);
    }
    // Append in original order so several recovered calls keep their sequence,
    // and group by source message: parallel calls declared together in one
    // assistant turn must be reinstated as *one* message. Splitting them into
    // `assistant(c1), assistant(c2)` puts an assistant turn between the
    // declaration and the results, which provider tool protocols reject — the
    // results must answer the single turn that declared both.
    chosen.sort_unstable();
    let mut index = 0;
    while index < chosen.len() {
        let (mi, _) = chosen[index];
        let mut group = Vec::new();
        while index < chosen.len() && chosen[index].0 == mi {
            group.push(chosen[index].1);
            index += 1;
        }
        let (first, rest) = group.split_first().expect("a group has at least one call");
        retained.push(reduced_call_with_signature(&original[mi], *first, rest));
        origins.push(Some(mi));
    }
}

/// The replay signature of the reasoning content immediately preceding the
/// content at `index`, if any.
///
/// Only a *contiguous* run of reasoning content counts, matching the request
/// builder's rule that a backfilled signature applies solely to a call directly
/// following its reasoning — any other content in between clears it.
fn preceding_reasoning_signature(message: &Message, index: usize) -> Option<String> {
    for content in message.contents[..index].iter().rev() {
        match content {
            Content::TextReasoning(t) => {
                if let Some(signature) = t.protected_data.as_deref().filter(|s| !s.is_empty()) {
                    return Some(signature.to_string());
                }
            }
            // Sibling calls in the same parallel group do not break the chain:
            // Gemini declares them together after one thought, so the reasoning
            // that signed the group still applies to a later member.
            Content::FunctionCall(_) => {}
            _ => return None,
        }
    }
    None
}

/// How many function results in `messages` no preceding call in `messages`
/// answers, per call id.
///
/// The mirror of [`unanswered_call_sites`], pairing by occurrence for the same
/// reason: ids are reused, so membership alone cannot tell a self-contained
/// exchange from one that reaches back into history.
fn unanswered_result_counts(messages: &[Message]) -> std::collections::HashMap<&str, usize> {
    use std::collections::HashMap;

    let mut available_calls: HashMap<&str, usize> = HashMap::new();
    let mut unanswered: HashMap<&str, usize> = HashMap::new();
    for content in messages.iter().flat_map(|m| m.contents.iter()) {
        match content {
            Content::FunctionCall(fc) if !fc.call_id.is_empty() => {
                *available_calls.entry(fc.call_id.as_str()).or_insert(0) += 1;
            }
            Content::FunctionResult(fr) if !fr.call_id.is_empty() => {
                match available_calls.get_mut(fr.call_id.as_str()) {
                    Some(count) if *count > 0 => *count -= 1,
                    _ => *unanswered.entry(fr.call_id.as_str()).or_insert(0) += 1,
                }
            }
            _ => {}
        }
    }
    unanswered.retain(|_, count| *count > 0);
    unanswered
}

/// Apply the invariants every compaction result must satisfy, whatever
/// strategy produced it: no half tool exchanges, and never system-only.
///
/// `pending` is a list that will be appended *after* this runs and is not ours
/// to modify — for [`CompactionProvider`] that is the run's own input. It
/// participates in pairing so a retained call answered by an incoming result is
/// not mistaken for an orphan, but is never stripped or returned.
///
/// Order matters: the orphan repair runs *first*, because it can itself strip
/// a conversation down to system messages only (a retained tool result whose
/// call fell outside the budget is removed, and it may have been the sole
/// non-system message). Running the minimum-retention check afterwards catches
/// that case too. The reverse order silently leaves a system-only result.
/// Neither pass can undo the other: the repair is a no-op on the orphan-free
/// message the fallback reinstates.
fn finalize_compaction(
    original: &[Message],
    retained: Vec<Message>,
    pending: &[Message],
) -> Vec<Message> {
    // Recovered once, before the repair rewrites any retained message.
    let mut origins = origin_indices(&retained, original);
    let mut retained = retained;
    reinstate_calls_answered_by_pending(original, &mut retained, &mut origins, pending);
    drop_orphaned_tool_exchanges(&mut retained, &mut origins, pending);
    // A non-system message in `pending` gives the model something to answer —
    // but only if it actually reaches the provider. An input carrying just an
    // audio attachment (dropped by Anthropic) or a hosted-file reference
    // (dropped by Gemini) contributes nothing on the wire, so the rule still
    // has work to do. Same predicate as the retained-side check, for the same
    // reason.
    if pending
        .iter()
        .any(|m| m.role != Role::system() && message_renders_on_every_provider(m))
    {
        return retained;
    }
    ensure_non_system_message(original, retained, &origins)
}

/// Compact `messages` with `strategy` and `tokenizer`.
///
/// This is the supported entry point: it runs the strategy and then enforces
/// the retention and pairing invariants (`finalize_compaction`). Calling
/// [`CompactionStrategy::compact`] directly gives the strategy's raw output
/// without them.
pub fn compact(
    messages: &[Message],
    strategy: &dyn CompactionStrategy,
    tokenizer: &dyn Tokenizer,
) -> Vec<Message> {
    finalize_compaction(messages, strategy.compact(messages, tokenizer), &[])
}

/// A [`ContextProvider`] that compacts the accumulated message list —
/// typically the run's history, once a [`HistoryProvider`](crate::history::HistoryProvider)
/// has prepended it in `before_run` — down to fit a [`CompactionStrategy`]'s
/// constraint before it reaches the model. Rust equivalent of (a subset of)
/// upstream's `CompactionProvider` (see module docs and `UPSTREAM_DRIFT.md`
/// §9).
///
/// Register it via [`AgentBuilder::with_compaction`](crate::agent::AgentBuilder::with_compaction),
/// which attaches it as one of the agent's own context providers — those run
/// *after* the session's (which is where a history provider, auto-attached
/// or explicit, lives — see [`Agent::combined_providers`](crate::agent::Agent)),
/// so compaction always sees the full, history-prepended message list for the
/// run.
pub struct CompactionProvider {
    strategy: Arc<dyn CompactionStrategy>,
    tokenizer: Box<dyn Tokenizer>,
}

impl CompactionProvider {
    /// A compaction provider using `strategy` with the default
    /// [`ApproxTokenizer`].
    pub fn new(strategy: impl CompactionStrategy + 'static) -> Self {
        Self::with_tokenizer(strategy, ApproxTokenizer)
    }

    /// A compaction provider using `strategy` and an explicit `tokenizer`.
    pub fn with_tokenizer(
        strategy: impl CompactionStrategy + 'static,
        tokenizer: impl Tokenizer + 'static,
    ) -> Self {
        Self {
            strategy: Arc::new(strategy),
            tokenizer: Box::new(tokenizer),
        }
    }
}

#[async_trait]
impl ContextProvider for CompactionProvider {
    /// Replace `ctx.messages` (the accumulated history + any earlier
    /// provider-injected messages) with the strategy's compacted subset.
    async fn before_run(&self, ctx: &mut SessionContext) -> Result<()> {
        let retained = self.strategy.compact(&ctx.messages, &*self.tokenizer);
        // Pair against the run's input as well as history. `prepare_request`
        // appends `input_messages` *after* every provider has run, so a
        // declaration-only (frontend) tool call sitting in history is routinely
        // answered by a result that arrives in this run's input. Repairing
        // history alone would see that call as unanswered, drop it, and leave
        // the incoming result unmatched — manufacturing exactly the invalid
        // conversation this pass exists to prevent.
        ctx.messages = finalize_compaction(&ctx.messages, retained, &ctx.input_messages);
        Ok(())
    }

    // `after_run` is intentionally a no-op (the default from `ContextProvider`):
    // compaction only shapes the outgoing request, it never observes or
    // records the run's outcome.
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::FunctionResultContent;
    use serde_json::json;

    fn text(role: Role, s: &str) -> Message {
        Message::new(role, s)
    }

    fn tool_result_message(call_id: &str, result: &str) -> Message {
        Message::with_contents(
            Role::tool(),
            vec![Content::FunctionResult(FunctionResultContent::new(
                call_id,
                Some(json!(result)),
            ))],
        )
    }

    fn tool_call_message(call_id: &str, name: &str) -> Message {
        Message::with_contents(
            Role::assistant(),
            vec![Content::FunctionCall(
                crate::types::FunctionCallContent::new(call_id, name, None),
            )],
        )
    }

    fn call_ids_in(messages: &[Message], f: fn(&Content) -> bool) -> Vec<String> {
        messages
            .iter()
            .flat_map(|m| m.contents.iter())
            .filter(|c| f(c))
            .filter_map(|c| match c {
                Content::FunctionCall(fc) => Some(fc.call_id.clone()),
                Content::FunctionResult(fr) => Some(fr.call_id.clone()),
                _ => None,
            })
            .collect()
    }

    // ---- compaction invariants (upstream #7406 / #7219) --------------------

    #[test]
    fn selective_tool_result_compacts_a_failed_calls_exception_too() {
        // Every provider converter renders `exception` instead of `result`, so
        // replacing only `result` left the original stack trace sent verbatim —
        // a no-op for exactly the results most worth compacting.
        let failed = Message::with_contents(
            Role::tool(),
            vec![Content::FunctionResult(FunctionResultContent {
                call_id: "c1".into(),
                result: None,
                exception: Some("Traceback: ...a very long stack trace...".into()),
            })],
        );
        let messages = vec![
            tool_call_message("c1", "t1"),
            failed,
            tool_call_message("c2", "t2"),
            tool_result_message("c2", "fresh"),
        ];
        let out = compact(&messages, &SelectiveToolResult::new(1), &ApproxTokenizer);

        let compacted = &out[1].function_results()[0];
        // Still visibly a failure, just without the payload.
        assert_eq!(compacted.exception.as_deref(), Some(OMITTED_TOOL_RESULT));
        // The most recent exchange is untouched.
        assert_eq!(out[3].function_results()[0].result, Some(json!("fresh")));
    }

    #[test]
    fn selective_tool_result_does_not_invent_an_exception_on_a_successful_result() {
        let messages = vec![
            tool_call_message("c1", "t1"),
            tool_result_message("c1", "stale"),
            tool_call_message("c2", "t2"),
            tool_result_message("c2", "fresh"),
        ];
        let out = compact(&messages, &SelectiveToolResult::new(1), &ApproxTokenizer);
        assert!(out[1].function_results()[0].exception.is_none());
    }

    #[test]
    fn selective_tool_result_compacts_the_payload_without_orphaning_the_call() {
        // Deleting the stale result would leave call_1's assistant `tool_calls`
        // entry unanswered, which providers reject with a 400. The result
        // content stays; only its payload is replaced.
        let messages = vec![
            tool_call_message("call_1", "get_weather"),
            tool_result_message("call_1", "a very long stale payload"),
            tool_call_message("call_2", "get_time"),
            tool_result_message("call_2", "noon"),
        ];
        let out = compact(&messages, &SelectiveToolResult::new(1), &ApproxTokenizer);

        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["call_1", "call_2"]
        );
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))),
            vec!["call_1", "call_2"]
        );
        assert_eq!(
            out[1].function_results()[0].result,
            Some(json!(OMITTED_TOOL_RESULT))
        );
        // The most recent exchange keeps its real payload.
        assert_eq!(out[3].function_results()[0].result, Some(json!("noon")));
    }

    #[test]
    fn a_budget_cut_between_call_and_result_orphans_neither() {
        let mut call_msg = tool_call_message("call_1", "get_weather");
        call_msg
            .contents
            .push(Content::text("let me look that up for you right now"));
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "hi"),
            call_msg,
            tool_result_message("call_1", "sunny"),
        ];
        let out = compact(&messages, &TokenBudget::new(4), &ApproxTokenizer);
        // The expensive call-bearing message fell outside the budget, so its
        // cheap result must not survive alone answering nothing.
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))).is_empty());
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty());
    }

    #[test]
    fn a_reused_call_id_is_paired_by_occurrence_not_by_id() {
        // Call ids are not guaranteed unique across a conversation. Comparing
        // *sets* of ids called this balanced (both sides are {c1}) and returned
        // early, leaving the second call unanswered.
        let messages = vec![
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
            tool_call_message("c1", "get_weather"),
        ];
        let out = compact(&messages, &SlidingWindow::new(10), &ApproxTokenizer);
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c1"],
            "the second, unanswered c1 call must be dropped"
        );
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))),
            vec!["c1"]
        );
    }

    #[test]
    fn two_full_exchanges_reusing_one_call_id_both_survive() {
        let messages = vec![
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "rainy"),
        ];
        let out = compact(&messages, &SlidingWindow::new(10), &ApproxTokenizer);
        assert_eq!(out.len(), 4);
    }

    #[test]
    fn a_result_preceding_its_call_is_not_treated_as_paired() {
        // A result can only answer a call that came before it.
        let messages = vec![
            tool_result_message("c1", "sunny"),
            tool_call_message("c1", "get_weather"),
        ];
        let out = compact(&messages, &SlidingWindow::new(10), &ApproxTokenizer);
        assert!(out.is_empty(), "both halves are orphans, got {out:?}");
    }

    #[test]
    fn an_intact_tool_exchange_is_left_alone() {
        let messages = vec![
            tool_call_message("call_1", "get_weather"),
            tool_result_message("call_1", "sunny"),
        ];
        let out = compact(&messages, &SlidingWindow::new(10), &ApproxTokenizer);
        assert_eq!(out.len(), 2);
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["call_1"]
        );
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))),
            vec!["call_1"]
        );
    }

    #[test]
    fn an_orphan_strip_keeps_the_rest_of_its_message() {
        // Only the orphaned call content is removed; sibling text survives and
        // the message itself is not dropped.
        let mut call_msg = tool_call_message("call_1", "get_weather");
        call_msg.contents.push(Content::text("checking now"));
        let out = compact(&[call_msg], &SlidingWindow::new(10), &ApproxTokenizer);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].text(), "checking now");
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty());
    }

    #[test]
    fn compaction_never_returns_system_messages_only() {
        // A budget smaller than the system prefix used to leave a
        // "conversation" with no turn for the model to answer.
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "hello"),
            text(Role::assistant(), "hi"),
        ];
        for out in [
            compact(&messages, &Truncation::new(1), &ApproxTokenizer),
            compact(&messages, &SlidingWindow::new(0), &ApproxTokenizer),
        ] {
            assert!(
                out.iter().any(|m| m.role != Role::system()),
                "expected a non-system message to be retained, got {:?}",
                out.iter().map(|m| m.role.as_str()).collect::<Vec<_>>()
            );
            // Upstream accepts exceeding the limit rather than emitting a
            // useless projection; the retained turn is the most recent one.
            assert_eq!(out.last().unwrap().text(), "hi");
        }
    }

    #[test]
    fn the_minimum_retained_message_is_never_a_half_exchange() {
        // The only non-system messages are a tool exchange, so the fallback
        // must reinstate the call-bearing message's *text*, not the orphan.
        let mut call_msg = tool_call_message("call_1", "get_weather");
        call_msg.contents.push(Content::text("checking now"));
        let messages = vec![
            text(Role::system(), "sys"),
            call_msg,
            tool_result_message("call_1", "sunny"),
        ];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);
        assert!(out.iter().any(|m| m.role != Role::system()));
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))).is_empty());
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty());
    }

    #[test]
    fn a_call_answered_by_the_runs_input_is_not_dropped_as_an_orphan() {
        // The frontend/declaration-only tool flow: the model's call sits in
        // history, and the caller supplies its result in the *next* run's
        // input. `prepare_request` appends that input after every provider has
        // run, so a repair that only sees history would drop the call and leave
        // the incoming result unmatched — the exact invalid conversation this
        // pass exists to prevent.
        let history = vec![
            text(Role::user(), "what's the weather?"),
            tool_call_message("c1", "get_weather"),
        ];
        let input = vec![tool_result_message("c1", "sunny")];

        let retained = SlidingWindow::new(10).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);

        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c1"],
            "the call must survive: its result arrives in this run's input"
        );
        // The input itself is never returned or modified by the repair.
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))).is_empty());

        // Contrast, pinning the bug this fixes: without visibility of the
        // pending input the same call *is* dropped as an orphan.
        let retained = SlidingWindow::new(10).compact(&history, &ApproxTokenizer);
        let blind = finalize_compaction(&history, retained, &[]);
        assert!(call_ids_in(&blind, |c| matches!(c, Content::FunctionCall(_))).is_empty());
    }

    #[test]
    fn a_call_the_strategy_dropped_is_reinstated_when_the_input_answers_it() {
        // Pairing against pending stops a *retained* call being stripped, but
        // the strategy runs first: SlidingWindow(0) excludes the call from
        // history entirely, and the incoming result then answers nothing.
        let history = vec![
            text(Role::user(), "what's the weather?"),
            tool_call_message("c1", "get_weather"),
        ];
        let input = vec![tool_result_message("c1", "sunny")];

        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        assert!(
            call_ids_in(&retained, |c| matches!(c, Content::FunctionCall(_))).is_empty(),
            "precondition: the strategy really did drop the call"
        );

        let out = finalize_compaction(&history, retained, &input);
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c1"],
            "the call must be put back so the incoming result answers something"
        );
    }

    #[test]
    fn a_reused_call_id_reinstates_the_unanswered_occurrence() {
        // `call(c1, old) -> result(c1) -> call(c1, new)`: the first occurrence
        // is already answered, so an incoming result answers the *new* call.
        // Picking the first id match replayed the old call's name and arguments
        // and attached the new result to it.
        let mut old_call = tool_call_message("c1", "get_weather");
        old_call.contents = vec![Content::FunctionCall(
            crate::types::FunctionCallContent::new(
                "c1",
                "get_weather",
                Some(crate::types::FunctionArguments::Raw(
                    "{\"city\":\"old\"}".into(),
                )),
            ),
        )];
        let mut new_call = tool_call_message("c1", "get_weather");
        new_call.contents = vec![Content::FunctionCall(
            crate::types::FunctionCallContent::new(
                "c1",
                "get_weather",
                Some(crate::types::FunctionArguments::Raw(
                    "{\"city\":\"new\"}".into(),
                )),
            ),
        )];
        let history = vec![old_call, tool_result_message("c1", "old answer"), new_call];
        let input = vec![tool_result_message("c1", "new answer")];

        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);

        let calls: Vec<&crate::types::FunctionCallContent> = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .filter_map(Content::as_function_call)
            .collect();
        assert_eq!(calls.len(), 1);
        let args = calls[0].parse_arguments().unwrap();
        assert_eq!(
            args.get("city").and_then(|v| v.as_str()),
            Some("new"),
            "the unanswered (new) occurrence must be the one reinstated"
        );
    }

    #[test]
    fn a_self_contained_pending_exchange_reinstates_nothing() {
        // The run's input carries its own call *and* result, reusing an id that
        // an older unanswered call also used. Counting every pending result
        // pulled the stale historical call back, and the FIFO repair then
        // paired the pending result with it, leaving the pending call
        // unanswered — an invalid exchange assembled from two valid halves.
        let history = vec![tool_call_message("c1", "get_weather")];
        let input = vec![
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
        ];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty(),
            "pending answers itself; nothing should be reinstated, got {out:?}"
        );
    }

    #[test]
    fn a_pending_result_beyond_what_pending_answers_still_reinstates() {
        // Two results, only one answered within pending: the extra one reaches
        // back into history and must still recover its call.
        let history = vec![tool_call_message("c1", "get_weather")];
        let input = vec![
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "first"),
            tool_result_message("c1", "second"),
        ];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c1"]
        );
    }

    #[test]
    fn a_pending_exchange_pairs_with_its_own_call_not_a_historical_one() {
        // Retained history holds an unanswered c1 call and the input carries its
        // own c1 call + result. Pairing with the *oldest* matching call attached
        // the pending result to the historical call, kept that call, and left
        // the pending call unanswered — two calls, one result.
        let history = vec![tool_call_message("c1", "get_weather")];
        let input = vec![
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
        ];
        let retained = SlidingWindow::new(10).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty(),
            "the unanswered historical call must be stripped, got {out:?}"
        );
    }

    #[test]
    fn a_reinstated_call_keeps_its_reasoning_signature() {
        // The signature sits on the preceding reasoning content (the supported
        // fallback placement). Reducing to the call alone stripped the only
        // copy, and the reasoning message is not retained either, so the
        // request builder had nothing left to backfill from.
        let signed = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "thinking".into(),
                    protected_data: Some("c2ln".into()),
                    ..Default::default()
                }),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "c1",
                    "get_weather",
                    None,
                )),
            ],
        );
        let history = vec![signed];
        let input = vec![tool_result_message("c1", "sunny")];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);

        let call = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .find_map(Content::as_function_call)
            .expect("the call is reinstated");
        assert_eq!(call.protected_data.as_deref(), Some("c2ln"));
    }

    #[test]
    fn a_reinstated_call_gains_no_signature_from_unrelated_content() {
        let mut msg = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "thinking".into(),
                    protected_data: Some("c2ln".into()),
                    ..Default::default()
                }),
                Content::text("intervening"),
            ],
        );
        msg.contents.push(Content::FunctionCall(
            crate::types::FunctionCallContent::new("c1", "get_weather", None),
        ));
        let history = vec![msg];
        let input = vec![tool_result_message("c1", "sunny")];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);

        let call = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .find_map(Content::as_function_call)
            .expect("the call is reinstated");
        assert!(call.protected_data.is_none());
    }

    #[test]
    fn the_fallback_restores_the_nearest_reused_call_not_the_oldest() {
        // `call(c1, old) -> call(c1, new) -> result(c1)`: the result answers the
        // *new* call. A third pairing walk here used FIFO while the repair paths
        // used nearest-preceding, so the fallback replayed the old call's
        // arguments with the new call's result.
        let mk = |city: &str| {
            Message::with_contents(
                Role::assistant(),
                vec![Content::FunctionCall(
                    crate::types::FunctionCallContent::new(
                        "c1",
                        "get_weather",
                        Some(crate::types::FunctionArguments::Raw(format!(
                            "{{\"city\":\"{city}\"}}"
                        ))),
                    ),
                )],
            )
        };
        let messages = vec![
            text(Role::system(), "sys"),
            mk("old"),
            mk("new"),
            tool_result_message("c1", "sunny"),
        ];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);

        let call = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .find_map(Content::as_function_call)
            .expect("the exchange is restored");
        assert_eq!(
            call.parse_arguments()
                .unwrap()
                .get("city")
                .and_then(|v| v.as_str()),
            Some("new")
        );
    }

    #[test]
    fn unknown_content_does_not_satisfy_the_retention_check() {
        // `Content::Unknown` is a forward-compatibility placeholder carrying no
        // data; no converter has an arm for it, so a message of only Unknown
        // reaches every provider as nothing.
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "a real earlier turn"),
            Message::with_contents(Role::assistant(), vec![Content::Unknown]),
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert!(
            out.iter()
                .any(|m| m.contents.iter().any(|c| matches!(c, Content::Text(_)))
                    && m.role != Role::system()),
            "expected a real turn to be restored, got {out:?}"
        );
    }

    /// A strategy that keeps only the messages at the given indices — used to
    /// reach retention shapes the built-in strategies cannot produce.
    struct KeepIndices(Vec<usize>);
    impl CompactionStrategy for KeepIndices {
        fn compact(&self, messages: &[Message], _t: &dyn Tokenizer) -> Vec<Message> {
            messages
                .iter()
                .enumerate()
                .filter(|(i, _)| self.0.contains(i))
                .map(|(_, m)| m.clone())
                .collect()
        }
    }

    #[test]
    fn an_older_retained_occurrence_does_not_suppress_reinstating_the_newer() {
        // A custom strategy keeps the *older* unanswered c1 call and drops the
        // newer one. Counting retained calls by id treated the old one as
        // sufficient, reinstated nothing, and let the incoming result attach to
        // the old call's name and arguments.
        let mk = |city: &str| {
            Message::with_contents(
                Role::assistant(),
                vec![Content::FunctionCall(
                    crate::types::FunctionCallContent::new(
                        "c1",
                        "get_weather",
                        Some(crate::types::FunctionArguments::Raw(format!(
                            "{{\"city\":\"{city}\"}}"
                        ))),
                    ),
                )],
            )
        };
        let history = vec![mk("old"), mk("new")];
        let input = vec![tool_result_message("c1", "sunny")];

        let retained = KeepIndices(vec![0]).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);

        let cities: Vec<String> = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .filter_map(Content::as_function_call)
            .filter_map(|c| {
                c.parse_arguments()
                    .ok()?
                    .get("city")?
                    .as_str()
                    .map(str::to_string)
            })
            .collect();
        assert!(
            cities.contains(&"new".to_string()),
            "the newer occurrence the result answers must be present, got {cities:?}"
        );
    }

    #[test]
    fn non_image_media_alone_does_not_satisfy_the_retention_check() {
        // Anthropic's image_block_from_data / image_block_from_uri return None
        // for anything that is not an image, so audio Data or a non-image Uri
        // is dropped there and the request is effectively system-only.
        for media in [
            Content::Data(crate::types::DataContent::from_bytes(b"aud", "audio/wav")),
            Content::Uri(crate::types::UriContent {
                uri: "https://example.com/doc.pdf".into(),
                media_type: "application/pdf".into(),
            }),
        ] {
            let messages = vec![
                text(Role::system(), "sys"),
                text(Role::user(), "a real earlier turn"),
                Message::with_contents(Role::assistant(), vec![media]),
            ];
            let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
            assert!(
                out.iter().any(|m| m.role != Role::system()
                    && m.contents.iter().any(|c| matches!(c, Content::Text(_)))),
                "expected a real turn to be restored, got {out:?}"
            );
        }
    }

    #[test]
    fn a_hosted_file_alone_does_not_satisfy_the_retention_check() {
        // Gemini's content_to_part has no HostedFile arm, so a message carrying
        // only one reaches it as nothing. A negative-list predicate counted
        // every non-reasoning variant as renderable.
        let hosted = Message::with_contents(
            Role::assistant(),
            vec![Content::HostedFile(crate::types::HostedFileContent {
                file_id: "file_1".into(),
            })],
        );
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "a real earlier turn"),
            hosted,
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert!(
            out.iter().any(|m| m.role != Role::system()
                && m.contents.iter().any(|c| matches!(c, Content::Text(_)))),
            "expected a real turn to be restored, got {out:?}"
        );
    }

    #[test]
    fn a_completed_exchange_in_history_is_not_reinstated() {
        // Nothing is outstanding, so an incoming result for a *fresh* call id
        // pulls back nothing.
        let history = vec![
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
        ];
        let input = vec![tool_result_message("c2", "noon")];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty());
    }

    #[test]
    fn a_reinstated_call_carries_nothing_else_from_its_message() {
        let mut call_msg = tool_call_message("c1", "get_weather");
        call_msg
            .contents
            .push(Content::text("a large unrelated payload"));
        let history = vec![call_msg];
        let input = vec![tool_result_message("c1", "sunny")];

        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].contents.len(), 1, "only the call rides back in");
        assert!(out[0].text().is_empty());
    }

    #[test]
    fn nothing_is_reinstated_when_the_input_carries_no_results() {
        let history = vec![tool_call_message("c1", "get_weather")];
        let input = vec![text(Role::user(), "hello")];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty());
    }

    #[test]
    fn an_already_retained_call_is_not_reinstated_twice() {
        let history = vec![tool_call_message("c1", "get_weather")];
        let input = vec![tool_result_message("c1", "sunny")];
        let retained = SlidingWindow::new(10).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c1"]
        );
    }

    #[test]
    fn the_minimum_retention_fallback_never_picks_a_tool_role_message() {
        // A tool message's text belongs to its exchange. Keeping it without the
        // result yields a tool message with no tool_call_id, which the OpenAI
        // converter drops outright (system-only after all) and which Gemini
        // emits as a bare text part under its `function` role.
        let mut result_msg = tool_result_message("c1", "sunny");
        result_msg.contents.push(Content::text("explanatory text"));
        let messages = vec![
            text(Role::system(), "sys"),
            tool_call_message("c1", "get_weather"),
            result_msg,
        ];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);
        // The complete exchange comes back instead of a bare tool-role text.
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c1"]
        );
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))),
            vec!["c1"]
        );
        for m in &out {
            if m.role == Role::tool() {
                assert!(
                    m.contents
                        .iter()
                        .any(|c| matches!(c, Content::FunctionResult(_))),
                    "a tool-role message must carry its result"
                );
            }
        }
    }

    #[test]
    fn a_call_with_no_answer_anywhere_is_still_dropped() {
        let history = vec![tool_call_message("c1", "get_weather")];
        let input = vec![text(Role::user(), "never mind")];
        let retained = SlidingWindow::new(10).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert!(call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))).is_empty());
    }

    #[test]
    fn pending_input_satisfies_the_minimum_retention_rule() {
        // The run's own input already gives the model something to answer, so
        // no history turn needs reinstating over the limit.
        let history = vec![text(Role::system(), "sys"), text(Role::user(), "older")];
        let input = vec![text(Role::user(), "current question")];
        let retained = Truncation::new(1).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert!(out.iter().all(|m| m.role == Role::system()));
    }

    #[test]
    fn a_conversation_of_only_a_tool_exchange_retains_it_whole() {
        // Stripping both halves (the ordinary-content fallback) would leave the
        // result system-only after all, so the complete exchange is reinstated
        // atomically instead.
        let messages = vec![
            text(Role::system(), "sys"),
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
        ];
        for out in [
            compact(&messages, &Truncation::new(1), &ApproxTokenizer),
            compact(&messages, &SlidingWindow::new(0), &ApproxTokenizer),
        ] {
            assert!(
                out.iter().any(|m| m.role != Role::system()),
                "expected the tool exchange to be retained, got {:?}",
                out.iter().map(|m| m.role.as_str()).collect::<Vec<_>>()
            );
            assert_eq!(
                call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
                vec!["c1"]
            );
            assert_eq!(
                call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))),
                vec!["c1"]
            );
        }
    }

    #[test]
    fn the_latest_complete_exchange_is_the_one_reinstated() {
        let messages = vec![
            text(Role::system(), "sys"),
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
            tool_call_message("c2", "get_time"),
            tool_result_message("c2", "noon"),
        ];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c2"]
        );
    }

    #[test]
    fn an_incomplete_tool_exchange_is_not_reinstated() {
        // A half exchange is worse than nothing: there is no complete pair to
        // fall back to, so the result stays system-only rather than becoming
        // invalid.
        let messages = vec![
            text(Role::system(), "sys"),
            tool_call_message("c1", "get_weather"),
        ];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);
        assert!(out.iter().all(|m| m.role == Role::system()));
    }

    #[test]
    fn reasoning_alone_does_not_qualify_as_the_minimum_retained_message() {
        // `[TextReasoning, FunctionCall]` + its result: stripping the call left
        // reasoning behind, which passed the non-empty check and returned early,
        // bypassing the complete-exchange fallback. Reasoning renders nothing
        // standalone, so the request was effectively system-only anyway.
        let signed = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "thinking".into(),
                    ..Default::default()
                }),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "c1",
                    "get_weather",
                    None,
                )),
            ],
        );
        let messages = vec![
            text(Role::system(), "sys"),
            signed,
            tool_result_message("c1", "sunny"),
        ];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);

        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionCall(_))),
            vec!["c1"],
            "the complete exchange must be restored, got {out:?}"
        );
        assert_eq!(
            call_ids_in(&out, |c| matches!(c, Content::FunctionResult(_))),
            vec!["c1"]
        );
    }

    #[test]
    fn text_beside_reasoning_still_qualifies() {
        let msg = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "thinking".into(),
                    ..Default::default()
                }),
                Content::text("the answer"),
            ],
        );
        let messages = vec![text(Role::system(), "sys"), msg];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);
        assert_eq!(out.last().unwrap().text(), "the answer");
    }

    #[test]
    fn the_complete_exchange_fallback_carries_the_reasoning_signature() {
        // The reinstatement path copied the signature; this one did not.
        let signed = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "thinking".into(),
                    protected_data: Some("c2ln".into()),
                    ..Default::default()
                }),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "c1",
                    "get_weather",
                    None,
                )),
            ],
        );
        let messages = vec![
            text(Role::system(), "sys"),
            signed,
            tool_result_message("c1", "sunny"),
        ];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);

        let call = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .find_map(Content::as_function_call)
            .expect("the exchange is restored");
        assert_eq!(call.protected_data.as_deref(), Some("c2ln"));
    }

    #[test]
    fn a_retained_message_that_renders_nothing_does_not_satisfy_retention() {
        // The orphan repair strips the call and leaves reasoning behind. That
        // message is non-system but reaches the model as nothing, so the
        // fallback must still run and restore the older complete exchange.
        let reasoning_plus_orphan = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "thinking".into(),
                    ..Default::default()
                }),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "orphan",
                    "get_weather",
                    None,
                )),
            ],
        );
        let messages = vec![
            text(Role::system(), "sys"),
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
            reasoning_plus_orphan,
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);

        assert!(
            out.iter().any(|m| m.role != Role::system()
                && m.contents
                    .iter()
                    .any(|c| !matches!(c, Content::TextReasoning(_)))),
            "expected something that actually renders, got {out:?}"
        );
    }

    #[test]
    fn retained_reasoning_is_never_discarded_by_the_retention_check() {
        // The conservative predicate decides whether to *add* a fallback; it
        // never removes anything. Providers that do render reasoning (Anthropic
        // `thinking` blocks, OpenAI Responses replaying a preserved item) keep
        // their context either way.
        let reasoning_only = Message::with_contents(
            Role::assistant(),
            vec![Content::TextReasoning(crate::types::TextReasoningContent {
                text: "thinking".into(),
                raw_representation: Some(json!({"id": "rs_1"})),
                ..Default::default()
            })],
        );
        let messages = vec![text(Role::system(), "sys"), reasoning_only];
        let out = compact(&messages, &SlidingWindow::new(10), &ApproxTokenizer);

        let kept = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .any(|c| matches!(c, Content::TextReasoning(_)));
        assert!(kept, "reasoning must survive, got {out:?}");
    }

    #[test]
    fn the_fallback_is_inserted_in_chronological_order() {
        // The retained message is newer but renders nothing universally, so a
        // fallback is added. Appending it put a stale user turn *after* the
        // assistant's reply — on Anthropic, which does render reasoning, the
        // model then sees the old request as the latest turn.
        let messages = vec![
            text(Role::user(), "old question"),
            Message::with_contents(
                Role::assistant(),
                vec![Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "newer thinking".into(),
                    ..Default::default()
                })],
            ),
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].text(), "old question", "got {out:?}");
        assert!(matches!(out[1].contents[0], Content::TextReasoning(_)));
    }

    #[test]
    fn a_fallback_newer_than_the_retained_message_is_appended() {
        let messages = vec![
            Message::with_contents(
                Role::assistant(),
                vec![Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "older thinking".into(),
                    ..Default::default()
                })],
            ),
            text(Role::user(), "newer question"),
        ];
        let retained = KeepIndices(vec![0]).compact(&messages, &ApproxTokenizer);
        let out = finalize_compaction(&messages, retained, &[]);
        assert_eq!(out.len(), 2);
        assert!(matches!(out[0].contents[0], Content::TextReasoning(_)));
        assert_eq!(out[1].text(), "newer question");
    }

    #[test]
    fn wire_empty_pending_input_does_not_satisfy_retention() {
        // The run's input is a hosted-file reference, which Gemini drops; it
        // contributes nothing, so the older real turn must still be restored.
        let history = vec![text(Role::system(), "sys"), text(Role::user(), "real turn")];
        let input = vec![Message::with_contents(
            Role::user(),
            vec![Content::HostedFile(crate::types::HostedFileContent {
                file_id: "file_1".into(),
            })],
        )];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);
        assert!(
            out.iter().any(|m| m.role != Role::system()
                && m.contents.iter().any(|c| matches!(c, Content::Text(_)))),
            "expected the real turn to be restored, got {out:?}"
        );
    }

    #[test]
    fn tool_payloads_count_against_the_token_budget() {
        // A megabyte-scale JSON result cost zero tokens, so any number of
        // completed exchanges survived TokenBudget and the "compacted" request
        // still blew the context window.
        let t = ApproxTokenizer;
        let bulky = Message::with_contents(
            Role::tool(),
            vec![Content::FunctionResult(FunctionResultContent::new(
                "c1",
                Some(json!("x".repeat(400))),
            ))],
        );
        assert!(
            count_message_tokens(&t, &bulky) >= 100,
            "a large tool result must cost tokens, got {}",
            count_message_tokens(&t, &bulky)
        );

        let call = Message::with_contents(
            Role::assistant(),
            vec![Content::FunctionCall(
                crate::types::FunctionCallContent::new(
                    "c1",
                    "get_weather",
                    Some(crate::types::FunctionArguments::Raw("y".repeat(400))),
                ),
            )],
        );
        assert!(count_message_tokens(&t, &call) >= 100);

        // And the budget now actually excludes them.
        let messages = vec![call.clone(), bulky.clone(), text(Role::user(), "tiny")];
        let out = TokenBudget::new(10).compact(&messages, &t);
        assert!(
            out.len() < 3,
            "the bulky exchange must not fit a 10-token budget, got {out:?}"
        );
    }

    #[test]
    fn duplicate_messages_do_not_alias_the_fallback_position() {
        // Identical reasoning messages either side of an older user turn. A
        // forward equality match aliased the retained *later* reasoning to the
        // earlier duplicate, concluded the fallback was newer, and appended it —
        // reversing the conversation again.
        let reasoning = || {
            Message::with_contents(
                Role::assistant(),
                vec![Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "same thinking".into(),
                    ..Default::default()
                })],
            )
        };
        let messages = vec![reasoning(), text(Role::user(), "old question"), reasoning()];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert_eq!(out.len(), 2);
        assert_eq!(
            out[0].text(),
            "old question",
            "the fallback must precede the retained later reasoning, got {out:?}"
        );
    }

    #[test]
    fn the_complete_exchange_fallback_is_also_inserted_chronologically() {
        // The chronological fix covered only the plain-content fallback; the
        // exchange fallback still appended, putting a stale tool turn after a
        // newer reasoning message that Anthropic does render.
        let messages = vec![
            tool_call_message("c1", "get_weather"),
            tool_result_message("c1", "sunny"),
            Message::with_contents(
                Role::assistant(),
                vec![Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "newer thinking".into(),
                    ..Default::default()
                })],
            ),
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);

        let reasoning_at = out
            .iter()
            .position(|m| {
                m.contents
                    .iter()
                    .any(|c| matches!(c, Content::TextReasoning(_)))
            })
            .expect("the retained reasoning is still present");
        let call_at = out
            .iter()
            .position(|m| {
                m.contents
                    .iter()
                    .any(|c| matches!(c, Content::FunctionCall(_)))
            })
            .expect("the exchange is restored");
        assert!(
            call_at < reasoning_at,
            "the older exchange must precede the newer reasoning, got {out:?}"
        );
        // ...and its result stays with it, still after the call.
        let result_at = out
            .iter()
            .position(|m| {
                m.contents
                    .iter()
                    .any(|c| matches!(c, Content::FunctionResult(_)))
            })
            .expect("the result is restored");
        assert!(
            call_at < result_at && result_at < reasoning_at,
            "got {out:?}"
        );
    }

    #[test]
    fn parallel_reinstated_calls_stay_in_one_message() {
        // Two calls declared together in one assistant turn. Reinstating them
        // as separate messages puts an assistant turn between the declaration
        // and the results, which provider tool protocols reject.
        let parallel = Message::with_contents(
            Role::assistant(),
            vec![
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "c1",
                    "get_weather",
                    None,
                )),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "c2", "get_time", None,
                )),
            ],
        );
        let history = vec![parallel];
        let input = vec![
            tool_result_message("c1", "sunny"),
            tool_result_message("c2", "noon"),
        ];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);

        let with_calls: Vec<&Message> = out
            .iter()
            .filter(|m| {
                m.contents
                    .iter()
                    .any(|c| matches!(c, Content::FunctionCall(_)))
            })
            .collect();
        assert_eq!(
            with_calls.len(),
            1,
            "both calls must be reinstated in one message, got {out:?}"
        );
        assert_eq!(with_calls[0].contents.len(), 2);
    }

    #[test]
    fn chronology_survives_the_orphan_repair_rewriting_a_message() {
        // The repair strips the orphaned call, so the retained message no longer
        // equals its original. Recovering positions by equality *after* that
        // matched nothing and the older fallback was appended, putting a stale
        // user turn last for providers that render reasoning.
        let newer = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "newer thinking".into(),
                    ..Default::default()
                }),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "orphan",
                    "get_weather",
                    None,
                )),
            ],
        );
        let messages = vec![text(Role::user(), "old question"), newer];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);

        assert_eq!(out.len(), 2);
        assert_eq!(
            out[0].text(),
            "old question",
            "the fallback must precede the retained (rewritten) message, got {out:?}"
        );
    }

    #[test]
    fn an_assistant_image_turn_does_not_satisfy_retention() {
        // Image blocks are user-turn content everywhere: Converse and
        // Anthropic reject an assistant image block outright, and the Bedrock
        // converter skips it — so an assistant message whose only content is
        // image data renders nowhere.
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "a real earlier turn"),
            Message::with_contents(
                Role::assistant(),
                vec![Content::Data(crate::types::DataContent::from_bytes(
                    b"img",
                    "image/png",
                ))],
            ),
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert!(
            out.iter().any(|m| m.role != Role::system()
                && m.contents.iter().any(|c| matches!(c, Content::Text(_)))),
            "expected the real turn restored, got {out:?}"
        );
    }

    #[test]
    fn an_image_turn_satisfies_the_retention_check() {
        // Images render on every converter, so a latest image-only turn needs no
        // fallback — appending a stale text turn changes the prompt and
        // defeats the window the caller asked for.
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "an older turn"),
            Message::with_contents(
                Role::user(),
                vec![Content::Data(crate::types::DataContent::from_bytes(
                    b"img",
                    "image/png",
                ))],
            ),
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert_eq!(out.len(), 2, "no fallback should be added, got {out:?}");
        assert!(out[1]
            .contents
            .iter()
            .any(|c| matches!(c, Content::Data(_))));
    }

    #[test]
    fn a_remote_image_uri_does_not_satisfy_retention() {
        // Bedrock's Converse API has no remote-URL image source (inline bytes
        // or S3 only), so a hosted image reference renders nowhere there — a
        // Uri turn is not universal even when it names an image.
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "an older turn"),
            Message::with_contents(
                Role::user(),
                vec![Content::Uri(crate::types::UriContent {
                    uri: "https://example.com/a.png".into(),
                    media_type: "image/png".into(),
                })],
            ),
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert!(
            out.iter().any(|m| m.role != Role::system()
                && m.contents.iter().any(|c| matches!(c, Content::Text(_)))),
            "expected the real turn restored, got {out:?}"
        );
    }

    #[test]
    fn an_image_format_outside_the_universal_set_does_not_satisfy_retention() {
        // The floor is Bedrock's format set (png/jpeg/gif/webp); an SVG is an
        // image but renders nowhere on Converse.
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "an older turn"),
            Message::with_contents(
                Role::user(),
                vec![Content::Data(crate::types::DataContent::from_bytes(
                    b"<svg/>",
                    "image/svg+xml",
                ))],
            ),
        ];
        let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
        assert!(
            out.iter().any(|m| m.role != Role::system()
                && m.contents.iter().any(|c| matches!(c, Content::Text(_)))),
            "expected the real turn restored, got {out:?}"
        );
    }

    #[test]
    fn a_reinstated_parallel_call_inherits_the_groups_reasoning_signature() {
        // The signature sits on the reasoning that precedes a parallel group;
        // restoring only the second call used to stop at its sibling and find
        // nothing.
        let group = Message::with_contents(
            Role::assistant(),
            vec![
                Content::TextReasoning(crate::types::TextReasoningContent {
                    text: "thinking".into(),
                    protected_data: Some("c2ln".into()),
                    ..Default::default()
                }),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "c1",
                    "get_weather",
                    None,
                )),
                Content::FunctionCall(crate::types::FunctionCallContent::new(
                    "c2", "get_time", None,
                )),
            ],
        );
        let history = vec![group];
        // Only c2 is answered by the incoming input.
        let input = vec![tool_result_message("c2", "noon")];
        let retained = SlidingWindow::new(0).compact(&history, &ApproxTokenizer);
        let out = finalize_compaction(&history, retained, &input);

        let call = out
            .iter()
            .flat_map(|m| m.contents.iter())
            .find_map(Content::as_function_call)
            .expect("c2 is reinstated");
        assert_eq!(call.call_id, "c2");
        assert_eq!(call.protected_data.as_deref(), Some("c2ln"));
    }

    #[test]
    fn an_image_type_on_a_malformed_uri_does_not_satisfy_retention() {
        // Anthropic and Gemini both parse the data URI before emitting, so a
        // declared image media type over a broken URI reaches neither.
        for uri in ["not-a-data-uri", "data:image/png,AAAA", ""] {
            let messages = vec![
                text(Role::system(), "sys"),
                text(Role::user(), "a real earlier turn"),
                Message::with_contents(
                    Role::user(),
                    vec![Content::Data(crate::types::DataContent {
                        uri: uri.into(),
                        media_type: Some("image/png".into()),
                    })],
                ),
            ];
            let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
            assert!(
                out.iter().any(|m| m.role != Role::system()
                    && m.contents.iter().any(|c| matches!(c, Content::Text(_)))),
                "expected a real turn restored for {uri:?}, got {out:?}"
            );
        }
    }

    #[test]
    fn blank_text_does_not_satisfy_the_retention_check() {
        // `Text("")` is worse than unrendered — Anthropic rejects an empty
        // text block outright — and a whitespace turn gives the model nothing
        // either way, so the older substantive turn must be restored.
        for blank in ["", "   \n\t"] {
            let messages = vec![
                text(Role::system(), "sys"),
                text(Role::user(), "a real earlier turn"),
                text(Role::assistant(), blank),
            ];
            let out = compact(&messages, &SlidingWindow::new(1), &ApproxTokenizer);
            assert!(
                out.iter()
                    .any(|m| m.role != Role::system() && !m.text().trim().is_empty()),
                "expected the real turn restored for {blank:?}, got {out:?}"
            );
        }
    }

    #[test]
    fn an_all_system_conversation_stays_all_system() {
        // Nothing to reinstate: the fallback must not invent a turn.
        let messages = vec![text(Role::system(), "a"), text(Role::system(), "b")];
        let out = compact(&messages, &Truncation::new(1), &ApproxTokenizer);
        assert!(out.iter().all(|m| m.role == Role::system()));
    }

    // ---- ApproxTokenizer -------------------------------------------------

    #[test]
    fn approx_tokenizer_uses_four_chars_per_token_ceiling() {
        let t = ApproxTokenizer;
        assert_eq!(t.count_tokens(""), 0);
        assert_eq!(t.count_tokens("abcd"), 1);
        assert_eq!(t.count_tokens("abcde"), 2); // ceil(5/4) = 2
        assert_eq!(t.count_tokens("abcdefgh"), 2);
        assert_eq!(t.count_tokens("abcdefghi"), 3); // ceil(9/4) = 3
    }

    #[test]
    fn approx_tokenizer_does_not_inflate_non_ascii_text() {
        // Upstream counted tokens off a JSON serialization with ensure_ascii=True,
        // so CJK text was measured as inflated `\uXXXX` escapes rather than the
        // characters the model actually sees (#7124). This port counts the
        // characters directly, so the escape inflation never existed here — pin
        // that: 4 CJK characters cost the same as 4 ASCII ones, not 6x more.
        let t = ApproxTokenizer;
        assert_eq!(t.count_tokens("日本語訳"), t.count_tokens("abcd"));

        let msg = Message::with_contents(Role::user(), vec![Content::text("日本語訳")]);
        assert_eq!(count_message_tokens(&t, &msg), 1);
    }

    #[test]
    fn count_message_tokens_sums_text_content() {
        let t = ApproxTokenizer;
        let msg = Message::with_contents(
            Role::user(),
            vec![Content::text("abcd"), Content::text("abcdefgh")],
        );
        // 1 + 2 = 3
        assert_eq!(count_message_tokens(&t, &msg), 3);
    }

    // ---- Truncation --------------------------------------------------------

    #[test]
    fn truncation_keeps_most_recent_messages() {
        let messages = vec![
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
            text(Role::assistant(), "4"),
        ];
        let strategy = Truncation::new(2);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].text(), "3");
        assert_eq!(out[1].text(), "4");
    }

    #[test]
    fn truncation_preserves_leading_system_messages() {
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
            text(Role::assistant(), "4"),
        ];
        let strategy = Truncation::new(2);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        // system preserved + 1 most recent (budget of 2 total)
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].role, Role::system());
        assert_eq!(out[0].text(), "sys");
        assert_eq!(out[1].text(), "4");
    }

    #[test]
    fn truncation_preserves_multiple_leading_system_messages() {
        let messages = vec![
            text(Role::system(), "sys1"),
            text(Role::system(), "sys2"),
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
        ];
        let strategy = Truncation::new(3);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        assert_eq!(out.len(), 3);
        assert_eq!(out[0].text(), "sys1");
        assert_eq!(out[1].text(), "sys2");
        assert_eq!(out[2].text(), "2");
    }

    #[test]
    fn truncation_noop_when_under_budget() {
        let messages = vec![text(Role::user(), "1"), text(Role::assistant(), "2")];
        let strategy = Truncation::new(10);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        assert_eq!(out, messages);
    }

    // ---- SlidingWindow -------------------------------------------------

    #[test]
    fn sliding_window_keeps_system_plus_last_n_non_system() {
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
        ];
        let strategy = SlidingWindow::new(2);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        assert_eq!(out.len(), 3);
        assert_eq!(out[0].text(), "sys");
        assert_eq!(out[1].text(), "2");
        assert_eq!(out[2].text(), "3");
    }

    #[test]
    fn sliding_window_with_no_system_message() {
        let messages = vec![
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
        ];
        let strategy = SlidingWindow::new(1);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].text(), "3");
    }

    // ---- TokenBudget --------------------------------------------------

    /// A tokenizer with a fixed per-message-call cost, for deterministic
    /// tests independent of exact text length.
    struct FixedTokenizer(usize);
    impl Tokenizer for FixedTokenizer {
        fn count_tokens(&self, _text: &str) -> usize {
            self.0
        }
    }

    #[test]
    fn token_budget_keeps_only_what_fits_from_the_newest_backward() {
        let messages = vec![
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
            text(Role::assistant(), "4"),
        ];
        // Each message costs a fixed 10 tokens; budget for 2 messages.
        let tokenizer = FixedTokenizer(10);
        let strategy = TokenBudget::new(25);
        let out = compact(&messages, &strategy, &tokenizer);
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].text(), "3");
        assert_eq!(out[1].text(), "4");
    }

    #[test]
    fn token_budget_preserves_leading_system_message_and_counts_it() {
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
        ];
        let tokenizer = FixedTokenizer(10);
        // System (10) + budget for one more message (<=20 total).
        let strategy = TokenBudget::new(20);
        let out = compact(&messages, &strategy, &tokenizer);
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].role, Role::system());
        assert_eq!(out[1].text(), "3");
    }

    #[test]
    fn token_budget_keeps_at_least_the_newest_message_even_if_it_alone_exceeds_budget() {
        let messages = vec![text(Role::user(), "1"), text(Role::assistant(), "2")];
        let tokenizer = FixedTokenizer(100);
        let strategy = TokenBudget::new(1);
        let out = compact(&messages, &strategy, &tokenizer);
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].text(), "2");
    }

    #[test]
    fn token_budget_keeps_everything_when_it_all_fits() {
        let messages = vec![text(Role::user(), "1"), text(Role::assistant(), "2")];
        let tokenizer = FixedTokenizer(1);
        let strategy = TokenBudget::new(1000);
        let out = compact(&messages, &strategy, &tokenizer);
        assert_eq!(out, messages);
    }

    // ---- SelectiveToolResult --------------------------------------------

    #[test]
    fn selective_tool_result_strips_stale_results_and_keeps_recent_ones() {
        let messages = vec![
            tool_call_message("c1", "t1"),
            tool_result_message("c1", "result 1"),
            tool_call_message("c2", "t2"),
            tool_result_message("c2", "result 2"),
            tool_call_message("c3", "t3"),
            tool_result_message("c3", "result 3"),
        ];
        let strategy = SelectiveToolResult::new(1);
        let out = compact(&messages, &strategy, &ApproxTokenizer);

        // Every message survives — the two oldest results keep their content
        // (so their calls stay answered) with only the payload replaced.
        assert_eq!(out.len(), 6);
        assert_eq!(
            out[1].function_results()[0].result,
            Some(json!(OMITTED_TOOL_RESULT))
        );
        assert_eq!(
            out[3].function_results()[0].result,
            Some(json!(OMITTED_TOOL_RESULT))
        );
        assert_eq!(out[5].function_results()[0].result, Some(json!("result 3")));
    }

    #[test]
    fn selective_tool_result_keeps_text_alongside_a_stripped_tool_result() {
        let mixed = Message::with_contents(
            Role::tool(),
            vec![
                Content::text("some accompanying text"),
                Content::FunctionResult(FunctionResultContent::new("c1", Some(json!("r1")))),
            ],
        );
        let messages = vec![
            tool_call_message("c1", "t1"),
            mixed,
            tool_call_message("c2", "t2"),
            tool_result_message("c2", "result 2"),
            tool_call_message("c3", "t3"),
            tool_result_message("c3", "result 3"),
        ];
        let strategy = SelectiveToolResult::new(2);
        let out = compact(&messages, &strategy, &ApproxTokenizer);

        // The first message's tool-result payload is compacted (only the two
        // most recent tool-result-bearing messages keep theirs), but its
        // accompanying text survives untouched alongside it.
        assert_eq!(out.len(), 6);
        assert_eq!(out[1].text(), "some accompanying text");
        assert_eq!(
            out[1].function_results()[0].result,
            Some(json!(OMITTED_TOOL_RESULT))
        );
        assert_eq!(out[3].function_results()[0].result, Some(json!("result 2")));
        assert_eq!(out[5].function_results()[0].result, Some(json!("result 3")));
    }

    #[test]
    fn selective_tool_result_noop_when_keep_last_covers_all() {
        let messages = vec![
            tool_call_message("c1", "t1"),
            tool_result_message("c1", "result 1"),
            tool_call_message("c2", "t2"),
            tool_result_message("c2", "result 2"),
        ];
        let strategy = SelectiveToolResult::new(5);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        assert_eq!(out, messages);
    }

    #[test]
    fn selective_tool_result_ignores_messages_without_tool_results() {
        let messages = vec![
            text(Role::system(), "sys"),
            text(Role::user(), "hi"),
            text(Role::assistant(), "hello"),
        ];
        let strategy = SelectiveToolResult::new(0);
        let out = compact(&messages, &strategy, &ApproxTokenizer);
        assert_eq!(out, messages);
    }

    // ---- CompactionProvider ---------------------------------------------

    #[tokio::test]
    async fn compaction_provider_before_run_replaces_ctx_messages_with_compacted_subset() {
        let provider = CompactionProvider::new(Truncation::new(2));
        let mut ctx = SessionContext::new(vec![]);
        ctx.messages = vec![
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
            text(Role::assistant(), "4"),
        ];
        provider.before_run(&mut ctx).await.unwrap();
        assert_eq!(ctx.messages.len(), 2);
        assert_eq!(ctx.messages[0].text(), "3");
        assert_eq!(ctx.messages[1].text(), "4");
    }

    #[tokio::test]
    async fn compaction_provider_with_tokenizer_uses_the_supplied_tokenizer() {
        struct FixedTokenizer(usize);
        impl Tokenizer for FixedTokenizer {
            fn count_tokens(&self, _text: &str) -> usize {
                self.0
            }
        }
        let provider = CompactionProvider::with_tokenizer(TokenBudget::new(25), FixedTokenizer(10));
        let mut ctx = SessionContext::new(vec![]);
        ctx.messages = vec![
            text(Role::user(), "1"),
            text(Role::assistant(), "2"),
            text(Role::user(), "3"),
            text(Role::assistant(), "4"),
        ];
        provider.before_run(&mut ctx).await.unwrap();
        // Budget of 25 with a fixed 10-token cost per message keeps 2 messages.
        assert_eq!(ctx.messages.len(), 2);
        assert_eq!(ctx.messages[0].text(), "3");
        assert_eq!(ctx.messages[1].text(), "4");
    }

    #[tokio::test]
    async fn compaction_provider_after_run_is_a_noop() {
        let provider = CompactionProvider::new(Truncation::new(1));
        provider
            .after_run(&[Message::new(Role::user(), "hi")], &[], None)
            .await
            .unwrap();
    }
}