axond 0.3.7

Axond — a stateless, single-binary, self-hosted AI gateway: one place for provider keys, model routing, usage, and telemetry.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
//! HTTP surface.
//!
//! Passthrough-first (delta A1): the OpenAI-shaped `/v1/chat/completions` route
//! forwards the caller's body to a same-shaped upstream and only rewrites the
//! `model` field to the resolved target. Cross-provider translation (e.g.
//! routing an OpenAI request to Anthropic) is deferred, so an alias whose
//! targets cannot serve a route's wire is rejected up front rather than
//! dispatched (ADR 0012). A `stream: true` request takes the SSE relay in
//! [`crate::streaming`].
//!
//! The provider-native routes — Anthropic's `/v1/messages` and OpenAI-shaped
//! `/v1/embeddings` — take the same path (ADR 0012). A caller already speaking
//! the target's wire has its body forwarded to the provider's own endpoint with
//! only `model` rewritten, so signed thinking and tool-use blocks survive intact
//! (verbatim bytes when streamed, re-serialized values when buffered); only how
//! usage is read back differs per route. `/v1/responses`
//! is a native OpenAI Responses passthrough.
//!
//! An alias's `targets` are tried in configured order (ADR 0008). The failover
//! walk is the *outer* loop around credential-pool dispatch: each target has an
//! in-memory per-target circuit breaker, a retryable upstream failure advances
//! to the next target, and the walk is bounded by both a total attempt count and
//! an overall wall-clock budget. Streaming rotates credentials while opening on
//! both wires, and may rotate after an OpenAI-framed stream fails before content
//! is emitted; native streams and partially delivered streams remain terminal.

use std::collections::HashSet;
use std::sync::Arc;
use std::task::Poll;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use axum::body::Body;
use axum::extract::rejection::JsonRejection;
use axum::extract::{DefaultBodyLimit, Extension, RawQuery, Request, State};
use axum::http::{HeaderMap, HeaderValue, StatusCode};
use axum::middleware::{Next, from_fn_with_state};
use axum::response::{IntoResponse, Response};
use axum::routing::{MethodRouter, get, post};
use axum::{Json, Router};
use futures::StreamExt;
use gateway_core::{
    CircuitDecision, FailoverDecision, FailoverPolicy, FailoverTarget, ModelPrice, ModelUsage,
    NativeMessagesDecoder, ProviderError, ProviderRequest, ProviderResponse, ProviderStreamDecoder,
    Surface, Usage,
};
use gateway_transport::{
    AuthScheme, Deadline, NativeCall, TimeoutBound, TimeoutKind, TransportError, Upstream,
};
use secrecy::ExposeSecret;
use serde::Deserialize;
use serde_json::{Value, json};
use tracing::{Instrument, debug, warn};
use tracing_opentelemetry::OpenTelemetrySpanExt;

use crate::admission::{AdmissionPermit, RequestKind};
use crate::aliases::AliasScope;
use crate::budget::{Admission, BudgetKey, Denial, Reservation};
use crate::config::{Model, Provider, ProviderKind, ProviderWire, Target};
use crate::credentials::{CredentialLease, CredentialPlan, CredentialSource, CredentialStatusView};
use crate::error::GatewayError;
use crate::mint::{MintRequest, mint_issued_at, mint_token_at};
use crate::principals::{
    Capability, Presented, PrincipalAuthority, PrincipalStoreError, TokenVerificationError,
};
use crate::rate_limit::{RateLimitKey, RateLimitPermit};
use crate::shutdown::Phase;
use crate::state::{AppState, ConfigSnapshot, InboundKey, adapter_for};
use crate::streaming::{self, Framing, StreamContext};
use crate::telemetry;
use crate::usage::{Status, UsageRecord};

pub fn router(state: AppState) -> Router {
    let minting_enabled = state.config().gateway_minting.is_some();
    // The inbound body bound is declared rather than inherited: axum's own
    // default would otherwise be the process's real memory ceiling per request.
    let max_request_bytes = state.0.admission.limits().max_request_bytes;
    route_specs(minting_enabled)
        .into_iter()
        .fold(Router::new(), |router, spec| {
            let route = (spec.router)().layer(DefaultBodyLimit::max(max_request_bytes));
            let route = match spec.auth {
                AuthPosture::LivenessProbe => route,
                // Admission is the outermost layer, so a request arriving after
                // the drain window is refused before it touches authentication,
                // budgets, or an upstream. The probes deliberately stay outside
                // it: a draining replica is still alive, and killing it early
                // would cut the very requests the drain exists to finish.
                AuthPosture::Authenticated => route
                    .layer(from_fn_with_state(
                        (state.clone(), spec.capability),
                        authenticate_middleware,
                    ))
                    .layer(from_fn_with_state(state.clone(), admission_middleware)),
            };
            router.route(spec.path, route)
        })
        .with_state(state)
}

/// Whether a route is one of the two unauthenticated liveness probes or must
/// pass inbound authentication before its handler can run.
#[derive(Clone, Copy, PartialEq, Eq)]
enum AuthPosture {
    LivenessProbe,
    Authenticated,
}

/// A route's complete registration: adding a route requires declaring its
/// authentication posture here rather than silently omitting the layer.
struct RouteSpec {
    path: &'static str,
    auth: AuthPosture,
    capability: Option<Capability>,
    router: fn() -> MethodRouter<AppState>,
}

/// The single route table: its posture is the source of truth for registration
/// and for the sweep test that keeps the unauthenticated set closed.
fn route_specs(minting_enabled: bool) -> Vec<RouteSpec> {
    let mut routes = vec![
        RouteSpec {
            path: "/healthz",
            auth: AuthPosture::LivenessProbe,
            capability: None,
            router: || get(healthz),
        },
        RouteSpec {
            path: "/readyz",
            auth: AuthPosture::LivenessProbe,
            capability: None,
            router: || get(readyz),
        },
        RouteSpec {
            path: "/v1/models",
            auth: AuthPosture::Authenticated,
            capability: Some(Capability::Models),
            router: || get(list_models),
        },
        RouteSpec {
            path: "/v1/credentials",
            auth: AuthPosture::Authenticated,
            capability: Some(Capability::Credentials),
            router: || get(list_credentials),
        },
        RouteSpec {
            path: "/v1/chat/completions",
            auth: AuthPosture::Authenticated,
            capability: Some(Capability::Chat),
            router: || post(chat_completions),
        },
        RouteSpec {
            path: "/v1/messages",
            auth: AuthPosture::Authenticated,
            capability: Some(Capability::Messages),
            router: || post(native_messages),
        },
        RouteSpec {
            path: "/v1/embeddings",
            auth: AuthPosture::Authenticated,
            capability: Some(Capability::Embeddings),
            router: || post(embeddings),
        },
        RouteSpec {
            path: "/v1/responses",
            auth: AuthPosture::Authenticated,
            capability: Some(Capability::Responses),
            router: || post(responses),
        },
    ];
    if minting_enabled {
        routes.push(RouteSpec {
            path: "/v1/tokens",
            auth: AuthPosture::Authenticated,
            capability: None,
            router: || post(mint_tokens),
        });
    }
    routes
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct MintTokenRequest {
    sub: String,
    ttl_seconds: Option<u64>,
    scope: Option<Vec<String>>,
    aliases: Option<Vec<String>>,
    max_request_microdollars: Option<u64>,
}

const MAX_MINT_SUBJECT_LENGTH: usize = 128;

async fn mint_tokens(
    Extension(snapshot): Extension<Arc<ConfigSnapshot>>,
    Extension(caller): Extension<InboundKey>,
    body: Result<Json<MintTokenRequest>, axum::extract::rejection::JsonRejection>,
) -> Result<(HeaderMap, Json<Value>), GatewayError> {
    let minting = snapshot
        .gateway_minting
        .as_ref()
        .ok_or(GatewayError::MintingDisabled)?;
    if !caller.can_mint {
        return Err(GatewayError::MintNotAuthorized);
    }
    let Json(request) = body.map_err(|error| GatewayError::BadRequest(error.to_string()))?;
    if request.sub.trim().is_empty() {
        return Err(GatewayError::BadRequest("`sub` must not be empty".into()));
    }
    let subject = request.sub.trim().to_owned();
    if subject.chars().count() > MAX_MINT_SUBJECT_LENGTH
        || !subject.chars().all(|character| {
            character.is_ascii_alphanumeric() || character == '_' || matches!(character, '.' | '-')
        })
    {
        return Err(GatewayError::BadRequest(format!(
            "`sub` must be at most {MAX_MINT_SUBJECT_LENGTH} ASCII letters, digits, or `_.-`"
        )));
    }
    let epoch = snapshot.gateway_token_epoch(&caller.namespace, &subject);
    let iat = epoch
        .map(|min_iat| {
            mint_issued_at(Some(min_iat)).map_err(|_| GatewayError::MintEpochNotUsable {
                kid: minting.kid.clone(),
                min_iat,
            })
        })
        .transpose()?;
    let ttl = Duration::from_secs(request.ttl_seconds.unwrap_or(minting.max_ttl.as_secs()));
    if ttl.is_zero() || ttl > minting.max_ttl {
        return Err(GatewayError::MintClaimsNotNarrowing);
    }
    let scope = match request.scope {
        Some(values) => {
            let parsed = values
                .iter()
                .map(|value| {
                    Capability::parse(value).ok_or_else(|| {
                        GatewayError::BadRequest(format!("unknown scope capability `{value}`"))
                    })
                })
                .collect::<Result<Vec<_>, _>>()?;
            let requested = parsed.iter().copied().collect::<HashSet<_>>();
            if let Some(ceiling) = &minting.scope
                && !requested.is_subset(ceiling)
            {
                return Err(GatewayError::MintClaimsNotNarrowing);
            }
            Some(parsed)
        }
        None => Some(
            minting
                .scope
                .as_ref()
                .map(|values| values.iter().copied().collect())
                .unwrap_or_else(|| {
                    Capability::ALL
                        .iter()
                        .copied()
                        .filter(|capability| !capability.is_operator_only())
                        .collect()
                }),
        ),
    };
    if scope.as_ref().is_some_and(|capabilities| {
        capabilities
            .iter()
            .any(|capability| !caller_can_mint_capability(&caller, &snapshot, *capability))
    }) {
        return Err(GatewayError::MintClaimsNotNarrowing);
    }
    let aliases = match request.aliases {
        Some(values) => {
            let requested = AliasScope::parse(values.iter().map(String::as_str))
                .map_err(|error| GatewayError::BadRequest(error.to_string()))?;
            if let Some(ceiling) = &minting.aliases
                && !requested.is_subset_of(ceiling)
            {
                return Err(GatewayError::MintClaimsNotNarrowing);
            }
            Some(values)
        }
        None => minting
            .aliases
            .as_ref()
            .map(|aliases| aliases.patterns_for_claim()),
    };
    let max_request_microdollars = match request.max_request_microdollars {
        Some(value) if value >= 1 => {
            if minting
                .max_request_microdollars
                .is_some_and(|ceiling| value > ceiling)
            {
                return Err(GatewayError::MintClaimsNotNarrowing);
            }
            Some(value)
        }
        Some(_) => return Err(GatewayError::MintClaimsNotNarrowing),
        None => minting.max_request_microdollars,
    };
    if !snapshot
        .config
        .gateway_verifier
        .iter()
        .find(|verifier| verifier.kid == minting.kid)
        .is_some_and(|verifier| verifier.namespaces.iter().any(|ns| ns == &caller.namespace))
    {
        return Err(GatewayError::MintClaimsNotNarrowing);
    }
    let minted = mint_token_at(
        MintRequest {
            kid: &minting.kid,
            algorithm: minting.algorithm,
            key_material: minting.key_material.expose_secret(),
            namespace: &caller.namespace,
            subject: &subject,
            audience: &minting.audience,
            ttl,
            aliases,
            max_request_microdollars,
            scope,
        },
        iat,
    )
    .map_err(|error| GatewayError::BadRequest(error.to_string()))?;
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| duration.as_secs());
    let expires_in = minted.exp.saturating_sub(now);
    let mut headers = HeaderMap::new();
    headers.insert(
        axum::http::header::CACHE_CONTROL,
        HeaderValue::from_static("no-store"),
    );
    Ok((
        headers,
        Json(json!({
            "token": minted.token,
            "exp": minted.exp,
            "expires_in": expires_in,
            "namespace": caller.namespace,
            "sub": subject,
        })),
    ))
}

fn caller_can_mint_capability(
    caller: &InboundKey,
    snapshot: &ConfigSnapshot,
    capability: Capability,
) -> bool {
    caller
        .scope
        .as_ref()
        .map_or(!capability.is_operator_only(), |scope| {
            scope.contains(&capability) && namespace_allows(snapshot, &caller.namespace, capability)
        })
}

/// Reserve a slot for a request and hold it until the response body is fully
/// delivered, so an open SSE stream counts as in-flight for as long as it runs.
async fn admission_middleware(
    State(state): State<AppState>,
    request: Request,
    next: Next,
) -> Result<Response, GatewayError> {
    let Some(admitted) = state.lifecycle().admit() else {
        telemetry::metrics::record_shutdown_rejection();
        return Err(GatewayError::Draining);
    };
    let lifecycle = Arc::clone(state.lifecycle());
    // A request still inside its handler has no body to end, so the deadline has
    // to reach it here: dropping the handler future cancels the upstream call at
    // its next await, and the guards it holds settle on the ordinary
    // cancellation path — the budget hold is released rather than charged,
    // because a caller that received nothing owes nothing. Without this arm such
    // a request would hold its admission slot until its own upstream budget
    // expired, spending the flush budget the usage records need.
    let response = tokio::select! {
        response = next.run(request) => response,
        () = lifecycle.abandoned() => return Err(GatewayError::Draining),
    };
    let (parts, body) = response.into_parts();
    let mut relayed = Some(body.into_data_stream());
    let mut admitted = Some(admitted);
    let mut abandoned = Box::pin(async move { lifecycle.abandoned().await });
    // The guard rides along inside the body, not this future: the response is
    // handed to hyper long before a streamed body ends, so releasing the slot
    // here would undercount every stream. Ending the body when the shutdown
    // deadline expires is also what settles an abandoned stream's spend, since
    // dropping the inner body is what cancels it upstream.
    let body = Body::from_stream(futures::stream::poll_fn(move |cx| {
        let Some(inner) = relayed.as_mut() else {
            return Poll::Ready(None);
        };
        if abandoned.as_mut().poll(cx).is_ready() {
            // Dropping the inner body cancels the stream upstream, which settles
            // the spend accrued so far; releasing the slot lets shutdown see it.
            drop(relayed.take());
            drop(admitted.take());
            // An error rather than a clean end: the caller must not read a
            // truncated stream as a complete answer.
            return Poll::Ready(Some(Err(axum::Error::new(std::io::Error::other(
                "the gateway shut down before this response finished",
            )))));
        }
        match inner.poll_next_unpin(cx) {
            Poll::Ready(None) => {
                drop(relayed.take());
                drop(admitted.take());
                Poll::Ready(None)
            }
            other => other,
        }
    }));
    Ok(Response::from_parts(parts, body))
}

async fn healthz() -> &'static str {
    "ok"
}

/// Readiness is where a rolling deployment learns this replica is leaving: it
/// fails as soon as the drain begins, before admission closes, so a load
/// balancer can stop routing while the replica is still able to serve. Real
/// dependency readiness (config loaded, credentials present) is a follow-up.
async fn readyz(State(state): State<AppState>) -> (StatusCode, &'static str) {
    match state.lifecycle().phase() {
        Phase::Serving => (StatusCode::OK, "ready"),
        Phase::Draining | Phase::Closing => (StatusCode::SERVICE_UNAVAILABLE, "draining"),
    }
}

/// Replica-local Tier 0 credential status. Presence is expressed by each
/// configured entry (boot resolves it or boot fails), never by an always-true
/// field. Credential ids are attribution labels only; secrets remain write-only.
async fn list_credentials(
    Extension(snapshot): Extension<Arc<ConfigSnapshot>>,
    Extension(caller): Extension<InboundKey>,
    RawQuery(raw_query): RawQuery,
) -> Result<Json<Value>, GatewayError> {
    let namespaces = parse_credential_query(raw_query.as_deref())?;
    let view = match namespaces.as_deref() {
        None => CredentialStatusView::Namespace(&caller.namespace),
        Some("all") => {
            if !caller_holds_direct_operator_authority(&caller, &snapshot) {
                return Err(GatewayError::ScopeInsufficient(Capability::CredentialsAll));
            }
            CredentialStatusView::All
        }
        Some(_) => {
            return Err(GatewayError::BadRequest(
                "invalid `namespaces` value".into(),
            ));
        }
    };
    Ok(Json(json!({
        "object": "list",
        "observed": "replica",
        "data": snapshot.credentials.status(&snapshot.config, view),
    })))
}

/// Whether the caller holds the operator's own authority over the whole
/// deployment, which is what the all-namespaces credential view exposes.
///
/// This is deliberately not `caller_can_mint_capability`: that predicate asks
/// whether a caller may *delegate* a capability to a subject, while this one
/// asks whether the caller *is* the operator. Only a configured static gateway
/// key in the default namespace is, because an operator placed that secret there
/// itself; a scope narrows a static key rather than widening it, so a scoped one
/// is not treated as unrestricted. Every minted token carries delegated
/// authority bounded by minting and its verifier, so no token reaches this view,
/// including one that presents `credentials:all` from a signer outside
/// `POST /v1/tokens`.
fn caller_holds_direct_operator_authority(caller: &InboundKey, snapshot: &ConfigSnapshot) -> bool {
    caller.authority == PrincipalAuthority::StaticKey
        && caller.scope.is_none()
        && caller.namespace == snapshot.config.default_namespace()
}

fn parse_credential_query(raw_query: Option<&str>) -> Result<Option<String>, GatewayError> {
    let mut namespaces = None;
    for pair in raw_query.unwrap_or_default().split('&') {
        if pair.is_empty() {
            continue;
        }
        let (raw_key, raw_value) = pair.split_once('=').unwrap_or((pair, ""));
        let key = decode_query_component(raw_key)?;
        let value = decode_query_component(raw_value)?;
        if key == "namespaces" {
            if namespaces.is_some() {
                return Err(GatewayError::BadRequest(
                    "duplicate query parameter `namespaces`".into(),
                ));
            }
            namespaces = Some(value);
        }
    }
    Ok(namespaces)
}

fn decode_query_component(value: &str) -> Result<String, GatewayError> {
    let bytes = value.as_bytes();
    let mut decoded = Vec::with_capacity(bytes.len());
    let mut index = 0;
    while index < bytes.len() {
        match bytes[index] {
            b'+' => decoded.push(b' '),
            b'%' if index + 2 < bytes.len() => {
                let high = hex_digit(bytes[index + 1]);
                let low = hex_digit(bytes[index + 2]);
                let (Some(high), Some(low)) = (high, low) else {
                    return Err(GatewayError::BadRequest(
                        "invalid query string encoding".into(),
                    ));
                };
                decoded.push((high << 4) | low);
                index += 2;
            }
            b'%' => {
                return Err(GatewayError::BadRequest(
                    "invalid query string encoding".into(),
                ));
            }
            byte => decoded.push(byte),
        }
        index += 1;
    }
    String::from_utf8(decoded)
        .map_err(|_| GatewayError::BadRequest("invalid query string encoding".into()))
}

fn hex_digit(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'a'..=b'f' => Some(byte - b'a' + 10),
        b'A'..=b'F' => Some(byte - b'A' + 10),
        _ => None,
    }
}

/// The model catalog, gated behind a gateway key and scoped to the caller's
/// namespace: a caller sees only the aliases it could actually invoke — those
/// with at least one target whose provider resolves a credential for the
/// caller's namespace (its own, or the platform's when fallback is allowed).
/// So a BYOK tenant cannot enumerate aliases it is not entitled to.
async fn list_models(
    Extension(snapshot): Extension<Arc<ConfigSnapshot>>,
    Extension(caller): Extension<InboundKey>,
) -> Result<Json<Value>, GatewayError> {
    let cfg = &snapshot.config;
    let data: Vec<Value> = cfg
        .model
        .iter()
        .filter(|m| {
            caller
                .alias_scope
                .as_ref()
                .is_none_or(|scope| scope.permits(&m.name))
                && m.targets.iter().any(|t| {
                    snapshot
                        .credentials
                        .is_present(cfg, &caller.namespace, &t.provider)
                })
        })
        .map(|m| json!({ "id": m.name, "object": "model", "owned_by": "axond" }))
        .collect();
    Ok(Json(json!({ "object": "list", "data": data })))
}

/// Resolve the caller's namespace + subject from the inbound key. Every request
/// must present a configured gateway key: authentication fails closed, and a
/// snapshot with no key never reaches a request (ADR 0013).
///
/// The key travels as `Authorization: Bearer` or, because that is what an
/// Anthropic SDK pointed at the gateway sends, as `x-api-key`. Both name the
/// same gateway key; the scheme is the client's, not a second credential space.
async fn authenticate(
    snapshot: &ConfigSnapshot,
    headers: &HeaderMap,
) -> Result<InboundKey, GatewayError> {
    let credential = headers
        .get(axum::http::header::AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.strip_prefix("Bearer "))
        .or_else(|| headers.get("x-api-key").and_then(|v| v.to_str().ok()))
        .ok_or(GatewayError::Unauthorized)?;
    let presented = Presented { credential };
    let store = snapshot.principal_store_name(&presented);
    let principal = match snapshot.resolve_principal(&presented).await {
        Ok(principal) => principal,
        Err(PrincipalStoreError::Unauthorized(error)) => {
            debug!(
                store,
                error = %error,
                "token rejected during principal resolution"
            );
            return Err(GatewayError::TokenUnauthorized(error));
        }
        Err(PrincipalStoreError::Forbidden(error)) => {
            debug!(
                store,
                error = %error,
                "token rejected during principal resolution"
            );
            return Err(GatewayError::TokenForbidden(error));
        }
        Err(error) => {
            // A layer error is terminal by design; it must not fall through to
            // another authority just because the owning layer is unavailable.
            warn!(
                store,
                error = %error,
                "principal store resolution failed"
            );
            return Err(GatewayError::Unauthorized);
        }
    };
    principal.ok_or(GatewayError::Unauthorized)
}

/// Authenticate once per request, before handler extractors, and carry the
/// resolved snapshot and caller into the handler. A reload landing mid-request
/// therefore cannot change what this request resolved; failures return `401`
/// before any typed handler error.
async fn authenticate_middleware(
    State((state, capability)): State<(AppState, Option<Capability>)>,
    headers: HeaderMap,
    mut request: Request,
    next: Next,
) -> Result<Response, GatewayError> {
    let snapshot = state.config();
    let caller = authenticate(&snapshot, &headers).await?;
    if let Some(jti) = &caller.jti {
        match state.0.revocation.is_revoked(jti).await {
            Ok(true) => {
                crate::telemetry::metrics::record_revocation_denial();
                return Err(GatewayError::TokenUnauthorized(
                    TokenVerificationError::Revoked,
                ));
            }
            Ok(false) => {}
            Err(crate::revocation::RevocationError::Unavailable { .. }) => {
                crate::telemetry::metrics::record_revocation_unavailable_denial();
                return Err(GatewayError::RevocationUnavailable);
            }
            Err(error) => {
                warn!(error = %error, "revocation store check failed");
                crate::telemetry::metrics::record_revocation_unavailable_denial();
                return Err(GatewayError::RevocationUnavailable);
            }
        }
    }
    if let Some(capability) = capability
        && let Some(scope) = caller.scope.as_ref()
        && (!scope.contains(&capability)
            || !namespace_allows(&snapshot, &caller.namespace, capability))
    {
        debug!(
            namespace = %caller.namespace,
            subject = %caller.subject,
            signer_kid = ?caller.signer_kid,
            %capability,
            "token scope denied route"
        );
        return Err(GatewayError::ScopeInsufficient(capability));
    }
    request.extensions_mut().insert(snapshot);
    request.extensions_mut().insert(caller);
    Ok(next.run(request).await)
}

fn namespace_allows(snapshot: &ConfigSnapshot, namespace: &str, capability: Capability) -> bool {
    let route = match capability {
        Capability::Chat => Some(Route::ChatCompletions),
        Capability::Messages => Some(Route::NativeMessages),
        Capability::Embeddings => Some(Route::Embeddings),
        Capability::Responses => Some(Route::Responses),
        Capability::Models => None,
        Capability::Credentials | Capability::CredentialsAll => None,
    };
    let Some(route) = route else {
        return true;
    };
    snapshot.config.model.iter().any(|model| {
        model.targets.iter().any(|target| {
            snapshot
                .config
                .provider(&target.provider)
                .is_some_and(|provider| {
                    route.serves(provider.kind)
                        && snapshot.credentials.is_present(
                            &snapshot.config,
                            namespace,
                            &target.provider,
                        )
                })
        })
    })
}

/// The wire shape a route speaks, which is the only thing that differs between
/// the routes: the upstream path, which provider kinds can serve it, and how
/// usage is read out of the provider's answer. Everything else — aliasing,
/// failover, credential pools, budgets, usage — is shared.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Route {
    /// OpenAI-shaped chat, dispatched through the provider adapter to an
    /// OpenAI-family target's `/chat/completions`.
    ChatCompletions,
    /// Anthropic-shaped Messages, forwarded verbatim to an Anthropic target.
    NativeMessages,
    /// OpenAI-shaped embeddings, forwarded verbatim to an OpenAI-family target.
    Embeddings,
    /// OpenAI Responses, forwarded verbatim to an OpenAI-family target.
    Responses,
}

impl Route {
    /// The caller-facing path, for error messages.
    fn label(self) -> &'static str {
        match self {
            Self::ChatCompletions => "/v1/chat/completions",
            Self::NativeMessages => "/v1/messages",
            Self::Embeddings => "/v1/embeddings",
            Self::Responses => "/v1/responses",
        }
    }

    /// Path appended to the provider's `base_url`.
    fn upstream_path(self) -> &'static str {
        match self {
            Self::ChatCompletions => "/chat/completions",
            Self::NativeMessages => "/messages",
            Self::Embeddings => "/embeddings",
            Self::Responses => "/responses",
        }
    }

    /// Whether a provider of this kind speaks the route's wire shape. No route
    /// translates between wires, so an alias whose target cannot serve the shape
    /// is a configuration mistake worth naming.
    fn serves(self, kind: ProviderKind) -> bool {
        self.wire() == kind.wire()
    }

    fn wire(self) -> ProviderWire {
        match self {
            Self::ChatCompletions | Self::Embeddings | Self::Responses => ProviderWire::Openai,
            Self::NativeMessages => ProviderWire::Anthropic,
        }
    }

    fn streamable(self) -> bool {
        self != Self::Embeddings
    }

    /// A stored Responses id only resolves on the provider — and under the
    /// credential — that stored it, so *every* Responses request, initial ones
    /// included, uses only the first configured target and credential. That is
    /// what lets a later continuation recover the same affinity without any
    /// durable state: had the initial call failed over, its response id would
    /// live on an upstream no continuation can reach.
    fn pins_affinity(self) -> bool {
        self == Self::Responses
    }

    /// Whether this request continues a provider-stored response. Only these
    /// carry continuity that can be lost, so only these report
    /// `continuation_affinity_unavailable`; a pinned *initial* request that
    /// cannot use its target reports the ordinary routing or credential error.
    /// Null and empty values are ordinary non-continuation requests.
    fn is_continuation(self, body: &Value) -> bool {
        self.pins_affinity()
            && body
                .get("previous_response_id")
                .and_then(Value::as_str)
                .is_some_and(|id| !id.is_empty())
    }

    fn max_attempts(self, configured: u32) -> u32 {
        if self.pins_affinity() { 1 } else { configured }
    }

    fn framing(self) -> Framing {
        match self {
            Self::ChatCompletions => Framing::OpenAiSse,
            Self::NativeMessages | Self::Embeddings => Framing::Native,
            Self::Responses => Framing::Responses,
        }
    }

    /// Usage from a *native* response, mapped onto the canonical record every
    /// route produces. Wire knowledge lives in `gateway-core`.
    fn native_usage(self, response: &Value) -> ModelUsage {
        match self {
            Self::NativeMessages => gateway_core::native_message_usage(response),
            Self::Responses => gateway_core::responses_usage(response),
            // Chat never takes this path (its adapter reports usage), so the
            // OpenAI-shaped prompt-only reader is the honest default.
            Self::ChatCompletions | Self::Embeddings => gateway_core::embeddings_usage(response),
        }
    }

    /// Pre-dispatch estimate the budget hold is priced from. Embeddings produce
    /// no completion, so nothing is held for output.
    fn estimate(self, body: &Value) -> Usage {
        let estimate = estimate_usage(body);
        match self {
            Self::Embeddings => Usage {
                output_tokens: 0,
                ..estimate
            },
            _ => estimate,
        }
    }

    /// Headers the wire shape itself requires upstream. Anthropic needs a
    /// version; the caller's own value wins so an SDK pinned to a newer wire
    /// keeps its behaviour, and its `anthropic-beta` opt-ins travel as sent.
    fn wire_headers(self, headers: &HeaderMap) -> Vec<(&'static str, String)> {
        if self != Self::NativeMessages {
            return Vec::new();
        }
        let mut wire = vec![(
            "anthropic-version",
            headers
                .get("anthropic-version")
                .and_then(|value| value.to_str().ok())
                .unwrap_or(gateway_core::AnthropicAdapter::VERSION)
                .to_owned(),
        )];
        if let Some(beta) = headers.get("anthropic-beta").and_then(|v| v.to_str().ok()) {
            wire.push(("anthropic-beta", beta.to_owned()));
        }
        wire
    }
}

/// A route plus the wire headers this request carries upstream, threaded through
/// the shared failover walk so both dispatch shapes reuse one request path.
#[derive(Clone)]
struct Wire {
    route: Route,
    headers: Vec<(&'static str, String)>,
}

impl Wire {
    /// Reject an alias whose targets cannot speak the route's wire *before*
    /// anything is reserved or dispatched: no route translates between wires,
    /// and failing over into a target that cannot serve the shape would turn a
    /// config mistake into a confusing upstream `404`.
    fn check_targets(
        &self,
        cfg: &crate::config::Config,
        model: &Model,
        alias: &str,
    ) -> Result<(), GatewayError> {
        for target in &model.targets {
            let Some(provider) = cfg.provider(&target.provider) else {
                continue;
            };
            if !self.route.serves(provider.kind) {
                return Err(GatewayError::UnsupportedWire {
                    route: self.route.label(),
                    alias: alias.to_owned(),
                    provider: provider.id.clone(),
                });
            }
        }
        Ok(())
    }

    fn call(&self, body: Value, provider: &'static str) -> NativeCall {
        NativeCall {
            provider,
            path: self.route.upstream_path(),
            body,
            headers: self.headers.clone(),
        }
    }
}

/// The inbound body, or a typed refusal. An oversized body is a bound the
/// gateway imposed (`413`), a wrong media type is `415` as axum's extractor
/// already answered it, and a malformed one is the caller's (`400`); no
/// response echoes the body it read.
fn inbound_body(body: Result<Json<Value>, JsonRejection>) -> Result<Value, GatewayError> {
    match body {
        Ok(Json(body)) => Ok(body),
        Err(rejection) if rejection.status() == StatusCode::PAYLOAD_TOO_LARGE => {
            Err(GatewayError::RequestTooLarge)
        }
        // Axum answered this arm with `415` before these rejections were mapped,
        // so it keeps that status; only the body becomes typed.
        Err(JsonRejection::MissingJsonContentType(_)) => Err(GatewayError::UnsupportedMediaType),
        Err(_) => Err(GatewayError::BadRequest(
            "request body is not valid JSON".into(),
        )),
    }
}

async fn chat_completions(
    State(state): State<AppState>,
    headers: HeaderMap,
    Extension(snapshot): Extension<Arc<ConfigSnapshot>>,
    Extension(caller): Extension<InboundKey>,
    body: Result<Json<Value>, JsonRejection>,
) -> Result<Response, GatewayError> {
    serve(
        state,
        headers,
        inbound_body(body)?,
        Route::ChatCompletions,
        snapshot,
        caller,
    )
    .await
}

/// Anthropic-native Messages. The caller's body already speaks the target's
/// wire, so it is forwarded to the provider's `/messages` untouched but for the
/// `model` alias — which is what keeps signed thinking and tool-use blocks
/// intact through the gateway (ADR 0012).
async fn native_messages(
    State(state): State<AppState>,
    headers: HeaderMap,
    Extension(snapshot): Extension<Arc<ConfigSnapshot>>,
    Extension(caller): Extension<InboundKey>,
    body: Result<Json<Value>, JsonRejection>,
) -> Result<Response, GatewayError> {
    serve(
        state,
        headers,
        inbound_body(body)?,
        Route::NativeMessages,
        snapshot,
        caller,
    )
    .await
}

async fn embeddings(
    State(state): State<AppState>,
    headers: HeaderMap,
    Extension(snapshot): Extension<Arc<ConfigSnapshot>>,
    Extension(caller): Extension<InboundKey>,
    body: Result<Json<Value>, JsonRejection>,
) -> Result<Response, GatewayError> {
    serve(
        state,
        headers,
        inbound_body(body)?,
        Route::Embeddings,
        snapshot,
        caller,
    )
    .await
}

async fn responses(
    State(state): State<AppState>,
    headers: HeaderMap,
    Extension(snapshot): Extension<Arc<ConfigSnapshot>>,
    Extension(caller): Extension<InboundKey>,
    body: Result<Json<Value>, JsonRejection>,
) -> Result<Response, GatewayError> {
    serve(
        state,
        headers,
        inbound_body(body)?,
        Route::Responses,
        snapshot,
        caller,
    )
    .await
}

/// The one request path every route shares: use the authenticated request
/// context, resolve the alias, hold a budget estimate, dispatch through the
/// failover walk, then settle the hold and record exactly one usage record.
/// Routes differ only in the wire they speak — where the body goes upstream and
/// how usage is read back out (see [`Route`]).
async fn serve(
    state: AppState,
    headers: HeaderMap,
    body: Value,
    route: Route,
    snapshot: Arc<ConfigSnapshot>,
    caller: InboundKey,
) -> Result<Response, GatewayError> {
    let cfg = &snapshot.config;

    let streamed = route.streamable() && body.get("stream").and_then(Value::as_bool) == Some(true);

    let alias = body
        .get("model")
        .and_then(Value::as_str)
        .ok_or_else(|| GatewayError::BadRequest("missing `model`".into()))?
        .to_string();

    if let Some(scope) = &caller.alias_scope
        && !scope.permits(&alias)
    {
        return Err(GatewayError::TokenForbidden(
            TokenVerificationError::AliasNotPermitted { alias },
        ));
    }

    let model = cfg
        .model(&alias)
        .ok_or_else(|| GatewayError::UnknownModel(alias.clone()))?;
    let wire = Wire {
        route,
        headers: route.wire_headers(&headers),
    };
    wire.check_targets(cfg, model, &alias)?;

    // The per-request bounds are checked before any dependency work and before
    // admission: they are pure functions of the parsed body, and a request that
    // cannot legally be served should not occupy capacity while it is refused.
    // Neither error repeats any part of the body.
    let estimate = route.estimate(&body);
    let limits = state.0.admission.limits();
    if let Some(limit_tokens) = limits.max_prompt_tokens
        && estimate.input_tokens > limit_tokens
    {
        return Err(GatewayError::PromptTooLarge { limit_tokens });
    }
    if let Some(limit_tokens) = limits.max_output_tokens
        && let Some(requested_tokens) = requested_output_tokens(&body)
        && requested_tokens > limit_tokens
    {
        return Err(GatewayError::OutputLimitExceeded {
            requested_tokens,
            limit_tokens,
        });
    }

    // Load shedding before any dependency work: an overloaded replica must not
    // spend a rate-limit round trip or a budget reservation on a request it is
    // about to refuse. It is also strictly after authentication, so unauthenticated
    // traffic can never occupy the process's or a tenant's capacity.
    //
    // Held for the request's lifetime the same way the rate-limit permit is:
    // dropped at scope end on a buffered request, moved into the relay's
    // accounting on a streamed one.
    let admission_permit = state
        .0
        .admission
        .admit(
            &caller.namespace,
            if streamed {
                RequestKind::Streamed
            } else {
                RequestKind::Buffered
            },
        )
        .await?;

    // The permit is held for the request's lifetime: buffered paths drop it at
    // scope end, and a stream moves it into the accounting owner that settles it.
    let rate_limit_key = RateLimitKey {
        namespace: caller.namespace.clone(),
        subject: caller.subject.clone(),
    };
    let rate_limit_permit = state
        .0
        .rate_limiter
        .acquire(&rate_limit_key)
        .await
        .map_err(|error| match error {
            crate::rate_limit::RateLimitError::StoreUnavailable => {
                GatewayError::RateLimitUnavailable
            }
            crate::rate_limit::RateLimitError::Exceeded
            | crate::rate_limit::RateLimitError::SubjectCapacityExceeded => {
                GatewayError::RateLimitExceeded {
                    retry_after_seconds: None,
                }
            }
        })?;

    // Budget is denominated in micro-dollars. Hold a conservative cost estimate
    // from the first target's price before dispatch; settle the hold against the
    // real cost — priced at whichever target actually served — after.
    let budget_key = BudgetKey {
        namespace: caller.namespace.clone(),
        subject: caller.subject.clone(),
    };
    let estimated_cost = model.targets[0].price.cost_microdollars(estimate);
    if let Some(ceiling) = caller.max_request_microdollars
        && estimated_cost > ceiling
    {
        return Err(GatewayError::RequestCostCeilingExceeded {
            alias: alias.clone(),
            estimated_microdollars: estimated_cost,
            ceiling_microdollars: ceiling,
        });
    }
    let reservation = match state.0.budget.reserve(&budget_key, estimated_cost).await {
        Admission::Allowed(reservation) => reservation,
        Admission::Denied(Denial::Exceeded) => return Err(GatewayError::BudgetExceeded(alias)),
        Admission::Denied(Denial::StoreUnavailable) => return Err(GatewayError::BudgetUnavailable),
    };

    if streamed {
        return stream_with_failover(
            &state,
            snapshot.clone(),
            &caller,
            model,
            StreamRequest {
                alias,
                body,
                wire: &wire,
                hold: BudgetHold {
                    key: budget_key,
                    reservation,
                    estimated_input_tokens: estimate.input_tokens,
                    permit: Some(rate_limit_permit),
                    admission: Some(admission_permit),
                },
            },
        )
        .await;
    }

    let reservation = BudgetReservation::new(state.clone(), budget_key, reservation);
    let outcome =
        match dispatch_with_failover(&state, &snapshot, &caller, model, &body, &wire).await {
            Ok(outcome) => outcome,
            Err(err) => {
                // Nothing reached a provider, so nothing was consumed: the whole
                // estimate goes back rather than lingering until it expires.
                reservation.release().await;
                return Err(err);
            }
        };
    let served = &outcome.served;
    match outcome.result {
        Ok(response) => {
            let usage = to_usage(&response.usage);
            let cost = served.price.cost_microdollars(usage);
            reservation.settle(cost).await;
            record_usage(
                &state,
                RecordArgs {
                    caller: &caller,
                    alias: &alias,
                    target_provider: &served.provider,
                    target_model: &served.model,
                    source: served.source,
                    credential_id: &served.credential_id,
                    status: Status::Ok,
                    input_tokens: response.usage.input_tokens,
                    cache_read_tokens: response.usage.cache_read_tokens,
                    cache_write_tokens: response.usage.cache_write_tokens,
                    output_tokens: response.usage.output_tokens,
                    cost_microdollars: cost,
                    latency_ms: outcome.latency_ms,
                    ttft_ms: outcome.ttft_ms,
                    attempts: outcome.attempts,
                },
            )
            .await;
            Ok(Json(response.body).into_response())
        }
        Err(err) => {
            // The charging policy is "what was actually consumed" (ADR 0010),
            // and a buffered failure reports no usage at all: providers do not
            // return a usage block with an error, and nothing was relayed to
            // measure. Spend is therefore genuinely unknowable and charged as
            // zero — the streamed path, which can measure what it relayed,
            // charges its partial spend.
            reservation.release().await;
            record_usage(
                &state,
                RecordArgs {
                    caller: &caller,
                    alias: &alias,
                    target_provider: &served.provider,
                    target_model: &served.model,
                    source: served.source,
                    credential_id: &served.credential_id,
                    status: Status::UpstreamError,
                    input_tokens: 0,
                    cache_read_tokens: 0,
                    cache_write_tokens: 0,
                    output_tokens: 0,
                    cost_microdollars: 0,
                    latency_ms: outcome.latency_ms,
                    ttft_ms: outcome.ttft_ms,
                    attempts: outcome.attempts,
                },
            )
            .await;
            Err(err.into())
        }
    }
}

/// The target that produced the outcome (served it, or made the last attempt),
/// carried out of the failover walk so the caller can price and attribute it.
struct ServedTarget {
    provider: String,
    model: String,
    price: ModelPrice,
    source: CredentialSource,
    credential_id: String,
}

/// The result of the buffered failover walk: the terminating attempt's result,
/// the target that produced it, and the attempt/timing attribution.
struct FailoverOutcome {
    result: Result<ProviderResponse, TransportError>,
    served: ServedTarget,
    attempts: u32,
    latency_ms: u64,
    ttft_ms: Option<u64>,
}

/// Walk an alias's targets in order, dispatching the credential pool at each and
/// advancing on a retryable upstream failure. This is the outer loop that #7's
/// native routes build on: per-target circuit gating and the attempt/wall-clock
/// bounds live here, while `dispatch_over_pool` owns credential rotation within
/// one target.
///
/// A `Return`/`Ok` outcome that actually dispatched carries a `ServedTarget` so
/// the handler can price and attribute it. A walk that never dispatched (every
/// target skipped by an open circuit, or none had a credential) is a typed
/// error rather than an outcome — nothing reached a provider, so there is no
/// usage to record.
async fn dispatch_with_failover(
    state: &AppState,
    snapshot: &ConfigSnapshot,
    caller: &InboundKey,
    model: &Model,
    body: &Value,
    wire: &Wire,
) -> Result<FailoverOutcome, GatewayError> {
    let cfg = &snapshot.config;
    let policy = FailoverPolicy;
    let deadline = Instant::now() + Duration::from_millis(cfg.failover.overall_timeout_ms);
    let max_attempts = wire.route.max_attempts(cfg.failover.max_attempts);
    let pinned = wire.route.pins_affinity();
    let continuation = wire.route.is_continuation(body);

    let mut walk = FailoverWalk::new(caller, model.targets.len());
    for (index, target) in model.targets.iter().enumerate() {
        if pinned && index > 0 {
            break;
        }
        if walk.attempts >= max_attempts || Instant::now() >= deadline {
            break;
        }
        let Some(provider) = cfg.provider(&target.provider) else {
            if continuation {
                return Err(GatewayError::ContinuationAffinityUnavailable {
                    provider: target.provider.clone(),
                    model: target.model.clone(),
                });
            }
            continue;
        };
        let circuit_key = target_key(target);
        if let CircuitDecision::Skip = snapshot.target_circuits.allow(&circuit_key) {
            if continuation {
                return Err(GatewayError::ContinuationAffinityUnavailable {
                    provider: target.provider.clone(),
                    model: target.model.clone(),
                });
            }
            walk.skipped_open.push(circuit_key);
            continue;
        }
        let Some(plan) = (if pinned {
            snapshot
                .credentials
                .plan_pinned(cfg, &caller.namespace, &provider.id)
        } else {
            snapshot
                .credentials
                .plan(cfg, &caller.namespace, &provider.id)
        }) else {
            if continuation {
                return Err(GatewayError::ContinuationAffinityUnavailable {
                    provider: target.provider.clone(),
                    model: target.model.clone(),
                });
            }
            walk.note_missing_credential(&provider.id);
            continue;
        };

        let mut req_body = body.clone();
        req_body["model"] = Value::String(target.model.clone());
        let attempt_span = telemetry::upstream_attempt_span(
            walk.attempts,
            &target.provider,
            &target.model,
            UsageRecord::credential_source_str(plan.source),
        );
        let started = Instant::now();
        let attempt = dispatch_over_pool(
            state,
            snapshot,
            provider,
            &plan,
            &target.model,
            req_body,
            wire,
            Deadline::at(deadline),
        )
        .instrument(attempt_span.clone())
        .await;
        let latency_ms = started.elapsed().as_millis() as u64;
        // A non-streamed response arrives whole, so the first token lands with
        // the last one; the streaming relay reports the real first chunk.
        let ttft_ms = attempt.result.is_ok().then_some(latency_ms);
        if let Err(err) = &attempt.result {
            note_attempt_timeout(&attempt_span, target, err);
        }
        telemetry::finish_upstream_attempt(
            &attempt_span,
            if attempt.result.is_ok() {
                telemetry::ATTEMPT_OK
            } else {
                telemetry::ATTEMPT_ERROR
            },
            latency_ms,
            ttft_ms,
        );
        walk.attempts += 1;

        let served = ServedTarget {
            provider: target.provider.clone(),
            model: target.model.clone(),
            price: target.price,
            source: plan.source,
            credential_id: attempt.credential_id.clone(),
        };
        match attempt.result {
            Ok(response) => {
                record_target_success(snapshot, target, &circuit_key);
                return Ok(FailoverOutcome {
                    result: Ok(response),
                    served,
                    attempts: walk.attempts,
                    latency_ms,
                    ttft_ms,
                });
            }
            Err(err) => {
                record_target_failure(snapshot, target, &circuit_key, &err);
                let has_next = index + 1 < walk.total
                    && walk.attempts < max_attempts
                    && Instant::now() < deadline;
                let decision = policy.decide(&as_provider_error(&err), has_next);
                walk.last = Some((err, served, latency_ms, ttft_ms));
                if decision == FailoverDecision::Return {
                    let (err, served, latency_ms, ttft_ms) = walk.last.take().expect("just set");
                    return Ok(FailoverOutcome {
                        result: Err(err),
                        served,
                        attempts: walk.attempts,
                        latency_ms,
                        ttft_ms,
                    });
                }
            }
        }
    }

    if let Some((err, served, latency_ms, ttft_ms)) = walk.last {
        return Ok(FailoverOutcome {
            result: Err(err),
            served,
            attempts: walk.attempts,
            latency_ms,
            ttft_ms,
        });
    }
    Err(walk.into_error())
}

enum StreamLeaseParent<'a> {
    Attempt(&'a tracing::Span),
    Rotation(opentelemetry::Context),
}

#[allow(clippy::too_many_arguments)]
async fn open_stream_lease(
    state: &AppState,
    ctx: &StreamContext,
    provider: &Provider,
    target: &Target,
    body: &Value,
    wire: &Wire,
    lease: &CredentialLease,
    lease_index: usize,
    parent: StreamLeaseParent<'_>,
    deadline: Deadline,
) -> Result<
    (
        Box<dyn ProviderStreamDecoder>,
        gateway_transport::ByteStream,
    ),
    TransportError,
> {
    let adapter = adapter_for(provider.kind);
    let decoder = match wire.route {
        Route::ChatCompletions => adapter
            .stream_decoder(Surface::ChatCompletions)
            .map_err(TransportError::Provider)?,
        Route::Responses => adapter
            .stream_decoder(Surface::Responses)
            .map_err(TransportError::Provider)?,
        _ => Box::new(NativeMessagesDecoder::new()) as Box<dyn ProviderStreamDecoder>,
    };
    let upstream = Upstream {
        base_url: provider.base_url.clone(),
        api_key: lease.secret.clone(),
        auth: auth_scheme(provider.kind),
    };
    let mut request_body = body.clone();
    request_body["model"] = Value::String(target.model.clone());
    let opened = match wire.route {
        Route::ChatCompletions => {
            let request = ProviderRequest {
                model: target.model.clone(),
                body: request_body,
            };
            match parent {
                StreamLeaseParent::Attempt(span) => {
                    streaming::open_stream_with_attempt_span(
                        ctx,
                        span,
                        &lease.id,
                        lease_index,
                        state.0.dispatcher.dispatch_stream(
                            adapter.as_ref(),
                            &upstream,
                            Surface::ChatCompletions,
                            request,
                            deadline,
                        ),
                    )
                    .await?
                }
                StreamLeaseParent::Rotation(parent) => {
                    let open = state.0.dispatcher.dispatch_stream(
                        adapter.as_ref(),
                        &upstream,
                        Surface::ChatCompletions,
                        request,
                        deadline,
                    );
                    streaming::open_stream_with_lease_parent(
                        ctx,
                        &lease.id,
                        lease_index,
                        open,
                        parent,
                    )
                    .await?
                }
            }
        }
        _ => {
            let call = wire.call(request_body, adapter.name());
            match parent {
                StreamLeaseParent::Attempt(span) => {
                    streaming::open_stream_with_attempt_span(
                        ctx,
                        span,
                        &lease.id,
                        lease_index,
                        state.0.dispatcher.send_stream(&upstream, &call, deadline),
                    )
                    .await?
                }
                StreamLeaseParent::Rotation(parent) => {
                    let open = state.0.dispatcher.send_stream(&upstream, &call, deadline);
                    streaming::open_stream_with_lease_parent(
                        ctx,
                        &lease.id,
                        lease_index,
                        open,
                        parent,
                    )
                    .await?
                }
            }
        }
    };
    Ok((decoder, opened))
}

/// Walk targets and their credential pools for a streamed request. HTTP
/// open-time 429s rotate on both wires. The relay receives remaining leases for
/// OpenAI-normalized framing, where a rate-limit event before content can be
/// retried without splicing bytes already sent to the caller.
async fn stream_with_failover(
    state: &AppState,
    snapshot: Arc<ConfigSnapshot>,
    caller: &InboundKey,
    model: &Model,
    request: StreamRequest<'_>,
) -> Result<Response, GatewayError> {
    let StreamRequest {
        alias,
        body,
        wire,
        mut hold,
    } = request;
    let reservation_guard =
        BudgetReservation::new(state.clone(), hold.key.clone(), hold.reservation.clone());
    let cfg = &snapshot.config;
    let policy = FailoverPolicy;
    let deadline = Instant::now() + Duration::from_millis(cfg.failover.overall_timeout_ms);
    let max_attempts = wire.route.max_attempts(cfg.failover.max_attempts);
    let pinned = wire.route.pins_affinity();
    let continuation = wire.route.is_continuation(&body);

    let mut walk = FailoverWalk::new(caller, model.targets.len());
    let mut last_ctx: Option<(StreamContext, Instant)> = None;
    'targets: for (index, target) in model.targets.iter().enumerate() {
        if pinned && index > 0 {
            break;
        }
        if walk.attempts >= max_attempts || Instant::now() >= deadline {
            break;
        }
        let Some(provider) = cfg.provider(&target.provider) else {
            if continuation {
                return Err(GatewayError::ContinuationAffinityUnavailable {
                    provider: target.provider.clone(),
                    model: target.model.clone(),
                });
            }
            continue;
        };
        let circuit_key = target_key(target);
        if let CircuitDecision::Skip = snapshot.target_circuits.allow(&circuit_key) {
            if continuation {
                return Err(GatewayError::ContinuationAffinityUnavailable {
                    provider: target.provider.clone(),
                    model: target.model.clone(),
                });
            }
            walk.skipped_open.push(circuit_key);
            continue;
        }
        let Some(plan) = (if pinned {
            snapshot
                .credentials
                .plan_pinned(cfg, &caller.namespace, &provider.id)
        } else {
            snapshot
                .credentials
                .plan(cfg, &caller.namespace, &provider.id)
        }) else {
            if continuation {
                return Err(GatewayError::ContinuationAffinityUnavailable {
                    provider: target.provider.clone(),
                    model: target.model.clone(),
                });
            }
            walk.note_missing_credential(&provider.id);
            continue;
        };
        if plan.attempts.is_empty() {
            walk.note_missing_credential(&provider.id);
            continue;
        }
        let target_attempt = walk.attempts;
        let mut attempt_started: Option<Instant> = None;
        let mut attempt_span: Option<tracing::Span> = None;
        for (lease_index, lease) in plan.attempts.iter().enumerate() {
            if Instant::now() >= deadline {
                if lease_index > 0 {
                    let started = attempt_started.expect("attempt start");
                    let span = attempt_span.as_ref().expect("attempt span");
                    telemetry::finish_upstream_attempt(
                        span,
                        telemetry::ATTEMPT_ERROR,
                        started.elapsed().as_millis() as u64,
                        None,
                    );
                    walk.attempts += 1;
                }
                break 'targets;
            }
            if attempt_span.is_none() {
                let span = telemetry::upstream_attempt_span(
                    target_attempt,
                    &target.provider,
                    &target.model,
                    UsageRecord::credential_source_str(plan.source),
                );
                for (index, skipped) in plan.parked.iter().enumerate() {
                    let lease_span = span.in_scope(|| {
                        telemetry::credential_lease_span(
                            &skipped.id,
                            UsageRecord::credential_source_str(plan.source),
                            index,
                        )
                    });
                    telemetry::finish_credential_lease(&lease_span, telemetry::LEASE_PARKED);
                }
                attempt_started = Some(Instant::now());
                attempt_span = Some(span);
            }
            let span = attempt_span.as_ref().expect("attempt span");
            let mut ctx = StreamContext {
                namespace: caller.namespace.clone(),
                subject: caller.subject.clone(),
                signer_kid: caller.signer_kid.clone(),
                alias: alias.clone(),
                target_provider: target.provider.clone(),
                target_model: target.model.clone(),
                source: plan.source,
                credential_id: lease.id.clone(),
                trace_id: telemetry::trace_id(),
                price: target.price,
                budget_key: hold.key.clone(),
                reservation: hold.reservation.clone(),
                rate_limit_permit: None,
                admission_permit: None,
                estimated_input_tokens: hold.estimated_input_tokens,
                attempts: 0,
            };
            let started = Instant::now();
            let opened = open_stream_lease(
                state,
                &ctx,
                provider,
                target,
                &body,
                wire,
                lease,
                plan.parked.len() + lease_index,
                StreamLeaseParent::Attempt(span),
                Deadline::at(deadline),
            )
            .await;
            ctx.attempts = target_attempt + 1;
            match opened {
                Ok((decoder, bytes)) => {
                    reservation_guard.disarm();
                    telemetry::finish_upstream_attempt(
                        span,
                        telemetry::ATTEMPT_OK,
                        attempt_started
                            .expect("attempt start")
                            .elapsed()
                            .as_millis() as u64,
                        None,
                    );
                    ctx.rate_limit_permit = hold.permit.take();
                    ctx.admission_permit = hold.admission.take();
                    record_target_success(&snapshot, target, &circuit_key);
                    telemetry::record_routing(
                        &ctx.namespace,
                        &ctx.subject,
                        &ctx.alias,
                        &ctx.target_provider,
                        &ctx.target_model,
                        UsageRecord::credential_source_str(ctx.source),
                    );
                    let remaining = plan.attempts[lease_index + 1..].to_vec();
                    let state_for_open = state.clone();
                    let provider_for_open = Arc::new(provider.clone());
                    let target_for_open = target.clone();
                    let wire_for_open = wire.clone();
                    let body_for_open = body.clone();
                    let caller_for_open = caller.clone();
                    let alias_for_open = alias.clone();
                    let hold_key_for_open = hold.key.clone();
                    let reservation_for_open = hold.reservation.clone();
                    let estimate_for_open = hold.estimated_input_tokens;
                    let source_for_open = plan.source;
                    let parent_context_for_open =
                        attempt_span.as_ref().expect("attempt span").context();
                    let opener =
                        move |next_lease: CredentialLease, _attempt: u32, lease_index: usize| {
                            let state = state_for_open.clone();
                            let provider = provider_for_open.clone();
                            let target = target_for_open.clone();
                            let wire = wire_for_open.clone();
                            let body = body_for_open.clone();
                            let caller = caller_for_open.clone();
                            let alias = alias_for_open.clone();
                            let budget_key = hold_key_for_open.clone();
                            let reservation = reservation_for_open.clone();
                            let parent_context = parent_context_for_open.clone();
                            Box::pin(async move {
                                let ctx = StreamContext {
                                    namespace: caller.namespace,
                                    subject: caller.subject,
                                    signer_kid: caller.signer_kid,
                                    alias,
                                    target_provider: target.provider.clone(),
                                    target_model: target.model.clone(),
                                    source: source_for_open,
                                    credential_id: next_lease.id.clone(),
                                    trace_id: telemetry::trace_id(),
                                    price: target.price,
                                    budget_key,
                                    reservation,
                                    rate_limit_permit: None,
                                    // Rotation re-opens upstream for a relay that
                                    // already holds the request's permits.
                                    admission_permit: None,
                                    estimated_input_tokens: estimate_for_open,
                                    attempts: 0,
                                };
                                open_stream_lease(
                                    &state,
                                    &ctx,
                                    provider.as_ref(),
                                    &target,
                                    &body,
                                    &wire,
                                    &next_lease,
                                    lease_index,
                                    StreamLeaseParent::Rotation(parent_context),
                                    Deadline::at(deadline),
                                )
                                .await
                                .map(|(decoder, bytes)| streaming::OpenedStream { decoder, bytes })
                            }) as futures::future::BoxFuture<'static, _>
                        };
                    let snapshot_for_health = snapshot.clone();
                    let rotation = streaming::RotationHandle::new_with_deadline(
                        remaining,
                        lease.clone(),
                        plan.parked.len() + lease_index + 1,
                        opener,
                        Some(deadline),
                        move |lease| snapshot_for_health.credentials.record_failure(lease),
                        {
                            let snapshot = snapshot.clone();
                            move |lease| snapshot.credentials.record_success(lease)
                        },
                    );
                    return Ok(streaming::relay_opened(
                        state.clone(),
                        ctx,
                        decoder,
                        bytes,
                        started,
                        wire.route.framing(),
                        Some(rotation),
                    ));
                }
                Err(err) if is_credential_exhausted(&err) => {
                    snapshot.credentials.record_failure(lease);
                    last_ctx = Some((ctx, started));
                    walk.last_error = Some(err);
                    continue;
                }
                Err(err) => {
                    note_attempt_timeout(span, target, &err);
                    record_target_failure(&snapshot, target, &circuit_key, &err);
                    let has_next = index + 1 < walk.total
                        && walk.attempts < max_attempts
                        && Instant::now() < deadline;
                    let decision = policy.decide(&as_provider_error(&err), has_next);
                    last_ctx = Some((ctx, started));
                    walk.last_error = Some(err);
                    if decision == FailoverDecision::Return {
                        telemetry::finish_upstream_attempt(
                            span,
                            telemetry::ATTEMPT_ERROR,
                            attempt_started
                                .expect("attempt start")
                                .elapsed()
                                .as_millis() as u64,
                            None,
                        );
                        walk.attempts += 1;
                        break 'targets;
                    }
                    break;
                }
            }
        }
        let span = attempt_span.as_ref().expect("attempt span");
        telemetry::finish_upstream_attempt(
            span,
            telemetry::ATTEMPT_ERROR,
            attempt_started
                .expect("attempt start")
                .elapsed()
                .as_millis() as u64,
            None,
        );
        walk.attempts += 1;
    }

    if let Some(err) = walk.last_error.take() {
        if let Some((mut ctx, started)) = last_ctx {
            reservation_guard.disarm();
            ctx.attempts = walk.attempts;
            ctx.rate_limit_permit = hold.permit.take();
            ctx.admission_permit = hold.admission.take();
            streaming::settle_upstream_error(state.clone(), ctx, started);
        } else {
            reservation_guard.release().await;
        }
        return Err(err.into());
    }
    reservation_guard.release().await;
    Err(walk.into_error())
}

/// One streamed request as the failover walk sees it: the alias it resolved,
/// the body to forward, the wire it speaks, and the budget hold it was admitted
/// under.
struct StreamRequest<'a> {
    alias: String,
    body: Value,
    wire: &'a Wire,
    hold: BudgetHold,
}

/// The budget reservation a request is dispatched under, plus the input-token
/// estimate it was priced from. The streaming relay needs both: the hold to
/// settle, and the estimate to price a stream that ends before the provider
/// reports authoritative usage.
struct BudgetHold {
    key: BudgetKey,
    reservation: Reservation,
    estimated_input_tokens: u64,
    permit: Option<RateLimitPermit>,
    /// The admission capacity the request was let in under. Moved into the
    /// stream context that ends up owning the relay, so an open stream keeps
    /// occupying a slot for exactly as long as it is open — and a walk that
    /// never opens one drops it here.
    admission: Option<AdmissionPermit>,
}

/// A buffered request's reservation must be reconciled even when its handler is
/// dropped while the upstream request is in flight. Streaming `Accounting`
/// covers cancellation once the relay exists; this guard covers the buffered path.
struct BudgetReservation {
    state: AppState,
    key: BudgetKey,
    reservation: Option<Reservation>,
}

impl BudgetReservation {
    fn new(state: AppState, key: BudgetKey, reservation: Reservation) -> Self {
        Self {
            state,
            key,
            reservation: Some(reservation),
        }
    }

    /// Disarm before awaiting so dropping the guard after settlement cannot
    /// submit a second budget operation.
    async fn settle(mut self, actual_microdollars: u64) {
        let reservation = self
            .reservation
            .take()
            .expect("budget reservation guard must be armed");
        self.state
            .0
            .budget
            .settle(&self.key, &reservation, actual_microdollars)
            .await;
    }

    /// Disarm before awaiting so the explicit release and the drop fallback
    /// cannot both reconcile the same hold.
    async fn release(mut self) {
        let reservation = self
            .reservation
            .take()
            .expect("budget reservation guard must be armed");
        self.state.0.budget.release(&self.key, &reservation).await;
    }

    fn disarm(mut self) {
        self.reservation.take();
    }
}

impl Drop for BudgetReservation {
    fn drop(&mut self) {
        let Some(reservation) = self.reservation.take() else {
            return;
        };
        let state = self.state.clone();
        let key = self.key.clone();
        streaming::spawn_settlement(async move {
            state.0.budget.release(&key, &reservation).await;
        });
    }
}

/// Mutable bookkeeping shared by the buffered and streaming failover walks: how
/// many upstream attempts have been made, which targets were circuit-skipped,
/// and the reason to surface if nothing ever dispatched.
struct FailoverWalk {
    namespace: String,
    total: usize,
    attempts: u32,
    skipped_open: Vec<String>,
    no_credential: Option<GatewayError>,
    /// The last buffered attempt's error + attribution, carried so a walk that
    /// exhausts its targets still returns a real upstream error.
    last: Option<(TransportError, ServedTarget, u64, Option<u64>)>,
    /// The last streaming open error (the streaming context is carried
    /// separately since it is consumed to settle the usage record).
    last_error: Option<TransportError>,
}

impl FailoverWalk {
    fn new(caller: &InboundKey, total: usize) -> Self {
        Self {
            namespace: caller.namespace.clone(),
            total,
            attempts: 0,
            skipped_open: Vec::new(),
            no_credential: None,
            last: None,
            last_error: None,
        }
    }

    fn note_missing_credential(&mut self, provider: &str) {
        self.no_credential
            .get_or_insert_with(|| GatewayError::NoCredential {
                namespace: self.namespace.clone(),
                provider: provider.to_owned(),
            });
    }

    /// The error for a walk that never dispatched: an open circuit on every
    /// candidate is a distinct, retriable condition from having no credential.
    fn into_error(self) -> GatewayError {
        if !self.skipped_open.is_empty() {
            return ProviderError::AllCircuitsOpen(self.skipped_open).into();
        }
        self.no_credential
            .unwrap_or_else(|| ProviderError::InvalidRequest("no attemptable target".into()).into())
    }
}

/// The circuit-breaker key for a target: its qualified `provider/model`, so two
/// aliases pointing at the same concrete target share one breaker.
fn target_key(target: &Target) -> String {
    FailoverTarget::new(&target.provider, &target.model).qualified_model()
}

fn auth_scheme(kind: ProviderKind) -> AuthScheme {
    match kind {
        ProviderKind::Anthropic => AuthScheme::Header("x-api-key"),
        ProviderKind::Openai | ProviderKind::OpenaiCompatible => AuthScheme::Bearer,
    }
}

/// Attribute a failed attempt's timeout class to its span and the timeout
/// counter. Only the bound is recorded — never the upstream URL, which the
/// transport has already kept out of the error.
fn note_attempt_timeout(span: &tracing::Span, target: &Target, err: &TransportError) {
    if let Some(kind) = err.timeout_kind() {
        let bound = err
            .timeout_bound()
            .map(TimeoutBound::label)
            .unwrap_or_default();
        telemetry::record_attempt_timeout(
            span,
            &target.provider,
            &target.model,
            kind.label(),
            bound,
        );
        warn!(
            provider = %target.provider,
            model = %target.model,
            timeout = kind.label(),
            timeout_bound = bound,
            "upstream attempt exceeded a transport bound"
        );
    }
}

fn record_target_success(snapshot: &ConfigSnapshot, target: &Target, circuit_key: &str) {
    snapshot.target_circuits.record_success(circuit_key);
    telemetry::metrics::record_circuit_state(
        &target.provider,
        &target.model,
        snapshot.target_circuits.state(circuit_key),
    );
}

/// A target failure trips its circuit only when it reflects on the *target*'s
/// health. A `429` that exhausted the pool is credential-scoped (ADR 0006) and a
/// `404` names a missing deployment, not an unhealthy target — both fail over
/// without opening the target's breaker. A walk budget spent before this target
/// was ever dispatched to belongs in the same category; a target that was given
/// time and stalled does not, however short that time was.
fn record_target_failure(
    snapshot: &ConfigSnapshot,
    target: &Target,
    circuit_key: &str,
    err: &TransportError,
) {
    if as_provider_error(err).affects_provider_health()
        && !is_credential_exhausted(err)
        && !was_never_dispatched(err)
    {
        snapshot.target_circuits.record_failure(circuit_key);
        telemetry::metrics::record_circuit_state(
            &target.provider,
            &target.model,
            snapshot.target_circuits.state(circuit_key),
        );
    }
}

/// View a transport error through the core retryability taxonomy so the failover
/// policy and the breaker share one definition of "retryable". A transport-level
/// error (no provider status) is a target-scoped dependency failure.
fn as_provider_error(err: &TransportError) -> ProviderError {
    match err {
        TransportError::Provider(pe) => pe.clone(),
        TransportError::Http(message) => ProviderError::transport("upstream", message.clone()),
        // A timeout says nothing conclusive about the target beyond "it did not
        // answer in time", which is exactly a target-scoped dependency failure.
        // An oversized body is the same: the target produced something this
        // gateway will not serve.
        TransportError::Timeout { .. } | TransportError::BodyTooLarge { .. } => {
            ProviderError::transport("upstream", err.to_string())
        }
    }
}

/// The upstream attempt that terminated the request, plus the credential that
/// made it (for attribution).
struct PooledAttempt {
    result: Result<ProviderResponse, TransportError>,
    credential_id: String,
}

/// Walk the credential pool: dispatch with the first credential, and on a
/// credential-scoped failure (rate limit / quota) park that credential and
/// retry the *same* target with the next one. Target-level failover is a
/// separate concern and is not attempted here.
#[allow(clippy::too_many_arguments)]
async fn dispatch_over_pool(
    state: &AppState,
    snapshot: &ConfigSnapshot,
    provider: &Provider,
    plan: &CredentialPlan,
    target_model: &str,
    body: Value,
    wire: &Wire,
    deadline: Deadline,
) -> PooledAttempt {
    let adapter = adapter_for(provider.kind);
    let mut exhausted: Option<PooledAttempt> = None;

    for (index, skipped) in plan.parked.iter().enumerate() {
        let span = telemetry::credential_lease_span(
            &skipped.id,
            UsageRecord::credential_source_str(plan.source),
            index,
        );
        telemetry::finish_credential_lease(&span, telemetry::LEASE_PARKED);
    }

    for (index, lease) in plan.attempts.iter().enumerate() {
        let lease_span = telemetry::credential_lease_span(
            &lease.id,
            UsageRecord::credential_source_str(plan.source),
            plan.parked.len() + index,
        );
        let upstream = Upstream {
            base_url: provider.base_url.clone(),
            api_key: lease.secret.clone(),
            auth: match provider.kind {
                ProviderKind::Anthropic => AuthScheme::Header("x-api-key"),
                ProviderKind::Openai | ProviderKind::OpenaiCompatible => AuthScheme::Bearer,
            },
        };
        let result = async {
            match wire.route {
                Route::ChatCompletions => {
                    let request = ProviderRequest {
                        model: target_model.to_string(),
                        body: body.clone(),
                    };
                    state
                        .0
                        .dispatcher
                        .dispatch(
                            adapter.as_ref(),
                            &upstream,
                            Surface::ChatCompletions,
                            request,
                            deadline,
                        )
                        .await
                }
                route => state
                    .0
                    .dispatcher
                    .send(
                        &upstream,
                        &wire.call(body.clone(), adapter.name()),
                        deadline,
                    )
                    .await
                    .map(|body| ProviderResponse {
                        usage: route.native_usage(&body),
                        body,
                    }),
            }
        }
        .instrument(lease_span.clone())
        .await;
        match result {
            Ok(response) => {
                telemetry::finish_credential_lease(&lease_span, telemetry::LEASE_SERVED);
                snapshot.credentials.record_success(lease);
                return PooledAttempt {
                    result: Ok(response),
                    credential_id: lease.id.clone(),
                };
            }
            Err(err) if is_credential_exhausted(&err) => {
                telemetry::finish_credential_lease(&lease_span, telemetry::LEASE_RATE_LIMITED);
                snapshot.credentials.record_failure(lease);
                tracing::warn!(
                    provider = %provider.id,
                    credential = %lease.id,
                    "credential is rate-limited or out of quota; trying the next in the pool"
                );
                exhausted = Some(PooledAttempt {
                    result: Err(err),
                    credential_id: lease.id.clone(),
                });
            }
            Err(err) => {
                telemetry::finish_credential_lease(&lease_span, telemetry::LEASE_ERROR);
                return PooledAttempt {
                    result: Err(err),
                    credential_id: lease.id.clone(),
                };
            }
        }
    }

    exhausted.unwrap_or_else(|| PooledAttempt {
        result: Err(ProviderError::InvalidRequest("empty credential pool".into()).into()),
        credential_id: String::new(),
    })
}

/// A `429` (rate limit or exhausted quota) is attributable to the *credential*,
/// so it parks that key and falls to the next. Every other upstream failure is
/// the target's problem, not the key's.
fn is_credential_exhausted(err: &TransportError) -> bool {
    matches!(err, TransportError::Provider(error) if error.is_credential_rate_limited())
}

/// `TimeoutKind::Overall` is the one timeout no target earned: the walk's budget
/// was already spent, so nothing was dispatched and there is no evidence about
/// this target to record. Parking a target the gateway never called would let
/// one slow target take healthy ones out of rotation.
///
/// Every other timeout names the phase that stalled — including one cut short by
/// what was left of `failover.overall_timeout_ms` — because a target that
/// accepted a request and produced nothing in the time it was given *is*
/// evidence, and treating a late-in-the-walk stall as the gateway's own problem
/// would keep a black-holing target's breaker closed forever.
fn was_never_dispatched(err: &TransportError) -> bool {
    err.timeout_kind() == Some(TimeoutKind::Overall)
}

fn to_usage(u: &gateway_core::ModelUsage) -> Usage {
    Usage {
        input_tokens: u.input_tokens,
        output_tokens: u.output_tokens,
        reasoning_tokens: u.reasoning_tokens,
        cache_read_tokens: u.cache_read_tokens,
        cache_write_tokens: u.cache_write_tokens,
    }
}

/// Conservative pre-dispatch usage estimate: input tokens from the request body
/// (~4 chars/token) plus a reserved output allowance (`max_tokens` when present,
/// else a default). Priced with a target's `ModelPrice` it becomes the held
/// estimate, which settlement replaces with the real cost.
fn estimate_usage(body: &Value) -> Usage {
    const DEFAULT_MAX_OUTPUT_TOKENS: u64 = 1_024;
    let input_tokens = (serde_json::to_string(body).map(|s| s.len()).unwrap_or(0) / 4) as u64;
    let output_tokens = requested_output_tokens(body).unwrap_or(DEFAULT_MAX_OUTPUT_TOKENS);
    Usage {
        input_tokens,
        output_tokens,
        reasoning_tokens: 0,
        cache_read_tokens: 0,
        cache_write_tokens: 0,
    }
}

/// The output allowance a request asked for, in whichever spelling its surface
/// uses. `None` when the caller left it to the provider.
///
/// A body carrying several spellings takes the largest of them, because a
/// present-but-unusable field (`null`, a string) must not hide a usable one from
/// the ceiling or from the hold: whichever field the provider honors, the answer
/// here is never below it.
fn requested_output_tokens(body: &Value) -> Option<u64> {
    ["max_tokens", "max_completion_tokens", "max_output_tokens"]
        .into_iter()
        .filter_map(|field| body.get(field).and_then(Value::as_u64))
        .max()
}

/// Monotonic per-process request id. The trace it belongs to travels in the
/// record's `trace_id`, which a caller's whole agent loop shares. `pub` so the
/// streaming relay can stamp the same id on its settled usage record.
pub fn next_request_id() -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    static COUNTER: AtomicU64 = AtomicU64::new(1);
    format!("req_{:016x}", COUNTER.fetch_add(1, Ordering::Relaxed))
}

struct RecordArgs<'a> {
    caller: &'a InboundKey,
    alias: &'a str,
    target_provider: &'a str,
    target_model: &'a str,
    source: CredentialSource,
    credential_id: &'a str,
    status: Status,
    input_tokens: u64,
    cache_read_tokens: u64,
    cache_write_tokens: u64,
    output_tokens: u64,
    cost_microdollars: u64,
    latency_ms: u64,
    /// Time to the first token, when one was produced.
    ttft_ms: Option<u64>,
    /// Upstream attempts made; the retry count is one less.
    attempts: u32,
}

async fn record_usage(state: &AppState, args: RecordArgs<'_>) {
    let ttft_ms = args.ttft_ms;
    let attempts = args.attempts;
    let record = UsageRecord {
        schema_version: UsageRecord::SCHEMA_VERSION,
        request_id: next_request_id(),
        trace_id: telemetry::trace_id(),
        namespace: args.caller.namespace.clone(),
        subject: args.caller.subject.clone(),
        signer_kid: args.caller.signer_kid.clone(),
        model: args.alias.to_string(),
        target_provider: args.target_provider.to_string(),
        target_model: args.target_model.to_string(),
        credential_source: UsageRecord::credential_source_str(args.source),
        credential_id: args.credential_id.to_string(),
        status: args.status,
        input_tokens: args.input_tokens,
        cache_read_tokens: args.cache_read_tokens,
        cache_write_tokens: args.cache_write_tokens,
        output_tokens: args.output_tokens,
        cost_microdollars: args.cost_microdollars,
        catalog_version: 0,
        latency_ms: args.latency_ms,
        attempts,
    };
    telemetry::record_request(&record, ttft_ms, attempts);
    state.0.usage.record(&record).await;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aliases::AliasScope;
    use crate::budget::NoBudget;
    use crate::config::Config;
    use crate::rate_limit::{InMemoryRateLimiter, NoLimit, RateLimitKey, RateLimiter};
    use crate::usage::{StdoutSink, UsageFanout, UsageSink};
    use axum::body::Body;
    use axum::http::{Method, Request, StatusCode};
    use http_body_util::BodyExt;
    use jsonwebtoken::{Algorithm, EncodingKey, Header, encode};
    use opentelemetry::trace::TracerProvider as _;
    use opentelemetry_sdk::trace::{InMemorySpanExporter, SdkTracerProvider};
    use serde::Serialize;
    use std::collections::HashMap;
    use std::future::pending;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::sync::{Arc, Mutex};
    use std::time::{Duration, SystemTime, UNIX_EPOCH};
    use tokio::sync::oneshot;
    use tower::util::ServiceExt;
    use tracing_subscriber::layer::SubscriberExt;

    /// An unusable spelling of the output allowance never hides a usable one, so
    /// neither the output ceiling nor the budget hold can be dodged by sending
    /// `max_tokens: null` alongside a real allowance.
    #[test]
    fn the_requested_output_allowance_takes_the_largest_usable_spelling() {
        for (body, expected) in [
            (serde_json::json!({}), None),
            (serde_json::json!({"max_tokens": 32}), Some(32)),
            (
                serde_json::json!({"max_tokens": Value::Null, "max_completion_tokens": 500_000}),
                Some(500_000),
            ),
            (
                serde_json::json!({"max_tokens": "many", "max_output_tokens": 64}),
                Some(64),
            ),
            (
                serde_json::json!({"max_tokens": 8, "max_completion_tokens": 4_096}),
                Some(4_096),
            ),
            (serde_json::json!({"max_tokens": -1}), None),
        ] {
            assert_eq!(requested_output_tokens(&body), expected, "{body}");
        }
        assert_eq!(
            estimate_usage(
                &serde_json::json!({"max_tokens": Value::Null, "max_completion_tokens": 500_000})
            )
            .output_tokens,
            500_000,
            "the hold prices the allowance the provider will honor"
        );
    }

    /// The inbound key every test config declares, and the secret the caller
    /// presents for it. Inbound auth is always enforced (ADR 0013).
    const GATEWAY_KEY: &str = r#"
[[gateway_key]]
env = "AXOND_INBOUND_KEY"
namespace = "platform"
"#;
    const CALLER_SECRET: &str = "inbound-secret";

    /// The given provider-credential env vars, plus the inbound key's.
    fn env_with<const N: usize>(credentials: [(&str, &str); N]) -> HashMap<String, String> {
        credentials
            .into_iter()
            .chain([("AXOND_INBOUND_KEY", CALLER_SECRET)])
            .map(|(k, v)| (k.to_owned(), v.to_owned()))
            .collect()
    }

    /// A stall is the target's problem whichever bound ended the wait; only a
    /// budget spent before dispatch is the gateway's, because then no target was
    /// asked anything.
    #[test]
    fn a_stalled_target_is_recorded_even_when_the_walk_budget_ended_the_wait() {
        for kind in [
            TimeoutKind::Connect,
            TimeoutKind::ResponseHeaders,
            TimeoutKind::BufferedBody,
            TimeoutKind::StreamIdle,
        ] {
            for bound in [TimeoutBound::Phase, TimeoutBound::WalkBudget] {
                let err = TransportError::Timeout {
                    kind,
                    bound,
                    budget_ms: 100,
                };
                assert!(as_provider_error(&err).affects_provider_health());
                assert!(
                    !was_never_dispatched(&err),
                    "{} on {}",
                    kind.label(),
                    bound.label()
                );
            }
        }

        let unattempted = TransportError::Timeout {
            kind: TimeoutKind::Overall,
            bound: TimeoutBound::WalkBudget,
            budget_ms: 0,
        };
        assert!(was_never_dispatched(&unattempted));
    }

    /// A JSON `POST` that already carries the caller's gateway key.
    fn authorized(uri: &str) -> axum::http::request::Builder {
        Request::post(uri)
            .header("content-type", "application/json")
            .header(
                axum::http::header::AUTHORIZATION,
                format!("Bearer {CALLER_SECRET}"),
            )
    }

    fn test_state() -> AppState {
        test_state_with_base_url("https://api.openai.com/v1")
    }

    fn test_state_with_base_url(base_url: &str) -> AppState {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "openai"
kind = "openai"
base_url = "{base_url}"

[[credential]]
namespace = "platform"
provider = "openai"
env = "AXOND_PLATFORM_OPENAI"

{GATEWAY_KEY}

[gateway_token]
audience = "test-audience"

[[gateway_verifier]]
kid = "scope-test-kid"
alg = "HS256"
env = "JWT_SECRET"
namespaces = ["platform"]
max_ttl = "15m"

[[model]]
name = "gpt-4o"
targets = [{{ provider = "openai", model = "gpt-4o", price = {{ input_microdollars_per_million = 2500000, output_microdollars_per_million = 10000000 }} }}]

[[model]]
name = "claude-3"
targets = [{{ provider = "openai", model = "claude-3", price = {{ input_microdollars_per_million = 2500000, output_microdollars_per_million = 10000000 }} }}]
"#
        ))
        .unwrap();
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(StdoutSink)];
        let mut env = env_with([("AXOND_PLATFORM_OPENAI", "sk-platform-test")]);
        env.insert(
            "JWT_SECRET".to_owned(),
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
        );
        AppState::new(cfg, &env, UsageFanout::new(sinks), Box::new(NoBudget))
            .expect("credentials resolve")
    }

    fn minting_state() -> AppState {
        minting_state_with_audience_epochs("test-audience", "")
    }

    fn minting_state_with_epochs(epochs: &str) -> AppState {
        minting_state_with_audience_epochs("test-audience", epochs)
    }

    fn minting_state_with_audience_epochs(audience: &str, epochs: &str) -> AppState {
        minting_state_with_scope_audience_epochs("scope = [\"chat\", \"models\"]", audience, epochs)
    }

    fn minting_state_without_scope() -> AppState {
        minting_state_with_scope_audience_epochs("", "test-audience", "")
    }

    fn minting_state_with_scope_audience_epochs(
        scope: &str,
        audience: &str,
        epochs: &str,
    ) -> AppState {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[gateway_key]]
env = "MINT_KEY"
namespace = "platform"
can_mint = true

[gateway_token]
audience = "{audience}"

[[gateway_verifier]]
kid = "mint-kid"
alg = "HS256"
env = "JWT_SECRET"
namespaces = ["platform"]
max_ttl = "15m"

[gateway_minting]
kid = "mint-kid"
env = "JWT_SECRET"
{scope}
aliases = ["gpt-*"]
max_request_microdollars = 1000
{epochs}
"#,
            audience = audience,
            scope = scope,
            epochs = epochs
        ))
        .unwrap();
        let env = HashMap::from([
            ("MINT_KEY".to_owned(), "mint-key".to_owned()),
            (
                "JWT_SECRET".to_owned(),
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
            ),
        ]);
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(StdoutSink)];
        AppState::new(cfg, &env, UsageFanout::new(sinks), Box::new(NoBudget))
            .expect("minting state")
    }

    async fn mint_request(state: AppState, body: Value) -> (StatusCode, Value) {
        mint_request_with_credential(state, "mint-key", body).await
    }

    async fn mint_request_with_credential(
        state: AppState,
        credential: &str,
        body: Value,
    ) -> (StatusCode, Value) {
        let response = router(state)
            .oneshot(
                Request::post("/v1/tokens")
                    .header(
                        axum::http::header::AUTHORIZATION,
                        format!("Bearer {credential}"),
                    )
                    .header("content-type", "application/json")
                    .body(Body::from(body.to_string()))
                    .unwrap(),
            )
            .await
            .unwrap();
        let status = response.status();
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let body = serde_json::from_slice(&body).unwrap_or(Value::Null);
        (status, body)
    }

    #[tokio::test]
    async fn minting_response_is_not_cacheable() {
        let response = router(minting_state())
            .oneshot(
                Request::post("/v1/tokens")
                    .header(axum::http::header::AUTHORIZATION, "Bearer mint-key")
                    .header("content-type", "application/json")
                    .body(Body::from(r#"{"sub":"agent"}"#))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(
            response.headers().get(axum::http::header::CACHE_CONTROL),
            Some(&HeaderValue::from_static("no-store"))
        );
    }

    #[tokio::test]
    async fn no_scope_ceiling_inherits_ordinary_capabilities() {
        let state = minting_state_without_scope();
        let (status, body) = mint_request(state.clone(), json!({"sub": "agent"})).await;
        assert_eq!(status, StatusCode::OK);
        let token = body["token"].as_str().unwrap();
        let principal = state
            .config()
            .resolve_principal(&Presented { credential: token })
            .await
            .expect("resolve minted token")
            .expect("minted principal");
        assert!(
            principal
                .scope
                .as_ref()
                .is_none_or(|scope| !scope.contains(&Capability::CredentialsAll))
        );
        let response =
            scoped_route_request(state.clone(), "/v1/credentials?namespaces=all", token).await;
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
        let response = router(state)
            .oneshot(
                Request::get("/v1/models")
                    .header(axum::http::header::AUTHORIZATION, format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    /// The two authorities stay separate in both directions: the static key can
    /// read every namespace itself, yet cannot hand that reach to a subject, and
    /// an omitted mint scope still cannot inherit it (#116).
    #[tokio::test]
    async fn minting_cannot_delegate_the_operator_view_a_static_key_holds_directly() {
        let state = minting_state_without_scope();
        let (status, body) = mint_request(
            state.clone(),
            json!({"sub": "agent", "scope": ["credentials", "credentials:all"]}),
        )
        .await;
        assert_eq!(status, StatusCode::FORBIDDEN);
        assert_eq!(body["error"]["type"], "mint_claims_not_narrowing");

        let (status, body) = mint_request(state.clone(), json!({"sub": "agent"})).await;
        assert_eq!(status, StatusCode::OK);
        let token = body["token"].as_str().expect("minted token").to_owned();
        let response =
            scoped_route_request(state.clone(), "/v1/credentials?namespaces=all", &token).await;
        assert_scope_denial(response, "credentials:all").await;

        let response =
            scoped_route_request(state, "/v1/credentials?namespaces=all", "mint-key").await;
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn default_scope_minted_token_reaches_responses() {
        let state = scoped_route_state().await;
        let mut config = state.config().config.clone();
        config.gateway_key[0].can_mint = true;
        config.gateway_minting = Some(crate::config::GatewayMinting {
            kid: "scope-test-kid".to_owned(),
            env: Some("JWT_SECRET".to_owned()),
            file: None,
            max_ttl: None,
            scope: None,
            aliases: None,
            max_request_microdollars: None,
        });
        let env = HashMap::from([
            ("CHAT_KEY".to_owned(), "chat-key".to_owned()),
            ("MESSAGES_KEY".to_owned(), "messages-key".to_owned()),
            ("EMBEDDINGS_KEY".to_owned(), "embeddings-key".to_owned()),
            ("RESPONSES_KEY".to_owned(), "responses-key".to_owned()),
            ("STATIC_KEY".to_owned(), "static-key".to_owned()),
            (
                "JWT_SECRET".to_owned(),
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
            ),
        ]);
        state.publish(ConfigSnapshot::build(config, &env, 0).expect("minting snapshot"));

        let (status, body) =
            mint_request_with_credential(state.clone(), "static-key", json!({"sub": "agent"}))
                .await;
        assert_eq!(status, StatusCode::OK);
        let token = body["token"].as_str().expect("minted token");
        let response = scoped_route_request(state, "/v1/responses", token).await;
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn padded_audience_mints_a_token_the_gateway_accepts() {
        let state = minting_state_with_audience_epochs("  test-audience  ", "");
        let response = router(state.clone())
            .oneshot(
                Request::post("/v1/tokens")
                    .header(axum::http::header::AUTHORIZATION, "Bearer mint-key")
                    .header("content-type", "application/json")
                    .body(Body::from(r#"{"sub":"agent"}"#))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let token = serde_json::from_slice::<Value>(&body).unwrap()["token"]
            .as_str()
            .unwrap()
            .to_owned();

        let response = router(state)
            .oneshot(
                Request::get("/v1/models")
                    .header(axum::http::header::AUTHORIZATION, format!("Bearer {token}"))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn future_token_epochs_block_minting_but_past_epochs_do_not() {
        let future = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
            + 3600;
        for (epoch, subject) in [
            (
                format!("[[gateway_token_epoch]]\nnamespace = \"platform\"\nmin_iat = {future}"),
                "agent",
            ),
            (
                format!(
                    "[[gateway_token_epoch]]\nnamespace = \"platform\"\nsubject = \"agent\"\nmin_iat = {future}"
                ),
                " agent ",
            ),
        ] {
            let (status, body) =
                mint_request(minting_state_with_epochs(&epoch), json!({"sub": subject})).await;
            assert_eq!(status, StatusCode::FORBIDDEN);
            assert_eq!(body["error"]["type"], "mint_epoch_not_usable");
        }

        let near = future - 3598;
        let state = minting_state_with_epochs(&format!(
            "[[gateway_token_epoch]]\nnamespace = \"platform\"\nsubject = \"near\"\nmin_iat = {near}"
        ));
        let (status, body) = mint_request(state.clone(), json!({"sub": "near"})).await;
        assert_eq!(status, StatusCode::OK);
        let token = body["token"].as_str().expect("minted token");
        let exp = body["exp"].as_u64().expect("expiry");
        let expires_in = body["expires_in"].as_u64().expect("remaining lifetime");
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        assert_eq!(expires_in, exp.saturating_sub(now));
        assert!(
            state
                .config()
                .resolve_principal(&Presented { credential: token })
                .await
                .expect("resolve minted token")
                .is_some()
        );

        let past = future - 7200;
        let epoch = format!("[[gateway_token_epoch]]\nnamespace = \"platform\"\nmin_iat = {past}");
        let (status, body) =
            mint_request(minting_state_with_epochs(&epoch), json!({"sub": " agent "})).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body["sub"], "agent");
    }

    #[test]
    fn namespace_authority_follows_reachable_provider_wires() {
        let snapshot = test_state().config();
        assert!(namespace_allows(&snapshot, "platform", Capability::Models));
        assert!(namespace_allows(&snapshot, "platform", Capability::Chat));
        assert!(namespace_allows(
            &snapshot,
            "platform",
            Capability::Embeddings
        ));
        assert!(namespace_allows(
            &snapshot,
            "platform",
            Capability::Responses
        ));
        assert!(!namespace_allows(
            &snapshot,
            "platform",
            Capability::Messages
        ));
    }

    async fn scoped_route_state() -> AppState {
        scoped_route_state_with_revocation(Box::new(crate::revocation::NoDenylist)).await
    }

    async fn scoped_route_state_with_revocation(
        revocation: Box<dyn crate::revocation::RevocationStore>,
    ) -> AppState {
        let (chat_url, _) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let (messages_url, _) = native_upstream(
            "/messages",
            Json(json!({
                "id": "msg-1",
                "usage": { "input_tokens": 1, "output_tokens": 1 }
            }))
            .into_response(),
        )
        .await;
        let (embeddings_url, _) = native_upstream(
            "/embeddings",
            Json(json!({
                "object": "list",
                "data": [],
                "usage": { "prompt_tokens": 1, "total_tokens": 1 }
            }))
            .into_response(),
        )
        .await;
        let (responses_url, _) = native_upstream(
            "/responses",
            Json(json!({
                "id": "resp-1",
                "usage": { "input_tokens": 1, "output_tokens": 1 }
            }))
            .into_response(),
        )
        .await;
        let config = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "chat"
kind = "openai"
base_url = "{chat_url}"

[[provider]]
id = "messages"
kind = "anthropic"
base_url = "{messages_url}"

[[provider]]
id = "embeddings"
kind = "openai"
base_url = "{embeddings_url}"

[[provider]]
id = "responses"
kind = "openai"
base_url = "{responses_url}"

[[credential]]
namespace = "platform"
provider = "chat"
env = "CHAT_KEY"

[[credential]]
namespace = "platform"
provider = "messages"
env = "MESSAGES_KEY"

[[credential]]
namespace = "platform"
provider = "embeddings"
env = "EMBEDDINGS_KEY"

[[credential]]
namespace = "platform"
provider = "responses"
env = "RESPONSES_KEY"

[[gateway_key]]
env = "STATIC_KEY"
namespace = "platform"

[gateway_token]
audience = "scope-tests"

[[gateway_verifier]]
kid = "scope-test-kid"
alg = "HS256"
env = "JWT_SECRET"
namespaces = ["platform"]
max_ttl = "15m"

[[model]]
name = "chat-model"
targets = [{{ provider = "chat", model = "chat-model", price = {{ input_microdollars_per_million = 1, output_microdollars_per_million = 1 }} }}]

[[model]]
name = "messages-model"
targets = [{{ provider = "messages", model = "messages-model", price = {{ input_microdollars_per_million = 1, output_microdollars_per_million = 1 }} }}]

[[model]]
name = "embeddings-model"
targets = [{{ provider = "embeddings", model = "embeddings-model", price = {{ input_microdollars_per_million = 1, output_microdollars_per_million = 1 }} }}]

[[model]]
name = "responses-model"
targets = [{{ provider = "responses", model = "responses-model", price = {{ input_microdollars_per_million = 1, output_microdollars_per_million = 1 }} }}]
"#
        ))
        .expect("scope test config");
        let env = HashMap::from([
            ("CHAT_KEY".to_owned(), "chat-key".to_owned()),
            ("MESSAGES_KEY".to_owned(), "messages-key".to_owned()),
            ("EMBEDDINGS_KEY".to_owned(), "embeddings-key".to_owned()),
            ("RESPONSES_KEY".to_owned(), "responses-key".to_owned()),
            ("STATIC_KEY".to_owned(), "static-key".to_owned()),
            (
                "JWT_SECRET".to_owned(),
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
            ),
        ]);
        AppState::new_with_rate_limiter(
            config,
            &env,
            UsageFanout::new(vec![Box::new(StdoutSink)]),
            Box::new(NoBudget),
            Box::new(NoLimit),
            revocation,
        )
        .expect("scope test state")
    }

    fn scoped_token(scope: Option<Vec<&'static str>>) -> String {
        scoped_token_for("scope-tests", scope)
    }

    fn scoped_token_for(audience: &'static str, scope: Option<Vec<&'static str>>) -> String {
        scoped_token_for_namespace(audience, "platform", scope)
    }

    fn scoped_token_for_namespace(
        audience: &'static str,
        namespace: &'static str,
        scope: Option<Vec<&'static str>>,
    ) -> String {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock")
            .as_secs();
        let claims = TestTokenClaims {
            exp: now + 900,
            iat: now,
            jti: "scope-test-jti",
            aud: audience,
            ns: namespace,
            sub: "scope-caller",
            scope,
            max_request_microdollars: None,
        };
        let mut header = Header::new(Algorithm::HS256);
        header.kid = Some("scope-test-kid".to_owned());
        format!(
            "axt1.{}",
            encode(
                &header,
                &claims,
                &EncodingKey::from_secret(b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
            )
            .expect("scope test token")
        )
    }

    async fn response_error_type(response: Response) -> Value {
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        serde_json::from_slice(&bytes).expect("typed error JSON")
    }

    #[tokio::test]
    async fn denylisted_minted_token_returns_token_revoked() {
        let calls = Arc::new(AtomicUsize::new(0));
        let state = scoped_route_state_with_revocation(Box::new(FakeRevocation {
            mode: FakeRevocationMode::Revoked,
            calls: Arc::clone(&calls),
        }))
        .await;
        let response = scoped_route_request(state, "/v1/models", &scoped_token(None)).await;
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
        assert_eq!(
            response_error_type(response).await["error"]["type"],
            "token_revoked"
        );
        assert_eq!(calls.load(Ordering::Relaxed), 1);
    }

    #[tokio::test]
    async fn unavailable_revocation_store_returns_revocation_unavailable() {
        let state = scoped_route_state_with_revocation(Box::new(FakeRevocation {
            mode: FakeRevocationMode::Unavailable,
            calls: Arc::new(AtomicUsize::new(0)),
        }))
        .await;
        let response = scoped_route_request(state, "/v1/models", &scoped_token(None)).await;
        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
        assert_eq!(
            response_error_type(response).await["error"]["type"],
            "revocation_unavailable"
        );
    }

    #[tokio::test]
    async fn revocation_store_allow_admits_the_minted_token() {
        let state = scoped_route_state_with_revocation(Box::new(FakeRevocation {
            mode: FakeRevocationMode::Allow,
            calls: Arc::new(AtomicUsize::new(0)),
        }))
        .await;
        let response = scoped_route_request(state, "/v1/models", &scoped_token(None)).await;
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn tier_zero_and_static_key_requests_do_not_consult_revocation() {
        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/models",
            &scoped_token(None),
        )
        .await;
        assert_eq!(response.status(), StatusCode::OK);

        let calls = Arc::new(AtomicUsize::new(0));
        let state = scoped_route_state_with_revocation(Box::new(FakeRevocation {
            mode: FakeRevocationMode::Revoked,
            calls: Arc::clone(&calls),
        }))
        .await;
        let response = scoped_route_request(state, "/v1/models", "static-key").await;
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(calls.load(Ordering::Relaxed), 0);
    }

    async fn assert_scope_denial(response: Response, capability: &str) {
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "token_scope_insufficient");
        assert!(
            body["error"]["message"]
                .as_str()
                .unwrap()
                .contains(capability),
            "scope denial did not name {capability}: {body}"
        );
    }

    async fn scoped_route_request(state: AppState, path: &str, token: &str) -> Response {
        let (method, body) = match path.split('?').next().unwrap_or(path) {
            "/v1/models" => (Method::GET, Vec::new()),
            "/v1/credentials" => (Method::GET, Vec::new()),
            "/v1/chat/completions" => (
                Method::POST,
                serde_json::to_vec(&json!({
                    "model": "chat-model",
                    "messages": []
                }))
                .unwrap(),
            ),
            "/v1/messages" => (
                Method::POST,
                serde_json::to_vec(&json!({
                    "model": "messages-model",
                    "max_tokens": 16,
                    "messages": []
                }))
                .unwrap(),
            ),
            "/v1/embeddings" => (
                Method::POST,
                serde_json::to_vec(&json!({
                    "model": "embeddings-model",
                    "input": ["hello"]
                }))
                .unwrap(),
            ),
            "/v1/responses" => (
                Method::POST,
                serde_json::to_vec(&json!({
                    "model": "responses-model",
                    "input": "hello"
                }))
                .unwrap(),
            ),
            _ => panic!("unknown scoped route {path}"),
        };
        router(state)
            .oneshot(
                Request::builder()
                    .method(method)
                    .uri(path)
                    .header("authorization", format!("Bearer {token}"))
                    .header("content-type", "application/json")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap()
    }

    #[tokio::test]
    async fn scoped_models_token_allows_models_and_denies_chat() {
        let state = scoped_route_state().await;
        assert_scope_denial(
            scoped_route_request(
                state.clone(),
                "/v1/models",
                &scoped_token(Some(vec!["chat"])),
            )
            .await,
            "models",
        )
        .await;
        assert_eq!(
            scoped_route_request(state, "/v1/models", &scoped_token(Some(vec!["models"])))
                .await
                .status(),
            StatusCode::OK
        );
    }

    #[tokio::test]
    async fn credentials_status_requires_its_scope_and_denies_every_minted_operator_view() {
        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials",
            &scoped_token(Some(vec!["chat"])),
        )
        .await;
        assert_scope_denial(response, "credentials").await;

        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials",
            &scoped_token(Some(vec!["credentials"])),
        )
        .await;
        assert_eq!(response.status(), StatusCode::OK);
        let body: Value =
            serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes())
                .unwrap();
        assert_eq!(body["observed"], "replica");
        assert_eq!(body["data"][0]["state"], "healthy");
        assert_eq!(body["data"][0]["source"], "platform");

        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials?namespaces=all",
            &scoped_token(Some(vec!["credentials"])),
        )
        .await;
        assert_scope_denial(response, "credentials:all").await;

        // A minted token cannot buy the operator view with a `credentials:all`
        // claim: delegation never confers direct operator authority, even from a
        // signer that emitted the claim outside `POST /v1/tokens`.
        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials?namespaces=all",
            &scoped_token(Some(vec!["credentials", "credentials:all"])),
        )
        .await;
        assert_scope_denial(response, "credentials:all").await;

        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials?namespaces=tenant",
            &scoped_token(Some(vec!["credentials"])),
        )
        .await;
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[tokio::test]
    async fn credentials_status_scope_less_minted_token_keeps_own_namespace_view_only() {
        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials",
            &scoped_token(None),
        )
        .await;
        assert_eq!(response.status(), StatusCode::OK);
        let body: Value =
            serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes())
                .unwrap();
        assert_eq!(body["data"][0]["namespace"], "platform");

        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials?namespaces=all",
            &scoped_token(None),
        )
        .await;
        assert_scope_denial(response, "credentials:all").await;
    }

    #[tokio::test]
    async fn credentials_status_default_namespace_static_key_reaches_the_operator_view() {
        let response =
            scoped_route_request(scoped_route_state().await, "/v1/credentials", "static-key").await;
        assert_eq!(response.status(), StatusCode::OK);
        let body: Value =
            serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes())
                .unwrap();
        assert_eq!(body["data"][0]["namespace"], "platform");

        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials?namespaces=all",
            "static-key",
        )
        .await;
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn credentials_status_operator_view_follows_authority_not_claims() {
        let response = scoped_route_request(
            isolated_tenant_state(),
            "/v1/credentials?namespaces=all",
            "platform-secret",
        )
        .await;
        assert_eq!(response.status(), StatusCode::OK);
        let body: Value =
            serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes())
                .unwrap();
        let namespaces: Vec<_> = body["data"]
            .as_array()
            .expect("credential list")
            .iter()
            .filter_map(|entry| entry["namespace"].as_str())
            .collect();
        assert_eq!(namespaces, ["acme", "beta"]);

        // A tenant static key holds no authority beyond its own namespace.
        let response =
            scoped_route_request(isolated_tenant_state(), "/v1/credentials", "acme-static").await;
        assert_eq!(response.status(), StatusCode::OK);
        let response = scoped_route_request(
            isolated_tenant_state(),
            "/v1/credentials?namespaces=all",
            "acme-static",
        )
        .await;
        assert_scope_denial(response, "credentials:all").await;

        // Neither does a token in the default namespace that claims the
        // operator capability outright.
        for token in [
            scoped_token_for_namespace(
                "scope-tests",
                "acme",
                Some(vec!["credentials", "credentials:all"]),
            ),
            scoped_token(Some(vec!["credentials", "credentials:all"])),
        ] {
            let response =
                scoped_route_request(isolated_tenant_state(), "/v1/credentials", &token).await;
            assert_eq!(response.status(), StatusCode::OK);
            let response = scoped_route_request(
                isolated_tenant_state(),
                "/v1/credentials?namespaces=all",
                &token,
            )
            .await;
            assert_scope_denial(response, "credentials:all").await;
        }
    }

    #[tokio::test]
    async fn credentials_status_never_serializes_secret_material() {
        let response = scoped_route_request(
            scoped_route_state().await,
            "/v1/credentials?namespaces=all",
            "static-key",
        )
        .await;
        assert_eq!(response.status(), StatusCode::OK);
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let serialized = String::from_utf8(body.to_vec()).expect("json body");
        for secret in [
            "chat-key",
            "messages-key",
            "embeddings-key",
            "static-key",
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
        ] {
            assert!(
                !serialized.contains(secret),
                "credential status leaked secret material: {secret}"
            );
        }
    }

    #[tokio::test]
    async fn credentials_status_rejects_duplicate_and_empty_namespaces_query_values() {
        for path in [
            "/v1/credentials?namespaces=all&namespaces=beta",
            "/v1/credentials?namespaces=",
        ] {
            let response = scoped_route_request(
                scoped_route_state().await,
                path,
                &scoped_token(Some(vec!["credentials"])),
            )
            .await;
            assert_eq!(response.status(), StatusCode::BAD_REQUEST);
            let body: Value =
                serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes())
                    .unwrap();
            assert_eq!(body["error"]["type"], "bad_request");
            let message = body["error"]["message"].as_str().expect("error message");
            if path.contains("all&namespaces") {
                assert!(message.contains("duplicate"));
            } else {
                assert!(message.contains("invalid `namespaces` value"));
            }
        }
    }

    fn isolated_tenant_state() -> AppState {
        let config = Config::from_toml_str(
            r#"
[[namespace]]
id = "platform"
default = true

[[namespace]]
id = "acme"

[[namespace]]
id = "beta"

[[provider]]
id = "openai"
kind = "openai"
base_url = "https://api.openai.com/v1"

[[credential]]
namespace = "acme"
provider = "openai"
env = "ACME_SECRET"
id = "acme-label"

[[credential]]
namespace = "beta"
provider = "openai"
env = "BETA_SECRET"
id = "beta-label"

[[gateway_key]]
env = "STATIC_KEY"
namespace = "acme"

[[gateway_key]]
env = "PLATFORM_STATIC_KEY"
namespace = "platform"

[gateway_token]
audience = "scope-tests"

[[gateway_verifier]]
kid = "scope-test-kid"
alg = "HS256"
env = "JWT_SECRET"
namespaces = ["platform", "acme", "beta"]
max_ttl = "15m"
"#,
        )
        .expect("isolated tenant config");
        let env = HashMap::from([
            ("ACME_SECRET".to_owned(), "acme-secret".to_owned()),
            ("BETA_SECRET".to_owned(), "beta-secret".to_owned()),
            ("STATIC_KEY".to_owned(), "acme-static".to_owned()),
            (
                "PLATFORM_STATIC_KEY".to_owned(),
                "platform-secret".to_owned(),
            ),
            (
                "JWT_SECRET".to_owned(),
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
            ),
        ]);
        AppState::new(
            config,
            &env,
            UsageFanout::new(vec![Box::new(StdoutSink)]),
            Box::new(NoBudget),
        )
        .expect("isolated tenant state")
    }

    #[tokio::test]
    async fn credentials_status_isolated_between_tenant_namespaces() {
        let response = scoped_route_request(
            isolated_tenant_state(),
            "/v1/credentials",
            &scoped_token_for_namespace("scope-tests", "acme", Some(vec!["credentials"])),
        )
        .await;
        assert_eq!(response.status(), StatusCode::OK);
        let body: Value =
            serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes())
                .unwrap();
        let ids: Vec<&str> = body["data"]
            .as_array()
            .expect("status list")
            .iter()
            .map(|entry| entry["credential_id"].as_str().expect("credential id"))
            .collect();
        assert_eq!(ids, ["acme-label"]);
    }

    #[tokio::test]
    async fn scoped_chat_token_allows_chat_and_denies_embeddings() {
        let state = scoped_route_state().await;
        assert_scope_denial(
            scoped_route_request(
                state.clone(),
                "/v1/chat/completions",
                &scoped_token(Some(vec!["embeddings"])),
            )
            .await,
            "chat",
        )
        .await;
        assert_eq!(
            scoped_route_request(
                state,
                "/v1/chat/completions",
                &scoped_token(Some(vec!["chat"]))
            )
            .await
            .status(),
            StatusCode::OK
        );
    }

    #[tokio::test]
    async fn scoped_messages_token_allows_messages_and_denies_chat() {
        let state = scoped_route_state().await;
        assert_scope_denial(
            scoped_route_request(
                state.clone(),
                "/v1/messages",
                &scoped_token(Some(vec!["chat"])),
            )
            .await,
            "messages",
        )
        .await;
        assert_eq!(
            scoped_route_request(state, "/v1/messages", &scoped_token(Some(vec!["messages"])),)
                .await
                .status(),
            StatusCode::OK
        );
    }

    #[tokio::test]
    async fn scoped_embeddings_token_allows_embeddings_and_denies_messages() {
        let state = scoped_route_state().await;
        assert_scope_denial(
            scoped_route_request(
                state.clone(),
                "/v1/embeddings",
                &scoped_token(Some(vec!["messages"])),
            )
            .await,
            "embeddings",
        )
        .await;
        assert_eq!(
            scoped_route_request(
                state,
                "/v1/embeddings",
                &scoped_token(Some(vec!["embeddings"])),
            )
            .await
            .status(),
            StatusCode::OK
        );
    }

    #[tokio::test]
    async fn scope_less_tokens_and_static_keys_reach_all_provider_routes() {
        for path in [
            "/v1/models",
            "/v1/chat/completions",
            "/v1/messages",
            "/v1/embeddings",
            "/v1/responses",
        ] {
            assert_eq!(
                scoped_route_request(scoped_route_state().await, path, &scoped_token(None))
                    .await
                    .status(),
                StatusCode::OK,
                "scope-less token denied {path}"
            );
            assert_eq!(
                scoped_route_request(scoped_route_state().await, path, "static-key")
                    .await
                    .status(),
                StatusCode::OK,
                "static key denied {path}"
            );
        }
    }

    #[tokio::test]
    async fn scoped_token_cannot_grant_a_route_the_namespace_lacks() {
        let body = serde_json::to_vec(&json!({ "model": "gpt-4o", "messages": [] })).expect("body");
        let response = router(test_state())
            .oneshot(
                Request::post("/v1/messages")
                    .header(
                        "authorization",
                        format!(
                            "Bearer {}",
                            scoped_token_for("test-audience", Some(vec!["messages"]),)
                        ),
                    )
                    .header("content-type", "application/json")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_scope_denial(response, "messages").await;
    }

    #[tokio::test]
    async fn scoped_token_requires_the_responses_capability() {
        let response = router(test_state())
            .oneshot(
                Request::post("/v1/responses")
                    .header(
                        "authorization",
                        format!(
                            "Bearer {}",
                            scoped_token_for("test-audience", Some(vec!["chat"]))
                        ),
                    )
                    .body(Body::from("{}"))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_scope_denial(response, "responses").await;
    }

    /// Inbound auth is enforced for every configured key set: the wrong
    /// credential, and no credential at all, are both `401`.
    #[tokio::test]
    async fn a_request_without_a_valid_gateway_key_is_rejected() {
        let body =
            || Body::from(serde_json::to_vec(&json!({"model": "gpt-4o", "messages": []})).unwrap());
        for request in [
            Request::post("/v1/chat/completions")
                .header("content-type", "application/json")
                .body(body())
                .unwrap(),
            Request::post("/v1/chat/completions")
                .header("content-type", "application/json")
                .header(axum::http::header::AUTHORIZATION, "Bearer not-the-key")
                .body(body())
                .unwrap(),
            Request::post("/v1/chat/completions")
                .header("content-type", "application/json")
                .header("x-api-key", "not-the-key")
                .body(body())
                .unwrap(),
        ] {
            let resp = router(test_state()).oneshot(request).await.unwrap();
            assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
        }
    }

    #[tokio::test]
    async fn every_authenticated_route_rejects_a_request_without_a_gateway_key() {
        let public_paths: Vec<_> = route_specs(false)
            .iter()
            .filter(|spec| spec.auth == AuthPosture::LivenessProbe)
            .map(|spec| spec.path)
            .collect();
        assert_eq!(public_paths, ["/healthz", "/readyz"]);

        for spec in route_specs(true)
            .into_iter()
            .filter(|spec| spec.auth == AuthPosture::Authenticated)
        {
            let mut rejected = false;
            for method in [axum::http::Method::GET, axum::http::Method::POST] {
                let request = Request::builder()
                    .method(method)
                    .uri(spec.path)
                    .header("content-type", "application/json")
                    .body(Body::from("{}"))
                    .unwrap();
                let response = if spec.path == "/v1/tokens" {
                    router(minting_state()).oneshot(request).await.unwrap()
                } else {
                    router(test_state()).oneshot(request).await.unwrap()
                };
                if response.status() != StatusCode::METHOD_NOT_ALLOWED {
                    assert_eq!(
                        response.status(),
                        StatusCode::UNAUTHORIZED,
                        "{} must authenticate before handling the request",
                        spec.path
                    );
                    rejected = true;
                }
            }
            assert!(rejected, "{0} must handle GET or POST", spec.path);
        }
    }

    #[tokio::test]
    async fn minting_route_is_absent_without_boot_minting_config() {
        let response = router(test_state())
            .oneshot(Request::post("/v1/tokens").body(Body::from("{}")).unwrap())
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn static_minting_key_mints_but_minted_token_cannot_mint() {
        let state = minting_state();
        let response = router(state.clone())
            .oneshot(
                Request::post("/v1/tokens")
                    .header(axum::http::header::AUTHORIZATION, "Bearer mint-key")
                    .header("content-type", "application/json")
                    .body(Body::from(
                        serde_json::json!({
                            "sub": "agent-1",
                            "ttl_seconds": 60,
                            "scope": ["chat"],
                            "aliases": ["gpt-4o"],
                            "max_request_microdollars": 500
                        })
                        .to_string(),
                    ))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::OK);
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&body).unwrap();
        let token = body["token"].as_str().unwrap().to_owned();
        let principal = state
            .config()
            .resolve_principal(&Presented { credential: &token })
            .await
            .unwrap()
            .unwrap();
        assert_eq!(principal.namespace, "platform");
        assert_eq!(principal.subject, "agent-1");
        assert!(!principal.can_mint);

        let response = router(state)
            .oneshot(
                Request::post("/v1/tokens")
                    .header(axum::http::header::AUTHORIZATION, format!("Bearer {token}"))
                    .header("content-type", "application/json")
                    .body(Body::from(r#"{"sub":"agent-2"}"#))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
    }

    #[tokio::test]
    async fn minting_rejects_unauthorized_and_malformed_requests() {
        let (status, body) = mint_request(minting_state(), json!({"sub": "agent"})).await;
        assert_eq!(status, StatusCode::OK);
        assert_eq!(body["namespace"], "platform");

        let state = minting_state();
        let mut config = state.config().config.clone();
        config.gateway_key[0].can_mint = false;
        let env = HashMap::from([
            ("MINT_KEY".to_owned(), "mint-key".to_owned()),
            (
                "JWT_SECRET".to_owned(),
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
            ),
        ]);
        let unauthorized = ConfigSnapshot::build(config, &env, 0).unwrap();
        state.publish(unauthorized);
        let (status, body) = mint_request(state, json!({"sub": "agent"})).await;
        assert_eq!(status, StatusCode::FORBIDDEN);
        assert_eq!(body["error"]["type"], "mint_not_authorized");

        for body in [
            json!({"sub": "agent", "ns": "platform"}),
            json!({"sub": ""}),
            json!({"sub": "   "}),
            json!({"sub": "a".repeat(MAX_MINT_SUBJECT_LENGTH + 1)}),
            json!({"sub": "agent@example"}),
        ] {
            let (status, response) = mint_request(minting_state(), body).await;
            assert_eq!(status, StatusCode::BAD_REQUEST);
            assert_eq!(response["error"]["type"], "bad_request");
        }
    }

    #[tokio::test]
    async fn minting_rejects_every_claim_widening_attempt() {
        for body in [
            json!({"sub": "agent", "ttl_seconds": 901}),
            json!({"sub": "agent", "ttl_seconds": 0}),
            json!({"sub": "agent", "scope": ["embeddings"]}),
            json!({"sub": "agent", "aliases": ["*"]}),
            json!({"sub": "agent", "aliases": ["claude-*"]}),
            json!({"sub": "agent", "max_request_microdollars": 1001}),
        ] {
            let (status, body) = mint_request(minting_state(), body).await;
            assert_eq!(status, StatusCode::FORBIDDEN, "{body}");
            assert_eq!(body["error"]["type"], "mint_claims_not_narrowing");
        }
    }

    #[tokio::test]
    async fn omitted_minting_claims_inherit_configured_ceilings() {
        let (status, body) = mint_request(minting_state(), json!({"sub": "agent"})).await;
        assert_eq!(status, StatusCode::OK);
        let token = body["token"].as_str().unwrap();
        let state = minting_state();
        let principal = state
            .config()
            .resolve_principal(&Presented { credential: token })
            .await
            .unwrap()
            .unwrap();
        assert_eq!(
            principal.scope,
            Some(HashSet::from([Capability::Chat, Capability::Models]))
        );
        assert_eq!(
            principal.alias_scope,
            Some(AliasScope::parse(["gpt-*"]).unwrap())
        );
        assert_eq!(principal.max_request_microdollars, Some(1000));
    }

    #[tokio::test]
    async fn removing_minting_from_a_published_snapshot_is_fail_closed() {
        let state = minting_state();
        let app = router(state.clone());
        let mut config = state.config().config.clone();
        config.gateway_minting = None;
        config.gateway_key[0].can_mint = false;
        let env = HashMap::from([
            ("MINT_KEY".to_owned(), "mint-key".to_owned()),
            (
                "JWT_SECRET".to_owned(),
                "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
            ),
        ]);
        state.publish(ConfigSnapshot::build(config, &env, 1).unwrap());
        let response = app
            .oneshot(
                Request::post("/v1/tokens")
                    .header(axum::http::header::AUTHORIZATION, "Bearer mint-key")
                    .header("content-type", "application/json")
                    .body(Body::from(r#"{"sub":"agent"}"#))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(body["error"]["type"], "minting_disabled");
    }

    #[tokio::test]
    async fn the_responses_route_rejects_anonymous_callers_before_dispatching() {
        let resp = router(test_state())
            .oneshot(
                Request::post("/v1/responses")
                    .header("content-type", "application/json")
                    .body(Body::from("{}"))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    /// The configured key passes in either scheme an SDK might send it in, and
    /// the caller is attributed to the key's namespace and env-var name.
    #[tokio::test]
    async fn a_configured_gateway_key_authenticates_in_either_scheme() {
        let state = test_state();
        let snapshot = state.config();
        for headers in [
            HeaderMap::from_iter([(
                axum::http::header::AUTHORIZATION,
                format!("Bearer {CALLER_SECRET}").parse().unwrap(),
            )]),
            HeaderMap::from_iter([(
                axum::http::HeaderName::from_static("x-api-key"),
                CALLER_SECRET.parse().unwrap(),
            )]),
        ] {
            let caller = authenticate(&snapshot, &headers)
                .await
                .expect("the key is configured");
            assert_eq!(caller.namespace, "platform");
            assert_eq!(caller.subject, "AXOND_INBOUND_KEY");
            assert_eq!(caller.signer_kid, None);
        }
    }

    #[derive(Serialize)]
    struct TestTokenClaims {
        exp: u64,
        iat: u64,
        jti: &'static str,
        aud: &'static str,
        ns: &'static str,
        sub: &'static str,
        #[serde(skip_serializing_if = "Option::is_none")]
        scope: Option<Vec<&'static str>>,
        #[serde(skip_serializing_if = "Option::is_none")]
        max_request_microdollars: Option<u64>,
    }

    #[tokio::test]
    async fn a_verified_token_usage_record_carries_its_signer_kid() {
        let (base_url, _) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "openai"
kind = "openai"
base_url = "{base_url}"

[[gateway_key]]
env = "STATIC_KEY"
namespace = "platform"

[gateway_token]
audience = "test-audience"

[[gateway_verifier]]
kid = "route-kid"
alg = "HS256"
env = "JWT_SECRET"
namespaces = ["platform"]
max_ttl = "15m"

[[credential]]
namespace = "platform"
provider = "openai"
env = "UPSTREAM_KEY"

[[model]]
name = "gpt-4o"
targets = [{{ provider = "openai", model = "gpt-4o", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 1000000 }} }}]
"#
        ))
        .expect("token test config");
        let env = HashMap::from([
            ("STATIC_KEY".to_owned(), "static-secret".to_owned()),
            ("JWT_SECRET".to_owned(), "a".repeat(32)),
            ("UPSTREAM_KEY".to_owned(), "sk-test".to_owned()),
        ]);
        let captured = CapturingSink::default();
        let records = captured.0.clone();
        let state = AppState::new(
            cfg,
            &env,
            UsageFanout::new(vec![Box::new(captured)]),
            Box::new(NoBudget),
        )
        .expect("state");
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock")
            .as_secs();
        let claims = TestTokenClaims {
            exp: now + 900,
            iat: now,
            jti: "route-jti",
            aud: "test-audience",
            ns: "platform",
            sub: "token-caller",
            scope: None,
            max_request_microdollars: None,
        };
        let mut header = Header::new(Algorithm::HS256);
        header.kid = Some("route-kid".to_owned());
        let token = format!(
            "axt1.{}",
            encode(
                &header,
                &claims,
                &EncodingKey::from_secret(b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
            )
            .expect("token")
        );
        let request = Request::post("/v1/chat/completions")
            .header("content-type", "application/json")
            .header("authorization", format!("Bearer {token}"))
            .body(Body::from(
                serde_json::to_vec(&json!({"model": "gpt-4o", "messages": []})).expect("body"),
            ))
            .expect("request");
        let response = router(state).oneshot(request).await.expect("response");
        assert_eq!(response.status(), StatusCode::OK);
        let records = records.lock().expect("records");
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].subject, "token-caller");
        assert_eq!(records[0].signer_kid.as_deref(), Some("route-kid"));
    }

    /// An epoch rejection is a typed authentication failure, so HTTP clients
    /// can distinguish it from an ordinary expired-token response.
    #[tokio::test]
    async fn an_epoch_rejected_token_returns_a_distinct_401_error_code() {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock")
            .as_secs();
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[gateway_key]]
env = "STATIC_KEY"
namespace = "platform"

[gateway_token]
audience = "test-audience"

[[gateway_verifier]]
kid = "route-kid"
alg = "HS256"
env = "JWT_SECRET"
namespaces = ["platform"]
max_ttl = "15m"

[[gateway_token_epoch]]
namespace = "platform"
min_iat = {}
"#,
            now + 1
        ))
        .expect("epoch test config");
        let env = HashMap::from([
            ("STATIC_KEY".to_owned(), "static-secret".to_owned()),
            ("JWT_SECRET".to_owned(), "a".repeat(32)),
        ]);
        let state = AppState::new(
            cfg,
            &env,
            UsageFanout::new(vec![Box::new(StdoutSink)]),
            Box::new(NoBudget),
        )
        .expect("state");
        let claims = TestTokenClaims {
            exp: now + 900,
            iat: now,
            jti: "route-epoch-jti",
            aud: "test-audience",
            ns: "platform",
            sub: "epoch-caller",
            scope: None,
            max_request_microdollars: None,
        };
        let mut header = Header::new(Algorithm::HS256);
        header.kid = Some("route-kid".to_owned());
        let token = format!(
            "axt1.{}",
            encode(
                &header,
                &claims,
                &EncodingKey::from_secret(b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
            )
            .expect("token")
        );
        let response = router(state)
            .oneshot(
                Request::post("/v1/chat/completions")
                    .header("content-type", "application/json")
                    .header("authorization", format!("Bearer {token}"))
                    .body(Body::from(
                        serde_json::to_vec(&json!({"model": "gpt-4o", "messages": []}))
                            .expect("body"),
                    ))
                    .expect("request"),
            )
            .await
            .expect("response");
        assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
        let body: Value = serde_json::from_slice(
            &response
                .into_body()
                .collect()
                .await
                .expect("body")
                .to_bytes(),
        )
        .expect("json error body");
        assert_eq!(body["error"]["type"], "token_issued_before_epoch");
        assert_ne!(body["error"]["type"], "token_expired");
    }

    #[tokio::test]
    async fn healthz_is_ok() {
        let resp = router(test_state())
            .oneshot(Request::get("/healthz").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    /// Draining is what a rolling deployment observes, and it must not take the
    /// liveness probe with it: a `/healthz` failure earns a `SIGKILL`, which is
    /// the one thing that would cut the requests the drain exists to finish.
    #[tokio::test]
    async fn draining_fails_readiness_while_liveness_stays_ok() {
        let state = test_state();
        let lifecycle = Arc::clone(state.lifecycle());
        let app = router(state);

        let ready = app
            .clone()
            .oneshot(Request::get("/readyz").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(ready.status(), StatusCode::OK);

        lifecycle.begin_drain();
        let draining = app
            .clone()
            .oneshot(Request::get("/readyz").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(draining.status(), StatusCode::SERVICE_UNAVAILABLE);
        let live = app
            .oneshot(Request::get("/healthz").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(live.status(), StatusCode::OK);
    }

    /// The drain window keeps serving: readiness has failed, but a request that
    /// arrives before routing catches up is still answered rather than lost.
    #[tokio::test]
    async fn a_request_arriving_during_the_drain_window_is_still_served() {
        let state = test_state();
        let lifecycle = Arc::clone(state.lifecycle());
        let app = router(state);
        lifecycle.begin_drain();

        let resp = app
            .oneshot(
                Request::get("/v1/models")
                    .header(
                        axum::http::header::AUTHORIZATION,
                        format!("Bearer {CALLER_SECRET}"),
                    )
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn a_request_arriving_after_admission_closes_is_refused_as_draining() {
        let state = test_state();
        let lifecycle = Arc::clone(state.lifecycle());
        let app = router(state);
        lifecycle.close();

        let resp = app
            .clone()
            .oneshot(
                Request::get("/v1/models")
                    .header(
                        axum::http::header::AUTHORIZATION,
                        format!("Bearer {CALLER_SECRET}"),
                    )
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let json: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(json["error"]["type"], "draining");

        // The probes stay outside admission, so an orchestrator can still tell a
        // draining replica from a dead one.
        let live = app
            .oneshot(Request::get("/healthz").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(live.status(), StatusCode::OK);
    }

    /// Admission is released by the *response body*, not by the handler future:
    /// a streamed response is in flight for as long as its body is open, and
    /// that is precisely the work the shutdown deadline bounds.
    #[tokio::test]
    async fn a_request_counts_as_in_flight_until_its_body_is_dropped() {
        let state = test_state();
        let lifecycle = Arc::clone(state.lifecycle());
        let app = router(state);

        let resp = app
            .oneshot(
                Request::get("/v1/models")
                    .header(
                        axum::http::header::AUTHORIZATION,
                        format!("Bearer {CALLER_SECRET}"),
                    )
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(lifecycle.in_flight(), 1, "the body is still undelivered");
        let body = resp.into_body();
        drop(body);
        assert_eq!(lifecycle.in_flight(), 0);
    }

    /// `/v1/models` fails closed like every other request path: no gateway key
    /// means `401`, not an open catalog (ADR 0013).
    #[tokio::test]
    async fn models_requires_a_gateway_key() {
        let resp = router(test_state())
            .oneshot(Request::get("/v1/models").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn models_lists_the_callers_aliases() {
        let resp = router(test_state())
            .oneshot(
                Request::get("/v1/models")
                    .header(
                        axum::http::header::AUTHORIZATION,
                        format!("Bearer {CALLER_SECRET}"),
                    )
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let json: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(json["data"][0]["id"], "gpt-4o");
    }

    #[tokio::test]
    async fn models_intersect_namespace_access_with_alias_scope() {
        let state = test_state();
        let snapshot = state.config();
        let caller = InboundKey {
            namespace: "platform".to_owned(),
            subject: "restricted".to_owned(),
            authority: PrincipalAuthority::MintedToken,
            signer_kid: Some("test-kid".to_owned()),
            scope: None,
            alias_scope: Some(AliasScope::parse(["gpt-4o"]).unwrap()),
            max_request_microdollars: None,
            can_mint: false,
            jti: None,
        };
        let response = list_models(Extension(snapshot), Extension(caller))
            .await
            .unwrap()
            .into_response();
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let json: Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["data"].as_array().unwrap().len(), 1);
        assert_eq!(json["data"][0]["id"], "gpt-4o");
    }

    /// A caller sees only the aliases it could invoke: a BYOK namespace with no
    /// credential for the target's provider (and no platform fallback) gets an
    /// empty list, so it cannot enumerate aliases it is not entitled to, while
    /// the platform namespace — which does hold the credential — sees the alias.
    #[tokio::test]
    async fn models_are_scoped_to_the_callers_namespace() {
        let cfg = Config::from_toml_str(
            r#"
[[namespace]]
id = "platform"
default = true

[[namespace]]
id = "acme"

[[provider]]
id = "openai"
kind = "openai"
base_url = "https://api.openai.com/v1"

[[credential]]
namespace = "platform"
provider = "openai"
env = "K_PLATFORM"

[[gateway_key]]
env = "GK_PLATFORM"
namespace = "platform"

[[gateway_key]]
env = "GK_ACME"
namespace = "acme"

[[model]]
name = "gpt-4o"
targets = [{ provider = "openai", model = "gpt-4o", price = { input_microdollars_per_million = 1, output_microdollars_per_million = 1 } }]
"#,
        )
        .unwrap();
        let env: HashMap<String, String> = [
            ("K_PLATFORM", "sk"),
            ("GK_PLATFORM", "plat-key"),
            ("GK_ACME", "acme-key"),
        ]
        .into_iter()
        .map(|(k, v)| (k.to_owned(), v.to_owned()))
        .collect();
        let state = AppState::new(
            cfg,
            &env,
            UsageFanout::new(vec![Box::new(StdoutSink)]),
            Box::new(NoBudget),
        )
        .unwrap();

        assert_eq!(
            models_for(&state, "plat-key").await["data"][0]["id"],
            "gpt-4o"
        );
        let acme = models_for(&state, "acme-key").await;
        assert_eq!(acme["data"].as_array().unwrap().len(), 0);
    }

    /// The `/v1/models` body a caller presenting `secret` receives.
    async fn models_for(state: &AppState, secret: &str) -> Value {
        let resp = router(state.clone())
            .oneshot(
                Request::get("/v1/models")
                    .header(
                        axum::http::header::AUTHORIZATION,
                        format!("Bearer {secret}"),
                    )
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        serde_json::from_slice(&bytes).unwrap()
    }

    #[tokio::test]
    async fn unknown_model_is_typed_404_not_a_missing_route() {
        let body = serde_json::to_vec(&json!({"model": "nope", "messages": []})).unwrap();
        let resp = router(test_state())
            .oneshot(
                authorized("/v1/chat/completions")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let json: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(json["error"]["type"], "unknown_model");
    }

    #[tokio::test]
    async fn a_disallowed_alias_is_forbidden_before_model_lookup() {
        let state = test_state();
        for alias in ["claude-3", "does-not-exist"] {
            let response = serve(
                state.clone(),
                HeaderMap::new(),
                json!({"model": alias, "messages": []}),
                Route::ChatCompletions,
                state.config(),
                InboundKey {
                    namespace: "platform".to_owned(),
                    subject: "restricted".to_owned(),
                    authority: PrincipalAuthority::MintedToken,
                    signer_kid: Some("test-kid".to_owned()),
                    scope: None,
                    alias_scope: Some(AliasScope::parse(["gpt-4o"]).unwrap()),
                    max_request_microdollars: None,
                    can_mint: false,
                    jti: None,
                },
            )
            .await
            .unwrap_err()
            .into_response();
            assert_eq!(response.status(), StatusCode::FORBIDDEN);
            let body = response.into_body().collect().await.unwrap().to_bytes();
            let json: Value = serde_json::from_slice(&body).unwrap();
            assert_eq!(json["error"]["type"], "token_alias_not_permitted");
        }
    }

    #[tokio::test]
    async fn the_responses_route_dispatches_through_the_shared_path() {
        let (base_url, _) = controllable_upstream(
            Arc::new(AtomicBool::new(false)),
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;
        let resp = router(test_state_with_base_url(&base_url))
            .oneshot(
                authorized("/v1/responses")
                    .body(Body::from(r#"{"model":"gpt-4o","input":"hello"}"#))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::BAD_GATEWAY);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let json: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(json["error"]["type"], "provider_dependency_failed");
    }

    /// An alias whose targets cannot speak the route's wire is the caller's
    /// mistake, answered as a typed 4xx before anything is dispatched — there is
    /// no translation to fall back on for a native route.
    #[tokio::test]
    async fn an_openai_only_alias_on_the_native_route_is_a_typed_4xx() {
        let body = serde_json::to_vec(&json!({ "model": "gpt-4o", "messages": [] })).unwrap();
        let resp = router(test_state())
            .oneshot(authorized("/v1/messages").body(Body::from(body)).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let json: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(json["error"]["type"], "unsupported_wire");
    }

    /// The mirror of the native-route case: an Anthropic-native target cannot
    /// serve the OpenAI chat wire, so the alias is rejected up front rather than
    /// dispatched into a `/chat/completions` the provider does not expose.
    #[tokio::test]
    async fn an_anthropic_alias_on_chat_completions_is_a_typed_4xx() {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "anthropic"
kind = "anthropic"
base_url = "https://api.anthropic.com/v1"

{GATEWAY_KEY}

[[model]]
name = "claude"
targets = [{{ provider = "anthropic", model = "claude-sonnet-4-5", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 2000000 }} }}]
"#
        ))
        .unwrap();
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(StdoutSink)];
        let state = AppState::new(
            cfg,
            &env_with([]),
            UsageFanout::new(sinks),
            Box::new(NoBudget),
        )
        .expect("no credentials to resolve");

        let body = serde_json::to_vec(&json!({ "model": "claude", "messages": [] })).unwrap();
        let resp = router(state)
            .oneshot(
                authorized("/v1/chat/completions")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let json: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(json["error"]["type"], "unsupported_wire");
        let message = json["error"]["message"].as_str().unwrap();
        assert!(message.contains("anthropic"), "{message}");
    }

    /// Collects records so attribution can be asserted.
    #[derive(Clone, Default)]
    struct CapturingSink(Arc<Mutex<Vec<UsageRecord>>>);

    #[async_trait::async_trait]
    impl UsageSink for CapturingSink {
        fn name(&self) -> &'static str {
            "capture"
        }

        async fn record(&self, record: &UsageRecord) {
            self.0.lock().unwrap().push(record.clone());
        }
    }

    /// A stand-in provider that rate-limits one key and serves the other, so the
    /// pool walk is exercised over real HTTP.
    async fn rate_limiting_upstream(exhausted_key: &'static str) -> String {
        let app = Router::new().route(
            "/chat/completions",
            post(move |headers: HeaderMap| async move {
                let authorized = headers
                    .get(axum::http::header::AUTHORIZATION)
                    .and_then(|v| v.to_str().ok())
                    .and_then(|v| v.strip_prefix("Bearer "))
                    .unwrap_or_default()
                    != exhausted_key;
                if authorized {
                    (
                        StatusCode::OK,
                        Json(json!({
                            "id": "chatcmpl-1",
                            "choices": [],
                            "usage": { "prompt_tokens": 10, "completion_tokens": 5 }
                        })),
                    )
                } else {
                    (
                        StatusCode::TOO_MANY_REQUESTS,
                        Json(json!({ "error": { "message": "rate limit exceeded" } })),
                    )
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        format!("http://{addr}")
    }

    async fn credential_probe_upstream(reject_first: bool) -> (String, Arc<Mutex<Vec<String>>>) {
        let seen = Arc::new(Mutex::new(Vec::new()));
        let app_seen = seen.clone();
        let app = Router::new().route(
            "/responses",
            post(move |headers: HeaderMap| {
                let seen = app_seen.clone();
                async move {
                    let authorization = headers
                        .get(axum::http::header::AUTHORIZATION)
                        .and_then(|value| value.to_str().ok())
                        .unwrap_or_default()
                        .to_owned();
                    seen.lock().unwrap().push(authorization.clone());
                    if reject_first && authorization == "Bearer sk-a" {
                        (
                            StatusCode::TOO_MANY_REQUESTS,
                            Json(json!({ "error": { "message": "rate limit exceeded" } })),
                        )
                            .into_response()
                    } else {
                        Json(json!({
                            "id": "resp-1",
                            "object": "response",
                            "usage": {
                                "input_tokens": 10,
                                "output_tokens": 5
                            }
                        }))
                        .into_response()
                    }
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        (format!("http://{addr}"), seen)
    }

    fn two_credential_responses_state(base_url: &str, captured: CapturingSink) -> AppState {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "openai"
kind = "openai"
base_url = "{base_url}"

{GATEWAY_KEY}

[[credential]]
namespace = "platform"
provider = "openai"
env = "K1"
id = "openai-a"

[[credential]]
namespace = "platform"
provider = "openai"
env = "K2"
id = "openai-b"

[[model]]
name = "gpt-4o"
targets = [{{ provider = "openai", model = "gpt-4o", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 1000000 }} }}]
"#
        ))
        .unwrap();
        let env = env_with([("K1", "sk-a"), ("K2", "sk-b")]);
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(captured)];
        AppState::new(cfg, &env, UsageFanout::new(sinks), Box::new(NoBudget)).unwrap()
    }

    #[tokio::test]
    async fn a_rate_limited_credential_falls_to_the_next_and_is_attributed() {
        let base_url = rate_limiting_upstream("sk-exhausted").await;
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "openai"
kind = "openai"
base_url = "{base_url}"

{GATEWAY_KEY}

[[credential]]
namespace = "platform"
provider = "openai"
env = "K1"
id = "openai-a"

[[credential]]
namespace = "platform"
provider = "openai"
env = "K2"
id = "openai-b"

[[model]]
name = "gpt-4o"
targets = [{{ provider = "openai", model = "gpt-4o", price = {{ input_microdollars_per_million = 2500000, output_microdollars_per_million = 10000000 }} }}]
"#
        ))
        .unwrap();
        let env = env_with([("K1", "sk-exhausted"), ("K2", "sk-good")]);
        let captured = CapturingSink::default();
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(captured.clone())];
        let state = AppState::new(cfg, &env, UsageFanout::new(sinks), Box::new(NoBudget)).unwrap();

        let body = serde_json::to_vec(&json!({"model": "gpt-4o", "messages": []})).unwrap();
        let resp = router(state)
            .oneshot(
                authorized("/v1/chat/completions")
                    .body(Body::from(body))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(resp.status(), StatusCode::OK);
        let records = captured.0.lock().unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].credential_id, "openai-b");
        assert_eq!(records[0].credential_source, "platform");
        assert_eq!(records[0].signer_kid, None);
        // The pool made one target attempt (credential rotation is inner).
        assert_eq!(records[0].attempts, 1);
    }

    #[tokio::test]
    async fn buffered_pool_dispatch_emits_parented_lease_spans() {
        let base_url = rate_limiting_upstream("sk-rate-limited").await;
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "openai"
kind = "openai"
base_url = "{base_url}"

{GATEWAY_KEY}

[credential_pool]
failure_threshold = 1
cooldown_seconds = 60

[[credential]]
namespace = "platform"
provider = "openai"
env = "K_PARKED"
id = "parked"

[[credential]]
namespace = "platform"
provider = "openai"
env = "K_RATE"
id = "rate-limited"

[[credential]]
namespace = "platform"
provider = "openai"
env = "K_SERVED"
id = "served"

[[model]]
name = "gpt-4o"
targets = [{{ provider = "openai", model = "gpt-4o", price = {{ input_microdollars_per_million = 1, output_microdollars_per_million = 1 }} }}]
"#
        ))
        .unwrap();
        let state = AppState::new(
            cfg,
            &env_with([
                ("K_PARKED", "sk-parked"),
                ("K_RATE", "sk-rate-limited"),
                ("K_SERVED", "sk-served"),
            ]),
            UsageFanout::new(vec![Box::new(StdoutSink)]),
            Box::new(NoBudget),
        )
        .unwrap();
        let snapshot = state.config();
        let parked = snapshot
            .credentials
            .plan(&snapshot.config, "platform", "openai")
            .unwrap()
            .attempts
            .into_iter()
            .find(|lease| lease.id == "parked")
            .unwrap();
        snapshot.credentials.record_failure(&parked);
        snapshot.credentials.record_failure(&parked);

        let exporter = InMemorySpanExporter::default();
        let provider = SdkTracerProvider::builder()
            .with_simple_exporter(exporter.clone())
            .build();
        let subscriber = tracing_subscriber::registry()
            .with(tracing_opentelemetry::layer().with_tracer(provider.tracer("axond-test")));
        let body = serde_json::to_vec(&json!({"model": "gpt-4o", "messages": []})).unwrap();
        let dispatch = tracing::Dispatch::new(subscriber);
        let response = tokio::spawn(async move {
            let _default = tracing::dispatcher::set_default(&dispatch);
            router(state)
                .oneshot(
                    authorized("/v1/chat/completions")
                        .body(Body::from(body))
                        .unwrap(),
                )
                .await
                .unwrap()
        })
        .await
        .unwrap();

        assert_eq!(response.status(), StatusCode::OK);
        provider.force_flush().unwrap();
        let spans = exporter.get_finished_spans().unwrap();
        let attempt = spans
            .iter()
            .find(|span| span.name == "axond.upstream.attempt")
            .unwrap();
        let leases: Vec<_> = spans
            .iter()
            .filter(|span| span.name == "axond.credential.lease")
            .collect();
        assert_eq!(leases.len(), 3);
        let attribute = |span: &opentelemetry_sdk::trace::SpanData, key: &str| {
            span.attributes
                .iter()
                .find(|kv| kv.key.as_str() == key)
                .map(|kv| kv.value.to_string())
        };
        for (id, status) in [
            ("parked", "parked"),
            ("rate-limited", "rate_limited"),
            ("served", "served"),
        ] {
            let lease = leases
                .iter()
                .find(|span| attribute(span, "axond.credential.id").as_deref() == Some(id))
                .unwrap();
            assert_eq!(lease.parent_span_id, attempt.span_context.span_id());
            assert_eq!(attribute(lease, "axond.status").as_deref(), Some(status));
        }
    }

    /// A stand-in provider whose health is flipped at test time, counting the
    /// requests that actually reached it. Serves `200` while `healthy`, and the
    /// given status otherwise.
    #[derive(Clone)]
    struct ControllableState {
        healthy: Arc<AtomicBool>,
        hits: Arc<AtomicUsize>,
        unhealthy_status: StatusCode,
    }

    async fn controllable_handler(State(state): State<ControllableState>) -> Response {
        state.hits.fetch_add(1, Ordering::SeqCst);
        if state.healthy.load(Ordering::SeqCst) {
            Json(json!({
                "id": "resp-1",
                "object": "response",
                "choices": [],
                "usage": {
                    "input_tokens": 10,
                    "output_tokens": 5,
                    "prompt_tokens": 10,
                    "completion_tokens": 5
                }
            }))
            .into_response()
        } else {
            (
                state.unhealthy_status,
                Json(json!({ "error": { "message": "upstream is unwell" } })),
            )
                .into_response()
        }
    }

    async fn controllable_upstream(
        healthy: Arc<AtomicBool>,
        unhealthy_status: StatusCode,
    ) -> (String, Arc<AtomicUsize>) {
        let hits = Arc::new(AtomicUsize::new(0));
        let state = ControllableState {
            healthy,
            hits: hits.clone(),
            unhealthy_status,
        };
        let app = Router::new()
            .route("/chat/completions", post(controllable_handler))
            .route("/responses", post(controllable_handler))
            .with_state(state);
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        (format!("http://{addr}"), hits)
    }

    /// Two targets (`pa/m-a` then `pb/m-b`) behind one alias, sharing one
    /// `AppState` so the per-target circuit persists across requests.
    fn two_target_state(
        url_a: &str,
        url_b: &str,
        failover: &str,
        captured: CapturingSink,
    ) -> AppState {
        two_target_state_with_budget(url_a, url_b, failover, captured, Box::new(NoBudget))
    }

    fn two_target_state_with_budget(
        url_a: &str,
        url_b: &str,
        failover: &str,
        captured: CapturingSink,
        budget: Box<dyn crate::budget::BudgetStore>,
    ) -> AppState {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "pa"
kind = "openai"
base_url = "{url_a}"

[[provider]]
id = "pb"
kind = "openai"
base_url = "{url_b}"

{GATEWAY_KEY}

[[credential]]
namespace = "platform"
provider = "pa"
env = "KA"
id = "cred-a"

[[credential]]
namespace = "platform"
provider = "pb"
env = "KB"
id = "cred-b"

{failover}

[[model]]
name = "gpt-4o"
targets = [
  {{ provider = "pa", model = "m-a", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 1000000 }} }},
  {{ provider = "pb", model = "m-b", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 1000000 }} }},
]
"#
        ))
        .unwrap();
        let env = env_with([("KA", "ka"), ("KB", "kb")]);
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(captured)];
        AppState::new(cfg, &env, UsageFanout::new(sinks), budget).unwrap()
    }

    fn chat_request() -> Request<Body> {
        let body = serde_json::to_vec(&json!({"model": "gpt-4o", "messages": []})).unwrap();
        authorized("/v1/chat/completions")
            .body(Body::from(body))
            .unwrap()
    }

    fn responses_request(previous_response_id: Option<&str>) -> Request<Body> {
        let mut body = json!({"model": "gpt-4o", "input": "hello"});
        if let Some(id) = previous_response_id {
            body["previous_response_id"] = json!(id);
        }
        authorized("/v1/responses")
            .body(Body::from(serde_json::to_vec(&body).unwrap()))
            .unwrap()
    }

    fn streaming_responses_request(previous_response_id: Option<&str>) -> Request<Body> {
        let mut body = json!({"model": "gpt-4o", "input": "hello", "stream": true});
        if let Some(id) = previous_response_id {
            body["previous_response_id"] = json!(id);
        }
        authorized("/v1/responses")
            .body(Body::from(serde_json::to_vec(&body).unwrap()))
            .unwrap()
    }

    fn responses_request_with_null_previous_id() -> Request<Body> {
        let body = json!({
            "model": "gpt-4o",
            "input": "hello",
            "previous_response_id": null
        });
        authorized("/v1/responses")
            .body(Body::from(serde_json::to_vec(&body).unwrap()))
            .unwrap()
    }

    /// Records what each request held and what it settled for, so the charging
    /// policy is asserted through the real request path.
    #[derive(Default, Clone)]
    struct RecordingBudget(Arc<Mutex<Vec<(u64, u64)>>>);

    #[async_trait::async_trait]
    impl crate::budget::BudgetStore for RecordingBudget {
        fn name(&self) -> &'static str {
            "recording"
        }
        async fn reserve(&self, _key: &BudgetKey, estimated_microdollars: u64) -> Admission {
            self.0.lock().unwrap().push((estimated_microdollars, 0));
            Admission::Allowed(Reservation {
                id: "recording".to_owned(),
                estimate_microdollars: estimated_microdollars,
            })
        }
        async fn settle(
            &self,
            _key: &BudgetKey,
            _reservation: &Reservation,
            actual_microdollars: u64,
        ) {
            if let Some(last) = self.0.lock().unwrap().last_mut() {
                last.1 = actual_microdollars;
            }
        }
    }

    struct SharedLimiter(Arc<InMemoryRateLimiter>);

    #[async_trait::async_trait]
    impl RateLimiter for SharedLimiter {
        fn name(&self) -> &'static str {
            "shared-test"
        }

        async fn acquire(
            &self,
            key: &RateLimitKey,
        ) -> Result<crate::rate_limit::RateLimitPermit, crate::rate_limit::RateLimitError> {
            self.0.acquire(key).await
        }
    }

    struct UnavailableLimiter;

    #[async_trait::async_trait]
    impl RateLimiter for UnavailableLimiter {
        fn name(&self) -> &'static str {
            "unavailable-test"
        }

        async fn acquire(
            &self,
            _key: &RateLimitKey,
        ) -> Result<crate::rate_limit::RateLimitPermit, crate::rate_limit::RateLimitError> {
            Err(crate::rate_limit::RateLimitError::StoreUnavailable)
        }
    }

    struct FakeRevocation {
        mode: FakeRevocationMode,
        calls: Arc<AtomicUsize>,
    }

    enum FakeRevocationMode {
        Revoked,
        Unavailable,
        Allow,
    }

    #[async_trait::async_trait]
    impl crate::revocation::RevocationStore for FakeRevocation {
        fn name(&self) -> &'static str {
            "fake"
        }

        async fn is_revoked(&self, _jti: &str) -> Result<bool, crate::revocation::RevocationError> {
            self.calls.fetch_add(1, Ordering::Relaxed);
            match self.mode {
                FakeRevocationMode::Revoked => Ok(true),
                FakeRevocationMode::Unavailable => {
                    Err(crate::revocation::RevocationError::Unavailable {
                        backend: "fake",
                        message: "test outage".to_owned(),
                    })
                }
                FakeRevocationMode::Allow => Ok(false),
            }
        }

        async fn revoke(
            &self,
            _jti: &str,
            _expires_at: SystemTime,
        ) -> Result<(), crate::revocation::RevocationError> {
            Ok(())
        }
    }

    struct RejectingBudget;

    #[async_trait::async_trait]
    impl crate::budget::BudgetStore for RejectingBudget {
        fn name(&self) -> &'static str {
            "rejecting"
        }

        async fn reserve(&self, _key: &BudgetKey, _estimate: u64) -> Admission {
            Admission::Denied(Denial::Exceeded)
        }

        async fn settle(&self, _key: &BudgetKey, _reservation: &Reservation, _actual: u64) {}
    }

    struct SharedBudget(Arc<crate::budget::InMemoryBudget>);

    #[async_trait::async_trait]
    impl crate::budget::BudgetStore for SharedBudget {
        fn name(&self) -> &'static str {
            "shared_test"
        }

        async fn reserve(&self, key: &BudgetKey, estimated: u64) -> Admission {
            self.0.reserve(key, estimated).await
        }

        async fn settle(&self, key: &BudgetKey, reservation: &Reservation, actual: u64) {
            self.0.settle(key, reservation, actual).await;
        }
    }

    /// One target, one credential, and the budget store under test.
    fn budgeted_state(base_url: &str, budget: Box<dyn crate::budget::BudgetStore>) -> AppState {
        budgeted_state_with_limiter(base_url, budget, Box::new(NoLimit))
    }

    fn budgeted_state_with_limiter(
        base_url: &str,
        budget: Box<dyn crate::budget::BudgetStore>,
        rate_limiter: Box<dyn RateLimiter>,
    ) -> AppState {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "openai"
kind = "openai"
base_url = "{base_url}"

{GATEWAY_KEY}

[[credential]]
namespace = "platform"
provider = "openai"
env = "K1"

[[model]]
name = "gpt-4o"
targets = [{{ provider = "openai", model = "gpt-4o", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 1000000 }} }}]
"#
        ))
        .unwrap();
        let env = env_with([("K1", "sk-test")]);
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(StdoutSink)];
        AppState::new_with_rate_limiter(
            cfg,
            &env,
            UsageFanout::new(sinks),
            budget,
            rate_limiter,
            Box::new(crate::revocation::NoDenylist),
        )
        .unwrap()
    }

    /// One target, one credential, and an explicit `[admission]` section.
    fn admitting_state(
        base_url: &str,
        admission: &str,
        budget: Box<dyn crate::budget::BudgetStore>,
    ) -> AppState {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "openai"
kind = "openai"
base_url = "{base_url}"

{GATEWAY_KEY}

[[credential]]
namespace = "platform"
provider = "openai"
env = "K1"

[[model]]
name = "gpt-4o"
targets = [{{ provider = "openai", model = "gpt-4o", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 1000000 }} }}]

[admission]
{admission}
"#
        ))
        .unwrap();
        let env = env_with([("K1", "sk-test")]);
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(StdoutSink)];
        AppState::new_with_rate_limiter(
            cfg,
            &env,
            UsageFanout::new(sinks),
            budget,
            Box::new(NoLimit),
            Box::new(crate::revocation::NoDenylist),
        )
        .unwrap()
    }

    /// Shedding is the first thing the request path spends nothing on: a
    /// saturated replica must not pay for a rate-limit round trip, a budget
    /// reservation, or a provider call to say no.
    #[tokio::test]
    async fn a_shed_request_costs_no_budget_and_no_provider_call() {
        let (base_url, hits) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let budget = RecordingBudget::default();
        let state = admitting_state(
            &base_url,
            "max_in_flight = 1\nmax_in_flight_streams = 1\nmax_in_flight_per_tenant = 0",
            Box::new(budget.clone()),
        );
        let held = state
            .0
            .admission
            .admit("platform", crate::admission::RequestKind::Buffered)
            .await
            .expect("the only slot");

        let response = router(state.clone()).oneshot(chat_request()).await.unwrap();
        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "gateway_overloaded");
        assert!(budget.0.lock().unwrap().is_empty());
        assert_eq!(hits.load(Ordering::SeqCst), 0);

        // The permit the shed request never took is still the held one; giving
        // it back admits the next caller.
        drop(held);
        let served = router(state).oneshot(chat_request()).await.unwrap();
        assert_eq!(served.status(), StatusCode::OK);
        assert!(hits.load(Ordering::SeqCst) > 0);
    }

    /// A tenant's own ceiling is the caller's problem (429); the process's is
    /// the replica's (503). An operator reading either one knows which.
    #[tokio::test]
    async fn a_tenant_ceiling_sheds_as_429_and_leaves_the_replica_serving() {
        let (base_url, _) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let state = admitting_state(
            &base_url,
            "max_in_flight = 8\nmax_in_flight_streams = 8\nmax_in_flight_per_tenant = 1",
            Box::new(NoBudget),
        );
        let held = state
            .0
            .admission
            .admit("platform", crate::admission::RequestKind::Buffered)
            .await
            .expect("the tenant's only slot");

        let response = router(state.clone()).oneshot(chat_request()).await.unwrap();
        assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "tenant_concurrency_exceeded");

        // Another tenant's request is unaffected: the ceiling that fired was
        // this tenant's, not the process's.
        assert!(
            state
                .0
                .admission
                .admit("other", crate::admission::RequestKind::Buffered)
                .await
                .is_ok()
        );
        drop(held);
    }

    /// An admitted request gives its capacity back when the handler returns, so
    /// a bounded replica serves an unbounded number of sequential requests.
    #[tokio::test]
    async fn a_completed_request_releases_the_capacity_it_held() {
        let (base_url, _) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let state = admitting_state(
            &base_url,
            "max_in_flight = 1\nmax_in_flight_streams = 1\nmax_in_flight_per_tenant = 1",
            Box::new(NoBudget),
        );
        for _ in 0..3 {
            let response = router(state.clone()).oneshot(chat_request()).await.unwrap();
            assert_eq!(response.status(), StatusCode::OK);
        }
    }

    /// The per-request bounds are refusals, not clamps, and neither answer
    /// repeats what the caller sent.
    #[tokio::test]
    async fn per_request_bounds_are_typed_and_never_echo_the_request() {
        let (base_url, hits) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let budget = RecordingBudget::default();
        let state = admitting_state(
            &base_url,
            "max_prompt_tokens = 64\nmax_output_tokens = 16",
            Box::new(budget.clone()),
        );

        let long_prompt = json!({
            "model": "gpt-4o",
            "messages": [{"role": "user", "content": "sensitive ".repeat(32)}]
        });
        let response = router(state.clone())
            .oneshot(
                authorized("/v1/chat/completions")
                    .body(Body::from(serde_json::to_vec(&long_prompt).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "prompt_too_large");
        assert!(!body.to_string().contains("sensitive"), "{body}");

        let large_output = json!({
            "model": "gpt-4o",
            "messages": [],
            "max_tokens": 4096
        });
        let response = router(state)
            .oneshot(
                authorized("/v1/chat/completions")
                    .body(Body::from(serde_json::to_vec(&large_output).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "output_limit_exceeded");

        assert!(budget.0.lock().unwrap().is_empty());
        assert_eq!(hits.load(Ordering::SeqCst), 0);
    }

    /// The router's own body limit answers before the body is buffered, so an
    /// oversized request is a typed 413 rather than a parse error.
    #[tokio::test]
    async fn an_oversized_body_is_refused_by_the_router_not_the_parser() {
        let (base_url, hits) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let state = admitting_state(&base_url, "max_request_bytes = 512", Box::new(NoBudget));
        let response = router(state)
            .oneshot(
                authorized("/v1/chat/completions")
                    .body(Body::from(
                        serde_json::to_vec(&json!({
                            "model": "gpt-4o",
                            "messages": [{"role": "user", "content": "x".repeat(4096)}]
                        }))
                        .unwrap(),
                    ))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "request_too_large");
        assert_eq!(hits.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn limiter_runs_before_budget_reservation() {
        let limiter = Arc::new(InMemoryRateLimiter::new(1, 10));
        let held = limiter
            .acquire(&RateLimitKey {
                namespace: "platform".to_owned(),
                subject: "AXOND_INBOUND_KEY".to_owned(),
            })
            .await
            .expect("held permit");
        let budget = RecordingBudget::default();
        let state = budgeted_state_with_limiter(
            "http://127.0.0.1:1",
            Box::new(budget.clone()),
            Box::new(SharedLimiter(Arc::clone(&limiter))),
        );
        let response = router(state).oneshot(chat_request()).await.unwrap();
        assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
        let no_retry_after = response.headers().get("retry-after").is_none();
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "rate_limited");
        assert!(no_retry_after);
        assert!(budget.0.lock().unwrap().is_empty());
        drop(held);
    }

    #[tokio::test]
    async fn unavailable_rate_limit_store_is_a_typed_503_before_budget_reservation() {
        let budget = RecordingBudget::default();
        let state = budgeted_state_with_limiter(
            "http://127.0.0.1:1",
            Box::new(budget.clone()),
            Box::new(UnavailableLimiter),
        );

        let response = router(state).oneshot(chat_request()).await.unwrap();
        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
        let bytes = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(body["error"]["type"], "rate_limit_unavailable");
        assert!(budget.0.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn budget_denial_does_not_leave_limiter_saturated() {
        let limiter = Arc::new(InMemoryRateLimiter::new(1, 10));
        let state = budgeted_state_with_limiter(
            "http://127.0.0.1:1",
            Box::new(RejectingBudget),
            Box::new(SharedLimiter(Arc::clone(&limiter))),
        );
        for _ in 0..2 {
            let response = router(state.clone()).oneshot(chat_request()).await.unwrap();
            assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
            let bytes = response.into_body().collect().await.unwrap().to_bytes();
            let body: Value = serde_json::from_slice(&bytes).unwrap();
            assert_eq!(body["error"]["type"], "budget_exceeded");
        }
    }

    #[tokio::test]
    async fn a_request_over_its_ceiling_is_rejected_before_reservation_or_dispatch() {
        let (base_url, hits) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let budget = RecordingBudget::default();
        let state = budgeted_state(&base_url, Box::new(budget.clone()));
        let snapshot = state.config();
        let caller = InboundKey {
            namespace: "platform".to_owned(),
            subject: "ceiling-caller".to_owned(),
            authority: PrincipalAuthority::MintedToken,
            signer_kid: Some("test-kid".to_owned()),
            scope: None,
            alias_scope: None,
            max_request_microdollars: Some(1),
            can_mint: false,
            jti: None,
        };
        let body = json!({"model": "gpt-4o", "messages": []});

        let error = serve(
            state,
            HeaderMap::new(),
            body,
            Route::ChatCompletions,
            snapshot,
            caller,
        )
        .await
        .expect_err("the estimate exceeds the caller ceiling");

        let response = error.into_response();
        assert_eq!(response.status(), StatusCode::FORBIDDEN);
        let body = response.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(body["error"]["type"], "request_cost_ceiling_exceeded");
        assert_eq!(hits.load(Ordering::SeqCst), 0);
        assert!(budget.0.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn a_request_under_its_ceiling_still_reserves_and_dispatches() {
        let (base_url, hits) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let budget = RecordingBudget::default();
        let state = budgeted_state(&base_url, Box::new(budget.clone()));
        let snapshot = state.config();
        let caller = InboundKey {
            namespace: "platform".to_owned(),
            subject: "ceiling-caller".to_owned(),
            authority: PrincipalAuthority::MintedToken,
            signer_kid: Some("test-kid".to_owned()),
            scope: None,
            alias_scope: None,
            max_request_microdollars: Some(10_000),
            can_mint: false,
            jti: None,
        };
        let body = json!({"model": "gpt-4o", "messages": []});

        let response = serve(
            state,
            HeaderMap::new(),
            body,
            Route::ChatCompletions,
            snapshot,
            caller,
        )
        .await
        .expect("the estimate is under the caller ceiling");

        assert_eq!(response.status(), StatusCode::OK);
        assert!(hits.load(Ordering::SeqCst) > 0);
        assert!(!budget.0.lock().unwrap().is_empty());
    }

    /// The reserved estimate is a ceiling, not the charge: a completed request
    /// settles the cost of the usage the provider reported.
    #[tokio::test]
    async fn a_buffered_response_settles_its_measured_cost() {
        let (base_url, _) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let budget = RecordingBudget::default();
        let state = budgeted_state(&base_url, Box::new(budget.clone()));

        let resp = router(state).oneshot(chat_request()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let ledger = budget.0.lock().unwrap();
        let (estimated, settled) = ledger[0];
        // 10 input + 5 output tokens at 1 µ$ each.
        assert_eq!(settled, 15);
        assert!(
            estimated > settled,
            "the estimate should be the conservative ceiling ({estimated} vs {settled})"
        );
    }

    /// A buffered failure reports no usage at all, so the spend is unknowable
    /// and charged as zero — and the hold is released rather than left to
    /// expire (ADR 0010).
    #[tokio::test]
    async fn a_buffered_upstream_failure_charges_nothing_and_releases_its_hold() {
        let (base_url, _) = controllable_upstream(
            Arc::new(AtomicBool::new(false)),
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;
        let budget = RecordingBudget::default();
        let state = budgeted_state(&base_url, Box::new(budget.clone()));

        let resp = router(state).oneshot(chat_request()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::BAD_GATEWAY);

        let ledger = budget.0.lock().unwrap();
        assert_eq!(ledger.len(), 1);
        assert_eq!(ledger[0].1, 0);
    }

    /// A cancelled buffered handler drops its reservation guard while the
    /// dispatcher is waiting for the provider; the detached release must run
    /// before the next request can observe the ledger.
    #[tokio::test]
    async fn a_cancelled_buffered_request_releases_its_reservation() {
        let (started_tx, started_rx) = oneshot::channel();
        let started_tx = Arc::new(Mutex::new(Some(started_tx)));
        let upstream = Router::new().route(
            "/chat/completions",
            post({
                let started_tx = started_tx.clone();
                move || async move {
                    if let Some(started_tx) = started_tx.lock().unwrap().take() {
                        let _ = started_tx.send(());
                    }
                    pending::<()>().await;
                    StatusCode::OK.into_response()
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, upstream).await.unwrap() });

        let budget = Arc::new(crate::budget::InMemoryBudget::new(10_000));
        let state = budgeted_state(
            &format!("http://{addr}"),
            Box::new(SharedBudget(budget.clone())),
        );
        let request = tokio::spawn(router(state).oneshot(chat_request()));
        started_rx.await.unwrap();
        request.abort();
        assert!(request.await.unwrap_err().is_cancelled());

        let key = BudgetKey {
            namespace: "platform".to_owned(),
            subject: "AXOND_INBOUND_KEY".to_owned(),
        };
        let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
        loop {
            let outstanding = budget.outstanding(&key);
            if outstanding == 0 {
                break;
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "reservation was not released before timeout; outstanding={outstanding}"
            );
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    }

    /// The shutdown deadline has to reach a request that has not produced a
    /// response yet. Such a request is waiting on an upstream inside its
    /// handler, bounded only by the failover budget — minutes, potentially — so
    /// if the abandonment signal stopped at the response body, the request would
    /// keep its admission slot for the whole termination and the settle wait
    /// would spend the budget the buffered records need.
    #[tokio::test]
    async fn abandonment_cancels_a_request_still_inside_its_handler() {
        let (started_tx, started_rx) = oneshot::channel();
        let started_tx = Arc::new(Mutex::new(Some(started_tx)));
        let upstream = Router::new().route(
            "/chat/completions",
            post({
                let started_tx = started_tx.clone();
                move || async move {
                    if let Some(started_tx) = started_tx.lock().unwrap().take() {
                        let _ = started_tx.send(());
                    }
                    // Never answers: only the cancellation can end this request.
                    pending::<()>().await;
                    StatusCode::OK.into_response()
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, upstream).await.unwrap() });

        let budget = Arc::new(crate::budget::InMemoryBudget::new(10_000));
        let state = budgeted_state(
            &format!("http://{addr}"),
            Box::new(SharedBudget(budget.clone())),
        );
        let lifecycle = Arc::clone(state.lifecycle());
        let request = tokio::spawn(router(state).oneshot(chat_request()));
        // The handler is now inside the upstream call, holding its slot.
        started_rx.await.unwrap();
        assert_eq!(lifecycle.in_flight(), 1);

        lifecycle.abandon();

        let response = request.await.unwrap().unwrap();
        // Refused with the drain's own answer rather than left hanging: the
        // caller learns the replica is going away and can retry elsewhere.
        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
        // And the slot is back, so the flush budget is not spent waiting for it.
        assert_eq!(lifecycle.in_flight(), 0);
    }

    /// The two denials are different answers to the caller: over-cap is the
    /// caller's problem, an unenforceable cap is the gateway's.
    #[tokio::test]
    async fn a_denied_request_never_reaches_the_provider() {
        struct Denying(Denial);

        #[async_trait::async_trait]
        impl crate::budget::BudgetStore for Denying {
            fn name(&self) -> &'static str {
                "denying"
            }
            async fn reserve(&self, _key: &BudgetKey, _estimated: u64) -> Admission {
                Admission::Denied(self.0)
            }
            async fn settle(&self, _key: &BudgetKey, _reservation: &Reservation, _actual: u64) {}
        }

        let (base_url, hits) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        for (denial, expected) in [
            (Denial::Exceeded, StatusCode::TOO_MANY_REQUESTS),
            (Denial::StoreUnavailable, StatusCode::SERVICE_UNAVAILABLE),
        ] {
            let state = budgeted_state(&base_url, Box::new(Denying(denial)));
            let resp = router(state).oneshot(chat_request()).await.unwrap();
            assert_eq!(resp.status(), expected);
        }
        assert_eq!(hits.load(Ordering::SeqCst), 0);
    }

    /// The cap is per `(namespace, subject)`, and two gateways sharing one store
    /// see each other's spend — the fleet enforces one cap, not one per replica.
    #[tokio::test]
    async fn two_replicas_sharing_one_store_enforce_a_single_cap() {
        let (base_url, _) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        // `max_tokens` bounds the reserved output allowance, so the estimate is
        // small and known: 16 output tokens plus the body's input estimate.
        let capped_request = || {
            let body =
                serde_json::to_vec(&json!({"model": "gpt-4o", "messages": [], "max_tokens": 16}))
                    .unwrap();
            authorized("/v1/chat/completions")
                .body(Body::from(body))
                .unwrap()
        };
        let shared = Arc::new(crate::budget::InMemoryBudget::new(30));

        struct Shared(Arc<crate::budget::InMemoryBudget>);

        #[async_trait::async_trait]
        impl crate::budget::BudgetStore for Shared {
            fn name(&self) -> &'static str {
                "shared"
            }
            async fn reserve(&self, key: &BudgetKey, estimated: u64) -> Admission {
                self.0.reserve(key, estimated).await
            }
            async fn settle(&self, key: &BudgetKey, reservation: &Reservation, actual: u64) {
                self.0.settle(key, reservation, actual).await;
            }
        }

        let replica_a = router(budgeted_state(&base_url, Box::new(Shared(shared.clone()))));
        let replica_b = router(budgeted_state(&base_url, Box::new(Shared(shared))));

        let first = replica_a.oneshot(capped_request()).await.unwrap();
        assert_eq!(first.status(), StatusCode::OK);
        // The 15 µ$ the other replica settled leaves no room for a second
        // estimate under the shared cap.
        let second = replica_b.oneshot(capped_request()).await.unwrap();
        assert_eq!(second.status(), StatusCode::TOO_MANY_REQUESTS);
    }

    #[tokio::test]
    async fn a_retryable_first_target_fails_over_to_the_second() {
        let (url_a, hits_a) = controllable_upstream(
            Arc::new(AtomicBool::new(false)),
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;
        let (url_b, hits_b) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let captured = CapturingSink::default();
        let state = two_target_state(&url_a, &url_b, "", captured.clone());

        let resp = router(state).oneshot(chat_request()).await.unwrap();

        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 1);
        let records = captured.0.lock().unwrap();
        assert_eq!(records.len(), 1);
        // The second target served, and both target attempts are attributed.
        assert_eq!(records[0].status.as_str(), "ok");
        assert_eq!(records[0].target_provider, "pb");
        assert_eq!(records[0].target_model, "m-b");
        assert_eq!(records[0].credential_id, "cred-b");
        assert_eq!(records[0].attempts, 2);
    }

    /// Affinity a continuation can recover requires the *initial* call to have
    /// used the first target too, so no Responses request fails over — not even
    /// one with no `previous_response_id` to lose.
    #[tokio::test]
    async fn every_responses_request_uses_the_first_target_without_failover() {
        let (url_a, hits_a) = controllable_upstream(
            Arc::new(AtomicBool::new(false)),
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;
        let (url_b, hits_b) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let captured = CapturingSink::default();
        let state = two_target_state(
            &url_a,
            &url_b,
            "[failover]\nmax_attempts = 3\nfailure_threshold = 10",
            captured.clone(),
        );

        for (index, request) in [
            responses_request(Some("resp-from-a")),
            responses_request_with_null_previous_id(),
            responses_request(None),
            streaming_responses_request(None),
        ]
        .into_iter()
        .enumerate()
        {
            let response = router(state.clone()).oneshot(request).await.unwrap();
            assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
            let _ = response.into_body().collect().await.unwrap();
            assert_eq!(hits_a.load(Ordering::SeqCst), index + 1);
            assert_eq!(hits_b.load(Ordering::SeqCst), 0);
        }

        // Streaming settlement is detached from the response future. Wait for
        // the fourth Responses record before issuing chat so the record order
        // below cannot race the settlement task.
        let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
        loop {
            let count = captured.0.lock().unwrap().len();
            if count >= 4 {
                break;
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "streamed Responses settlement did not arrive before timeout; records={count}"
            );
            tokio::time::sleep(Duration::from_millis(5)).await;
        }

        // Chat over the same alias still fails over: pinning is scoped to the
        // Responses wire, not to the alias.
        let chat = router(state).oneshot(chat_request()).await.unwrap();
        assert_eq!(chat.status(), StatusCode::OK);
        assert_eq!(hits_b.load(Ordering::SeqCst), 1);

        let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
        loop {
            let count = captured.0.lock().unwrap().len();
            if count >= 5 {
                break;
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "chat settlement did not arrive before timeout; records={count}"
            );
            tokio::time::sleep(Duration::from_millis(5)).await;
        }

        let records = captured.0.lock().unwrap();
        assert_eq!(records.len(), 5);
        for record in records.iter().take(4) {
            assert_eq!(record.status.as_str(), "upstream_error");
            assert_eq!(record.attempts, 1);
            assert_eq!(record.target_provider, "pa");
        }
        assert_eq!(records[4].status.as_str(), "ok");
        assert_eq!(records[4].target_provider, "pb");
    }

    /// Initial and continuation requests share the pin but not its error
    /// semantics: only a request carrying a `previous_response_id` has affinity
    /// to lose, so only it reports `continuation_affinity_unavailable`. An
    /// initial request that cannot use the pinned target reports the ordinary
    /// routing error.
    #[tokio::test]
    async fn only_a_continuation_reports_lost_affinity_for_a_skipped_first_target() {
        let (url_a, hits_a) = controllable_upstream(
            Arc::new(AtomicBool::new(false)),
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;
        let (url_b, hits_b) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let captured = CapturingSink::default();
        let state = two_target_state(
            &url_a,
            &url_b,
            "[failover]\nmax_attempts = 3\nfailure_threshold = 1",
            captured.clone(),
        );

        // The initial call is pinned to the failing first target, tripping its
        // breaker without ever reaching the second one.
        let first = router(state.clone())
            .oneshot(responses_request(None))
            .await
            .unwrap();
        assert_eq!(first.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 0);

        let pinned = router(state.clone())
            .oneshot(responses_request(Some("resp-from-a")))
            .await
            .unwrap();
        assert_eq!(pinned.status(), StatusCode::SERVICE_UNAVAILABLE);
        let body = pinned.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(body["error"]["type"], "continuation_affinity_unavailable");
        assert!(
            body["error"]["message"]
                .as_str()
                .unwrap()
                .contains("continuation affinity")
        );

        let initial = router(state)
            .oneshot(responses_request(None))
            .await
            .unwrap();
        assert_eq!(initial.status(), StatusCode::SERVICE_UNAVAILABLE);
        let body = initial.into_body().collect().await.unwrap().to_bytes();
        let body: Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(body["error"]["type"], "all_provider_circuits_open");

        // Neither request that skipped the pinned target reached an upstream.
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 0);
        let records = captured.0.lock().unwrap();
        assert_eq!(records.len(), 1);
    }

    #[tokio::test]
    async fn a_pinned_streaming_responses_skipped_target_releases_its_budget_hold() {
        let (url_a, hits_a) = controllable_upstream(
            Arc::new(AtomicBool::new(false)),
            StatusCode::INTERNAL_SERVER_ERROR,
        )
        .await;
        let (url_b, hits_b) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let captured = CapturingSink::default();
        let budget = Arc::new(crate::budget::InMemoryBudget::new(1_000_000));
        let state = two_target_state_with_budget(
            &url_a,
            &url_b,
            "[failover]\nmax_attempts = 3\nfailure_threshold = 1",
            captured,
            Box::new(SharedBudget(budget.clone())),
        );

        let first = router(state.clone())
            .oneshot(responses_request(None))
            .await
            .unwrap();
        assert_eq!(first.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 0);

        // Both streamed shapes skip the tripped pinned target, and neither may
        // leave its reservation outstanding.
        for request in [
            streaming_responses_request(Some("resp-from-a")),
            streaming_responses_request(None),
        ] {
            let pinned = router(state.clone()).oneshot(request).await.unwrap();
            assert_eq!(pinned.status(), StatusCode::SERVICE_UNAVAILABLE);
            let _ = pinned.into_body().collect().await.unwrap();
        }
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 0);

        let key = BudgetKey {
            namespace: "platform".to_owned(),
            subject: "AXOND_INBOUND_KEY".to_owned(),
        };
        let deadline = tokio::time::Instant::now() + Duration::from_secs(2);
        loop {
            if budget.outstanding(&key) == 0 {
                break;
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "a pinned Responses request leaked its budget reservation"
            );
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    }

    /// A response created under a rotated key is one no continuation can
    /// recover, so the pooled credential is pinned for initial calls too.
    #[tokio::test]
    async fn every_responses_request_reuses_the_first_pooled_credential() {
        let (base_url, seen) = credential_probe_upstream(false).await;
        let captured = CapturingSink::default();
        let state = two_credential_responses_state(&base_url, captured);

        for _ in 0..2 {
            assert_eq!(
                router(state.clone())
                    .oneshot(responses_request(Some("resp-from-a")))
                    .await
                    .unwrap()
                    .status(),
                StatusCode::OK
            );
        }
        for _ in 0..2 {
            assert_eq!(
                router(state.clone())
                    .oneshot(responses_request(None))
                    .await
                    .unwrap()
                    .status(),
                StatusCode::OK
            );
        }
        let streamed = router(state)
            .oneshot(streaming_responses_request(None))
            .await
            .unwrap();
        let _ = streamed.into_body().collect().await.unwrap();

        assert_eq!(*seen.lock().unwrap(), ["Bearer sk-a"; 5]);
    }

    #[tokio::test]
    async fn a_pinned_responses_rate_limit_does_not_rotate_credentials() {
        let (base_url, seen) = credential_probe_upstream(true).await;
        let captured = CapturingSink::default();
        let state = two_credential_responses_state(&base_url, captured);

        let response = router(state.clone())
            .oneshot(responses_request(Some("resp-from-a")))
            .await
            .unwrap();

        assert_eq!(response.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(*seen.lock().unwrap(), ["Bearer sk-a"]);

        // The initial call is pinned to the same exhausted key: rotation stays
        // off, and it reports the ordinary upstream error.
        let initial = router(state)
            .oneshot(responses_request(None))
            .await
            .unwrap();
        assert_eq!(initial.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(*seen.lock().unwrap(), ["Bearer sk-a", "Bearer sk-a"]);
    }

    #[tokio::test]
    async fn a_non_retryable_error_is_not_failed_over() {
        let (url_a, hits_a) =
            controllable_upstream(Arc::new(AtomicBool::new(false)), StatusCode::BAD_REQUEST).await;
        let (url_b, hits_b) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let captured = CapturingSink::default();
        let state = two_target_state(&url_a, &url_b, "", captured.clone());

        let resp = router(state).oneshot(chat_request()).await.unwrap();

        // A 4xx-class (non-retryable) error stops the walk: the error is returned
        // and the second target is never tried.
        assert_eq!(resp.status(), StatusCode::BAD_GATEWAY);
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 0);
        let records = captured.0.lock().unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].status.as_str(), "upstream_error");
        assert_eq!(records[0].target_provider, "pa");
        assert_eq!(records[0].attempts, 1);
    }

    /// What the upstream was actually sent, so passthrough can be asserted from
    /// the provider's side as well as the caller's.
    type Received = Arc<Mutex<Vec<(Value, HeaderMap)>>>;

    /// A stand-in provider serving one native path, answering with a fixed body
    /// (or SSE text) and recording every request it received.
    async fn native_upstream(path: &'static str, answer: Response) -> (String, Received) {
        let received: Received = Arc::new(Mutex::new(Vec::new()));
        let seen = received.clone();
        let answer = Arc::new(Mutex::new(Some(answer)));
        let app = Router::new().route(
            path,
            post(move |headers: HeaderMap, Json(body): Json<Value>| {
                let seen = seen.clone();
                let answer = answer.clone();
                async move {
                    seen.lock().unwrap().push((body, headers));
                    answer.lock().unwrap().take().expect("one request")
                }
            }),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
        (format!("http://{addr}"), received)
    }

    /// One alias over one target of the given provider kind, at 1 µ$/token both
    /// ways so a settled cost reads as a token count.
    fn native_state(kind: &str, base_url: &str, captured: CapturingSink) -> AppState {
        let cfg = Config::from_toml_str(&format!(
            r#"
[[namespace]]
id = "platform"
default = true

[[provider]]
id = "p"
kind = "{kind}"
base_url = "{base_url}"

{GATEWAY_KEY}

[[credential]]
namespace = "platform"
provider = "p"
env = "K1"
id = "cred-a"

[[model]]
name = "alias"
targets = [{{ provider = "p", model = "upstream-model", price = {{ input_microdollars_per_million = 1000000, output_microdollars_per_million = 1000000 }} }}]
"#
        ))
        .unwrap();
        let env = env_with([("K1", "sk-test")]);
        let sinks: Vec<Box<dyn UsageSink>> = vec![Box::new(captured)];
        AppState::new(cfg, &env, UsageFanout::new(sinks), Box::new(NoBudget)).unwrap()
    }

    /// The point of serving the native wire: a body a translation would mangle
    /// (a signed thinking block, a tool-use block) crosses the gateway
    /// unchanged, and only `model` differs from what the caller sent.
    #[tokio::test]
    async fn a_native_message_is_forwarded_verbatim_with_only_the_model_rewritten() {
        let upstream_answer = json!({
            "id": "msg_1",
            "type": "message",
            "role": "assistant",
            "content": [
                { "type": "thinking", "thinking": "deliberating", "signature": "sig-abc" },
                { "type": "tool_use", "id": "toolu_1", "name": "search", "input": { "q": "x" } }
            ],
            "stop_reason": "tool_use",
            "usage": {
                "input_tokens": 10,
                "output_tokens": 5,
                "cache_creation_input_tokens": 2,
                "cache_read_input_tokens": 1
            }
        });
        let (base_url, received) =
            native_upstream("/messages", Json(upstream_answer.clone()).into_response()).await;
        let captured = CapturingSink::default();
        let state = native_state("anthropic", &base_url, captured.clone());

        let sent = json!({
            "model": "alias",
            "max_tokens": 64,
            "thinking": { "type": "enabled", "budget_tokens": 32 },
            "messages": [{
                "role": "assistant",
                "content": [
                    { "type": "thinking", "thinking": "earlier", "signature": "sig-prior" }
                ]
            }],
            "tools": [{ "name": "search", "input_schema": { "type": "object" } }]
        });
        let resp = router(state)
            .oneshot(
                authorized("/v1/messages")
                    .header("anthropic-version", "2099-01-01")
                    .body(Body::from(serde_json::to_vec(&sent).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let returned: Value = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(returned, upstream_answer);

        let requests = received.lock().unwrap();
        let (forwarded, headers) = &requests[0];
        let mut expected = sent.clone();
        expected["model"] = json!("upstream-model");
        assert_eq!(forwarded, &expected);
        // The caller's pinned wire version is honoured, and the provider key is
        // injected in Anthropic's own scheme.
        assert_eq!(headers["anthropic-version"], "2099-01-01");
        assert_eq!(headers["x-api-key"], "sk-test");

        let records = captured.0.lock().unwrap();
        assert_eq!(records[0].input_tokens, 10);
        assert_eq!(records[0].output_tokens, 5);
        // Anthropic's cache counters are billed too, at the input rate here.
        assert_eq!(records[0].cost_microdollars, 18);
        assert_eq!(records[0].status.as_str(), "ok");
    }

    /// Embeddings have no completion to bill, so the record carries input only
    /// even when the provider reports something else.
    #[tokio::test]
    async fn embeddings_pass_through_and_bill_input_only() {
        let answer = json!({
            "object": "list",
            "data": [{ "object": "embedding", "index": 0, "embedding": [0.25, -0.5] }],
            "usage": { "prompt_tokens": 8, "total_tokens": 8, "completion_tokens": 4 }
        });
        let (base_url, received) =
            native_upstream("/embeddings", Json(answer.clone()).into_response()).await;
        let captured = CapturingSink::default();
        let state = native_state("openai", &base_url, captured.clone());

        let sent = json!({ "model": "alias", "input": ["one", "two"], "dimensions": 2 });
        let resp = router(state)
            .oneshot(
                authorized("/v1/embeddings")
                    .body(Body::from(serde_json::to_vec(&sent).unwrap()))
                    .unwrap(),
            )
            .await
            .unwrap();

        assert_eq!(resp.status(), StatusCode::OK);
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        assert_eq!(serde_json::from_slice::<Value>(&bytes).unwrap(), answer);

        // Forwarded as sent but for the model — notably without a `stream` field,
        // which the embeddings endpoint does not accept.
        let requests = received.lock().unwrap();
        let (forwarded, _) = &requests[0];
        assert_eq!(
            forwarded,
            &json!({ "model": "upstream-model", "input": ["one", "two"], "dimensions": 2 })
        );

        let records = captured.0.lock().unwrap();
        assert_eq!(records[0].input_tokens, 8);
        assert_eq!(records[0].output_tokens, 0);
        assert_eq!(records[0].cost_microdollars, 8);
    }

    #[tokio::test]
    async fn a_tripped_target_is_skipped_then_recovers_via_a_half_open_probe() {
        let healthy_a = Arc::new(AtomicBool::new(false));
        let (url_a, hits_a) =
            controllable_upstream(healthy_a.clone(), StatusCode::INTERNAL_SERVER_ERROR).await;
        let (url_b, hits_b) =
            controllable_upstream(Arc::new(AtomicBool::new(true)), StatusCode::OK).await;
        let captured = CapturingSink::default();
        // One failure trips the target; a 1s cooldown then allows a probe.
        let failover = "[failover]\nmax_attempts = 3\noverall_timeout_ms = 30000\nfailure_threshold = 1\ncooldown_seconds = 1";
        let state = two_target_state(&url_a, &url_b, failover, captured.clone());

        // Request 1: pa fails (trips its circuit) and pb serves.
        let resp = router(state.clone()).oneshot(chat_request()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 1);

        // Request 2: pa's circuit is open, so it is skipped entirely — pb serves
        // on the first attempt without pa being touched again.
        let resp = router(state.clone()).oneshot(chat_request()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(hits_a.load(Ordering::SeqCst), 1);
        assert_eq!(hits_b.load(Ordering::SeqCst), 2);
        {
            let records = captured.0.lock().unwrap();
            assert_eq!(records[1].target_provider, "pb");
            assert_eq!(records[1].attempts, 1);
        }

        // pa recovers; after the cooldown a single half-open probe reaches it.
        healthy_a.store(true, Ordering::SeqCst);
        tokio::time::sleep(Duration::from_millis(1_100)).await;

        let resp = router(state).oneshot(chat_request()).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(hits_a.load(Ordering::SeqCst), 2);
        let records = captured.0.lock().unwrap();
        assert_eq!(records.len(), 3);
        assert_eq!(records[2].target_provider, "pa");
        assert_eq!(records[2].attempts, 1);
    }
}