allama 0.1.0

Low-level, provider-agnostic LLM abstractions for Rust
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
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::{ExtraMap, RequestField, Subject};

type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;

/// Convenience alias for `std::result::Result<T, E>` with [`Error`] as the
/// default error type.
///
/// The error parameter stays overridable, so importing the alias (for example
/// through `use allama::prelude::*`) does not break code that names its own
/// error type:
///
/// ```
/// use allama::prelude::*;
///
/// #[derive(Debug)]
/// struct MyError;
///
/// fn parse(input: &str) -> Result<u32, MyError> {
///     input.parse().map_err(|_| MyError)
/// }
///
/// fn provider_call() -> Result<u32> {
///     Err(Error::invalid_request("demo"))
/// }
///
/// assert_eq!(parse("7").unwrap(), 7);
/// assert!(provider_call().is_err());
/// ```
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Diagnostic context shared by provider-originated errors.
///
/// Fields are best-effort: providers fill what their transport exposes. The
/// HTTP-shaped fields (`status`, `body`) exist because they are the most
/// useful debugging data for HTTP providers; they do not imply every provider
/// is HTTP-based.
///
/// # Message provenance
///
/// An error's text can come from two places, and the redacted
/// [`ErrorLog`] treats them differently:
///
/// - The **constructor message** (the `message` passed to
///   [`Error::invalid_request`] and friends) is shown in [`ErrorLog`] only
///   when [`message_origin`](Self::message_origin) is
///   [`MessageOrigin::Authored`], set with [`Error::authored`].
/// - The **upstream message** ([`upstream_message`](Self::upstream_message),
///   set with [`Error::with_upstream_message`]) is the provider's own text.
///   It may echo prompts or input values, so [`ErrorLog`] leaves it out
///   unless asked for with [`ErrorLog::with_message`].
///
/// `Display` is not redacted and shows both. `Debug` shows the constructor
/// message and the upstream message's length, never its text
/// (`upstream_message: <withheld N bytes; print with {err}>`), filters the
/// provider code, type and `param` as [`ErrorLog`] does, and leaves out the
/// fields that are not set.
#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ErrorContext {
    /// Identity of the provider that produced the error.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,
    /// HTTP status code, when the failure came from an HTTP response.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<u16>,
    /// Provider-assigned request ID for support tickets.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub request_id: Option<String>,
    /// Suggested delay before retrying, when reported.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        with = "option_duration_secs_f64"
    )]
    pub retry_after: Option<Duration>,
    /// The server's retry directive, when it sent one: `Some(true)` asks the
    /// client to retry, `Some(false)` not to (HTTP `x-should-retry`).
    ///
    /// The default [`RetryPolicy`](crate::wrappers::RetryPolicy) classifier
    /// follows it ahead of the error kind and status; custom classifiers
    /// read it with [`Error::server_retry_directive`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub server_retry_directive: Option<bool>,
    /// Provider error code (e.g. `insufficient_quota`, `RESOURCE_EXHAUSTED`).
    ///
    /// Upstream text: [`ErrorLog`] shows it only when it is a bounded token
    /// (see [`ErrorLog`]'s filter).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider_code: Option<String>,
    /// Provider error type or category, when distinct from the code.
    ///
    /// Upstream text: [`ErrorLog`] shows it only when it is a bounded token
    /// (see [`ErrorLog`]'s filter).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider_type: Option<String>,
    /// The path of the request field the provider rejected, when it names
    /// one (OpenAI's `param`, such as `messages.[1].content`; Gemini's
    /// `BadRequest` field violations).
    ///
    /// A field path, never a field value, is expected; [`ErrorLog`] shows it
    /// only when it is a bounded token (see [`ErrorLog`]'s filter).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub param: Option<String>,
    /// Where the constructor message came from. Only an
    /// [`Authored`](MessageOrigin::Authored) message appears in
    /// [`ErrorLog`].
    #[serde(default, skip_serializing_if = "MessageOrigin::is_unknown")]
    pub message_origin: MessageOrigin,
    /// The provider's own error text, such as the `message` of a JSON error
    /// envelope. Excluded from [`ErrorLog`] unless asked for with
    /// [`ErrorLog::with_message`], because it may echo prompts or input
    /// values.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub upstream_message: Option<String>,
    /// Raw response body, capped at [`MAX_BODY_BYTES`](Self::MAX_BODY_BYTES).
    /// Excluded from [`ErrorLog`] because it may contain prompts, model
    /// output, or credentials.
    ///
    /// [`Error::with_body`] applies the cap. A body written to this field
    /// directly is capped again when serialized and in `Debug`, so serde
    /// round trips of a longer body are lossy.
    #[serde(
        default,
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_capped_body"
    )]
    pub body: Option<String>,
    /// Additional error details, keyed by namespace: provider-specific
    /// details from the adapter, and records that allama itself attaches
    /// under an `allama.*` key. Keys under `allama.` are reserved for
    /// allama.
    ///
    /// Extraction attaches its pass record (mode, passes, summed usage) to
    /// provider errors under `allama.extraction`; read it with
    /// `ExtractionMetadata::from_error`.
    /// [`embed_chunked`](crate::EmbeddingProviderExt::embed_chunked)
    /// attaches its progress (inputs completed, summed usage) under
    /// `allama.embedding`; read it with
    /// [`ChunkedEmbeddingProgress::from_error`](crate::ChunkedEmbeddingProgress::from_error).
    /// [`ErrorLog`] includes those records, which hold no input or output,
    /// and leaves every other key out.
    #[serde(default, skip_serializing_if = "ExtraMap::is_empty")]
    pub metadata: ExtraMap,
    /// The error tried before this one, when this error ended a failover:
    /// read it with [`Error::fallback_from`], set it with
    /// [`Error::with_fallback_from`]. Serialized as `fallback_from`, not
    /// compared, and logged by [`ErrorLog`] as a nested redacted log.
    #[serde(default, skip_serializing_if = "FallbackFrom::is_none")]
    fallback_from: FallbackFrom,
    /// The underlying cause, returned from the error's
    /// [`source`](std::error::Error::source). Not serialized, not compared,
    /// and not shown by `Debug` or [`ErrorLog`]. Set it with
    /// [`Error::with_source`].
    #[serde(skip)]
    source: ErrorSource,
}

/// The cause an [`ErrorContext`] keeps: shared so the error stays `Clone`,
/// and equal to any other so the context's equality reads its fields.
#[derive(Clone, Default)]
struct ErrorSource(Option<Arc<dyn std::error::Error + Send + Sync + 'static>>);

impl PartialEq for ErrorSource {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}

impl Eq for ErrorSource {}

/// The error an [`ErrorContext`] records as tried before it
/// ([`Error::fallback_from`]): shared so the error stays `Clone`, and equal
/// to any other, like [`ErrorSource`], so the context keeps `PartialEq` and
/// `Eq` although [`Error`] has neither.
#[derive(Clone, Default)]
struct FallbackFrom(Option<Arc<Error>>);

impl FallbackFrom {
    fn is_none(&self) -> bool {
        self.0.is_none()
    }
}

impl PartialEq for FallbackFrom {
    fn eq(&self, _: &Self) -> bool {
        true
    }
}

impl Eq for FallbackFrom {}

impl Serialize for FallbackFrom {
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        self.0.as_deref().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for FallbackFrom {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        Ok(Self(
            Option::<Error>::deserialize(deserializer)?.map(Arc::new),
        ))
    }
}

/// The [`ErrorContext::metadata`] key of an extraction's pass record.
pub(crate) const EXTRACTION_METADATA_KEY: &str = "allama.extraction";

/// The [`ErrorContext::metadata`] key of a chunked embedding's progress.
pub(crate) const EMBEDDING_METADATA_KEY: &str = "allama.embedding";

fn context_is_empty(context: &ErrorContext) -> bool {
    context.is_empty()
}

impl ErrorContext {
    /// The most of a response body an error keeps, in bytes. Longer bodies
    /// are truncated at a character boundary.
    pub const MAX_BODY_BYTES: usize = 8 * 1024;

    /// Create an empty context.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The body, capped at [`MAX_BODY_BYTES`](Self::MAX_BODY_BYTES).
    #[must_use]
    pub fn capped_body(&self) -> Option<&str> {
        self.body.as_deref().map(cap_body)
    }

    fn is_empty(&self) -> bool {
        self == &Self::default() && self.source.0.is_none() && self.fallback_from.is_none()
    }

    /// The underlying cause, when one was attached with
    /// [`Error::with_source`].
    #[must_use]
    pub fn source(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
        self.source.0.as_deref()
    }

    /// [`Error::with_context`]: overwrite with every `Some` field of
    /// `other` (the earlier errors of a failover too), merge metadata, and
    /// never touch the message origin.
    fn merge(&mut self, other: ErrorContext) {
        // Destructured so a new field needs a merge rule to compile.
        let ErrorContext {
            provider,
            status,
            request_id,
            retry_after,
            server_retry_directive,
            provider_code,
            provider_type,
            param,
            message_origin: _,
            upstream_message,
            body,
            metadata,
            fallback_from,
            source,
        } = other;
        overwrite(&mut self.provider, provider);
        overwrite(&mut self.status, status);
        overwrite(&mut self.request_id, request_id);
        overwrite(&mut self.retry_after, retry_after);
        overwrite(&mut self.server_retry_directive, server_retry_directive);
        overwrite(&mut self.provider_code, provider_code);
        overwrite(&mut self.provider_type, provider_type);
        overwrite(&mut self.param, param);
        overwrite(&mut self.upstream_message, upstream_message);
        overwrite(
            &mut self.body,
            body.map(|mut body| {
                body.truncate(cap_body(&body).len());
                body
            }),
        );
        self.metadata.extend(metadata);
        if !fallback_from.is_none() {
            self.fallback_from = fallback_from;
        }
        if source.0.is_some() {
            self.source = source;
        }
    }

    /// Add the fields that are set to `debug`, redacted as its `Debug`
    /// describes. [`Error`]'s `Debug` shows them inline.
    fn debug_fields(&self, debug: &mut fmt::DebugStruct<'_, '_>) {
        // Destructured so a new field needs a `Debug` rule to compile.
        let Self {
            provider,
            status,
            request_id,
            retry_after,
            server_retry_directive,
            provider_code,
            provider_type,
            param,
            message_origin,
            upstream_message,
            body: _,
            metadata,
            fallback_from,
            // Its text may hold upstream text or a URL.
            source: _,
        } = self;
        if let Some(provider) = provider {
            debug.field("provider", provider);
        }
        if let Some(status) = status {
            debug.field("status", status);
        }
        for (name, value) in [
            ("provider_code", provider_code),
            ("provider_type", provider_type),
            ("param", param),
        ] {
            if let Some(value) = value {
                debug.field(name, &log_token(value));
            }
        }
        if let Some(request_id) = request_id {
            debug.field("request_id", request_id);
        }
        if let Some(retry_after) = retry_after {
            debug.field("retry_after", retry_after);
        }
        if let Some(directive) = server_retry_directive {
            debug.field("server_retry_directive", directive);
        }
        if !message_origin.is_unknown() {
            debug.field("message_origin", message_origin);
        }
        if let Some(upstream) = upstream_message {
            debug.field("upstream_message", &Withheld::shown_by_display(upstream));
        }
        if let Some(body) = self.capped_body() {
            debug.field("body_len", &body.len());
        }
        if !metadata.is_empty() {
            debug.field("metadata", &MetadataKeys(metadata));
        }
        if let Some(earlier) = &fallback_from.0 {
            // Redacted like this error: `Error`'s own `Debug`.
            debug.field("fallback_from", earlier);
        }
    }
}

/// Shows the fields that are set, the body's and the upstream message's
/// lengths, never their text, the provider code, type and `param` only when
/// they are bounded tokens (see [`ErrorLog`]'s filter), and the metadata
/// keys without their values.
impl fmt::Debug for ErrorContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = f.debug_struct("ErrorContext");
        self.debug_fields(&mut debug);
        debug.finish()
    }
}

/// `Debug` text that stands in for upstream text, such as a provider's
/// message or a model's output: its length, never the text.
pub(crate) struct Withheld {
    bytes: usize,
    shown_by_display: bool,
}

impl Withheld {
    /// Text that the error's `Display` shows.
    pub(crate) fn shown_by_display(text: &str) -> Self {
        Self {
            bytes: text.len(),
            shown_by_display: true,
        }
    }

    /// Text that the error's `Display` does not show either.
    #[cfg(feature = "extract")]
    pub(crate) fn new(text: &str) -> Self {
        Self {
            bytes: text.len(),
            shown_by_display: false,
        }
    }
}

impl fmt::Debug for Withheld {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "<withheld {} bytes", self.bytes)?;
        if self.shown_by_display {
            f.write_str("; print with {err}")?;
        }
        f.write_str(">")
    }
}

/// `Debug` text for [`ErrorContext::metadata`]: its keys, not their values.
struct MetadataKeys<'a>(&'a ExtraMap);

impl fmt::Debug for MetadataKeys<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.0.keys()).finish()
    }
}

/// Where an [`Error::Unsupported`] refusal happened (see
/// [`Error::unsupported_stage`]).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum UnsupportedStage {
    /// The adapter refused the request before sending it: nothing reached
    /// the provider. Every refusal [`Degradations`](crate::adapter::Degradations)
    /// builds has this stage.
    BeforeSending,
    /// The provider refused the request it received, and the adapter mapped
    /// that reply ([`Error::unsupported_by_provider`]).
    Provider,
    /// Not stated ([`Error::unsupported`]): the request may have been sent.
    ///
    /// Also what an error stored by a newer allama with a stage this
    /// version does not know reads as.
    #[default]
    #[serde(other)]
    Unspecified,
}

impl UnsupportedStage {
    /// Stable snake_case name, as serialized.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::BeforeSending => "before_sending",
            Self::Provider => "provider",
            Self::Unspecified => "unspecified",
        }
    }

    fn is_unspecified(&self) -> bool {
        *self == Self::Unspecified
    }
}

impl fmt::Display for UnsupportedStage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Where an error's constructor message came from.
///
/// See [`ErrorContext`]'s message provenance section.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum MessageOrigin {
    /// Not known to be free of upstream text: redacted from [`ErrorLog`].
    ///
    /// The default, including for errors applications and tests build with
    /// the public constructors.
    #[default]
    Unknown,
    /// Written by library or adapter code, with no upstream text in it:
    /// shown in [`ErrorLog`]. Set with [`Error::authored`].
    Authored,
}

impl MessageOrigin {
    fn is_unknown(&self) -> bool {
        *self == Self::Unknown
    }
}

/// Kinds of streaming failure.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum StreamErrorKind {
    /// The stream ended before its terminal event (truncated). Retryable.
    Incomplete,
    /// The transport failed mid-stream (connection reset, read error). Retryable.
    Transport,
    /// A stream event could not be decoded.
    Decode,
    /// Stream events violated the event protocol (ordering, conflicts).
    Protocol,
    /// Any other streaming failure.
    Other,
}

impl StreamErrorKind {
    fn as_str(self) -> &'static str {
        match self {
            Self::Incomplete => "incomplete",
            Self::Transport => "transport",
            Self::Decode => "decode",
            Self::Protocol => "protocol",
            Self::Other => "other",
        }
    }
}

/// Unified error type for all model operations.
///
/// Provider-originated variants carry an [`ErrorContext`]. Variants are
/// `#[non_exhaustive]`: construct them with the lowercase constructors
/// ([`Error::timeout`], [`Error::rate_limited`], ...) and the `with_*`
/// builders, and match them with `..`.
///
/// # Building errors
///
/// One rule covers the context builders: **`with_*` takes the value**, and
/// each field an upstream reply may lack also has a **`with_*_opt`** twin
/// that takes an `Option` and keeps the current value on `None`, so adapter
/// code passes parsed headers straight through. Text twins take an
/// `Option<&str>`: a header passes as it is, an owned field with
/// `.as_deref()`, and a bare `None` compiles. The provider and the status
/// have no `_opt` form: the adapter always knows them.
///
/// ```
/// use std::time::Duration;
///
/// use allama::Error;
///
/// // Application and test code: values.
/// let err = Error::rate_limited("HTTP 429")
///     .with_status(429)
///     .with_retry_after(Duration::from_secs(30))
///     .with_request_id("req_1");
/// assert_eq!(err.retry_after(), Some(Duration::from_secs(30)));
///
/// // Adapter code: whatever the response had.
/// let header: Option<&str> = None;
/// let err = err.with_request_id_opt(header);
/// assert_eq!(err.request_id(), Some("req_1"));
/// ```
///
/// # Handling errors
///
/// The variants are the error kinds: match on them to decide what to do.
/// For logs and metrics, [`kind_name`](Self::kind_name) gives the variant's
/// stable snake_case name, and [`as_log`](Self::as_log) a redacted view that
/// keeps prompts and provider text out.
///
/// [`Error::Unsupported`] is the application's mistake or the end user's,
/// by what it refused ([`unsupported_subject`](Self::unsupported_subject)):
/// a [`Subject::Content`] is a part of the conversation, such as an image
/// the user attached for a text-only model, so it answers like a bad
/// request; anything else is a feature the application chose, a
/// configuration fault.
///
/// A [`FallbackProvider`](crate::wrappers::FallbackProvider) that fails on
/// every side returns the last side's error as itself: same variant, status
/// and kind, so the same `match` handles a failed failover. The errors the
/// failover tried before it are kept on it, newest first: walk them with
/// [`fallback_from`](Self::fallback_from). [`as_log`](Self::as_log) logs
/// them too, and `Display` shows only the error itself.
///
/// ```
/// use allama::adapter::Degradations;
/// use allama::{Error, RequestField, Subject, UnsupportedPolicy};
///
/// fn http_status(err: &Error) -> u16 {
///     // One label per kind, safe for a metric or a log field.
///     let kind = err.kind_name();
///     // The redacted log includes the errors a failover tried first.
///     eprintln!("chat failed: kind={kind} {}", err.as_log());
///
///     match err {
///         // Content the end user sent that the model cannot take, such as
///         // an image for a text-only model: their request.
///         Error::Unsupported { .. }
///             if matches!(err.unsupported_subject(), Some(Subject::Content { .. })) =>
///         {
///             400
///         }
///         // A setup mistake: a missing key, a model the provider does not
///         // have, or a feature the application asks for that the model
///         // lacks.
///         Error::Configuration { .. } | Error::ModelNotFound { .. } | Error::Unsupported { .. } => 500,
///         Error::ContextLengthExceeded { .. } => 413,
///         Error::ContentFiltered { .. } => 422,
///         Error::InvalidRequest { .. } => 400,
///         Error::RateLimited { .. } => 429,
///         // Refused by the application's own policy, such as a budget.
///         Error::Rejected { .. } => 403,
///         _ if err.is_retryable() => 503,
///         // Rejected credentials, an exhausted quota, other provider failures.
///         _ => 502,
///     }
/// }
///
/// let err = Error::rate_limited("HTTP 429").with_status(429);
/// assert_eq!(err.kind_name(), "rate_limited");
/// assert_eq!(http_status(&err), 429);
///
/// // What a failed failover returns: the backup's error, with the
/// // primary's behind it.
/// let failover = err.with_fallback_from(Error::overloaded("HTTP 529"));
/// assert_eq!(failover.kind_name(), "rate_limited");
/// assert_eq!(http_status(&failover), 429);
/// assert!(matches!(failover.fallback_from(), Some(Error::Overloaded { .. })));
///
/// // `Unsupported` maps by what was refused.
/// let degradations = Degradations::new("demo", UnsupportedPolicy::Warn);
/// let tools = degradations.unsupported(RequestField::Tools, "no tool calls");
/// assert_eq!(http_status(&tools), 500);
/// let image = Subject::Content { message: 0, block: Some(1) };
/// assert_eq!(http_status(&degradations.reject(image, "no image input")), 400);
/// ```
///
/// # Printing errors
///
/// `Display` (`{err}`) shows everything, including the provider's own
/// text ([`upstream_message`](Self::upstream_message)), which may echo
/// prompts or input values. `Debug` (`{err:?}`) is compact: the variant,
/// its message, and the context fields that are set. It withholds the
/// provider's text, the response body, and the model's output in an
/// [`Error::Extract`], and shows only their lengths:
///
/// ```text
/// InvalidRequest { message: "HTTP 400", provider: "openai", status: 400, upstream_message: <withheld 69 bytes; print with {err}> }
/// ```
///
/// So `tracing::error!(?err)` keeps prompts out of logs.
/// [`as_log`](Self::as_log) is the redacted view for structured logs.
///
/// A `main` that returns `allama::Result<()>` or `Result<(), Box<dyn
/// std::error::Error>>` prints a failure with this `Debug`, so the
/// provider's explanation is withheld there. (`anyhow::Result` shows it:
/// anyhow's `Debug` prints the error's `Display` and its causes.) Print it
/// with `Display` to see it:
///
/// ```no_run
/// # fn run() -> allama::Result<()> { Ok(()) }
/// fn main() {
///     if let Err(err) = run() {
///         eprintln!("error: {err}");
///         std::process::exit(1);
///     }
/// }
/// ```
///
/// An error serializes, for logs and job queues. Its names are pinned (the
/// kind, and the persisted names it holds), while its shape is
/// same-version (see
/// [Serialization and stability](crate#serialization-and-stability)).
///
#[cfg_attr(
    not(feature = "extract"),
    doc = "[`Error::Extract`]: crate#cargo-features"
)]
#[derive(Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Error {
    /// The request timed out: the client's own timeout, or a server or
    /// gateway that gave up (HTTP `408`, `504`, or Cloudflare's `524`).
    ///
    /// Retryable. A gateway timeout can come after the model ran, so the
    /// request may still have been processed and billed.
    #[non_exhaustive]
    Timeout {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// No response was received: connection refused, DNS or TLS failure, or
    /// the connection dropped before a response arrived.
    ///
    /// The request may or may not have reached the provider: a connection
    /// reset after the request was written cannot be told apart from one
    /// that was never sent. Do not assume it was not processed or billed.
    ///
    /// Retryable, so a retry may repeat a request the provider already
    /// processed. Distinct from [`Error::Provider`], which means the provider
    /// answered with a failure, and from [`Error::Timeout`].
    #[non_exhaustive]
    Transport {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// Authentication or authorization failed.
    #[non_exhaustive]
    Auth {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// The client is misconfigured: a missing credential, an unknown provider
    /// key, a malformed model address, or a missing runtime binding.
    ///
    /// Raised locally, before any request reaches a provider. Not retryable
    /// and not eligible for fallback: the same configuration fails the same
    /// way every time, and failing over would hide the mistake. Distinct from
    /// [`Error::Auth`], which means the provider rejected the credentials it
    /// was given.
    #[non_exhaustive]
    Configuration {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// A wrapper or the application refused the call before any provider
    /// saw it: a tenant budget, a policy or moderation pre-check, a guard on
    /// the request's content.
    ///
    /// Not retryable and never fails over: the same call is refused the same
    /// way, and no provider is to blame. Distinct from
    /// [`Error::InvalidRequest`] (the request is malformed) and
    /// [`Error::QuotaExhausted`] (a provider's account is out of credit).
    /// Build it with [`Error::rejected`], and use
    /// [`with_provider_code`](Error::with_provider_code) for a reason code
    /// the application branches on.
    ///
    /// Do not set a provider on it with
    /// [`with_provider`](Error::with_provider): the provider is the one that
    /// served the call, and a refused call had none. `Display` would then
    /// read `<provider>: rejected: ...`, and a span's
    /// `allama.response.provider` would blame an upstream that was never
    /// called. Put the provider or model the policy was about in the
    /// message, or in your own error type behind
    /// [`with_source`](Error::with_source).
    ///
    /// ```
    /// use allama::Error;
    ///
    /// let err = Error::rejected("tenant budget exhausted")
    ///     .authored()
    ///     .with_provider_code("tenant_budget_exhausted");
    /// assert_eq!(err.kind_name(), "rejected");
    /// assert!(!err.is_retryable());
    /// assert!(!allama::wrappers::should_fallback(&err));
    /// ```
    #[non_exhaustive]
    Rejected {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// Rate limited: back off and retry the same provider.
    #[non_exhaustive]
    RateLimited {
        /// Human-readable description.
        message: String,
        /// Diagnostic context, including `retry_after` when reported.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// Quota, credit, or spend limit exhausted. Retrying will not help until
    /// the account changes.
    #[non_exhaustive]
    QuotaExhausted {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// The provider is overloaded or temporarily unavailable. Distinct from
    /// rate limiting: consider failing over to another provider.
    #[non_exhaustive]
    Overloaded {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// A provider failure outside a more specific category.
    ///
    /// `context.status` is `Some` for HTTP error responses and `None` for
    /// failures a provider reported without one, such as an error inside a
    /// stream. Retryable without a status or with a 5xx one, except `501`
    /// (Not Implemented) and `505` (HTTP Version Not Supported): the server
    /// answers those the same way every time. A `499` (the client closed the
    /// request) is one, so it is neither retried nor reported as a rejected
    /// request.
    #[non_exhaustive]
    Provider {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// The requested feature is not supported by this provider or model.
    ///
    /// It says what was refused ([`subject`](Error::unsupported_subject))
    /// and where ([`stage`](Error::unsupported_stage)). Adapters refuse
    /// before sending through [`Degradations`](crate::adapter::Degradations)
    /// (stage [`BeforeSending`](UnsupportedStage::BeforeSending)), and map a
    /// provider's own refusal with [`Error::unsupported_by_provider`]
    /// (stage [`Provider`](UnsupportedStage::Provider)).
    /// [`Error::unsupported`] states neither.
    #[non_exhaustive]
    Unsupported {
        /// Human-readable description.
        message: String,
        /// What was refused: a request field, or a content block or part,
        /// when the refusal names one. Read it with
        /// [`unsupported_subject`](Error::unsupported_subject) or
        /// [`unsupported_field`](Error::unsupported_field).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        subject: Option<Subject>,
        /// Where the refusal happened. Read it with
        /// [`unsupported_stage`](Error::unsupported_stage).
        #[serde(default, skip_serializing_if = "UnsupportedStage::is_unspecified")]
        stage: UnsupportedStage,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// The request was invalid.
    #[non_exhaustive]
    InvalidRequest {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// The requested model does not exist for this provider.
    #[non_exhaustive]
    ModelNotFound {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// The provider returned a response the adapter could not interpret,
    /// such as a success response whose body is not JSON (a proxy's HTML
    /// page) or not of the expected shape.
    ///
    /// Carries the provider, the status and the request id when the
    /// response had them. Not retryable and not eligible for fallback.
    #[non_exhaustive]
    UnexpectedResponse {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// The input exceeds the model's context window. Truncate or summarize
    /// the conversation and retry.
    ///
    /// The same limit reached while the model writes is not an error: the
    /// response arrives with
    /// [`FinishReason::ContextWindow`](crate::FinishReason::ContextWindow)
    /// (or `Length`, on providers that do not tell the two apart), and the
    /// remedy is the same.
    #[non_exhaustive]
    ContextLengthExceeded {
        /// Human-readable description.
        message: String,
        /// The context or input-token limit the provider reported, if it
        /// reported one. Most providers report the model's context window;
        /// Gemini reports its input-token limit.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        context_window: Option<u64>,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// Content was blocked by provider safety systems.
    #[non_exhaustive]
    ContentFiltered {
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// A streaming failure.
    #[non_exhaustive]
    Stream {
        /// What went wrong.
        kind: StreamErrorKind,
        /// Human-readable description.
        message: String,
        /// Diagnostic context.
        #[serde(default, skip_serializing_if = "context_is_empty")]
        context: Box<ErrorContext>,
    },

    /// Serialization or deserialization of the caller's own data failed,
    /// such as a typed option or provider data entry.
    ///
    /// What a provider sent that cannot be decoded is
    /// [`Error::UnexpectedResponse`] instead. Build it with
    /// [`Error::serialization`].
    ///
    /// It carries no [`ErrorContext`], so it cannot record the errors a
    /// failover tried before it (see
    /// [`with_fallback_from`](Error::with_fallback_from)).
    #[non_exhaustive]
    Serialization {
        /// The underlying failure.
        error: SerializationError,
    },

    /// Structured extraction produced no value: no mode could be used, the
    /// request conflicted with the selected mode, or the output could not be
    /// used.
    ///
    /// The [`ExtractFailure`](crate::extract::ExtractFailure) carries why,
    /// the last provider response, and the pass record (calls made, summed
    /// usage). Not retryable and not eligible for fallback. Provider errors
    /// during an extraction are returned as themselves, not wrapped here;
    /// their context carries the pass record instead
    /// ([`ExtractionMetadata::from_error`](crate::extract::ExtractionMetadata::from_error)).
    ///
    /// Build it with [`Error::extract`]. Every variant is
    /// `#[non_exhaustive]`, so a struct expression does not compile
    /// (`E0639`) outside this crate. To return the error after inspecting
    /// it, match on `&result`, or bind it whole with
    /// `err @ Error::Extract { .. }`; to rebuild one you took apart, use
    /// `Error::extract(*failure)`.
    #[cfg(feature = "extract")]
    #[non_exhaustive]
    Extract {
        /// The failed extraction.
        failure: Box<crate::extract::ExtractFailure>,
    },
}

macro_rules! constructors {
    ($($(#[$doc:meta])* $fn_name:ident => $variant:ident),* $(,)?) => {
        $(
            $(#[$doc])*
            ///
            /// The message's origin is [`MessageOrigin::Unknown`], so
            /// [`ErrorLog`] leaves it out. Mark text your code wrote, with no
            /// upstream text in it, with [`authored`](Self::authored).
            #[must_use]
            pub fn $fn_name(message: impl Into<String>) -> Self {
                Self::$variant {
                    message: message.into(),
                    context: Box::default(),
                }
            }
        )*
    };
}

impl Error {
    constructors! {
        /// Build an [`Error::Timeout`].
        timeout => Timeout,
        /// Build an [`Error::Transport`].
        transport => Transport,
        /// Build an [`Error::Auth`].
        auth => Auth,
        /// Build an [`Error::Configuration`].
        configuration => Configuration,
        /// Build an [`Error::Rejected`]: a wrapper or the application
        /// refused the call before any provider saw it.
        rejected => Rejected,
        /// Build an [`Error::RateLimited`].
        rate_limited => RateLimited,
        /// Build an [`Error::QuotaExhausted`].
        quota_exhausted => QuotaExhausted,
        /// Build an [`Error::Overloaded`].
        overloaded => Overloaded,
        /// Build an [`Error::Provider`].
        ///
        /// For failures a provider reported. A wrapper or middleware must
        /// not use it for its own failures: a `Provider` error without a
        /// status is retryable and eligible for fallback, so a retry or
        /// fallback wrapper around it would repeat a call that cannot
        /// succeed. Use the variant that says what went wrong instead, such
        /// as [`Error::configuration`] for a wrapper set up wrong.
        provider => Provider,
        /// Build an [`Error::InvalidRequest`].
        invalid_request => InvalidRequest,
        /// Build an [`Error::ModelNotFound`].
        model_not_found => ModelNotFound,
        /// Build an [`Error::UnexpectedResponse`].
        unexpected_response => UnexpectedResponse,
        /// Build an [`Error::ContentFiltered`].
        content_filtered => ContentFiltered,
    }

    /// Build an [`Error::Unsupported`] that names no subject, at an
    /// [unspecified](UnsupportedStage::Unspecified) stage.
    ///
    /// The stage is unspecified, so a caller must assume the request may
    /// have been sent. Adapters refuse before sending through
    /// [`Degradations`](crate::adapter::Degradations) instead, which records the
    /// subject and the [`BeforeSending`](UnsupportedStage::BeforeSending)
    /// stage, and map an upstream refusal with
    /// [`unsupported_by_provider`](Self::unsupported_by_provider).
    ///
    /// The message's origin is [`MessageOrigin::Unknown`], so [`ErrorLog`]
    /// leaves it out. Mark text your code wrote, with no upstream text in
    /// it, with [`authored`](Self::authored).
    #[must_use]
    pub fn unsupported(message: impl Into<String>) -> Self {
        Self::Unsupported {
            message: message.into(),
            subject: None,
            stage: UnsupportedStage::Unspecified,
            context: Box::default(),
        }
    }

    /// Build an [`Error::Unsupported`] for a refusal the provider itself
    /// returned: stage [`Provider`](UnsupportedStage::Provider), refusing
    /// `subject` when the adapter can name it.
    ///
    /// For upstream error mapping. Map only a reply the adapter recognizes
    /// as a refusal of something the caller asked for (a request field it
    /// resolves through its own table of wire paths, with a fixture that
    /// pins the reply); a parameter the adapter itself got wrong is an
    /// [`InvalidRequest`](Error::InvalidRequest). The message's origin is
    /// [`MessageOrigin::Unknown`], as for [`unsupported`](Self::unsupported).
    ///
    /// ```
    /// use allama::{Error, RequestField, Subject, UnsupportedStage};
    ///
    /// let err = Error::unsupported_by_provider(
    ///     Some(Subject::Field { field: RequestField::Temperature }),
    ///     "the model does not support `temperature`",
    /// )
    /// .with_status(400);
    /// assert_eq!(err.unsupported_stage(), Some(UnsupportedStage::Provider));
    /// assert_eq!(err.unsupported_field(), Some(RequestField::Temperature));
    /// ```
    #[must_use]
    pub fn unsupported_by_provider(subject: Option<Subject>, message: impl Into<String>) -> Self {
        Self::Unsupported {
            message: message.into(),
            subject,
            stage: UnsupportedStage::Provider,
            context: Box::default(),
        }
    }

    /// An [`Error::Unsupported`] refusing `subject` before sending, for
    /// [`Degradations`](crate::adapter::Degradations).
    pub(crate) fn unsupported_before_sending(
        message: impl Into<String>,
        subject: Option<Subject>,
    ) -> Self {
        Self::Unsupported {
            message: message.into(),
            subject,
            stage: UnsupportedStage::BeforeSending,
            context: Box::default(),
        }
    }

    /// Build an [`Error::ContextLengthExceeded`].
    ///
    /// The message's origin is [`MessageOrigin::Unknown`], as for
    /// [`invalid_request`](Self::invalid_request).
    #[must_use]
    pub fn context_length_exceeded(
        message: impl Into<String>,
        context_window: Option<u64>,
    ) -> Self {
        Self::ContextLengthExceeded {
            message: message.into(),
            context_window,
            context: Box::default(),
        }
    }

    /// Build an [`Error::Stream`].
    ///
    /// The message's origin is [`MessageOrigin::Unknown`], as for
    /// [`invalid_request`](Self::invalid_request).
    #[must_use]
    pub fn stream(kind: StreamErrorKind, message: impl Into<String>) -> Self {
        Self::Stream {
            kind,
            message: message.into(),
            context: Box::default(),
        }
    }

    /// Build an [`Error::Stream`] for event-protocol violations.
    #[must_use]
    pub fn stream_protocol(message: impl Into<String>) -> Self {
        Self::stream(StreamErrorKind::Protocol, message)
    }

    /// Build an [`Error::Stream`] for undecodable stream data.
    #[must_use]
    pub fn stream_decode(message: impl Into<String>) -> Self {
        Self::stream(StreamErrorKind::Decode, message)
    }

    /// Build an [`Error::Serialization`] from any compatible source.
    #[must_use]
    pub fn serialization(err: impl Into<SerializationError>) -> Self {
        Self::Serialization { error: err.into() }
    }

    /// Build an [`Error::Extract`].
    #[cfg(feature = "extract")]
    #[must_use]
    pub fn extract(failure: crate::extract::ExtractFailure) -> Self {
        Self::Extract {
            failure: Box::new(failure),
        }
    }

    /// The error tried before this one, when this error ended a failover.
    ///
    /// A [`FallbackProvider`](crate::wrappers::FallbackProvider) whose sides
    /// all fail returns the last side's error, and keeps the earlier ones
    /// on it, newest first: this is the side tried just before, and calling
    /// `fallback_from` on the result walks back to the first primary. The
    /// returned error's own variant, status and kind are the last side's,
    /// so code that matches on the error needs no special case for a
    /// failover; read the chain to report or log what failed first.
    ///
    /// `None` for an error that ended no failover, and for the variants
    /// without a context ([`Error::Serialization`], `Error::Extract`):
    /// when the last side fails with one of them, the earlier errors are
    /// dropped and the failover's `allama::fallback` event is their only
    /// record.
    ///
    /// ```
    /// use allama::Error;
    ///
    /// // A primary, then two backups: what the chain of failovers returns.
    /// let err = Error::rate_limited("HTTP 429")
    ///     .with_status(429)
    ///     .with_fallback_from(Error::overloaded("secondary"))
    ///     .with_fallback_from(Error::timeout("primary"));
    /// assert!(matches!(err, Error::RateLimited { .. }));
    /// assert_eq!(err.status(), Some(429));
    ///
    /// let tried: Vec<_> = std::iter::successors(err.fallback_from(), |e| e.fallback_from())
    ///     .map(Error::kind_name)
    ///     .collect();
    /// assert_eq!(tried, ["overloaded", "timeout"]);
    /// ```
    #[must_use]
    pub fn fallback_from(&self) -> Option<&Error> {
        self.context()?.fallback_from.0.as_deref()
    }

    /// Record `earlier` as an error tried before this one, at the tail of
    /// the [`fallback_from`](Self::fallback_from) chain: after every error
    /// this one already records, since it came before them.
    ///
    /// This is how [`FallbackProvider`](crate::wrappers::FallbackProvider)
    /// builds the error it returns, and how a third-party failover or
    /// hedging wrapper builds the same shape: return the last error it got,
    /// with each earlier one attached, newest first. `earlier`'s own chain
    /// comes with it. Nesting composes: when the last side is itself a
    /// failover whose error already records its own primary, the outer
    /// primary goes after it.
    ///
    /// Has no effect on variants without a context ([`Error::Serialization`],
    /// `Error::Extract`), and the chain ends at an error that has none.
    ///
    /// ```
    /// use allama::Error;
    ///
    /// let inner = Error::rate_limited("backup").with_fallback_from(Error::overloaded("secondary"));
    /// let outer = inner.with_fallback_from(Error::timeout("primary"));
    /// assert!(matches!(outer.fallback_from(), Some(Error::Overloaded { .. })));
    /// assert!(matches!(
    ///     outer.fallback_from().and_then(Error::fallback_from),
    ///     Some(Error::Timeout { .. })
    /// ));
    /// ```
    #[must_use]
    pub fn with_fallback_from(mut self, earlier: Error) -> Self {
        self.attach_fallback_from(earlier);
        self
    }

    fn attach_fallback_from(&mut self, earlier: Error) {
        let Some(context) = self.context_mut() else {
            return;
        };
        match &mut context.fallback_from.0 {
            Some(next) => Arc::make_mut(next).attach_fallback_from(earlier),
            slot @ None => *slot = Some(Arc::new(earlier)),
        }
    }

    /// Borrow the diagnostic context, when this variant carries one.
    #[must_use]
    pub fn context(&self) -> Option<&ErrorContext> {
        match self {
            Error::Timeout { context, .. }
            | Error::Transport { context, .. }
            | Error::Auth { context, .. }
            | Error::Configuration { context, .. }
            | Error::Rejected { context, .. }
            | Error::RateLimited { context, .. }
            | Error::QuotaExhausted { context, .. }
            | Error::Overloaded { context, .. }
            | Error::Provider { context, .. }
            | Error::Unsupported { context, .. }
            | Error::InvalidRequest { context, .. }
            | Error::ModelNotFound { context, .. }
            | Error::UnexpectedResponse { context, .. }
            | Error::ContextLengthExceeded { context, .. }
            | Error::ContentFiltered { context, .. }
            | Error::Stream { context, .. } => Some(context.as_ref()),
            Error::Serialization { .. } => None,
            #[cfg(feature = "extract")]
            Error::Extract { .. } => None,
        }
    }

    /// Mutably borrow the diagnostic context, when this variant carries one.
    ///
    /// Use it to change or clear one field, or to replace the whole context
    /// (`*context = ErrorContext::new()`, which also drops the
    /// [`fallback_from`](Self::fallback_from) chain), which
    /// [`with_context`](Self::with_context) never does because it merges:
    ///
    /// ```
    /// use allama::{Error, ErrorContext};
    ///
    /// let mut err = Error::overloaded("fallback").with_request_id("req_1");
    /// if let Some(context) = err.context_mut() {
    ///     context.request_id = Some("req_123".into());
    /// }
    /// assert_eq!(err.request_id(), Some("req_123"));
    ///
    /// if let Some(context) = err.context_mut() {
    ///     *context = ErrorContext::new();
    /// }
    /// assert_eq!(err.request_id(), None);
    /// ```
    pub fn context_mut(&mut self) -> Option<&mut ErrorContext> {
        match self {
            Error::Timeout { context, .. }
            | Error::Transport { context, .. }
            | Error::Auth { context, .. }
            | Error::Configuration { context, .. }
            | Error::Rejected { context, .. }
            | Error::RateLimited { context, .. }
            | Error::QuotaExhausted { context, .. }
            | Error::Overloaded { context, .. }
            | Error::Provider { context, .. }
            | Error::Unsupported { context, .. }
            | Error::InvalidRequest { context, .. }
            | Error::ModelNotFound { context, .. }
            | Error::UnexpectedResponse { context, .. }
            | Error::ContextLengthExceeded { context, .. }
            | Error::ContentFiltered { context, .. }
            | Error::Stream { context, .. } => Some(context.as_mut()),
            Error::Serialization { .. } => None,
            #[cfg(feature = "extract")]
            Error::Extract { .. } => None,
        }
    }

    fn map_context(mut self, f: impl FnOnce(&mut ErrorContext)) -> Self {
        if let Some(context) = self.context_mut() {
            f(context);
        }
        self
    }

    /// Merge `context` into the diagnostic context.
    ///
    /// - Each `Some` field of `context` overwrites the error's value; a
    ///   `None` field keeps it. So the order of `with_context` and the
    ///   single-field builders (`with_status`, `with_provider`, …) does not
    ///   matter, except where both set the same field: the later call wins.
    /// - [`metadata`](ErrorContext::metadata) merges by key, and the keys of
    ///   `context` win.
    /// - The body is capped at [`ErrorContext::MAX_BODY_BYTES`].
    /// - [`message_origin`](ErrorContext::message_origin) is never changed.
    ///   Only [`authored`](Self::authored) marks the constructor message as
    ///   written by the adapter, so a context built elsewhere cannot let
    ///   upstream text into [`ErrorLog`], and merging after `authored()`
    ///   keeps the mark.
    ///
    /// To clear a field or replace the whole context, write through
    /// [`context_mut`](Self::context_mut).
    ///
    /// ```
    /// use allama::{Error, ErrorContext, MessageOrigin};
    ///
    /// let mut headers = ErrorContext::new();
    /// headers.status = Some(429);
    /// headers.request_id = Some("req_1".into());
    ///
    /// let err = Error::rate_limited("HTTP 429")
    ///     .authored()
    ///     .with_provider("acme")
    ///     .with_context(headers);
    /// assert_eq!(err.status(), Some(429));
    /// assert_eq!(err.request_id(), Some("req_1"));
    /// assert_eq!(err.context().unwrap().provider.as_deref(), Some("acme"));
    /// assert_eq!(err.message_origin(), MessageOrigin::Authored);
    /// ```
    #[must_use]
    pub fn with_context(self, context: ErrorContext) -> Self {
        self.map_context(|c| c.merge(context))
    }

    /// Set the provider identity.
    #[must_use]
    pub fn with_provider(self, provider: impl Into<String>) -> Self {
        let provider = provider.into();
        self.map_context(|c| c.provider = Some(provider))
    }

    /// Set the HTTP status code.
    #[must_use]
    pub fn with_status(self, status: u16) -> Self {
        self.map_context(|c| c.status = Some(status))
    }

    /// Set the provider request ID.
    #[must_use]
    pub fn with_request_id(self, request_id: impl Into<String>) -> Self {
        let request_id = request_id.into();
        self.map_context(|c| c.request_id = Some(request_id))
    }

    /// Set the provider request ID when there is one; `None` keeps the
    /// current value. The `_opt` form of
    /// [`with_request_id`](Self::with_request_id).
    #[must_use]
    pub fn with_request_id_opt(self, request_id: Option<&str>) -> Self {
        match request_id {
            Some(request_id) => self.with_request_id(request_id),
            None => self,
        }
    }

    /// Set the retry-after hint.
    #[must_use]
    pub fn with_retry_after(self, retry_after: Duration) -> Self {
        self.with_retry_after_opt(Some(retry_after))
    }

    /// Set the retry-after hint when there is one; `None` keeps the current
    /// value. The `_opt` form of [`with_retry_after`](Self::with_retry_after).
    #[must_use]
    pub fn with_retry_after_opt(self, retry_after: Option<Duration>) -> Self {
        self.map_context(|c| overwrite(&mut c.retry_after, retry_after))
    }

    /// Set the server's retry directive (see
    /// [`ErrorContext::server_retry_directive`]).
    #[must_use]
    pub fn with_server_retry_directive(self, directive: bool) -> Self {
        self.with_server_retry_directive_opt(Some(directive))
    }

    /// Set the server's retry directive when it sent one; `None` keeps the
    /// current value. The `_opt` form of
    /// [`with_server_retry_directive`](Self::with_server_retry_directive).
    #[must_use]
    pub fn with_server_retry_directive_opt(self, directive: Option<bool>) -> Self {
        self.map_context(|c| overwrite(&mut c.server_retry_directive, directive))
    }

    /// Set the provider error code (see [`ErrorContext::provider_code`]).
    #[must_use]
    pub fn with_provider_code(self, code: impl Into<String>) -> Self {
        let code = code.into();
        self.map_context(|c| c.provider_code = Some(code))
    }

    /// Set the provider error code when there is one; `None` keeps the
    /// current value. The `_opt` form of
    /// [`with_provider_code`](Self::with_provider_code).
    #[must_use]
    pub fn with_provider_code_opt(self, code: Option<&str>) -> Self {
        match code {
            Some(code) => self.with_provider_code(code),
            None => self,
        }
    }

    /// Set the provider error type (see [`ErrorContext::provider_type`]).
    #[must_use]
    pub fn with_provider_type(self, provider_type: impl Into<String>) -> Self {
        let provider_type = provider_type.into();
        self.map_context(|c| c.provider_type = Some(provider_type))
    }

    /// Set the provider error type when there is one; `None` keeps the
    /// current value. The `_opt` form of
    /// [`with_provider_type`](Self::with_provider_type).
    #[must_use]
    pub fn with_provider_type_opt(self, provider_type: Option<&str>) -> Self {
        match provider_type {
            Some(provider_type) => self.with_provider_type(provider_type),
            None => self,
        }
    }

    /// Set the raw response body, capped at [`ErrorContext::MAX_BODY_BYTES`].
    #[must_use]
    pub fn with_body(self, body: impl Into<String>) -> Self {
        let mut body = body.into();
        body.truncate(cap_body(&body).len());
        self.map_context(|c| c.body = Some(body))
    }

    /// Set the raw response body when there is one, capped at
    /// [`ErrorContext::MAX_BODY_BYTES`]; `None` keeps the current value.
    /// The `_opt` form of [`with_body`](Self::with_body).
    #[must_use]
    pub fn with_body_opt(self, body: Option<&str>) -> Self {
        match body {
            Some(body) => self.with_body(cap_body(body)),
            None => self,
        }
    }

    /// Set the provider's own error text, such as the `message` of a JSON
    /// error envelope. Empty text is ignored.
    ///
    /// Upstream text may echo prompts or input values, so the redacted
    /// [`ErrorLog`] leaves it out unless asked for with
    /// [`ErrorLog::with_message`]. `Display` shows it after the constructor
    /// message.
    ///
    /// ```
    /// use allama::Error;
    ///
    /// let err = Error::invalid_request("HTTP 400")
    ///     .authored()
    ///     .with_status(400)
    ///     .with_upstream_message("bad value 'my SSN is 123-45-6789'");
    /// assert_eq!(err.as_log().to_string(), r#"kind=invalid_request status=400 message="HTTP 400""#);
    /// assert!(err.to_string().contains("123-45-6789"));
    /// ```
    #[must_use]
    pub fn with_upstream_message(self, message: impl Into<String>) -> Self {
        let message = message.into();
        if message.trim().is_empty() {
            return self;
        }
        self.map_context(|c| c.upstream_message = Some(message))
    }

    /// Set the provider's own error text when there is one; `None` and
    /// empty text keep the current value. The `_opt` form of
    /// [`with_upstream_message`](Self::with_upstream_message).
    #[must_use]
    pub fn with_upstream_message_opt(self, message: Option<&str>) -> Self {
        match message {
            Some(message) => self.with_upstream_message(message),
            None => self,
        }
    }

    /// Set the path of the rejected request field (see
    /// [`ErrorContext::param`]).
    #[must_use]
    pub fn with_param(self, param: impl Into<String>) -> Self {
        let param = param.into();
        self.map_context(|c| c.param = Some(param))
    }

    /// Set the path of the rejected request field when the provider names
    /// one; `None` keeps the current value. The `_opt` form of
    /// [`with_param`](Self::with_param).
    #[must_use]
    pub fn with_param_opt(self, param: Option<&str>) -> Self {
        match param {
            Some(param) => self.with_param(param),
            None => self,
        }
    }

    /// Attach the underlying cause, returned from
    /// [`source`](std::error::Error::source): a typed error a caller may
    /// downcast, such as an I/O error behind a transport failure or a
    /// policy error behind an [`Error::Rejected`].
    ///
    /// The source is kept in memory only: it is not serialized, compared,
    /// or shown by `Debug`, `Display` or [`ErrorLog`]. `Display` does not
    /// show it, so a report chain (`anyhow`'s `{:#}`) prints it once,
    /// after the error: keep its text out of the message. Its text may
    /// hold whatever the cause's own `Display` says, such as a URL, so
    /// attach only causes that are safe to print where the chain is
    /// printed. Has no effect on variants without a context
    /// ([`Error::Serialization`], [`Error::Extract`]).
    ///
    /// ```
    /// use std::error::Error as _;
    ///
    /// use allama::Error;
    ///
    /// #[derive(Debug)]
    /// struct OverBudget {
    ///     tenant: u32,
    /// }
    ///
    /// impl std::fmt::Display for OverBudget {
    ///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    ///         write!(f, "tenant {} is over its monthly budget", self.tenant)
    ///     }
    /// }
    ///
    /// impl std::error::Error for OverBudget {}
    ///
    /// let err = Error::rejected("budget guard").with_source(OverBudget { tenant: 7 });
    /// let cause = err.source().unwrap().downcast_ref::<OverBudget>().unwrap();
    /// assert_eq!(cause.tenant, 7);
    /// assert_eq!(err.to_string(), "rejected: budget guard");
    /// ```
    #[cfg_attr(
        not(feature = "extract"),
        doc = "[`Error::Extract`]: crate#cargo-features"
    )]
    #[must_use]
    pub fn with_source(
        self,
        source: impl Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
    ) -> Self {
        let source = ErrorSource(Some(Arc::from(source.into())));
        self.map_context(|c| c.source = source)
    }

    /// Mark the constructor message as written by library or adapter code,
    /// so the redacted [`ErrorLog`] shows it.
    ///
    /// Never call this on a message that contains upstream text: a
    /// provider's error message, a response body, or the text of a foreign
    /// error (an HTTP client's error can contain the request URL, and some
    /// APIs put the key in the query string). Put provider text in
    /// [`with_upstream_message`](Self::with_upstream_message) instead.
    ///
    /// ```
    /// use allama::Error;
    ///
    /// let upstream = "Invalid value: 'my SSN is 123-45-6789'";
    ///
    /// // Bad: the upstream text would reach the redacted log.
    /// let leaky = Error::invalid_request(upstream).authored();
    /// assert!(leaky.as_log().to_string().contains("123-45-6789"));
    ///
    /// // Good: authored text in the message, upstream text in its own slot.
    /// let err = Error::invalid_request("HTTP 400")
    ///     .authored()
    ///     .with_upstream_message(upstream);
    /// assert!(!err.as_log().to_string().contains("123-45-6789"));
    /// ```
    ///
    /// Has no effect on variants without a context
    /// ([`Error::Serialization`], [`Error::Extract`]).
    ///
    #[cfg_attr(
        not(feature = "extract"),
        doc = "[`Error::Extract`]: crate#cargo-features"
    )]
    #[must_use]
    pub fn authored(self) -> Self {
        self.map_context(|c| c.message_origin = MessageOrigin::Authored)
    }

    /// Put `prefix` and `": "` in front of the message, keeping everything
    /// else: the kind, the context and the message's origin.
    ///
    /// This is how a wrapper or application adds what it was doing to an
    /// error it passes on. Re-wrapping instead (`Error::provider(format!(
    /// "summarize: {err}"))`) loses the kind and the context, and with them
    /// the retry hint and the status: a `Provider` error without a status
    /// is retried and fails over.
    ///
    /// - The prefix is shown wherever the message is. When the message is
    ///   [`Authored`](MessageOrigin::Authored), that includes the redacted
    ///   [`ErrorLog`], so use text your code wrote: never a prompt, an input
    ///   value or upstream text.
    /// - An error that ended a failover takes the prefix itself; the
    ///   errors in its [`fallback_from`](Self::fallback_from) chain keep
    ///   their messages.
    /// - [`Error::Serialization`]: the prefix goes in front of the
    ///   underlying failure's text; the failure itself stays reachable
    ///   through [`SerializationError::get_ref`].
    /// - [`Error::Extract`] is returned unchanged: its failure is
    ///   structured and carries no free-form message.
    ///
    /// ```
    /// use std::time::Duration;
    ///
    /// use allama::{Error, MessageOrigin};
    ///
    /// let err = Error::rate_limited("HTTP 429")
    ///     .authored()
    ///     .with_status(429)
    ///     .with_retry_after(Duration::from_secs(2));
    ///
    /// let err = err.with_message_prefix("summarize step");
    /// assert!(matches!(err, Error::RateLimited { .. }));
    /// assert_eq!(err.message(), "summarize step: HTTP 429");
    /// assert_eq!(err.message_origin(), MessageOrigin::Authored);
    /// assert_eq!(err.retry_after(), Some(Duration::from_secs(2)));
    /// assert_eq!(
    ///     err.as_log().to_string(),
    ///     r#"kind=rate_limited status=429 message="summarize step: HTTP 429""#,
    /// );
    /// ```
    #[cfg_attr(
        not(feature = "extract"),
        doc = "[`Error::Extract`]: crate#cargo-features"
    )]
    #[must_use]
    pub fn with_message_prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix_message(&prefix.into());
        self
    }

    fn prefix_message(&mut self, prefix: &str) {
        match self {
            Error::Timeout { message, .. }
            | Error::Transport { message, .. }
            | Error::Auth { message, .. }
            | Error::Configuration { message, .. }
            | Error::Rejected { message, .. }
            | Error::RateLimited { message, .. }
            | Error::QuotaExhausted { message, .. }
            | Error::Overloaded { message, .. }
            | Error::Provider { message, .. }
            | Error::Unsupported { message, .. }
            | Error::InvalidRequest { message, .. }
            | Error::ModelNotFound { message, .. }
            | Error::UnexpectedResponse { message, .. }
            | Error::ContextLengthExceeded { message, .. }
            | Error::ContentFiltered { message, .. }
            | Error::Stream { message, .. } => *message = format!("{prefix}: {message}"),
            Error::Serialization { error } => error.prefix(prefix),
            #[cfg(feature = "extract")]
            Error::Extract { .. } => {}
        }
    }

    /// The constructor message, without the category prefix and without the
    /// [`upstream_message`](Self::upstream_message).
    ///
    /// Not redacted: use [`as_log`](Self::as_log) for logs and telemetry.
    #[must_use]
    pub fn message(&self) -> String {
        match self {
            Error::Timeout { message, .. }
            | Error::Transport { message, .. }
            | Error::Auth { message, .. }
            | Error::Configuration { message, .. }
            | Error::Rejected { message, .. }
            | Error::RateLimited { message, .. }
            | Error::QuotaExhausted { message, .. }
            | Error::Overloaded { message, .. }
            | Error::Provider { message, .. }
            | Error::Unsupported { message, .. }
            | Error::InvalidRequest { message, .. }
            | Error::ModelNotFound { message, .. }
            | Error::UnexpectedResponse { message, .. }
            | Error::ContextLengthExceeded { message, .. }
            | Error::ContentFiltered { message, .. }
            | Error::Stream { message, .. } => message.clone(),
            Error::Serialization { error } => error.to_string(),
            #[cfg(feature = "extract")]
            Error::Extract { failure } => failure.to_string(),
        }
    }

    /// HTTP status code, when known.
    #[must_use]
    pub fn status(&self) -> Option<u16> {
        self.context()?.status
    }

    /// Provider request ID, when known.
    #[must_use]
    pub fn request_id(&self) -> Option<&str> {
        self.context()?.request_id.as_deref()
    }

    /// Retry-after hint, when known.
    #[must_use]
    pub fn retry_after(&self) -> Option<Duration> {
        self.context()?.retry_after
    }

    /// The server's retry directive, when it sent one: `Some(true)` asks the
    /// client to retry, `Some(false)` not to (see
    /// [`ErrorContext::server_retry_directive`]).
    ///
    /// The default [`RetryPolicy`](crate::wrappers::RetryPolicy) follows it;
    /// [`is_retryable`](Self::is_retryable) does not, because it classifies
    /// by kind and status alone. A custom classifier decides for itself:
    ///
    /// ```
    /// use allama::Error;
    /// use allama::wrappers::RetryPolicy;
    ///
    /// // Retry what the server asks for, and auth failures (a rotated key).
    /// let policy = RetryPolicy::new().retry_if(|error: &Error| {
    ///     error
    ///         .server_retry_directive()
    ///         .unwrap_or_else(|| error.is_retryable() || error.is_auth())
    /// });
    /// let conflict = Error::invalid_request("HTTP 409")
    ///     .with_status(409)
    ///     .with_server_retry_directive(true);
    /// assert!(policy.should_retry(&conflict));
    /// ```
    #[must_use]
    pub fn server_retry_directive(&self) -> Option<bool> {
        self.context()?.server_retry_directive
    }

    /// The provider's own error text, when known (see
    /// [`with_upstream_message`](Self::with_upstream_message)).
    #[must_use]
    pub fn upstream_message(&self) -> Option<&str> {
        self.context()?.upstream_message.as_deref()
    }

    /// The path of the request field the provider rejected, when it named
    /// one (see [`ErrorContext::param`]).
    ///
    /// ```
    /// use allama::Error;
    ///
    /// let err = Error::invalid_request("HTTP 400")
    ///     .with_param("messages.[1].content")
    ///     .with_provider_code("invalid_value");
    /// assert_eq!(err.param(), Some("messages.[1].content"));
    /// assert_eq!(err.provider_code(), Some("invalid_value"));
    /// ```
    #[must_use]
    pub fn param(&self) -> Option<&str> {
        self.context()?.param.as_deref()
    }

    /// The provider's machine-readable error code, when it sent one (see
    /// [`ErrorContext::provider_code`]).
    #[must_use]
    pub fn provider_code(&self) -> Option<&str> {
        self.context()?.provider_code.as_deref()
    }

    /// What an [`Error::Unsupported`] refused, when the refusal names it: a
    /// request field ([`Subject::Field`]), or a content block or part
    /// ([`Subject::Content`], with the caller's indices).
    ///
    /// `None` for other variants and for refusals that name nothing.
    ///
    /// ```
    /// use allama::adapter::Degradations;
    /// use allama::{Error, Subject, UnsupportedPolicy};
    ///
    /// let degradations = Degradations::new("demo", UnsupportedPolicy::Warn);
    /// let image = Subject::Content { message: 0, block: Some(1) };
    /// let err = degradations.reject(image.clone(), "no image input");
    /// assert_eq!(err.unsupported_subject(), Some(&image));
    /// ```
    #[must_use]
    pub fn unsupported_subject(&self) -> Option<&Subject> {
        match self {
            Error::Unsupported { subject, .. } => subject.as_ref(),
            _ => None,
        }
    }

    /// The portable request field an [`Error::Unsupported`] refused: its
    /// [subject](Self::unsupported_subject) when that is a
    /// [`Subject::Field`].
    ///
    /// The field says what was refused, not whether anything was sent; read
    /// that from [`unsupported_stage`](Self::unsupported_stage).
    ///
    /// ```
    /// use allama::adapter::Degradations;
    /// use allama::{Error, RequestField, UnsupportedPolicy};
    ///
    /// let degradations = Degradations::new("demo", UnsupportedPolicy::Warn);
    /// let err = degradations.unsupported(RequestField::ResponseFormat, "no JSON Schema output");
    /// assert_eq!(err.unsupported_field(), Some(RequestField::ResponseFormat));
    /// assert_eq!(Error::unsupported("HTTP 400").unsupported_field(), None);
    /// ```
    #[must_use]
    pub fn unsupported_field(&self) -> Option<RequestField> {
        match self.unsupported_subject()? {
            Subject::Field { field } => Some(*field),
            _ => None,
        }
    }

    /// Where an [`Error::Unsupported`] was refused; `None` for other
    /// variants.
    ///
    /// [`BeforeSending`](UnsupportedStage::BeforeSending) means nothing
    /// reached the provider, so nothing was billed and failing over or
    /// changing the request costs nothing extra.
    /// [`Provider`](UnsupportedStage::Provider) means the provider refused
    /// the request it received. [`Unspecified`](UnsupportedStage::Unspecified)
    /// promises neither: treat the request as possibly sent.
    ///
    /// ```
    /// use allama::adapter::Degradations;
    /// use allama::{Error, RequestField, UnsupportedPolicy, UnsupportedStage};
    ///
    /// let degradations = Degradations::new("demo", UnsupportedPolicy::Warn);
    /// let err = degradations.unsupported(RequestField::Seed, "no seed");
    /// assert_eq!(err.unsupported_stage(), Some(UnsupportedStage::BeforeSending));
    /// assert_eq!(
    ///     Error::unsupported("HTTP 400").unsupported_stage(),
    ///     Some(UnsupportedStage::Unspecified)
    /// );
    /// assert_eq!(Error::timeout("slow").unsupported_stage(), None);
    /// ```
    #[must_use]
    pub fn unsupported_stage(&self) -> Option<UnsupportedStage> {
        match self {
            Error::Unsupported { stage, .. } => Some(*stage),
            _ => None,
        }
    }

    /// Where the constructor message came from. [`MessageOrigin::Unknown`]
    /// for variants without a context.
    #[must_use]
    pub fn message_origin(&self) -> MessageOrigin {
        self.context()
            .map_or(MessageOrigin::Unknown, |c| c.message_origin)
    }

    /// Stable snake_case name of the error kind (the variant), for logs,
    /// metrics and telemetry: `"rate_limited"` for [`Error::RateLimited`].
    ///
    /// To branch on the kind, match on the variants instead (see
    /// [`Error`]'s "Handling errors" section).
    #[doc(alias = "kind")]
    #[must_use]
    pub fn kind_name(&self) -> &'static str {
        match self {
            Error::Timeout { .. } => "timeout",
            Error::Transport { .. } => "transport",
            Error::Auth { .. } => "auth",
            Error::Configuration { .. } => "configuration",
            Error::Rejected { .. } => "rejected",
            Error::RateLimited { .. } => "rate_limited",
            Error::QuotaExhausted { .. } => "quota_exhausted",
            Error::Overloaded { .. } => "overloaded",
            Error::Provider { .. } => "provider",
            Error::Serialization { .. } => "serialization",
            Error::Unsupported { .. } => "unsupported",
            Error::InvalidRequest { .. } => "invalid_request",
            Error::ModelNotFound { .. } => "model_not_found",
            Error::UnexpectedResponse { .. } => "unexpected_response",
            Error::ContextLengthExceeded { .. } => "context_length_exceeded",
            Error::ContentFiltered { .. } => "content_filtered",
            Error::Stream { .. } => "stream",
            #[cfg(feature = "extract")]
            Error::Extract { .. } => "extract",
        }
    }

    /// Returns `true` if the error is likely transient and worth retrying
    /// against the same provider.
    ///
    /// Covers rate limits, overload, timeouts, transport failures, provider
    /// failures without a status or with a 5xx status other than `501` and
    /// `505`, and incomplete or transport-failed streams. Quota exhaustion,
    /// local refusals ([`Error::Rejected`]) and configuration errors are
    /// never retryable, and neither is a
    /// `501` (Not Implemented) or `505` (HTTP Version Not Supported): the
    /// server answers those the same way every time.
    ///
    /// Classifies by kind and status alone: it never reads the
    /// [server retry directive](Self::server_retry_directive), which the
    /// default [`RetryPolicy`](crate::wrappers::RetryPolicy) follows first.
    #[must_use]
    pub fn is_retryable(&self) -> bool {
        match self {
            Error::RateLimited { .. }
            | Error::Overloaded { .. }
            | Error::Timeout { .. }
            | Error::Transport { .. } => true,
            Error::Provider { context, .. } => context
                .status
                .is_none_or(|s| s >= 500 && !matches!(s, 501 | 505)),
            Error::Stream { kind, .. } => {
                matches!(
                    kind,
                    StreamErrorKind::Incomplete | StreamErrorKind::Transport
                )
            }
            _ => false,
        }
    }

    /// Returns `true` for canonically transient errors: timeouts, rate
    /// limits, and overload. Stricter than [`is_retryable`](Self::is_retryable):
    /// transport failures are excluded because a wrong base URL fails the
    /// same way every time.
    #[must_use]
    pub fn is_transient(&self) -> bool {
        matches!(
            self,
            Error::Timeout { .. } | Error::RateLimited { .. } | Error::Overloaded { .. }
        )
    }

    /// Returns `true` for authentication or authorization failures reported
    /// by a provider.
    ///
    /// A credential that is missing locally is an [`Error::Configuration`],
    /// not an authentication failure, and returns `false`.
    #[must_use]
    pub fn is_auth(&self) -> bool {
        matches!(self, Error::Auth { .. })
    }

    /// Borrow a redacted logging view of this error.
    #[must_use]
    pub fn as_log(&self) -> ErrorLog<'_> {
        ErrorLog::new(self)
    }
}

/// Not redacted: shows the constructor message and the
/// [`upstream_message`](Error::upstream_message), which may echo prompts or
/// input values. Use [`Error::as_log`] for logs and telemetry.
///
/// Starts with the provider when the context names one
/// ([`ErrorContext::provider`]), as in `openai: authentication error: HTTP
/// 401: Incorrect API key provided.`, so an error says where it came from
/// behind a fallback or a router. An error that ended a failover shows
/// itself only, not the errors in its
/// [`fallback_from`](Error::fallback_from) chain; [`Error::as_log`] logs
/// those.
impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(provider) = self.context().and_then(|c| c.provider.as_deref()) {
            write!(f, "{provider}: ")?;
        }
        match self {
            Error::Timeout { message, .. } => write!(f, "timeout: {message}"),
            Error::Transport { message, .. } => write!(f, "transport error: {message}"),
            Error::Auth { message, .. } => write!(f, "authentication error: {message}"),
            Error::Configuration { message, .. } => {
                write!(f, "configuration error: {message}")
            }
            Error::Rejected { message, .. } => write!(f, "rejected: {message}"),
            Error::RateLimited { message, .. } => write!(f, "rate limited: {message}"),
            Error::QuotaExhausted { message, .. } => write!(f, "quota exhausted: {message}"),
            Error::Overloaded { message, .. } => write!(f, "overloaded: {message}"),
            Error::Provider { message, context } => match context.status {
                Some(status) => write!(f, "provider error ({status}): {message}"),
                None => write!(f, "provider error: {message}"),
            },
            Error::Serialization { error } => write!(f, "serialization error: {error}"),
            Error::Unsupported { message, .. } => write!(f, "unsupported: {message}"),
            Error::InvalidRequest { message, .. } => write!(f, "invalid request: {message}"),
            Error::ModelNotFound { message, .. } => write!(f, "model not found: {message}"),
            Error::UnexpectedResponse { message, .. } => {
                write!(f, "unexpected response: {message}")
            }
            Error::ContextLengthExceeded { message, .. } => {
                write!(f, "context length exceeded: {message}")
            }
            Error::ContentFiltered { message, .. } => write!(f, "content filtered: {message}"),
            Error::Stream { kind, message, .. } => {
                write!(f, "stream error ({}): {message}", kind.as_str())
            }
            #[cfg(feature = "extract")]
            Error::Extract { failure } => write!(f, "extraction error: {failure}"),
        }?;
        if let Some(upstream) = self.upstream_message() {
            write!(f, ": {upstream}")?;
        }
        Ok(())
    }
}

/// Redacted and compact: shows the variant, its constructor message, and
/// the context fields that are set, inline. The upstream message, a
/// serialization failure's text and an extraction's model output show as
/// their length (`<withheld 21 bytes; print with {err}>`), and the response
/// body as `body_len`; see [`ErrorContext`]'s `Debug`.
impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (name, message, context) = match self {
            Error::Timeout { message, context } => ("Timeout", message, context),
            Error::Transport { message, context } => ("Transport", message, context),
            Error::Auth { message, context } => ("Auth", message, context),
            Error::Configuration { message, context } => ("Configuration", message, context),
            Error::Rejected { message, context } => ("Rejected", message, context),
            Error::RateLimited { message, context } => ("RateLimited", message, context),
            Error::QuotaExhausted { message, context } => ("QuotaExhausted", message, context),
            Error::Overloaded { message, context } => ("Overloaded", message, context),
            Error::Provider { message, context } => ("Provider", message, context),
            Error::Unsupported {
                message, context, ..
            } => ("Unsupported", message, context),
            Error::InvalidRequest { message, context } => ("InvalidRequest", message, context),
            Error::ModelNotFound { message, context } => ("ModelNotFound", message, context),
            Error::UnexpectedResponse { message, context } => {
                ("UnexpectedResponse", message, context)
            }
            Error::ContextLengthExceeded {
                message, context, ..
            } => ("ContextLengthExceeded", message, context),
            Error::ContentFiltered { message, context } => ("ContentFiltered", message, context),
            Error::Stream {
                message, context, ..
            } => ("Stream", message, context),
            // A decode failure may quote the value it could not decode.
            Error::Serialization { error } => {
                return f
                    .debug_struct("Serialization")
                    .field("error", &Withheld::shown_by_display(&error.to_string()))
                    .finish();
            }
            #[cfg(feature = "extract")]
            Error::Extract { failure } => {
                return f.debug_struct("Extract").field("failure", failure).finish();
            }
        };
        let mut debug = f.debug_struct(name);
        debug.field("message", message);
        match self {
            Error::Unsupported { subject, stage, .. } => {
                if let Some(subject) = subject {
                    debug.field("subject", subject);
                }
                if !stage.is_unspecified() {
                    debug.field("stage", stage);
                }
            }
            Error::ContextLengthExceeded {
                context_window: Some(context_window),
                ..
            } => {
                debug.field("context_window", context_window);
            }
            Error::Stream { kind, .. } => {
                debug.field("kind", kind);
            }
            _ => {}
        }
        context.debug_fields(&mut debug);
        debug.finish()
    }
}

/// The source chain never repeats what `Display` shows. `Display` shows the
/// whole error, including the failure an [`Error::Serialization`] or
/// [`Error::Extract`] holds, so their `source()` is the cause *behind* what
/// is shown: the serialization failure's source, and so on. For the other
/// variants it is the cause attached with [`Error::with_source`]. The
/// errors a failover tried first ([`Error::fallback_from`]) are not causes,
/// so they are never a `source()`.
///
/// So `anyhow`'s `{:#}` and other report chains print each text once.
///
#[cfg_attr(
    not(feature = "extract"),
    doc = "[`Error::Extract`]: crate#cargo-features"
)]
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Serialization { error } => error.source(),
            #[cfg(feature = "extract")]
            Self::Extract { failure } => failure.source(),
            _ => {
                let source = self.context()?.source()?;
                Some(source)
            }
        }
    }
}

/// A `serde_json` failure as [`Error::Serialization`]: the caller's own
/// data failed to (de)serialize, such as a typed option.
///
/// This is what `?` on a `serde_json` call gives. It carries no
/// [`ErrorContext`], so the provider, status and request id are lost, and a
/// failover's earlier errors with them. An adapter that decodes what a
/// provider sent maps a failure to [`Error::UnexpectedResponse`] instead
/// (`allama_http::malformed_response` builds it with the status and body
/// size), never with `?`.
impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        Self::serialization(err)
    }
}

/// Wrapper for serialization failures surfaced through [`Error`].
///
/// Keeps [`Error`] cloneable and serializable while preserving the original
/// error as the source when available.
#[derive(Debug)]
pub struct SerializationError(BoxError);

impl Clone for SerializationError {
    fn clone(&self) -> Self {
        self.to_string().into()
    }
}

impl Serialize for SerializationError {
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for SerializationError {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
        Ok(String::deserialize(deserializer)?.into())
    }
}

impl fmt::Display for SerializationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Transparent: `Display` is the underlying failure's text, so `source()`
/// is that failure's own source. Reach the failure itself with
/// [`get_ref`](Self::get_ref).
impl std::error::Error for SerializationError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.0.source()
    }
}

impl From<serde_json::Error> for SerializationError {
    fn from(err: serde_json::Error) -> Self {
        Self(Box::new(err))
    }
}

impl From<String> for SerializationError {
    fn from(message: String) -> Self {
        Self(Box::new(StringError(message)))
    }
}

impl From<&str> for SerializationError {
    fn from(message: &str) -> Self {
        message.to_owned().into()
    }
}

impl SerializationError {
    /// The underlying failure, such as a [`serde_json::Error`], to downcast.
    ///
    /// ```
    /// use allama::SerializationError;
    ///
    /// let failure = serde_json::from_str::<u8>("x").unwrap_err();
    /// let error = SerializationError::from(failure);
    /// assert!(error.get_ref().downcast_ref::<serde_json::Error>().is_some());
    /// ```
    #[must_use]
    pub fn get_ref(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
        match self.0.downcast_ref::<PrefixedError>() {
            Some(prefixed) => prefixed.root(),
            None => self.0.as_ref(),
        }
    }

    /// Put `prefix` in front of the failure's text, keeping the failure as
    /// the source.
    pub(crate) fn prefix(&mut self, prefix: &str) {
        let source = std::mem::replace(&mut self.0, Box::new(StringError(String::new())));
        self.0 = Box::new(PrefixedError {
            prefix: prefix.to_owned(),
            source,
        });
    }
}

#[derive(Debug)]
struct StringError(String);

impl fmt::Display for StringError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for StringError {}

/// A failure with context in front of its text, from
/// [`Error::with_message_prefix`].
#[derive(Debug)]
struct PrefixedError {
    prefix: String,
    source: BoxError,
}

impl fmt::Display for PrefixedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.prefix, self.source)
    }
}

/// Transparent, like [`SerializationError`]: `Display` shows the failure.
impl std::error::Error for PrefixedError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source.source()
    }
}

impl PrefixedError {
    /// The failure under every prefix.
    fn root(&self) -> &(dyn std::error::Error + Send + Sync + 'static) {
        match self.source.downcast_ref::<PrefixedError>() {
            Some(inner) => inner.root(),
            None => self.source.as_ref(),
        }
    }
}

/// A name that is not one of a configuration enum's values, from its
/// [`FromStr`](std::str::FromStr) impl (`"high".parse::<ReasoningEffort>()`).
///
/// ```
/// use allama::ReasoningEffort;
///
/// assert_eq!("xhigh".parse::<ReasoningEffort>()?, ReasoningEffort::XHigh);
///
/// let err = "hgih".parse::<ReasoningEffort>().unwrap_err();
/// assert_eq!(
///     err.to_string(),
///     r#"unknown reasoning effort "hgih"; expected one of: none, minimal, low, medium, high, xhigh"#
/// );
/// # Ok::<(), allama::ParseEnumError>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParseEnumError {
    /// The type, in words: `"reasoning effort"`.
    pub type_name: &'static str,
    /// The text that did not parse.
    pub value: String,
    /// Every accepted name, in declaration order.
    pub expected: &'static [&'static str],
}

impl ParseEnumError {
    pub(crate) fn new(
        type_name: &'static str,
        value: &str,
        expected: &'static [&'static str],
    ) -> Self {
        Self {
            type_name,
            value: value.to_owned(),
            expected,
        }
    }
}

impl fmt::Display for ParseEnumError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "unknown {} {:?}; expected one of: {}",
            self.type_name,
            self.value,
            self.expected.join(", ")
        )
    }
}

impl std::error::Error for ParseEnumError {}

/// Redacted logging and telemetry view of an [`Error`].
///
/// Unlike `Display`, `Debug` and serde serialization of `Error`, this leaves
/// out everything that may contain prompts, model output, or credentials.
/// It includes:
///
/// - the kind, provider, status, the request ID, the retry hint, and the
///   server retry directive;
/// - the provider code and type and the rejected request field
///   ([`ErrorContext::param`]), when each is a bounded token: 1 to 128
///   characters out of `A-Z a-z 0-9 _ . : [ ] -`. Any other value is shown
///   as `<redacted>`, so the log still says the provider sent one;
/// - the body's length, never its text;
/// - the constructor message only when it is
///   [`Authored`](MessageOrigin::Authored) (see [`Error::authored`]);
/// - the pass record an extraction attached under `allama.extraction` in
///   [`ErrorContext::metadata`]: usage, not input, so cost logs keep the
///   tokens an extraction spent before a provider error. The serde form has
///   the whole record; `Display` shows its mode, passes, and input and
///   output tokens;
/// - likewise the progress a chunked embedding attached under
///   `allama.embedding`: `Display` shows the inputs completed and the input
///   tokens.
///
/// It never includes the body or any other metadata, and includes the
/// [`upstream_message`](ErrorContext::upstream_message) only when asked for
/// with [`with_message`](Self::with_message). The errors a failover tried
/// before this one ([`Error::fallback_from`]) are logged under these rules
/// as a nested `fallback_from` log, newest first.
///
/// The token filter keeps prose and markup out of the code, type and
/// `param` fields, where a provider might echo a sentence of input. It
/// cannot tell a code from an input value shaped like one: a provider that
/// puts a single word, a number or an identifier from the request into one
/// of these fields gets it logged.
///
/// Serialize it for structured logs, or format it with `Display` for a
/// single `key=value` line:
///
/// ```
/// use allama::Error;
///
/// let err = Error::rate_limited("HTTP 429")
///     .authored()
///     .with_provider("openai")
///     .with_status(429)
///     .with_request_id("req_1")
///     .with_upstream_message("slow down")
///     .with_body("{\"error\":\"...\"}");
/// assert_eq!(
///     err.as_log().to_string(),
///     r#"kind=rate_limited provider=openai status=429 request_id=req_1 body_len=15 message="HTTP 429""#,
/// );
/// // Opt in to the upstream text, for example at debug level.
/// assert_eq!(
///     err.as_log().with_message(4).to_string(),
///     r#"kind=rate_limited provider=openai status=429 request_id=req_1 body_len=15 message="HTTP 429" upstream_message="slow""#,
/// );
/// ```
#[derive(Debug, Clone, Copy)]
pub struct ErrorLog<'a> {
    error: &'a Error,
    message_chars: Option<usize>,
}

impl<'a> ErrorLog<'a> {
    /// Wrap an error in its redacted logging view.
    #[must_use]
    pub const fn new(error: &'a Error) -> Self {
        Self {
            error,
            message_chars: None,
        }
    }

    /// Also include the [`upstream_message`](ErrorContext::upstream_message),
    /// truncated to `max_chars` characters.
    ///
    /// Upstream text may echo prompts or input values, so keep this for
    /// debug-level logs. It never includes the response body, so text from a
    /// body the adapter did not recognise stays out.
    #[must_use]
    pub const fn with_message(mut self, max_chars: usize) -> Self {
        self.message_chars = Some(max_chars);
        self
    }

    /// The error referenced by this view.
    #[must_use]
    pub const fn error(self) -> &'a Error {
        self.error
    }

    /// The same view of another error, for nested errors.
    const fn of(self, error: &'a Error) -> Self {
        Self { error, ..self }
    }

    /// The constructor message, when it may be logged.
    fn message(self) -> Option<String> {
        match self.error {
            Error::Serialization { .. } => None,
            #[cfg(feature = "extract")]
            Error::Extract { failure } => Some(failure.error().log_message()),
            error => (error.message_origin() == MessageOrigin::Authored).then(|| error.message()),
        }
    }

    /// The truncated upstream message, when asked for.
    fn upstream_message(self) -> Option<&'a str> {
        let max_chars = self.message_chars?;
        let upstream = self.error.upstream_message()?;
        Some(
            upstream
                .char_indices()
                .nth(max_chars)
                .map_or(upstream, |(end, _)| &upstream[..end]),
        )
    }
}

impl Serialize for ErrorLog<'_> {
    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
        let error = self.error;
        let mut map = serde_json::Map::new();
        map.insert("type".into(), error.kind_name().into());

        insert_opt(&mut map, "message", self.message());
        insert_opt(&mut map, "upstream_message", self.upstream_message());
        if let Error::ContextLengthExceeded {
            context_window: Some(window),
            ..
        } = error
        {
            map.insert("context_window".into(), (*window).into());
        }
        if let Error::Stream { kind, .. } = error {
            map.insert("stream_kind".into(), kind.as_str().into());
        }
        if let Some(context) = error.context() {
            insert_opt(&mut map, "provider", context.provider.clone());
            insert_opt(&mut map, "status", context.status);
            insert_opt(&mut map, "request_id", context.request_id.clone());
            insert_opt(
                &mut map,
                "retry_after_secs",
                context.retry_after.map(|d| d.as_secs_f64()),
            );
            insert_opt(
                &mut map,
                "server_retry_directive",
                context.server_retry_directive,
            );
            for (key, value) in [
                ("provider_code", &context.provider_code),
                ("provider_type", &context.provider_type),
                ("param", &context.param),
            ] {
                insert_opt(&mut map, key, value.as_deref().map(log_token));
            }
            if let Some(body) = context.capped_body() {
                map.insert("body_present".into(), true.into());
                map.insert("body_len".into(), body.len().into());
            }
            for key in [EXTRACTION_METADATA_KEY, EMBEDDING_METADATA_KEY] {
                if let Some(record) = context.metadata.get(key) {
                    map.insert(key.into(), record.clone());
                }
            }
        }
        if let Some(earlier) = error.fallback_from() {
            map.insert(
                "fallback_from".into(),
                serde_json::to_value(self.of(earlier)).unwrap_or_default(),
            );
        }

        serde_json::Value::Object(map).serialize(serializer)
    }
}

impl fmt::Display for ErrorLog<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let error = self.error;
        write!(f, "kind={}", error.kind_name())?;
        if let Error::Stream { kind, .. } = error {
            write!(f, " stream_kind={}", kind.as_str())?;
        }
        if let Some(context) = error.context() {
            write_log_field(f, "provider", context.provider.as_deref())?;
            if let Some(status) = context.status {
                write!(f, " status={status}")?;
            }
            for (key, value) in [
                ("provider_code", &context.provider_code),
                ("provider_type", &context.provider_type),
                ("param", &context.param),
            ] {
                write_log_field(f, key, value.as_deref().map(log_token))?;
            }
            write_log_field(f, "request_id", context.request_id.as_deref())?;
            if let Some(body) = context.capped_body() {
                write!(f, " body_len={}", body.len())?;
            }
            if let Some(extraction) = context.metadata.get(EXTRACTION_METADATA_KEY) {
                write_extraction_fields(f, extraction)?;
            }
            if let Some(embedding) = context.metadata.get(EMBEDDING_METADATA_KEY) {
                write_embedding_fields(f, embedding)?;
            }
        }
        if let Some(message) = self.message() {
            write!(f, " message={message:?}")?;
        }
        if let Some(upstream) = self.upstream_message() {
            write!(f, " upstream_message={upstream:?}")?;
        }
        if let Some(earlier) = error.fallback_from() {
            write!(f, " fallback_from=[{}]", self.of(earlier))?;
        }
        Ok(())
    }
}

/// What [`ErrorLog`] shows of an upstream code, type or `param` that is
/// not a bounded token.
const REDACTED: &str = "<redacted>";

/// The longest code, type or `param` [`ErrorLog`] shows.
const MAX_LOG_TOKEN_LEN: usize = 128;

/// `value` when it is a bounded token (`[A-Za-z0-9_.:\[\]-]{1,128}`), or
/// [`REDACTED`]: upstream codes, types and field paths, never prose.
fn log_token(value: &str) -> &str {
    let token = (1..=MAX_LOG_TOKEN_LEN).contains(&value.len())
        && value.bytes().all(|b| {
            b.is_ascii_alphanumeric() || matches!(b, b'_' | b'.' | b':' | b'[' | b']' | b'-')
        });
    if token { value } else { REDACTED }
}

/// Write ` key=value` when `value` is known.
fn write_log_field(f: &mut fmt::Formatter<'_>, key: &str, value: Option<&str>) -> fmt::Result {
    match value {
        Some(value) => {
            write!(f, " {key}=")?;
            write_log_value(f, value)
        }
        None => Ok(()),
    }
}

/// Write the cost fields of an extraction's pass record: its mode, passes,
/// and summed input and output tokens.
fn write_extraction_fields(f: &mut fmt::Formatter<'_>, record: &serde_json::Value) -> fmt::Result {
    let key = EXTRACTION_METADATA_KEY;
    write_log_field(f, &format!("{key}.mode"), record["mode"].as_str())?;
    for (name, value) in [
        ("passes", &record["passes"]),
        ("input_tokens", &record["usage"]["input_tokens"]),
        ("output_tokens", &record["usage"]["output_tokens"]),
    ] {
        if let Some(value) = value.as_u64() {
            write!(f, " {key}.{name}={value}")?;
        }
    }
    Ok(())
}

/// Write the cost fields of a chunked embedding's progress: the inputs
/// completed and the summed input tokens.
fn write_embedding_fields(f: &mut fmt::Formatter<'_>, record: &serde_json::Value) -> fmt::Result {
    let key = EMBEDDING_METADATA_KEY;
    for (name, value) in [
        ("completed_inputs", &record["completed_inputs"]),
        ("input_tokens", &record["usage"]["input_tokens"]),
    ] {
        if let Some(value) = value.as_u64() {
            write!(f, " {key}.{name}={value}")?;
        }
    }
    Ok(())
}

/// Write a value bare when it is a simple token, quoted and escaped otherwise,
/// so the rendering stays on one line.
fn write_log_value(f: &mut fmt::Formatter<'_>, value: &str) -> fmt::Result {
    let bare = !value.is_empty()
        && value
            .chars()
            .all(|c| c.is_ascii_graphic() && c != '"' && c != '=');
    if bare {
        f.write_str(value)
    } else {
        write!(f, "{value:?}")
    }
}

/// `body` truncated to [`ErrorContext::MAX_BODY_BYTES`] at a character
/// boundary.
fn cap_body(body: &str) -> &str {
    &body[..body.floor_char_boundary(ErrorContext::MAX_BODY_BYTES)]
}

fn serialize_capped_body<S: Serializer>(
    body: &Option<String>,
    serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
    body.as_deref().map(cap_body).serialize(serializer)
}

/// Set `slot` when `value` is `Some`; keep it otherwise.
fn overwrite<T>(slot: &mut Option<T>, value: Option<T>) {
    if value.is_some() {
        *slot = value;
    }
}

fn insert_opt<T: Into<serde_json::Value>>(
    map: &mut serde_json::Map<String, serde_json::Value>,
    key: &str,
    value: Option<T>,
) {
    if let Some(value) = value {
        map.insert(key.into(), value.into());
    }
}

mod option_duration_secs_f64 {
    use std::time::Duration;

    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    pub fn serialize<S: Serializer>(
        value: &Option<Duration>,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error> {
        value.map(|d| d.as_secs_f64()).serialize(serializer)
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> std::result::Result<Option<Duration>, D::Error> {
        // `try_from`: a stored value too large for a `Duration` (such as
        // `1e20`) reads as no hint instead of panicking.
        Ok(Option::<f64>::deserialize(deserializer)?
            .and_then(|secs| Duration::try_from_secs_f64(secs).ok()))
    }
}

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

    #[test]
    fn display_names_the_provider_of_each_error() {
        let auth = Error::auth("HTTP 401")
            .with_provider("openai")
            .with_upstream_message("Incorrect API key provided.");
        assert_eq!(
            auth.to_string(),
            "openai: authentication error: HTTP 401: Incorrect API key provided."
        );
        assert_eq!(
            Error::rate_limited("HTTP 429").to_string(),
            "rate limited: HTTP 429"
        );
        // An error that ended a failover shows itself only.
        let failover =
            auth.with_fallback_from(Error::rate_limited("HTTP 429").with_provider("anthropic"));
        assert_eq!(
            failover.to_string(),
            "openai: authentication error: HTTP 401: Incorrect API key provided."
        );
    }

    #[test]
    fn transport_errors_are_retryable_but_not_transient() {
        let err = Error::transport("connection refused").with_provider("demo");
        assert_eq!(err.kind_name(), "transport");
        assert!(err.is_retryable());
        assert!(!err.is_transient());
        assert_eq!(err.to_string(), "demo: transport error: connection refused");
        let round_trip: Error =
            serde_json::from_value(serde_json::to_value(&err).unwrap()).unwrap();
        assert!(matches!(round_trip, Error::Transport { .. }));
        assert_eq!(
            round_trip.context().unwrap().provider.as_deref(),
            Some("demo")
        );
    }

    #[test]
    fn display_and_retryability() {
        let cases = [
            (Error::timeout("slow"), "timeout: slow", true),
            (
                Error::auth("bad key"),
                "authentication error: bad key",
                false,
            ),
            (
                Error::configuration("OPENAI_API_KEY not set"),
                "configuration error: OPENAI_API_KEY not set",
                false,
            ),
            (Error::rate_limited("busy"), "rate limited: busy", true),
            (
                Error::quota_exhausted("no credit"),
                "quota exhausted: no credit",
                false,
            ),
            (Error::overloaded("down"), "overloaded: down", true),
            (Error::provider("reset"), "provider error: reset", true),
            (
                Error::provider("boom").with_status(500),
                "provider error (500): boom",
                true,
            ),
            (
                Error::provider("bad").with_status(400),
                "provider error (400): bad",
                false,
            ),
            (Error::unsupported("x"), "unsupported: x", false),
            (Error::invalid_request("x"), "invalid request: x", false),
            (Error::model_not_found("x"), "model not found: x", false),
            (
                Error::unexpected_response("x"),
                "unexpected response: x",
                false,
            ),
            (Error::content_filtered("x"), "content filtered: x", false),
            (
                Error::stream(StreamErrorKind::Incomplete, "cut"),
                "stream error (incomplete): cut",
                true,
            ),
            (
                Error::stream(StreamErrorKind::Transport, "reset"),
                "stream error (transport): reset",
                true,
            ),
            (
                Error::stream_decode("bad json"),
                "stream error (decode): bad json",
                false,
            ),
        ];
        for (err, display, retryable) in cases {
            assert_eq!(err.to_string(), display);
            assert_eq!(err.is_retryable(), retryable, "{display}");
        }
    }

    /// A 5xx is retried, except the two statuses a server answers the same
    /// way every time: `501` (Ollama's "does not support embeddings" for a
    /// chat-only model) and `505`.
    #[test]
    fn server_errors_are_retryable_except_501_and_505() {
        for (status, retryable) in [(500, true), (502, true), (501, false), (505, false)] {
            let err = Error::provider(format!("HTTP {status}")).with_status(status);
            assert_eq!(err.is_retryable(), retryable, "HTTP {status}");
        }
    }

    #[test]
    fn builders_fill_context() {
        let err = Error::rate_limited("slow down")
            .with_status(429)
            .with_request_id("req_1")
            .with_retry_after(Duration::from_secs(2))
            .with_provider_code("rate_limit_exceeded")
            .with_provider("openai");
        assert_eq!(err.status(), Some(429));
        assert_eq!(err.request_id(), Some("req_1"));
        assert_eq!(err.retry_after(), Some(Duration::from_secs(2)));
        assert_eq!(
            err.context().unwrap().provider_code.as_deref(),
            Some("rate_limit_exceeded")
        );
    }

    #[test]
    fn server_retry_directive_is_context() {
        let err = Error::rate_limited("HTTP 429").with_server_retry_directive(false);
        assert_eq!(err.server_retry_directive(), Some(false));
        // `None` keeps what is there, like the other `with_*` builders.
        let err = err.with_server_retry_directive_opt(None);
        assert_eq!(err.server_retry_directive(), Some(false));
        // It does not change the kind-and-status classification.
        assert!(err.is_retryable());
        assert_eq!(Error::provider("x").server_retry_directive(), None);

        let round_trip: Error =
            serde_json::from_str(&serde_json::to_string(&err).unwrap()).unwrap();
        assert_eq!(round_trip.server_retry_directive(), Some(false));
        let log = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(log["server_retry_directive"], false);
        assert!(format!("{:?}", err.context().unwrap()).contains("server_retry_directive"));
        // Older serialized errors have no directive.
        let old: Error =
            serde_json::from_str(r#"{"type":"rate_limited","data":{"message":"m"}}"#).unwrap();
        assert_eq!(old.server_retry_directive(), None);
    }

    /// The kinds along an error's `fallback_from` chain, newest first.
    fn tried(error: &Error) -> Vec<&'static str> {
        std::iter::successors(error.fallback_from(), |e| e.fallback_from())
            .map(Error::kind_name)
            .collect()
    }

    #[test]
    fn an_error_that_ended_a_failover_is_itself_and_logs_the_chain() {
        let err = Error::rate_limited("fallback")
            .with_request_id("r")
            .with_fallback_from(Error::timeout("primary").with_provider("a"));
        assert!(matches!(err, Error::RateLimited { .. }));
        assert_eq!(err.kind_name(), "rate_limited");
        assert!(err.is_retryable());
        assert_eq!(err.request_id(), Some("r"));
        assert_eq!(tried(&err), ["timeout"]);
        let log = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(log["type"], "rate_limited");
        assert_eq!(log["request_id"], "r");
        assert_eq!(log["fallback_from"]["type"], "timeout");
        assert_eq!(log["fallback_from"]["provider"], "a");
        assert!(log["fallback_from"].get("fallback_from").is_none());
    }

    #[test]
    fn with_fallback_from_attaches_at_the_tail() {
        let last = || {
            Error::invalid_request("HTTP 400")
                .with_status(400)
                .with_param("messages.[1].content")
                .with_provider_code("invalid_value")
        };
        assert_eq!(last().fallback_from().map(Error::kind_name), None);

        // Each call adds an older error after the ones already recorded.
        let chain = last()
            .with_fallback_from(Error::rate_limited("c"))
            .with_fallback_from(Error::overloaded("b").with_fallback_from(Error::timeout("a")));
        assert_eq!(chain.kind_name(), "invalid_request");
        assert_eq!(tried(&chain), ["rate_limited", "overloaded", "timeout"]);
        // The context accessors read the error itself.
        assert_eq!(chain.param(), Some("messages.[1].content"));
        assert_eq!(chain.provider_code(), Some("invalid_value"));
        assert_eq!(chain.status(), Some(400));
        assert!(!chain.is_retryable());

        // A clone shares the chain, and attaching to it leaves the
        // original alone.
        let shared = chain.clone();
        let longer = shared.with_fallback_from(Error::auth("first"));
        assert_eq!(tried(&chain), ["rate_limited", "overloaded", "timeout"]);
        assert_eq!(
            tried(&longer),
            ["rate_limited", "overloaded", "timeout", "auth"]
        );

        // Without a context, there is no chain to keep.
        let plain = Error::serialization("bad json").with_fallback_from(Error::timeout("a"));
        assert_eq!(plain.param(), None);
        assert_eq!(plain.provider_code(), None);
        assert!(plain.fallback_from().is_none());
        // A chain ends at an error without a context.
        let ends = Error::timeout("b")
            .with_fallback_from(Error::serialization("bad json"))
            .with_fallback_from(Error::overloaded("a"));
        assert_eq!(tried(&ends), ["serialization"]);
    }

    #[test]
    fn the_chain_is_not_compared_and_merges_only_when_set() {
        let plain = Error::rate_limited("HTTP 429");
        let chained = plain.clone().with_fallback_from(Error::timeout("a"));
        // `ErrorContext` keeps `Eq`: the chain is not part of it.
        assert_eq!(plain.context(), chained.context());
        assert!(!chained.context().unwrap().is_empty());

        // Merging a context without a chain keeps the chain…
        let kept = chained.clone().with_context(ErrorContext::new());
        assert_eq!(tried(&kept), ["timeout"]);
        // …and one with a chain replaces it.
        let other = Error::overloaded("x").with_fallback_from(Error::auth("b"));
        let replaced = chained.with_context(other.context().unwrap().clone());
        assert_eq!(tried(&replaced), ["auth"]);
    }

    #[test]
    fn the_chain_survives_serde_under_fallback_from() {
        let err = Error::rate_limited("HTTP 429")
            .with_status(429)
            .with_fallback_from(
                Error::overloaded("HTTP 529")
                    .with_provider("anthropic")
                    .with_fallback_from(Error::timeout("slow")),
            );
        let json = serde_json::to_value(&err).unwrap();
        assert_eq!(
            json,
            serde_json::json!({
                "type": "rate_limited",
                "data": {
                    "message": "HTTP 429",
                    "context": {
                        "status": 429,
                        "fallback_from": {
                            "type": "overloaded",
                            "data": {
                                "message": "HTTP 529",
                                "context": {
                                    "provider": "anthropic",
                                    "fallback_from": {
                                        "type": "timeout",
                                        "data": {"message": "slow"}
                                    }
                                }
                            }
                        }
                    }
                }
            })
        );
        let back: Error = serde_json::from_value(json.clone()).unwrap();
        assert!(matches!(back, Error::RateLimited { .. }));
        assert_eq!(tried(&back), ["overloaded", "timeout"]);
        assert_eq!(
            back.fallback_from()
                .unwrap()
                .context()
                .unwrap()
                .provider
                .as_deref(),
            Some("anthropic")
        );
        assert_eq!(serde_json::to_value(&back).unwrap(), json);
        // A chain alone keeps the context in the serialized form.
        let only_chain = Error::timeout("b").with_fallback_from(Error::timeout("a"));
        let json = serde_json::to_value(&only_chain).unwrap();
        assert_eq!(json["data"]["context"]["fallback_from"]["type"], "timeout");
    }

    #[test]
    fn with_context_merges_and_never_touches_the_message_origin() {
        let mut context = ErrorContext::new();
        context.status = Some(429);
        context.request_id = Some("req_new".into());
        context
            .metadata
            .insert("b".into(), serde_json::json!("new"));
        context.metadata.insert("c".into(), serde_json::json!(3));
        context.message_origin = MessageOrigin::Authored;

        let mut existing = ErrorContext::new();
        existing.provider = Some("acme".into());
        existing.request_id = Some("req_old".into());
        existing.upstream_message = Some("slow down".into());
        existing.metadata.insert("a".into(), serde_json::json!(1));
        existing
            .metadata
            .insert("b".into(), serde_json::json!("old"));
        let mut err = Error::rate_limited("HTTP 429");
        *err.context_mut().unwrap() = existing;

        let merged = err.with_context(context.clone());
        let c = merged.context().unwrap();
        // `None` keeps, `Some` overwrites.
        assert_eq!(c.provider.as_deref(), Some("acme"));
        assert_eq!(c.upstream_message.as_deref(), Some("slow down"));
        assert_eq!(c.status, Some(429));
        assert_eq!(c.request_id.as_deref(), Some("req_new"));
        // Metadata merges by key; the argument wins.
        assert_eq!(c.metadata["a"], serde_json::json!(1));
        assert_eq!(c.metadata["b"], serde_json::json!("new"));
        assert_eq!(c.metadata["c"], serde_json::json!(3));
        // An authored origin in the argument is not promoted…
        assert_eq!(merged.message_origin(), MessageOrigin::Unknown);
        // …and an unknown one does not demote.
        let kept = Error::rate_limited("HTTP 429")
            .authored()
            .with_context(ErrorContext::new());
        assert_eq!(kept.message_origin(), MessageOrigin::Authored);

        // Slots set before or after the call give the same result.
        let before = Error::rate_limited("HTTP 429")
            .with_provider("acme")
            .authored()
            .with_context(context.clone());
        let after = Error::rate_limited("HTTP 429")
            .with_context(context)
            .with_provider("acme")
            .authored();
        assert_eq!(before.context(), after.context());
    }

    #[test]
    fn context_mut_changes_the_error_itself_not_the_chain() {
        let mut chain = Error::rate_limited("c")
            .with_fallback_from(Error::overloaded("b"))
            .with_fallback_from(Error::timeout("a"));
        chain.context_mut().unwrap().provider_code = Some("rate_limit_exceeded".into());
        assert_eq!(chain.provider_code(), Some("rate_limit_exceeded"));
        assert_eq!(chain.fallback_from().unwrap().provider_code(), None);

        // Replacing the whole context drops the chain.
        *chain.context_mut().unwrap() = ErrorContext::new();
        assert!(chain.fallback_from().is_none());
    }

    #[test]
    fn log_redacts_body() {
        let err = Error::provider("bad").with_status(400).with_body("secret");
        let log = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(log["body_present"], true);
        assert_eq!(log["body_len"], 6);
        assert!(log.get("body").is_none());
    }

    #[test]
    fn log_display_is_one_redacted_line() {
        let err = Error::provider("bad\nrequest")
            .authored()
            .with_provider("openai-compat:groq")
            .with_status(400)
            .with_provider_code("invalid_value")
            .with_param("messages.[1].content")
            .with_request_id("req 1")
            .with_body("secret prompt");
        let line = err.as_log().to_string();
        assert_eq!(
            line,
            r#"kind=provider provider=openai-compat:groq status=400 provider_code=invalid_value param=messages.[1].content request_id="req 1" body_len=13 message="bad\nrequest""#
        );
        assert!(!line.contains("secret"));
        assert!(!line.contains('\n'));

        assert_eq!(
            Error::configuration("OPENAI_API_KEY not set")
                .authored()
                .as_log()
                .to_string(),
            r#"kind=configuration message="OPENAI_API_KEY not set""#
        );
    }

    #[test]
    fn log_omits_messages_of_unknown_origin() {
        let err = Error::invalid_request("echoed SENTINEL")
            .with_status(400)
            .with_body("SENTINEL");
        assert_eq!(err.message_origin(), MessageOrigin::Unknown);
        let line = err.as_log().with_message(100).to_string();
        assert_eq!(line, "kind=invalid_request status=400 body_len=8");
        let json = serde_json::to_value(err.as_log().with_message(100)).unwrap();
        assert!(json.get("message").is_none(), "{json}");
        assert!(!json.to_string().contains("SENTINEL"), "{json}");
    }

    #[test]
    fn upstream_message_is_opt_in_and_truncated_at_a_char_boundary() {
        let err = Error::invalid_request("HTTP 400")
            .authored()
            .with_status(400)
            .with_upstream_message("héllo wörld");
        assert_eq!(err.upstream_message(), Some("héllo wörld"));

        let redacted = err.as_log().to_string();
        assert!(!redacted.contains("héllo"), "{redacted}");
        assert!(
            serde_json::to_value(err.as_log())
                .unwrap()
                .get("upstream_message")
                .is_none()
        );

        assert_eq!(
            err.as_log().with_message(2).to_string(),
            r#"kind=invalid_request status=400 message="HTTP 400" upstream_message="hé""#
        );
        let json = serde_json::to_value(err.as_log().with_message(100)).unwrap();
        assert_eq!(json["upstream_message"], "héllo wörld");
        assert_eq!(json["message"], "HTTP 400");
    }

    #[test]
    fn display_is_not_redacted() {
        let err = Error::invalid_request("HTTP 400")
            .authored()
            .with_upstream_message("bad value SENTINEL");
        assert_eq!(
            err.to_string(),
            "invalid request: HTTP 400: bad value SENTINEL"
        );
        assert_eq!(err.message(), "HTTP 400");

        // Empty upstream text is ignored.
        let err = Error::timeout("slow").with_upstream_message("  ");
        assert_eq!(err.upstream_message(), None);
        assert_eq!(err.to_string(), "timeout: slow");

        // A failover's earlier errors are not shown.
        let failover = Error::rate_limited("f")
            .with_upstream_message("slow down")
            .with_fallback_from(Error::overloaded("p").with_upstream_message("busy"));
        assert_eq!(failover.to_string(), "rate limited: f: slow down");
    }

    #[test]
    fn provenance_round_trips_and_unknown_is_not_serialized() {
        let unknown = serde_json::to_value(Error::timeout("x").with_status(408)).unwrap();
        assert_eq!(
            unknown,
            serde_json::json!({"type": "timeout", "data": {"message": "x", "context": {"status": 408}}})
        );

        let err = Error::invalid_request("HTTP 400")
            .authored()
            .with_param("tools[0].function.parameters")
            .with_upstream_message("bad schema");
        let json = serde_json::to_value(&err).unwrap();
        assert_eq!(json["data"]["context"]["message_origin"], "authored");
        let back: Error = serde_json::from_value(json).unwrap();
        assert_eq!(back.message_origin(), MessageOrigin::Authored);
        assert_eq!(back.upstream_message(), Some("bad schema"));
        assert_eq!(
            back.context().unwrap().param.as_deref(),
            Some("tools[0].function.parameters")
        );
    }

    #[test]
    fn body_is_capped_in_with_body_serialization_and_debug() {
        // A two-byte character straddles the cap.
        let long = format!(
            "{}é{}",
            "a".repeat(ErrorContext::MAX_BODY_BYTES - 1),
            "b".repeat(100)
        );
        let err = Error::provider("x").with_body(long.clone());
        let body = err.context().unwrap().body.as_deref().unwrap();
        assert_eq!(body.len(), ErrorContext::MAX_BODY_BYTES - 1);
        assert!(body.chars().all(|c| c == 'a'));
        assert_eq!(
            serde_json::to_value(err.as_log()).unwrap()["body_len"],
            ErrorContext::MAX_BODY_BYTES - 1
        );

        let mut context = ErrorContext::new();
        context.body = Some(long.clone());
        assert_eq!(
            Error::provider("x")
                .with_context(context.clone())
                .context()
                .unwrap()
                .body
                .as_deref()
                .unwrap()
                .len(),
            ErrorContext::MAX_BODY_BYTES - 1
        );

        // Written directly: capped when serialized and in `Debug`.
        let mut direct = Error::provider("x");
        direct.context_mut().unwrap().body = Some(long.clone());
        let json = serde_json::to_value(&direct).unwrap();
        assert_eq!(
            json["data"]["context"]["body"].as_str().unwrap().len(),
            ErrorContext::MAX_BODY_BYTES - 1
        );
        let debug = format!("{direct:?}");
        assert!(!debug.contains("aaaa"), "{debug}");
        assert!(
            debug.contains(&format!("body_len: {}", ErrorContext::MAX_BODY_BYTES - 1)),
            "{debug}"
        );
    }

    #[test]
    fn debug_shows_the_body_length_only() {
        let err = Error::provider("x").with_body("my SSN is 123-45-6789");
        let debug = format!("{err:?}");
        assert!(!debug.contains("123-45-6789"), "{debug}");
        assert!(debug.contains("body_len: 21"), "{debug}");
    }

    #[test]
    fn debug_shows_the_upstream_message_length_only() {
        let err = Error::invalid_request("HTTP 400")
            .authored()
            .with_upstream_message("my SSN is 123-45-6789");
        let debug = format!("{err:?}");
        assert!(!debug.contains("123-45-6789"), "{debug}");
        assert!(
            debug.contains("upstream_message: <withheld 21 bytes; print with {err}>"),
            "{debug}"
        );
        let none = format!("{:?}", Error::invalid_request("HTTP 400"));
        assert!(!none.contains("upstream_message"), "{none}");
        // `Display` is not redacted.
        assert!(err.to_string().contains("123-45-6789"));
    }

    #[test]
    fn debug_is_compact_and_omits_unset_fields() {
        let err = Error::invalid_request("HTTP 400")
            .with_provider("openai")
            .with_status(400)
            .with_provider_code("invalid_value")
            .with_param("messages.[1].content")
            .with_request_id("req_1")
            .with_upstream_message("my SSN is 123-45-6789");
        assert_eq!(
            format!("{err:?}"),
            r#"InvalidRequest { message: "HTTP 400", provider: "openai", status: 400, provider_code: "invalid_value", param: "messages.[1].content", request_id: "req_1", upstream_message: <withheld 21 bytes; print with {err}> }"#
        );
        assert_eq!(
            format!("{:?}", Error::timeout("deadline")),
            r#"Timeout { message: "deadline" }"#
        );
        assert_eq!(format!("{:?}", ErrorContext::new()), "ErrorContext");

        let retry = Error::rate_limited("HTTP 429")
            .authored()
            .with_retry_after(Duration::from_millis(1500))
            .with_server_retry_directive(true);
        assert_eq!(
            format!("{retry:?}"),
            r#"RateLimited { message: "HTTP 429", retry_after: 1.5s, server_retry_directive: true, message_origin: Authored }"#
        );
        assert_eq!(
            format!("{:?}", retry.context().unwrap()),
            "ErrorContext { retry_after: 1.5s, server_retry_directive: true, message_origin: Authored }"
        );
    }

    #[test]
    fn debug_shows_variant_fields_and_metadata_keys() {
        let unsupported = Error::unsupported_by_provider(
            Some(Subject::Field {
                field: RequestField::Seed,
            }),
            "no seed",
        );
        assert_eq!(
            format!("{unsupported:?}"),
            r#"Unsupported { message: "no seed", subject: Field { field: Seed }, stage: Provider }"#
        );
        assert_eq!(
            format!("{:?}", Error::unsupported("no seed")),
            r#"Unsupported { message: "no seed" }"#
        );
        assert_eq!(
            format!(
                "{:?}",
                Error::context_length_exceeded("too long", Some(8192))
            ),
            r#"ContextLengthExceeded { message: "too long", context_window: 8192 }"#
        );
        assert_eq!(
            format!("{:?}", Error::stream_protocol("bad order")),
            r#"Stream { message: "bad order", kind: Protocol }"#
        );

        let mut err = Error::stream(StreamErrorKind::Incomplete, "cut off");
        err.context_mut()
            .unwrap()
            .metadata
            .insert("vendor.position".into(), serde_json::json!("SENTINEL"));
        let debug = format!("{err:?}");
        assert!(
            debug.contains(r#"metadata: ["vendor.position"]"#),
            "{debug}"
        );
        assert!(!debug.contains("SENTINEL"), "{debug}");
    }

    #[test]
    fn pretty_debug_puts_each_field_on_its_own_line() {
        let err = Error::provider("HTTP 500")
            .with_status(500)
            .with_upstream_message("SENTINEL");
        assert_eq!(
            format!("{err:#?}"),
            "Provider {\n    message: \"HTTP 500\",\n    status: 500,\n    \
             upstream_message: <withheld 8 bytes; print with {err}>,\n}"
        );
    }

    #[test]
    fn debug_never_shows_upstream_text_through_a_fallback() {
        let leaf = |message: &str| {
            Error::overloaded("HTTP 529")
                .with_upstream_message(format!("UPSTREAM {message}"))
                .with_body(format!("BODY {message}"))
        };
        let err = leaf("three")
            .with_fallback_from(leaf("two"))
            .with_fallback_from(leaf("one"))
            .with_fallback_from(Error::serialization(
                "invalid type: string \"SERDE\", expected u64",
            ));
        for debug in [
            format!("{err:?}"),
            format!("{err:#?}"),
            format!("{:?}", err.fallback_from().unwrap()),
            format!("{:?}", err.context().unwrap()),
        ] {
            for secret in ["UPSTREAM", "BODY", "SERDE"] {
                assert!(!debug.contains(secret), "{secret}: {debug}");
            }
        }
        let debug = format!("{err:?}");
        assert!(
            debug.starts_with(
                "Overloaded { message: \"HTTP 529\", upstream_message: <withheld 14 bytes; \
                 print with {err}>, body_len: 10, fallback_from: Overloaded {"
            ),
            "{debug}"
        );
        assert!(
            debug.contains("Serialization { error: <withheld 42 bytes; print with {err}> }"),
            "{debug}"
        );
        // `Display` is not redacted, and shows the error itself only.
        assert!(err.to_string().contains("UPSTREAM three"));
        assert!(!err.to_string().contains("UPSTREAM two"));
    }

    #[test]
    fn log_shows_codes_types_and_params_only_as_bounded_tokens() {
        let tokens = Error::invalid_request("HTTP 400")
            .authored()
            .with_provider_code("context_length_exceeded")
            .with_provider_type("invalid_request_error")
            .with_param("tools[0].function.parameters");
        assert_eq!(
            tokens.as_log().to_string(),
            "kind=invalid_request provider_code=context_length_exceeded \
             provider_type=invalid_request_error param=tools[0].function.parameters \
             message=\"HTTP 400\""
        );

        // Prose, markup, an empty value or one past 128 characters: the log
        // says a value existed, not what it was.
        for value in [
            "my SSN is 123-45-6789",
            "<b>bad</b>",
            "a,b",
            "",
            &"a".repeat(129),
        ] {
            let err = Error::invalid_request("HTTP 400")
                .authored()
                .with_provider_code(value.to_owned())
                .with_provider_type(value.to_owned())
                .with_param(value.to_owned());
            assert_eq!(
                err.as_log().to_string(),
                "kind=invalid_request provider_code=<redacted> provider_type=<redacted> \
                 param=<redacted> message=\"HTTP 400\"",
                "{value:?}"
            );
            let json = serde_json::to_value(err.as_log()).unwrap();
            for key in ["provider_code", "provider_type", "param"] {
                assert_eq!(json[key], REDACTED, "{value:?}");
            }
            let debug = format!("{err:?}");
            assert!(debug.contains(r#"provider_type: "<redacted>""#), "{debug}");
            if !value.is_empty() {
                assert!(!debug.contains(value), "{debug}");
            }
        }
        // 128 characters still pass.
        let long = "a".repeat(128);
        let err = Error::provider("x").with_provider_code(long.clone());
        assert!(err.as_log().to_string().contains(&long));
    }

    /// The filter's limit, pinned: it keeps prose out, but an input value
    /// shaped like a code (one word, a number, an identifier) passes.
    #[test]
    fn log_cannot_tell_a_code_from_a_value_shaped_like_one() {
        for echoed in ["123-45-6789", "alice", "user_42:secret"] {
            let err = Error::invalid_request("HTTP 400")
                .authored()
                .with_param(echoed);
            assert!(err.as_log().to_string().contains(echoed), "{echoed}");
        }
    }

    #[test]
    fn log_renders_the_fallback_chain_as_nested_redacted_logs() {
        let err = Error::rate_limited("f")
            .with_status(429)
            .with_upstream_message("upstream f")
            .with_fallback_from(
                Error::overloaded("q")
                    .authored()
                    .with_body("BODY q")
                    .with_upstream_message("upstream q"),
            )
            .with_fallback_from(
                Error::timeout("p")
                    .authored()
                    .with_provider("a")
                    .with_upstream_message("upstream p"),
            );
        assert_eq!(
            err.as_log().to_string(),
            r#"kind=rate_limited status=429 fallback_from=[kind=overloaded body_len=6 message="q" fallback_from=[kind=timeout provider=a message="p"]]"#
        );
        assert_eq!(
            err.as_log().with_message(8).to_string(),
            r#"kind=rate_limited status=429 upstream_message="upstream" fallback_from=[kind=overloaded body_len=6 message="q" upstream_message="upstream" fallback_from=[kind=timeout provider=a message="p" upstream_message="upstream"]]"#
        );
        let json = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(json["type"], "rate_limited");
        assert!(json.get("message").is_none(), "{json}");
        assert!(json.get("upstream_message").is_none(), "{json}");
        let q = &json["fallback_from"];
        assert_eq!(q["type"], "overloaded");
        assert_eq!(q["message"], "q");
        assert_eq!(q["body_len"], 6);
        assert!(q.get("body").is_none(), "{json}");
        assert!(q.get("upstream_message").is_none(), "{json}");
        assert_eq!(q["fallback_from"]["type"], "timeout");
        assert_eq!(q["fallback_from"]["message"], "p");
        assert_eq!(q["fallback_from"]["provider"], "a");
        assert!(!json.to_string().contains("BODY"), "{json}");
    }

    #[test]
    fn serialization_messages_are_not_logged() {
        let err = Error::serialization("invalid type: string \"SENTINEL\", expected u64");
        assert_eq!(err.as_log().to_string(), "kind=serialization");
    }

    #[cfg(feature = "extract")]
    #[test]
    fn extraction_logs_omit_parse_detail() {
        use crate::extract::{ExtractError, ExtractFailure, ExtractionMetadata, ExtractionMode};

        let parse = ExtractError::Parse {
            mode: ExtractionMode::Native,
            provider: "demo".into(),
            raw: "{\"ssn\": \"SENTINEL\"}".into(),
            message: "\"SENTINEL\" is not of type integer".into(),
            path: Some("SENTINEL_KEY".into()),
        };
        let metadata = ExtractionMetadata::new(ExtractionMode::Native);
        let err = Error::extract(ExtractFailure::new(parse, metadata));
        assert_eq!(
            err.as_log().to_string(),
            r#"kind=extract message="demo: failed to parse the output using mode Native""#
        );
        assert!(err.to_string().contains("SENTINEL"));
        assert!(err.to_string().contains("SENTINEL_KEY"));
    }

    /// An extraction error's chain prints its
    /// failure once, with the provider prefix other errors use.
    #[cfg(feature = "extract")]
    #[test]
    fn an_extraction_error_chain_prints_the_failure_once() {
        use crate::extract::{ExtractError, ExtractFailure, ExtractionMetadata, ExtractionMode};

        let failure = ExtractError::MissingToolCall {
            provider: "demo".into(),
        };
        let metadata = ExtractionMetadata::new(ExtractionMode::ForcedTool);
        let err = Error::extract(ExtractFailure::new(failure, metadata));
        assert_eq!(
            chain(&err),
            [
                "extraction error: demo: the model did not call the extraction tool 'submit_structured_output'"
            ]
        );
    }

    #[cfg(feature = "extract")]
    #[test]
    fn extraction_debug_never_shows_the_models_text() {
        use crate::extract::{
            ExtractError, ExtractFailure, ExtractionMetadata, ExtractionMode, RefusalSource,
        };
        use crate::{ChatResponse, ContentBlock, FinishReason};

        let response = ChatResponse::new(vec![ContentBlock::text("RESPONSE {\"ssn\": 1}")]);
        let parse = ExtractError::Parse {
            mode: ExtractionMode::Native,
            provider: "demo".into(),
            raw: "{\"ssn\": \"SENTINEL\"}".into(),
            message: "\"SENTINEL\" is not of type integer".into(),
            path: Some("SENTINEL_KEY".into()),
        };
        let refused = ExtractError::Refused {
            mode: ExtractionMode::Native,
            provider: "demo".into(),
            finish_reason: Some(FinishReason::Refusal),
            text: Some("I won't share SENTINEL".into()),
            source: RefusalSource::Model,
        };
        let metadata = ExtractionMetadata::new(ExtractionMode::Native);
        let parse_err =
            Error::extract(ExtractFailure::new(parse, metadata.clone()).with_response(response));
        let refused_err = Error::extract(ExtractFailure::new(refused, metadata));
        // Also in a failover's chain.
        let nested = Error::timeout("t").with_fallback_from(parse_err.clone());
        for err in [&parse_err, &refused_err, &nested] {
            for debug in [format!("{err:?}"), format!("{err:#?}")] {
                for secret in ["SENTINEL", "RESPONSE"] {
                    assert!(!debug.contains(secret), "{secret}: {debug}");
                }
            }
        }

        assert_eq!(
            format!("{parse_err:?}"),
            "Extract { failure: ExtractFailure { error: Parse { mode: Native, \
             provider: \"demo\", raw: <withheld 19 bytes>, message: <withheld 33 \
             bytes; print with {err}>, path: <withheld 12 bytes; print with {err}> }, \
             response: <withheld>, metadata: ExtractionMetadata { mode: Native, \
             passes: 0, enforcement: NotEnforced } } }"
        );
        let debug = format!("{refused_err:?}");
        assert!(
            debug.contains(
                "Refused { mode: Native, provider: \"demo\", finish_reason: Refusal, \
                 text: <withheld 22 bytes; print with {err}>, source: Model }"
            ),
            "{debug}"
        );
        assert!(!debug.contains("response"), "{debug}");
    }

    #[cfg(feature = "extract")]
    #[test]
    fn extraction_failures_serialize_under_failure() {
        use crate::extract::{ExtractError, ExtractFailure, ExtractionMetadata, ExtractionMode};

        let failure = ExtractFailure::new(
            ExtractError::ToolConflict {
                provider: "demo".into(),
            },
            ExtractionMetadata::new(ExtractionMode::ForcedTool),
        );
        let json = serde_json::to_value(Error::extract(failure)).unwrap();
        assert_eq!(json["type"], "extract");
        assert!(json["data"]["failure"].is_object(), "{json}");
        assert!(json["data"].get("error").is_none(), "{json}");
        let back: Error = serde_json::from_value(json).unwrap();
        let Error::Extract { failure, .. } = back else {
            panic!("an extraction failure expected: {back:?}");
        };
        assert!(matches!(failure.error(), ExtractError::ToolConflict { .. }));
    }

    #[test]
    fn message_prefix_keeps_kind_context_and_origin() {
        let context = |err: Error| {
            err.with_provider("demo")
                .with_status(503)
                .with_request_id("req_1")
                .with_retry_after(Duration::from_secs(3))
                .with_upstream_message("upstream")
        };
        let errors = [
            Error::timeout("m"),
            Error::transport("m"),
            Error::auth("m"),
            Error::configuration("m"),
            Error::rate_limited("m"),
            Error::quota_exhausted("m"),
            Error::overloaded("m"),
            Error::provider("m"),
            Error::unsupported("m"),
            Error::invalid_request("m"),
            Error::model_not_found("m"),
            Error::unexpected_response("m"),
            Error::context_length_exceeded("m", Some(8)),
            Error::content_filtered("m"),
            Error::stream(StreamErrorKind::Incomplete, "m"),
        ];
        for err in errors {
            for authored in [false, true] {
                let err = context(if authored {
                    err.clone().authored()
                } else {
                    err.clone()
                });
                let prefixed = err.clone().with_message_prefix("step");
                assert_eq!(prefixed.kind_name(), err.kind_name());
                assert_eq!(prefixed.message(), "step: m");
                assert_eq!(prefixed.message_origin(), err.message_origin());
                assert_eq!(prefixed.is_retryable(), err.is_retryable());
                assert_eq!(prefixed.upstream_message(), Some("upstream"));
                assert_eq!(prefixed.context(), err.context());
                let log = prefixed.as_log().to_string();
                assert_eq!(log.contains("step: m"), authored, "{log}");
            }
        }
    }

    #[test]
    fn message_prefix_goes_on_the_error_not_its_fallback_chain() {
        let err = Error::rate_limited("HTTP 429")
            .authored()
            .with_status(429)
            .with_fallback_from(Error::overloaded("q"))
            .with_fallback_from(Error::timeout("p").authored())
            .with_message_prefix("step");
        assert!(matches!(err, Error::RateLimited { .. }));
        assert_eq!(err.message(), "step: HTTP 429");
        assert_eq!(err.status(), Some(429));
        let primary = err.fallback_from().and_then(Error::fallback_from).unwrap();
        assert_eq!(primary.message(), "p");
        assert_eq!(
            err.as_log().to_string(),
            r#"kind=rate_limited status=429 message="step: HTTP 429" fallback_from=[kind=overloaded fallback_from=[kind=timeout message="p"]]"#
        );
    }

    #[test]
    fn message_prefix_on_serialization_keeps_the_failure() {
        let source = serde_json::from_str::<u64>("\"x\"").unwrap_err();
        let source_text = source.to_string();
        let err = Error::from(source).with_message_prefix("decode option");
        assert!(matches!(err, Error::Serialization { .. }));
        assert_eq!(err.message(), format!("decode option: {source_text}"));
        assert_eq!(
            err.to_string(),
            format!("serialization error: decode option: {source_text}")
        );
        assert_eq!(err.as_log().to_string(), "kind=serialization");

        // `Display` shows the failure, so the chain does not repeat it; the
        // failure itself stays reachable.
        assert!(std::error::Error::source(&err).is_none());
        let Error::Serialization { error } = &err else {
            unreachable!()
        };
        assert!(
            error
                .get_ref()
                .downcast_ref::<serde_json::Error>()
                .is_some()
        );

        let back: Error = serde_json::from_value(serde_json::to_value(&err).unwrap()).unwrap();
        assert_eq!(back.message(), err.message());
    }

    #[cfg(feature = "extract")]
    #[test]
    fn message_prefix_leaves_extract_unchanged() {
        use crate::extract::{ExtractError, ExtractFailure, ExtractionMetadata, ExtractionMode};

        let failure = ExtractFailure::new(
            ExtractError::ToolConflict {
                provider: "demo".into(),
            },
            ExtractionMetadata::new(ExtractionMode::Native),
        );
        let err = Error::extract(failure);
        let prefixed = err.clone().with_message_prefix("step");
        assert!(matches!(prefixed, Error::Extract { .. }));
        assert_eq!(prefixed.to_string(), err.to_string());
    }

    #[test]
    fn configuration_errors_are_terminal_and_not_auth() {
        let err = Error::configuration("ANTHROPIC_API_KEY not set").with_provider("anthropic");
        assert_eq!(err.kind_name(), "configuration");
        assert!(!err.is_retryable());
        assert!(!err.is_transient());
        assert!(!err.is_auth());
        let json = serde_json::to_value(&err).unwrap();
        assert_eq!(json["type"], "configuration");
        let back: Error = serde_json::from_value(json).unwrap();
        assert!(matches!(back, Error::Configuration { .. }));
        assert_eq!(
            back.context().unwrap().provider.as_deref(),
            Some("anthropic")
        );
        let log = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(log["type"], "configuration");
        assert_eq!(log["provider"], "anthropic");
    }

    #[test]
    fn serde_round_trip() {
        let err = Error::context_length_exceeded("too long", Some(128_000)).with_request_id("r");
        let json = serde_json::to_value(&err).unwrap();
        assert_eq!(json["data"]["context_window"], 128_000, "{json}");
        let log = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(log["context_window"], 128_000, "{log}");
        let back: Error = serde_json::from_value(json).unwrap();
        assert!(matches!(
            back,
            Error::ContextLengthExceeded {
                context_window: Some(128_000),
                ..
            }
        ));
        assert_eq!(back.request_id(), Some("r"));
    }

    /// Regression: a stored hint too large for a
    /// `Duration` used to panic while loading.
    #[test]
    fn deserializing_a_huge_retry_after_never_panics() {
        for value in ["1e20", "1.9e19", "-1", "1e308"] {
            let json = format!(
                r#"{{"type":"rate_limited","data":{{"message":"x","context":{{"retry_after":{value}}}}}}}"#
            );
            let err: Error = serde_json::from_str(&json).unwrap();
            assert_eq!(err.retry_after(), None, "{value}");
        }
        let err: Error = serde_json::from_str(
            r#"{"type":"rate_limited","data":{"message":"x","context":{"retry_after":1.5}}}"#,
        )
        .unwrap();
        assert_eq!(err.retry_after(), Some(Duration::from_millis(1500)));
    }

    /// The error's `Display` followed by each `source()`, as report chains
    /// (`anyhow`'s `{:#}`) print it.
    fn chain(error: &(dyn std::error::Error + 'static)) -> Vec<String> {
        let mut lines = vec![error.to_string()];
        let mut source = error.source();
        while let Some(cause) = source {
            lines.push(cause.to_string());
            source = cause.source();
        }
        lines
    }

    /// Regression: the fallback side, a
    /// serialization failure and a prefixed one were printed twice by a
    /// report chain: in `Display` and again as `source()`.
    #[test]
    fn report_chains_print_each_error_once() {
        // A failover's earlier errors are not causes.
        let fallback = Error::rate_limited("HTTP 429")
            .with_provider("openai")
            .with_fallback_from(Error::overloaded("HTTP 529").with_provider("anthropic"));
        assert_eq!(chain(&fallback), ["openai: rate limited: HTTP 429"]);

        let json_failure = serde_json::from_str::<u8>("x").unwrap_err();
        let serialization = Error::from(json_failure).with_message_prefix("tool arguments");
        let lines = chain(&serialization);
        assert_eq!(lines.len(), 1, "{lines:?}");
        assert!(lines[0].starts_with("serialization error: tool arguments: expected value"));
        let Error::Serialization { error } = &serialization else {
            unreachable!()
        };
        assert!(
            error
                .get_ref()
                .downcast_ref::<serde_json::Error>()
                .is_some()
        );
    }

    #[derive(Debug)]
    struct Cause(&'static str);

    impl fmt::Display for Cause {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str(self.0)
        }
    }

    impl std::error::Error for Cause {}

    /// A typed cause survives as `source()`, and
    /// `Display`, `Debug`, the log view and serde leave it out.
    #[test]
    fn an_attached_source_is_the_source_and_nothing_else() {
        use std::error::Error as _;

        let err = Error::transport("the request got no response")
            .authored()
            .with_source(Cause("dns: no such host"));
        let cause = err.source().unwrap();
        assert_eq!(
            cause.downcast_ref::<Cause>().unwrap().0,
            "dns: no such host"
        );
        assert_eq!(
            chain(&err),
            [
                "transport error: the request got no response",
                "dns: no such host"
            ]
        );
        assert!(!format!("{err:?}").contains("dns"));
        assert!(!err.as_log().to_string().contains("dns"));
        assert!(!serde_json::to_string(&err).unwrap().contains("dns"));

        // Clones share it; equality and `with_context` ignore or keep it.
        let clone = err.clone();
        assert!(clone.source().is_some());
        let merged = Error::transport("x").with_context(clone.context().unwrap().clone());
        assert!(merged.source().is_some());
        assert_eq!(
            clone.context().unwrap(),
            Error::transport("x").authored().context().unwrap()
        );

        // An error that ended a failover keeps its own source, and the
        // errors before it are not one.
        let fallback = err.clone().with_fallback_from(Error::timeout("p"));
        assert!(fallback.source().unwrap().downcast_ref::<Cause>().is_some());
        let before = Error::timeout("p").with_fallback_from(err);
        assert!(before.source().is_none());
    }

    /// A local refusal has its own kind, is not
    /// retryable, and round-trips.
    #[test]
    fn rejected_is_a_local_refusal() {
        let err = Error::rejected("tenant budget exhausted")
            .authored()
            .with_provider_code("tenant_budget_exhausted");
        assert_eq!(err.kind_name(), "rejected");
        assert!(!err.is_retryable());
        assert!(!err.is_transient());
        assert_eq!(err.to_string(), "rejected: tenant budget exhausted");
        assert_eq!(
            err.as_log().to_string(),
            r#"kind=rejected provider_code=tenant_budget_exhausted message="tenant budget exhausted""#
        );
        let json = serde_json::to_value(&err).unwrap();
        assert_eq!(json["type"], "rejected");
        let back: Error = serde_json::from_value(json).unwrap();
        assert!(matches!(back, Error::Rejected { .. }));
        assert_eq!(back.provider_code(), Some("tenant_budget_exhausted"));
    }

    /// Context builders take values; their `_opt` twins take options.
    #[test]
    fn context_builders_take_values_and_opt_twins_take_options() {
        let err = Error::rate_limited("HTTP 429")
            .with_retry_after(Duration::from_secs(30))
            .with_request_id("req_1")
            .with_server_retry_directive(true)
            .with_provider_code("rate_limit_exceeded")
            .with_provider_type("requests")
            .with_param("model")
            .with_upstream_message("slow down")
            .with_body("{}");
        let kept = err
            .clone()
            .with_retry_after_opt(None)
            .with_request_id_opt(None)
            .with_server_retry_directive_opt(None)
            .with_provider_code_opt(None)
            .with_provider_type_opt(None)
            .with_param_opt(None)
            .with_upstream_message_opt(Some("  "))
            .with_upstream_message("")
            .with_body_opt(None);
        assert_eq!(kept.context(), err.context());
        let header: Option<&str> = Some("req_2");
        assert_eq!(err.with_request_id_opt(header).request_id(), Some("req_2"));
    }
}