vtcode-core 0.166.0

Core library for VT Code - a Rust-based terminal coding agent
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
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde_json::{Value, json};
use std::fmt::Write;
use std::sync::Arc;
use vtcode_commons::is_context_capacity_message;
use vtcode_commons::llm::FinishReason;
use vtcode_config::constants::context::DEFAULT_COMPACTION_TRIGGER_RATIO;

use crate::config::types::{ReasoningEffortLevel, VerbosityLevel};
use crate::exec::events::CompactionMode;
use crate::llm::reasoning_effort::ReasoningEffortMapper;
use crate::llm::utils::truncate_to_token_limit;
use crate::llm::{
    collect_single_response,
    provider::{
        LLMProvider, LLMRequest, LLMResponse, Message, MessageContent, MessageRole, ResponsesCompactionOptions,
        ToolChoice, ToolDefinition,
    },
};

pub mod auto;
pub mod memory_envelope;
pub mod prefire;
pub mod two_pass;

pub use crate::compaction::memory_envelope::{effective_context_budget, effective_session_context_budget};
pub use crate::compaction::prefire::{AsyncCompactionCache, PrefireState};

pub const SUPPRESS_NONE: u8 = 0;
pub const SUPPRESS_TURN: u8 = 1;
pub const SUPPRESS_STICKY: u8 = 2;
pub const SUPPRESS_UNTIL_SUCCESS: u8 = 3;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SuppressReason {
    CreditBlock,
    Size,
    Auth,
    Schema,
    Other,
}

impl SuppressReason {
    fn suppress_state(self) -> u8 {
        match self {
            SuppressReason::Size | SuppressReason::Schema => SUPPRESS_STICKY,
            SuppressReason::CreditBlock | SuppressReason::Auth => SUPPRESS_UNTIL_SUCCESS,
            SuppressReason::Other => SUPPRESS_TURN,
        }
    }
}

/// Classify a deterministic compaction failure's error text into a fixed
/// [`SuppressReason`] (drives telemetry + sticky-vs-per-turn scope).
pub(crate) fn classify_suppress_reason(error_msg: &str) -> SuppressReason {
    let m = error_msg.to_ascii_lowercase();
    if m.contains("spending-limit")
        || m.contains("spending limit")
        || m.contains("out of credits")
        || m.contains("usage balance exhausted")
        || m.contains("usage limit reached")
    {
        SuppressReason::CreditBlock
    } else if m.contains("context length") || m.contains("too many tokens") {
        SuppressReason::Size
    } else if m.contains("status 401") || m.contains("unauthorized") {
        SuppressReason::Auth
    } else if m.contains("invalid_request_error") {
        SuppressReason::Schema
    } else {
        SuppressReason::Other
    }
}

const DEFAULT_COMPACTION_TARGET_THRESHOLD: f64 = 0.50;
const DEFAULT_COMPACTION_KEEP_LAST_MESSAGES: usize = 10;
const DEFAULT_RETAINED_USER_MESSAGE_TOKENS: usize = 20_000;
const DEFAULT_RETAINED_USER_MESSAGES: usize = 6;
/// Internal continuity budget. This is deliberately not configuration: changing
/// it changes the shape of every compacted request and therefore provider cache
/// behavior.
const CONTINUITY_TAIL_TARGET_TOKENS: usize = 20_000;
/// Keep history below the model window after reserving space for the system
/// prompt, memory envelope, summary framing, and the next response.
const COMPACTION_CONTEXT_OVERHEAD_FRACTION_DENOMINATOR: usize = 8;
const COMPACTION_CONTEXT_FIXED_OVERHEAD_TOKENS: usize = 512;
const SUMMARY_PREFIX: &str = "Previous conversation summary:\n";
const ABSTRACT_PREFIX: &str = "Earlier context (abstract):\n";
const DETAIL_PREFIX: &str = "Recent context (summary):\n";

/// Default summarization prompt. Structures the summary for continuity: after
/// reading it, the next context must feel like a seamless continuation, not a
/// fresh start. Kept as a `const` so `CompactionConfig::default` does not
/// re-allocate a ~1KB literal on every construction.
const DEFAULT_SUMMARY_PROMPT: &str = "Summarize the conversation so far using this exact structure. The goal is continuity: after reading this summary, the next context must feel like a seamless continuation of the same task, not a fresh start.\n\n## Goal\n[What the user is trying to accomplish]\n\n## Constraints & Preferences\n- [Requirements, preferences, or constraints from the user]\n\n## What I Was Just Doing\n[The single most recent action in progress: what step the agent was executing, which tool or edit was underway, and where it left off. This is the continuity anchor.]\n\n## Last Action & Result\n[The last completed action and its outcome (success, error, or partial). Include the exact error or status if relevant.]\n\n## Progress\n### Done\n- [Completed work]\n\n### In Progress\n- [Current work]\n\n### Blocked\n- [Blocking issues, if any]\n\n## Key Decisions\n- **[Decision]**: [Reason]\n\n## Next Steps\n1. [Most important next step]\n\n## Critical Context\n- [Facts needed to continue]\n\nKeep it concise and actionable. Always preserve the current task objective and acceptance criteria, file paths that were read or modified, test results and error messages, and decisions with their reasoning.";

/// Compaction configuration for context window management.
#[derive(Debug, Clone)]
pub struct CompactionConfig {
    /// Threshold (0.0-1.0) at which to trigger compaction.
    pub trigger_threshold: f64,
    /// Target usage ratio (0.0-1.0) after compaction.
    pub target_threshold: f64,
    /// Prompt for summarization.
    pub summary_prompt: String,
    /// Legacy short-circuit used to skip local compaction for tiny histories.
    pub keep_last_messages: usize,
    /// Total token budget reserved for retaining real user messages verbatim.
    pub retained_user_message_tokens: usize,
    /// Maximum number of recent user messages to retain verbatim.
    pub retained_user_messages: usize,
    /// Force local summarization even for short histories and providers with native compaction.
    pub always_summarize: bool,
    /// Enable hierarchical summarization (multi-level pyramid).
    ///
    /// When `true`, compaction produces three tiers instead of a flat summary:
    /// - **Abstract**: oldest turns compressed into 1-2 sentences
    /// - **Detail**: middle turns summarized into a paragraph
    /// - **Verbatim**: most recent turns kept as-is
    ///
    /// When `false` (default), all old turns become a single flat summary.
    pub hierarchical: bool,
    /// Auto-compaction suppression state. `SUPPRESS_NONE` (0) means compaction
    /// is allowed; any other value gates automatic compaction until the
    /// appropriate clearing event (success, model switch, etc.).
    pub auto_compact_suppressed: u8,
}

impl Default for CompactionConfig {
    fn default() -> Self {
        Self {
            trigger_threshold: DEFAULT_COMPACTION_TRIGGER_RATIO,
            target_threshold: DEFAULT_COMPACTION_TARGET_THRESHOLD,
            summary_prompt: DEFAULT_SUMMARY_PROMPT.to_string(),
            keep_last_messages: DEFAULT_COMPACTION_KEEP_LAST_MESSAGES,
            retained_user_message_tokens: DEFAULT_RETAINED_USER_MESSAGE_TOKENS,
            retained_user_messages: DEFAULT_RETAINED_USER_MESSAGES,
            always_summarize: false,
            hierarchical: false,
            auto_compact_suppressed: SUPPRESS_NONE,
        }
    }
}

/// Per-route compaction policy in the DeepSeek-harness shape.
///
/// `threshold_ratio` caps the auto-compaction trigger as a fraction of the
/// route window (fail-safe: it can only fire *earlier*). `retain_ratio` sizes
/// the verbatim continuity tail as a fraction of the route window so small
/// windows keep a usable summary instead of a tail-only history.
/// `max_overflow_retries` bounds extra bounded-fork attempts after a
/// context-capacity rejection.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CompactionRoutePolicy {
    /// Trigger ceiling as a fraction of the route window. `1.0` preserves the
    /// absolute-tokens trigger unchanged.
    pub threshold_ratio: f64,
    /// Verbatim-tail share of the route window.
    pub retain_ratio: f64,
    /// Extra attempts after a context-capacity rejection (progressive halving).
    pub max_overflow_retries: u32,
}

/// Floor keeping the continuity anchor meaningful on tiny windows.
const MIN_TAIL_TARGET_TOKENS: usize = 1_024;

impl Default for CompactionRoutePolicy {
    fn default() -> Self {
        Self {
            threshold_ratio: 1.0,
            retain_ratio: 0.16,
            max_overflow_retries: 1,
        }
    }
}

/// Per-route overrides, matched as `(provider_substring, model_substring)`.
/// Empty until a route demonstrates a need: the window-derived defaults
/// already adapt retention and budgets to every route's resolved capacity.
const ROUTE_POLICIES: &[(&str, &str, CompactionRoutePolicy)] = &[];

impl CompactionRoutePolicy {
    /// Resolve the policy for a provider/model route (first table hit wins,
    /// otherwise the default).
    #[must_use]
    pub fn resolve(provider_name: &str, model: &str) -> Self {
        ROUTE_POLICIES
            .iter()
            .find(|(provider, model_match, _)| provider_name.contains(provider) && model.contains(model_match))
            .map(|(_, _, policy)| *policy)
            .unwrap_or_default()
    }

    /// Verbatim-tail budget for a route window. Unknown windows (`0`) keep the
    /// legacy constant; otherwise the tail scales with the window so small
    /// routes keep a usable summary instead of a tail-only history.
    #[allow(
        clippy::cast_sign_loss,
        reason = "The ratio is clamped to [0.0, 1.0] and the window is non-negative, so the product cannot be negative."
    )]
    #[must_use]
    pub fn tail_target_tokens(&self, window_tokens: usize) -> usize {
        if window_tokens == 0 {
            return CONTINUITY_TAIL_TARGET_TOKENS;
        }
        ((window_tokens as f64 * self.retain_ratio.clamp(0.0, 1.0)) as usize)
            .clamp(MIN_TAIL_TARGET_TOKENS, CONTINUITY_TAIL_TARGET_TOKENS)
    }

    /// Fail-safe trigger cap: only fires compaction *earlier*, never later.
    /// A `1.0` ratio (the default) leaves the absolute-tokens trigger
    /// unchanged.
    #[allow(
        clippy::cast_sign_loss,
        reason = "The ratio is clamped to [0.0, 1.0] and the window is non-negative, so the product cannot be negative."
    )]
    #[must_use]
    pub fn apply_threshold_cap(&self, threshold_tokens: usize, window_tokens: usize) -> usize {
        if self.threshold_ratio < 1.0 && window_tokens > 0 {
            threshold_tokens.min((window_tokens as f64 * self.threshold_ratio.clamp(0.0, 1.0)) as usize)
        } else {
            threshold_tokens
        }
    }
}

/// Resolve the route tail budget from the session budget (already intersected
/// with the provider window) or the provider window when unset.
#[must_use]
pub(crate) fn route_tail_target_tokens(
    provider: &dyn LLMProvider,
    model: &str,
    context_budget: Option<usize>,
) -> usize {
    let policy = CompactionRoutePolicy::resolve(provider.name(), model);
    let window = context_budget
        .filter(|value| *value > 0)
        .unwrap_or_else(|| provider.effective_context_size(model));
    policy.tail_target_tokens(window)
}

/// Parent request prefix for cache-safe compaction forking.
///
/// Prompt caching is a prefix match: a compaction request only reuses the
/// parent conversation's cached prefix when it carries the *exact same*
/// system prompt, tool definitions, and history prefix, with the compaction
/// instruction appended as the final new turn. A standalone single-message
/// summary prompt pays the full uncached input rate for the entire history.
#[derive(Debug, Clone, Default)]
pub struct CompactionParentContext {
    /// Exact system prompt of the parent segment (`request_envelope.system_prompt()`).
    pub system_prompt: Option<Arc<str>>,
    /// Exact ordered tool catalog of the parent segment
    /// (`request_envelope.ordered_tools()`). Empty catalogs should be `None`.
    pub tools: Option<Arc<Vec<ToolDefinition>>>,
}

impl CompactionParentContext {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.system_prompt.is_none() && self.tools.is_none()
    }
}

/// Build a cache-safe compaction history: the parent's full conversation
/// prefix verbatim, plus the compaction instruction as the only new turn.
/// From the provider's perspective this looks nearly identical to the
/// parent's last request, so the cached prefix is reused.
#[must_use]
pub fn build_cache_safe_compaction_history(history: &[Message], compaction_prompt: &str) -> Vec<Message> {
    let mut forked = Vec::with_capacity(history.len().saturating_add(1));
    forked.extend(history.iter().cloned());
    forked.push(Message::user(compaction_prompt.to_string()));
    forked
}

/// Bound the summarizer's input to the resolved context budget.
///
/// The local-summary path forks the parent's entire conversation verbatim and
/// appends the compaction instruction, but nothing bounded that fork. On a
/// near-full context (the normal reason to run `/compact`) or after switching
/// to a model with a smaller window, the summary request itself exceeded the
/// summarizer's window and the provider rejected it, failing the whole
/// compaction. Local output paths are bounded by
/// [`bound_compacted_history_to_context`]; native provider output is treated as
/// canonical and must not be rewritten. Bound the input so local requests fit:
/// keep the newest complete protocol groups that fit and drop the oldest.
///
/// Returns the history to append the instruction to (see
/// [`build_cache_safe_compaction_history`]), not the finished fork.
fn bound_history_for_summarization(history: &[Message], instructions: &str, budget: Option<usize>) -> Vec<Message> {
    let Some(budget) = budget.filter(|value| *value > 0) else {
        return history.to_vec();
    };
    let instruction_tokens = Message::user(instructions.to_string()).estimate_tokens();
    let history_budget = budget.saturating_sub(instruction_tokens).max(4);
    bound_history_to_token_budget(history, history_budget)
}

/// Bound a native compaction request while retaining the latest provider
/// compaction marker. Provider-native windows use that opaque marker as the
/// continuity anchor; dropping it while selecting a recent suffix loses the
/// state needed to replay the window on the next request.
fn bound_history_for_native_compaction(history: &[Message], instructions: &str, budget: Option<usize>) -> Vec<Message> {
    let Some(budget) = budget.filter(|value| *value > 0) else {
        return history.to_vec();
    };
    let instruction_tokens = Message::user(instructions.to_string()).estimate_tokens();
    let history_budget = budget.saturating_sub(instruction_tokens).max(4);
    let total_tokens = history.iter().map(Message::estimate_tokens).sum::<usize>();
    if total_tokens <= history_budget {
        return history.to_vec();
    }

    let Some(marker_index) = history.iter().rposition(is_provider_compaction_message) else {
        return bound_history_to_token_budget(history, history_budget);
    };
    let marker = history[marker_index].clone();
    let marker_tokens = marker.estimate_tokens();
    let mut bounded = vec![marker];
    if marker_tokens < history_budget {
        bounded.extend(bound_history_to_token_budget(&history[marker_index + 1..], history_budget - marker_tokens));
    }
    bounded
}

fn bound_history_to_token_budget(history: &[Message], history_budget: usize) -> Vec<Message> {
    let total_tokens = history.iter().map(Message::estimate_tokens).sum::<usize>();
    if total_tokens <= history_budget {
        return history.to_vec();
    }

    let group_starts: Vec<usize> = history
        .iter()
        .enumerate()
        .filter_map(|(index, message)| (message.role == MessageRole::User).then_some(index))
        .collect();
    let last_group_index = group_starts.len().saturating_sub(1);

    let mut selected_start = history.len();
    let mut selected_end = history.len();
    let mut selected_tokens = 0usize;
    for (position, &start) in group_starts.iter().enumerate().rev() {
        let natural_end = group_starts.get(position + 1).copied().unwrap_or(history.len());
        // The trailing group may end on an unanswered tool call; drop that
        // invalid protocol suffix instead of shipping it to the provider.
        let end = if position == last_group_index {
            start.saturating_add(complete_protocol_group_prefix(&history[start..natural_end]))
        } else {
            natural_end
        };
        if start >= end {
            continue;
        }
        let group_tokens = history[start..end].iter().map(Message::estimate_tokens).sum::<usize>();
        if selected_tokens.saturating_add(group_tokens) > history_budget {
            break;
        }
        if selected_start == history.len() {
            selected_end = end;
        }
        selected_tokens += group_tokens;
        selected_start = start;
    }

    if selected_start >= selected_end {
        // No complete protocol group fits (for example a single oversized tool
        // result). Degrade to protocol-bounded previews so the request still
        // goes out instead of failing the entire compaction.
        return bounded_protocol_group(history, history_budget);
    }
    history[selected_start..selected_end].to_vec()
}

fn is_provider_compaction_message(message: &Message) -> bool {
    message.role == MessageRole::Assistant
        && message
            .reasoning_details
            .as_ref()
            .is_some_and(|details| details.iter().any(is_compaction_detail))
}

#[cfg(test)]
mod summarization_fork_bounds_tests {
    use super::{Message, bound_history_for_native_compaction, bound_history_for_summarization};

    const INSTRUCTIONS: &str = "Summarize now.";

    fn total_tokens(messages: &[Message]) -> usize {
        messages.iter().map(Message::estimate_tokens).sum()
    }

    #[test]
    fn keeps_history_verbatim_when_it_already_fits() {
        let history = vec![Message::user("a".repeat(4_000)), Message::user("b".repeat(4_000))];
        let bounded = bound_history_for_summarization(&history, INSTRUCTIONS, Some(10_000_000));
        assert_eq!(bounded.len(), history.len());
        assert_eq!(total_tokens(&bounded), total_tokens(&history));
    }

    #[test]
    fn keeps_history_verbatim_when_budget_is_unknown() {
        let history = vec![Message::user("x".repeat(40_000))];
        let bounded = bound_history_for_summarization(&history, INSTRUCTIONS, None);
        assert_eq!(bounded.len(), 1);
        assert_eq!(total_tokens(&bounded), total_tokens(&history));
    }

    #[test]
    fn trims_oldest_groups_when_over_budget_and_keeps_the_newest_turn() {
        let history = vec![
            Message::user("old".repeat(2_000)),
            Message::user("middle".repeat(2_000)),
            Message::user("newest".repeat(64)),
        ];
        // Derive the limit from the measured fixture so this remains a
        // genuine over-budget case across tokenizer changes.
        let budget = total_tokens(&history).saturating_sub(1);
        let bounded = bound_history_for_summarization(&history, INSTRUCTIONS, Some(budget));
        assert!(bounded.len() < history.len(), "expected trimming, kept {}", bounded.len());
        assert_eq!(
            bounded.last().unwrap().content.as_text().as_ref(),
            history.last().unwrap().content.as_text().as_ref(),
            "the newest turn must survive the trim"
        );
        assert!(total_tokens(&bounded) <= budget);
    }

    #[test]
    fn falls_back_to_protocol_previews_when_no_group_fits() {
        let history = vec![Message::user("x".repeat(40_000))];
        // Derive the limit from the measured fixture so this remains a
        // genuine no-group-fits case across tokenizer changes.
        let budget = total_tokens(&history) / 4;
        let bounded = bound_history_for_summarization(&history, INSTRUCTIONS, Some(budget));
        assert_eq!(bounded.len(), 1);
        assert!(total_tokens(&bounded) < total_tokens(&history), "an oversized single group must still be reduced");
        assert!(
            total_tokens(&bounded) <= budget,
            "fallback must respect budget {budget}, used {}",
            total_tokens(&bounded)
        );
    }

    #[test]
    fn native_bound_preserves_latest_provider_compaction_marker() {
        let marker = Message::assistant(String::new()).with_reasoning_details(Some(vec![serde_json::json!({
            "type": "compaction",
            "content": null,
            "encrypted_content": "opaque-state",
        })]));
        let newest = Message::user("newest".repeat(64));
        let history = vec![marker.clone(), Message::user("old".repeat(4_000)), newest.clone()];
        let instruction_tokens = Message::user(INSTRUCTIONS.to_string()).estimate_tokens();
        let history_budget = marker.estimate_tokens() + newest.estimate_tokens() + 4;
        let bounded =
            bound_history_for_native_compaction(&history, INSTRUCTIONS, Some(instruction_tokens + history_budget));

        assert_eq!(bounded.first(), Some(&marker));
        assert_eq!(bounded.last(), Some(&newest));
        assert!(bounded.len() < history.len(), "expected old pre-compaction history to be dropped");
    }
}

/// Build a cache-safe local-summary request that reuses the parent prefix.
/// `tool_choice` is forced to `none` so the summarizer cannot spend the
/// compaction pass on tool calls while the system/tools/messages prefix
/// stays identical to the parent's last request.
fn compaction_summary_request(
    model: &str,
    history: &[Message],
    instructions: &str,
    max_output_tokens: Option<u32>,
    reasoning_effort: Option<ReasoningEffortLevel>,
    verbosity: Option<VerbosityLevel>,
    parent: Option<&CompactionParentContext>,
) -> LLMRequest {
    LLMRequest {
        messages: Arc::new(build_cache_safe_compaction_history(history, instructions)),
        model: model.to_string(),
        system_prompt: parent.and_then(|parent| parent.system_prompt.clone()),
        tools: parent.and_then(|parent| parent.tools.clone()).filter(|tools| !tools.is_empty()),
        tool_choice: Some(ToolChoice::none()),
        max_tokens: max_output_tokens,
        reasoning_effort,
        verbosity,
        ..Default::default()
    }
}

/// Compact conversation history using the configured summarizer.
#[cfg_attr(feature = "profiling", hotpath::measure)]
pub async fn compact_history(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
) -> Result<Vec<Message>> {
    compact_history_with_budget(provider, model, history, config, None).await
}

/// Compact conversation history using a caller-resolved context budget for
/// input bounding and locally rebuilt output. Standalone native responses are
/// returned as the provider's canonical next context window.
pub async fn compact_history_with_budget(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    context_budget: Option<usize>,
) -> Result<Vec<Message>> {
    if history.is_empty() {
        return Ok(Vec::new());
    }

    if !config.always_summarize && history.len() <= config.keep_last_messages {
        // Message-count retention is only a fast path. A single large tool
        // result can exceed the resolved token budget even when the history
        // contains fewer messages than `keep_last_messages`.
        return Ok(bound_compacted_history_to_context(history.to_vec(), provider, model, context_budget));
    }

    // `supports_responses_compaction` is shared by standalone Responses
    // endpoints and Anthropic's inline context-management path. This legacy
    // function calls `compact_history` directly, so only the narrower
    // standalone capability is safe here; inline providers use the manual
    // strategy dispatcher below (or the local fallback on this legacy path).
    if !config.always_summarize && provider.supports_manual_openai_compaction(model) {
        let native_source = bound_history_for_native_compaction(
            history,
            "",
            compaction_history_budget(provider, model, context_budget),
        );
        let compacted = provider
            .compact_history(model, &native_source)
            .await
            .context("Failed to compact history via Responses compact endpoint")?;
        // A standalone compaction response is the provider's canonical next
        // context window. Do not append a locally selected continuity tail or
        // discard opaque provider items from it. The provider has already
        // produced the exact window that must be replayed on the next turn.
        return Ok(compacted);
    }

    let effective_config = context_bounded_compaction_config(provider, model, history, config, context_budget);
    // Cache-safe forking: prepend the parent's full conversation prefix and
    // append the compaction instruction, instead of reformatting the entire
    // history into a single new user message (which would pay the full
    // uncached input rate). No parent system/tools are available on this
    // legacy path; callers with a request envelope should prefer
    // `compact_history_manual_with_parent_context`.
    // Bound the fork so the summary request itself fits the summarizer window;
    // an unbounded fork is what made `/compact` fail on near-full or
    // smaller-window (model-switch) histories.
    let history_budget = compaction_history_budget(provider, model, context_budget);
    let tail_target = route_tail_target_tokens(provider, model, context_budget);
    // Trim oversized tool outputs before bounding so large dumps do not evict
    // whole protocol groups from the summarizer fork.
    let pruned_history = prune_oversized_tool_outputs(history);
    let summary_source =
        bound_history_for_summarization(&pruned_history, &effective_config.summary_prompt, history_budget);
    let summary = generate_local_summary_with_retry(
        provider,
        model,
        &pruned_history,
        &summary_source,
        &effective_config.summary_prompt,
        history_budget,
        &ManualCompactionOptions::default(),
        None,
    )
    .await?;

    Ok(bound_compacted_history_to_context(
        build_local_compacted_history(
            history,
            &summary,
            effective_config.retained_user_message_tokens,
            effective_config.retained_user_messages,
            // Keep the same protocol-safe continuity tail as the live/manual
            // paths. Forked histories must not lose the newest working turn.
            true,
            tail_target,
        ),
        provider,
        model,
        context_budget,
    ))
}

/// How the manual `/compact` command compacts for a given provider/model.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompactionStrategy {
    /// Provider exposes a standalone on-demand compaction endpoint
    /// (OpenAI `/responses/compact`). Delegates to `LLMProvider::compact_history_with_options`.
    NativeStandalone,
    /// Provider compacts inline via request fields, threshold-triggered
    /// (Anthropic `compact_20260112`). Invoked through the capability-aware
    /// one-shot response collector with `context_management` set and
    /// `pause_after_compaction`.
    NativeInline,
    /// Universal fallback: summarize history via the capability-aware one-shot
    /// response collector and rebuild as a summary message plus retained recent
    /// user messages. Works for every provider.
    Local,
}

/// Select the manual-compaction strategy for a provider/model.
///
/// `NativeStandalone` when the provider opts in via `supports_manual_openai_compaction`
/// (e.g. OpenAI `/responses/compact`), `NativeInline` when the provider reports
/// inline compaction support via `supports_native_inline_compaction` (e.g.
/// Anthropic `compact_20260112`), otherwise `Local`.
///
/// Note: `supports_responses_compaction` is intentionally *not* the discriminator
/// for `NativeInline`. It is overloaded — true for both OpenAI-compatible
/// standalone compaction and Anthropic inline compaction — so OpenAI-compatible
/// custom endpoints (which report it but cannot serve an Anthropic
/// `compact_20260112` edit) would otherwise be misrouted to `NativeInline` and
/// waste a rejected `generate` call before falling back to `Local`.
pub fn manual_compaction_strategy(provider: &dyn LLMProvider, model: &str) -> CompactionStrategy {
    if provider.supports_manual_openai_compaction(model) {
        CompactionStrategy::NativeStandalone
    } else if provider.supports_native_inline_compaction(model) {
        CompactionStrategy::NativeInline
    } else {
        CompactionStrategy::Local
    }
}

/// Whether a compaction result is worth keeping.
///
/// Local compaction always appends framing around the summarized history (a
/// summary message plus the persisted session-memory envelope), so a result
/// whose message count is not strictly smaller than the input grows the
/// conversation instead of compacting it. This happens when the continuity
/// tail already covers the whole history (small, tool-heavy sessions where a
/// handful of user turns anchor dozens of assistant/tool messages): the
/// rebuild keeps every message and adds framing on top (observed as
/// `86 -> 88`). Such results must be discarded in favor of the already-compact
/// path so callers never report growth as a successful compaction.
///
/// The check is intentionally message-count based: that is the unit surfaced
/// in user-facing progress (`86 -> 12`) and harness `compact_boundary`
/// events, so the persisted history must never exceed it.
#[must_use]
pub fn compacted_history_shrinks(original_len: usize, compacted_len: usize, mode: CompactionMode) -> bool {
    // Local mode injects one envelope message into the returned history during
    // persistence; account for it here so the *final* history is what shrinks.
    // Provider-native windows (and the `Unknown` catch-all, which never arises
    // from a live compaction pass) are canonical replay state with no envelope
    // injected, so the raw lengths compare directly.
    let envelope_messages = match mode {
        CompactionMode::Local => 1,
        CompactionMode::Provider | CompactionMode::Unknown => 0,
    };
    compacted_len.saturating_add(envelope_messages) < original_len
}

/// Universally meaningful manual-compaction options.
///
/// Provider-specific extras (OpenAI `service_tier` / `prompt_cache_key` / `store` /
/// `include`) are intentionally absent: the manual `/compact` command exposes only
/// the options that apply across every provider.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ManualCompactionOptions {
    /// Overrides the default summary/compaction prompt when set.
    pub instructions: Option<String>,
    /// Caps the summary/compaction output length on every provider.
    pub max_output_tokens: Option<u32>,
    /// Optional reasoning effort override for the compaction pass.
    pub reasoning_effort: Option<ReasoningEffortLevel>,
    /// Permit an explicit lower supported effort when the requested level is
    /// unavailable on the selected provider/model route. The default is
    /// strict blocking so compaction never silently changes reasoning
    /// fidelity.
    pub allow_reasoning_effort_downgrade: bool,
    /// Optional verbosity override for the compaction output.
    pub verbosity: Option<VerbosityLevel>,
}

impl From<ManualCompactionOptions> for ResponsesCompactionOptions {
    fn from(options: ManualCompactionOptions) -> Self {
        Self {
            instructions: options.instructions,
            max_output_tokens: options.max_output_tokens,
            reasoning_effort: options.reasoning_effort,
            verbosity: options.verbosity,
            responses_include: None,
            response_store: None,
            service_tier: None,
            prompt_cache_key: None,
        }
    }
}

impl CompactionConfig {
    /// Return a config with the manual options' instructions applied as the
    /// summary prompt override. The remaining option fields
    /// (`max_output_tokens`, `reasoning_effort`, `verbosity`) are applied to the
    /// summary `LLMRequest` directly by `summarize_locally`, not stored here.
    fn with_manual_overrides(self, options: &ManualCompactionOptions) -> Self {
        let summary_prompt = options
            .instructions
            .clone()
            .map(|instructions| instructions.trim().to_string())
            .filter(|instructions| !instructions.is_empty())
            .unwrap_or(self.summary_prompt);
        Self { summary_prompt, ..self }
    }
}

/// Compact history for the manual `/compact` command using provider-native
/// compaction when available, falling back to local summarization otherwise.
///
/// Returns the compacted messages and the `CompactionMode` that produced them
/// (`Provider` for native compaction, `Local` for client-side summarization).
#[cfg_attr(feature = "profiling", hotpath::measure)]
pub async fn compact_history_manual(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    options: &ManualCompactionOptions,
) -> Result<(Vec<Message>, CompactionMode)> {
    compact_history_manual_with_budget(provider, model, history, config, options, None).await
}

/// Manual compaction with the resolved session context budget applied to native
/// request inputs and locally rebuilt summaries. Native provider responses are
/// returned unchanged so opaque continuation state remains valid.
pub async fn compact_history_manual_with_budget(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    options: &ManualCompactionOptions,
    context_budget: Option<usize>,
) -> Result<(Vec<Message>, CompactionMode)> {
    compact_history_manual_with_parent_context(provider, model, history, config, options, context_budget, None).await
}

/// Manual compaction that reuses the parent segment's cached prefix.
///
/// Pass the live request envelope's system prompt + ordered tools as `parent`
/// so the local-summary fork (`summarize_locally`) hits the provider prompt
/// cache instead of re-paying full input cost for the entire history.
pub async fn compact_history_manual_with_parent_context(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    options: &ManualCompactionOptions,
    context_budget: Option<usize>,
    parent: Option<&CompactionParentContext>,
) -> Result<(Vec<Message>, CompactionMode)> {
    if history.is_empty() {
        return Ok((Vec::new(), CompactionMode::Local));
    }
    let options = resolve_manual_compaction_options(provider, model, options)?;
    match manual_compaction_strategy(provider, model) {
        CompactionStrategy::NativeStandalone => {
            let responses_options: ResponsesCompactionOptions = options.clone().into();
            // Bound the native input the same way as the local fork: a
            // near-full history would otherwise exceed the summarizer window
            // and fail the whole `/compact` command.
            let native_source = bound_history_for_native_compaction(
                history,
                options.instructions.as_deref().unwrap_or(""),
                compaction_history_budget(provider, model, context_budget),
            );
            let compacted = provider
                .compact_history_with_options(model, &native_source, &responses_options)
                .await
                .context("Failed to compact history via provider-native compaction")?;
            // A standalone compaction response is the provider's canonical
            // next context window. Keep its retained items and opaque
            // compaction items intact; the next provider request must receive
            // this window as returned rather than a locally pruned variant.
            Ok((compacted, CompactionMode::Provider))
        }
        CompactionStrategy::NativeInline => {
            compact_history_native_inline(provider, model, history, config, &options, context_budget, parent).await
        }
        CompactionStrategy::Local => {
            let compacted =
                summarize_locally(provider, model, history, config, &options, context_budget, parent).await?;
            Ok((compacted, CompactionMode::Local))
        }
    }
}

/// Resolve a manual compaction effort before selecting a provider strategy.
/// Every strategy eventually emits an `LLMRequest` or provider compaction
/// options, so validating once at this boundary prevents native, inline, and
/// hierarchical paths from silently coercing unsupported levels.
fn resolve_manual_compaction_options(
    provider: &dyn LLMProvider,
    model: &str,
    options: &ManualCompactionOptions,
) -> Result<ManualCompactionOptions> {
    let Some(requested) = options.reasoning_effort else {
        return Ok(options.clone());
    };

    let mapping = ReasoningEffortMapper::resolve(provider, model, requested, options.allow_reasoning_effort_downgrade)
        .with_context(|| {
            format!("Failed to resolve compaction reasoning effort for {} / {}", provider.name(), model)
        })?;

    if mapping.degraded() {
        tracing::warn!(
            provider = provider.name(),
            model,
            requested = %mapping.requested,
            effective = %mapping.effective,
            "Compaction reasoning effort explicitly downgraded"
        );
    }

    let mut resolved = options.clone();
    resolved.reasoning_effort = Some(mapping.effective);
    Ok(resolved)
}

/// Native inline compaction (Anthropic `compact_20260112`).
///
/// Forces a compaction pass by setting the minimum trigger threshold with
/// `pause_after_compaction: true`, so the response contains only the compaction
/// block. If compaction does not fire (history below the provider's minimum
/// trigger, currently 50k tokens for Anthropic), transparently falls back to
/// local summarization so the manual command always succeeds.
///
/// The inline request already carries the full parent history; the parent
/// system/tools prefix is attached as well so the fork hits the provider
/// prompt cache. `tool_choice` stays `none` so the pass cannot be spent on
/// tool calls, and both local fallbacks forward `parent` for the same reason.
/// Anthropic inline context-management triggers, matching the provider docs:
/// thinking blocks are cheapest to clear, tool results next, and full
/// compaction is the most aggressive last resort.
const ANTHROPIC_COMPACT_TRIGGER_FLOOR: u64 = 50_000;
const ANTHROPIC_CLEAR_TOOL_USES_TRIGGER_TOKENS: u64 = 100_000;
const ANTHROPIC_CLEAR_TOOL_USES_KEEP: u64 = 3;
const ANTHROPIC_CLEAR_THINKING_KEEP_TURNS: u64 = 2;

/// Build the Anthropic inline `context_management.edits` ladder.
///
/// Order matters: the thinking-block edit runs first, then tool-result
/// clearing, then the `compact_20260112` summarization edit. The cheaper
/// clearing edits are only included when the provider reports
/// `supports_context_edits`; otherwise the request carries the compact edit
/// alone so non-Anthropic inline routes keep working.
fn anthropic_inline_compaction_edits(instructions: Option<&str>, include_context_edits: bool) -> Vec<Value> {
    let mut edits = Vec::with_capacity(3);
    if include_context_edits {
        edits.push(json!({
            "type": "clear_thinking_20251015",
            "keep": { "type": "thinking_turns", "value": ANTHROPIC_CLEAR_THINKING_KEEP_TURNS },
        }));
        edits.push(json!({
            "type": "clear_tool_uses_20250919",
            "trigger": { "type": "input_tokens", "value": ANTHROPIC_CLEAR_TOOL_USES_TRIGGER_TOKENS },
            "keep": { "type": "tool_uses", "value": ANTHROPIC_CLEAR_TOOL_USES_KEEP },
        }));
    }
    let mut compact_edit = serde_json::Map::new();
    compact_edit.insert("type".to_string(), json!("compact_20260112"));
    compact_edit
        .insert("trigger".to_string(), json!({ "type": "input_tokens", "value": ANTHROPIC_COMPACT_TRIGGER_FLOOR }));
    compact_edit.insert("pause_after_compaction".to_string(), json!(true));
    if let Some(instructions) = instructions.map(str::trim).filter(|instructions| !instructions.is_empty()) {
        compact_edit.insert("instructions".to_string(), json!(instructions));
    }
    edits.push(Value::Object(compact_edit));
    edits
}

async fn compact_history_native_inline(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    options: &ManualCompactionOptions,
    context_budget: Option<usize>,
    parent: Option<&CompactionParentContext>,
) -> Result<(Vec<Message>, CompactionMode)> {
    let edits =
        anthropic_inline_compaction_edits(options.instructions.as_deref(), provider.supports_context_edits(model));

    // Bound the inline request input so a near-full history fits the
    // summarizer window, mirroring the local-summary fork bound.
    let inline_source = bound_history_for_native_compaction(
        history,
        options.instructions.as_deref().unwrap_or(""),
        compaction_history_budget(provider, model, context_budget),
    );
    let request = LLMRequest {
        messages: Arc::new(inline_source),
        model: model.to_string(),
        system_prompt: parent.and_then(|parent| parent.system_prompt.clone()),
        tools: parent.and_then(|parent| parent.tools.clone()).filter(|tools| !tools.is_empty()),
        tool_choice: Some(ToolChoice::none()),
        context_management: Some(json!({ "edits": edits })),
        max_tokens: options.max_output_tokens,
        reasoning_effort: options.reasoning_effort,
        verbosity: options.verbosity,
        ..Default::default()
    };

    // The inline compaction request is Anthropic-specific (`compact_20260112`).
    // If the provider does not actually support inline compaction (e.g. it was
    // selected because it exposes a different standalone Responses compact
    // endpoint) the request may be rejected. Per the manual `/compact` contract
    // ("always succeeds"), swallow the inline error and fall back to local
    // summarization rather than aborting the whole command.
    let response = match collect_single_response(provider, request).await {
        Ok(response) => response,
        Err(error) => {
            tracing::warn!(
                error = ?error,
                "provider-native inline compaction request failed; \
                 falling back to local summarization"
            );
            let compacted =
                summarize_locally(provider, model, history, config, options, context_budget, parent).await?;
            return Ok((compacted, CompactionMode::Local));
        }
    };

    if response.finish_reason == FinishReason::Pause
        && let Some(summary) = response
            .compaction
            .as_ref()
            .map(|summary| summary.trim())
            .filter(|summary| !summary.is_empty())
    {
        let effective_config = context_bounded_compaction_config(provider, model, history, config, context_budget);
        let tail_target = route_tail_target_tokens(provider, model, context_budget);
        if let Some(detail) = response_compaction_detail(&response) {
            // The provider compaction block is opaque state. Preserve the exact
            // block and the selected protocol-safe tail for replay; applying the
            // generic local bound here could turn it into an ordinary text/system
            // message and invalidate the provider's continuation contract.
            return Ok((
                build_provider_compacted_history(history, detail, &effective_config, true, tail_target),
                CompactionMode::Provider,
            ));
        }

        // A provider may expose only the public summary field without the raw
        // continuation block. Treat that response as an ordinary local summary
        // so it receives the local context bound and is not mislabeled as a
        // provider-native replay window.
        let compacted = bound_compacted_history_to_context(
            build_summary_compacted_history(history, summary, &effective_config, true, tail_target),
            provider,
            model,
            context_budget,
        );
        return Ok((compacted, CompactionMode::Local));
    }

    // Compaction did not fire (e.g. history below the minimum trigger threshold);
    // fall back to local summarization so the manual command always succeeds.
    let compacted = summarize_locally(provider, model, history, config, options, context_budget, parent).await?;
    Ok((compacted, CompactionMode::Local))
}

/// Local (provider-agnostic) summarization compaction.
///
/// Forks the parent conversation cache-safely: same history prefix plus the
/// compaction instruction appended, with the parent system/tools prefix when
/// supplied (`None` keeps the legacy standalone shape). Rebuilds the history
/// as a summary system message plus the retained recent user messages.
/// Applies the manual options to the summary request.
///
/// When `config.hierarchical` is `true`, delegates to
/// [`summarize_locally_hierarchical`] which produces a multi-tier pyramid
/// (abstract + detail + verbatim) instead of a flat summary.
async fn summarize_locally(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    options: &ManualCompactionOptions,
    context_budget: Option<usize>,
    parent: Option<&CompactionParentContext>,
) -> Result<Vec<Message>> {
    if config.hierarchical {
        return summarize_locally_hierarchical(provider, model, history, config, options, context_budget, parent).await;
    }

    let effective_config = context_bounded_compaction_config(
        provider,
        model,
        history,
        &config.clone().with_manual_overrides(options),
        context_budget,
    );
    let history_budget = compaction_history_budget(provider, model, context_budget);
    let tail_target = route_tail_target_tokens(provider, model, context_budget);
    // Bound the fork so the summary request itself fits the summarizer window.
    // `/compact` is normally run when the context is already near full, so an
    // unbounded fork is rejected by the provider and the whole command fails.
    // Oversized tool outputs are pruned first so large dumps do not evict
    // whole protocol groups from the fork.
    let pruned_history = prune_oversized_tool_outputs(history);
    let summary_source =
        bound_history_for_summarization(&pruned_history, &effective_config.summary_prompt, history_budget);
    let summary = generate_local_summary_with_retry(
        provider,
        model,
        &pruned_history,
        &summary_source,
        &effective_config.summary_prompt,
        history_budget,
        options,
        parent,
    )
    .await?;

    Ok(bound_compacted_history_to_context(
        build_summary_compacted_history(history, summary, &effective_config, true, tail_target),
        provider,
        model,
        context_budget,
    ))
}

/// Generate a summary request, retrying with progressively halved input budgets
/// when the provider rejects the request as over context capacity.
///
/// Token estimates are heuristic, so a bounded input can still overflow the
/// summarizer window. Bounded retries recover instead of failing the whole
/// `/compact` command; any other error (or exhausted retries) propagates.
/// `make_request` rebuilds the request shape (cache-safe fork or single-shot
/// band prompt) from the given source messages so every local summarization
/// path shares one retry contract.
async fn generate_summary_with_capacity_retry(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    instructions: &str,
    first_source: &[Message],
    history_budget: Option<usize>,
    max_overflow_retries: u32,
    error_context: &'static str,
    make_request: impl Fn(&[Message]) -> LLMRequest,
) -> Result<String> {
    let first_tokens = first_source.iter().map(Message::estimate_tokens).sum::<usize>();
    let mut current_source: &[Message] = first_source;
    let mut current_tokens = first_tokens;
    let mut current_budget = history_budget;
    let mut remaining_retries = max_overflow_retries;
    let mut retry_storage: Vec<Message>;
    loop {
        match collect_single_response(provider, make_request(current_source)).await {
            Ok(response) => {
                let summary = response.content.unwrap_or_default().trim().to_string();
                if summary.is_empty() {
                    return Err(anyhow::anyhow!(
                        "{error_context} for {} / {}: provider returned an empty summary (input_tokens={current_tokens}, budget={current_budget:?})",
                        provider.name(),
                        model,
                    ));
                }
                return Ok(summary);
            }
            Err(error) if is_context_capacity_message(&error.to_string()) && remaining_retries > 0 => {
                // With an unknown budget the bound is verbatim, so halving
                // `None` would resend the identical fork. Derive a fallback
                // from the failing attempt so the retry can still shrink.
                current_budget = match current_budget {
                    Some(budget) => Some((budget / 2).max(4)),
                    None => Some((current_tokens / 2).max(4)),
                };
                retry_storage = bound_history_for_summarization(history, instructions, current_budget);
                // A retry only helps when it actually shrinks the input: with
                // an already-fitting history it would resend the identical
                // request, so propagate the original failure instead.
                let retry_tokens = retry_storage.iter().map(Message::estimate_tokens).sum::<usize>();
                if retry_tokens >= current_tokens {
                    return Err(anyhow::Error::from(error).context(format!(
                        "{error_context} for {} / {} (input_tokens={current_tokens}, budget={current_budget:?})",
                        provider.name(),
                        model,
                    )));
                }
                tracing::warn!(
                    provider = provider.name(),
                    model,
                    input_tokens = current_tokens,
                    retry_tokens,
                    budget = ?current_budget,
                    error = ?error,
                    "{error_context}: input exceeded context capacity; retrying with a halved input budget"
                );
                current_source = &retry_storage;
                current_tokens = retry_tokens;
                remaining_retries -= 1;
            }
            Err(error) => {
                return Err(anyhow::Error::from(error).context(format!(
                    "{error_context} for {} / {} (input_tokens={current_tokens}, budget={current_budget:?})",
                    provider.name(),
                    model,
                )));
            }
        }
    }
}

/// Generate a cache-safe fork summary with the shared capacity-retry contract.
async fn generate_local_summary_with_retry(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    summary_source: &[Message],
    instructions: &str,
    history_budget: Option<usize>,
    options: &ManualCompactionOptions,
    parent: Option<&CompactionParentContext>,
) -> Result<String> {
    generate_summary_with_capacity_retry(
        provider,
        model,
        history,
        instructions,
        summary_source,
        history_budget,
        CompactionRoutePolicy::resolve(provider.name(), model).max_overflow_retries,
        "Failed to generate compaction summary",
        |source| {
            compaction_summary_request(
                model,
                source,
                instructions,
                options.max_output_tokens,
                options.reasoning_effort,
                options.verbosity,
                parent,
            )
        },
    )
    .await
}

/// Intro framing for the hierarchical abstract band (oldest third).
const ABSTRACT_BAND_INTRO: &str =
    "In 1-2 sentences, what was the overall goal and major progress in this portion of the conversation?\n\n";

/// Build a single-shot band summary request: one user message carrying the
/// pre-rendered band prompt, with the parent system/tools prefix reused.
fn band_summary_request(
    model: &str,
    prompt: String,
    max_tokens: Option<u32>,
    options: &ManualCompactionOptions,
    parent: Option<&CompactionParentContext>,
) -> LLMRequest {
    LLMRequest {
        messages: Arc::new(vec![Message::user(prompt)]),
        model: model.to_string(),
        system_prompt: parent.and_then(|parent| parent.system_prompt.clone()),
        tools: parent.and_then(|parent| parent.tools.clone()).filter(|tools| !tools.is_empty()),
        tool_choice: Some(ToolChoice::none()),
        max_tokens,
        reasoning_effort: options.reasoning_effort,
        verbosity: options.verbosity,
        ..Default::default()
    }
}
/// Hierarchical local summarization: abstract + detail + verbatim pyramid.
///
/// Splits the history into three bands and summarizes each with a different
/// compression target:
/// - **Abstract** (oldest third): 1-2 sentence overview
/// - **Detail** (middle third): paragraph-level summary
/// - **Verbatim** (newest third): kept as-is plus importance-weighted retained messages
///
/// This follows the hierarchical summarization strategy from the context window
/// management literature: recent turns verbatim, older turns as paragraph
/// summaries, oldest turns as a single abstract.
///
/// Band requests carry only their band's messages (not the full history
/// prefix), so message-prefix reuse does not apply; the parent system/tools
/// prefix is still reused, and `tool_choice` stays `none` so neither pass can
/// spend the compaction budget on tool calls.
async fn summarize_locally_hierarchical(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    options: &ManualCompactionOptions,
    context_budget: Option<usize>,
    parent: Option<&CompactionParentContext>,
) -> Result<Vec<Message>> {
    let effective_config = context_bounded_compaction_config(
        provider,
        model,
        history,
        &config.clone().with_manual_overrides(options),
        context_budget,
    );
    let (summary_history, _) =
        split_continuity_history_with_target(history, route_tail_target_tokens(provider, model, context_budget));

    // Split history into three bands at roughly equal thirds. Each band is
    // bounded to the summarizer window so a near-full context cannot overflow
    // the band request itself (same class of bug as the flat-path fork).
    // Oversized tool outputs are pruned first so large dumps do not evict
    // whole protocol groups from a band.
    let total = summary_history.len();
    let band_size = total / 3;
    let abstract_end = band_size;
    let detail_end = band_size * 2;
    let history_budget = compaction_history_budget(provider, model, context_budget);
    let max_overflow_retries = CompactionRoutePolicy::resolve(provider.name(), model).max_overflow_retries;

    // Band 1 (oldest): compress into 1-2 sentence abstract.
    let pruned_abstract = prune_oversized_tool_outputs(&summary_history[..abstract_end]);
    let abstract_band = bound_history_for_summarization(&pruned_abstract, "", history_budget);
    let abstract_summary = generate_summary_with_capacity_retry(
        provider,
        model,
        &pruned_abstract,
        "",
        &abstract_band,
        history_budget,
        max_overflow_retries,
        "Failed to generate abstract summary",
        |band| {
            band_summary_request(
                model,
                format!("{ABSTRACT_BAND_INTRO}{}", build_summary_prompt(band, "")),
                Some(150),
                options,
                parent,
            )
        },
    )
    .await?;

    // Band 2 (middle): paragraph-level summary using the full summary prompt.
    let pruned_detail = prune_oversized_tool_outputs(&summary_history[abstract_end..detail_end]);
    let detail_band = bound_history_for_summarization(&pruned_detail, &effective_config.summary_prompt, history_budget);
    let detail_summary = generate_summary_with_capacity_retry(
        provider,
        model,
        &pruned_detail,
        &effective_config.summary_prompt,
        &detail_band,
        history_budget,
        max_overflow_retries,
        "Failed to generate detail summary",
        |band| {
            band_summary_request(
                model,
                build_summary_prompt(band, &effective_config.summary_prompt),
                options.max_output_tokens,
                options,
                parent,
            )
        },
    )
    .await?;

    // Band 3 (newest): retain verbatim via the bounded protocol tail.
    let recent_band = &summary_history[detail_end..];
    let retained = collect_retained_user_messages(
        recent_band,
        effective_config.retained_user_message_tokens,
        effective_config.retained_user_messages,
    );

    // Assemble: [abstract, detail, ...retained_recent, ...continuity_tail]
    let mut new_history = Vec::with_capacity(2 + retained.len());
    new_history.push(Message::system(format!("{ABSTRACT_PREFIX}{abstract_summary}")));
    new_history.push(Message::system(format!("{DETAIL_PREFIX}{detail_summary}")));
    new_history.extend(retained);
    // Live compaction: retain the most recent turn verbatim for continuity.
    for message in continuity_tail_with_target(history, route_tail_target_tokens(provider, model, context_budget)) {
        new_history.push(message.clone());
    }
    Ok(bound_compacted_history_to_context(new_history, provider, model, context_budget))
}

pub(crate) fn build_summary_prompt(history: &[Message], instructions: &str) -> String {
    // Pre-size for the header plus every (non-empty) message body, avoiding
    // repeated reallocations while the summary prompt is assembled.
    let estimated_len =
        instructions.len() + history.iter().map(|m| m.content.as_text().len()).sum::<usize>() + history.len() * 16;
    let mut formatted = String::with_capacity(estimated_len);
    let now: DateTime<Utc> = Utc::now();
    let _ = writeln!(&mut formatted, "Summary requested at {}.\n{}", now.to_rfc3339(), instructions);

    for message in history {
        let role = match message.role {
            MessageRole::System => "system",
            MessageRole::User => "user",
            MessageRole::Assistant => "assistant",
            MessageRole::Tool => "tool",
        };
        let content = message.content.as_text();
        if content.trim().is_empty() {
            continue;
        }
        let _ = writeln!(&mut formatted, "\n[{}]\n{}", role, content.trim());
    }

    formatted
}

fn compaction_history_budget(provider: &dyn LLMProvider, model: &str, context_budget: Option<usize>) -> Option<usize> {
    let context_size = context_budget
        .filter(|value| *value > 0)
        .unwrap_or_else(|| provider.effective_context_size(model));
    (context_size > 0).then(|| {
        context_size
            .saturating_sub(context_size / COMPACTION_CONTEXT_OVERHEAD_FRACTION_DENOMINATOR)
            .saturating_sub(COMPACTION_CONTEXT_FIXED_OVERHEAD_TOKENS)
    })
}

fn context_bounded_compaction_config(
    provider: &dyn LLMProvider,
    model: &str,
    history: &[Message],
    config: &CompactionConfig,
    context_budget: Option<usize>,
) -> CompactionConfig {
    let mut bounded = config.clone();
    if let Some(history_budget) = compaction_history_budget(provider, model, context_budget) {
        let tail_target = route_tail_target_tokens(provider, model, context_budget);
        let continuity_tokens = continuity_tail_with_target(history, tail_target)
            .iter()
            .map(Message::estimate_tokens)
            .sum::<usize>();
        bounded.retained_user_message_tokens = bounded
            .retained_user_message_tokens
            .min(history_budget.saturating_sub(continuity_tokens));
    }
    bounded
}

/// Bound a locally managed compacted history to the resolved context budget,
/// including any caller-supplied session ceiling. Standalone native responses
/// must bypass this helper: their retained items and opaque continuation state
/// are the provider's canonical next context window.
pub fn bound_compacted_history_to_context(
    compacted: Vec<Message>,
    provider: &dyn LLMProvider,
    model: &str,
    context_budget: Option<usize>,
) -> Vec<Message> {
    let Some(history_budget) = compaction_history_budget(provider, model, context_budget) else {
        return compacted;
    };
    if compacted.iter().map(Message::estimate_tokens).sum::<usize>() <= history_budget {
        return compacted;
    }

    let Some((tail_start, tail_end, _)) =
        continuity_tail_selection_with_target(&compacted, route_tail_target_tokens(provider, model, context_budget))
    else {
        return compacted
            .into_iter()
            .scan(history_budget, |remaining, message| {
                if *remaining < 4 {
                    return None;
                }
                let bounded = bounded_message_preview(&message, *remaining);
                let used = bounded.estimate_tokens();
                if used > *remaining {
                    return None;
                }
                *remaining -= used;
                Some(bounded)
            })
            .collect();
    };

    let raw_tail = &compacted[tail_start..tail_end];
    let raw_tail_tokens = raw_tail.iter().map(Message::estimate_tokens).sum::<usize>();
    let tail = if raw_tail_tokens > history_budget {
        bounded_protocol_group(raw_tail, history_budget.max(4))
    } else {
        raw_tail.to_vec()
    };
    let tail_tokens = tail.iter().map(Message::estimate_tokens).sum::<usize>();
    let mut remaining = history_budget.saturating_sub(tail_tokens);
    let mut bounded_prefix = Vec::new();

    for (index, message) in compacted[..tail_start].iter().enumerate() {
        if remaining < 4 {
            break;
        }
        // Keep the leading summary/envelope message readable, but cap it so
        // the newest complete protocol groups retain priority.
        let message_budget = if index == 0 { remaining.min(4_096) } else { remaining };
        let bounded = bounded_message_preview(message, message_budget);
        let used = bounded.estimate_tokens();
        if used > remaining {
            if index == 0 {
                let fallback = Message::system(truncate_to_token_limit(
                    message.content.as_text().as_ref(),
                    remaining.saturating_sub(4),
                ));
                let fallback_tokens = fallback.estimate_tokens();
                if fallback_tokens <= remaining {
                    bounded_prefix.push(fallback);
                }
            }
            break;
        }
        remaining -= used;
        bounded_prefix.push(bounded);
    }

    bounded_prefix.extend(tail);
    bounded_prefix
}

pub(crate) fn build_local_compacted_history(
    history: &[Message],
    summary: &str,
    retained_user_message_tokens: usize,
    retained_user_messages: usize,
    include_continuity_tail: bool,
    tail_target_tokens: usize,
) -> Vec<Message> {
    let (retention_history, continuity) = if include_continuity_tail {
        split_continuity_history_with_target(history, tail_target_tokens)
    } else {
        (history, Vec::new())
    };
    let retained_users =
        collect_retained_user_messages(retention_history, retained_user_message_tokens, retained_user_messages);
    let mut new_history = Vec::with_capacity(retained_users.len().saturating_add(1));
    new_history.push(Message::system(format!("{SUMMARY_PREFIX}{}", summary.trim())));
    new_history.extend(retained_users);

    // Continuity anchor: retain the newest complete protocol groups verbatim
    // within the route tail budget. Duplicate message text is still valid
    // across turns, so preserve the sequence by index rather than deduplicating
    // on role/content.
    if include_continuity_tail {
        for message in continuity {
            new_history.push(message);
        }
    }
    new_history
}

/// Return the newest complete user-anchored protocol groups that fit the fixed
/// continuity budget. The returned messages are owned because an oversized
/// individual group may need a bounded preview.
#[cfg(test)]
fn continuity_tail(history: &[Message]) -> Vec<Message> {
    continuity_tail_with_target(history, CONTINUITY_TAIL_TARGET_TOKENS)
}

/// [`continuity_tail`] with a route-scaled budget so small windows keep a
/// usable summary instead of a tail-only history.
fn continuity_tail_with_target(history: &[Message], tail_target_tokens: usize) -> Vec<Message> {
    let Some((start, end, oversized)) = continuity_tail_selection_with_target(history, tail_target_tokens) else {
        return Vec::new();
    };
    if oversized {
        bounded_protocol_group(&history[start..end], tail_target_tokens.max(4))
    } else {
        history[start..end].to_vec()
    }
}

/// Find one contiguous suffix of complete protocol groups. An incomplete
/// trailing assistant tool-call group is truncated at the assistant message,
/// preserving its user anchor while excluding the invalid protocol suffix.
fn continuity_tail_selection_with_target(
    history: &[Message],
    tail_target_tokens: usize,
) -> Option<(usize, usize, bool)> {
    if history.is_empty() {
        return None;
    }
    let group_starts: Vec<usize> = history
        .iter()
        .enumerate()
        .filter_map(|(index, message)| (message.role == MessageRole::User).then_some(index))
        .collect();
    let last_group_index = group_starts.len().saturating_sub(1);
    let last_group_start = *group_starts.last()?;
    let last_group_end = history.len();
    let last_group = &history[last_group_start..last_group_end];
    let last_group_prefix_end = complete_protocol_group_prefix(last_group);
    let tail_end = last_group_start.saturating_add(last_group_prefix_end);

    let mut selected_start = None;
    let mut estimated_tokens = 0usize;
    for group_index in (0..group_starts.len()).rev() {
        let start = group_starts[group_index];
        let natural_end = group_starts.get(group_index + 1).copied().unwrap_or(history.len());
        let end = if group_index == last_group_index {
            tail_end
        } else {
            natural_end
        };
        if start == end {
            continue;
        }
        let group = &history[start..end];
        if group_index != last_group_index && !protocol_group_is_complete(group) {
            break;
        }
        let group_tokens = group.iter().map(Message::estimate_tokens).sum::<usize>();
        if selected_start.is_none() && group_tokens > tail_target_tokens {
            return Some((start, end, true));
        }
        if estimated_tokens.saturating_add(group_tokens) > tail_target_tokens {
            break;
        }
        selected_start = Some(start);
        estimated_tokens += group_tokens;
    }

    selected_start.map(|start| (start, tail_end, false))
}

fn protocol_group_is_complete(group: &[Message]) -> bool {
    complete_protocol_group_prefix(group) == group.len()
}

/// Return the length of the valid protocol prefix in a user-anchored group.
/// When a tool call is still pending, the prefix ends before the assistant
/// message that introduced it. This handles parallel tool calls and prevents
/// a partially answered group from entering the continuity tail.
fn complete_protocol_group_prefix(group: &[Message]) -> usize {
    if group.first().is_none_or(|message| message.role != MessageRole::User) {
        return 0;
    }

    let mut pending_tool_call_ids: Vec<&str> = Vec::new();
    let mut pending_origin = None;

    for (index, message) in group.iter().enumerate() {
        if !pending_tool_call_ids.is_empty() {
            if message.role != MessageRole::Tool {
                return pending_origin.unwrap_or(index);
            }
            let Some(tool_call_id) = message.tool_call_id.as_deref() else {
                return pending_origin.unwrap_or(index);
            };
            let Some(pending_index) = pending_tool_call_ids.iter().position(|pending_id| *pending_id == tool_call_id)
            else {
                return pending_origin.unwrap_or(index);
            };
            pending_tool_call_ids.swap_remove(pending_index);
            if pending_tool_call_ids.is_empty() {
                pending_origin = None;
            }
            continue;
        }

        // Older persisted histories may contain a tool result without the
        // assistant call metadata that introduced it. Preserve that legacy
        // pair rather than discarding an otherwise complete protocol group.
        if message.role == MessageRole::Tool {
            continue;
        }

        if message.role == MessageRole::Assistant
            && let Some(tool_calls) = message.tool_calls.as_ref()
            && !tool_calls.is_empty()
        {
            for (call_index, call) in tool_calls.iter().enumerate() {
                if call.id.is_empty() || tool_calls[..call_index].iter().any(|prior| prior.id == call.id) {
                    return index;
                }
            }
            pending_origin = Some(index);
            pending_tool_call_ids.extend(tool_calls.iter().map(|call| call.id.as_str()));
        }
    }

    pending_origin.unwrap_or(group.len())
}

fn split_continuity_history_with_target(history: &[Message], tail_target_tokens: usize) -> (&[Message], Vec<Message>) {
    let Some((tail_start, _, _)) = continuity_tail_selection_with_target(history, tail_target_tokens) else {
        return (history, Vec::new());
    };
    (&history[..tail_start], continuity_tail_with_target(history, tail_target_tokens))
}

fn response_compaction_detail(response: &LLMResponse) -> Option<Value> {
    response.reasoning_details.as_ref()?.iter().find_map(|detail| {
        let parsed = serde_json::from_str::<Value>(detail).ok()?;
        let parsed = match parsed {
            Value::String(serialized) => serde_json::from_str::<Value>(&serialized).ok()?,
            value => value,
        };
        let summary = parsed
            .get("content")
            .and_then(Value::as_str)
            .map(str::trim)
            .filter(|value| !value.is_empty());
        (parsed.get("type").and_then(Value::as_str) == Some("compaction") && summary.is_some()).then_some(parsed)
    })
}

fn build_summary_compacted_history(
    history: &[Message],
    summary: impl AsRef<str>,
    config: &CompactionConfig,
    include_continuity_tail: bool,
    tail_target_tokens: usize,
) -> Vec<Message> {
    build_compacted_history_with_leading(
        history,
        Message::system(format!("{SUMMARY_PREFIX}{}", summary.as_ref().trim())),
        config,
        include_continuity_tail,
        tail_target_tokens,
        false,
    )
}

fn build_provider_compacted_history(
    history: &[Message],
    compaction_detail: Value,
    config: &CompactionConfig,
    include_continuity_tail: bool,
    tail_target_tokens: usize,
) -> Vec<Message> {
    let signed_compaction = compaction_detail
        .get("signature")
        .and_then(Value::as_str)
        .is_some_and(|signature| !signature.trim().is_empty());
    let history_without_provider_compaction = history
        .iter()
        .cloned()
        .filter_map(strip_provider_compaction_detail)
        .collect::<Vec<_>>();
    build_compacted_history_with_leading(
        &history_without_provider_compaction,
        Message::assistant(String::new()).with_reasoning_details(Some(vec![compaction_detail])),
        config,
        include_continuity_tail,
        tail_target_tokens,
        signed_compaction,
    )
}

fn build_compacted_history_with_leading(
    history: &[Message],
    leading: Message,
    config: &CompactionConfig,
    include_continuity_tail: bool,
    tail_target_tokens: usize,
    strip_pre_compaction_thinking: bool,
) -> Vec<Message> {
    let (retention_history, continuity) = split_continuity_history_with_target(history, tail_target_tokens);
    let retained_users = collect_retained_user_messages(
        retention_history,
        config.retained_user_message_tokens,
        config.retained_user_messages,
    );
    let retained_users = if strip_pre_compaction_thinking {
        retained_users.into_iter().map(strip_anthropic_thinking_details).collect()
    } else {
        retained_users
    };
    let mut compacted = Vec::with_capacity(retained_users.len().saturating_add(1));
    compacted.push(leading);
    compacted.extend(retained_users);
    if include_continuity_tail {
        for message in continuity {
            compacted.push(if strip_pre_compaction_thinking {
                strip_anthropic_thinking_details(message)
            } else {
                message
            });
        }
    }
    compacted
}

fn strip_anthropic_thinking_details(mut message: Message) -> Message {
    if message.role != MessageRole::Assistant {
        return message;
    }

    let Some(details) = message.reasoning_details.take() else {
        return message;
    };
    let retained = details
        .into_iter()
        .filter(|detail| !is_reasoning_detail_type(detail, "thinking"))
        .filter(|detail| !is_reasoning_detail_type(detail, "redacted_thinking"))
        .collect::<Vec<_>>();
    message.reasoning_details = (!retained.is_empty()).then_some(retained);
    message
}

fn strip_provider_compaction_detail(mut message: Message) -> Option<Message> {
    if message.role != MessageRole::Assistant {
        return Some(message);
    }

    let Some(details) = message.reasoning_details.take() else {
        return Some(message);
    };
    let retained = details
        .into_iter()
        .filter(|detail| !is_compaction_detail(detail))
        .collect::<Vec<_>>();
    message.reasoning_details = (!retained.is_empty()).then_some(retained);

    let has_content = !message.content.trim().is_empty();
    let has_reasoning = message.reasoning.as_ref().is_some_and(|reasoning| !reasoning.trim().is_empty());
    let has_tool_calls = message.tool_calls.as_ref().is_some_and(|tool_calls| !tool_calls.is_empty());
    let has_other_metadata = message.tool_call_id.is_some()
        || message.phase.is_some()
        || message.origin_tool.is_some()
        || message.metadata.is_some()
        || message.clear_at.is_some();
    (has_content || has_reasoning || message.reasoning_details.is_some() || has_tool_calls || has_other_metadata)
        .then_some(message)
}

fn is_compaction_detail(detail: &Value) -> bool {
    is_reasoning_detail_type(detail, "compaction")
}

fn is_reasoning_detail_type(detail: &Value, expected_type: &str) -> bool {
    let value = match detail {
        Value::String(serialized) => serde_json::from_str::<Value>(serialized).ok(),
        value => Some(value.clone()),
    };
    value.as_ref().and_then(|value| value.get("type")).and_then(Value::as_str) == Some(expected_type)
}

fn bounded_protocol_group(group: &[Message], token_budget: usize) -> Vec<Message> {
    let per_message_budget = (token_budget / group.len().max(1)).max(4);
    group
        .iter()
        .map(|message| bounded_message_preview(message, per_message_budget))
        .collect()
}

/// Per-message cap applied to tool outputs in summarizer inputs. Large dumps
/// (file reads, command output) otherwise evict whole protocol groups from the
/// bounded fork. Call IDs and pairing metadata are preserved by
/// [`bounded_message_preview`], so trimmed groups stay protocol-valid.
const TOOL_RESULT_PRUNE_TARGET_TOKENS: usize = 4_096;

/// Trim oversized tool outputs to previews before bounding the summarizer
/// input. Only `Tool` messages are touched; everything else passes through
/// verbatim (cloned). Under the cap this is a pure copy.
fn prune_oversized_tool_outputs(history: &[Message]) -> Vec<Message> {
    history
        .iter()
        .map(|message| {
            if message.role == MessageRole::Tool {
                bounded_message_preview(message, TOOL_RESULT_PRUNE_TARGET_TOKENS)
            } else {
                message.clone()
            }
        })
        .collect()
}

fn bounded_message_preview(message: &Message, token_budget: usize) -> Message {
    if message.estimate_tokens() <= token_budget {
        return message.clone();
    }
    let mut preview = message.clone();
    let available = token_budget.saturating_sub(4);
    let text = truncate_to_token_limit(message.content.as_text().as_ref(), available);
    preview.content = MessageContent::Text(text);

    // Tool-call arguments and provider metadata are part of the message's
    // token footprint even when `content` is empty. Preserve call IDs and
    // function names so the protocol group remains correlatable, but replace
    // oversized argument payloads with valid minimal JSON and drop optional
    // reasoning/signature metadata.
    preview.reasoning = preview
        .reasoning
        .map(|reasoning| truncate_to_token_limit(&reasoning, available.min(1_024)));
    preview.reasoning_details = None;
    preview.metadata = None;
    preview.origin_tool = None;
    if let Some(tool_calls) = preview.tool_calls.as_mut() {
        for call in tool_calls {
            if let Some(function) = call.function.as_mut()
                && function.arguments.len() > available.saturating_mul(4)
            {
                function.arguments = "{}".to_string();
            }
            if let Some(text) = call.text.as_mut() {
                *text = truncate_to_token_limit(text, available.min(1_024));
            }
            call.thought_signature = None;
        }
    }

    if preview.estimate_tokens() > token_budget {
        preview.content = MessageContent::Text(String::new());
        preview.reasoning = None;
        preview.reasoning_details = None;
        if let Some(tool_calls) = preview.tool_calls.as_mut() {
            for call in tool_calls {
                if let Some(function) = call.function.as_mut() {
                    function.arguments = "{}".to_string();
                }
                call.text = None;
                call.thought_signature = None;
            }
        }
    }
    preview
}

fn collect_retained_user_messages(history: &[Message], token_budget: usize, max_messages: usize) -> Vec<Message> {
    if token_budget == 0 || max_messages == 0 {
        return Vec::new();
    }

    // Phase 1: select up to `max_messages` user messages, scored by importance.
    let total = history.len();
    let mut user_scored: Vec<(usize, f64, &Message)> = history
        .iter()
        .enumerate()
        .filter(|(_, m)| m.role == MessageRole::User && !m.content.trim().is_empty())
        .map(|(i, m)| {
            let score = score_message(m, i, total);
            (i, score, m)
        })
        .collect();
    user_scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

    let mut selected: Vec<(usize, Message)> = Vec::with_capacity(max_messages.min(history.len()));
    let mut remaining = token_budget;

    for (original_idx, _score, message) in &user_scored {
        if selected.len() >= max_messages {
            break;
        }
        let estimated = message.estimate_tokens();
        if estimated <= remaining {
            selected.push((*original_idx, (*message).clone()));
            remaining = remaining.saturating_sub(estimated);
            continue;
        }
        if let Some(truncated) = truncate_user_message(message, remaining) {
            selected.push((*original_idx, truncated));
        }
        break;
    }

    // Phase 2: if budget remains, add high-value non-user messages (tool
    // results, assistant tool calls) that fit within the remaining capacity.
    if selected.len() < max_messages && remaining > 0 {
        let mut non_user_scored: Vec<(usize, f64, &Message)> = history
            .iter()
            .enumerate()
            .filter(|(_, m)| m.role != MessageRole::User && m.role != MessageRole::System && is_retainable_message(m))
            .map(|(i, m)| {
                let score = score_message(m, i, total);
                (i, score, m)
            })
            .collect();
        non_user_scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        for (original_idx, _score, message) in &non_user_scored {
            if selected.len() >= max_messages {
                break;
            }
            let estimated = message.estimate_tokens();
            if estimated <= remaining {
                selected.push((*original_idx, (*message).clone()));
                remaining = remaining.saturating_sub(estimated);
            }
        }
    }

    // Re-sort by original conversation order, then enforce tool-call/turn
    // coherence so the compacted history is valid to send back to a provider.
    selected.sort_by_key(|(idx, _)| *idx);
    coherence_tool_call_pairs(history, &selected)
        .into_iter()
        .map(|(_, msg)| msg)
        .collect()
}

/// Keep retained tool-call turns internally consistent.
///
/// A `Tool` message references a tool call the model must have seen, and an
/// `Assistant` message that still carries `tool_calls` must be followed by the
/// results those calls produced. Sending either without its counterpart is
/// invalid: providers reject unmatched tool calls, and orphaned tool results
/// reference a call the model never observed. This pass:
///
/// - **Force-keeps** the `Tool` messages immediately following any retained
///   `Assistant` that carries `tool_calls`, so the model observes each call's
///   return value. A complete turn ends with its results, which survive even
///   if they push past the soft `max_messages` cap.
/// - **Drops** a retained `Tool` message whose calling `Assistant` (the message
///   directly before it in `history`) was *not* retained — an orphaned result
///   the model cannot reconcile.
///
/// Tool results that follow a plain `Assistant` (no `tool_calls`) are ordinary
/// turn output and are kept exactly as selected.
fn coherence_tool_call_pairs(history: &[Message], selected: &[(usize, Message)]) -> Vec<(usize, Message)> {
    let mut keep: std::collections::HashSet<usize> = selected.iter().map(|(i, _)| *i).collect();

    for (idx, msg) in selected {
        if msg.role == MessageRole::Assistant && msg.tool_calls.as_ref().is_some_and(|calls| !calls.is_empty()) {
            let mut j = *idx + 1;
            while let Some(next) = history.get(j) {
                if next.role == MessageRole::Tool {
                    keep.insert(j);
                    j += 1;
                } else {
                    break;
                }
            }
        }
    }

    // Emit every index in `keep` in original history order, dropping orphaned
    // Tool results whose calling Assistant turn was not retained.
    let mut indices: Vec<usize> = keep.iter().copied().collect();
    indices.sort_unstable();

    indices
        .into_iter()
        .filter(|idx| {
            let msg = &history[*idx];
            if msg.role != MessageRole::Tool {
                return true;
            }
            // Walk backward through this contiguous result run to decide
            // coherence against the calling assistant turn.
            let mut cursor = *idx;
            loop {
                match history.get(cursor) {
                    Some(m) if m.role == MessageRole::Tool => {
                        cursor = match cursor.checked_sub(1) {
                            Some(c) => c,
                            None => break,
                        };
                    }
                    Some(m)
                        if m.role == MessageRole::Assistant && m.tool_calls.as_ref().is_some_and(|c| !c.is_empty()) =>
                    {
                        // Reached the calling assistant: coherent only if it
                        // was retained.
                        return keep.contains(&cursor);
                    }
                    _ => {
                        // Plain assistant or boundary: ordinary output.
                        return true;
                    }
                }
            }
            true
        })
        .map(|idx| (idx, history[idx].clone()))
        .collect()
}

/// Score a message for importance-weighted retention during compaction.
///
/// Uses a weighted combination of content importance and recency:
/// - Messages containing errors, corrections, or tool results score higher
/// - Recent messages get a recency bonus
/// - Assistant messages with tool calls are moderately important
fn score_message(message: &Message, index: usize, total: usize) -> f64 {
    let content = message.content.as_text();
    let content_lower = content.to_lowercase();

    // Importance weight based on content signals.
    let importance = match message.role {
        MessageRole::User => {
            if contains_error_signal(&content_lower) {
                3.0
            } else if contains_correction_signal(&content_lower) {
                2.5
            } else {
                1.0
            }
        }
        MessageRole::Tool => {
            // Tool results contain factual data the model may need.
            2.0
        }
        MessageRole::Assistant => {
            if message.tool_calls.is_some() {
                // Assistant messages with tool calls show action taken.
                0.5
            } else {
                0.1
            }
        }
        MessageRole::System => 0.0,
    };

    // Recency bonus: linear from 0.0 (oldest) to 1.0 (newest).
    let recency = if total > 0 { index as f64 / total as f64 } else { 0.0 };

    importance + recency
}

/// Check if content contains error or failure signals.
fn contains_error_signal(content: &str) -> bool {
    content.contains("error")
        || content.contains("failed")
        || content.contains("failure")
        || content.contains("panic")
        || content.contains("bug")
        || content.contains("broken")
        || content.contains("regression")
}

/// Check if content contains user correction signals.
fn contains_correction_signal(content: &str) -> bool {
    content.contains("no,")
        || content.contains("wrong")
        || content.contains("actually")
        || content.contains("fix")
        || content.contains("instead")
        || content.contains("should be")
        || content.contains("don't")
}

/// Whether a message is worth retaining during compaction.
fn is_retainable_message(message: &Message) -> bool {
    match message.role {
        MessageRole::User => !message.content.trim().is_empty(),
        MessageRole::Tool => !message.content.trim().is_empty(),
        MessageRole::Assistant => {
            // Retain assistant messages that contain tool calls (action history).
            message.tool_calls.is_some()
        }
        MessageRole::System => false,
    }
}

fn truncate_user_message(message: &Message, token_budget: usize) -> Option<Message> {
    if token_budget <= 4 {
        return None;
    }

    let available_content_tokens = token_budget.saturating_sub(4);
    let truncated = truncate_to_token_limit(message.content.as_text().as_ref(), available_content_tokens);
    let trimmed = truncated.trim();
    if trimmed.is_empty() {
        return None;
    }

    Some(Message::user(trimmed.to_string()))
}

#[cfg(test)]
mod tests {
    use super::{
        CompactionConfig, ManualCompactionOptions, compact_history, compact_history_manual,
        compact_history_manual_with_budget, continuity_tail, manual_compaction_strategy,
    };
    use crate::config::types::{ReasoningEffortLevel, VerbosityLevel};
    use crate::exec::events::CompactionMode;
    use crate::llm::provider::{
        LLMError, LLMNormalizedStream, LLMProvider, LLMRequest, LLMResponse, Message, MessageRole,
        NormalizedStreamEvent, ResponsesCompactionOptions,
    };
    use async_trait::async_trait;
    use futures::stream;
    use serde_json::json;
    use std::sync::Arc;
    use std::sync::Mutex;
    use vtcode_commons::llm::{FinishReason, ToolCall};

    struct StubProvider;

    struct NativeCompactionProvider;

    /// Provider that opts into the standalone manual-compaction path
    /// (`supports_manual_openai_compaction -> true`), e.g. OpenAI `/responses/compact`.
    struct ManualStandaloneProvider {
        last_options: Mutex<Option<ResponsesCompactionOptions>>,
        output: Vec<Message>,
    }

    /// Inline-compaction-capable provider (`supports_responses_compaction -> true`,
    /// `supports_manual_openai_compaction -> false`), e.g. Anthropic `compact_20260112`.
    /// Returns a `Pause` finish with a compaction block so the inline path succeeds.
    struct InlinePauseProvider {
        last_request: Mutex<Option<LLMRequest>>,
        include_compaction_detail: bool,
    }

    /// Inline provider that also reports `supports_context_edits`, e.g. Anthropic
    /// on a compaction-capable model. The inline request must carry the full
    /// clearing ladder (thinking, tool uses, compact) in documented order.
    struct ContextEditsInlineProvider {
        last_request: Mutex<Option<LLMRequest>>,
    }

    /// Local summarizer that rejects the first summary request with a
    /// context-capacity error and succeeds on retry. Exercises the halved-budget
    /// overflow retry in `summarize_locally`.
    struct CapacityFailOnceProvider {
        attempts: Mutex<usize>,
        request_tokens: Mutex<Vec<usize>>,
        /// Number of leading requests to reject before succeeding.
        failures: usize,
    }

    /// Local summarizer that only supports normalized streaming. Its
    /// non-streaming method deliberately returns an error so compaction tests
    /// prove the capability-aware collection path is used.
    struct StreamingOnlyCompactionProvider {
        generate_calls: Mutex<usize>,
        stream_calls: Mutex<usize>,
        stream_modes: Mutex<Vec<bool>>,
    }

    /// Unknown-window summarizer (`effective_context_size == 0`) that rejects
    /// the first summary request with a capacity error. Exercises the
    /// `None`-budget fallback so an unbounded first fork can still shrink.
    struct UnknownBudgetFailOnceProvider {
        attempts: Mutex<usize>,
        request_tokens: Mutex<Vec<usize>>,
    }

    /// Local summarizer that returns an empty summary body. An empty
    /// compaction summary must fail with a diagnostic instead of producing
    /// an empty `Previous conversation summary:` history.
    struct EmptySummaryProvider;

    /// Capturing provider with no native support; used to assert the Local summary
    /// request carries the manual options.
    struct CapturingProvider {
        last_request: Mutex<Option<LLMRequest>>,
    }

    /// Local summarizer that exposes only the lower reasoning levels. This
    /// exercises the compaction boundary's strict block and explicit
    /// downgrade behavior before any hierarchical summary request is sent.
    struct LimitedReasoningProvider {
        last_request: Mutex<Option<LLMRequest>>,
    }

    /// Inline-dispatched provider whose inline `generate` rejects the Anthropic
    /// `compact_20260112` edit. Models providers that report
    /// `supports_responses_compaction` but are not Anthropic-style inline
    /// compactors; the dispatch must fall back to Local rather than aborting.
    struct InlineRejectingProvider;

    /// Models an OpenAI-compatible custom endpoint (non-`api.openai.com` host or
    /// `provider_key_override`): it exposes the Responses API
    /// (`supports_responses_compaction == true`) but neither the standalone
    /// `/responses/compact` endpoint nor Anthropic inline compaction. The dispatch
    /// must pick `Local` rather than misrouting it to `NativeInline` (which would
    /// send an Anthropic `compact_20260112` edit only to be rejected).
    struct CompatibleEndpointProvider;

    #[async_trait]
    impl LLMProvider for StubProvider {
        fn name(&self) -> &str {
            "stub"
        }

        async fn generate(&self, _request: LLMRequest) -> Result<LLMResponse, LLMError> {
            Ok(LLMResponse::new("stub-model", "summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn effective_context_size(&self, _model: &str) -> usize {
            32_768
        }
    }

    #[async_trait]
    impl LLMProvider for NativeCompactionProvider {
        fn name(&self) -> &str {
            "native"
        }

        async fn generate(&self, _request: LLMRequest) -> Result<LLMResponse, LLMError> {
            Ok(LLMResponse::new("stub-model", "summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn supports_responses_compaction(&self, _model: &str) -> bool {
            true
        }

        async fn compact_history(&self, _model: &str, _history: &[Message]) -> Result<Vec<Message>, LLMError> {
            Ok(vec![Message::system("provider compacted".to_string())])
        }
    }

    #[async_trait]
    impl LLMProvider for ManualStandaloneProvider {
        fn name(&self) -> &str {
            "manual-standalone"
        }

        async fn generate(&self, _request: LLMRequest) -> Result<LLMResponse, LLMError> {
            Ok(LLMResponse::new("stub-model", "summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn supports_manual_openai_compaction(&self, _model: &str) -> bool {
            true
        }

        fn supports_reasoning_effort(&self, _model: &str) -> bool {
            true
        }

        fn supported_reasoning_efforts(&self, _model: &str) -> &'static [&'static str] {
            &["minimal", "low", "medium", "high", "xhigh", "max"]
        }

        async fn compact_history_with_options(
            &self,
            _model: &str,
            _history: &[Message],
            options: &ResponsesCompactionOptions,
        ) -> Result<Vec<Message>, LLMError> {
            *self.last_options.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(options.clone());
            Ok(self.output.clone())
        }
    }

    #[async_trait]
    impl LLMProvider for InlinePauseProvider {
        fn name(&self) -> &str {
            "inline-pause"
        }

        async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError> {
            *self.last_request.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(request);
            let mut response = LLMResponse::new("stub-model", "compacted by provider");
            response.finish_reason = FinishReason::Pause;
            response.compaction = Some("provider compaction summary".to_string());
            if self.include_compaction_detail {
                response.reasoning_details = Some(vec![
                    json!({
                        "type": "compaction",
                        "content": "provider compaction summary",
                        "signature": "provider-signature",
                        "opaque_extension": "preserve-me",
                    })
                    .to_string(),
                ]);
            }
            Ok(response)
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn supports_responses_compaction(&self, _model: &str) -> bool {
            true
        }

        fn supports_native_inline_compaction(&self, _model: &str) -> bool {
            true
        }

        fn effective_context_size(&self, _model: &str) -> usize {
            200_000
        }
    }

    #[async_trait]
    impl LLMProvider for ContextEditsInlineProvider {
        fn name(&self) -> &str {
            "context-edits-inline"
        }

        async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError> {
            *self.last_request.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(request);
            let mut response = LLMResponse::new("stub-model", "compacted by provider");
            response.finish_reason = FinishReason::Pause;
            response.compaction = Some("provider compaction summary".to_string());
            response.reasoning_details = Some(vec![
                json!({
                    "type": "compaction",
                    "content": "provider compaction summary",
                    "signature": "provider-signature",
                })
                .to_string(),
            ]);
            Ok(response)
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn supports_responses_compaction(&self, _model: &str) -> bool {
            true
        }

        fn supports_context_edits(&self, _model: &str) -> bool {
            true
        }

        fn supports_native_inline_compaction(&self, _model: &str) -> bool {
            true
        }

        fn effective_context_size(&self, _model: &str) -> usize {
            200_000
        }
    }

    #[async_trait]
    impl LLMProvider for CapacityFailOnceProvider {
        fn name(&self) -> &str {
            "capacity-fail-once"
        }

        async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError> {
            let mut attempts = self.attempts.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
            self.request_tokens
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .push(request.messages.iter().map(Message::estimate_tokens).sum());
            *attempts += 1;
            if *attempts <= self.failures {
                return Err(LLMError::Provider {
                    message: "maximum context length exceeded".to_string(),
                    metadata: None,
                });
            }
            Ok(LLMResponse::new("stub-model", "retried summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn effective_context_size(&self, _model: &str) -> usize {
            200_000
        }
    }

    #[async_trait]
    impl LLMProvider for StreamingOnlyCompactionProvider {
        fn name(&self) -> &str {
            "streaming-only-compaction"
        }

        fn supports_streaming(&self) -> bool {
            true
        }

        fn supports_non_streaming(&self, _model: &str) -> bool {
            false
        }

        async fn generate(&self, _request: LLMRequest) -> Result<LLMResponse, LLMError> {
            *self.generate_calls.lock().unwrap_or_else(std::sync::PoisonError::into_inner) += 1;
            Err(LLMError::Provider {
                message: "streaming-only provider cannot generate non-streaming responses".to_string(),
                metadata: None,
            })
        }

        async fn stream_normalized(&self, request: LLMRequest) -> Result<LLMNormalizedStream, LLMError> {
            *self.stream_calls.lock().unwrap_or_else(std::sync::PoisonError::into_inner) += 1;
            self.stream_modes
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .push(request.stream);
            Ok(Box::pin(stream::iter(vec![Ok(NormalizedStreamEvent::Done {
                response: Box::new(LLMResponse::new("stub-model", "streamed summary")),
            })])))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }
    }

    #[async_trait]
    impl LLMProvider for UnknownBudgetFailOnceProvider {
        fn name(&self) -> &str {
            "unknown-budget-fail-once"
        }

        async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError> {
            let mut attempts = self.attempts.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
            self.request_tokens
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .push(request.messages.iter().map(Message::estimate_tokens).sum());
            *attempts += 1;
            if *attempts == 1 {
                return Err(LLMError::Provider {
                    message: "maximum context length exceeded".to_string(),
                    metadata: None,
                });
            }
            Ok(LLMResponse::new("stub-model", "retried summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn effective_context_size(&self, _model: &str) -> usize {
            0
        }
    }

    #[async_trait]
    impl LLMProvider for EmptySummaryProvider {
        fn name(&self) -> &str {
            "empty-summary"
        }

        async fn generate(&self, _request: LLMRequest) -> Result<LLMResponse, LLMError> {
            Ok(LLMResponse::new("stub-model", "   "))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn effective_context_size(&self, _model: &str) -> usize {
            200_000
        }
    }

    #[async_trait]
    impl LLMProvider for CapturingProvider {
        fn name(&self) -> &str {
            "capturing"
        }

        async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError> {
            *self.last_request.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(request);
            Ok(LLMResponse::new("stub-model", "summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn supports_reasoning_effort(&self, _model: &str) -> bool {
            true
        }

        fn supported_reasoning_efforts(&self, _model: &str) -> &'static [&'static str] {
            &["minimal", "low", "medium", "high", "xhigh", "max"]
        }
    }

    #[async_trait]
    impl LLMProvider for LimitedReasoningProvider {
        fn name(&self) -> &str {
            "limited-reasoning"
        }

        async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError> {
            *self.last_request.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = Some(request);
            Ok(LLMResponse::new("stub-model", "summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn supports_reasoning_effort(&self, _model: &str) -> bool {
            true
        }

        fn supported_reasoning_efforts(&self, _model: &str) -> &'static [&'static str] {
            &["low", "medium", "high"]
        }
    }

    #[async_trait]
    impl LLMProvider for InlineRejectingProvider {
        fn name(&self) -> &str {
            "inline-rejecting"
        }

        async fn generate(&self, request: LLMRequest) -> Result<LLMResponse, LLMError> {
            // Reject only the inline compaction request (carries the Anthropic
            // `compact_20260112` edit); the Local summary request must succeed.
            if request.context_management.is_some() {
                return Err(LLMError::Provider {
                    message: "provider rejected inline compact edit".to_string(),
                    metadata: None,
                });
            }
            Ok(LLMResponse::new("stub-model", "summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        fn supports_responses_compaction(&self, _model: &str) -> bool {
            true
        }

        fn supports_native_inline_compaction(&self, _model: &str) -> bool {
            true
        }
    }

    #[async_trait]
    impl LLMProvider for CompatibleEndpointProvider {
        fn name(&self) -> &str {
            "compatible-endpoint"
        }

        async fn generate(&self, _request: LLMRequest) -> Result<LLMResponse, LLMError> {
            Ok(LLMResponse::new("stub-model", "summary"))
        }

        fn supported_models(&self) -> Vec<String> {
            vec!["stub-model".to_string()]
        }

        fn validate_request(&self, _request: &LLMRequest) -> Result<(), LLMError> {
            Ok(())
        }

        // Reports Responses API support but neither standalone nor inline
        // compaction (defaults: supports_manual_openai_compaction and
        // supports_native_inline_compaction are both false).
        fn supports_responses_compaction(&self, _model: &str) -> bool {
            true
        }
    }

    fn sample_history() -> Vec<Message> {
        vec![
            Message::assistant("setup".to_string()),
            Message::user("first request".to_string()),
            Message::assistant("working".to_string()),
            Message::user("second request".to_string()),
        ]
    }

    fn canonical_standalone_output() -> Vec<Message> {
        vec![
            Message::user("retained by provider".to_string()),
            Message::assistant(String::new()).with_reasoning_details(Some(vec![json!({
                "type": "compaction",
                "id": "cmp_1",
                "encrypted_content": "opaque_state"
            })])),
        ]
    }

    #[test]
    fn signed_provider_compaction_strips_old_thinking_but_preserves_other_details() {
        let old_assistant = Message::assistant_with_tools(
            "old answer".to_string(),
            vec![ToolCall::function(
                "call-1".to_string(),
                "lookup".to_string(),
                "{}".to_string(),
            )],
        )
        .with_reasoning_details(Some(vec![
            json!({
                "type": "thinking",
                "thinking": "old trace",
                "signature": "old-signature"
            }),
            json!({"type": "provider_extension", "state": "keep"}),
        ]));
        let history = vec![
            Message::user("old request".to_string()),
            old_assistant,
            Message::tool_response("call-1".to_string(), "lookup result".to_string()),
            Message::user("latest request ".repeat(100_000)),
        ];

        let compacted = super::build_provider_compacted_history(
            &history,
            json!({
                "type": "compaction",
                "content": "summary",
                "signature": "new-signature"
            }),
            &CompactionConfig::default(),
            true,
            20_000,
        );

        let old = compacted
            .iter()
            .find(|message| message.content.as_text() == "old answer")
            .expect("retained action history should keep the old answer");
        let details = old.reasoning_details.as_ref().expect("opaque detail should survive");
        assert_eq!(details, &vec![json!({"type": "provider_extension", "state": "keep"})]);
    }

    #[test]
    fn provider_compaction_replaces_old_provider_marker_in_continuity_tail() {
        let old_marker = Message::assistant(String::new()).with_reasoning_details(Some(vec![json!({
            "type": "compaction",
            "content": "old summary",
            "signature": "old-signature",
        })]));
        let history = vec![
            Message::user("old request".to_string()),
            old_marker,
            Message::user("latest request".to_string()),
            Message::assistant("latest response".to_string()),
        ];

        let compacted = super::build_provider_compacted_history(
            &history,
            json!({
                "type": "compaction",
                "content": "new summary",
                "signature": "new-signature"
            }),
            &CompactionConfig::default(),
            true,
            20_000,
        );
        let markers = compacted
            .iter()
            .flat_map(|message| message.reasoning_details.as_deref().unwrap_or(&[]))
            .filter(|detail| detail.get("type").and_then(serde_json::Value::as_str) == Some("compaction"))
            .collect::<Vec<_>>();

        assert_eq!(markers.len(), 1);
        assert_eq!(markers[0]["content"], "new summary");
    }

    /// Build an assistant message that carries a (single) pending tool call.
    fn assistant_with_calls(content: &str, call_id: &str) -> Message {
        let mut message = Message::assistant(content.to_string());
        message.tool_calls = Some(vec![ToolCall {
            id: call_id.to_string(),
            call_type: "function".to_string(),
            function: None,
            text: None,
            thought_signature: None,
        }]);
        message
    }

    #[test]
    fn collect_retained_keeps_tool_result_with_its_assistant() {
        // When the assistant tool-call turn is retained, its tool result must
        // survive so the turn stays coherent (the model sees each call's return).
        let history = vec![
            Message::user("u1".to_string()),
            assistant_with_calls("calling tool", "c1"),
            Message::tool_response("c1".to_string(), "r1".to_string()),
            Message::user("u2".to_string()),
        ];
        let retained = super::collect_retained_user_messages(&history, 20_000, 4);
        assert!(retained.iter().any(|m| m.content.as_text().contains("u1")));
        assert!(retained.iter().any(|m| m.content.as_text().contains("u2")));
        assert!(
            retained.iter().any(|m| m.content.as_text().contains("r1")),
            "tool result paired with its retained assistant must survive"
        );
    }

    #[test]
    fn collect_retained_drops_orphaned_tool_result() {
        // If the assistant tool-call turn is dropped (over the retention cap),
        // its tool result is orphaned — the model never saw the call — and must
        // not survive compaction, because an orphaned result is invalid to send
        // to a provider.
        let history = vec![
            Message::user("u1".to_string()),
            assistant_with_calls("calling tool", "c1"),
            Message::tool_response("c1".to_string(), "r1".to_string()),
            Message::user("u2".to_string()),
        ];
        let retained = super::collect_retained_user_messages(&history, 20_000, 3);
        assert!(retained.iter().any(|m| m.content.as_text().contains("u1")));
        assert!(retained.iter().any(|m| m.content.as_text().contains("u2")));
        assert!(!retained.iter().any(|m| m.content.as_text().contains("r1")), "orphaned tool result must be dropped");
    }

    #[tokio::test]
    async fn manual_compaction_strategy_picks_local_for_plain_provider() {
        assert_eq!(manual_compaction_strategy(&StubProvider, "stub-model"), super::CompactionStrategy::Local);
    }

    #[tokio::test]
    async fn manual_compaction_strategy_picks_native_standalone_for_manual_provider() {
        let provider = ManualStandaloneProvider {
            last_options: Mutex::new(None),
            output: canonical_standalone_output(),
        };
        assert_eq!(manual_compaction_strategy(&provider, "stub-model"), super::CompactionStrategy::NativeStandalone);
    }

    #[tokio::test]
    async fn manual_compaction_strategy_picks_native_inline_for_responses_capable_provider() {
        let provider = InlinePauseProvider {
            last_request: Mutex::new(None),
            include_compaction_detail: true,
        };
        assert_eq!(manual_compaction_strategy(&provider, "stub-model"), super::CompactionStrategy::NativeInline);
    }

    #[tokio::test]
    async fn manual_compaction_strategy_picks_local_for_compatible_endpoint() {
        // OpenAI-compatible custom endpoints report `supports_responses_compaction`
        // but cannot serve standalone `/responses/compact` or Anthropic inline
        // compaction; they must route to Local, not NativeInline.
        assert_eq!(
            manual_compaction_strategy(&CompatibleEndpointProvider, "stub-model"),
            super::CompactionStrategy::Local
        );
    }

    #[tokio::test]
    async fn compact_history_manual_uses_local_summary_for_plain_provider() {
        let history = sample_history();
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };

        let (compacted, mode) =
            compact_history_manual(&StubProvider, "stub-model", &history, &config, &ManualCompactionOptions::default())
                .await
                .expect("manual compaction");

        assert_eq!(mode, CompactionMode::Local);
        assert_eq!(compacted.len(), 4);
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
        assert_eq!(compacted[1].content.as_text(), "first request");
        assert_eq!(compacted[2].content.as_text(), "working");
        assert_eq!(compacted[3].content.as_text(), "second request");
    }

    #[tokio::test]
    async fn local_compaction_collects_summary_from_streaming_only_provider() {
        let provider = StreamingOnlyCompactionProvider {
            generate_calls: Mutex::new(0),
            stream_calls: Mutex::new(0),
            stream_modes: Mutex::new(Vec::new()),
        };

        let (compacted, mode) = compact_history_manual(
            &provider,
            "stub-model",
            &sample_history(),
            &CompactionConfig::default(),
            &ManualCompactionOptions::default(),
        )
        .await
        .expect("streaming-only local compaction should succeed");

        assert_eq!(mode, CompactionMode::Local);
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nstreamed summary");
        assert_eq!(*provider.generate_calls.lock().unwrap(), 0, "non-streaming generation must not be attempted");
        assert_eq!(*provider.stream_calls.lock().unwrap(), 1, "summary should be collected from one normalized stream");
        assert_eq!(*provider.stream_modes.lock().unwrap(), vec![true], "stream fallback must set the request mode");
    }

    #[tokio::test]
    async fn compact_history_manual_preserves_native_standalone_window() {
        let history = sample_history();
        let config = CompactionConfig::default();
        let provider = ManualStandaloneProvider {
            last_options: Mutex::new(None),
            output: canonical_standalone_output(),
        };

        let (compacted, mode) =
            compact_history_manual(&provider, "stub-model", &history, &config, &ManualCompactionOptions::default())
                .await
                .expect("manual compaction");

        assert_eq!(mode, CompactionMode::Provider);
        assert_eq!(compacted, canonical_standalone_output());
    }

    #[tokio::test]
    async fn native_compaction_preserves_canonical_window_over_session_budget() {
        let history = sample_history();
        let canonical = vec![
            Message::user("provider-retained ".repeat(20_000)),
            Message::assistant(String::new()).with_reasoning_details(Some(vec![json!({
                "type": "compaction",
                "id": "cmp_canonical",
                "encrypted_content": "opaque_state",
            })])),
        ];
        let provider = ManualStandaloneProvider {
            last_options: Mutex::new(None),
            output: canonical.clone(),
        };
        let (compacted, mode) = compact_history_manual_with_budget(
            &provider,
            "stub-model",
            &history,
            &CompactionConfig::default(),
            &ManualCompactionOptions::default(),
            Some(8_192),
        )
        .await
        .expect("native compaction");

        assert_eq!(mode, CompactionMode::Provider);
        assert_eq!(compacted, canonical, "standalone output is the canonical replay window");
        assert!(
            compacted.iter().map(Message::estimate_tokens).sum::<usize>() > 8_192,
            "fixture must prove that the local session bound was not applied"
        );
    }

    #[tokio::test]
    async fn compact_history_manual_passes_options_to_native_standalone() {
        let history = sample_history();
        let config = CompactionConfig::default();
        let provider = ManualStandaloneProvider {
            last_options: Mutex::new(None),
            output: canonical_standalone_output(),
        };
        let options = ManualCompactionOptions {
            instructions: Some("keep only decisions".to_string()),
            max_output_tokens: Some(256),
            reasoning_effort: Some(ReasoningEffortLevel::Minimal),
            verbosity: Some(VerbosityLevel::High),
            ..ManualCompactionOptions::default()
        };

        let (_compacted, mode) = compact_history_manual(&provider, "stub-model", &history, &config, &options)
            .await
            .expect("manual compaction");

        assert_eq!(mode, CompactionMode::Provider);
        let captured = provider.last_options.lock().unwrap().clone().expect("captured options");
        assert_eq!(captured.instructions.as_deref(), Some("keep only decisions"));
        assert_eq!(captured.max_output_tokens, Some(256));
        assert_eq!(captured.reasoning_effort, Some(ReasoningEffortLevel::Minimal));
        assert_eq!(captured.verbosity, Some(VerbosityLevel::High));
    }

    #[tokio::test]
    async fn compact_history_manual_uses_native_inline_when_pause_and_compaction_present() {
        let history = sample_history();
        let config = CompactionConfig::default();
        let provider = InlinePauseProvider {
            last_request: Mutex::new(None),
            include_compaction_detail: true,
        };

        let (compacted, mode) =
            compact_history_manual(&provider, "stub-model", &history, &config, &ManualCompactionOptions::default())
                .await
                .expect("manual compaction");

        assert_eq!(mode, CompactionMode::Provider);
        assert_eq!(compacted.len(), 4);
        assert_eq!(compacted[0].role, MessageRole::Assistant);
        assert!(compacted[0].content.as_text().is_empty());
        let detail = compacted[0]
            .reasoning_details
            .as_ref()
            .and_then(|details| details.first())
            .expect("provider compaction detail");
        let detail: serde_json::Value = serde_json::from_value(detail.clone()).expect("provider detail");
        assert_eq!(detail["signature"], "provider-signature");
        assert_eq!(detail["opaque_extension"], "preserve-me");
        assert_eq!(compacted[1].content.as_text(), "first request");
        assert_eq!(compacted[2].content.as_text(), "working");
        assert_eq!(compacted[3].content.as_text(), "second request");

        // The inline request must carry the `compact_20260112` edit with a forced
        // pause so the provider actually performs compaction on demand.
        let captured = provider.last_request.lock().unwrap().clone().expect("captured inline request");
        let context_management = captured
            .context_management
            .as_ref()
            .expect("context_management set on inline compaction request");
        let edit = &context_management["edits"][0];
        assert_eq!(edit["type"].as_str(), Some("compact_20260112"));
        assert_eq!(edit["pause_after_compaction"].as_bool(), Some(true));
        assert_eq!(edit["trigger"]["value"].as_u64(), Some(50_000));
    }

    #[tokio::test]
    async fn inline_summary_without_opaque_detail_falls_back_to_local_mode() {
        let history = sample_history();
        let provider = InlinePauseProvider {
            last_request: Mutex::new(None),
            include_compaction_detail: false,
        };

        let (compacted, mode) = compact_history_manual_with_budget(
            &provider,
            "stub-model",
            &history,
            &CompactionConfig::default(),
            &ManualCompactionOptions::default(),
            Some(8_192),
        )
        .await
        .expect("manual compaction should fall back to local mode");

        assert_eq!(mode, CompactionMode::Local);
        assert_eq!(compacted[0].role, MessageRole::System);
        assert!(compacted[0].content.as_text().starts_with("Previous conversation summary:"));
    }

    #[tokio::test]
    async fn compact_history_manual_inline_request_carries_instructions_when_provided() {
        let history = sample_history();
        let config = CompactionConfig::default();
        let provider = InlinePauseProvider {
            last_request: Mutex::new(None),
            include_compaction_detail: true,
        };
        let options = ManualCompactionOptions {
            instructions: Some("  keep only decisions  ".to_string()),
            ..ManualCompactionOptions::default()
        };

        let (_compacted, _mode) = compact_history_manual(&provider, "stub-model", &history, &config, &options)
            .await
            .expect("manual compaction");

        let captured = provider.last_request.lock().unwrap().clone().expect("captured inline request");
        let edit = &captured.context_management.as_ref().expect("context_management")["edits"][0];
        assert_eq!(edit["instructions"].as_str(), Some("keep only decisions"));
    }

    #[tokio::test]
    async fn native_inline_fork_reuses_parent_prefix_when_supplied() {
        use super::{CompactionParentContext, compact_history_manual_with_parent_context};

        let history = sample_history();
        let config = CompactionConfig::default();
        let options = ManualCompactionOptions::default();

        // Asymmetric arms on the same history: without a parent the inline
        // request carries no fork fields; with one it carries all three while
        // the compaction edit stays intact in both.
        let provider = InlinePauseProvider {
            last_request: Mutex::new(None),
            include_compaction_detail: true,
        };
        let (_compacted, mode) = compact_history_manual_with_parent_context(
            &provider,
            "stub-model",
            &history,
            &config,
            &options,
            None,
            None,
        )
        .await
        .expect("manual compaction");
        assert_eq!(mode, CompactionMode::Provider);
        let bare = provider.last_request.lock().unwrap().clone().expect("captured inline request");
        assert!(bare.system_prompt.is_none());
        assert!(bare.tools.is_none());
        assert!(bare.context_management.is_some());

        let provider = InlinePauseProvider {
            last_request: Mutex::new(None),
            include_compaction_detail: true,
        };
        let parent = CompactionParentContext {
            system_prompt: Some(Arc::from("parent system")),
            tools: None,
        };
        let (_compacted, mode) = compact_history_manual_with_parent_context(
            &provider,
            "stub-model",
            &history,
            &config,
            &options,
            None,
            Some(&parent),
        )
        .await
        .expect("manual compaction");
        assert_eq!(mode, CompactionMode::Provider);
        let forked = provider.last_request.lock().unwrap().clone().expect("captured inline request");
        assert_eq!(forked.system_prompt.as_deref(), Some("parent system"));
        assert!(forked.tools.is_none(), "empty parent catalog must stay off the wire");
        assert!(matches!(forked.tool_choice, Some(crate::llm::provider::ToolChoice::None)));
        let edit = &forked.context_management.as_ref().expect("context_management")["edits"][0];
        assert_eq!(edit["type"].as_str(), Some("compact_20260112"));
        assert_eq!(edit["pause_after_compaction"].as_bool(), Some(true));
    }

    #[test]
    fn inline_compaction_edits_follow_the_documented_ladder_order() {
        use super::anthropic_inline_compaction_edits;

        let edits = anthropic_inline_compaction_edits(Some("keep decisions"), true);
        let types: Vec<&str> = edits
            .iter()
            .filter_map(|edit| edit.get("type").and_then(serde_json::Value::as_str))
            .collect();
        assert_eq!(
            types,
            vec![
                "clear_thinking_20251015",
                "clear_tool_uses_20250919",
                "compact_20260112"
            ],
            "thinking clears first, tool uses next, compaction last"
        );
        assert_eq!(edits[2]["pause_after_compaction"].as_bool(), Some(true));
        assert_eq!(edits[2]["instructions"].as_str(), Some("keep decisions"));

        let compact_only = anthropic_inline_compaction_edits(None, false);
        assert_eq!(compact_only.len(), 1);
        assert_eq!(compact_only[0]["type"].as_str(), Some("compact_20260112"));
    }

    #[tokio::test]
    async fn native_inline_request_carries_ladder_when_provider_supports_context_edits() {
        use super::compact_history_manual_with_parent_context;

        let history = sample_history();
        let config = CompactionConfig::default();
        let provider = ContextEditsInlineProvider { last_request: Mutex::new(None) };
        let (_compacted, mode) = compact_history_manual_with_parent_context(
            &provider,
            "stub-model",
            &history,
            &config,
            &ManualCompactionOptions::default(),
            None,
            None,
        )
        .await
        .expect("manual compaction");
        assert_eq!(mode, CompactionMode::Provider);
        let request = provider.last_request.lock().unwrap().clone().expect("captured inline request");
        let edits = request.context_management.as_ref().expect("context_management")["edits"]
            .as_array()
            .expect("edits array")
            .clone();
        let types: Vec<&str> = edits
            .iter()
            .filter_map(|edit| edit.get("type").and_then(serde_json::Value::as_str))
            .collect();
        assert_eq!(
            types,
            vec![
                "clear_thinking_20251015",
                "clear_tool_uses_20250919",
                "compact_20260112"
            ]
        );
    }

    #[tokio::test]
    async fn local_summary_retries_once_on_context_capacity_error() {
        use super::compact_history_manual;

        // Over-budget history so the halved retry is strictly smaller than
        // the first attempt (the retry is skipped when it cannot shrink).
        // Each turn is ~100k tokens against the stub's ~174k summarizer
        // budget: the first fork keeps one turn, the retry degrades to
        // previews, and the compacted output still fits the summary.
        let history = vec![
            Message::user("old ".repeat(100_000)),
            Message::user("middle ".repeat(100_000)),
            Message::user("newest ".repeat(100_000)),
        ];
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };
        let provider = CapacityFailOnceProvider {
            attempts: Mutex::new(0),
            request_tokens: Mutex::new(Vec::new()),
            failures: 1,
        };
        let (compacted, mode) =
            compact_history_manual(&provider, "stub-model", &history, &config, &ManualCompactionOptions::default())
                .await
                .expect("retry must recover the summary");
        assert_eq!(mode, CompactionMode::Local);
        assert_eq!(*provider.attempts.lock().unwrap(), 2, "exactly one retry");
        let tokens = provider.request_tokens.lock().unwrap().clone();
        assert_eq!(tokens.len(), 2);
        assert!(tokens[1] < tokens[0], "retry must shrink the fork");
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nretried summary");
    }

    #[tokio::test]
    async fn local_summary_skips_retry_when_it_cannot_shrink() {
        use super::compact_history_manual;

        // Tiny history fits the budget verbatim, so a halved retry would
        // resend the identical fork. The capacity failure must propagate
        // without a wasted second call.
        let history = sample_history();
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };
        let provider = CapacityFailOnceProvider {
            attempts: Mutex::new(0),
            request_tokens: Mutex::new(Vec::new()),
            failures: 1,
        };
        let error =
            compact_history_manual(&provider, "stub-model", &history, &config, &ManualCompactionOptions::default())
                .await
                .expect_err("identical retry must not be attempted");
        assert_eq!(*provider.attempts.lock().unwrap(), 1, "no second call");
        assert!(error.to_string().contains("Failed to generate compaction summary"));
    }

    #[tokio::test]
    async fn hierarchical_bands_retry_once_on_context_capacity_error() {
        use super::compact_history_manual_with_budget;

        // Small session budget forces over-budget bands; the failing abstract
        // pass must retry with a halved band and the detail pass must still
        // run, so exactly three generate calls happen.
        let mut history = Vec::new();
        for turn in ["first", "second", "third", "fourth", "fifth", "sixth"] {
            history.push(Message::user(format!("{turn} {}", "text ".repeat(1_500))));
        }
        history.push(Message::user(format!("latest {}", "big ".repeat(25_000))));
        let config = CompactionConfig {
            hierarchical: true,
            always_summarize: true,
            ..CompactionConfig::default()
        };
        let provider = CapacityFailOnceProvider {
            attempts: Mutex::new(0),
            request_tokens: Mutex::new(Vec::new()),
            failures: 1,
        };
        let (_compacted, mode) = compact_history_manual_with_budget(
            &provider,
            "stub-model",
            &history,
            &config,
            &ManualCompactionOptions::default(),
            Some(3_000),
        )
        .await
        .expect("hierarchical bands must recover from a capacity error");
        assert_eq!(mode, CompactionMode::Local);
        assert_eq!(*provider.attempts.lock().unwrap(), 3, "abstract retries once, detail runs once");
    }

    #[test]
    fn route_policy_defaults_to_status_quo_shape() {
        use super::CompactionRoutePolicy;

        let policy = CompactionRoutePolicy::resolve("unknown-provider", "unknown-model");
        assert_eq!(policy, CompactionRoutePolicy::default());
        assert!((policy.threshold_ratio - 1.0).abs() < f64::EPSILON);
        assert!((policy.retain_ratio - 0.16).abs() < f64::EPSILON);
        assert_eq!(policy.max_overflow_retries, 1);
    }

    #[test]
    fn route_tail_target_scales_with_window() {
        use super::{CONTINUITY_TAIL_TARGET_TOKENS, CompactionRoutePolicy};

        let policy = CompactionRoutePolicy::default();
        // Unknown windows keep the legacy constant.
        assert_eq!(policy.tail_target_tokens(0), CONTINUITY_TAIL_TARGET_TOKENS);
        // Large windows keep the legacy constant exactly.
        assert_eq!(policy.tail_target_tokens(1_000_000), CONTINUITY_TAIL_TARGET_TOKENS);
        assert_eq!(policy.tail_target_tokens(128_000), CONTINUITY_TAIL_TARGET_TOKENS);
        // Small windows scale down so the summary survives output bounding.
        assert_eq!(policy.tail_target_tokens(32_768), 5_242);
        // Tiny windows keep a meaningful floor instead of collapsing to zero.
        assert_eq!(policy.tail_target_tokens(4_096), 1_024);
    }

    #[test]
    fn compacted_history_shrinks_rejects_growth() {
        use super::compacted_history_shrinks;
        use crate::exec::events::CompactionMode;

        // Reported `86 -> 88` regression: a rebuild that keeps every message
        // plus framing must be discarded, accounting for the envelope message
        // Local persistence injects.
        assert!(!compacted_history_shrinks(86, 87, CompactionMode::Local));
        assert!(!compacted_history_shrinks(12, 12, CompactionMode::Local));
        assert!(!compacted_history_shrinks(0, 0, CompactionMode::Local));
        // Genuine compression passes, including the envelope slot. Note a
        // one-message reduction is still rejected for Local mode: the
        // envelope re-adds it, netting zero.
        assert!(compacted_history_shrinks(88, 11, CompactionMode::Local));
        assert!(compacted_history_shrinks(12, 10, CompactionMode::Local));
        assert!(!compacted_history_shrinks(12, 11, CompactionMode::Local));
        // Provider-native windows carry no envelope framing, so the raw
        // lengths compare directly.
        assert!(compacted_history_shrinks(12, 11, CompactionMode::Provider));
        assert!(!compacted_history_shrinks(12, 12, CompactionMode::Provider));
        assert!(!compacted_history_shrinks(12, 13, CompactionMode::Provider));
    }

    #[test]
    fn threshold_cap_only_fires_earlier() {
        use super::CompactionRoutePolicy;

        let policy = CompactionRoutePolicy::default();
        assert_eq!(policy.apply_threshold_cap(900_000, 1_000_000), 900_000);
        let eager = CompactionRoutePolicy {
            threshold_ratio: 0.7,
            ..CompactionRoutePolicy::default()
        };
        assert_eq!(eager.apply_threshold_cap(900_000, 1_000_000), 700_000);
        assert_eq!(eager.apply_threshold_cap(500_000, 1_000_000), 500_000);
        assert_eq!(eager.apply_threshold_cap(900_000, 0), 900_000);
    }

    #[test]
    fn prune_oversized_tool_outputs_trims_only_tool_dumps() {
        use super::{TOOL_RESULT_PRUNE_TARGET_TOKENS, prune_oversized_tool_outputs};

        let user = Message::user("do the thing".to_string());
        let mut assistant = Message::assistant("calling tool".to_string());
        assistant.tool_calls = Some(vec![ToolCall::function(
            "call-big".to_string(),
            "read_file".to_string(),
            "{}".to_string(),
        )]);
        let huge = {
            let mut message = Message::tool_response("call-big".to_string(), "data ".repeat(20_000));
            message.tool_call_id = Some("call-big".to_string());
            message
        };
        let small = Message::tool_response("call-small".to_string(), "ok".to_string());
        let history = vec![user.clone(), assistant.clone(), huge.clone(), small.clone()];

        let pruned = prune_oversized_tool_outputs(&history);
        assert_eq!(pruned.len(), history.len());
        // Untouched roles pass through verbatim.
        assert_eq!(pruned[0].content.as_text(), user.content.as_text());
        assert_eq!(pruned[1].content.as_text(), assistant.content.as_text());
        // The dump shrinks within budget while the small result is identical.
        // Previews keep the head: the start survives, the tail does not.
        let huge_text = huge.content.as_text();
        let pruned_text = pruned[2].content.as_text();
        assert!(pruned[2].estimate_tokens() < huge.estimate_tokens(), "oversized tool output must shrink");
        assert!(
            pruned[2].estimate_tokens() <= TOOL_RESULT_PRUNE_TARGET_TOKENS + 32,
            "pruned output must respect the cap, used {}",
            pruned[2].estimate_tokens()
        );
        assert!(pruned_text.len() < huge_text.len(), "preview must be shorter than the dump");
        assert!(huge_text.starts_with(pruned_text.trim_end_matches('.')), "preview must be a head truncation");
        assert_eq!(pruned[2].tool_call_id.as_deref(), Some("call-big"), "pairing ID must survive");
        assert_eq!(pruned[3].content.as_text(), small.content.as_text());
    }

    #[tokio::test]
    async fn capacity_retry_halves_progressively_until_recovery() {
        use super::generate_summary_with_capacity_retry;

        // Each turn is tens of thousands of tokens against a 30k budget: the
        // first fork keeps one turn, then two halved retries shrink strictly
        // before the third attempt succeeds.
        let history = vec![
            Message::user("a ".repeat(20_000)),
            Message::user("b ".repeat(20_000)),
            Message::user("c ".repeat(20_000)),
        ];
        let provider = CapacityFailOnceProvider {
            attempts: Mutex::new(0),
            request_tokens: Mutex::new(Vec::new()),
            failures: 2,
        };
        let summary = generate_summary_with_capacity_retry(
            &provider,
            "stub-model",
            &history,
            "test instructions",
            &history,
            Some(30_000),
            2,
            "Failed to generate compaction summary",
            |source| {
                super::compaction_summary_request("stub-model", source, "test instructions", None, None, None, None)
            },
        )
        .await
        .expect("two retries must recover the summary");
        assert_eq!(summary, "retried summary");
        assert_eq!(*provider.attempts.lock().unwrap(), 3, "two failures then success");
        let tokens = provider.request_tokens.lock().unwrap().clone();
        assert_eq!(tokens.len(), 3);
        assert!(tokens[0] > tokens[1] && tokens[1] > tokens[2], "each retry must shrink strictly, got {tokens:?}");
    }

    #[tokio::test]
    async fn capacity_retry_recovers_with_unknown_budget_fallback() {
        use super::generate_summary_with_capacity_retry;

        // Unknown window (`None` budget) sends the first fork verbatim. A
        // capacity rejection must still shrink via a fallback derived from
        // the failing attempt instead of resending the identical fork.
        // Asymmetric vs the verbatim/no-retry case: oldest vs newest text
        // differ so trimming the oldest is observable.
        let history = vec![
            Message::user(format!("oldest {}", "old ".repeat(20_000))),
            Message::user(format!("newest {}", "new ".repeat(20_000))),
        ];
        let provider = UnknownBudgetFailOnceProvider {
            attempts: Mutex::new(0),
            request_tokens: Mutex::new(Vec::new()),
        };
        let summary = generate_summary_with_capacity_retry(
            &provider,
            "stub-model",
            &history,
            "test instructions",
            &history,
            None,
            1,
            "Failed to generate compaction summary",
            |source| {
                super::compaction_summary_request("stub-model", source, "test instructions", None, None, None, None)
            },
        )
        .await
        .expect("unknown-budget capacity error must recover via fallback");
        assert_eq!(summary, "retried summary");
        assert_eq!(*provider.attempts.lock().unwrap(), 2, "one failure then success");
        let tokens = provider.request_tokens.lock().unwrap().clone();
        assert_eq!(tokens.len(), 2);
        assert!(tokens[1] < tokens[0], "fallback retry must shrink, got {tokens:?}");
    }

    #[tokio::test]
    async fn empty_summary_fails_with_diagnostic() {
        use super::generate_summary_with_capacity_retry;

        let history = sample_history();
        let provider = EmptySummaryProvider;
        let error = generate_summary_with_capacity_retry(
            &provider,
            "stub-model",
            &history,
            "test instructions",
            &history,
            Some(30_000),
            1,
            "Failed to generate compaction summary",
            |source| {
                super::compaction_summary_request("stub-model", source, "test instructions", None, None, None, None)
            },
        )
        .await
        .expect_err("empty provider summary must fail");
        let message = format!("{error:#}");
        assert!(message.contains("Failed to generate compaction summary"), "outer context preserved: {message}");
        assert!(message.contains("empty summary"), "empty cause surfaced: {message}");
        assert!(message.contains("empty-summary"), "provider identity surfaced: {message}");
    }

    #[tokio::test]
    async fn capacity_error_context_carries_route_diagnostics() {
        // Tiny fitting history with a capacity failure cannot shrink, so the
        // original failure propagates. The propagated error must carry the
        // route diagnostics needed to debug `/compact` without guessing.
        let history = sample_history();
        let provider = CapacityFailOnceProvider {
            attempts: Mutex::new(0),
            request_tokens: Mutex::new(Vec::new()),
            failures: 1,
        };
        let error = compact_history_manual(
            &provider,
            "stub-model",
            &history,
            &CompactionConfig {
                always_summarize: true,
                ..CompactionConfig::default()
            },
            &ManualCompactionOptions::default(),
        )
        .await
        .expect_err("identical retry must not be attempted");
        let message = format!("{error:#}");
        assert!(message.contains("Failed to generate compaction summary"), "outer context: {message}");
        assert!(message.contains("capacity-fail-once"), "provider name: {message}");
        assert!(message.contains("stub-model"), "model name: {message}");
        assert!(message.contains("input_tokens="), "token counts: {message}");
    }

    #[tokio::test]
    async fn compact_history_manual_falls_back_to_local_when_inline_compaction_not_fired() {
        let history = sample_history();
        let config = CompactionConfig::default();

        // NativeCompactionProvider is inline-capable but its `generate` returns a
        // normal `Stop` with no compaction block, so the inline attempt cannot
        // fire and the dispatch must transparently fall back to Local.
        let (compacted, mode) = compact_history_manual(
            &NativeCompactionProvider,
            "stub-model",
            &history,
            &config,
            &ManualCompactionOptions::default(),
        )
        .await
        .expect("manual compaction");

        assert_eq!(mode, CompactionMode::Local);
        assert_eq!(compacted.len(), 4);
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
    }

    #[tokio::test]
    async fn compact_history_manual_falls_back_to_local_when_inline_request_errors() {
        let history = sample_history();
        let config = CompactionConfig::default();

        // A provider dispatched to NativeInline that rejects the Anthropic
        // `compact_20260112` edit must not abort the whole command; the dispatch
        // falls back to Local summarization (the manual `/compact` contract:
        // always succeeds).
        let (compacted, mode) = compact_history_manual(
            &InlineRejectingProvider,
            "stub-model",
            &history,
            &config,
            &ManualCompactionOptions::default(),
        )
        .await
        .expect("manual compaction should fall back to local");

        assert_eq!(mode, CompactionMode::Local);
        assert_eq!(compacted.len(), 4);
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
    }

    #[tokio::test]
    async fn compact_history_manual_applies_options_to_local_summary_request() {
        let history = sample_history();
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };
        let provider = CapturingProvider { last_request: Mutex::new(None) };
        let options = ManualCompactionOptions {
            instructions: Some("KEEP DECISIONS ONLY".to_string()),
            max_output_tokens: Some(128),
            reasoning_effort: Some(ReasoningEffortLevel::Minimal),
            verbosity: Some(VerbosityLevel::High),
            ..ManualCompactionOptions::default()
        };

        let (compacted, mode) = compact_history_manual(&provider, "stub-model", &history, &config, &options)
            .await
            .expect("manual compaction");

        assert_eq!(mode, CompactionMode::Local);
        let captured = provider.last_request.lock().unwrap().clone().expect("captured summary request");
        assert_eq!(captured.max_tokens, Some(128));
        assert_eq!(captured.reasoning_effort, Some(ReasoningEffortLevel::Minimal));
        assert_eq!(captured.verbosity, Some(VerbosityLevel::High));
        // Cache-safe forking: the parent history prefix is reused verbatim and
        // the custom instructions are appended as the only new turn.
        assert_eq!(captured.messages.len(), history.len() + 1);
        for (sent, original) in captured.messages.iter().zip(history.iter()) {
            assert_eq!(sent.content.as_text(), original.content.as_text());
        }
        let prompt = captured.messages.last().expect("compaction prompt").content.as_text();
        assert!(prompt.contains("KEEP DECISIONS ONLY"));
        assert!(!prompt.contains("acceptance criteria"));
        // The fork must not invite tool calls: same tools may be present for
        // prefix reuse, but the choice disables invocation.
        assert!(matches!(captured.tool_choice, Some(crate::llm::provider::ToolChoice::None)));
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
    }

    #[tokio::test]
    async fn compact_history_manual_blocks_unsupported_reasoning_before_summary() {
        let provider = LimitedReasoningProvider { last_request: Mutex::new(None) };
        let options = ManualCompactionOptions {
            reasoning_effort: Some(ReasoningEffortLevel::Max),
            ..ManualCompactionOptions::default()
        };
        let error = compact_history_manual(
            &provider,
            "stub-model",
            &sample_history(),
            &CompactionConfig {
                always_summarize: true,
                ..CompactionConfig::default()
            },
            &options,
        )
        .await
        .expect_err("unsupported compaction effort must block");

        assert!(
            error
                .downcast_ref::<crate::llm::reasoning_effort::ReasoningEffortUnsupported>()
                .is_some(),
            "strict compaction failure should preserve the capability diagnostic: {error:#}"
        );
        assert!(provider.last_request.lock().unwrap().is_none(), "provider must not receive a blocked request");
    }

    #[tokio::test]
    async fn compact_history_manual_downgrades_only_when_explicitly_enabled() {
        let provider = LimitedReasoningProvider { last_request: Mutex::new(None) };
        let options = ManualCompactionOptions {
            reasoning_effort: Some(ReasoningEffortLevel::Max),
            allow_reasoning_effort_downgrade: true,
            ..ManualCompactionOptions::default()
        };
        let config = CompactionConfig {
            always_summarize: true,
            hierarchical: true,
            ..CompactionConfig::default()
        };
        compact_history_manual(&provider, "stub-model", &sample_history(), &config, &options)
            .await
            .expect("explicit downgrade should permit compaction");

        let captured = provider.last_request.lock().unwrap().clone().expect("summary request captured");
        assert_eq!(captured.reasoning_effort, Some(ReasoningEffortLevel::High));
    }

    #[tokio::test]
    async fn compact_history_manual_returns_empty_for_empty_history() {
        let (compacted, mode) = compact_history_manual(
            &StubProvider,
            "stub-model",
            &[],
            &CompactionConfig::default(),
            &ManualCompactionOptions::default(),
        )
        .await
        .expect("manual compaction");

        assert!(compacted.is_empty());
        assert_eq!(mode, CompactionMode::Local);
    }

    #[tokio::test]
    async fn compact_history_rebuilds_history_around_summary_and_important_messages() {
        let history = vec![
            Message::assistant("setup".to_string()),
            Message::user("first request".to_string()),
            Message::assistant("working".to_string()),
            Message::tool_response("call-1".to_string(), "done".to_string()),
            Message::user("second request".to_string()),
            Message::assistant("final reply".to_string()),
        ];
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };

        let compacted = compact_history(&StubProvider, "stub-model", &history, &config)
            .await
            .expect("compacted history");

        // Summary plus the complete newest protocol groups. The assistant/tool
        // messages remain paired with their user anchors in the continuity
        // tail.
        assert_eq!(compacted.len(), 6);
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
        assert_eq!(compacted[1].content.as_text(), "first request");
        assert_eq!(compacted[2].content.as_text(), "working");
        assert_eq!(compacted[3].content.as_text(), "done");
        assert_eq!(compacted[4].content.as_text(), "second request");
        assert_eq!(compacted[5].content.as_text(), "final reply");
    }

    #[tokio::test]
    async fn legacy_compaction_uses_local_for_responses_only_capability() {
        let config = CompactionConfig {
            keep_last_messages: 0,
            ..CompactionConfig::default()
        };

        // Responses capability alone is not enough for this legacy entry point:
        // the provider's `compact_history` method is only valid for standalone
        // compaction endpoints. The universal local path must remain usable.
        let compacted = compact_history(&CompatibleEndpointProvider, "stub-model", &sample_history(), &config)
            .await
            .expect("local compaction should handle responses-only providers");

        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
    }

    #[tokio::test]
    async fn compact_history_preserves_continuity_tail_over_retention_budget() {
        let history = vec![
            Message::user("alpha beta gamma delta epsilon zeta".to_string()),
            Message::assistant("ack".to_string()),
            Message::user("newest request".to_string()),
        ];
        let config = CompactionConfig {
            always_summarize: true,
            retained_user_message_tokens: 8,
            ..CompactionConfig::default()
        };

        let compacted = compact_history(&StubProvider, "stub-model", &history, &config)
            .await
            .expect("compacted history");

        assert_eq!(compacted.len(), 4);
        assert_eq!(compacted[1].content.as_text(), "alpha beta gamma delta epsilon zeta");
        assert_eq!(compacted[2].content.as_text(), "ack");
        assert_eq!(compacted[3].content.as_text(), "newest request");
    }

    #[tokio::test]
    async fn compacted_history_respects_model_context_budget() {
        let mut history = Vec::new();
        for index in 0..24 {
            history.push(Message::user(format!("request-{index} {}", "context ".repeat(1_200))));
            history.push(Message::assistant(format!("completed request {index}")));
        }
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };

        let compacted = compact_history(&StubProvider, "stub-model", &history, &config)
            .await
            .expect("compacted history");
        let estimated_tokens = compacted.iter().map(Message::estimate_tokens).sum::<usize>();

        assert!(estimated_tokens <= 32_768 - 512);
    }

    #[tokio::test]
    async fn compact_history_caps_retained_user_message_count() {
        let history = vec![
            Message::user("first request".to_string()),
            Message::assistant("ack".to_string()),
            Message::user("second request".to_string()),
            Message::assistant("ack".to_string()),
            Message::user("third request".to_string()),
            Message::assistant("ack".to_string()),
            Message::user("fourth request".to_string()),
            Message::assistant("ack".to_string()),
            Message::user("fifth request".to_string()),
        ];
        let config = CompactionConfig {
            always_summarize: true,
            retained_user_messages: 4,
            ..CompactionConfig::default()
        };

        let compacted = compact_history(&StubProvider, "stub-model", &history, &config)
            .await
            .expect("compacted history");

        let continuity_tail = compacted
            .iter()
            .skip(1)
            .map(|message| message.content.as_text().to_string())
            .collect::<Vec<_>>();
        assert_eq!(continuity_tail.len(), history.len());
        assert_eq!(continuity_tail[0], "first request");
        assert_eq!(continuity_tail[8], "fifth request");
    }

    #[tokio::test]
    async fn compact_history_forces_local_summary_when_always_summarize_is_enabled() {
        let history = vec![
            Message::user("first request".to_string()),
            Message::assistant("working".to_string()),
            Message::user("second request".to_string()),
        ];
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };

        let compacted = compact_history(&NativeCompactionProvider, "stub-model", &history, &config)
            .await
            .expect("compacted history");

        assert_eq!(compacted.len(), 4);
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
        assert_eq!(compacted[1].content.as_text(), "first request");
        assert_eq!(compacted[2].content.as_text(), "working");
        assert_eq!(compacted[3].content.as_text(), "second request");
    }

    #[test]
    fn default_summary_prompt_preserves_required_compaction_context() {
        let prompt = CompactionConfig::default().summary_prompt;

        assert!(prompt.contains("acceptance criteria"));
        assert!(prompt.contains("file paths that were read or modified"));
        assert!(prompt.contains("test results and error messages"));
        assert!(prompt.contains("decisions with their reasoning"));
    }

    #[test]
    fn continuity_tail_keeps_complete_turn_but_drops_unmatched_tool_call() {
        // A completed turn: user -> assistant(tool call) -> tool result. The
        // tail must keep the whole turn intact (the tool result makes the
        // trailing assistant tool call valid to send).
        let complete = vec![
            Message::user("do the thing".into()),
            {
                let mut m = Message::assistant("calling".into());
                m.tool_calls = Some(vec![ToolCall::function("c1".into(), "run".into(), "{}".into())]);
                m
            },
            Message::tool_response("c1".into(), "ran".into()),
        ];
        assert_eq!(continuity_tail(&complete).len(), 3);

        // An interrupted turn: user -> assistant(tool call) with no tool
        // result. Sending the trailing assistant message to a provider is
        // invalid, so the tail must drop it and keep only the user message.
        let interrupted = vec![Message::user("do the thing".into()), {
            let mut m = Message::assistant("calling".into());
            m.tool_calls = Some(vec![ToolCall::function("c1".into(), "run".into(), "{}".into())]);
            m
        }];
        let tail = continuity_tail(&interrupted);
        assert_eq!(tail.len(), 1);
        assert_eq!(tail[0].role, MessageRole::User);

        let parallel_complete = vec![
            Message::user("run both".into()),
            Message::assistant_with_tools(
                "calling".into(),
                vec![
                    ToolCall::function("c1".into(), "run".into(), "{}".into()),
                    ToolCall::function("c2".into(), "run".into(), "{}".into()),
                ],
            ),
            Message::tool_response("c1".into(), "first result".into()),
            Message::tool_response("c2".into(), "second result".into()),
        ];
        assert_eq!(continuity_tail(&parallel_complete).len(), 4);

        let parallel_interrupted = parallel_complete[..3].to_vec();
        let tail = continuity_tail(&parallel_interrupted);
        assert_eq!(tail.len(), 1, "a missing parallel result drops the whole assistant call");
        assert_eq!(tail[0].content.as_text(), "run both");
    }

    #[test]
    fn continuity_tail_keeps_newest_complete_groups_within_budget() {
        let mut history = Vec::new();
        for group_index in 0..12 {
            history.push(Message::user(format!("group-{group_index} {}", "alpha beta gamma delta ".repeat(2_000))));
            history.push(Message::assistant(format!("completed group {group_index}")));
        }

        let tail = continuity_tail(&history);
        let estimated_tokens = tail.iter().map(Message::estimate_tokens).sum::<usize>();

        assert!(estimated_tokens <= super::CONTINUITY_TAIL_TARGET_TOKENS);
        assert_eq!(tail.first().map(|message| message.role), Some(MessageRole::User));
        assert!(tail.iter().any(|message| message.content.as_text().contains("group-11")));
        assert!(!tail.iter().any(|message| message.content.as_text().contains("group-0 ")));
        assert_eq!(tail.len() % 2, 0, "protocol groups must remain atomic");
    }

    #[test]
    fn continuity_tail_bounds_an_oversized_newest_group() {
        let history = vec![
            Message::user("u".repeat(100_000)),
            Message::assistant("a".repeat(100_000)),
        ];

        let tail = continuity_tail(&history);

        assert_eq!(tail.len(), 2);
        assert!(tail.iter().map(Message::estimate_tokens).sum::<usize>() <= super::CONTINUITY_TAIL_TARGET_TOKENS);
    }

    #[test]
    fn continuity_tail_bounds_tool_call_metadata_without_breaking_correlation() {
        let mut assistant = Message::assistant(String::new());
        assistant.tool_calls = Some(vec![ToolCall::function(
            "call-large".into(),
            "run_command".into(),
            format!("{{\"command\":\"{}\"}}", "x".repeat(300_000)),
        )]);
        let history = vec![
            Message::user("run the command".into()),
            assistant,
            Message::tool_response("call-large".into(), "completed".into()),
        ];

        let tail = continuity_tail(&history);
        assert!(tail.iter().map(Message::estimate_tokens).sum::<usize>() <= super::CONTINUITY_TAIL_TARGET_TOKENS);
        let call = tail[1].tool_calls.as_ref().expect("tool call should remain").first().unwrap();
        assert_eq!(call.id, "call-large");
        assert_eq!(call.function.as_ref().unwrap().arguments, "{}");
        assert_eq!(tail[2].tool_call_id.as_deref(), Some("call-large"));
    }

    /// History-growth verify-item: the local summarization prompt must exclude
    /// `Message.reasoning` / `reasoning_details` and serialize only the visible
    /// text content (`content.as_text()`). Reasoning traces are large and
    /// ephemeral; including them in every compaction summary would bloat the
    /// post-compaction context and re-inject stale chain-of-thought. The
    /// continuity tail preserves provider-protocol reasoning (Anthropic
    /// thinking signatures, OpenAI reasoning items) separately and is NOT
    /// summarized, so stripping reasoning here is safe and correct. This test
    /// pins that invariant so a change to `build_summary_prompt` cannot
    /// accidentally start including reasoning.
    #[test]
    fn build_summary_prompt_excludes_reasoning_traces() {
        use super::build_summary_prompt;

        let instructions = "Summarize the conversation.";
        // Assistant message carrying a reasoning trace alongside its visible
        // content. If `build_summary_prompt` ever reads `reasoning`, the
        // summary would contain "SECRET_REASONING" and "raw-only reasoning".
        let history = vec![
            Message::user("What is 2+2?".to_string()),
            Message::assistant("The answer is 4.".to_string())
                .with_reasoning(Some("SECRET_REASONING: I computed 2+2=4.".to_string())),
        ];

        let prompt = build_summary_prompt(&history, instructions);

        assert!(prompt.contains("The answer is 4."), "visible assistant text must appear in the summary prompt");
        assert!(
            !prompt.contains("SECRET_REASONING"),
            "Message.reasoning must NOT be included in the summary prompt -- \
             it would bloat every compaction pass with ephemeral chain-of-thought"
        );
        assert!(prompt.contains("Summarize the conversation."), "instructions must appear in the summary prompt");
    }

    #[test]
    fn coherence_force_keeps_tool_results_not_in_selection() {
        // Regression: `coherence_tool_call_pairs` force-keeps the Tool results
        // that follow a retained Assistant-with-tool_calls, but the previous
        // implementation derived its output from `selected` only, so those
        // force-kept messages silently vanished. A compacted history can then
        // carry an Assistant tool-call with no result, which providers reject.
        use super::coherence_tool_call_pairs;

        let history = vec![
            Message::user("check the code".to_string()),
            Message::assistant("Looking...".to_string()).with_tool_calls(vec![ToolCall {
                id: "call_1".to_string(),
                call_type: "function".to_string(),
                function: Some(crate::llm::provider::FunctionCall {
                    namespace: None,
                    name: "read_file".to_string(),
                    arguments: "{}".to_string(),
                }),
                text: None,
                thought_signature: None,
            }]),
            Message::tool_response("call_1".to_string(), "tool result that must survive".to_string()),
            Message::assistant("Done.".to_string()),
        ];

        // Selection keeps only the assistant-with-tool-calls turn, dropping the
        // following Tool result from the budget-driven selection.
        let selected = vec![(1, history[1].clone())];
        let result = coherence_tool_call_pairs(&history, &selected);

        let tool_results = result
            .iter()
            .filter(|(_, m)| m.role == MessageRole::Tool)
            .map(|(idx, m)| (*idx, m.content.as_text().to_string()))
            .collect::<Vec<_>>();

        assert_eq!(
            tool_results,
            vec![(2usize, "tool result that must survive".to_string())],
            "force-kept tool result must survive compaction even when not selected"
        );
        // History order must be preserved with the assistant preceding its result.
        let indices = result.iter().map(|(idx, _)| *idx).collect::<Vec<_>>();
        assert_eq!(indices, vec![1, 2]);
    }

    #[test]
    fn coherence_drops_orphaned_tool_results_of_unretained_assistant() {
        use super::coherence_tool_call_pairs;

        let history = vec![
            Message::user("check".to_string()),
            Message::assistant("Looking...".to_string()).with_tool_calls(vec![ToolCall {
                id: "call_1".to_string(),
                call_type: "function".to_string(),
                function: Some(crate::llm::provider::FunctionCall {
                    namespace: None,
                    name: "read_file".to_string(),
                    arguments: "{}".to_string(),
                }),
                text: None,
                thought_signature: None,
            }]),
            Message::tool_response("call_1".to_string(), "orphan result".to_string()),
        ];

        // The calling assistant was NOT retained; its orphaned Tool result must
        // be dropped so we never emit a result the model never saw a call for.
        let selected = vec![(0, history[0].clone())];
        let result = coherence_tool_call_pairs(&history, &selected);

        assert_eq!(result.len(), 1, "orphaned tool result must be dropped");
        assert_eq!(result[0].0, 0);
    }

    #[test]
    fn cache_safe_fork_reuses_parent_prefix_with_appended_prompt() {
        use super::{CompactionParentContext, build_cache_safe_compaction_history, compaction_summary_request};

        let history = sample_history();
        let forked = build_cache_safe_compaction_history(&history, "Summarize now.");
        assert_eq!(forked.len(), history.len() + 1);
        for (forked_msg, original) in forked.iter().zip(history.iter()) {
            assert_eq!(forked_msg.content.as_text(), original.content.as_text());
            assert_eq!(forked_msg.role, original.role);
        }
        let last = forked.last().expect("appended compaction prompt");
        assert_eq!(last.role, MessageRole::User);
        assert_eq!(last.content.as_text(), "Summarize now.");

        // Parent prefix reuse: same system/tools, tool calls disabled.
        let parent = CompactionParentContext {
            system_prompt: Some(Arc::from("stable system")),
            tools: Some(Arc::new(vec![crate::llm::provider::ToolDefinition::function(
                "read".to_string(),
                "read".to_string(),
                serde_json::json!({"type": "object"}),
            )])),
        };
        let request =
            compaction_summary_request("stub-model", &history, "Summarize now.", None, None, None, Some(&parent));
        assert_eq!(request.system_prompt.as_deref(), Some("stable system"));
        assert_eq!(request.tools.as_deref().map(Vec::len), Some(1));
        assert_eq!(request.messages.len(), history.len() + 1);
        assert!(matches!(request.tool_choice, Some(crate::llm::provider::ToolChoice::None)));
        assert!(!parent.is_empty());
        assert!(CompactionParentContext::default().is_empty());
    }

    #[tokio::test]
    async fn manual_compaction_with_parent_context_reuses_prefix() {
        use super::{CompactionParentContext, compact_history_manual_with_parent_context};

        let history = sample_history();
        let config = CompactionConfig {
            always_summarize: true,
            ..CompactionConfig::default()
        };
        let provider = CapturingProvider { last_request: Mutex::new(None) };
        let parent = CompactionParentContext {
            system_prompt: Some(Arc::from("parent system")),
            tools: None,
        };
        let (compacted, mode) = compact_history_manual_with_parent_context(
            &provider,
            "stub-model",
            &history,
            &config,
            &ManualCompactionOptions::default(),
            None,
            Some(&parent),
        )
        .await
        .expect("parent-aware compaction");
        assert_eq!(mode, CompactionMode::Local);
        let captured = provider.last_request.lock().unwrap().clone().expect("captured request");
        assert_eq!(captured.system_prompt.as_deref(), Some("parent system"));
        assert_eq!(captured.messages.len(), history.len() + 1);
        assert_eq!(compacted[0].content.as_text(), "Previous conversation summary:\nsummary");
    }

    #[tokio::test]
    async fn hierarchical_bands_reuse_parent_prefix_without_tool_calls() {
        use super::{CompactionParentContext, compact_history_manual_with_parent_context};

        let history = (0..12)
            .map(|index| Message::user(format!("hierarchical request {index}")))
            .collect::<Vec<_>>();
        let config = CompactionConfig {
            always_summarize: true,
            hierarchical: true,
            ..CompactionConfig::default()
        };
        let provider = CapturingProvider { last_request: Mutex::new(None) };
        let parent = CompactionParentContext {
            system_prompt: Some(Arc::from("parent system")),
            tools: None,
        };
        let (compacted, mode) = compact_history_manual_with_parent_context(
            &provider,
            "stub-model",
            &history,
            &config,
            &ManualCompactionOptions::default(),
            None,
            Some(&parent),
        )
        .await
        .expect("hierarchical compaction");
        assert_eq!(mode, CompactionMode::Local);
        // CapturingProvider keeps the last request, which is the detail band.
        let captured = provider.last_request.lock().unwrap().clone().expect("captured detail request");
        assert_eq!(captured.system_prompt.as_deref(), Some("parent system"));
        assert!(matches!(captured.tool_choice, Some(crate::llm::provider::ToolChoice::None)));
        assert!(!compacted.is_empty());
    }
}