stakai 0.3.80

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

use super::types::{
    AnthropicAuth, AnthropicCacheControl, AnthropicConfig, AnthropicContent, AnthropicMessage,
    AnthropicMessageContent, AnthropicRequest, AnthropicResponse, AnthropicSource,
    AnthropicSystemBlock, AnthropicSystemContent, AnthropicThinkingConfig as AnthropicThinking,
    CLAUDE_CODE_SYSTEM_PREFIX, infer_max_tokens,
};
use crate::error::{Error, Result};
use crate::types::{
    CacheContext, CacheControlValidator, CacheWarning, CacheWarningType, ContentPart, FinishReason,
    FinishReasonKind, GenerateRequest, GenerateResponse, InputTokenDetails, Message,
    OutputTokenDetails, ResponseContent, Role, Usage,
};
use serde_json::json;
use std::collections::HashSet;

/// Check whether the target model belongs to the Opus 4.7 (or later) family.
///
/// Opus 4.7 dropped `temperature`, `top_p`, `top_k`, and `thinking.budget_tokens` from
/// the Messages API. This helper centralizes detection so the conversion layer can shape
/// requests to the subset of parameters those models still accept. Case-insensitive prefix
/// match, mirroring `is_reasoning_model` in `providers/openai/convert.rs`.
///
/// See: https://platform.claude.com/docs/en/about-claude/models/whats-new-claude-4-7
fn is_opus_4_7_or_later(model_id: &str) -> bool {
    let id = model_id.to_lowercase();
    id.starts_with("claude-opus-4-7")
}

/// Result of converting a request to Anthropic format
pub struct AnthropicConversionResult {
    /// The converted request
    pub request: AnthropicRequest,
    /// Warnings generated during conversion (e.g., cache validation)
    pub warnings: Vec<CacheWarning>,
    /// Whether any cache control was used (to determine if beta header is needed)
    pub has_cache_control: bool,
}

/// Convert unified request to Anthropic request with smart caching
///
/// This function applies the caching strategy from the request options,
/// falling back to the provider's default strategy if not specified.
pub fn to_anthropic_request(
    req: &GenerateRequest,
    config: &AnthropicConfig,
    stream: bool,
) -> Result<AnthropicConversionResult> {
    let mut validator = CacheControlValidator::new();

    // Determine the effective caching strategy:
    // 1. Request-level strategy takes precedence
    // 2. Fall back to provider default
    let cache_strategy = req
        .options
        .cache_strategy
        .clone()
        .unwrap_or_else(|| config.default_cache_strategy.clone());

    let cache_config = cache_strategy.to_anthropic_config();

    // Check if we have tools (for cache budget calculation)
    let has_tools = req.options.tools.as_ref().is_some_and(|t| !t.is_empty());

    // Build tools with smart caching (cache last tool)
    let tools = build_tools_with_caching(
        &req.options.tools,
        &mut validator,
        cache_config
            .as_ref()
            .is_some_and(|c| c.cache_tools && has_tools),
    )?;

    // Extract and convert system messages with smart caching
    let system = build_system_content_with_caching(
        &req.messages,
        &config.auth,
        &mut validator,
        cache_config.as_ref().is_some_and(|c| c.cache_system),
    )?;

    // Calculate remaining budget for tail messages
    let tail_budget = cache_config.as_ref().map_or(0, |c| {
        let used = validator.breakpoint_count();
        let max = 4usize; // Anthropic limit
        let remaining = max.saturating_sub(used);
        c.tail_message_count.min(remaining)
    });

    // Convert non-system messages with smart tail caching
    let messages = build_messages_with_caching(&req.messages, &mut validator, tail_budget)?;

    // Determine max_tokens (required by Anthropic!)
    let max_tokens = req
        .options
        .max_tokens
        .unwrap_or_else(|| infer_max_tokens(&req.model.id));

    // Convert tool_choice to Anthropic format
    let tool_choice = req.options.tool_choice.as_ref().map(|choice| match choice {
        crate::types::ToolChoice::Auto => json!({"type": "auto"}),
        crate::types::ToolChoice::None => json!({"type": "none"}),
        crate::types::ToolChoice::Required { name } => json!({
            "type": "tool",
            "name": name
        }),
    });

    let is_opus_47 = is_opus_4_7_or_later(&req.model.id);

    let thinking = req.provider_options.as_ref().and_then(|opts| {
        if let crate::types::ProviderOptions::Anthropic(anthropic) = opts {
            anthropic.thinking.as_ref().map(|t| {
                if is_opus_47 {
                    AnthropicThinking {
                        type_: "adaptive".to_string(),
                        budget_tokens: None,
                    }
                } else {
                    AnthropicThinking {
                        type_: "enabled".to_string(),
                        budget_tokens: Some(t.budget_tokens.max(1024)),
                    }
                }
            })
        } else {
            None
        }
    });

    let has_cache_control = validator.breakpoint_count() > 0;
    let mut warnings = validator.take_warnings();

    // top_k is already None at the struct level; only cover temperature/top_p on input.
    let (temperature, top_p) = if is_opus_47 {
        if req.options.temperature.is_some() {
            warnings.push(opus_47_strip_warning("temperature"));
        }
        if req.options.top_p.is_some() {
            warnings.push(opus_47_strip_warning("top_p"));
        }
        (None, None)
    } else {
        (req.options.temperature, req.options.top_p)
    };

    if is_opus_47 && thinking.is_some() {
        warnings.push(opus_47_thinking_rewrite_warning());
    }

    Ok(AnthropicConversionResult {
        request: AnthropicRequest {
            model: req.model.id.clone(),
            messages,
            max_tokens,
            system,
            temperature,
            top_p,
            top_k: None,
            metadata: None,
            stop_sequences: req.options.stop_sequences.clone(),
            stream: if stream { Some(true) } else { None },
            thinking,
            tools,
            tool_choice,
        },
        warnings,
        has_cache_control,
    })
}

fn opus_47_strip_warning(param: &str) -> CacheWarning {
    CacheWarning::new(
        CacheWarningType::UnsupportedContext,
        format!(
            "Claude Opus 4.7 removed the `{}` sampling parameter; it was dropped from the outgoing request.",
            param
        ),
    )
}

fn opus_47_thinking_rewrite_warning() -> CacheWarning {
    CacheWarning::new(
        CacheWarningType::UnsupportedContext,
        "Claude Opus 4.7 removed `thinking.budget_tokens`; request rewritten to `thinking: {type: \"adaptive\"}`."
            .to_string(),
    )
}

/// Build system content with smart caching and OAuth handling
///
/// When `auto_cache_last` is true, the last system block gets a cache breakpoint.
/// This caches ALL system messages (Anthropic caches the full prefix up to the breakpoint).
fn build_system_content_with_caching(
    messages: &[Message],
    auth: &AnthropicAuth,
    validator: &mut CacheControlValidator,
    auto_cache_last: bool,
) -> Result<Option<AnthropicSystemContent>> {
    let system_messages: Vec<&Message> =
        messages.iter().filter(|m| m.role == Role::System).collect();

    // For OAuth, we need the Claude Code prefix
    let is_oauth = matches!(auth, AnthropicAuth::OAuth { .. });

    if system_messages.is_empty() && !is_oauth {
        return Ok(None);
    }

    // Check if any system message has explicit cache control
    let has_explicit_cache = system_messages.iter().any(|m| m.cache_control().is_some());

    // Determine if we should use blocks format
    let use_blocks = is_oauth || has_explicit_cache || auto_cache_last;

    // For OAuth, always use blocks format with Claude Code prefix
    if is_oauth {
        let mut blocks = vec![];

        // Add Claude Code prefix with 1-hour cache
        blocks.push(AnthropicSystemBlock {
            type_: "text".to_string(),
            text: CLAUDE_CODE_SYSTEM_PREFIX.to_string(),
            cache_control: Some(AnthropicCacheControl::ephemeral_with_ttl("1h")),
        });
        // Count this as a cache breakpoint
        validator.validate(
            Some(&crate::types::CacheControl::ephemeral_with_ttl("1h")),
            CacheContext::system_message(),
        );

        // Add user system messages
        let msg_count = system_messages.len();
        for (i, msg) in system_messages.iter().enumerate() {
            if let Some(text) = msg.text() {
                let is_last = i == msg_count - 1;

                // Use explicit cache or auto-cache last with 1-hour TTL
                let cache_control = msg.cache_control().cloned().or_else(|| {
                    if is_last && auto_cache_last {
                        Some(crate::types::CacheControl::ephemeral_with_ttl("1h"))
                    } else {
                        None
                    }
                });

                let validated_cache =
                    validator.validate(cache_control.as_ref(), CacheContext::system_message());

                blocks.push(AnthropicSystemBlock {
                    type_: "text".to_string(),
                    text,
                    cache_control: validated_cache.map(|c| AnthropicCacheControl::from(&c)),
                });
            }
        }

        return Ok(Some(AnthropicSystemContent::Blocks(blocks)));
    }

    // For API key auth without any caching, use simple string format
    if !use_blocks {
        let combined = system_messages
            .iter()
            .filter_map(|m| m.text())
            .collect::<Vec<_>>()
            .join("\n\n");
        return Ok(Some(AnthropicSystemContent::String(combined)));
    }

    // Complex case: caching needed, use blocks format
    let msg_count = system_messages.len();
    let blocks: Vec<AnthropicSystemBlock> = system_messages
        .iter()
        .enumerate()
        .filter_map(|(i, msg)| {
            let text = msg.text()?;
            let is_last = i == msg_count - 1;

            // Use explicit cache or auto-cache last with 1-hour TTL
            let cache_control = msg.cache_control().cloned().or_else(|| {
                if is_last && auto_cache_last {
                    Some(crate::types::CacheControl::ephemeral_with_ttl("1h"))
                } else {
                    None
                }
            });

            let validated_cache =
                validator.validate(cache_control.as_ref(), CacheContext::system_message());

            Some(AnthropicSystemBlock {
                type_: "text".to_string(),
                text,
                cache_control: validated_cache.map(|c| AnthropicCacheControl::from(&c)),
            })
        })
        .collect();

    if blocks.is_empty() {
        Ok(None)
    } else {
        Ok(Some(AnthropicSystemContent::Blocks(blocks)))
    }
}

/// Build tools with smart caching on the last tool
///
/// When `auto_cache_last` is true, the last tool gets a cache breakpoint.
/// This caches ALL tools as a group (Anthropic caches the full prefix).
fn build_tools_with_caching(
    tools: &Option<Vec<crate::types::Tool>>,
    validator: &mut CacheControlValidator,
    auto_cache_last: bool,
) -> Result<Option<Vec<serde_json::Value>>> {
    let tools = match tools {
        Some(t) if !t.is_empty() => t,
        _ => return Ok(None),
    };

    let len = tools.len();
    let converted: Vec<serde_json::Value> = tools
        .iter()
        .enumerate()
        .map(|(i, tool)| {
            let is_last = i == len - 1;

            // Use explicit cache_control if set, otherwise auto-cache last tool with 1h TTL
            let cache_control = tool.cache_control().cloned().or_else(|| {
                if is_last && auto_cache_last {
                    Some(crate::types::CacheControl::ephemeral_with_ttl("1h"))
                } else {
                    None
                }
            });

            let validated_cache =
                validator.validate(cache_control.as_ref(), CacheContext::tool_definition());

            let mut tool_json = json!({
                "name": tool.function.name,
                "description": tool.function.description,
                "input_schema": tool.function.parameters,
            });

            if let Some(cache) = validated_cache {
                tool_json["cache_control"] = json!(AnthropicCacheControl::from(&cache));
            }

            tool_json
        })
        .collect();

    Ok(Some(converted))
}

/// Build messages with smart tail caching
///
/// Caches the last N non-system messages to maximize cache hits
/// on subsequent requests in a conversation.
///
/// Tail caching runs **last** — after all structural mutations (merging,
/// per-message sanitization, and sequence-level sanitization) are complete.
/// This guarantees cache breakpoints land on the final stable message
/// boundaries, preventing stale breakpoints from messages that get
/// inserted, removed, or re-merged by sanitization phases.
fn build_messages_with_caching(
    messages: &[Message],
    validator: &mut CacheControlValidator,
    tail_count: usize,
) -> Result<Vec<AnthropicMessage>> {
    let non_system: Vec<&Message> = messages.iter().filter(|m| m.role != Role::System).collect();

    // Phase 1: Convert each message individually (no auto-caching yet)
    let converted: Vec<AnthropicMessage> = non_system
        .iter()
        .map(|msg| to_anthropic_message_with_caching(msg, validator, false))
        .collect::<Result<Vec<_>>>()?;

    // Phase 2: Merge consecutive same-role messages
    let mut merged = merge_consecutive_messages(converted);

    // Phase 3: Sanitize individual messages to enforce per-message constraints.
    // Runs before sequence sanitization so that empty text blocks are removed
    // before tool-pairing logic inspects message content.
    for msg in &mut merged {
        sanitize_anthropic_message(msg);
    }

    // Phase 4: Enforce message-sequence-level Anthropic constraints.
    // This handles structural invariants that span multiple messages:
    // - Every tool_use must have a matching tool_result in the next user message
    // - Orphan tool_results without matching tool_use are removed
    // - Conversation must start with a user message
    // - Conversation must not end with an assistant message (unless prefill-safe)
    //
    // This phase can insert, remove, and re-merge messages, so caching
    // must run after it to avoid stale breakpoint placement.
    sanitize_message_sequence(&mut merged);

    // Phase 5: Apply tail caching to the last N messages of the *final* array.
    // Running after all mutations ensures breakpoints land on stable positions
    // and won't be shifted by later inserts/removes/re-merges.
    if tail_count > 0 {
        let len = merged.len();
        let cache_start = len.saturating_sub(tail_count);
        for msg in &mut merged[cache_start..] {
            if !is_empty_content_message(msg) {
                apply_tail_cache_to_message(msg, validator);
            }
        }
    }

    Ok(merged)
}

/// Apply ephemeral cache control to the last content block of a message.
///
/// Used for tail-caching after message merging to ensure cache breakpoints
/// land on the actual last block of each merged message.
fn apply_tail_cache_to_message(msg: &mut AnthropicMessage, validator: &mut CacheControlValidator) {
    let cache = crate::types::CacheControl::ephemeral();
    let context = if msg.role == "assistant" {
        CacheContext::assistant_message_part()
    } else {
        CacheContext::user_message_part()
    };

    let Some(validated_cache) = validator.validate(Some(&cache), context) else {
        return; // Breakpoint limit exceeded
    };

    let anthropic_cc = AnthropicCacheControl::from(&validated_cache);
    match &mut msg.content {
        AnthropicMessageContent::Blocks(blocks) => {
            if let Some(last) = blocks.last_mut() {
                set_block_cache_control(last, Some(anthropic_cc));
            }
        }
        AnthropicMessageContent::String(s) => {
            // Convert to blocks format to attach cache control
            msg.content = AnthropicMessageContent::Blocks(vec![AnthropicContent::Text {
                text: std::mem::take(s),
                cache_control: Some(anthropic_cc),
            }]);
        }
    }
}

/// Returns true if the message contains only empty text content (no cacheable substance).
///
/// Used to skip tail-caching on messages that would waste a cache breakpoint,
/// since Phase 4 would strip the `cache_control` from empty text blocks anyway.
fn is_empty_content_message(msg: &AnthropicMessage) -> bool {
    match &msg.content {
        AnthropicMessageContent::String(s) => s.is_empty(),
        AnthropicMessageContent::Blocks(blocks) => blocks
            .iter()
            .all(|b| matches!(b, AnthropicContent::Text { text, .. } if text.is_empty())),
    }
}

/// Sanitize an Anthropic message to enforce per-message API constraints.
///
/// This is the **single boundary** that fixes structural issues before the
/// message is sent to the API. All Anthropic-specific content invariants
/// are enforced here, rather than scattering guards across conversion,
/// merging, and caching phases.
///
/// Rules (validated against live API + informed by Vercel AI SDK / OpenCode):
/// - Strip empty text blocks from blocks content
///   (prevents "all messages must have non-empty content" when only empty text remains)
/// - Strip `cache_control` from any remaining empty text blocks
///   (Anthropic rejects: "cache_control cannot be set for empty text blocks")
fn sanitize_anthropic_message(msg: &mut AnthropicMessage) {
    match &mut msg.content {
        AnthropicMessageContent::Blocks(blocks) => {
            // Remove empty text blocks entirely (OpenCode pattern: filter empty text/reasoning).
            // Keep non-text blocks (tool_result, tool_use, image) and non-empty text.
            blocks.retain(
                |block| !matches!(block, AnthropicContent::Text { text, .. } if text.is_empty()),
            );

            // Safety: strip cache_control from any remaining empty text blocks
            // (e.g., if a block somehow slipped through)
            for block in blocks.iter_mut() {
                if let AnthropicContent::Text {
                    text,
                    cache_control,
                } = block
                    && text.is_empty()
                    && cache_control.is_some()
                {
                    *cache_control = None;
                }
            }
        }
        AnthropicMessageContent::String(_) => {
            // String content has no cache_control field; nothing to sanitize.
        }
    }
}

/// Enforce Anthropic message-sequence-level constraints on the complete array.
///
/// This runs as the final phase after conversion, merging, and caching.
/// It handles structural invariants that span multiple messages.
///
/// Constraints enforced (validated against live Anthropic API 2025-02):
///
/// 1. Every `tool_use` must have exactly one `tool_result` in the immediately
///    following user message (adds placeholders for missing ones)
/// 2. Orphan `tool_result` blocks (not referencing any `tool_use` in the
///    immediately preceding assistant message) are removed
/// 3. No duplicate `tool_result` blocks for the same `tool_use_id`
///    (Anthropic rejects: "each tool_use must have a single result")
/// 4. No empty-content messages — empty string or empty blocks array
///    (Anthropic rejects: "all messages must have non-empty content")
/// 5. Conversation must start with role="user"
/// 6. Conversation must not end with role="assistant" (no prefill — some
///    models reject it; defensive for cross-model compatibility)
/// 7. Re-merges consecutive same-role messages after mutations
fn sanitize_message_sequence(messages: &mut Vec<AnthropicMessage>) {
    if messages.is_empty() {
        return;
    }

    // Step 1: Ensure every tool_use has a matching tool_result.
    patch_tool_result_coverage(messages);

    // Step 2: Remove orphan tool_results that don't match any tool_use
    // in the immediately preceding assistant message.
    remove_orphan_tool_results(messages);

    // Step 3: Deduplicate tool_results — keep only the last result per tool_use_id.
    // Anthropic rejects: "each tool_use must have a single result. Found multiple
    // `tool_result` blocks with id: <id>"
    dedup_tool_results(messages);

    // Step 4: Remove messages with empty content (empty string or empty blocks).
    // Anthropic rejects: "all messages must have non-empty content except for
    // the optional final assistant message"
    remove_empty_content_messages(messages);

    // Step 5: Re-merge consecutive same-role messages that may have been
    // introduced by insertions/removals in steps 1-4.
    let re_merged = merge_consecutive_messages(std::mem::take(messages));
    *messages = re_merged;

    // Step 6: Ensure the first message is role="user".
    if messages.first().is_some_and(|m| m.role != "user") {
        messages.insert(
            0,
            AnthropicMessage {
                role: "user".to_string(),
                content: AnthropicMessageContent::String(".".to_string()),
            },
        );
    }

    // Step 7: Ensure the conversation does not end with an assistant message.
    ensure_not_trailing_assistant(messages);
}

/// Ensure every `tool_use` in assistant messages has a matching `tool_result`
/// in the immediately following user message.
///
/// If the next message is a user message with missing tool_results, placeholder
/// results are injected. If no user message follows, a new one is inserted.
fn patch_tool_result_coverage(messages: &mut Vec<AnthropicMessage>) {
    let mut i = 0;
    while i < messages.len() {
        if messages[i].role != "assistant" {
            i += 1;
            continue;
        }

        let tool_use_ids = extract_tool_use_ids(&messages[i]);
        if tool_use_ids.is_empty() {
            i += 1;
            continue;
        }

        let next_is_user = messages.get(i + 1).is_some_and(|m| m.role == "user");
        if next_is_user {
            // Check which tool_use IDs are already covered
            let covered_ids = extract_tool_result_ids(&messages[i + 1]);
            let missing: Vec<String> = tool_use_ids
                .into_iter()
                .filter(|id| !covered_ids.contains(id))
                .collect();

            if !missing.is_empty() {
                inject_placeholder_tool_results(&mut messages[i + 1], &missing);
            }
        } else {
            // No user message follows — insert one with all tool_results
            let tool_results: Vec<AnthropicContent> = tool_use_ids
                .into_iter()
                .map(|id| AnthropicContent::ToolResult {
                    tool_use_id: id,
                    content: Some(AnthropicMessageContent::String(
                        "[Tool call not executed]".to_string(),
                    )),
                    is_error: Some(true),
                    cache_control: None,
                })
                .collect();
            messages.insert(
                i + 1,
                AnthropicMessage {
                    role: "user".to_string(),
                    content: AnthropicMessageContent::Blocks(tool_results),
                },
            );
        }

        // Skip over the assistant + user pair
        i += 2;
    }
}

/// Remove `tool_result` blocks from user messages that don't match any
/// `tool_use` in the immediately preceding assistant message.
///
/// Also removes user messages that become empty after orphan removal.
fn remove_orphan_tool_results(messages: &mut Vec<AnthropicMessage>) {
    let mut i = 0;
    while i < messages.len() {
        if messages[i].role != "user" {
            i += 1;
            continue;
        }

        // Collect valid tool_use IDs from the immediately preceding assistant message
        let valid_ids: HashSet<String> = if i > 0 && messages[i - 1].role == "assistant" {
            extract_tool_use_ids(&messages[i - 1]).into_iter().collect()
        } else {
            HashSet::new()
        };

        if let AnthropicMessageContent::Blocks(blocks) = &mut messages[i].content {
            let had_tool_results = blocks
                .iter()
                .any(|b| matches!(b, AnthropicContent::ToolResult { .. }));

            if had_tool_results {
                blocks.retain(|block| match block {
                    AnthropicContent::ToolResult { tool_use_id, .. } => {
                        valid_ids.contains(tool_use_id)
                    }
                    _ => true,
                });
            }

            // If all blocks were removed, drop the message entirely
            if blocks.is_empty() {
                messages.remove(i);
                continue; // Don't increment — next message shifted into position i
            }
        }

        i += 1;
    }
}

/// Deduplicate `tool_result` blocks within user messages.
///
/// Anthropic rejects: "each tool_use must have a single result. Found multiple
/// `tool_result` blocks with id: <id>". When duplicates exist (e.g., from
/// retry flows or checkpoint corruption), keep only the **last** result per
/// `tool_use_id`.
fn dedup_tool_results(messages: &mut [AnthropicMessage]) {
    for msg in messages.iter_mut() {
        if msg.role != "user" {
            continue;
        }

        if let AnthropicMessageContent::Blocks(blocks) = &mut msg.content {
            let has_tool_results = blocks
                .iter()
                .any(|b| matches!(b, AnthropicContent::ToolResult { .. }));

            if !has_tool_results {
                continue;
            }

            // Find the last occurrence index for each tool_use_id
            let mut last_index: std::collections::HashMap<String, usize> =
                std::collections::HashMap::new();
            for (i, block) in blocks.iter().enumerate() {
                if let AnthropicContent::ToolResult { tool_use_id, .. } = block {
                    last_index.insert(tool_use_id.clone(), i);
                }
            }

            // Retain non-tool-result blocks and only the last tool_result per ID
            let mut i = 0;
            blocks.retain(|block| {
                let keep = match block {
                    AnthropicContent::ToolResult { tool_use_id, .. } => {
                        last_index.get(tool_use_id) == Some(&i)
                    }
                    _ => true,
                };
                i += 1;
                keep
            });
        }
    }
}

/// Remove messages with empty content.
///
/// Anthropic rejects: "all messages must have non-empty content except for
/// the optional final assistant message". This covers:
/// - Empty string content (`""`)
/// - Empty blocks array (`[]`)
fn remove_empty_content_messages(messages: &mut Vec<AnthropicMessage>) {
    messages.retain(|msg| match &msg.content {
        AnthropicMessageContent::String(s) => !s.is_empty(),
        AnthropicMessageContent::Blocks(blocks) => !blocks.is_empty(),
    });
}

/// Ensure the conversation does not end with an assistant message that would
/// cause API errors.
///
/// Handling by case:
/// - **tool_use blocks present**: append a user message with placeholder
///   `tool_result` blocks (API requires every tool_use to have a result).
/// - **Empty or whitespace-only text**: remove the trailing assistant
///   (Anthropic rejects trailing whitespace-only assistant content, and
///   empty responses indicate incomplete/dangling state).
/// - **Substantive text content**: preserve it as-is. The Anthropic API
///   accepts trailing assistant messages as "prefill" for continuation on
///   models that support it (Claude Sonnet 4, Opus 4, etc.). Removing
///   valid context would lose information from checkpoints and context
///   managers that legitimately produce this state.
fn ensure_not_trailing_assistant(messages: &mut Vec<AnthropicMessage>) {
    // Loop in case removing an assistant reveals another trailing assistant.
    while messages.last().is_some_and(|m| m.role == "assistant") {
        let last = messages.last().expect("checked above");
        let tool_use_ids = extract_tool_use_ids(last);

        if !tool_use_ids.is_empty() {
            // Has tool_use — add user message with placeholder tool_results
            let tool_results: Vec<AnthropicContent> = tool_use_ids
                .into_iter()
                .map(|id| AnthropicContent::ToolResult {
                    tool_use_id: id,
                    content: Some(AnthropicMessageContent::String(
                        "[Tool call interrupted]".to_string(),
                    )),
                    is_error: Some(true),
                    cache_control: None,
                })
                .collect();
            messages.push(AnthropicMessage {
                role: "user".to_string(),
                content: AnthropicMessageContent::Blocks(tool_results),
            });
            break; // We added a user message, so we're done
        }

        // No tool_use — check if content is substantive (worth keeping as prefill)
        let is_substantive = match &last.content {
            AnthropicMessageContent::String(s) => !s.trim().is_empty(),
            AnthropicMessageContent::Blocks(blocks) => blocks.iter().any(|b| match b {
                AnthropicContent::Text { text, .. } => !text.trim().is_empty(),
                // Non-text blocks (images, thinking) count as substantive
                AnthropicContent::Image { .. }
                | AnthropicContent::Thinking { .. }
                | AnthropicContent::RedactedThinking { .. } => true,
                // tool_use handled above; tool_result in assistant is unusual
                _ => false,
            }),
        };

        if is_substantive {
            // Preserve trailing assistant with real content (API accepts prefill)
            break;
        }

        // Empty/whitespace-only — discard (dangling/incomplete response)
        messages.pop();
    }
}

/// Extract all `tool_use` IDs from an Anthropic message's content blocks.
fn extract_tool_use_ids(msg: &AnthropicMessage) -> Vec<String> {
    match &msg.content {
        AnthropicMessageContent::Blocks(blocks) => blocks
            .iter()
            .filter_map(|b| {
                if let AnthropicContent::ToolUse { id, .. } = b {
                    Some(id.clone())
                } else {
                    None
                }
            })
            .collect(),
        _ => vec![],
    }
}

/// Extract all `tool_result` tool_use_ids from an Anthropic message's content blocks.
fn extract_tool_result_ids(msg: &AnthropicMessage) -> HashSet<String> {
    match &msg.content {
        AnthropicMessageContent::Blocks(blocks) => blocks
            .iter()
            .filter_map(|b| {
                if let AnthropicContent::ToolResult { tool_use_id, .. } = b {
                    Some(tool_use_id.clone())
                } else {
                    None
                }
            })
            .collect(),
        _ => HashSet::new(),
    }
}

/// Inject placeholder `tool_result` blocks for missing tool_use IDs into a user message.
fn inject_placeholder_tool_results(msg: &mut AnthropicMessage, missing_ids: &[String]) {
    let new_blocks: Vec<AnthropicContent> = missing_ids
        .iter()
        .map(|id| AnthropicContent::ToolResult {
            tool_use_id: id.clone(),
            content: Some(AnthropicMessageContent::String(
                "[Tool call not executed]".to_string(),
            )),
            is_error: Some(true),
            cache_control: None,
        })
        .collect();

    match &mut msg.content {
        AnthropicMessageContent::Blocks(blocks) => {
            blocks.extend(new_blocks);
        }
        AnthropicMessageContent::String(s) => {
            // Convert String content to Blocks. Skip creating an empty text block
            // from String("") — this avoids reintroducing empty text blocks that
            // per-message sanitization already stripped.
            let mut blocks = Vec::new();
            if !s.is_empty() {
                blocks.push(AnthropicContent::Text {
                    text: std::mem::take(s),
                    cache_control: None,
                });
            }
            blocks.extend(new_blocks);
            msg.content = AnthropicMessageContent::Blocks(blocks);
        }
    }
}

/// Set cache_control on an AnthropicContent block.
fn set_block_cache_control(block: &mut AnthropicContent, cc: Option<AnthropicCacheControl>) {
    match block {
        AnthropicContent::Text { cache_control, .. }
        | AnthropicContent::ToolUse { cache_control, .. }
        | AnthropicContent::ToolResult { cache_control, .. }
        | AnthropicContent::Image { cache_control, .. } => *cache_control = cc,
        AnthropicContent::Thinking { .. } | AnthropicContent::RedactedThinking { .. } => {
            // Thinking blocks don't support cache_control
        }
    }
}

/// Merge consecutive messages with the same role into single messages.
///
/// Anthropic requires that tool_result blocks appear in a single user message
/// immediately after the assistant message containing the matching tool_use blocks.
/// When multiple tool results are converted individually, each becomes a separate
/// "user" message. This function combines them (and any other consecutive same-role
/// messages) into one.
fn merge_consecutive_messages(messages: Vec<AnthropicMessage>) -> Vec<AnthropicMessage> {
    if messages.is_empty() {
        return messages;
    }

    let mut result: Vec<AnthropicMessage> = Vec::with_capacity(messages.len());

    for msg in messages {
        let should_merge = result.last().is_some_and(|last| last.role == msg.role);

        if should_merge {
            let Some(last) = result.last_mut() else {
                // unreachable: guarded by is_some_and check above
                result.push(msg);
                continue;
            };
            let prev = std::mem::take(&mut last.content);
            last.content = merge_content(prev, msg.content);
        } else {
            result.push(msg);
        }
    }

    result
}

/// Convert AnthropicMessageContent to a Vec<AnthropicContent> blocks.
fn content_to_blocks(content: AnthropicMessageContent) -> Vec<AnthropicContent> {
    match content {
        AnthropicMessageContent::Blocks(blocks) => blocks,
        AnthropicMessageContent::String(s) => {
            vec![AnthropicContent::Text {
                text: s,
                cache_control: None,
            }]
        }
    }
}

/// Merge two AnthropicMessageContent values into one Blocks variant.
fn merge_content(
    a: AnthropicMessageContent,
    b: AnthropicMessageContent,
) -> AnthropicMessageContent {
    let mut blocks = content_to_blocks(a);
    blocks.extend(content_to_blocks(b));
    AnthropicMessageContent::Blocks(blocks)
}

/// Convert unified message to Anthropic message with optional auto-caching
fn to_anthropic_message_with_caching(
    msg: &Message,
    validator: &mut CacheControlValidator,
    auto_cache: bool,
) -> Result<AnthropicMessage> {
    // Determine the Anthropic role - Tool messages become "user" with tool_result content
    // (Anthropic doesn't support role="tool" like OpenAI)
    let role = match msg.role {
        Role::User => "user",
        Role::Assistant => "assistant",
        Role::Tool => "user", // Anthropic expects tool results as user messages
        Role::System => {
            return Err(Error::invalid_response(
                "System messages should be filtered out",
            ));
        }
    };

    // Get the message-level cache control, or use auto-cache
    let msg_cache_control = msg.cache_control().cloned().or_else(|| {
        if auto_cache {
            Some(crate::types::CacheControl::ephemeral())
        } else {
            None
        }
    });

    // Convert content parts
    let parts = msg.parts();

    // Check if any part has cache control, or if message has cache control
    let has_cache_control =
        msg_cache_control.is_some() || parts.iter().any(|p| p.cache_control().is_some());

    // Tool messages always use blocks format (tool_result content blocks)
    let force_blocks = msg.role == Role::Tool;

    let content = if parts.len() == 1 && !has_cache_control && !force_blocks {
        // Single content without cache control - try to use simple string format if text
        match &parts[0] {
            ContentPart::Text { text, .. } => AnthropicMessageContent::String(text.clone()),
            _ => AnthropicMessageContent::Blocks(vec![to_anthropic_content_part(
                &parts[0], None, validator, true,
            )?]),
        }
    } else {
        // Multiple content parts, has cache control, or tool message - use array format
        let num_parts = parts.len();
        let content_parts = parts
            .iter()
            .enumerate()
            .map(|(i, part)| {
                let is_last = i == num_parts - 1;
                // For the last part, include message-level cache control as fallback
                let fallback_cache = if is_last {
                    msg_cache_control.as_ref()
                } else {
                    None
                };
                to_anthropic_content_part(part, fallback_cache, validator, is_last)
            })
            .collect::<Result<Vec<_>>>()?;

        AnthropicMessageContent::Blocks(content_parts)
    };

    Ok(AnthropicMessage {
        role: role.to_string(),
        content,
    })
}

/// Convert a single message to Anthropic format (test helper, no auto-caching)
#[cfg(test)]
fn to_anthropic_message(
    msg: &Message,
    validator: &mut CacheControlValidator,
) -> Result<AnthropicMessage> {
    to_anthropic_message_with_caching(msg, validator, false)
}

/// Convert a content part to Anthropic format with cache control
fn to_anthropic_content_part(
    part: &ContentPart,
    fallback_cache: Option<&crate::types::CacheControl>,
    validator: &mut CacheControlValidator,
    is_last_part: bool,
) -> Result<AnthropicContent> {
    // Get the part-level cache control, with fallback to message-level for last part
    let part_cache = part.cache_control();
    let effective_cache = if part_cache.is_some() {
        part_cache
    } else if is_last_part {
        fallback_cache
    } else {
        None
    };

    match part {
        ContentPart::Text { text, .. } => {
            let context = CacheContext::user_message_part();
            let validated_cache = validator.validate(effective_cache, context);

            Ok(AnthropicContent::Text {
                text: text.clone(),
                cache_control: validated_cache.map(|c| AnthropicCacheControl::from(&c)),
            })
        }
        ContentPart::Image { url, .. } => {
            let context = CacheContext::image_content();
            let validated_cache = validator.validate(effective_cache, context);

            Ok(AnthropicContent::Image {
                source: parse_image_source(url)?,
                cache_control: validated_cache.map(|c| AnthropicCacheControl::from(&c)),
            })
        }
        ContentPart::ToolCall {
            id,
            name,
            arguments,
            ..
        } => {
            let context = CacheContext::assistant_message_part();
            let validated_cache = validator.validate(effective_cache, context);

            Ok(AnthropicContent::ToolUse {
                id: id.clone(),
                name: name.clone(),
                input: arguments.clone(),
                cache_control: validated_cache.map(|c| AnthropicCacheControl::from(&c)),
            })
        }
        ContentPart::ToolResult {
            tool_call_id,
            content,
            ..
        } => {
            let context = CacheContext::tool_result();
            let validated_cache = validator.validate(effective_cache, context);

            Ok(AnthropicContent::ToolResult {
                tool_use_id: tool_call_id.clone(),
                content: Some(AnthropicMessageContent::String(content.to_string())),
                is_error: None,
                cache_control: validated_cache.map(|c| AnthropicCacheControl::from(&c)),
            })
        }
    }
}

/// Parse image URL to Anthropic image source format
fn parse_image_source(url: &str) -> Result<AnthropicSource> {
    if url.starts_with("data:") {
        // Data URL format: data:image/png;base64,iVBORw0KG...
        let parts: Vec<&str> = url.splitn(2, ',').collect();
        if parts.len() != 2 {
            return Err(Error::invalid_response("Invalid data URL format"));
        }

        let media_type = parts[0]
            .strip_prefix("data:")
            .and_then(|s| s.strip_suffix(";base64"))
            .ok_or_else(|| Error::invalid_response("Invalid data URL media type"))?;

        Ok(AnthropicSource {
            type_: "base64".to_string(),
            media_type: media_type.to_string(),
            data: parts[1].to_string(),
        })
    } else {
        // URL format (Anthropic doesn't support direct URLs, would need to fetch)
        Err(Error::invalid_response(
            "Anthropic requires base64-encoded images, not URLs",
        ))
    }
}

/// Convert Anthropic response to unified response with warnings from conversion
pub fn from_anthropic_response_with_warnings(
    resp: AnthropicResponse,
    warnings: Vec<CacheWarning>,
) -> Result<GenerateResponse> {
    use crate::types::{ResponseWarning, ToolCall};

    let content: Vec<ResponseContent> = resp
        .content
        .iter()
        .filter_map(|c| match c {
            AnthropicContent::Text { text, .. } => {
                Some(ResponseContent::Text { text: text.clone() })
            }
            AnthropicContent::Thinking { thinking, .. } => Some(ResponseContent::Reasoning {
                reasoning: thinking.clone(),
            }),
            AnthropicContent::ToolUse {
                id, name, input, ..
            } => Some(ResponseContent::ToolCall(ToolCall {
                id: id.clone(),
                name: name.clone(),
                arguments: input.clone(),
                metadata: None,
            })),
            _ => None,
        })
        .collect();

    if content.is_empty() {
        return Err(Error::invalid_response("No content in response"));
    }

    // Determine finish reason - tool_use should be ToolCalls
    let finish_reason = if content
        .iter()
        .any(|c| matches!(c, ResponseContent::ToolCall(_)))
    {
        FinishReason::with_raw(FinishReasonKind::ToolCalls, "tool_use")
    } else {
        parse_stop_reason(&resp.stop_reason)
    };

    // Calculate cache tokens
    // Anthropic token breakdown (per official API docs):
    // - input_tokens: tokens NOT read from or written to cache (non-cached input)
    // - cache_creation_input_tokens: tokens written to cache (cache miss, creating entry)
    // - cache_read_input_tokens: tokens read from cache (cache hit)
    // Total input = non-cached + cache-write + cache-read
    let cache_creation = resp.usage.cache_creation_input_tokens.unwrap_or(0);
    let cache_read = resp.usage.cache_read_input_tokens.unwrap_or(0);
    let input_tokens = resp.usage.input_tokens;
    let output_tokens = resp.usage.output_tokens;

    let total_input = input_tokens + cache_creation + cache_read;

    let usage = Usage::with_details(
        InputTokenDetails {
            total: Some(total_input),
            no_cache: Some(input_tokens),
            cache_read: if cache_read > 0 {
                Some(cache_read)
            } else {
                None
            },
            cache_write: if cache_creation > 0 {
                Some(cache_creation)
            } else {
                None
            },
        },
        OutputTokenDetails {
            total: Some(output_tokens),
            text: None,      // Anthropic doesn't break down output tokens
            reasoning: None, // Will be populated if extended thinking is used
        },
        Some(serde_json::to_value(&resp.usage).unwrap_or_default()),
    );

    // Convert cache warnings to response warnings
    let response_warnings: Option<Vec<ResponseWarning>> = if warnings.is_empty() {
        None
    } else {
        Some(warnings.into_iter().map(ResponseWarning::from).collect())
    };

    Ok(GenerateResponse {
        content,
        usage,
        finish_reason,
        metadata: Some(json!({
            "id": resp.id,
            "model": resp.model,
        })),
        warnings: response_warnings,
    })
}

/// Parse Anthropic stop reason to unified finish reason
fn parse_stop_reason(reason: &Option<String>) -> FinishReason {
    match reason.as_deref() {
        Some("end_turn") => FinishReason::with_raw(FinishReasonKind::Stop, "end_turn"),
        Some("max_tokens") => FinishReason::with_raw(FinishReasonKind::Length, "max_tokens"),
        Some("stop_sequence") => FinishReason::with_raw(FinishReasonKind::Stop, "stop_sequence"),
        Some("tool_use") => FinishReason::with_raw(FinishReasonKind::ToolCalls, "tool_use"),
        Some(raw) => FinishReason::with_raw(FinishReasonKind::Other, raw),
        None => FinishReason::other(),
    }
}

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

    #[test]
    fn test_infer_max_tokens() {
        assert_eq!(infer_max_tokens("claude-opus-4-5"), 64000);
        assert_eq!(infer_max_tokens("claude-sonnet-4"), 64000);
        assert_eq!(infer_max_tokens("claude-opus-4"), 32000);
        assert_eq!(infer_max_tokens("claude-3-5-sonnet"), 8192);
        assert_eq!(infer_max_tokens("claude-3-opus"), 4096);
    }

    #[test]
    fn test_parse_image_source() {
        let data_url = "data:image/png;base64,iVBORw0KGgoAAAANS";
        let result = parse_image_source(data_url).unwrap();

        assert_eq!(result.type_, "base64");
        assert_eq!(result.media_type, "image/png");
        assert_eq!(result.data, "iVBORw0KGgoAAAANS");
    }

    #[test]
    fn test_tool_role_message_converted_to_user_with_tool_result() {
        // Test that Role::Tool messages are converted to Anthropic's expected format:
        // role="user" with tool_result content blocks
        // This is critical for Anthropic compatibility - they don't support role="tool"
        let mut validator = CacheControlValidator::new();

        let tool_msg = Message {
            role: Role::Tool,
            content: MessageContent::Parts(vec![ContentPart::ToolResult {
                tool_call_id: "toolu_01Abc123".to_string(),
                content: serde_json::json!("Tool execution result"),
                provider_options: None,
            }]),
            name: None,
            provider_options: None,
        };

        let result = to_anthropic_message(&tool_msg, &mut validator).unwrap();

        // Role should be converted to "user" for Anthropic
        assert_eq!(
            result.role, "user",
            "Tool role should be converted to user for Anthropic"
        );

        // Content should contain tool_result block
        match result.content {
            AnthropicMessageContent::Blocks(blocks) => {
                assert_eq!(blocks.len(), 1, "Should have exactly one content block");
                match &blocks[0] {
                    AnthropicContent::ToolResult {
                        tool_use_id,
                        content,
                        ..
                    } => {
                        assert_eq!(tool_use_id, "toolu_01Abc123");
                        // Content should be the stringified JSON
                        match content {
                            Some(AnthropicMessageContent::String(s)) => {
                                assert_eq!(s, "\"Tool execution result\"");
                            }
                            _ => panic!("Expected string content in tool result"),
                        }
                    }
                    _ => panic!("Expected ToolResult content block, got {:?}", blocks[0]),
                }
            }
            _ => panic!("Expected Blocks content, got {:?}", result.content),
        }
    }

    #[test]
    fn test_tool_role_message_with_text_content() {
        // Test tool result with plain text content (common case)
        let mut validator = CacheControlValidator::new();

        let tool_msg = Message {
            role: Role::Tool,
            content: MessageContent::Parts(vec![ContentPart::ToolResult {
                tool_call_id: "toolu_02Xyz789".to_string(),
                content: serde_json::json!({"temperature": 22, "unit": "celsius"}),
                provider_options: None,
            }]),
            name: None,
            provider_options: None,
        };

        let result = to_anthropic_message(&tool_msg, &mut validator).unwrap();

        assert_eq!(result.role, "user");
        match result.content {
            AnthropicMessageContent::Blocks(blocks) => {
                assert_eq!(blocks.len(), 1);
                match &blocks[0] {
                    AnthropicContent::ToolResult {
                        tool_use_id,
                        content,
                        ..
                    } => {
                        assert_eq!(tool_use_id, "toolu_02Xyz789");
                        match content {
                            Some(AnthropicMessageContent::String(s)) => {
                                // JSON object should be stringified
                                assert!(s.contains("temperature"));
                                assert!(s.contains("22"));
                            }
                            _ => panic!("Expected string content"),
                        }
                    }
                    _ => panic!("Expected ToolResult"),
                }
            }
            _ => panic!("Expected Blocks"),
        }
    }

    #[test]
    fn test_assistant_message_not_affected_by_tool_conversion() {
        // Ensure assistant messages are not affected by the tool role handling
        let mut validator = CacheControlValidator::new();

        let assistant_msg = Message {
            role: Role::Assistant,
            content: MessageContent::Text("I'll help you with that.".to_string()),
            name: None,
            provider_options: None,
        };

        let result = to_anthropic_message(&assistant_msg, &mut validator).unwrap();

        assert_eq!(result.role, "assistant");
        match result.content {
            AnthropicMessageContent::String(s) => {
                assert_eq!(s, "I'll help you with that.");
            }
            _ => panic!("Expected string content for simple assistant message"),
        }
    }

    #[test]
    fn test_user_message_not_affected_by_tool_conversion() {
        // Ensure user messages are not affected by the tool role handling
        let mut validator = CacheControlValidator::new();

        let user_msg = Message {
            role: Role::User,
            content: MessageContent::Text("Hello!".to_string()),
            name: None,
            provider_options: None,
        };

        let result = to_anthropic_message(&user_msg, &mut validator).unwrap();

        assert_eq!(result.role, "user");
        match result.content {
            AnthropicMessageContent::String(s) => {
                assert_eq!(s, "Hello!");
            }
            _ => panic!("Expected string content for simple user message"),
        }
    }

    // --- merge_consecutive_messages tests ---

    fn user_msg(text: &str) -> AnthropicMessage {
        AnthropicMessage {
            role: "user".to_string(),
            content: AnthropicMessageContent::String(text.to_string()),
        }
    }

    fn assistant_msg(text: &str) -> AnthropicMessage {
        AnthropicMessage {
            role: "assistant".to_string(),
            content: AnthropicMessageContent::String(text.to_string()),
        }
    }

    fn user_blocks_msg(blocks: Vec<AnthropicContent>) -> AnthropicMessage {
        AnthropicMessage {
            role: "user".to_string(),
            content: AnthropicMessageContent::Blocks(blocks),
        }
    }

    fn assistant_blocks_msg(blocks: Vec<AnthropicContent>) -> AnthropicMessage {
        AnthropicMessage {
            role: "assistant".to_string(),
            content: AnthropicMessageContent::Blocks(blocks),
        }
    }

    fn text_block(text: &str) -> AnthropicContent {
        AnthropicContent::Text {
            text: text.to_string(),
            cache_control: None,
        }
    }

    fn tool_result_block(tool_use_id: &str, content: &str) -> AnthropicContent {
        AnthropicContent::ToolResult {
            tool_use_id: tool_use_id.to_string(),
            content: Some(AnthropicMessageContent::String(content.to_string())),
            is_error: None,
            cache_control: None,
        }
    }

    fn tool_use_block(id: &str, name: &str) -> AnthropicContent {
        AnthropicContent::ToolUse {
            id: id.to_string(),
            name: name.to_string(),
            input: serde_json::json!({}),
            cache_control: None,
        }
    }

    fn count_blocks(content: &AnthropicMessageContent) -> usize {
        match content {
            AnthropicMessageContent::String(_) => 1,
            AnthropicMessageContent::Blocks(b) => b.len(),
        }
    }

    #[test]
    fn test_merge_consecutive_user_messages() {
        let messages = vec![user_msg("Hello"), user_msg("World")];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].role, "user");
        assert_eq!(count_blocks(&merged[0].content), 2);
    }

    #[test]
    fn test_merge_consecutive_tool_result_messages() {
        // Three consecutive user messages (each with a tool_result) should merge into one
        let messages = vec![
            user_blocks_msg(vec![tool_result_block("t1", "result1")]),
            user_blocks_msg(vec![tool_result_block("t2", "result2")]),
            user_blocks_msg(vec![tool_result_block("t3", "result3")]),
        ];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].role, "user");
        assert_eq!(count_blocks(&merged[0].content), 3);

        // Verify all tool_result blocks are present
        if let AnthropicMessageContent::Blocks(blocks) = &merged[0].content {
            for (i, block) in blocks.iter().enumerate() {
                match block {
                    AnthropicContent::ToolResult { tool_use_id, .. } => {
                        assert_eq!(tool_use_id, &format!("t{}", i + 1));
                    }
                    _ => panic!("Expected ToolResult block at index {}", i),
                }
            }
        } else {
            panic!("Expected Blocks content");
        }
    }

    #[test]
    fn test_merge_consecutive_assistant_messages() {
        let messages = vec![assistant_msg("Part 1"), assistant_msg("Part 2")];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].role, "assistant");
        assert_eq!(count_blocks(&merged[0].content), 2);
    }

    #[test]
    fn test_no_merge_alternating_roles() {
        let messages = vec![user_msg("Hi"), assistant_msg("Hello"), user_msg("Bye")];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 3);
        assert_eq!(merged[0].role, "user");
        assert_eq!(merged[1].role, "assistant");
        assert_eq!(merged[2].role, "user");
    }

    #[test]
    fn test_merge_mixed_string_and_blocks() {
        let messages = vec![
            user_msg("Hello"),
            user_blocks_msg(vec![text_block("World")]),
        ];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 1);
        assert_eq!(count_blocks(&merged[0].content), 2);

        if let AnthropicMessageContent::Blocks(blocks) = &merged[0].content {
            match &blocks[0] {
                AnthropicContent::Text { text, .. } => assert_eq!(text, "Hello"),
                _ => panic!("Expected Text block"),
            }
            match &blocks[1] {
                AnthropicContent::Text { text, .. } => assert_eq!(text, "World"),
                _ => panic!("Expected Text block"),
            }
        } else {
            panic!("Expected Blocks content");
        }
    }

    #[test]
    fn test_merge_preserves_cache_control_on_last() {
        let cached_block = AnthropicContent::Text {
            text: "cached".to_string(),
            cache_control: Some(AnthropicCacheControl::ephemeral()),
        };
        let messages = vec![user_msg("first"), user_blocks_msg(vec![cached_block])];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 1);
        if let AnthropicMessageContent::Blocks(blocks) = &merged[0].content {
            assert_eq!(blocks.len(), 2);
            // First block should have no cache control
            match &blocks[0] {
                AnthropicContent::Text { cache_control, .. } => {
                    assert!(cache_control.is_none());
                }
                _ => panic!("Expected Text block"),
            }
            // Last block should preserve cache control
            match &blocks[1] {
                AnthropicContent::Text { cache_control, .. } => {
                    assert!(cache_control.is_some());
                }
                _ => panic!("Expected Text block"),
            }
        } else {
            panic!("Expected Blocks content");
        }
    }

    #[test]
    fn test_single_message_no_merge() {
        let messages = vec![user_msg("solo")];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].role, "user");
        match &merged[0].content {
            AnthropicMessageContent::String(s) => assert_eq!(s, "solo"),
            _ => panic!("Expected String content for single message"),
        }
    }

    #[test]
    fn test_empty_messages() {
        let messages: Vec<AnthropicMessage> = vec![];
        let merged = merge_consecutive_messages(messages);
        assert!(merged.is_empty());
    }

    #[test]
    fn test_full_conversation_with_multiple_tool_results() {
        // Simulate: assistant with 3 tool_use, followed by 3 tool result messages
        let messages = vec![
            assistant_blocks_msg(vec![
                tool_use_block("t1", "tool_a"),
                tool_use_block("t2", "tool_b"),
                tool_use_block("t3", "tool_c"),
            ]),
            user_blocks_msg(vec![tool_result_block("t1", "result_a")]),
            user_blocks_msg(vec![tool_result_block("t2", "result_b")]),
            user_blocks_msg(vec![tool_result_block("t3", "result_c")]),
        ];
        let merged = merge_consecutive_messages(messages);

        // Should produce [assistant, user(3 tool_results)]
        assert_eq!(merged.len(), 2);
        assert_eq!(merged[0].role, "assistant");
        assert_eq!(merged[1].role, "user");

        // Assistant should have 3 tool_use blocks
        assert_eq!(count_blocks(&merged[0].content), 3);

        // User should have 3 tool_result blocks
        assert_eq!(count_blocks(&merged[1].content), 3);
        if let AnthropicMessageContent::Blocks(blocks) = &merged[1].content {
            for block in blocks {
                assert!(
                    matches!(block, AnthropicContent::ToolResult { .. }),
                    "Expected ToolResult block"
                );
            }
        }
    }

    #[test]
    fn test_user_message_followed_by_tool_results_merges() {
        // A user text message followed by tool result messages should merge
        let messages = vec![
            user_msg("Here are the results:"),
            user_blocks_msg(vec![tool_result_block("t1", "result1")]),
            user_blocks_msg(vec![tool_result_block("t2", "result2")]),
        ];
        let merged = merge_consecutive_messages(messages);

        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].role, "user");
        assert_eq!(count_blocks(&merged[0].content), 3);

        if let AnthropicMessageContent::Blocks(blocks) = &merged[0].content {
            match &blocks[0] {
                AnthropicContent::Text { text, .. } => {
                    assert_eq!(text, "Here are the results:");
                }
                _ => panic!("Expected Text block first"),
            }
            assert!(matches!(&blocks[1], AnthropicContent::ToolResult { .. }));
            assert!(matches!(&blocks[2], AnthropicContent::ToolResult { .. }));
        }
    }

    // --- apply_tail_cache_to_message tests ---

    #[test]
    fn test_apply_tail_cache_to_string_message() {
        let mut validator = CacheControlValidator::new();
        let mut msg = user_msg("hello");

        apply_tail_cache_to_message(&mut msg, &mut validator);

        // Should convert to blocks and add cache_control
        match &msg.content {
            AnthropicMessageContent::Blocks(blocks) => {
                assert_eq!(blocks.len(), 1);
                match &blocks[0] {
                    AnthropicContent::Text {
                        text,
                        cache_control,
                    } => {
                        assert_eq!(text, "hello");
                        assert!(cache_control.is_some());
                    }
                    _ => panic!("Expected Text block"),
                }
            }
            _ => panic!("Expected Blocks content after tail cache"),
        }
        assert_eq!(validator.breakpoint_count(), 1);
    }

    #[test]
    fn test_apply_tail_cache_to_blocks_message() {
        let mut validator = CacheControlValidator::new();
        let mut msg = user_blocks_msg(vec![
            tool_result_block("t1", "result1"),
            tool_result_block("t2", "result2"),
        ]);

        apply_tail_cache_to_message(&mut msg, &mut validator);

        // Only the LAST block should get cache_control
        if let AnthropicMessageContent::Blocks(blocks) = &msg.content {
            assert_eq!(blocks.len(), 2);
            match &blocks[0] {
                AnthropicContent::ToolResult { cache_control, .. } => {
                    assert!(cache_control.is_none(), "First block should NOT be cached");
                }
                _ => panic!("Expected ToolResult"),
            }
            match &blocks[1] {
                AnthropicContent::ToolResult { cache_control, .. } => {
                    assert!(cache_control.is_some(), "Last block SHOULD be cached");
                }
                _ => panic!("Expected ToolResult"),
            }
        } else {
            panic!("Expected Blocks content");
        }
        assert_eq!(validator.breakpoint_count(), 1);
    }

    #[test]
    fn test_apply_tail_cache_respects_breakpoint_limit() {
        let mut validator = CacheControlValidator::new();
        let cache = crate::types::CacheControl::ephemeral();

        // Exhaust all 4 breakpoints
        for _ in 0..4 {
            validator.validate(Some(&cache), CacheContext::user_message_part());
        }
        assert!(validator.is_at_limit());

        let mut msg = user_msg("no room");
        apply_tail_cache_to_message(&mut msg, &mut validator);

        // Should remain a String (no conversion) since breakpoint was rejected
        match &msg.content {
            AnthropicMessageContent::String(s) => assert_eq!(s, "no room"),
            _ => panic!("Should not convert to blocks when breakpoint limit exceeded"),
        }
    }

    #[test]
    fn test_tail_cache_after_merge_uses_one_breakpoint_for_merged_tool_results() {
        // Scenario: 3 consecutive tool_result user messages merge into 1.
        // Tail caching should use only 1 breakpoint (on the merged message),
        // NOT 3 breakpoints (one per pre-merge message).
        let mut validator = CacheControlValidator::new();

        let mut merged = [
            assistant_blocks_msg(vec![
                tool_use_block("t1", "tool_a"),
                tool_use_block("t2", "tool_b"),
                tool_use_block("t3", "tool_c"),
            ]),
            // Simulate 3 tool_result messages already merged into 1
            user_blocks_msg(vec![
                tool_result_block("t1", "result_a"),
                tool_result_block("t2", "result_b"),
                tool_result_block("t3", "result_c"),
            ]),
        ];

        // Apply tail caching with tail_count=2 (both messages)
        let len = merged.len();
        let cache_start = len.saturating_sub(2);
        for msg in &mut merged[cache_start..] {
            apply_tail_cache_to_message(msg, &mut validator);
        }

        // Should use exactly 2 breakpoints (one per merged message)
        assert_eq!(
            validator.breakpoint_count(),
            2,
            "Should use 2 breakpoints, not more"
        );

        // Assistant message: last block (tool_use t3) should be cached
        if let AnthropicMessageContent::Blocks(blocks) = &merged[0].content {
            match &blocks[2] {
                AnthropicContent::ToolUse { cache_control, .. } => {
                    assert!(cache_control.is_some(), "Last tool_use should be cached");
                }
                _ => panic!("Expected ToolUse"),
            }
            // First two should NOT be cached
            for block in &blocks[..2] {
                match block {
                    AnthropicContent::ToolUse { cache_control, .. } => {
                        assert!(cache_control.is_none());
                    }
                    _ => panic!("Expected ToolUse"),
                }
            }
        }

        // User message: last block (tool_result t3) should be cached
        if let AnthropicMessageContent::Blocks(blocks) = &merged[1].content {
            match &blocks[2] {
                AnthropicContent::ToolResult { cache_control, .. } => {
                    assert!(cache_control.is_some(), "Last tool_result should be cached");
                }
                _ => panic!("Expected ToolResult"),
            }
            // First two should NOT be cached
            for block in &blocks[..2] {
                match block {
                    AnthropicContent::ToolResult { cache_control, .. } => {
                        assert!(cache_control.is_none());
                    }
                    _ => panic!("Expected ToolResult"),
                }
            }
        }
    }

    #[test]
    fn test_set_block_cache_control() {
        let cc = AnthropicCacheControl::ephemeral();

        // Text block
        let mut block = text_block("hello");
        set_block_cache_control(&mut block, Some(cc.clone()));
        match &block {
            AnthropicContent::Text { cache_control, .. } => assert!(cache_control.is_some()),
            _ => panic!("Expected Text"),
        }

        // ToolResult block
        let mut block = tool_result_block("t1", "result");
        set_block_cache_control(&mut block, Some(cc.clone()));
        match &block {
            AnthropicContent::ToolResult { cache_control, .. } => {
                assert!(cache_control.is_some())
            }
            _ => panic!("Expected ToolResult"),
        }

        // ToolUse block
        let mut block = tool_use_block("t1", "tool_a");
        set_block_cache_control(&mut block, Some(cc.clone()));
        match &block {
            AnthropicContent::ToolUse { cache_control, .. } => assert!(cache_control.is_some()),
            _ => panic!("Expected ToolUse"),
        }
    }

    // --- sanitize_anthropic_message tests ---

    #[test]
    fn test_sanitize_removes_empty_text_blocks() {
        let cc = AnthropicCacheControl::ephemeral();

        // Empty text block (with or without cache_control) should be removed entirely
        let mut msg = user_blocks_msg(vec![AnthropicContent::Text {
            text: String::new(),
            cache_control: Some(cc.clone()),
        }]);
        sanitize_anthropic_message(&mut msg);
        match &msg.content {
            AnthropicMessageContent::Blocks(blocks) => {
                assert!(
                    blocks.is_empty(),
                    "Empty text blocks must be removed entirely"
                );
            }
            _ => panic!("Expected Blocks"),
        }
    }

    #[test]
    fn test_sanitize_preserves_cache_control_on_non_empty_text() {
        let cc = AnthropicCacheControl::ephemeral();

        let mut msg = user_blocks_msg(vec![AnthropicContent::Text {
            text: "hello".to_string(),
            cache_control: Some(cc.clone()),
        }]);
        sanitize_anthropic_message(&mut msg);
        match &msg.content {
            AnthropicMessageContent::Blocks(blocks) => match &blocks[0] {
                AnthropicContent::Text { cache_control, .. } => {
                    assert!(
                        cache_control.is_some(),
                        "cache_control must be preserved on non-empty text"
                    );
                }
                _ => panic!("Expected Text block"),
            },
            _ => panic!("Expected Blocks"),
        }
    }

    #[test]
    fn test_sanitize_handles_mixed_blocks() {
        let cc = AnthropicCacheControl::ephemeral();

        // Mix of empty text (with cache), non-empty text (with cache), and tool_result
        let mut msg = user_blocks_msg(vec![
            AnthropicContent::Text {
                text: String::new(),
                cache_control: Some(cc.clone()),
            },
            AnthropicContent::Text {
                text: "real content".to_string(),
                cache_control: Some(cc.clone()),
            },
            AnthropicContent::ToolResult {
                tool_use_id: "t1".to_string(),
                content: Some(AnthropicMessageContent::String("ok".to_string())),
                is_error: None,
                cache_control: Some(cc.clone()),
            },
        ]);
        sanitize_anthropic_message(&mut msg);
        match &msg.content {
            AnthropicMessageContent::Blocks(blocks) => {
                // Empty text block should be removed entirely
                assert_eq!(blocks.len(), 2);
                // Non-empty text: cache_control preserved
                match &blocks[0] {
                    AnthropicContent::Text {
                        text,
                        cache_control,
                    } => {
                        assert_eq!(text, "real content");
                        assert!(cache_control.is_some());
                    }
                    _ => panic!("Expected Text"),
                }
                // ToolResult: cache_control preserved
                match &blocks[1] {
                    AnthropicContent::ToolResult { cache_control, .. } => {
                        assert!(cache_control.is_some());
                    }
                    _ => panic!("Expected ToolResult"),
                }
            }
            _ => panic!("Expected Blocks"),
        }
    }

    #[test]
    fn test_sanitize_noop_on_string_content() {
        // String content has no cache_control field — sanitize should be a no-op
        let mut msg = user_msg("hello");
        sanitize_anthropic_message(&mut msg);
        match &msg.content {
            AnthropicMessageContent::String(s) => assert_eq!(s, "hello"),
            _ => panic!("Expected String content"),
        }
    }

    // --- is_empty_content_message tests ---

    #[test]
    fn test_is_empty_content_message() {
        // Empty string content
        assert!(is_empty_content_message(&user_msg("")));

        // Non-empty string content
        assert!(!is_empty_content_message(&user_msg("hello")));

        // Blocks with only empty text
        assert!(is_empty_content_message(&user_blocks_msg(vec![
            text_block(""),
        ])));

        // Blocks with non-empty text
        assert!(!is_empty_content_message(&user_blocks_msg(vec![
            text_block("hello"),
        ])));

        // Blocks with tool_result (not empty even if no text)
        assert!(!is_empty_content_message(&user_blocks_msg(vec![
            tool_result_block("t1", "result"),
        ])));

        // Mixed: empty text + tool_result → not empty
        assert!(!is_empty_content_message(&user_blocks_msg(vec![
            text_block(""),
            tool_result_block("t1", "result"),
        ])));
    }

    #[test]
    fn test_empty_message_does_not_waste_cache_breakpoint() {
        // Phase 5 (tail cache) should skip empty messages, preserving breakpoints for real content.
        let mut validator = CacheControlValidator::new();

        // Simulate tail caching over [non-empty, empty, non-empty]
        let mut messages = vec![
            user_msg("real content"),
            assistant_msg(""), // empty — should be skipped
            user_msg("more content"),
        ];

        for msg in &mut messages {
            if !is_empty_content_message(msg) {
                apply_tail_cache_to_message(msg, &mut validator);
            }
        }

        // Only 2 breakpoints consumed, not 3
        assert_eq!(
            validator.breakpoint_count(),
            2,
            "Empty message must not consume a cache breakpoint"
        );
    }

    // --- sanitize_message_sequence tests ---

    #[test]
    fn test_sanitize_sequence_adds_missing_tool_results() {
        // Assistant with 3 tool_use, but user only has 1 tool_result
        let mut messages = vec![
            user_msg("Hello"),
            assistant_blocks_msg(vec![
                tool_use_block("t1", "tool_a"),
                tool_use_block("t2", "tool_b"),
                tool_use_block("t3", "tool_c"),
            ]),
            user_blocks_msg(vec![tool_result_block("t1", "result_a")]),
        ];
        sanitize_message_sequence(&mut messages);

        // Should still have 3 messages (user, assistant, user)
        assert_eq!(messages.len(), 3);
        assert_eq!(messages[0].role, "user");
        assert_eq!(messages[1].role, "assistant");
        assert_eq!(messages[2].role, "user");

        // The user message should now have 3 tool_result blocks
        let result_ids = extract_tool_result_ids(&messages[2]);
        assert!(result_ids.contains("t1"), "Original tool_result preserved");
        assert!(result_ids.contains("t2"), "Placeholder added for t2");
        assert!(result_ids.contains("t3"), "Placeholder added for t3");
    }

    #[test]
    fn test_sanitize_sequence_inserts_user_for_dangling_tool_use() {
        // Assistant with tool_use but NO following user message
        let mut messages = vec![
            user_msg("Hello"),
            assistant_blocks_msg(vec![
                tool_use_block("t1", "tool_a"),
                tool_use_block("t2", "tool_b"),
            ]),
        ];
        sanitize_message_sequence(&mut messages);

        // Should insert a user message with placeholder tool_results
        // and the conversation should end with user
        assert!(messages.last().is_some_and(|m| m.role == "user"));

        // Check the last user message has both tool_results
        let last = messages.last().expect("non-empty");
        let result_ids = extract_tool_result_ids(last);
        assert!(result_ids.contains("t1"));
        assert!(result_ids.contains("t2"));
    }

    #[test]
    fn test_sanitize_sequence_preserves_trailing_assistant_with_substantive_text() {
        // Conversation ending with substantive assistant text (no tool_use)
        let mut messages = vec![user_msg("Hello"), assistant_msg("I'll help you with that.")];
        sanitize_message_sequence(&mut messages);

        // Substantive trailing assistant should be preserved (API accepts prefill)
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].role, "user");
        assert_eq!(messages[1].role, "assistant");
    }

    #[test]
    fn test_sanitize_sequence_removes_trailing_assistant_empty_text() {
        // Conversation ending with empty/whitespace assistant text
        let mut messages = vec![user_msg("Hello"), assistant_msg("   ")];
        sanitize_message_sequence(&mut messages);

        // Whitespace-only trailing assistant should be removed
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].role, "user");
    }

    #[test]
    fn test_sanitize_sequence_trailing_assistant_with_tool_use() {
        // Conversation ending with assistant that has tool_use
        let mut messages = vec![
            user_msg("Hello"),
            assistant_blocks_msg(vec![
                text_block("Let me check..."),
                tool_use_block("t1", "search"),
            ]),
        ];
        sanitize_message_sequence(&mut messages);

        // Should add user message with tool_result placeholder
        assert_eq!(messages.len(), 3);
        assert_eq!(messages[0].role, "user");
        assert_eq!(messages[1].role, "assistant");
        assert_eq!(messages[2].role, "user");

        let result_ids = extract_tool_result_ids(&messages[2]);
        assert!(result_ids.contains("t1"));
    }

    #[test]
    fn test_sanitize_sequence_removes_orphan_tool_results() {
        // User message has tool_results that don't match any tool_use in preceding assistant
        let mut messages = vec![
            user_msg("Hello"),
            assistant_msg("Sure!"), // No tool_use blocks
            user_blocks_msg(vec![
                tool_result_block("orphan_id", "stale result"),
                text_block("Follow-up text"),
            ]),
        ];
        sanitize_message_sequence(&mut messages);

        // The orphan tool_result should be removed, but text should remain
        let last = messages.last().expect("non-empty");
        assert_eq!(last.role, "user");
        let result_ids = extract_tool_result_ids(last);
        assert!(
            result_ids.is_empty(),
            "Orphan tool_result should be removed"
        );

        // Text block should be preserved
        if let AnthropicMessageContent::Blocks(blocks) = &last.content {
            assert!(blocks.iter().any(
                |b| matches!(b, AnthropicContent::Text { text, .. } if text == "Follow-up text")
            ));
        } else {
            panic!("Expected Blocks content");
        }
    }

    #[test]
    fn test_sanitize_sequence_removes_user_with_only_orphan_results() {
        // User message that becomes empty after orphan removal
        let mut messages = vec![
            user_msg("Hello"),
            assistant_msg("Sure!"), // No tool_use
            user_blocks_msg(vec![tool_result_block("orphan_id", "stale result")]),
        ];
        sanitize_message_sequence(&mut messages);

        // After removing orphan, user message becomes empty and should be removed.
        // The assistant "Sure!" is substantive, so it's preserved as prefill.
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].role, "user");
        assert_eq!(messages[1].role, "assistant");
    }

    #[test]
    fn test_sanitize_sequence_ensures_starts_with_user() {
        // Conversation that starts with assistant
        let mut messages = vec![assistant_msg("Hello"), user_msg("Hi")];
        sanitize_message_sequence(&mut messages);

        assert_eq!(messages[0].role, "user");
    }

    #[test]
    fn test_sanitize_sequence_noop_for_valid_conversation() {
        // Already valid alternating conversation
        let mut messages = vec![
            user_msg("Hello"),
            assistant_msg("Hi there!"),
            user_msg("How are you?"),
        ];
        let original_len = messages.len();
        sanitize_message_sequence(&mut messages);

        assert_eq!(messages.len(), original_len);
        assert_eq!(messages[0].role, "user");
        assert_eq!(messages[1].role, "assistant");
        assert_eq!(messages[2].role, "user");
    }

    #[test]
    fn test_sanitize_sequence_full_tool_call_flow() {
        // Realistic scenario: assistant with 6 tool calls, all results present
        let mut messages = vec![
            user_msg("Run all checks"),
            assistant_blocks_msg(vec![
                tool_use_block("t1", "check_a"),
                tool_use_block("t2", "check_b"),
                tool_use_block("t3", "check_c"),
                tool_use_block("t4", "check_d"),
                tool_use_block("t5", "check_e"),
                tool_use_block("t6", "check_f"),
            ]),
            user_blocks_msg(vec![
                tool_result_block("t1", "ok"),
                tool_result_block("t2", "ok"),
                tool_result_block("t3", "ok"),
                tool_result_block("t4", "ok"),
                tool_result_block("t5", "ok"),
                tool_result_block("t6", "ok"),
            ]),
        ];
        sanitize_message_sequence(&mut messages);

        // Should remain unchanged
        assert_eq!(messages.len(), 3);
        let result_ids = extract_tool_result_ids(&messages[2]);
        assert_eq!(result_ids.len(), 6);
    }

    #[test]
    fn test_sanitize_sequence_partial_tool_results_missing() {
        // Error scenario from the bug report: 6 tool_use but 0 tool_results
        let mut messages = vec![
            user_msg("Run all checks"),
            assistant_blocks_msg(vec![
                tool_use_block("t1", "check_a"),
                tool_use_block("t2", "check_b"),
                tool_use_block("t3", "check_c"),
                tool_use_block("t4", "check_d"),
                tool_use_block("t5", "check_e"),
                tool_use_block("t6", "check_f"),
            ]),
            user_msg("Continue"), // No tool_results at all
        ];
        sanitize_message_sequence(&mut messages);

        // Should have placeholders for all 6 tool_use IDs
        assert_eq!(messages[2].role, "user");
        let result_ids = extract_tool_result_ids(&messages[2]);
        assert_eq!(
            result_ids.len(),
            6,
            "All 6 missing tool_results should have placeholders"
        );
    }

    #[test]
    fn test_sanitize_sequence_empty_messages() {
        let mut messages: Vec<AnthropicMessage> = vec![];
        sanitize_message_sequence(&mut messages);
        assert!(messages.is_empty());
    }

    #[test]
    fn test_sanitize_sequence_multiple_consecutive_trailing_assistants_substantive() {
        // Edge case: multiple trailing assistants merge into one with content
        let mut messages = vec![
            user_msg("Hello"),
            assistant_msg("Part 1"),
            assistant_msg("Part 2"),
        ];
        sanitize_message_sequence(&mut messages);

        // After merge, becomes [user, assistant("Part 1\nPart 2")].
        // Substantive content is preserved as prefill.
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].role, "user");
        assert_eq!(messages[1].role, "assistant");
    }

    #[test]
    fn test_sanitize_sequence_multiple_consecutive_trailing_assistants_empty() {
        // Edge case: multiple trailing assistants that are all empty/whitespace
        let mut messages = vec![user_msg("Hello"), assistant_msg(""), assistant_msg("   ")];
        sanitize_message_sequence(&mut messages);

        // Empty messages removed by step 4, whitespace-only removed as non-substantive
        assert!(
            messages.last().is_some_and(|m| m.role == "user"),
            "Must end with user after empty trailing assistants removed"
        );
    }

    #[test]
    fn test_extract_tool_use_ids() {
        let msg = assistant_blocks_msg(vec![
            text_block("I'll run some tools"),
            tool_use_block("t1", "search"),
            tool_use_block("t2", "fetch"),
        ]);
        let ids = extract_tool_use_ids(&msg);
        assert_eq!(ids, vec!["t1", "t2"]);
    }

    #[test]
    fn test_extract_tool_use_ids_from_string_content() {
        let msg = assistant_msg("No tools here");
        let ids = extract_tool_use_ids(&msg);
        assert!(ids.is_empty());
    }

    #[test]
    fn test_extract_tool_result_ids() {
        let msg = user_blocks_msg(vec![
            tool_result_block("t1", "result1"),
            tool_result_block("t2", "result2"),
            text_block("Some text"),
        ]);
        let ids = extract_tool_result_ids(&msg);
        assert_eq!(ids.len(), 2);
        assert!(ids.contains("t1"));
        assert!(ids.contains("t2"));
    }

    #[test]
    fn test_inject_placeholder_tool_results_into_blocks() {
        let mut msg = user_blocks_msg(vec![tool_result_block("t1", "result1")]);
        inject_placeholder_tool_results(&mut msg, &["t2".to_string(), "t3".to_string()]);

        if let AnthropicMessageContent::Blocks(blocks) = &msg.content {
            assert_eq!(blocks.len(), 3);
            // Original
            assert!(
                matches!(&blocks[0], AnthropicContent::ToolResult { tool_use_id, .. } if tool_use_id == "t1")
            );
            // Injected placeholders
            assert!(
                matches!(&blocks[1], AnthropicContent::ToolResult { tool_use_id, is_error, .. } if tool_use_id == "t2" && *is_error == Some(true))
            );
            assert!(
                matches!(&blocks[2], AnthropicContent::ToolResult { tool_use_id, .. } if tool_use_id == "t3")
            );
        } else {
            panic!("Expected Blocks");
        }
    }

    #[test]
    fn test_inject_placeholder_tool_results_into_string() {
        let mut msg = user_msg("Continue");
        inject_placeholder_tool_results(&mut msg, &["t1".to_string()]);

        if let AnthropicMessageContent::Blocks(blocks) = &msg.content {
            assert_eq!(blocks.len(), 2);
            // Original text preserved as Text block
            assert!(
                matches!(&blocks[0], AnthropicContent::Text { text, .. } if text == "Continue")
            );
            // Injected placeholder
            assert!(
                matches!(&blocks[1], AnthropicContent::ToolResult { tool_use_id, .. } if tool_use_id == "t1")
            );
        } else {
            panic!("Expected Blocks");
        }
    }

    #[test]
    fn test_inject_placeholder_into_empty_string_skips_empty_text_block() {
        // Issue 2: inject_placeholder_tool_results on String("") should NOT create
        // an empty text block that would reintroduce the problem sanitize_anthropic_message fixed.
        let mut msg = AnthropicMessage {
            role: "user".to_string(),
            content: AnthropicMessageContent::String(String::new()),
        };
        inject_placeholder_tool_results(&mut msg, &["t1".to_string()]);

        if let AnthropicMessageContent::Blocks(blocks) = &msg.content {
            // Should contain ONLY the tool_result — no empty text block
            assert_eq!(
                blocks.len(),
                1,
                "Empty string should not produce a text block"
            );
            assert!(
                matches!(&blocks[0], AnthropicContent::ToolResult { tool_use_id, .. } if tool_use_id == "t1")
            );
        } else {
            panic!("Expected Blocks");
        }
    }

    #[test]
    fn test_sanitize_sequence_distant_orphan_tool_result() {
        // Vercel-inspired: tool_result appears in a distant user message,
        // not immediately after the assistant with the matching tool_use.
        // This simulates context manager truncation or checkpoint corruption.
        let mut messages = vec![
            assistant_blocks_msg(vec![tool_use_block("t1", "search")]),
            user_msg("Intermediate text"), // No tool_result for t1
            assistant_msg("I found something"),
            user_blocks_msg(vec![tool_result_block("t1", "late result")]), // Orphan
        ];
        sanitize_message_sequence(&mut messages);

        // Step 1 should inject placeholder for t1 after first assistant.
        // Step 2 should remove orphan t1 from the last user message.
        // Step 4 should prepend a user message since first msg is assistant.
        // Step 5 should handle trailing state.

        // Verify: first message is user
        assert_eq!(messages[0].role, "user");

        // Verify: the assistant with tool_use(t1) is followed by a user with tool_result(t1)
        let assistant_idx = messages
            .iter()
            .position(|m| m.role == "assistant" && !extract_tool_use_ids(m).is_empty())
            .expect("Should have assistant with tool_use");
        let next = &messages[assistant_idx + 1];
        assert_eq!(next.role, "user");
        let result_ids = extract_tool_result_ids(next);
        assert!(
            result_ids.contains("t1"),
            "tool_result for t1 must follow its tool_use"
        );

        // The trailing assistant "I found something" is substantive → preserved
        let last = messages.last().expect("non-empty");
        assert!(
            last.role == "user" || last.role == "assistant",
            "Must end with user or substantive assistant"
        );

        // Verify: no orphan tool_result(t1) in later messages
        for msg in &messages[(assistant_idx + 2)..] {
            let orphans = extract_tool_result_ids(msg);
            assert!(
                !orphans.contains("t1"),
                "Orphan tool_result(t1) should be removed from later messages"
            );
        }
    }

    #[test]
    fn test_sanitize_sequence_context_manager_truncated_results() {
        // Simulates context manager dropping tool_result messages to save tokens.
        // Multi-turn conversation where middle tool_results were removed.
        let mut messages = vec![
            user_msg("Start"),
            // Turn 1: assistant calls tools, results present
            assistant_blocks_msg(vec![
                tool_use_block("t1", "search"),
                tool_use_block("t2", "fetch"),
            ]),
            user_blocks_msg(vec![
                tool_result_block("t1", "ok"),
                tool_result_block("t2", "ok"),
            ]),
            // Turn 2: assistant calls more tools, but results were TRUNCATED
            assistant_blocks_msg(vec![
                tool_use_block("t3", "analyze"),
                tool_use_block("t4", "summarize"),
            ]),
            // Context manager dropped the tool_result messages here
            user_msg("What did you find?"),
            assistant_msg("Based on my analysis..."),
        ];
        sanitize_message_sequence(&mut messages);

        // Verify: t3 and t4 have placeholder results
        // After sanitization, find the assistant with t3/t4
        let assistant_idx = messages
            .iter()
            .position(|m| {
                m.role == "assistant"
                    && extract_tool_use_ids(m)
                        .iter()
                        .any(|id| id == "t3" || id == "t4")
            })
            .expect("Should find assistant with t3/t4");

        let next = &messages[assistant_idx + 1];
        assert_eq!(next.role, "user");
        let result_ids = extract_tool_result_ids(next);
        assert!(result_ids.contains("t3"), "Placeholder for t3");
        assert!(result_ids.contains("t4"), "Placeholder for t4");

        // Verify conversation is structurally valid
        assert_eq!(messages[0].role, "user", "Must start with user");

        // The trailing assistant "Based on my analysis..." is substantive,
        // so it's preserved as prefill (not removed).
        let last = messages.last().expect("non-empty");
        assert!(
            last.role == "user" || last.role == "assistant",
            "Must end with user or substantive assistant"
        );

        // Verify alternating roles
        for window in messages.windows(2) {
            assert_ne!(
                window[0].role, window[1].role,
                "Roles must alternate: {:?} followed by {:?}",
                window[0].role, window[1].role
            );
        }
    }

    #[test]
    fn test_sanitize_sequence_preserves_valid_tool_results() {
        // Ensure sanitization doesn't remove valid tool_results
        let mut messages = vec![
            user_msg("Hello"),
            assistant_blocks_msg(vec![
                tool_use_block("t1", "search"),
                tool_use_block("t2", "fetch"),
            ]),
            user_blocks_msg(vec![
                tool_result_block("t1", "found it"),
                tool_result_block("t2", "fetched it"),
            ]),
            assistant_msg("Here's what I found"),
            user_msg("Thanks"),
        ];
        let original_len = messages.len();
        sanitize_message_sequence(&mut messages);

        // Should remain unchanged
        assert_eq!(messages.len(), original_len);

        // Verify tool_results are preserved
        let result_ids = extract_tool_result_ids(&messages[2]);
        assert!(result_ids.contains("t1"));
        assert!(result_ids.contains("t2"));
    }

    // --- dedup_tool_results tests ---

    #[test]
    fn test_dedup_tool_results_removes_duplicates() {
        // API error: "each tool_use must have a single result. Found multiple
        // `tool_result` blocks with id: t1"
        let mut messages = vec![
            user_msg("Hi"),
            assistant_blocks_msg(vec![tool_use_block("t1", "test_tool")]),
            user_blocks_msg(vec![
                tool_result_block("t1", "first"),
                tool_result_block("t1", "second"),
            ]),
        ];
        dedup_tool_results(&mut messages);

        if let AnthropicMessageContent::Blocks(blocks) = &messages[2].content {
            let results: Vec<_> = blocks
                .iter()
                .filter(|b| matches!(b, AnthropicContent::ToolResult { .. }))
                .collect();
            assert_eq!(results.len(), 1, "Should keep only one tool_result per ID");
            // Should keep the LAST one
            match &results[0] {
                AnthropicContent::ToolResult { content, .. } => match content {
                    Some(AnthropicMessageContent::String(s)) => {
                        assert_eq!(s, "second", "Should keep last result");
                    }
                    _ => panic!("Expected string content"),
                },
                _ => panic!("Expected ToolResult"),
            }
        } else {
            panic!("Expected Blocks");
        }
    }

    #[test]
    fn test_dedup_tool_results_preserves_different_ids() {
        let mut messages = vec![user_blocks_msg(vec![
            tool_result_block("t1", "result1"),
            tool_result_block("t2", "result2"),
        ])];
        dedup_tool_results(&mut messages);

        if let AnthropicMessageContent::Blocks(blocks) = &messages[0].content {
            assert_eq!(blocks.len(), 2, "Different IDs should both be kept");
        }
    }

    #[test]
    fn test_dedup_tool_results_preserves_non_tool_blocks() {
        let mut messages = vec![user_blocks_msg(vec![
            text_block("hello"),
            tool_result_block("t1", "first"),
            tool_result_block("t1", "second"),
            text_block("world"),
        ])];
        dedup_tool_results(&mut messages);

        if let AnthropicMessageContent::Blocks(blocks) = &messages[0].content {
            assert_eq!(blocks.len(), 3, "2 text blocks + 1 deduped tool_result");
            assert!(matches!(&blocks[0], AnthropicContent::Text { text, .. } if text == "hello"));
            assert!(matches!(&blocks[1], AnthropicContent::ToolResult { .. }));
            assert!(matches!(&blocks[2], AnthropicContent::Text { text, .. } if text == "world"));
        }
    }

    #[test]
    fn test_dedup_skips_assistant_messages() {
        // dedup only applies to user messages (tool_results are in user messages)
        let mut messages = vec![assistant_blocks_msg(vec![
            tool_use_block("t1", "a"),
            tool_use_block("t1", "a"), // weird but not tool_result
        ])];
        let original_len = match &messages[0].content {
            AnthropicMessageContent::Blocks(b) => b.len(),
            _ => panic!(),
        };
        dedup_tool_results(&mut messages);
        match &messages[0].content {
            AnthropicMessageContent::Blocks(b) => assert_eq!(b.len(), original_len),
            _ => panic!(),
        }
    }

    // --- remove_empty_content_messages tests ---

    #[test]
    fn test_remove_empty_string_content() {
        // API error: "all messages must have non-empty content"
        let mut messages = vec![user_msg("Hello"), assistant_msg(""), user_msg("World")];
        remove_empty_content_messages(&mut messages);
        assert_eq!(messages.len(), 2);
        match &messages[0].content {
            AnthropicMessageContent::String(s) => assert_eq!(s, "Hello"),
            _ => panic!(),
        }
        match &messages[1].content {
            AnthropicMessageContent::String(s) => assert_eq!(s, "World"),
            _ => panic!(),
        }
    }

    #[test]
    fn test_remove_empty_blocks_content() {
        // API error: "all messages must have non-empty content"
        let mut messages = vec![
            user_msg("Hello"),
            AnthropicMessage {
                role: "user".to_string(),
                content: AnthropicMessageContent::Blocks(vec![]),
            },
            user_msg("World"),
        ];
        remove_empty_content_messages(&mut messages);
        assert_eq!(messages.len(), 2);
    }

    #[test]
    fn test_remove_empty_preserves_non_empty() {
        let mut messages = vec![user_msg("Hello"), assistant_msg("Hi"), user_msg("Bye")];
        remove_empty_content_messages(&mut messages);
        assert_eq!(messages.len(), 3);
    }

    // --- sanitize_message_sequence integration with new steps ---

    #[test]
    fn test_sanitize_sequence_dedup_tool_results() {
        // End-to-end: duplicate tool_results should be deduped
        let mut messages = vec![
            user_msg("Hi"),
            assistant_blocks_msg(vec![tool_use_block("t1", "tool_a")]),
            user_blocks_msg(vec![
                tool_result_block("t1", "first"),
                tool_result_block("t1", "second"),
            ]),
        ];
        sanitize_message_sequence(&mut messages);

        // Should have exactly 1 tool_result for t1
        let result_ids = extract_tool_result_ids(&messages[2]);
        assert_eq!(result_ids.len(), 1);
        assert!(result_ids.contains("t1"));

        // Count actual tool_result blocks
        if let AnthropicMessageContent::Blocks(blocks) = &messages[2].content {
            let count = blocks
                .iter()
                .filter(|b| matches!(b, AnthropicContent::ToolResult { .. }))
                .count();
            assert_eq!(count, 1, "Only 1 tool_result block after dedup");
        }
    }

    #[test]
    fn test_sanitize_sequence_removes_empty_content_preserves_substantive_assistant() {
        // Empty user messages should be removed; substantive trailing
        // assistant is preserved as prefill
        let mut messages = vec![
            user_msg("Hello"),
            assistant_msg("Response"),
            user_msg(""), // empty — should be removed
        ];
        sanitize_message_sequence(&mut messages);

        // Empty user removed → trailing assistant "Response" is substantive → kept
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].role, "user");
        assert_eq!(messages[1].role, "assistant");
    }

    #[test]
    fn test_sanitize_sequence_removes_empty_content_and_empty_trailing_assistant() {
        // Both empty user AND empty trailing assistant
        let mut messages = vec![
            user_msg("Hello"),
            assistant_msg("  "), // whitespace only
            user_msg(""),        // empty
        ];
        sanitize_message_sequence(&mut messages);

        // Empty user removed → whitespace assistant removed → just "Hello"
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].role, "user");
    }

    // ---- Opus 4.7 helper ----------------------------------------------------

    #[test]
    fn test_is_opus_4_7_or_later_matches_canonical_id() {
        assert!(is_opus_4_7_or_later("claude-opus-4-7"));
    }

    #[test]
    fn test_is_opus_4_7_or_later_is_case_insensitive() {
        assert!(is_opus_4_7_or_later("CLAUDE-OPUS-4-7"));
    }

    #[test]
    fn test_is_opus_4_7_or_later_rejects_opus_4_6() {
        assert!(!is_opus_4_7_or_later("claude-opus-4-6"));
    }

    #[test]
    fn test_is_opus_4_7_or_later_rejects_sonnet_4_6() {
        assert!(!is_opus_4_7_or_later("claude-sonnet-4-6"));
    }

    #[test]
    fn test_is_opus_4_7_or_later_rejects_empty() {
        assert!(!is_opus_4_7_or_later(""));
    }

    // ---- Opus 4.7 request shaping ------------------------------------------

    fn request_for(model_id: &str) -> crate::types::GenerateRequest {
        crate::types::GenerateRequest::new(
            crate::types::Model::custom(model_id, "anthropic"),
            vec![crate::types::Message::new(
                crate::types::Role::User,
                "Hello",
            )],
        )
    }

    fn anthropic_config() -> crate::providers::anthropic::types::AnthropicConfig {
        crate::providers::anthropic::types::AnthropicConfig::new("key")
    }

    #[test]
    fn test_opus_4_7_strips_temperature_and_top_p() {
        let mut req = request_for("claude-opus-4-7");
        req.options.temperature = Some(0.0);
        req.options.top_p = Some(0.9);

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        assert_eq!(result.request.temperature, None);
        assert_eq!(result.request.top_p, None);
        assert_eq!(result.request.top_k, None);
    }

    #[test]
    fn test_opus_4_6_preserves_temperature_and_top_p() {
        let mut req = request_for("claude-opus-4-6");
        req.options.temperature = Some(0.7);
        req.options.top_p = Some(0.95);

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        assert_eq!(result.request.temperature, Some(0.7));
        assert_eq!(result.request.top_p, Some(0.95));
    }

    #[test]
    fn test_opus_4_7_none_temperature_stays_none() {
        let req = request_for("claude-opus-4-7");

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        assert_eq!(result.request.temperature, None);
    }

    // ---- Thinking rewrite --------------------------------------------------

    fn anthropic_thinking_options(budget_tokens: u32) -> crate::types::ProviderOptions {
        crate::types::ProviderOptions::Anthropic(crate::types::AnthropicOptions {
            thinking: Some(crate::types::ThinkingOptions::new(budget_tokens)),
            effort: None,
        })
    }

    #[test]
    fn test_opus_4_7_thinking_serializes_to_adaptive_only() {
        let mut req = request_for("claude-opus-4-7");
        req.provider_options = Some(anthropic_thinking_options(32000));

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        let thinking_json = serde_json::to_value(result.request.thinking.unwrap()).unwrap();
        assert_eq!(thinking_json, serde_json::json!({"type": "adaptive"}));
    }

    #[test]
    fn test_opus_4_6_preserves_enabled_thinking_budget() {
        let mut req = request_for("claude-opus-4-6");
        req.provider_options = Some(anthropic_thinking_options(32000));

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        let thinking_json = serde_json::to_value(result.request.thinking.unwrap()).unwrap();
        assert_eq!(
            thinking_json,
            serde_json::json!({"type": "enabled", "budget_tokens": 32000})
        );
    }

    // ---- Warning surfacing --------------------------------------------------

    fn has_opus_47_warning(warnings: &[CacheWarning], needle: &str) -> bool {
        warnings
            .iter()
            .any(|w| w.message.contains("Opus 4.7") && w.message.contains(needle))
    }

    #[test]
    fn test_opus_4_7_emits_warning_when_temperature_stripped() {
        let mut req = request_for("claude-opus-4-7");
        req.options.temperature = Some(0.0);

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        assert!(
            has_opus_47_warning(&result.warnings, "temperature"),
            "expected Opus-4.7 temperature warning, got {:?}",
            result.warnings
        );
    }

    #[test]
    fn test_opus_4_7_emits_no_warning_when_nothing_supplied() {
        let req = request_for("claude-opus-4-7");

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        assert!(
            !result
                .warnings
                .iter()
                .any(|w| w.message.contains("Opus 4.7")),
            "expected no Opus-4.7 warnings, got {:?}",
            result.warnings
        );
    }

    #[test]
    fn test_opus_4_7_emits_warning_when_thinking_rewritten() {
        let mut req = request_for("claude-opus-4-7");
        req.provider_options = Some(anthropic_thinking_options(32000));

        let result = to_anthropic_request(&req, &anthropic_config(), false).unwrap();

        assert!(
            has_opus_47_warning(&result.warnings, "adaptive"),
            "expected Opus-4.7 thinking-rewrite warning, got {:?}",
            result.warnings
        );
    }
}