cpa-server 0.4.4

Part of cliproxy: HTTP routes, client auth, credential selection and the management API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
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
//! Credential routing policy. Config parsing remains owned by cpa-core; integration
//! publishes this policy through Runtime::publish_policy.

use std::collections::HashMap;
use std::time::{Duration, Instant, SystemTime};

use cpa_common::gostr::GoStr;
use cpa_core::credential::Credential;
use cpa_core::exec::ExecError;
#[cfg(test)]
use cpa_core::exec::FailureScope;
use serde::Deserialize;
use serde_json::Value;

use crate::runtime::{Outcome, Selection};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Strategy {
    #[default]
    RoundRobin,
    FillFirst,
    WeightedRoundRobin,
    /// cliproxy-rs addition (`soonest-reset`, alias `reset-first`) and the default for
    /// an unset strategy: spend the account whose weekly window resets soonest first,
    /// until it cools or a usage window runs out, then the next soonest. Go has no such
    /// strategy and reads it, like an unset one, as round-robin.
    SoonestReset,
}

/// What `soonest-reset` knows about a credential's usage windows, from its latest
/// passive quota observation (Claude `anthropic-ratelimit-unified-*`, Codex
/// `x-codex-*`). The default is "nothing known".
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Windows {
    /// When the long (weekly) window resets, while that is still ahead.
    pub weekly_reset: Option<SystemTime>,
    /// The long window's reset once it has passed: the window rolled over, so what was
    /// learned about it is stale and the credential may be probed again. Only a reset
    /// upstream stated (an absolute time, or a delay above zero) identifies a rollover;
    /// a zero delay would make every answer's observation time a new one.
    pub rolled_over: Option<Rollover>,
    /// A window (Claude 5-hour or 7-day, Codex primary or secondary) is used up and has
    /// not reset yet: upstream would refuse until it does.
    pub exhausted: bool,
}

/// Which window a rollover belongs to. Probe markers are kept per window, so a
/// rollover of one window is never compared with a probe made for another.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WindowId {
    Claude7d,
    Claude5h,
    CodexPrimary,
    CodexSecondary,
}

/// A long window that rolled over.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rollover {
    pub window: WindowId,
    /// The stated reset that passed.
    pub at: SystemTime,
    /// How far apart two rollovers of this window must be to count as different ones:
    /// half its length (five hours when unknown), which absorbs the rounding of
    /// relative resets around a boundary.
    pub gap: Duration,
}

/// How long a used-up window without a usable reset or length is assumed to hold.
const FALLBACK_WINDOW: Duration = Duration::from_secs(5 * 3600);

/// One usage window read from quota signals; each part may be missing.
#[derive(Debug, Clone, Copy)]
struct Window {
    id: WindowId,
    used_up: bool,
    reset: Option<SystemTime>,
    /// `reset` when it can identify a rollover (not synthesized from a zero delay).
    stated_reset: Option<SystemTime>,
    /// The window length, when one was reported and can be represented.
    length: Option<Duration>,
}

impl Window {
    /// Until when a used-up window holds: its reset, else its length (or five hours)
    /// after the observation. `None` when it is not used up.
    fn exhausted_until(&self, observed_at: SystemTime) -> Option<SystemTime> {
        if !self.used_up {
            return None;
        }
        self.reset.or_else(|| {
            self.length
                .and_then(|l| observed_at.checked_add(l))
                .or_else(|| observed_at.checked_add(FALLBACK_WINDOW))
        })
    }
}

/// A window length in minutes, when positive and representable as a deadline from
/// `observed_at`; otherwise the missing-length policy applies.
fn window_length(minutes: f64, observed_at: SystemTime) -> Option<Duration> {
    if !minutes.is_finite() || minutes <= 0.0 {
        return None;
    }
    let length = Duration::try_from_secs_f64(minutes * 60.0).ok()?;
    observed_at.checked_add(length).map(|_| length)
}

/// A reset signal: epoch seconds (fractions allowed) or an RFC 3339 time.
fn reset_time(raw: &str) -> Option<SystemTime> {
    let raw = raw.trim();
    if let Ok(seconds) = raw.parse::<f64>() {
        return (seconds.is_finite() && seconds > 0.0)
            .then(|| Duration::try_from_secs_f64(seconds).ok())
            .flatten()
            .and_then(|d| SystemTime::UNIX_EPOCH.checked_add(d));
    }
    chrono::DateTime::parse_from_rfc3339(raw).ok().map(SystemTime::from)
}

impl Windows {
    /// Reads a snapshot's `signals` (header name to value), observed at `observed_at`,
    /// for `provider` (`claude` or `codex`; anything else knows nothing).
    pub fn observed(
        provider: &str,
        signals: &std::collections::BTreeMap<String, String>,
        observed_at: SystemTime,
        now: SystemTime,
    ) -> Self {
        let get = |name: &str| {
            signals
                .iter()
                .find(|(k, _)| k.eq_ignore_ascii_case(name))
                .map(|(_, v)| v.trim())
        };
        let number = |name: &str| get(name).and_then(|v| v.parse::<f64>().ok()).filter(|n| n.is_finite());
        let codex = provider.trim().eq_ignore_ascii_case("codex");
        let (long, short) = match provider.trim().to_ascii_lowercase().as_str() {
            "claude" => {
                let window = |id: WindowId, label: &str, minutes: f64| Window {
                    id,
                    used_up: get(&format!("anthropic-ratelimit-unified-{label}-status"))
                        .is_some_and(|s| s.eq_ignore_ascii_case("rejected"))
                        || number(&format!("anthropic-ratelimit-unified-{label}-utilization"))
                            .is_some_and(|u| u >= 1.0),
                    reset: get(&format!("anthropic-ratelimit-unified-{label}-reset")).and_then(reset_time),
                    stated_reset: get(&format!("anthropic-ratelimit-unified-{label}-reset")).and_then(reset_time),
                    length: window_length(minutes, observed_at),
                };
                (
                    Some(window(WindowId::Claude7d, "7d", 7.0 * 24.0 * 60.0)),
                    Some(window(WindowId::Claude5h, "5h", 300.0)),
                )
            }
            "codex" => {
                // Usage, reset and length are read independently; a window exists when
                // any of them was reported.
                let window = |id: WindowId, label: &str| {
                    let p = format!("x-codex-{label}-");
                    let used = number(&format!("{p}used-percent"));
                    let absolute = get(&format!("{p}reset-at")).and_then(reset_time);
                    let after = number(&format!("{p}reset-after-seconds")).filter(|s| *s >= 0.0);
                    let relative = after.and_then(|a| observed_at.checked_add(Duration::try_from_secs_f64(a).ok()?));
                    let reset = absolute.or(relative);
                    // A zero delay still ends an exhausted window now, but its time is
                    // just the observation's, so it names no rollover.
                    let stated_reset = absolute.or(relative.filter(|_| after.is_some_and(|a| a > 0.0)));
                    let length = number(&format!("{p}window-minutes")).and_then(|m| window_length(m, observed_at));
                    (used.is_some() || reset.is_some() || length.is_some()).then_some(Window {
                        id,
                        used_up: used.is_some_and(|u| u >= 100.0),
                        reset,
                        stated_reset,
                        length,
                    })
                };
                let day = Duration::from_secs(24 * 3600);
                match (
                    window(WindowId::CodexPrimary, "primary"),
                    window(WindowId::CodexSecondary, "secondary"),
                ) {
                    // The longer window is the weekly one, whichever header carries it;
                    // without lengths, secondary is weekly.
                    (Some(p), Some(s)) if p.length.unwrap_or(Duration::ZERO) > s.length.unwrap_or(Duration::MAX) => {
                        (Some(p), Some(s))
                    }
                    (Some(p), Some(s)) => (Some(s), Some(p)),
                    (Some(p), None) if p.length.is_some_and(|l| l >= day) => (Some(p), None),
                    (None, Some(s)) if s.length.is_none_or(|l| l >= day) => (Some(s), None),
                    (only, None) | (None, only) => (None, only),
                }
            }
            _ => (None, None),
        };
        let windows = [long, short];
        let until = windows
            .iter()
            .flatten()
            .filter_map(|w| w.exhausted_until(observed_at))
            .max();
        // `X-Codex-Limit-Reached` lasts as long as the used-up windows do; when none is
        // used up, until the soonest reset any window reports; with no reset at all,
        // five hours from the observation.
        let flag_until = (codex && get("x-codex-limit-reached").is_some_and(|v| v.eq_ignore_ascii_case("true")))
            .then(|| {
                until
                    .or_else(|| windows.iter().flatten().filter_map(|w| w.reset).min())
                    .or_else(|| observed_at.checked_add(FALLBACK_WINDOW))
            })
            .flatten();
        let long_reset = long.and_then(|w| w.reset);
        Self {
            weekly_reset: long_reset.filter(|r| *r > now),
            rolled_over: long.and_then(|w| {
                let at = w.stated_reset.filter(|r| *r <= now)?;
                Some(Rollover {
                    window: w.id,
                    at,
                    gap: w.length.unwrap_or(FALLBACK_WINDOW) / 2,
                })
            }),
            exhausted: until.max(flag_until).is_some_and(|u| u > now),
        }
    }

    /// `soonest-reset` order. Usable accounts come before used-up ones. Among them, an
    /// account without a known reset that has not been probed since its long window
    /// last rolled over gets one probe request, then known resets go earliest first,
    /// then accounts whose probe taught nothing.
    fn order(&self, probed: bool) -> (bool, u8, Option<SystemTime>) {
        let class = match (self.weekly_reset, probed) {
            (Some(_), _) => 1,
            (None, false) => 0,
            (None, true) => 2,
        };
        (self.exhausted, class, self.weekly_reset)
    }
}

/// A credential's `soonest-reset` probes: present once its probe for an unknown reset
/// was spent, with the last rollover probed for, per window.
type Probes = HashMap<WindowId, SystemTime>;

/// Whether a credential was already probed for what it reports now. A probe holds
/// until its long window rolls over after it: a reset of that same window that passed
/// and is more than the window's gap later than the rollover last probed for it.
fn probed(record: Option<&Probes>, rolled_over: Option<Rollover>) -> bool {
    match (record, rolled_over) {
        (None, _) => false,
        (Some(_), None) => true,
        (Some(markers), Some(r)) => markers
            .get(&r.window)
            .is_some_and(|at| at.checked_add(r.gap).is_none_or(|limit| r.at <= limit)),
    }
}

/// A credential's [`Windows`] for one pick.
pub type Ranks<'a> = dyn Fn(&Credential) -> Windows + 'a;

#[derive(Debug, Clone)]
pub struct Policy {
    pub strategy: Strategy,
    /// routing.retry.request-retry: int, default 0 (additional rounds).
    pub request_retry: usize,
    /// routing.retry.max-retry-credentials: int, default 0 (unlimited per round).
    pub max_retry_credentials: usize,
    /// routing.retry.max-retry-interval: int seconds, default 0 (no positive waits).
    pub max_retry_interval: Duration,
    /// routing.cooldown.disable-cooling: bool, default false.
    pub disable_cooling: bool,
    /// routing.cooldown.transient-error-cooldown-seconds: int, 0 = 60s, negative disables.
    pub transient_error_cooldown_seconds: i64,
    /// routing.session-affinity: bool, default false.
    pub session_affinity: bool,
    /// routing.session-affinity-ttl: duration string, default 1h.
    pub session_affinity_ttl: Duration,
    pub session_affinity_subagents: bool,
    /// routing.cooldown.save-cooldown-status: persist cooldowns as `.cds` files in
    /// `auth-dir` (cooldown_store.rs), never in credential files.
    pub save_cooldown_status: bool,
    /// routing.cooldown.max-trusted-cooldown (cliproxy-rs only): the first bound on a
    /// quota reset an upstream states ([`trusted`]); zero trusts every reset, as Go does.
    pub max_trusted_cooldown: Duration,
    /// routing.force-model-prefix: bool, default false.
    pub force_model_prefix: bool,
    /// Enabled `openai-compatibility` entries in config order with their
    /// `disable-cooling` (Go `providerCoolingOverrideForAuth`). Filled from the config on
    /// every publish ([`compat_cooling`]).
    pub compat_disable_cooling: Vec<(String, Option<bool>)>,
    /// OAuth-only `oauth.request-scoped-errors` channels; API keys must not inherit these.
    pub oauth_request_scoped_errors: HashMap<String, Vec<ErrorRule>>,
}

impl Default for Policy {
    fn default() -> Self {
        Self {
            strategy: Strategy::RoundRobin,
            request_retry: 0,
            max_retry_credentials: 0,
            max_retry_interval: Duration::ZERO,
            disable_cooling: false,
            transient_error_cooldown_seconds: 0,
            session_affinity: false,
            session_affinity_ttl: Duration::from_secs(3600),
            session_affinity_subagents: true,
            save_cooldown_status: false,
            max_trusted_cooldown: DEFAULT_TRUSTED_COOLDOWN,
            force_model_prefix: false,
            compat_disable_cooling: Vec::new(),
            oauth_request_scoped_errors: HashMap::new(),
        }
    }
}

/// Raw config values, independent of the config stream's RoutingConfig type.
#[derive(Default)]
pub struct RawRouting<'a> {
    pub strategy: &'a str,
    pub force_model_prefix: bool,
    pub session_affinity: bool,
    pub session_affinity_ttl: &'a str,
    pub session_affinity_subagents: Option<bool>,
    pub request_retry: i64,
    pub max_retry_credentials: i64,
    pub max_retry_interval: i64,
    pub disable_cooling: bool,
    pub save_cooldown_status: bool,
    pub transient_error_cooldown_seconds: i64,
    pub max_trusted_cooldown: &'a str,
}

// ponytail: OAuth-provider disable-cooling and request-scoped-error maps stay empty until
// the config stream synthesizes provider overrides; per-credential metadata still applies.
impl From<&cpa_core::config::RoutingConfig> for Policy {
    fn from(r: &cpa_core::config::RoutingConfig) -> Self {
        normalize(RawRouting {
            strategy: &r.strategy,
            force_model_prefix: r.force_model_prefix,
            session_affinity: r.session_affinity,
            session_affinity_ttl: &r.session_affinity_ttl,
            session_affinity_subagents: r.session_affinity_subagents,
            request_retry: r.retry.request_retry,
            max_retry_credentials: r.retry.max_retry_credentials,
            max_retry_interval: r.retry.max_retry_interval,
            disable_cooling: r.cooldown.disable_cooling,
            save_cooldown_status: r.cooldown.save_cooldown_status,
            transient_error_cooldown_seconds: r.cooldown.transient_error_cooldown_seconds,
            max_trusted_cooldown: &r.cooldown.max_trusted_cooldown,
        })
    }
}

pub fn normalize(raw: RawRouting<'_>) -> Policy {
    let ttl = cpa_core::config::parse_duration(raw.session_affinity_ttl.trim())
        .filter(|d| *d > 0)
        .map(|n| Duration::from_nanos(n as u64).max(Duration::from_secs(1)))
        .unwrap_or(Duration::from_secs(3600));
    Policy {
        strategy: match raw.strategy.trim().to_ascii_lowercase().as_str() {
            "weighted-round-robin" | "weightedroundrobin" | "wrr" => Strategy::WeightedRoundRobin,
            "fill-first" | "fillfirst" | "ff" => Strategy::FillFirst,
            // An unset strategy is reset first; Go reads it as round-robin.
            "" | "soonest-reset" | "soonestreset" | "reset-first" | "resetfirst" => Strategy::SoonestReset,
            _ => Strategy::RoundRobin,
        },
        force_model_prefix: raw.force_model_prefix,
        session_affinity: raw.session_affinity,
        session_affinity_ttl: ttl,
        session_affinity_subagents: !raw.session_affinity || raw.session_affinity_subagents.unwrap_or(true),
        request_retry: raw.request_retry.max(0) as usize,
        max_retry_credentials: raw.max_retry_credentials.max(0) as usize,
        max_retry_interval: Duration::from_secs(raw.max_retry_interval.max(0) as u64),
        disable_cooling: raw.disable_cooling,
        save_cooldown_status: raw.save_cooldown_status,
        transient_error_cooldown_seconds: raw.transient_error_cooldown_seconds,
        max_trusted_cooldown: trusted_cooldown(raw.max_trusted_cooldown),
        ..Policy::default()
    }
}

const DEFAULT_TRUSTED_COOLDOWN: Duration = Duration::from_secs(3600);

/// `max-trusted-cooldown`: a Go duration (`"90m"`) or a bare number of seconds. Empty
/// means the one-hour default and zero or negative turns the bound off; a positive value
/// is at least Go's 10 s quota floor. An unreadable value (`"1d"`, `false`, `"1.5"`)
/// keeps the default and is logged once per value.
fn trusted_cooldown(raw: &str) -> Duration {
    let raw = raw.trim();
    if raw.is_empty() {
        return DEFAULT_TRUSTED_COOLDOWN;
    }
    let nanos = match raw.parse::<i64>() {
        Ok(seconds) => seconds.saturating_mul(1_000_000_000),
        Err(_) => match cpa_core::config::parse_duration(raw) {
            Some(nanos) => nanos,
            None => {
                static WARNED: std::sync::Mutex<String> = std::sync::Mutex::new(String::new());
                let mut last = WARNED.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
                if *last != raw {
                    raw.clone_into(&mut last);
                    tracing::warn!(
                        "routing.cooldown.max-trusted-cooldown {raw:?} is not a duration (\"90m\") or a \
                         number of seconds; using 1h"
                    );
                }
                return DEFAULT_TRUSTED_COOLDOWN;
            }
        },
    };
    match nanos {
        ..=0 => Duration::ZERO,
        n => Duration::from_nanos(n as u64).max(Duration::from_secs(10)),
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct ErrorRule {
    #[serde(default)]
    pub status: i64,
    #[serde(default)]
    pub r#match: Vec<String>,
    #[serde(default, rename = "match-regexr")]
    pub match_regex: Vec<String>,
    #[serde(default)]
    pub action: String,
}

#[derive(Debug, Clone, Copy)]
pub struct ErrorAction {
    /// A configured request-scoped rule decided this outcome.
    pub matched: bool,
    pub stop: bool,
    pub cooldown: bool,
    pub force_cooldown: bool,
}

fn metadata<'a>(c: &'a Credential, key: &str) -> Option<&'a Value> {
    // Canonical presence wins even for false, zero and null.
    c.metadata
        .get(key)
        .or_else(|| c.metadata.get(key.replace('_', "-").as_str()))
}

/// A credential's priority tier, as selection reads it.
pub(crate) fn priority(c: &Credential) -> i64 {
    integer(c, "priority").unwrap_or(0)
}

fn integer(c: &Credential, key: &str) -> Option<i64> {
    c.attributes
        .get(key)
        .filter(|_| key == "priority")
        .and_then(|s| s.trim().parse().ok())
        .or_else(|| {
            [key.to_owned(), key.replace('_', "-")].iter().find_map(|key| {
                let v = c.metadata.get(key)?;
                v.as_i64()
                    .or_else(|| v.as_str()?.trim().parse().ok())
                    .or_else(|| v.as_f64().map(|n| n as i64))
            })
        })
}

fn boolean(c: &Credential, key: &str) -> Option<bool> {
    let parse = |s: &str| match s.trim() {
        "true" | "True" | "TRUE" | "1" | "t" | "T" => Some(true),
        "false" | "False" | "FALSE" | "0" | "f" | "F" => Some(false),
        _ => None,
    };
    [key.to_owned(), key.replace('_', "-")].iter().find_map(|key| {
        let v = c.metadata.get(key)?;
        v.as_bool()
            .or_else(|| parse(v.as_str()?))
            .or_else(|| v.as_f64().map(|n| n != 0.0))
    })
}

fn oauth(c: &Credential) -> bool {
    c.str("api_key").is_none() && !c.attributes.contains_key("api_key")
}

impl Policy {
    pub fn retry_limit(&self, c: &Credential) -> usize {
        integer(c, "request_retry")
            .filter(|n| *n >= 0)
            .map(|n| n as usize)
            .unwrap_or(self.request_retry)
    }

    /// Go `quotaCooldownDisabledForAuthWithConfig`: the credential's `disable_cooling`
    /// metadata, then its OpenAI-compatibility entry's `disable-cooling`, then global.
    pub(crate) fn cooling_disabled(&self, c: &Credential) -> bool {
        boolean(c, "disable_cooling")
            .or_else(|| self.provider_cooling(c))
            .unwrap_or(self.disable_cooling)
    }

    /// Go `providerCoolingOverrideForAuth` with `resolveOpenAICompatConfig`: the first
    /// enabled entry (config order) named by the credential's `compat_name`,
    /// `provider_key` or provider.
    fn provider_cooling(&self, c: &Credential) -> Option<bool> {
        let provider = c.provider.trim().go_lower();
        let attr = |k: &str| c.attributes.get(k).map(|v| v.trim()).unwrap_or_default();
        let (compat_name, mut provider_key) = (attr("compat_name"), attr("provider_key"));
        if provider.is_empty()
            || (provider_key.is_empty() && compat_name.is_empty() && provider != "openai-compatibility")
        {
            return None;
        }
        if provider_key.is_empty() {
            provider_key = &provider;
        }
        let candidates = [compat_name, provider_key, provider.as_str()];
        self.compat_disable_cooling
            .iter()
            .find(|(name, _)| candidates.iter().any(|c| !c.is_empty() && c.go_eq_fold(name)))
            .and_then(|(_, disable)| *disable)
    }

    pub fn error_action(&self, c: &Credential, error: &ExecError) -> ErrorAction {
        let file_rules: Option<Vec<ErrorRule>> =
            metadata(c, "request_scoped_errors").and_then(|v| serde_json::from_value(v.clone()).ok());
        let rules = file_rules.as_ref().filter(|r| !r.is_empty()).or_else(|| {
            oauth(c)
                .then(|| self.oauth_request_scoped_errors.get(&c.provider.trim().go_lower()))
                .flatten()
        });
        if let Some(rules) = rules {
            let body = crate::classify::rule_text(error);
            let status = i64::from(crate::classify::go_status(error));
            for rule in rules {
                if rule.status <= 0 || rule.status != status {
                    continue;
                }
                let matches = rule.r#match.iter().any(|s| !s.is_empty() && body.contains(s))
                    || rule
                        .match_regex
                        .iter()
                        .any(|s| !s.is_empty() && regex::Regex::new(s).is_ok_and(|re| re.is_match(&body)));
                if !matches {
                    continue;
                }
                let (stop, cooldown) = match rule.action.trim().to_ascii_lowercase().as_str() {
                    "stop" => (true, false),
                    "stop-and-cooldown" => (true, true),
                    "continue" => (false, false),
                    "continue-and-cooldown" => (false, true),
                    _ => continue,
                };
                return ErrorAction {
                    matched: true,
                    stop,
                    cooldown,
                    force_cooldown: cooldown,
                };
            }
        }
        let request = crate::classify::is_request_invalid(error);
        ErrorAction {
            matched: false,
            stop: request,
            cooldown: !crate::classify::skips_cooldown(error),
            force_cooldown: false,
        }
    }
}

/// Go `isCredentialRetryRoundStatus`.
pub fn retry_status(status: u16) -> bool {
    matches!(status, 403 | 408 | 429 | 500 | 502 | 503 | 504)
}

fn weight(c: &Credential) -> i64 {
    let value = if let Some(s) = c.attributes.get("weight").filter(|s| !s.trim().is_empty()) {
        s.trim().parse::<i64>().ok()
    } else if let Some(v) = metadata(c, "weight") {
        v.as_i64().or_else(|| v.as_str()?.trim().parse().ok()).or_else(|| {
            let n = v.as_f64()?;
            (n.fract() == 0.0 && n <= 1_000_000.0).then_some(n as i64)
        })
    } else {
        Some(1)
    };
    value.filter(|n| *n <= 1_000_000).unwrap_or(0).max(0)
}

pub use cpa_core::registry::dynamic::canonical_model;

#[derive(Default)]
struct Rotation {
    last: String,
    weights: HashMap<String, i64>,
    current: HashMap<String, i64>,
}

pub(crate) struct Cooldown {
    pub deadline: Instant,
    pub level: u32,
    pub status: u16,
    /// Quota-style cooldown (429 or Cloudflare): reported as a model cooldown.
    pub quota: bool,
    /// The failure that set it (Go `ModelState.LastError`), for error summaries.
    pub error: String,
    /// When it was set (Go `ModelState.UpdatedAt`), for `save-cooldown-status`.
    pub since: SystemTime,
    /// A model quota inherited from a credential-wide quota (Go reason
    /// `credential_quota` on sibling model states).
    pub credential: bool,
    /// Bounded trust of a stated quota reset (cliproxy-rs only); `level` stays Go's.
    pub trust: Trust,
    /// What the quota is when `quota` is set: the status that set it and its reason as
    /// `records` names it (a Cloudflare challenge can answer 429).
    pub quota_kind: Option<QuotaKind>,
    /// A live quota block (its end and kind) that this non-quota failure answered: Go
    /// keeps `ModelState.Quota` when it records a 401, and a recovery that clears the 401
    /// keeps the block. Not saved: a record saved meanwhile is the failure's alone, and
    /// a restore keeps a live block from the entry it merges with.
    pub quota_under: Option<(Instant, QuotaKind)>,
    /// The dispatch watermark when a failure last recorded this entry (Go
    /// `cooldownRecordedSeq`): a success from a dispatch at or before it predates the
    /// failure and leaves a live cooldown alone. Zero after a restart, as in Go.
    pub recorded: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct QuotaKind {
    pub status: u16,
    pub reason: &'static str,
}

impl Cooldown {
    /// The live quota block this entry is or carries.
    fn quota_block(&self, now: Instant) -> Option<(Instant, QuotaKind)> {
        let block = if self.quota {
            self.quota_kind.map(|k| (self.deadline, k))
        } else {
            self.quota_under
        };
        block.filter(|(end, _)| *end > now || self.trust.stated.is_some_and(|r| r > now))
    }
}

/// Bounded trust of one cooldown (`max-trusted-cooldown`, docs/DIFFERENCES-FROM-GO.md).
/// Costs nothing until a stated reset is cut: then one comparison per 429 and, per
/// recorded result, a probe check on the two keys the result touches.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct Trust {
    /// Exponent of the latest bounded window, which lasted `cap << window`. Kept after
    /// the window ends, so the next cut window doubles; a success clears it.
    pub window: Option<u32>,
    /// The scheduler's generation when this bounded window opened or its probe was
    /// reserved ([`Scheduler::reserve_probe`]). A lease picked before it holds an older
    /// answer, which neither ends the window nor frees the reservation. Cost: four
    /// bytes here and on each lease, one comparison per key a result touches.
    pub generation: u32,
    /// The deadline is the bound's: a concurrent 429 inside it keeps it.
    pub bounded: bool,
    /// A probe the upstream accepted (a stream started) answered this ended bounded
    /// window: it no longer reserves probes, but keeps its generation, so an older
    /// lease's late answer still cannot replace it. Fits in existing padding.
    pub accepted: bool,
    /// The upstream's stated reset behind a bounded deadline (Go's recovery time).
    pub stated: Option<Instant>,
    /// An unanswered probe request holds the ended window until this instant.
    pub probe: Option<Instant>,
}

impl Trust {
    /// The window's probe was accepted ([`Scheduler::accept_probe_picked`]).
    fn accept(&mut self) {
        self.bounded = false;
        self.accepted = true;
        self.probe = None;
    }
}

/// Longest a probe reserves an ended bounded window. A 429 arrives within seconds; a
/// probe still unanswered after this is being served, and its result clears it anyway.
const PROBE_HOLD: Duration = Duration::from_secs(30);
/// Largest trust exponent: `cap << 20` is over a century for any cap of 1 h or more.
const MAX_TRUST_WINDOW: u32 = 20;

impl Cooldown {
    /// When this entry stops blocking picks: its deadline, or an unanswered probe.
    fn until(&self, now: Instant) -> Option<Instant> {
        let live = (self.deadline > now).then_some(self.deadline);
        live.max(self.trust.probe.filter(|p| *p > now))
    }

    /// A bounded window (still armed, or answered by an accepted probe) that opened, or
    /// had its probe reserved, after the lease picked at generation `picked`: that
    /// lease's answer does not answer it.
    fn newer_than(&self, picked: u32) -> bool {
        // Serial-number order, so the wrapping counter compares right while a lease is
        // fewer than 2^31 generations old.
        (self.trust.bounded || self.trust.accepted) && (self.trust.generation.wrapping_sub(picked) as i32) > 0
    }
}

#[derive(Default)]
pub(crate) struct Scheduler {
    rotations: HashMap<(String, String), Rotation>,
    /// Mixed-provider round-robin cursors (Go `mixedCursors`).
    cursors: HashMap<(String, String), usize>,
    pub(crate) cooldowns: HashMap<(String, String), Cooldown>,
    /// Session affinity bindings (Go `SessionAffinitySelector.cache`).
    affinity: crate::affinity::Cache,
    /// Bindings of requests without an explicit session (Go
    /// `SessionAffinitySelector.matcher`).
    lcp: crate::lcp::Matcher,
    /// `soonest-reset` probes by credential, reserved when the probe request is picked
    /// (under the scheduler lock, so concurrent picks never probe twice). See [`probed`].
    probes: HashMap<String, Probes>,
    /// The live policy turned session affinity off: results of leases picked while it
    /// was on no longer bind anything (Go's replacement selector has no affinity).
    affinity_off: bool,
    /// Bounded windows opened plus probes reserved; each lease carries the value at its
    /// pick ([`Trust::generation`]).
    generation: u32,
    /// Upstream dispatches so far (Go `Manager.executionSeq`): each dispatch takes the
    /// next value ([`Self::dispatch`]); a recorded failure stamps the current one.
    dispatched: u64,
    /// Credentials whose refresh after an upstream 401 failed with `invalid_grant`
    /// while the refused tokens were still current, as they were then (Go
    /// `hasUnauthorizedAuthFailure` from `refreshAuthForRequestAtEpoch`). Neither
    /// selected nor refreshed until their tokens change ([`Self::tokens_rejected`]).
    rejected: HashMap<String, std::sync::Arc<Credential>>,
}

/// Go `cfg.OpenAICompatibility` for cooling: enabled entries with a base URL (Go
/// `SanitizeOpenAICompatibility`), in config order, with their `disable-cooling`.
pub fn compat_cooling(cfg: &cpa_core::config::Config) -> Vec<(String, Option<bool>)> {
    cfg.document
        .get("api-keys")
        .and_then(|k| k.get("openai-compatibility"))
        .and_then(serde_yaml_ng::Value::as_sequence)
        .into_iter()
        .flatten()
        .filter(|g| {
            g.get("base-url")
                .and_then(serde_yaml_ng::Value::as_str)
                .is_some_and(|b| !b.trim().is_empty())
                && g.get("disabled").and_then(serde_yaml_ng::Value::as_bool) != Some(true)
        })
        .map(|g| {
            (
                g.get("name")
                    .and_then(serde_yaml_ng::Value::as_str)
                    .unwrap_or_default()
                    .trim()
                    .to_owned(),
                g.get("disable-cooling").and_then(serde_yaml_ng::Value::as_bool),
            )
        })
        .collect()
}

/// A selection's affinity keys: the session, and its parent or alias when distinct.
struct SessionKeys {
    primary: crate::affinity::Key,
    fallback: Option<crate::affinity::Key>,
    /// The raw parent/alias session, for Go `isSubagentSession`.
    parent: String,
}

fn session_keys(selection: &Selection) -> Option<SessionKeys> {
    let primary = selection.session.as_deref().filter(|s| !s.is_empty())?;
    let scope = selection.provider_keys().join(",");
    let model = canonical_model(&selection.model).to_owned();
    let parent = selection.session_parent.clone().unwrap_or_default();
    let fallback = (!parent.is_empty() && parent != primary).then(|| (scope.clone(), model.clone(), parent.clone()));
    Some(SessionKeys {
        primary: (scope, model, primary.to_owned()),
        fallback,
        parent,
    })
}

/// Go `nextQuotaCooldown`: 1s doubling to 30m.
fn quota_backoff(level: u32) -> (Duration, u32) {
    let seconds = (1u64 << level.min(11)).min(1800);
    if seconds >= 1800 {
        (Duration::from_secs(1800), level)
    } else {
        (Duration::from_secs(seconds), level + 1)
    }
}

/// Bounded trust for a stated quota reset (docs/DIFFERENCES-FROM-GO.md): a reset longer
/// than the window's bound is cut to it, so the next ordinary request after the bound
/// probes the account and an early provider reset is noticed within the bound. The
/// first window is `cap`; each cut window after one that ended without a success is
/// twice the last (`window` is the last exponent). Returns the bound and its exponent,
/// or `None` when the stated reset is trusted as it is (it fits, or `cap` is zero).
fn bound(stated: Duration, cap: Duration, window: Option<u32>) -> Option<(Duration, u32)> {
    if cap.is_zero() {
        return None;
    }
    let step = window.map_or(0, |w| w.saturating_add(1)).min(MAX_TRUST_WINDOW);
    let bound = cap.saturating_mul(1 << step);
    (stated > bound).then_some((bound, step))
}

/// Go `recoverableFailureRetryAfterWithHint`.
fn transient(policy: &Policy, hint: Option<Duration>, disabled: bool) -> Option<Duration> {
    if disabled || policy.transient_error_cooldown_seconds < 0 {
        return None;
    }
    if let Some(hint) = hint.filter(|d| !d.is_zero()) {
        return Some(hint);
    }
    Some(Duration::from_secs(match policy.transient_error_cooldown_seconds {
        0 => 60,
        s => s as u64,
    }))
}

impl Scheduler {
    pub fn configure(&mut self, previous: &Policy, next: &Policy) {
        self.affinity_off = !next.session_affinity;
        if previous.strategy != next.strategy
            || previous.session_affinity != next.session_affinity
            || previous.session_affinity_ttl != next.session_affinity_ttl
            || previous.session_affinity_subagents != next.session_affinity_subagents
        {
            self.rotations.clear();
            self.cursors.clear();
            self.affinity.clear();
            // Go builds a new selector, and with it a new matcher on the new TTL. Unlike
            // Go, access generations keep counting (docs/DIFFERENCES-FROM-GO.md).
            self.lcp.reset(crate::lcp::Limits {
                ttl: next.session_affinity_ttl,
                ..Default::default()
            });
            self.probes.clear();
        }
    }

    /// Session affinity under `policy`, unless the live policy has since turned it off.
    fn affinity(&self, policy: &Policy) -> bool {
        policy.session_affinity && !self.affinity_off
    }

    pub fn quota_cooling(&self, c: &Credential, model: &str, now: Instant) -> bool {
        let model = canonical_model(model);
        [model, ""].into_iter().any(|m| {
            self.cooldowns
                .get(&(c.id.clone(), m.to_owned()))
                .is_some_and(|s| s.quota && s.until(now).is_some())
        })
    }

    pub fn wait(&self, c: &Credential, model: &str, now: Instant) -> Option<Duration> {
        let model = canonical_model(model);
        [model, ""]
            .into_iter()
            .filter_map(|model| {
                self.cooldowns
                    .get(&(c.id.clone(), model.to_owned()))
                    .and_then(|s| s.until(now)?.checked_duration_since(now))
                    .filter(|d| !d.is_zero())
            })
            .max()
    }

    /// Whether a cooldown deadline (not a probe reservation) is ahead for `model`.
    pub fn cooling(&self, c: &Credential, model: &str, now: Instant) -> bool {
        let model = canonical_model(model);
        [model, ""].into_iter().any(|m| {
            self.cooldowns
                .get(&(c.id.clone(), m.to_owned()))
                .is_some_and(|s| s.deadline > now)
        })
    }

    /// The last failure recorded for this credential and model, if any.
    pub fn last_error(&self, c: &Credential, model: &str) -> Option<&Cooldown> {
        let model = canonical_model(model);
        self.cooldowns
            .get(&(c.id.clone(), model.to_owned()))
            .or_else(|| self.cooldowns.get(&(c.id.clone(), String::new())))
    }

    pub fn retry_eligible(&self, c: &Credential, model: &str, now: Instant) -> bool {
        let model = canonical_model(model);
        // A bounded window or an unanswered probe is not waited for: the probe answers
        // for everyone, and holding requests to the window's end would release them
        // together as a burst of probes.
        [model, ""].into_iter().all(|model| {
            self.cooldowns.get(&(c.id.clone(), model.to_owned())).is_none_or(|s| {
                let probing = s.trust.probe.is_some_and(|p| p > now);
                !probing && (s.deadline <= now || (retry_status(s.status) && !s.trust.bounded))
            })
        })
    }

    /// [`Self::accept_probe_picked`] for a lease picked just now.
    #[cfg(test)]
    pub fn accept_probe(&mut self, c: &Credential, model: &str, now: Instant) {
        self.accept_probe_picked(c, model, self.generation, now);
    }

    /// The upstream accepted the attempt that probes `model` (a stream's first chunk),
    /// picked at generation `picked`: the ended bounded window has its answer, so other
    /// picks may go through while the stream runs. Only expired bounded entries that
    /// are not newer than the lease change; their count stays until the stream's own
    /// outcome is recorded. When the credential-wide window ended, the model keys that
    /// share its deadline (its copies) are disarmed too; a model's own window is not.
    /// Cost: two lookups, and a scan of the cooldown table only when the credential-wide
    /// window ended.
    pub fn accept_probe_picked(&mut self, c: &Credential, model: &str, picked: u32, now: Instant) {
        let model = canonical_model(model);
        let mut credential_window = None;
        for m in [model, ""] {
            if let Some(s) = self.cooldowns.get_mut(&(c.id.clone(), m.to_owned()))
                && s.trust.bounded
                && s.deadline <= now
                && !s.newer_than(picked)
            {
                s.trust.accept();
                if m.is_empty() {
                    credential_window = Some(s.deadline);
                }
            }
        }
        if let Some(ended) = credential_window {
            for ((id, m), s) in &mut self.cooldowns {
                if *id == c.id && !m.is_empty() && s.trust.bounded && s.deadline == ended && !s.newer_than(picked) {
                    s.trust.accept();
                }
            }
        }
    }

    /// The generation of a lease picked outside selection (the Antigravity credits
    /// fallback, which runs on credentials selection refused): one that every bounded
    /// window and probe reservation within 2^30 generations of now, before or after, is
    /// newer than. Such a lease answers none of them: a cancelled or successful credits
    /// attempt neither frees another request's probe nor ends a window, which stays with
    /// its own probe.
    pub fn outside_selection_generation(&self) -> u32 {
        self.generation.wrapping_sub(1 << 30)
    }

    /// Reserves the probe of `c` for `model` when its pick ends a bounded window: until
    /// the probe's result is recorded (or [`PROBE_HOLD`] passes), the window's keys keep
    /// cooling, so concurrent requests do not each go upstream. Returns the generation
    /// the pick's lease carries; a reservation takes a new one, so only its own lease's
    /// result frees it. Cost per pick: two lookups.
    pub fn reserve_probe(&mut self, c: &Credential, model: &str, now: Instant) -> u32 {
        let model = canonical_model(model);
        let next = self.generation.wrapping_add(1);
        for m in [model, ""] {
            if let Some(s) = self.cooldowns.get_mut(&(c.id.clone(), m.to_owned()))
                && s.trust.bounded
                && s.deadline <= now
            {
                s.trust.probe = now.checked_add(PROBE_HOLD);
                s.trust.generation = next;
                self.generation = next;
            }
        }
        self.generation
    }

    /// Picks among ready candidates, each paired with its provider key (Go
    /// `SessionAffinitySelector.Pick` over the configured selector).
    ///
    /// An established binding outranks credential priority; a session's parent (or
    /// prompt-cache conversation alias) binding is inherited by forks and, when
    /// `session-affinity-subagents` allows, by subagents.
    #[cfg(test)]
    pub fn pick<'a>(
        &mut self,
        candidates: &[(&'a Credential, &str)],
        selection: &Selection,
        policy: &Policy,
        now: Instant,
    ) -> Option<&'a Credential> {
        self.pick_ranked(candidates, selection, policy, &|_| Windows::default(), now)
    }

    /// [`Self::pick`] with each credential's usage windows, for `soonest-reset`.
    #[cfg(test)]
    pub fn pick_ranked<'a>(
        &mut self,
        candidates: &[(&'a Credential, &str)],
        selection: &Selection,
        policy: &Policy,
        ranks: &Ranks<'_>,
        now: Instant,
    ) -> Option<&'a Credential> {
        self.pick_session(candidates, selection, policy, ranks, now)
            .map(|(c, _)| c)
    }

    /// [`Self::pick_ranked`], with the LCP binding of a request without an explicit
    /// session (Go `pickLCP`): its session identity, lineage and access generation.
    pub fn pick_session<'a>(
        &mut self,
        candidates: &[(&'a Credential, &str)],
        selection: &Selection,
        policy: &Policy,
        ranks: &Ranks<'_>,
        now: Instant,
    ) -> Option<(&'a Credential, Option<crate::lcp::Match>)> {
        if candidates.is_empty() {
            return None;
        }
        if let Some(request) = selection.lcp.as_deref().filter(|_| self.affinity(policy)) {
            let namespace = request.namespace(&selection.model);
            // A known trajectory stays on its credential while that one is available.
            if let Some(found) = self.lcp.find(&namespace, request.prepared(), now)
                && let Some((c, _)) = candidates.iter().find(|(c, _)| c.id == found.auth)
            {
                return Some((c, Some(found)));
            }
            let picked = self.pick_unbound(candidates, selection, policy.strategy, ranks)?;
            let bound = self.lcp.bind(&namespace, request.prepared(), &picked.id, now);
            return Some((picked, bound));
        }
        self.pick_legacy(candidates, selection, policy, ranks, now)
            .map(|c| (c, None))
    }

    /// Go `SessionAffinitySelector.Pick` after the LCP step.
    fn pick_legacy<'a>(
        &mut self,
        candidates: &[(&'a Credential, &str)],
        selection: &Selection,
        policy: &Policy,
        ranks: &Ranks<'_>,
        now: Instant,
    ) -> Option<&'a Credential> {
        let ttl = policy.session_affinity_ttl;
        let Some(keys) = self.affinity(policy).then(|| session_keys(selection)).flatten() else {
            return self.pick_unbound(candidates, selection, policy.strategy, ranks);
        };
        self.affinity.sweep(now, ttl);
        let fork = selection.session_fork;
        let subagent = !fork && cpa_common::session::is_subagent_session(&keys.primary.2, &keys.parent);
        let bind_keys = match &keys.fallback {
            Some(fallback) if !subagent && !fork => vec![keys.primary.clone(), fallback.clone()],
            _ => vec![keys.primary.clone()],
        };
        let find = |id: &str| candidates.iter().find(|(c, _)| c.id == id).map(|(c, _)| *c);
        let reuse = match self.affinity.get_and_refresh(&keys.primary, now, ttl) {
            // A bound credential that is no longer available is replaced, not inherited.
            Some(id) => find(&id),
            None => keys
                .fallback
                .as_ref()
                .and_then(|fallback| self.affinity.get(fallback, now))
                .filter(|_| !subagent || policy.session_affinity_subagents)
                .and_then(|id| find(&id)),
        };
        let picked = match reuse {
            Some(c) => c,
            None => self.pick_unbound(candidates, selection, policy.strategy, ranks)?,
        };
        self.affinity.bind(&picked.id, &bind_keys, now, ttl);
        Some(picked)
    }

    /// Go `SessionAffinitySelector.OnResult`: success refreshes the session's bindings to
    /// this credential, a credential-attributed failure releases them. Request-scoped
    /// and transport failures leave them alone.
    ///
    /// A request without an explicit session refreshes its LCP sequence on success; a
    /// credential failure removes that exact sequence unless a newer request refreshed
    /// it after `lcp`'s generation. Such a request never touches the session cache.
    pub fn session_result(
        &mut self,
        c: &Credential,
        selection: &Selection,
        outcome: &Outcome,
        policy: &Policy,
        lcp: Option<&crate::lcp::Match>,
        now: Instant,
    ) {
        if !self.affinity(policy) {
            return;
        }
        let success = match outcome {
            Outcome::Success => true,
            Outcome::Failure(error) if policy.error_action(c, error).cooldown => false,
            _ => return,
        };
        if let Some(request) = selection.lcp.as_deref() {
            let namespace = request.namespace(&selection.model);
            if success {
                self.lcp.touch(&namespace, request.prepared(), &c.id, now);
            } else {
                let generation = lcp.map_or(0, |m| m.access);
                self.lcp
                    .remove(&namespace, &request.prepared().fingerprints, &c.id, generation, now);
            }
            if lcp.is_some() {
                return;
            }
        }
        let Some(keys) = session_keys(selection) else {
            return;
        };
        let mut targets = vec![keys.primary.clone()];
        if let Some(fallback) = keys.fallback
            && !cpa_common::session::is_subagent_session(&keys.primary.2, &keys.parent)
        {
            targets.push(fallback);
        }
        for key in &targets {
            if success {
                self.affinity.touch(key, &c.id, now, policy.session_affinity_ttl);
            } else {
                self.affinity.compare_and_delete(key, &c.id);
            }
        }
    }

    /// Go's built-in scheduler with `strategy` (`pickSingleWithStrategy` and
    /// `pickMixedWithStrategy`), without session affinity: what a plugin scheduler's
    /// `delegate_builtin` answer runs.
    /// ponytail: Go keeps the built-in scheduler's cursors apart from the configured
    /// selector's; here they share the rotation state. Separate the maps if a plugin
    /// scheduler mixing delegation and its own picks needs Go's exact rotation.
    pub fn pick_builtin<'a>(
        &mut self,
        candidates: &[(&'a Credential, &str)],
        selection: &Selection,
        strategy: Strategy,
        ranks: &Ranks<'_>,
    ) -> Option<&'a Credential> {
        self.pick_unbound(candidates, selection, strategy, ranks)
    }

    fn pick_unbound<'a>(
        &mut self,
        candidates: &[(&'a Credential, &str)],
        selection: &Selection,
        strategy: Strategy,
        ranks: &Ranks<'_>,
    ) -> Option<&'a Credential> {
        let model = canonical_model(&selection.model).to_owned();
        let provider_keys = selection.provider_keys();
        let providers_key = provider_keys.join(",");
        let tier = candidates
            .iter()
            .map(|(c, _)| integer(c, "priority").unwrap_or(0))
            .max()?;
        let mut ready: Vec<(&Credential, &str)> = candidates
            .iter()
            .copied()
            .filter(|(c, _)| integer(c, "priority").unwrap_or(0) == tier)
            .collect();
        ready.sort_by(|a, b| a.0.id.cmp(&b.0.id));
        // Providers that have ready candidates, in the request's provider order.
        let mut groups: Vec<(&str, Vec<&Credential>)> = Vec::new();
        for provider in provider_keys
            .iter()
            .map(String::as_str)
            .chain(ready.iter().map(|(_, p)| *p))
        {
            if groups.iter().any(|(p, _)| *p == provider) {
                continue;
            }
            let members: Vec<&Credential> = ready.iter().filter(|(_, p)| *p == provider).map(|(c, _)| *c).collect();
            if !members.is_empty() {
                groups.push((provider, members));
            }
        }
        if self.rotations.len() >= 4096 {
            self.rotations.clear();
            self.cursors.clear();
        }
        let picked = if groups.len() == 1 {
            let (provider, members) = &groups[0];
            self.pick_within(provider, &model, members, strategy, ranks)
        } else {
            match strategy {
                Strategy::FillFirst => groups[0].1[0],
                // Like fill-first: the first provider group, then its soonest reset.
                Strategy::SoonestReset => {
                    let (provider, members) = &groups[0];
                    self.pick_within(provider, &model, members, Strategy::SoonestReset, ranks)
                }
                Strategy::WeightedRoundRobin => {
                    let all: Vec<&Credential> = ready.iter().map(|(c, _)| *c).collect();
                    self.pick_within(&providers_key, &model, &all, Strategy::WeightedRoundRobin, ranks)
                }
                Strategy::RoundRobin => {
                    // Go `pickMixed`: a cursor over provider segments sized by their ready
                    // counts, then that provider's own round-robin.
                    let total: usize = groups.iter().map(|(_, m)| m.len()).sum();
                    let cursor = self.cursors.entry((providers_key.clone(), model.clone())).or_default();
                    let slot = *cursor % total;
                    *cursor = slot + 1;
                    let mut start = 0;
                    let mut index = 0;
                    for (i, (_, members)) in groups.iter().enumerate() {
                        if slot < start + members.len() {
                            index = i;
                            break;
                        }
                        start += members.len();
                    }
                    let (provider, members) = &groups[index];
                    self.pick_within(provider, &model, members, Strategy::RoundRobin, ranks)
                }
            }
        };
        Some(picked)
    }

    fn pick_within<'a>(
        &mut self,
        scope: &str,
        model: &str,
        members: &[&'a Credential],
        strategy: Strategy,
        ranks: &Ranks<'_>,
    ) -> &'a Credential {
        let state = self.rotations.entry((scope.to_owned(), model.to_owned())).or_default();
        match strategy {
            Strategy::FillFirst => members[0],
            Strategy::SoonestReset => {
                let probes = &mut self.probes;
                let ranked: Vec<_> = members
                    .iter()
                    .map(|c| {
                        let windows = ranks(c);
                        let order = windows.order(probed(probes.get(&c.id), windows.rolled_over));
                        (order, windows.rolled_over, *c)
                    })
                    .collect();
                let best = ranked.iter().map(|(order, ..)| *order).min().expect("members");
                // Equal ranks take turns.
                let tied: Vec<_> = ranked.iter().filter(|(o, ..)| *o == best).collect();
                let (order, rolled_over, picked) = *tied
                    .iter()
                    .find(|(.., c)| c.id > state.last)
                    .copied()
                    .unwrap_or(tied[0]);
                if order.1 == 0 {
                    // Its one probe, spent whatever the answer: the next pick ranks it
                    // by what the answer reports, if anything.
                    let markers = probes.entry(picked.id.clone()).or_default();
                    if let Some(r) = rolled_over {
                        markers.insert(r.window, r.at);
                    }
                }
                state.last.clone_from(&picked.id);
                picked
            }
            Strategy::RoundRobin => {
                let picked = members
                    .iter()
                    .find(|c| c.id > state.last)
                    .copied()
                    .unwrap_or(members[0]);
                state.last.clone_from(&picked.id);
                picked
            }
            Strategy::WeightedRoundRobin => {
                if members
                    .iter()
                    .any(|c| state.weights.get(&c.id).is_some_and(|w| *w != weight(c)))
                {
                    state.current.clear();
                }
                if state.weights.len() > 1024 || state.current.len() > 1024 {
                    state.weights.retain(|id, _| members.iter().any(|c| c.id == *id));
                    state.current.retain(|id, _| members.iter().any(|c| c.id == *id));
                }
                let mut total = 0i64;
                let mut best = i64::MIN;
                let mut picked = members[0];
                for c in members {
                    let w = weight(c);
                    state.weights.insert(c.id.clone(), w);
                    let current = state.current.entry(c.id.clone()).or_default();
                    *current = current.saturating_add(w);
                    total = total.saturating_add(w);
                    if *current > best {
                        best = *current;
                        picked = c;
                    }
                }
                let current = state.current.get_mut(&picked.id).unwrap();
                *current = current.saturating_sub(total);
                picked
            }
        }
    }

    pub fn admits(&self, c: &Credential, policy: &Policy) -> bool {
        policy.strategy != Strategy::WeightedRoundRobin || weight(c) > 0
    }

    /// Records that neither of `c`'s tokens can recover without a new sign-in.
    pub(crate) fn reject_tokens(&mut self, c: std::sync::Arc<Credential>) {
        self.rejected.insert(c.id.clone(), c);
    }

    /// Whether `c` still holds the tokens [`Self::reject_tokens`] recorded (Go
    /// `updateInternalLocked` lifts the block once `CredentialsChanged`).
    pub(crate) fn tokens_rejected(&self, c: &Credential) -> bool {
        self.rejected
            .get(&c.id)
            .is_some_and(|r| !crate::runtime::credentials_changed(r, c))
    }

    /// [`Self::record_picked`] for an attempt picked just now.
    #[cfg(test)]
    pub fn record(&mut self, c: &Credential, model: &str, outcome: &Outcome, policy: &Policy, now: Instant) {
        self.record_picked(c, model, outcome, policy, self.generation, now);
    }

    /// [`Self::record_dispatched`] for a result without a dispatch stamp.
    #[cfg(test)]
    pub fn record_picked(
        &mut self,
        c: &Credential,
        model: &str,
        outcome: &Outcome,
        policy: &Policy,
        picked: u32,
        now: Instant,
    ) {
        self.record_dispatched(c, model, outcome, policy, picked, 0, now);
    }

    /// Stamps one upstream dispatch (Go `nextExecutionSeq`).
    pub fn dispatch(&mut self) -> u64 {
        self.dispatched += 1;
        self.dispatched
    }

    /// Applies one attempt's outcome (Go `MarkResult`). `picked` is the generation its
    /// lease was picked at ([`Self::reserve_probe`]): a bounded window or probe
    /// reservation newer than that is not answered by it. `seq` is its dispatch stamp
    /// ([`Self::dispatch`], 0 when unknown): a success leaves alone a live cooldown that
    /// a failure recorded after the dispatch (Go 35af74f9).
    #[allow(clippy::too_many_arguments)]
    pub fn record_dispatched(
        &mut self,
        c: &Credential,
        model: &str,
        outcome: &Outcome,
        policy: &Policy,
        picked: u32,
        seq: u64,
        now: Instant,
    ) {
        let model = canonical_model(model);
        self.apply(c, model, outcome, policy, picked, seq, now);
        // Go stamps the failing state with the dispatch watermark; a credential-wide 429
        // also stamps the credential and every sibling state. A credential-level failure
        // stamps the credential even when it skips the cooldown (Go's auth-level branch
        // has no skip).
        if let Outcome::Failure(error) = outcome {
            let action = policy.error_action(c, error);
            if action.cooldown || model.is_empty() {
                let disabled = policy.cooling_disabled(c) && !action.force_cooldown;
                let all = crate::classify::credential_scoped(error) && !disabled;
                for ((id, m), s) in &mut self.cooldowns {
                    if *id == c.id && (all || m == model) {
                        s.recorded = self.dispatched;
                    }
                }
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn apply(
        &mut self,
        c: &Credential,
        model: &str,
        outcome: &Outcome,
        policy: &Policy,
        picked: u32,
        seq: u64,
        now: Instant,
    ) {
        let key = (c.id.clone(), model.to_owned());
        // Any answer ends a probe reservation on the keys it touches, except one taken
        // after its lease was picked (a later probe's, once this one's hold lapsed).
        for m in [model, ""] {
            if let Some(s) = self.cooldowns.get_mut(&(c.id.clone(), m.to_owned()))
                && !s.newer_than(picked)
            {
                s.trust.probe = None;
            }
        }
        let error = match outcome {
            Outcome::Success => {
                // Active credential quota survives success on an in-flight sibling.
                let global = (c.id.clone(), String::new());
                if self.cooldowns.get(&global).is_some_and(|s| s.quota && s.deadline > now) {
                    return;
                }
                // A live cooldown recorded after this attempt was dispatched: the success
                // predates the failure and proves no recovery (Go `retainModelCooldown`,
                // `retainAuthCooldown`).
                if seq > 0
                    && self
                        .cooldowns
                        .get(&key)
                        .is_some_and(|s| s.deadline > now && s.recorded >= seq)
                {
                    return;
                }
                // A request that started before a bounded window opened leaves it for the
                // window's own probe.
                if let Some(ended) = self
                    .cooldowns
                    .get(&global)
                    .filter(|s| s.deadline <= now && !s.newer_than(picked))
                    .map(|s| s.deadline)
                {
                    self.cooldowns.remove(&global);
                    // The credential recovered: model keys that a credential-wide window
                    // escalated forget it too (their Go state is left as Go keeps it). A
                    // model's own ended bounded window still waits for its probe, and one
                    // a newer probe reserved or answered keeps its state.
                    for ((id, _), s) in &mut self.cooldowns {
                        if *id == c.id
                            && s.deadline <= now
                            && (!s.trust.bounded || s.deadline == ended)
                            && !s.newer_than(picked)
                        {
                            s.trust = Trust::default();
                        }
                    }
                }
                if !self.cooldowns.get(&key).is_some_and(|s| s.newer_than(picked)) {
                    self.cooldowns.remove(&key);
                }
                return;
            }
            Outcome::Cancelled | Outcome::Neutral(_) => return,
            Outcome::Failure(error) => error,
        };
        let action = policy.error_action(c, error);
        if !action.cooldown {
            return;
        }
        let credential_quota = crate::classify::credential_scoped(error);
        let key = if credential_quota {
            (c.id.clone(), String::new())
        } else {
            key
        };
        // A forced cooldown applies the normal policy even when cooling is disabled.
        let disabled = policy.cooling_disabled(c) && !action.force_cooldown;
        let status = crate::classify::go_status(error);
        let text = crate::classify::error_text(error);
        let prev = self.cooldowns.get(&key);
        // An ended bounded window that a newer probe took over, or that opened after this
        // attempt was picked, waits for its own probe: this older answer neither renews
        // nor replaces it. Inside a live window the handling below applies.
        if prev.is_some_and(|s| s.deadline <= now && s.newer_than(picked)) {
            return;
        }
        let prev_deadline = prev.map(|s| s.deadline);
        let prev_live = prev.filter(|s| s.deadline > now).map(|s| s.deadline);
        let prev_trust = prev.map(|s| s.trust).unwrap_or_default();
        // A live quota block this failure answers, if it is not a quota failure itself.
        let quota_under = prev.and_then(|p| p.quota_block(now));
        let mut level = prev.filter(|s| s.quota).map(|s| s.level).unwrap_or(0);
        // Causes other than a cut reset keep the trust count: nothing recovered.
        let mut trust = Trust {
            window: prev_trust.window,
            ..Trust::default()
        };
        let hint = error.retry_after.filter(|d| !d.is_zero());
        let mut quota = false;
        let mut cloudflare = false;
        let duration = if crate::classify::is_model_support(status, &text) {
            (!disabled).then(|| hint.unwrap_or(Duration::from_secs(43200)))
        } else if crate::classify::is_cloudflare(status, &text) {
            quota = true;
            cloudflare = true;
            (!disabled).then(|| {
                let (d, next) = quota_backoff(level);
                level = next;
                d.max(Duration::from_secs(10))
            })
        } else if crate::classify::is_invalid_grant(status, &text) {
            (!disabled).then_some(Duration::from_secs(1800))
        } else {
            match status {
                401..=403 => (!disabled).then_some(Duration::from_secs(1800)),
                404 => (!disabled).then(|| hint.unwrap_or(Duration::from_secs(43200))),
                429 => {
                    quota = true;
                    if credential_quota && prev.is_none_or(|s| !s.quota) {
                        level = 0;
                    }
                    if disabled {
                        None
                    } else if let Some(hint) = error.retry_after {
                        // A present hint, even zero, gets the 10s floor (Go
                        // minQuotaCooldownFloor); only an absent one backs off.
                        let stated = hint.max(Duration::from_secs(10));
                        let cap = policy.max_trusted_cooldown;
                        if !cap.is_zero()
                            && let Some(window) = prev.filter(|s| s.trust.bounded && s.deadline > now)
                        {
                            // A concurrent answer inside a live bounded window: the probe
                            // deadline and the count stand, but an earlier stated reset
                            // moves the probe up to it.
                            let reset = now.checked_add(stated);
                            let old = window.deadline;
                            let deadline = reset.map_or(old, |r| old.min(r));
                            let mut window_trust = window.trust;
                            if let Some(slot) = self.cooldowns.get_mut(&key) {
                                slot.deadline = deadline;
                                slot.error = text;
                                slot.trust.stated = reset.or(slot.trust.stated);
                                window_trust = slot.trust;
                            }
                            if credential_quota {
                                // Model keys that inherited this credential window (same
                                // deadline, bounded) follow it when it moves up; longer
                                // independent cooldowns stand.
                                for ((id, m), state) in &mut self.cooldowns {
                                    if *id == c.id && !m.is_empty() && state.trust.bounded && state.deadline == old {
                                        state.deadline = deadline;
                                        state.trust.stated = window_trust.stated;
                                        state.trust.window = state.trust.window.max(window_trust.window);
                                    }
                                }
                                self.extend_siblings(c, deadline, None, now);
                            }
                            return;
                        }
                        Some(match bound(stated, cap, prev_trust.window) {
                            Some((bound, window)) => {
                                self.generation = self.generation.wrapping_add(1);
                                trust = Trust {
                                    window: Some(window),
                                    generation: self.generation,
                                    bounded: true,
                                    accepted: false,
                                    stated: now.checked_add(stated),
                                    probe: None,
                                };
                                bound
                            }
                            None => stated,
                        })
                    } else if let Some(prev) = prev.filter(|s| s.quota && s.deadline > now) {
                        // Go `quotaCooldownAfterFailure`: an active quota deadline is
                        // reused, not extended.
                        let deadline = prev.deadline;
                        if let Some(slot) = self.cooldowns.get_mut(&key) {
                            slot.error = text;
                        }
                        if credential_quota {
                            self.extend_siblings(c, deadline, None, now);
                        }
                        return;
                    } else {
                        let (d, next) = quota_backoff(level);
                        level = next;
                        Some(d)
                    }
                }
                408 | 500 | 502..=504 | 520..=526 => transient(policy, hint, disabled),
                _ => transient(policy, None, disabled),
            }
        };
        // Go falls back to the one-minute transient cooldown when a forced cooldown's
        // policy produced none (for example transient cooldowns disabled).
        let duration = match duration {
            None if action.force_cooldown => Some(Duration::from_secs(60)),
            d => d,
        };
        let Some(next) = duration.and_then(|d| now.checked_add(d)) else {
            self.cooldowns.remove(&key);
            return;
        };
        let deadline = prev_live.map_or(next, |p| p.max(next));
        // An answer from a request already in flight (a 503, a 401) inside a live bounded
        // quota window that outlasts it: the window stands as it is, quota and all, so
        // its saved record keeps the stated reset and count. Only bounded windows; Go's
        // own cooldowns merge as Go does.
        if prev_live == Some(deadline)
            && let Some(slot) = self.cooldowns.get_mut(&key).filter(|s| s.quota && s.trust.bounded)
        {
            slot.error = text;
            // Go's level still counts a Cloudflare challenge (unchanged for other causes).
            slot.level = level;
            return;
        }
        // Provenance follows the deadline: a longer earlier cooldown that stands keeps
        // its own bounded flag and stated reset.
        let provenance = |deadline: Instant| {
            if deadline == next {
                trust
            } else if prev_live == Some(deadline) {
                Trust {
                    probe: None,
                    ..prev_trust
                }
            } else {
                Trust {
                    window: trust.window,
                    ..Trust::default()
                }
            }
        };
        let kind = |reason| {
            quota.then_some(QuotaKind {
                status: error.status,
                reason,
            })
        };
        let own_reason = if cloudflare { "cloudflare challenge" } else { "quota" };
        let state = |deadline, reason| Cooldown {
            deadline,
            level,
            status: error.status,
            quota,
            error: text.clone(),
            since: SystemTime::now(),
            credential: false,
            trust: provenance(deadline),
            quota_kind: kind(reason),
            quota_under: quota_under.filter(|_| !quota),
            recorded: 0,
        };
        let reason = if credential_quota {
            "credential_quota"
        } else {
            own_reason
        };
        self.cooldowns.insert(key, state(deadline, reason));
        if credential_quota {
            self.extend_siblings(c, deadline, prev_deadline, now);
            // Go also records the failing model's own quota state (reason `quota`).
            if !model.is_empty() {
                let own = (c.id.clone(), model.to_owned());
                // `extend_siblings` above already took a live own key to the credential's
                // deadline, unless it is longer, the model's own bounded window, or the
                // credential's window is bounded: then the own deadline stands.
                let own_deadline = self
                    .cooldowns
                    .get(&own)
                    .filter(|s| s.deadline > now)
                    .map_or(deadline, |s| s.deadline);
                let own_trust = self.cooldowns.get(&own).map(|s| s.trust).unwrap_or_default();
                let own_window = own_trust.window;
                let mut own_state = state(own_deadline, own_reason);
                if own_deadline != deadline {
                    // The model's own window (longer, or bounded) wins: its provenance
                    // goes with it.
                    own_state.trust = Trust {
                        probe: None,
                        ..own_trust
                    };
                } else {
                    own_state.trust = provenance(deadline);
                }
                own_state.trust.window = own_state.trust.window.max(own_window);
                self.cooldowns.insert(own, own_state);
            }
        }
    }

    /// Go's credential-scoped 429: live sibling model states become quota cooldowns
    /// (`credential_quota`) lasting at least as long as the credential. Under a bounded
    /// credential window, and for a model's own bounded window under any, a model's own
    /// deadline is left as it is: the credential's key holds the model until the
    /// credential's probe anyway, and a copied deadline would follow the credential's
    /// when an earlier reset moves it up, before the model's own deadline (Go never
    /// shortens one). Only copies take the credential's deadline: the copies of the
    /// window this one `renews` (the credential's previous deadline) move onto it even
    /// when they ended, so no copy outlives the account's recovery.
    fn extend_siblings(&mut self, c: &Credential, deadline: Instant, renews: Option<Instant>, now: Instant) {
        let (level, trust, status) = self
            .cooldowns
            .get(&(c.id.clone(), String::new()))
            .map_or((0, Trust::default(), 429), |s| (s.level, s.trust, s.status));
        for ((id, model), state) in &mut self.cooldowns {
            if *id != c.id || model.is_empty() {
                continue;
            }
            let copy = state.trust.bounded && (state.deadline == deadline || Some(state.deadline) == renews);
            if state.deadline > now || copy {
                let window = state.trust.window.max(trust.window);
                if copy || !(state.trust.bounded || trust.bounded) {
                    if deadline >= state.deadline {
                        state.trust = Trust { probe: None, ..trust };
                    }
                    state.deadline = state.deadline.max(deadline);
                }
                // A model further along its own escalation keeps its count.
                state.trust.window = window;
                state.quota = true;
                state.credential = true;
                state.level = level;
                state.quota_kind = Some(QuotaKind {
                    status,
                    reason: "credential_quota",
                });
            }
        }
    }

    /// Clears every cooldown of one credential (Go `ResetQuota`), returning the models
    /// that were cooling.
    pub fn reset(&mut self, id: &str) -> Vec<String> {
        let mut models: Vec<String> = self
            .cooldowns
            .keys()
            .filter(|(cid, _)| cid == id)
            .map(|(_, m)| m.clone())
            .collect();
        self.cooldowns.retain(|(cid, _), _| cid != id);
        self.affinity.invalidate(id);
        self.lcp.invalidate(id);
        self.probes.remove(id);
        models.sort();
        models
    }

    /// Go `cooldownStateRecordsForAuthLocked`: one record per live cooldown of `c`, by
    /// model (the credential-wide quota is the model-less record).
    pub fn records(&self, c: &Credential, now: Instant, wall: SystemTime) -> Vec<crate::cooldown_store::Record> {
        use crate::cooldown_store::{LastError, Quota, Record};
        let mut out: Vec<Record> = self
            .cooldowns
            .iter()
            .filter(|((id, _), state)| *id == c.id && state.deadline > now)
            .map(|((_, model), state)| {
                let at = wall + (state.deadline - now);
                // Go's recovery time is the stated reset; the probe time is the retry.
                let recover = state
                    .trust
                    .stated
                    .filter(|r| state.trust.bounded && *r > state.deadline)
                    .map_or(at, |r| wall + (r - now));
                let reason = match (model.is_empty() || state.credential, state.quota, state.status) {
                    (true, true, _) => "credential_quota".to_owned(),
                    (false, true, 429) => "quota".to_owned(),
                    (false, true, _) => "cloudflare challenge".to_owned(),
                    (_, false, _) => state.error.clone(),
                };
                Record {
                    provider: c.provider.trim().to_owned(),
                    auth_id: c.id.clone(),
                    model: model.clone(),
                    status: "cooling".into(),
                    next_retry_after: Some(at),
                    quota: if state.quota {
                        Quota {
                            exceeded: true,
                            reason: reason.clone(),
                            next_recover_at: Some(recover),
                            backoff_level: state.level,
                            observed_at: None,
                            trust_windows: Some(state.trust.window.map_or(0, |w| w.saturating_add(1))),
                        }
                    } else {
                        Quota::default()
                    },
                    reason,
                    last_error: Some(LastError {
                        message: state.error.clone(),
                        http_status: u32::from(state.status),
                        ..LastError::default()
                    }),
                    updated_at: Some(state.since),
                    auth_file: match &c.source {
                        cpa_core::credential::Source::File(path) => Some(path.clone()),
                        _ => None,
                    },
                }
            })
            .collect();
        out.sort_by(|a, b| a.model.cmp(&b.model));
        out
    }

    /// Go `restoreCooldownRecordLocked` for one live record of `c`. A model-less record
    /// restores the credential-wide quota; other model-less records aggregate the model
    /// records (Go recomputes them) and apply only when `c` has no model records.
    pub fn restore(
        &mut self,
        c: &Credential,
        record: &crate::cooldown_store::Record,
        has_model_records: bool,
        cap: Duration,
        now: Instant,
        wall: SystemTime,
    ) -> bool {
        // Go blocks until the later of the retry deadline and, for a quota state, the
        // quota recovery time (`availabilityBlock`); one deadline here.
        // Go restores only records whose retry deadline is still ahead.
        let Some(retry) = record.next_retry_after.filter(|at| *at > wall) else {
            return false;
        };
        let recover = record.quota.next_recover_at.filter(|_| record.quota.exceeded);
        let saved = record.quota.trust_windows.map(|n| n.min(MAX_TRUST_WINDOW + 1));
        let model = record.model.trim();
        if model.is_empty() && has_model_records && record.quota.reason != "credential_quota" {
            return false;
        }
        let since = |at: SystemTime| at.duration_since(wall).unwrap_or_default();
        let (remaining, trust) = match (record.quota.exceeded && !cap.is_zero(), saved) {
            // Written by cliproxy-rs: the saved retry is the deadline, as it is, whatever
            // set it (a bounded window, a trusted reset, or a longer cooldown from another
            // cause that a credential-wide quota marked), so a restart never cuts or
            // escalates it. A stated reset after the retry marks a bounded window.
            (true, Some(spent)) => {
                let stated = recover.filter(|r| *r > retry).map(|r| now + since(r));
                let trust = Trust {
                    window: spent.checked_sub(1),
                    generation: 0,
                    bounded: stated.is_some(),
                    accepted: false,
                    stated,
                    probe: None,
                };
                (since(retry), trust)
            }
            // A Go record, or one from before the bound: Go's deadline gets the first
            // bound. Go's `backoff_level` keeps its meaning and does not raise it.
            (true, None) => {
                let at = recover.map_or(retry, |r| retry.max(r));
                match bound(since(at), cap, None) {
                    Some((bound, window)) => (
                        bound,
                        Trust {
                            window: Some(window),
                            generation: 0,
                            bounded: true,
                            accepted: false,
                            stated: Some(now + since(at)),
                            probe: None,
                        },
                    ),
                    None => (since(at), Trust::default()),
                }
            }
            (false, _) => (since(recover.map_or(retry, |r| retry.max(r))), Trust::default()),
        };
        if remaining.is_zero() {
            return false;
        }
        let deadline = now + remaining;
        let error = record.last_error.clone().unwrap_or_default();
        let status = match error.http_status {
            0 if record.quota.exceeded => 429,
            s => u16::try_from(s).unwrap_or(0),
        };
        let key = (c.id.clone(), canonical_model(model).to_owned());
        let recorded = self.cooldowns.get(&key).map_or(0, |prev| prev.recorded);
        let prev = self.cooldowns.get(&key).filter(|prev| prev.deadline > now);
        let prev_wins = prev.is_some_and(|p| p.deadline > deadline);
        let deadline = prev.map_or(deadline, |prev| prev.deadline.max(deadline));
        let mut quota = (
            record.quota.exceeded,
            status,
            !model.is_empty() && record.quota.reason == "credential_quota",
        );
        let reason = match record.quota.reason.as_str() {
            "credential_quota" => "credential_quota",
            "cloudflare challenge" => "cloudflare challenge",
            _ => "quota",
        };
        let mut quota_kind = record.quota.exceeded.then_some(QuotaKind { status, reason });
        // A live quota block under the entry this merges with stays: a record saved while
        // a 401 answered a quota is the 401's alone.
        let quota_under = prev.and_then(|p| p.quota_block(now));
        let mut trust = trust;
        // With bounded trust on either side, provenance and the quota classification come
        // from the entry that supplies the deadline, and the counts merge. Without it the
        // merge is Go's.
        if let Some(prev) = prev.filter(|p| p.trust != Trust::default() || trust != Trust::default()) {
            let window = prev.trust.window.max(trust.window);
            if prev_wins {
                trust = Trust {
                    probe: None,
                    ..prev.trust
                };
                quota = (prev.quota, prev.status, prev.credential);
                quota_kind = prev.quota_kind;
            }
            trust.window = window;
        }
        // A bounded window this restore installs is newer than every lease in flight (a
        // restore also runs while serving, when `save-cooldown-status` turns on or its
        // directory moves); a live window it keeps keeps its generation.
        if trust.bounded && !prev_wins {
            self.generation = self.generation.wrapping_add(1);
            trust.generation = self.generation;
        }
        let (quota, status, credential) = quota;
        self.cooldowns.insert(
            key,
            Cooldown {
                deadline,
                level: record.quota.backoff_level,
                status,
                quota,
                error: if error.message.is_empty() {
                    record.reason.clone()
                } else {
                    error.message
                },
                since: record.updated_at.unwrap_or(wall),
                credential,
                trust,
                quota_kind: quota_kind.filter(|_| quota),
                quota_under: quota_under.filter(|_| !quota),
                // Go `mergeModelState` keeps the live state's watermark.
                recorded,
            },
        );
        true
    }

    /// Go `clearDisabledCooldownStates`: drops cooldowns of credentials that are disabled
    /// or whose cooling the policy disables. Returns whether anything was cleared.
    pub fn clear_disabled(&mut self, credentials: &[std::sync::Arc<Credential>], policy: &Policy) -> bool {
        let before = self.cooldowns.len();
        self.cooldowns.retain(|(id, _), _| {
            credentials
                .iter()
                .find(|c| c.id == *id)
                .is_none_or(|c| !c.disabled && !policy.cooling_disabled(c))
        });
        self.cooldowns.len() != before
    }

    pub fn reconcile(&mut self, credentials: &[std::sync::Arc<Credential>]) {
        self.cooldowns
            .retain(|(id, _), _| credentials.iter().any(|c| c.id == *id));
        self.affinity.retain(|id| credentials.iter().any(|c| c.id == id));
        self.lcp.retain(|id| credentials.iter().any(|c| c.id == id));
        self.probes.retain(|id, _| credentials.iter().any(|c| c.id == *id));
        self.rejected.retain(|id, _| credentials.iter().any(|c| c.id == *id));
    }
}

/// Time left until a bounded cooldown's stated reset, while it is ahead.
fn recover_after(s: &Cooldown, now: Instant) -> Option<Duration> {
    let stated = s
        .trust
        .stated
        .filter(|r| s.trust.bounded && *r > now && *r > s.deadline)?;
    Some(stated - now)
}

/// One active cooldown for management views (additive read API). `model` is empty for
/// a credential-wide cooldown.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CooldownState {
    pub model: String,
    /// Until the next attempt (zero once a bounded window ended and the next request
    /// probes the account).
    pub remaining: Duration,
    /// Until the upstream's stated reset, when the bound cut it (cliproxy-rs only).
    pub recover_in: Option<Duration>,
    pub level: u32,
    pub status: u16,
    pub quota: bool,
}

impl Scheduler {
    /// Unexpired cooldowns of one credential, model keys sorted, credential-wide first.
    pub(crate) fn cooldowns_of(&self, id: &str, now: Instant) -> Vec<CooldownState> {
        let mut out: Vec<CooldownState> = self
            .cooldowns
            .iter()
            .filter(|((cid, _), s)| cid == id && (s.deadline > now || recover_after(s, now).is_some()))
            .map(|((_, model), s)| CooldownState {
                model: model.clone(),
                remaining: s.deadline.saturating_duration_since(now),
                recover_in: recover_after(s, now),
                level: s.level,
                status: s.status,
                quota: s.quota,
            })
            .collect();
        out.sort_by(|a, b| a.model.cmp(&b.model));
        out
    }

    /// Clears every cooldown of one credential; returns the model keys that had one.
    pub(crate) fn reset_cooldowns(&mut self, id: &str) -> Vec<String> {
        let mut models: Vec<String> = self
            .cooldowns
            .keys()
            .filter(|(cid, model)| cid == id && !model.is_empty())
            .map(|(_, model)| model.clone())
            .collect();
        models.sort();
        self.cooldowns.retain(|(cid, _), _| cid != id);
        // A reset credential is probed again under `soonest-reset`; affinity is kept.
        self.probes.remove(id);
        models
    }

    /// The unauthorized part of Go `BindCodexSIWC`: the 401 cooldowns of one
    /// credential are dropped, except that a quota block still live under one stays, as
    /// a quota cooldown again with its quota reason (Go clears only `LastError` and keeps
    /// the retry time). Returns whether anything changed.
    ///
    /// Model keys match like `clearUnauthorizedModelStates` (a 401, or "unauthorized" in
    /// the status message, which is the error here); the credential-wide key like
    /// `isUnauthorizedError`. Go also matches the error code `unauthorized`, which
    /// cooldowns do not keep.
    pub(crate) fn clear_unauthorized(&mut self, id: &str, now: Instant) -> bool {
        self.rejected.remove(id);
        let mut changed = false;
        self.cooldowns.retain(|(cid, model), c| {
            let error = c.error.to_ascii_lowercase();
            let unauthorized = c.status == 401
                || error.contains("status 401")
                || error.contains("401 unauthorized")
                || (!model.is_empty() && error.contains("unauthorized"));
            if cid != id || !unauthorized {
                return true;
            }
            changed = true;
            let Some((end, kind)) = c.quota_block(now) else {
                return false;
            };
            c.quota = true;
            c.quota_kind = Some(kind);
            c.quota_under = None;
            c.deadline = c.deadline.max(end);
            c.status = kind.status;
            // Go clears `LastError` and puts the quota reason in a status message that
            // names the failure.
            if error.contains("unauthorized") {
                c.error = kind.reason.into();
            }
            true
        });
        changed
    }
}

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

    /// The quota a recovery keeps under a 401 survives a restore while serving (a saved
    /// record is the 401's alone) and keeps its own kind: a Cloudflare challenge that
    /// answered 429 stays one, and a credential-wide quota that reached a model's 401
    /// keeps the quota's status. Error text that does not name the 401 stays.
    #[test]
    fn a_quota_under_a_401_keeps_its_kind_through_restore_and_recovery() {
        let (c, d) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let p = Policy {
            max_trusted_cooldown: Duration::ZERO,
            ..Policy::default()
        };
        let (start, wall) = (Instant::now(), SystemTime::now());
        let unauthorized = |text: &str| Outcome::Failure(ExecError::local(401, FailureScope::Credential, text));
        let mut challenge = ExecError::local(429, FailureScope::Model, "<title>Just a moment...</title> cloudflare");
        challenge.retry_after = Some(secs(2 * H));
        let mut s = Scheduler::default();
        s.record(&c, "restored", &hinted(FailureScope::Model, secs(2 * H)), &p, start);
        s.record(&c, "cloudflare", &Outcome::Failure(challenge), &p, start);
        s.record(&c, "plain", &hinted(FailureScope::Model, secs(2 * H)), &p, start);
        s.record(&d, "sibling", &unauthorized("unauthorized"), &p, start);
        // Inside the challenge's 10s cooldown.
        let answered = start + secs(2);
        for model in ["restored", "cloudflare"] {
            s.record(&c, model, &unauthorized("unauthorized"), &p, answered);
        }
        s.record(&c, "plain", &unauthorized("status 401"), &p, answered);
        s.record(
            &d,
            "other",
            &hinted(FailureScope::Credential, secs(2 * H)),
            &p,
            answered,
        );
        assert!(
            entry(&s, &d, "sibling").quota,
            "the credential-wide quota reached the 401"
        );
        let restored = s
            .records(&c, answered, wall)
            .into_iter()
            .find(|r| r.model == "restored")
            .unwrap();
        assert!(!restored.quota.exceeded, "the saved record is the 401's");
        assert!(s.restore(&c, &restored, true, Duration::ZERO, answered, wall));
        let cases = [
            (&c, "restored", "quota"),
            (&c, "cloudflare", "cloudflare challenge"),
            (&c, "plain", "status 401"),
            (&d, "sibling", "credential_quota"),
        ];
        let blocked: Vec<_> = cases.iter().map(|(x, m, _)| s.wait(x, m, answered)).collect();
        assert!(s.clear_unauthorized("a", answered));
        assert!(s.clear_unauthorized("b", answered));
        for (x, model, error) in cases {
            let e = entry(&s, x, model);
            assert_eq!((e.quota, e.status, e.error.as_str()), (true, 429, error), "{model}");
        }
        let after: Vec<_> = cases.iter().map(|(x, m, _)| s.wait(x, m, answered)).collect();
        assert_eq!(after, blocked, "each stays blocked until the same time");
        assert!(after.iter().all(Option::is_some));
    }

    /// Which cooldowns the SIWC recovery clears: model keys match a 401 or
    /// "unauthorized" in the error (Go's status-message check), the credential-wide key
    /// only Go's `isUnauthorizedError`; other causes and other credentials stay.
    #[test]
    fn clear_unauthorized_matches_go_per_key_kind() {
        let now = Instant::now();
        let cooldown = |status: u16, error: &str| Cooldown {
            deadline: now + Duration::from_secs(600),
            level: 0,
            status,
            quota: false,
            error: error.into(),
            since: SystemTime::now(),
            credential: false,
            trust: Trust::default(),
            quota_kind: None,
            quota_under: None,
            recorded: 0,
        };
        let mut s = Scheduler::default();
        for (id, model, c) in [
            ("a", "401", cooldown(401, "upstream said no")),
            ("a", "403-text", cooldown(403, "unauthorized operation")),
            ("a", "403", cooldown(403, "forbidden")),
            ("a", "", cooldown(403, "unauthorized operation")),
            ("b", "401", cooldown(401, "upstream said no")),
        ] {
            s.cooldowns.insert((id.into(), model.into()), c);
        }
        assert!(s.clear_unauthorized("a", now));
        let mut left: Vec<_> = s.cooldowns.keys().map(|(id, m)| (id.as_str(), m.as_str())).collect();
        left.sort();
        assert_eq!(left, [("a", ""), ("a", "403"), ("b", "401")]);
        for (status, error) in [(401, "x"), (403, "Status 401: revoked"), (403, "HTTP 401 Unauthorized")] {
            s.cooldowns.insert(("a".into(), String::new()), cooldown(status, error));
            assert!(s.clear_unauthorized("a", now), "{status} {error}");
            assert!(
                !s.cooldowns.contains_key(&("a".into(), String::new())),
                "{status} {error}"
            );
        }
        assert!(!s.clear_unauthorized("a", now), "nothing left to clear");
    }

    /// Go keeps `ModelState.Quota` when a 401 lands on a model under a live quota, and
    /// the SIWC recovery keeps that block (with its quota reason) while it lasts: the
    /// model is not freed early. A quota that ended before the recovery goes with the 401.
    #[test]
    fn a_401_over_a_live_quota_keeps_the_quota_through_recovery() {
        let c = cred("a", serde_json::json!({}));
        let start = Instant::now();
        let unauthorized = Outcome::Failure(ExecError::local(401, FailureScope::Credential, "unauthorized"));
        // Go's own cooldowns (the 401 replaces the entry) and a bounded window (the
        // window stands and takes the 401's error).
        for (cap, replaced) in [(Duration::ZERO, 401), (Policy::default().max_trusted_cooldown, 429)] {
            let p = Policy {
                max_trusted_cooldown: cap,
                ..Policy::default()
            };
            let mut s = Scheduler::default();
            s.record(&c, "long", &hinted(FailureScope::Model, secs(2 * H)), &p, start);
            s.record(&c, "short", &hinted(FailureScope::Model, secs(MIN)), &p, start);
            let answered = start + secs(MIN / 2);
            s.record(&c, "long", &unauthorized, &p, answered);
            s.record(&c, "short", &unauthorized, &p, answered);
            assert_eq!(entry(&s, &c, "long").status, replaced, "{cap:?}");
            let blocked = s.wait(&c, "long", answered);
            let bind = start + secs(5 * MIN);
            assert!(s.clear_unauthorized("a", bind), "{cap:?}");
            let long = entry(&s, &c, "long");
            assert_eq!(
                (long.quota, long.status, long.error.as_str()),
                (true, 429, "quota"),
                "{cap:?}"
            );
            assert_eq!(
                s.wait(&c, "long", bind),
                blocked.map(|b| b - (bind - answered)),
                "{cap:?}: still blocked until the same time"
            );
            assert!(s.wait(&c, "long", bind).is_some_and(|w| w > secs(30 * MIN)), "{cap:?}");
            assert_eq!(s.wait(&c, "short", bind), None, "{cap:?}: its quota had ended");
        }
    }

    fn tag<'a>(creds: &[&'a Credential]) -> Vec<(&'a Credential, &'static str)> {
        creds.iter().map(|c| (*c, "claude")).collect()
    }

    fn cred(id: &str, extra: Value) -> Credential {
        let mut metadata = extra.as_object().unwrap().clone();
        metadata.insert("type".into(), "claude".into());
        Credential::from_file(Path::new("/mock"), &Path::new("/mock").join(id), metadata).unwrap()
    }

    fn selection(model: &str) -> Selection {
        Selection {
            provider: "claude".into(),
            model: model.into(),
            ..Default::default()
        }
    }

    #[test]
    fn normalization_matches_go_runtime_not_example_config() {
        for (raw, expected) in [
            (" WRR ", Strategy::WeightedRoundRobin),
            ("weightedroundrobin", Strategy::WeightedRoundRobin),
            ("weighted-round-robin", Strategy::WeightedRoundRobin),
            (" FF", Strategy::FillFirst),
            ("fillfirst", Strategy::FillFirst),
            ("fill-first", Strategy::FillFirst),
            ("soonest-reset", Strategy::SoonestReset),
            (" Reset-First ", Strategy::SoonestReset),
            ("resetfirst", Strategy::SoonestReset),
            ("", Strategy::SoonestReset),
            (" ", Strategy::SoonestReset),
            ("round-robin", Strategy::RoundRobin),
            ("anything", Strategy::RoundRobin),
        ] {
            assert_eq!(
                normalize(RawRouting {
                    strategy: raw,
                    ..Default::default()
                })
                .strategy,
                expected
            );
        }
        for (ttl, seconds) in [
            ("", 3600),
            ("bad", 3600),
            ("-1h", 3600),
            ("0", 3600),
            ("0.5s", 1),
            ("0.0000000001s", 3600),
            ("1h30m", 5400),
            (" 2m ", 120),
        ] {
            assert_eq!(
                normalize(RawRouting {
                    session_affinity_ttl: ttl,
                    ..Default::default()
                })
                .session_affinity_ttl,
                Duration::from_secs(seconds),
                "{ttl}"
            );
        }
        let policy = normalize(RawRouting {
            request_retry: -1,
            max_retry_credentials: -5,
            max_retry_interval: -3,
            session_affinity_subagents: Some(false),
            ..Default::default()
        });
        assert_eq!(policy.request_retry, 0);
        assert_eq!(policy.max_retry_credentials, 0);
        assert_eq!(policy.max_retry_interval, Duration::ZERO);
        assert!(policy.session_affinity_subagents, "ignored when affinity disabled");
        assert!(
            !normalize(RawRouting {
                session_affinity: true,
                session_affinity_subagents: Some(false),
                ..Default::default()
            })
            .session_affinity_subagents
        );
        assert!(
            normalize(RawRouting {
                session_affinity: true,
                ..Default::default()
            })
            .session_affinity_subagents
        );
    }

    #[test]
    fn go_duration_decimal_compound_units_and_overflow() {
        for (raw, expected) in [
            ("1h2m3.4s", 3_723_400_000_000),
            (".5s", 500_000_000),
            ("+1.000000001s", 1_000_000_001),
            ("1.s", 1_000_000_000),
            ("1µs2μs3us", 6000),
            ("0.3333333333333333333h", 1_200_000_000_000),
            ("0.100000000000000000000h", 360_000_000_000),
            ("-9223372036854775808ns", i64::MIN),
            ("9223372036854775807ns", i64::MAX),
            ("0.9ns", 0),
        ] {
            assert_eq!(cpa_core::config::parse_duration(raw), Some(expected), "{raw}");
        }
        for raw in [
            "1d",
            ".s",
            "1",
            "1h 2m",
            "1s-2s",
            "1e3s",
            "9223372036854775808ns",
            "-9223372036854775809ns",
        ] {
            assert_eq!(cpa_core::config::parse_duration(raw), None, "{raw}");
        }
    }

    #[test]
    fn rotation_tracks_previous_identity_and_provider_model_keys() {
        let a = cred("a", serde_json::json!({}));
        let b = cred("b", serde_json::json!({}));
        let c = cred("c", serde_json::json!({}));
        let p = Policy::default();
        let now = Instant::now();
        let mut s = Scheduler::default();
        assert_eq!(s.pick(&tag(&[&c, &b, &a]), &selection("m"), &p, now).unwrap().id, "a");
        assert_eq!(s.pick(&tag(&[&a, &c]), &selection("m"), &p, now).unwrap().id, "c");
        assert_eq!(s.pick(&tag(&[&a, &b, &c]), &selection("m"), &p, now).unwrap().id, "a");
        assert_eq!(s.pick(&tag(&[&a, &b]), &selection("other"), &p, now).unwrap().id, "a");
        assert_eq!(s.pick(&tag(&[&a, &b]), &selection("m(high)"), &p, now).unwrap().id, "b");
    }

    #[test]
    fn smooth_weights_keep_signed_credits_across_temporary_exclusions() {
        let a = cred("a", serde_json::json!({"weight":5}));
        let b = cred("b", serde_json::json!({"weight":1}));
        let c = cred("c", serde_json::json!({"weight":1}));
        let p = Policy {
            strategy: Strategy::WeightedRoundRobin,
            ..Default::default()
        };
        let now = Instant::now();
        let mut s = Scheduler::default();
        let picks: Vec<_> = (0..7)
            .map(|_| {
                s.pick(&tag(&[&a, &b, &c]), &selection("m"), &p, now)
                    .unwrap()
                    .id
                    .clone()
            })
            .collect();
        assert_eq!(picks, ["a", "a", "b", "a", "c", "a", "a"]);
        let mut s = Scheduler::default();
        assert_eq!(s.pick(&tag(&[&a, &b, &c]), &selection("m"), &p, now).unwrap().id, "a");
        assert_eq!(s.pick(&tag(&[&b, &c]), &selection("m"), &p, now).unwrap().id, "b");
        assert_eq!(s.pick(&tag(&[&a, &b, &c]), &selection("m"), &p, now).unwrap().id, "a");
        assert_eq!(s.pick(&tag(&[&a, &b, &c]), &selection("m"), &p, now).unwrap().id, "c");
        for v in [
            serde_json::json!(0),
            serde_json::json!(-3),
            serde_json::json!(1_000_001),
            serde_json::json!(1.5),
            serde_json::json!("bad"),
        ] {
            assert!(!s.admits(&cred("off", serde_json::json!({"weight":v})), &p));
        }
        assert_eq!(weight(&cred("max", serde_json::json!({"weight":1_000_000}))), 1_000_000);
        assert_eq!(weight(&cred("float", serde_json::json!({"weight":2.0}))), 2);
    }

    #[test]
    fn affinity_beats_recovered_priority_until_unusable_or_expired() {
        let low = cred("a", serde_json::json!({"priority":-1}));
        let high = cred("b", serde_json::json!({"priority":9}));
        let p = Policy {
            strategy: Strategy::FillFirst,
            session_affinity: true,
            session_affinity_ttl: Duration::from_secs(10),
            ..Default::default()
        };
        let sel = Selection {
            session: Some("session".into()),
            ..selection("m")
        };
        let now = Instant::now();
        let mut s = Scheduler::default();
        assert_eq!(s.pick(&tag(&[&low]), &sel, &p, now).unwrap().id, "a");
        assert_eq!(
            s.pick(&tag(&[&low, &high]), &sel, &p, now + Duration::from_secs(5))
                .unwrap()
                .id,
            "a"
        );
        assert_eq!(
            s.pick(&tag(&[&low, &high]), &sel, &p, now + Duration::from_secs(15))
                .unwrap()
                .id,
            "b"
        );
        assert_eq!(
            s.pick(&tag(&[&low]), &sel, &p, now + Duration::from_secs(16))
                .unwrap()
                .id,
            "a"
        );
        assert_eq!(s.pick(&tag(&[&low, &high]), &selection("m"), &p, now).unwrap().id, "b");
    }

    #[test]
    fn max_trusted_cooldown_reads_durations_seconds_and_off() {
        let read = |raw| {
            normalize(RawRouting {
                max_trusted_cooldown: raw,
                ..Default::default()
            })
            .max_trusted_cooldown
        };
        assert_eq!(read(""), Duration::from_secs(3600));
        assert_eq!(read("90m"), Duration::from_secs(5400));
        assert_eq!(read("120"), Duration::from_secs(120));
        assert_eq!(read("0"), Duration::ZERO);
        assert_eq!(read("0s"), Duration::ZERO);
        assert_eq!(read("-5"), Duration::ZERO);
        for unreadable in ["soon", "1d", "false", "1.5"] {
            assert_eq!(read(unreadable), Duration::from_secs(3600), "{unreadable}");
        }
        assert_eq!(read("3"), Duration::from_secs(10), "clamped to Go's floor");
        assert_eq!(read("2s"), Duration::from_secs(10), "clamped to Go's floor");
    }

    const H: u64 = 3600;
    const MIN: u64 = 60;

    fn secs(n: u64) -> Duration {
        Duration::from_secs(n)
    }

    /// A 429 whose stated reset is `stated` from `at`.
    fn hinted(scope: FailureScope, stated: Duration) -> Outcome {
        let mut e = ExecError::local(429, scope, "usage_limit_reached");
        e.retry_after = Some(stated);
        Outcome::Failure(e)
    }

    fn unhinted(scope: FailureScope) -> Outcome {
        Outcome::Failure(ExecError::local(429, scope, "rate limited"))
    }

    fn entry<'a>(s: &'a Scheduler, c: &Credential, model: &str) -> &'a Cooldown {
        &s.cooldowns[&(c.id.clone(), model.to_owned())]
    }

    /// A stated reset six days away is trusted for one hour, then for doubling windows
    /// while the account keeps answering 429, and never past the stated reset. Success
    /// clears the escalation; `0` trusts the stated reset as Go does.
    #[test]
    fn quota_resets_are_trusted_up_to_a_doubling_bound() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let reset = start + secs(6 * 24 * H);
        for (scope, key) in [(FailureScope::Model, "m"), (FailureScope::Credential, "")] {
            let mut s = Scheduler::default();
            let mut now = start;
            for window in [1, 2, 4, 8, 16, 32, 64] {
                s.record(&c, "m", &hinted(scope, reset - now), &p, now);
                assert_eq!(s.wait(&c, "m", now), Some(secs(window * H)), "{scope:?}");
                assert_eq!(entry(&s, &c, key).trust.stated, Some(reset), "{scope:?}: stated kept");
                assert_eq!(entry(&s, &c, key).level, 0, "{scope:?}: Go's level untouched");
                now += secs(window * H);
                assert!(s.retry_eligible(&c, "m", now));
            }
            // 127 hours have passed; the next window would be 128 hours, past the reset.
            s.record(&c, "m", &hinted(scope, reset - now), &p, now);
            assert_eq!(
                s.wait(&c, "m", now),
                Some(reset - now),
                "{scope:?}: capped by the stated reset"
            );
            // The account came back early: success clears the escalation.
            let mut s = Scheduler::default();
            s.record(&c, "m", &hinted(scope, reset - start), &p, start);
            let then = start + secs(H);
            s.record(&c, "m", &hinted(scope, reset - then), &p, then);
            assert_eq!(s.wait(&c, "m", then), Some(secs(2 * H)));
            let later = start + secs(3 * H);
            s.record(&c, "m", &Outcome::Success, &p, later);
            s.record(&c, "m", &hinted(scope, reset - later), &p, later);
            assert_eq!(
                s.wait(&c, "m", later),
                Some(secs(H)),
                "{scope:?}: back to the first bound"
            );
        }
        // A reset shorter than the bound is kept as stated.
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, secs(30 * MIN)), &p, start);
        assert_eq!(s.wait(&c, "m", start), Some(secs(30 * MIN)));
        assert!(!entry(&s, &c, "m").trust.bounded);
        // `0` is Go: the whole stated reset.
        let go = Policy {
            max_trusted_cooldown: Duration::ZERO,
            ..Policy::default()
        };
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, reset - start), &go, start);
        assert_eq!(s.wait(&c, "m", start), Some(reset - start));
    }

    /// The trust count is its own: Go's no-hint backoff level neither raises the first
    /// bound nor is changed by a cut window.
    #[test]
    fn trust_count_and_go_backoff_level_are_separate() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        // No-hint 429s first: Go's level climbs (1 s, 2 s, 4 s, ...).
        let mut s = Scheduler::default();
        let mut now = Instant::now();
        for _ in 0..9 {
            s.record(&c, "m", &unhinted(FailureScope::Model), &p, now);
            now += secs(600);
        }
        assert_eq!(entry(&s, &c, "m").level, 9);
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, now);
        assert_eq!(s.wait(&c, "m", now), Some(secs(H)), "the first bound, not 2^9 hours");
        assert_eq!(entry(&s, &c, "m").level, 9, "Go's level keeps its meaning");
        // Hinted first, then no-hint 429s after the window: Go's backoff runs from its
        // own level, and the next cut window still doubles.
        let mut s = Scheduler::default();
        let mut now = Instant::now();
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, now);
        now += secs(H);
        s.record(&c, "m", &unhinted(FailureScope::Model), &p, now);
        assert_eq!(s.wait(&c, "m", now), Some(secs(1)), "Go's first backoff step");
        now += secs(1);
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, now);
        assert_eq!(s.wait(&c, "m", now), Some(secs(2 * H)), "the trust count survived");
    }

    /// A stated reset that lands during a cooldown the bound did not set (a 5xx, a
    /// 401, a no-hint 429 backoff) opens a bounded window instead of probing at once.
    #[test]
    fn a_reset_inside_another_cooldown_gets_the_bound() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        let start = Instant::now();
        let first = |status: u16, scope| {
            let mut s = Scheduler::default();
            s.record(
                &c,
                "m",
                &Outcome::Failure(ExecError::local(status, scope, "earlier")),
                &p,
                start,
            );
            s
        };
        for (status, scope, earlier) in [
            (503, FailureScope::Credential, secs(60)),
            (429, FailureScope::Model, secs(1)),
        ] {
            let mut s = first(status, scope);
            assert_eq!(s.wait(&c, "m", start), Some(earlier));
            let at = start + Duration::from_millis(500);
            s.record(&c, "m", &hinted(FailureScope::Model, days), &p, at);
            assert_eq!(s.wait(&c, "m", at), Some(secs(H)), "after {status}");
            assert!(entry(&s, &c, "m").trust.bounded, "after {status}");
            // A concurrent 6-day answer inside that window neither extends nor escalates.
            let later = at + secs(60);
            s.record(&c, "m", &hinted(FailureScope::Model, days), &p, later);
            assert_eq!(s.wait(&c, "m", at), Some(secs(H)), "after {status}");
            assert_eq!(entry(&s, &c, "m").trust.window, Some(0), "after {status}");
        }
        // A longer cooldown from another cause stands, and is not marked bounded.
        let mut s = first(401, FailureScope::Credential);
        let cap = Policy {
            max_trusted_cooldown: secs(600),
            ..Policy::default()
        };
        s.record(&c, "m", &hinted(FailureScope::Model, days), &cap, start);
        assert_eq!(s.wait(&c, "m", start), Some(secs(30 * MIN)));
        assert!(!entry(&s, &c, "m").trust.bounded);
    }

    /// Inside a live bounded window, a concurrent stated reset keeps the probe deadline
    /// unless it is earlier; with the setting off Go's merge applies.
    #[test]
    fn concurrent_resets_keep_or_shorten_the_window() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let window = |s: &mut Scheduler, policy: &Policy| {
            s.record(&c, "m", &hinted(FailureScope::Model, secs(6 * 24 * H)), policy, start);
        };
        // A 45-minute reset at minute 30 does not push the probe to minute 75.
        let mut s = Scheduler::default();
        window(&mut s, &p);
        let at = start + secs(30 * MIN);
        s.record(&c, "m", &hinted(FailureScope::Model, secs(45 * MIN)), &p, at);
        assert_eq!(s.wait(&c, "m", start), Some(secs(H)));
        // A 30-minute reset at minute 1 moves it up to minute 31.
        let mut s = Scheduler::default();
        window(&mut s, &p);
        let at = start + secs(MIN);
        s.record(&c, "m", &hinted(FailureScope::Model, secs(30 * MIN)), &p, at);
        assert_eq!(s.wait(&c, "m", start), Some(secs(31 * MIN)));
        assert_eq!(entry(&s, &c, "m").trust.window, Some(0), "not a new window");
        // Setting off: Go keeps the later deadline.
        let go = Policy {
            max_trusted_cooldown: Duration::ZERO,
            ..Policy::default()
        };
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, secs(H)), &go, start);
        let at = start + secs(30 * MIN);
        s.record(&c, "m", &hinted(FailureScope::Model, secs(45 * MIN)), &go, at);
        assert_eq!(s.wait(&c, "m", start), Some(secs(75 * MIN)));
    }

    /// The first pick after a bounded window reserves the probe: the window keeps
    /// cooling until the probe's answer, and is never waited for by retries.
    #[test]
    fn one_probe_per_ended_window() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        for (scope, key) in [(FailureScope::Model, "m"), (FailureScope::Credential, "")] {
            let mut s = Scheduler::default();
            s.record(&c, "m", &hinted(scope, secs(6 * 24 * H)), &p, start);
            let near = start + secs(H) - secs(5);
            assert!(
                !s.retry_eligible(&c, "m", near),
                "{scope:?}: a bounded window is not waited for"
            );
            let end = start + secs(H);
            assert_eq!(s.wait(&c, "m", end), None, "{scope:?}: the probe is due");
            s.reserve_probe(&c, "m", end);
            assert_eq!(s.wait(&c, "m", end), Some(PROBE_HOLD), "{scope:?}: reserved");
            assert!(s.quota_cooling(&c, "m", end), "{scope:?}");
            assert!(!s.retry_eligible(&c, "m", end), "{scope:?}");
            // An unrelated model of the same credential waits too when the window is the
            // credential's.
            assert_eq!(s.wait(&c, "other", end).is_some(), key.is_empty(), "{scope:?}");
            // The probe's 429 opens the next window; its success would clear all.
            s.record(&c, "m", &hinted(scope, secs(5 * 24 * H)), &p, end + secs(2));
            assert_eq!(s.wait(&c, "m", end + secs(2)), Some(secs(2 * H)), "{scope:?}");
            assert_eq!(entry(&s, &c, key).trust.probe, None);
            // A cancelled probe frees the window at once; a lost one after PROBE_HOLD.
            let next = end + secs(2) + secs(2 * H);
            s.reserve_probe(&c, "m", next);
            s.record(&c, "m", &Outcome::Cancelled, &p, next + secs(1));
            assert_eq!(s.wait(&c, "m", next + secs(1)), None, "{scope:?}");
            s.reserve_probe(&c, "m", next + secs(1));
            assert_eq!(s.wait(&c, "m", next + secs(1) + PROBE_HOLD), None, "{scope:?}");
        }
        // Only bounded windows reserve: an ended Go backoff does not.
        let mut s = Scheduler::default();
        s.record(&c, "m", &unhinted(FailureScope::Model), &p, start);
        s.reserve_probe(&c, "m", start + secs(2));
        assert_eq!(s.wait(&c, "m", start + secs(2)), None);
    }

    /// A credential-wide window escalates the model keys too; when the credential
    /// recovers through a success on another model, they forget the count.
    #[test]
    fn a_success_on_another_model_clears_the_escalation() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let days = secs(6 * 24 * H);
        let mut s = Scheduler::default();
        s.record(&c, "a", &hinted(FailureScope::Credential, days), &p, start);
        assert_eq!(entry(&s, &c, "a").trust.window, Some(0));
        let end = start + secs(H);
        s.record(&c, "b", &Outcome::Success, &p, end);
        s.record(&c, "a", &hinted(FailureScope::Model, days), &p, end);
        assert_eq!(s.wait(&c, "a", end), Some(secs(H)), "the first bound again");
    }

    /// A probe the upstream accepted (a stream started) ends the window's question at
    /// once: the next pick goes through while the stream runs, and a later hinted 429
    /// still doubles. A credential-wide window's copies on model keys are disarmed too;
    /// a newer live window is left alone.
    #[test]
    fn an_accepted_probe_frees_the_account_and_keeps_the_count() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        let start = Instant::now();
        let end = start + secs(H);
        // Model scope.
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, start);
        s.reserve_probe(&c, "m", end);
        assert!(s.wait(&c, "m", end).is_some(), "reserved");
        s.accept_probe(&c, "m", end);
        assert_eq!(s.wait(&c, "m", end), None, "the next pick goes through");
        s.reserve_probe(&c, "m", end + secs(1));
        assert_eq!(s.wait(&c, "m", end + secs(1)), None, "no second reservation");
        let later = end + secs(60);
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, later);
        assert_eq!(s.wait(&c, "m", later), Some(secs(2 * H)), "the count survived");
        // A live window (another model's newer one) is untouched.
        s.accept_probe(&c, "m", later + secs(1));
        assert!(entry(&s, &c, "m").trust.bounded);
        // Credential scope: B's accepted probe disarms A's copy as well.
        let mut s = Scheduler::default();
        s.record(&c, "a", &hinted(FailureScope::Credential, days), &p, start);
        s.reserve_probe(&c, "b", end);
        s.accept_probe(&c, "b", end);
        assert_eq!(s.wait(&c, "b", end), None);
        s.reserve_probe(&c, "a", end);
        assert_eq!(s.wait(&c, "a", end), None, "A's copy does not reserve again");
        assert_eq!(entry(&s, &c, "a").trust.window, Some(0), "count kept");
    }

    /// The credential's probe answers its window and the window's copies on model keys,
    /// not a model's own window that ended unprobed before it: that model still gets a
    /// probe of its own, after a streamed probe and after a plain success alike, and the
    /// credential probe's later success leaves that model's newer probe state alone.
    #[test]
    fn a_credential_probe_leaves_a_models_own_ended_window() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        let start = Instant::now();
        for streamed in [true, false] {
            let mut s = Scheduler::default();
            s.record(&c, "m", &hinted(FailureScope::Model, days), &p, start);
            let opened = start + secs(H) + secs(MIN);
            s.record(&c, "a", &hinted(FailureScope::Credential, days), &p, opened);
            let end = opened + secs(H);
            s.reserve_probe(&c, "b", end);
            if streamed {
                s.accept_probe(&c, "b", end);
            } else {
                s.record(&c, "b", &Outcome::Success, &p, end);
            }
            s.reserve_probe(&c, "a", end);
            assert_eq!(s.wait(&c, "a", end), None, "streamed {streamed}: A's copy is answered");
            s.reserve_probe(&c, "m", end);
            assert_eq!(
                s.wait(&c, "m", end),
                Some(PROBE_HOLD),
                "streamed {streamed}: m reserves its own probe"
            );
        }
        // The credential's stream started, then m's probe was taken over by a later one
        // that was accepted too. The credential stream's success comes from an older
        // lease: it leaves m's accepted window alone, so the superseded m probe's late
        // 429 is still ignored and the later probe's success frees m.
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, start);
        let opened = start + secs(H) + secs(MIN);
        s.record(&c, "a", &hinted(FailureScope::Credential, days), &p, opened);
        let end = opened + secs(H);
        let credential_probe = s.reserve_probe(&c, "b", end);
        s.accept_probe_picked(&c, "b", credential_probe, end);
        let first = s.reserve_probe(&c, "m", end);
        let lapsed = end + PROBE_HOLD + secs(1);
        let second = s.reserve_probe(&c, "m", lapsed);
        assert!(credential_probe != first && first != second, "real generations");
        s.accept_probe_picked(&c, "m", second, lapsed);
        let done = lapsed + secs(1);
        s.record_picked(&c, "b", &Outcome::Success, &p, credential_probe, done);
        s.record_picked(&c, "m", &hinted(FailureScope::Model, days), &p, first, done);
        assert_eq!(s.wait(&c, "m", done), None, "no window from the superseded probe");
        s.record_picked(&c, "m", &Outcome::Success, &p, second, done + secs(1));
        assert!(
            !s.cooldowns.contains_key(&(c.id.clone(), "m".to_owned())),
            "m recovered"
        );
    }

    /// A probe whose hold lapsed was taken over by a later probe: its late 429 neither
    /// opens the next window nor touches the later probe's reservation, also once the
    /// later probe was accepted, so the later probe's success frees the account.
    #[test]
    fn a_superseded_probes_429_leaves_the_window_to_the_current_probe() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        let start = Instant::now();
        for scope in [FailureScope::Model, FailureScope::Credential] {
            // The second probe is still unanswered, or already accepted (its stream's
            // first event arrived) when the first one's 429 comes back.
            for accepted in [false, true] {
                let mut s = Scheduler::default();
                s.record(&c, "m", &hinted(scope, days), &p, start);
                let end = start + secs(H);
                let first = s.reserve_probe(&c, "m", end);
                let lapsed = end + PROBE_HOLD + secs(1);
                let second = s.reserve_probe(&c, "m", lapsed);
                if accepted {
                    s.accept_probe_picked(&c, "m", second, lapsed);
                }
                let late = lapsed + secs(1);
                s.record_picked(&c, "m", &hinted(scope, days - secs(H)), &p, first, late);
                assert_eq!(
                    s.wait(&c, "m", late),
                    (!accepted).then(|| PROBE_HOLD - secs(1)),
                    "{scope:?}, accepted {accepted}: still the second probe's"
                );
                s.record_picked(&c, "m", &Outcome::Success, &p, second, late + secs(1));
                assert_eq!(
                    s.wait(&c, "m", late + secs(1)),
                    None,
                    "{scope:?}, accepted {accepted}: recovered"
                );
            }
        }
    }

    /// A credential-wide window that its probe's 429 renews carries its copies on model
    /// keys along, ended ones included: when the next probe recovers the account, no copy
    /// is left to ask for a probe of its own or to keep the count.
    #[test]
    fn a_renewed_credential_window_carries_its_ended_copies() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        let start = Instant::now();
        for streamed in [true, false] {
            let mut s = Scheduler::default();
            s.record(&c, "a", &hinted(FailureScope::Credential, days), &p, start);
            let first_end = start + secs(H);
            s.reserve_probe(&c, "b", first_end);
            let renewed = first_end + secs(1);
            s.record(&c, "b", &hinted(FailureScope::Credential, days - secs(H)), &p, renewed);
            let second_end = renewed + secs(2 * H);
            s.reserve_probe(&c, "c", second_end);
            if streamed {
                s.accept_probe(&c, "c", second_end);
            } else {
                s.record(&c, "c", &Outcome::Success, &p, second_end);
            }
            s.reserve_probe(&c, "a", second_end);
            assert_eq!(s.wait(&c, "a", second_end), None, "streamed {streamed}: no probe for A");
            if !streamed {
                s.record(&c, "a", &hinted(FailureScope::Model, days), &p, second_end);
                assert_eq!(s.wait(&c, "a", second_end), Some(secs(H)), "the count is cleared");
            }
        }
    }

    /// A lease picked outside selection (the credits fallback) on a credential whose
    /// ended window has a probe in flight neither frees that probe nor ends the window,
    /// whatever its outcome; nor does it answer a window opened after it.
    #[test]
    fn outside_selection_leases_answer_no_bounded_window() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, secs(6 * 24 * H)), &p, start);
        let ended = start + secs(H);
        let probe = s.reserve_probe(&c, "m", ended);
        let credits = s.outside_selection_generation();
        for outcome in [Outcome::Cancelled, Outcome::Success] {
            s.record_picked(&c, "m", &outcome, &p, credits, ended);
            assert!(s.wait(&c, "m", ended).is_some(), "the reserved probe holds the window");
        }
        s.accept_probe_picked(&c, "m", credits, ended);
        assert!(s.wait(&c, "m", ended).is_some(), "a credits stream accepts nothing");
        s.record_picked(&c, "m", &Outcome::Success, &p, probe, ended);
        assert_eq!(s.wait(&c, "m", ended), None, "the probe's own success ends it");
        // A window opened after the credits lease was picked is newer than it too.
        let credits = s.outside_selection_generation();
        let later = ended + secs(MIN);
        s.record(&c, "m", &hinted(FailureScope::Model, secs(6 * 24 * H)), &p, later);
        let reopened = later + secs(H);
        s.reserve_probe(&c, "m", reopened);
        s.record_picked(&c, "m", &Outcome::Cancelled, &p, credits, reopened);
        assert!(s.wait(&c, "m", reopened).is_some(), "the later probe still holds");
    }

    /// Generations compare in serial-number order: a window opened just after the counter
    /// wraps is newer than a lease picked just before, and older than one picked after.
    #[test]
    fn generations_compare_across_the_wrap() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let mut s = Scheduler {
            generation: u32::MAX,
            ..Scheduler::default()
        };
        let before = s.reserve_probe(&c, "m", start);
        assert_eq!(before, u32::MAX);
        s.record(&c, "m", &hinted(FailureScope::Model, secs(6 * 24 * H)), &p, start);
        assert_eq!(entry(&s, &c, "m").trust.generation, 0, "wrapped");
        let at = start + secs(MIN);
        s.record_picked(&c, "m", &Outcome::Success, &p, before, at);
        assert_eq!(s.wait(&c, "m", at), Some(secs(H - MIN)), "an older success leaves it");
        s.record_picked(&c, "m", &Outcome::Success, &p, 0, at);
        assert_eq!(s.wait(&c, "m", at), None, "a newer success ends it");
    }

    /// An earlier credential-wide reset moves up the model keys that inherited the
    /// window; an independent longer cooldown stands.
    #[test]
    fn an_earlier_credential_reset_shortens_inherited_model_windows() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let mut s = Scheduler::default();
        let support = ExecError::local(400, FailureScope::Model, "model is not supported");
        s.record(&c, "support", &Outcome::Failure(support), &p, start);
        s.record(&c, "sibling", &unhinted(FailureScope::Model), &p, start);
        s.record(&c, "a", &hinted(FailureScope::Credential, secs(6 * 24 * H)), &p, start);
        let at = start + secs(MIN);
        s.record(&c, "a", &hinted(FailureScope::Credential, secs(30 * MIN)), &p, at);
        for model in ["a", "sibling", "unrelated"] {
            assert_eq!(s.wait(&c, model, start), Some(secs(31 * MIN)), "{model}");
        }
        assert_eq!(s.wait(&c, "support", start), Some(secs(12 * H)));
    }

    /// A model's own bounded window that a credential-wide window covers keeps its own
    /// probe time: an earlier credential reset moves the credential's probe up, never
    /// the model's before its own bound, whether the credential's 429 came from another
    /// model or from that one.
    #[test]
    fn an_earlier_credential_reset_never_moves_a_models_own_probe_earlier() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        let start = Instant::now();
        let own_end = start + secs(H);
        for failing in ["a", "m"] {
            let mut s = Scheduler::default();
            s.record(&c, "m", &hinted(FailureScope::Model, days), &p, start);
            let opened = start + secs(30 * MIN);
            s.record(&c, failing, &hinted(FailureScope::Credential, days), &p, opened);
            let at = opened + secs(MIN);
            s.record(&c, failing, &hinted(FailureScope::Credential, secs(10 * MIN)), &p, at);
            let moved = at + secs(10 * MIN);
            assert_eq!(
                s.wait(&c, "other", moved),
                None,
                "{failing}: the credential's probe is due"
            );
            assert_eq!(
                s.wait(&c, "m", moved),
                Some(own_end - moved),
                "{failing}: m waits for its own bound"
            );
        }
    }

    /// A model's own cooldown of another cause (a 401's 30 minutes) under a bounded
    /// credential window keeps its own deadline, as Go keeps the later one: an earlier
    /// credential reset brings the credential's probe forward, never the model back
    /// before its own deadline, whether the credential's 429 came from another model or
    /// from that one.
    #[test]
    fn an_earlier_credential_reset_never_shortens_a_models_own_cooldown() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let unauthorized = Outcome::Failure(ExecError::local(401, FailureScope::Credential, "unauthorized"));
        for failing in ["a", "m"] {
            let mut s = Scheduler::default();
            s.record(&c, "m", &unauthorized, &p, start);
            assert_eq!(s.wait(&c, "m", start), Some(secs(30 * MIN)));
            let opened = start + secs(MIN);
            s.record(
                &c,
                failing,
                &hinted(FailureScope::Credential, secs(6 * 24 * H)),
                &p,
                opened,
            );
            let at = start + secs(2 * MIN);
            s.record(&c, failing, &hinted(FailureScope::Credential, secs(MIN)), &p, at);
            let moved = start + secs(3 * MIN);
            assert_eq!(
                s.wait(&c, "other", moved),
                None,
                "{failing}: the credential's probe is due"
            );
            assert_eq!(
                s.wait(&c, "m", moved),
                Some(secs(27 * MIN)),
                "{failing}: m keeps its own 30 minutes"
            );
        }
    }

    /// Provenance follows the winning deadline: the failing model's own longer bounded
    /// window keeps its flag and stated reset through a credential-wide merge.
    #[test]
    fn the_own_key_keeps_its_window_through_a_credential_merge() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        let start = Instant::now();
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, start);
        s.record(
            &c,
            "m",
            &hinted(FailureScope::Model, days - secs(H)),
            &p,
            start + secs(H),
        );
        let own_end = start + secs(3 * H);
        let at = start + secs(H) + secs(MIN);
        s.record(&c, "m", &hinted(FailureScope::Credential, days), &p, at);
        let own = entry(&s, &c, "m");
        assert_eq!(own.deadline, own_end);
        assert!(own.trust.bounded && own.trust.stated == Some(start + days));
        s.reserve_probe(&c, "m", own_end);
        assert!(
            entry(&s, &c, "m").trust.probe.is_some(),
            "it reserves a probe at its end"
        );
    }

    /// A restore into a scheduler that already holds a later deadline keeps that
    /// entry's provenance and classification, merging the counts.
    #[test]
    fn a_restore_keeps_the_winner_trust() {
        use crate::cooldown_store::{Quota, Record};
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let (now, wall) = (Instant::now(), SystemTime::now());
        let days = secs(6 * 24 * H);
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, now);
        let live = entry(&s, &c, "m").trust;
        let shorter = Record {
            auth_id: "a".into(),
            model: "m".into(),
            next_retry_after: Some(wall + secs(10 * MIN)),
            quota: Quota {
                exceeded: true,
                reason: "quota".into(),
                next_recover_at: Some(wall + secs(10 * MIN)),
                trust_windows: Some(3),
                ..Default::default()
            },
            ..Default::default()
        };
        assert!(s.restore(&c, &shorter, true, secs(H), now, wall));
        let merged = entry(&s, &c, "m");
        assert_eq!(merged.deadline, now + secs(H));
        assert!(merged.trust.bounded && merged.trust.stated == live.stated);
        assert_eq!(merged.trust.window, Some(2), "counts merge");
        assert_eq!(merged.trust.generation, live.generation, "the live window's generation");
    }

    /// Go 35af74f9 (conductor_issue6643_test.go): a success dispatched before a live
    /// cooldown was recorded leaves it, model-keyed or credential-level, and across a
    /// restore while serving; one dispatched after it, or one without a stamp, clears it.
    #[test]
    fn a_success_dispatched_before_a_cooldown_keeps_it() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy {
            max_trusted_cooldown: Duration::ZERO,
            ..Policy::default()
        };
        let (now, wall) = (Instant::now(), SystemTime::now());
        let g = 0;
        let quota = || hinted(FailureScope::Model, secs(H));
        // Here a credential-level 429 is the credential's quota, which no success clears;
        // the credential-level case uses a 503 with a retry hint.
        let unavailable = || {
            let mut e = ExecError::local(503, FailureScope::Model, "unavailable");
            e.retry_after = Some(secs(H));
            Outcome::Failure(e)
        };
        for (model, failure) in [("m", quota()), ("", unavailable())] {
            let mut s = Scheduler::default();
            let stale = s.dispatch();
            let failed = s.dispatch();
            s.record_dispatched(&c, model, &failure, &p, g, failed, now);
            s.record_dispatched(&c, model, &Outcome::Success, &p, g, stale, now);
            s.record_dispatched(&c, model, &Outcome::Success, &p, g, failed, now);
            assert!(s.cooling(&c, model, now), "{model:?}: the stale successes keep it");
            let after = s.dispatch();
            s.record_dispatched(&c, model, &Outcome::Success, &p, g, after, now);
            assert!(!s.cooling(&c, model, now), "{model:?}: a later dispatch clears it");

            let again = s.dispatch();
            s.record_dispatched(&c, model, &failure, &p, g, again, now);
            s.record_dispatched(&c, model, &Outcome::Success, &p, g, 0, now);
            assert!(!s.cooling(&c, model, now), "{model:?}: an unstamped success clears it");
        }
        // A skipped credential-level failure (a connection drop) still moves the
        // credential's watermark past a success dispatched before it.
        let mut s = Scheduler::default();
        let first = s.dispatch();
        s.record_dispatched(&c, "", &unavailable(), &p, g, first, now);
        let success = s.dispatch();
        let dropped = Outcome::Failure(ExecError::local(0, FailureScope::Model, "http2: client conn is closed"));
        let third = s.dispatch();
        s.record_dispatched(&c, "", &dropped, &p, g, third, now);
        s.record_dispatched(&c, "", &Outcome::Success, &p, g, success, now);
        assert!(s.cooling(&c, "", now), "the skipped failure stamped the credential");
        // Go `mergeModelState` keeps the live watermark through a restore; a cooldown
        // restored after a restart has none, and any success clears it.
        let mut s = Scheduler::default();
        let stale = s.dispatch();
        let failed = s.dispatch();
        s.record_dispatched(&c, "m", &quota(), &p, g, failed, now);
        let saved = s.records(&c, now, wall);
        assert!(s.restore(&c, &saved[0], true, Duration::ZERO, now, wall));
        s.record_dispatched(&c, "m", &Outcome::Success, &p, g, stale, now);
        assert!(s.cooling(&c, "m", now), "the restore kept the watermark");
        let mut restarted = Scheduler::default();
        assert!(restarted.restore(&c, &saved[0], true, Duration::ZERO, now, wall));
        let first = restarted.dispatch();
        restarted.record_dispatched(&c, "m", &Outcome::Success, &p, g, first, now);
        assert!(!restarted.cooling(&c, "m", now), "no watermark after a restart");
        // A credential-wide 429 stamps every sibling it extends.
        let mut s = Scheduler::default();
        let other = s.dispatch();
        s.record_dispatched(&c, "other", &quota(), &p, g, other, now);
        let stale = s.dispatch();
        let wide = s.dispatch();
        s.record_dispatched(&c, "m", &hinted(FailureScope::Credential, secs(H)), &p, g, wide, now);
        s.cooldowns.remove(&(c.id.clone(), String::new()));
        s.record_dispatched(&c, "other", &Outcome::Success, &p, g, stale, now);
        assert!(
            s.cooling(&c, "other", now),
            "the sibling carries the credential's watermark"
        );
    }

    /// A restore while serving (`save-cooldown-status` turned on) that reinstalls a
    /// bounded window gives it a fresh generation: a stream picked before the window
    /// opened still cannot end it.
    #[test]
    fn a_restore_while_serving_keeps_the_window_newer_than_older_leases() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let (now, wall) = (Instant::now(), SystemTime::now());
        let mut s = Scheduler::default();
        let stream = s.reserve_probe(&c, "m", now);
        s.record(&c, "m", &hinted(FailureScope::Model, secs(6 * 24 * H)), &p, now);
        let saved = s.records(&c, now, wall);
        assert!(s.restore(&c, &saved[0], true, secs(H), now, wall));
        assert!(entry(&s, &c, "m").trust.bounded);
        let at = now + secs(MIN);
        s.record_picked(&c, "m", &Outcome::Success, &p, stream, at);
        assert_eq!(s.wait(&c, "m", at), Some(secs(H - MIN)), "the window stands");
    }

    /// A 503 from a request already in flight inside a live bounded window: the window
    /// stands, and its saved record keeps the stated reset and count.
    #[test]
    fn a_503_inside_a_bounded_window_keeps_it() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let (now, wall) = (Instant::now(), SystemTime::now());
        let days = secs(6 * 24 * H);
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, now);
        let at = now + secs(30 * MIN);
        let unavailable = ExecError::local(503, FailureScope::Model, "overloaded");
        s.record(&c, "m", &Outcome::Failure(unavailable), &p, at);
        let saved = &s.records(&c, at, wall + secs(30 * MIN))[0];
        assert!(saved.quota.exceeded);
        assert_eq!(saved.quota.next_recover_at, Some(wall + days));
        assert_eq!(saved.quota.trust_windows, Some(1));
        assert_eq!(saved.reason, "quota");
    }

    /// A Cloudflare challenge from a request already in flight inside a live bounded
    /// window leaves the window as it is but still raises Go's level, as Go does.
    #[test]
    fn a_cloudflare_challenge_inside_a_bounded_window_raises_the_level() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let start = Instant::now();
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, secs(6 * 24 * H)), &p, start);
        let challenge = Outcome::Failure(ExecError::local(403, FailureScope::Model, "cloudflare challenge"));
        for (n, level) in [(1, 1), (2, 2)] {
            s.record(&c, "m", &challenge, &p, start + secs(n * MIN));
            assert_eq!(entry(&s, &c, "m").level, level);
            assert_eq!(s.wait(&c, "m", start), Some(secs(H)), "the window stands");
            assert!(entry(&s, &c, "m").trust.bounded);
        }
    }

    /// `.cds` records: the probe time is the retry, the stated reset Go's recovery time,
    /// and the count is its own field. Restores never escalate, and Go's records get
    /// the first bound whatever their backoff level.
    #[test]
    fn restored_quota_resets_are_bounded_too() {
        use crate::cooldown_store::{Quota, Record};
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let (now, wall) = (Instant::now(), SystemTime::now());
        let cap = secs(H);
        let days = secs(6 * 24 * H);
        let mut s = Scheduler::default();
        s.record(&c, "m", &hinted(FailureScope::Model, days), &p, now);
        let saved = s.records(&c, now, wall);
        assert_eq!(saved[0].next_retry_after, Some(wall + secs(H)));
        assert_eq!(saved[0].quota.next_recover_at, Some(wall + days));
        assert_eq!(
            (saved[0].quota.trust_windows, saved[0].quota.backoff_level),
            (Some(1), 0)
        );
        // Our own record restores as it was: one hour, the same window.
        let mut back = Scheduler::default();
        assert!(back.restore(&c, &saved[0], true, cap, now, wall));
        assert_eq!(back.wait(&c, "m", now), Some(secs(H)));
        assert_eq!(back.records(&c, now, wall), saved);
        let record_at = |retry: Duration, trust_windows, backoff_level| Record {
            auth_id: "a".into(),
            model: "m".into(),
            next_retry_after: Some(wall + retry),
            quota: Quota {
                exceeded: true,
                reason: "quota".into(),
                next_recover_at: Some(wall + days),
                backoff_level,
                trust_windows,
                ..Default::default()
            },
            ..Default::default()
        };
        // Go's record with backoff level 2: the first bound; the level stays Go's.
        let mut s = Scheduler::default();
        assert!(s.restore(&c, &record_at(days, None, 2), true, cap, now, wall));
        assert_eq!(s.wait(&c, "m", now), Some(secs(H)));
        assert_eq!(entry(&s, &c, "m").level, 2);
        // Our record in its third window (4 h, count 2): restored as it is, and the
        // count survives, so the next cut window is 8 h.
        let mut s = Scheduler::default();
        assert!(s.restore(&c, &record_at(secs(4 * H), Some(3), 0), true, cap, now, wall));
        assert_eq!(s.wait(&c, "m", now), Some(secs(4 * H)));
        let trust = entry(&s, &c, "m").trust;
        assert!(trust.bounded && trust.window == Some(2));
        assert_eq!(trust.stated, Some(now + days));
        let later = now + secs(4 * H);
        s.record(&c, "m", &hinted(FailureScope::Model, days - secs(4 * H)), &p, later);
        assert_eq!(s.wait(&c, "m", later), Some(secs(8 * H)));
        // A reset trusted as stated restores as it was, unbounded, keeping the count.
        let ninety = secs(90 * MIN);
        let trusted = Record {
            quota: Quota {
                next_recover_at: Some(wall + ninety),
                ..record_at(ninety, Some(1), 0).quota
            },
            ..record_at(ninety, Some(1), 0)
        };
        let mut s = Scheduler::default();
        assert!(s.restore(&c, &trusted, true, cap, now, wall));
        assert_eq!(s.wait(&c, "m", now), Some(ninety));
        let back = &s.records(&c, now, wall)[0];
        assert_eq!(
            (
                back.next_retry_after,
                back.quota.next_recover_at,
                back.quota.trust_windows
            ),
            (trusted.next_retry_after, trusted.quota.next_recover_at, Some(1))
        );
        assert!(!entry(&s, &c, "m").trust.bounded);
        // An absurd saved count is clamped, not overflowed.
        let mut s = Scheduler::default();
        assert!(s.restore(&c, &record_at(secs(H), Some(u32::MAX), 0), true, cap, now, wall));
        assert_eq!(entry(&s, &c, "m").trust.window, Some(MAX_TRUST_WINDOW));
        // Setting off: Go's six days.
        let mut s = Scheduler::default();
        assert!(s.restore(&c, &record_at(days, Some(3), 0), true, Duration::ZERO, now, wall));
        assert_eq!(s.wait(&c, "m", now), Some(days));
    }

    /// A longer cooldown of another cause that a credential-wide quota marked as quota
    /// (model B's 12 h model-support cooldown during a six-day 429 on model A) keeps its
    /// own deadline through a save and a restore: it is cliproxy-rs's record, not Go's.
    #[test]
    fn a_marked_sibling_restores_its_own_deadline() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let (now, wall) = (Instant::now(), SystemTime::now());
        let mut s = Scheduler::default();
        let support = ExecError::local(400, FailureScope::Model, "model is not supported");
        s.record(&c, "b", &Outcome::Failure(support), &p, now);
        assert_eq!(s.wait(&c, "b", now), Some(secs(12 * H)), "Go's model-support cooldown");
        s.record(&c, "a", &hinted(FailureScope::Credential, secs(6 * 24 * H)), &p, now);
        let records = s.records(&c, now, wall);
        let b = records.iter().find(|r| r.model == "b").unwrap();
        assert!(b.quota.exceeded && b.quota.trust_windows.is_some(), "{b:?}");
        let mut back = Scheduler::default();
        for r in &records {
            back.restore(&c, r, true, secs(H), now, wall);
        }
        assert_eq!(back.wait(&c, "b", now), Some(secs(12 * H)));
        assert!(!entry(&back, &c, "b").trust.bounded, "not cut, so not a bounded window");
    }

    /// A credential-wide window does not lower a model's own count: a model already in
    /// its fourth window (8 h) keeps it when a credential-wide 1 h window outlasts its
    /// own, so its next cut window is 16 h, not 2 h. Both the sibling and the failing
    /// model's own key.
    #[test]
    fn credential_wide_windows_keep_the_larger_model_count() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let days = secs(6 * 24 * H);
        for failing in ["other", "m"] {
            let mut now = Instant::now();
            let mut s = Scheduler::default();
            for window in [1, 2, 4, 8] {
                s.record(&c, "m", &hinted(FailureScope::Model, days), &p, now);
                assert_eq!(s.wait(&c, "m", now), Some(secs(window * H)));
                if window < 8 {
                    now += secs(window * H);
                }
            }
            // Ten minutes before m's 8 h window ends, a credential-wide hint whose 1 h
            // window outlasts it.
            let late = now + secs(8 * H) - secs(10 * MIN);
            s.record(&c, failing, &hinted(FailureScope::Credential, days), &p, late);
            assert_eq!(entry(&s, &c, "m").trust.window, Some(3), "{failing}: count kept");
            let end = late + secs(H);
            s.record(&c, "m", &hinted(FailureScope::Model, days), &p, end);
            assert_eq!(s.wait(&c, "m", end), Some(secs(16 * H)), "{failing}");
        }
    }

    /// Go's level climbs under Cloudflare challenges too, even inside a live window; a
    /// stated reset there still gets the first bound.
    #[test]
    fn a_reset_inside_a_cloudflare_window_gets_the_first_bound() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let mut now = Instant::now();
        let mut s = Scheduler::default();
        for _ in 0..5 {
            let challenge = ExecError::local(403, FailureScope::Model, "cloudflare challenge");
            s.record(&c, "m", &Outcome::Failure(challenge), &p, now);
            now += secs(1);
        }
        assert!(entry(&s, &c, "m").level >= 5);
        assert!(s.wait(&c, "m", now).is_some(), "inside the challenge window");
        s.record(&c, "m", &hinted(FailureScope::Model, secs(6 * 24 * H)), &p, now);
        assert_eq!(s.wait(&c, "m", now), Some(secs(H)));
    }

    #[test]
    fn cooldown_deadlines_floor_backoff_and_sibling_success() {
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let now = Instant::now();
        let mut s = Scheduler::default();
        let fail = |status, scope, hint| {
            let mut e = ExecError::local(status, scope, "failure");
            e.retry_after = hint;
            Outcome::Failure(e)
        };
        s.record(&c, "sibling", &fail(404, FailureScope::Model, None), &p, now);
        s.record(
            &c,
            "m",
            &fail(429, FailureScope::Credential, Some(Duration::from_secs(2))),
            &p,
            now,
        );
        assert_eq!(s.wait(&c, "m", now), Some(Duration::from_secs(10)));
        assert_eq!(s.wait(&c, "new", now), Some(Duration::from_secs(10)));
        assert_eq!(
            s.wait(&c, "sibling", now),
            Some(Duration::from_secs(43200)),
            "quota must not shorten sibling"
        );
        s.record(&c, "sibling", &Outcome::Success, &p, now + Duration::from_secs(1));
        assert_eq!(
            s.wait(&c, "sibling", now),
            Some(Duration::from_secs(43200)),
            "inflight success cannot clear live quota"
        );
        assert_eq!(
            s.wait(&c, "new", now + Duration::from_secs(10)),
            None,
            "deadline boundary"
        );
        s.record(&c, "new", &Outcome::Success, &p, now + Duration::from_secs(10));
        assert!(s.wait(&c, "sibling", now + Duration::from_secs(10)).is_some());
        let mut s = Scheduler::default();
        s.record(&c, "m", &fail(429, FailureScope::Model, None), &p, now);
        assert_eq!(s.wait(&c, "m(high)", now), Some(Duration::from_secs(1)));
        s.record(&c, "m", &fail(429, FailureScope::Model, None), &p, now);
        assert_eq!(
            s.wait(&c, "m", now),
            Some(Duration::from_secs(1)),
            "active quota reused"
        );
        s.record(
            &c,
            "m",
            &fail(429, FailureScope::Model, None),
            &p,
            now + Duration::from_secs(1),
        );
        assert_eq!(
            s.wait(&c, "m", now + Duration::from_secs(1)),
            Some(Duration::from_secs(2))
        );
        assert_eq!(s.wait(&c, "other", now), None, "model failure stays local");
        for (status, seconds) in [
            (401, 1800),
            (402, 1800),
            (403, 1800),
            (404, 43200),
            (408, 60),
            (500, 60),
            (526, 60),
        ] {
            let mut s = Scheduler::default();
            s.record(&c, "m", &fail(status, FailureScope::Model, None), &p, now);
            assert_eq!(
                s.wait(&c, "m", now),
                Some(Duration::from_secs(seconds)),
                "status {status}"
            );
        }
    }

    /// Go `Manager.MarkResult` cooldowns: goldens from tests/reference/server/main.go,
    /// replayed with upstream-shaped errors (headers present, executor scopes).
    #[test]
    fn cooldowns_match_go_mark_result() {
        let fixture: Value = serde_json::from_str(include_str!("../tests/fixtures/server_go.json")).unwrap();
        let cases = fixture["cooldown"].as_array().unwrap();
        assert_eq!(cases.len(), 25);
        // Go trusts every stated reset (docs/DIFFERENCES-FROM-GO.md).
        let policy = Policy {
            max_trusted_cooldown: Duration::ZERO,
            ..Policy::default()
        };
        for case in cases {
            let name = case["name"].as_str().unwrap();
            let c = cred(&format!("{name}.json"), serde_json::json!({}));
            let mut s = Scheduler::default();
            let now = Instant::now();
            for step in case["steps"].as_array().unwrap() {
                let status = step["status"].as_u64().unwrap() as u16;
                let scope = if step["credential_scope"].as_bool().unwrap() {
                    FailureScope::Credential
                } else {
                    match status {
                        429 => FailureScope::Model,
                        401..=403 | 408 | 500.. => FailureScope::Credential,
                        _ => FailureScope::Request,
                    }
                };
                let mut error = ExecError::local(status, scope, step["message"].as_str().unwrap());
                error
                    .headers
                    .insert("content-type", "application/json".parse().unwrap());
                let hint = step["retry_after_ms"].as_i64().unwrap();
                error.retry_after = (hint >= 0).then(|| Duration::from_millis(hint as u64));
                s.record(
                    &c,
                    step["model"].as_str().unwrap(),
                    &Outcome::Failure(error),
                    &policy,
                    now,
                );
            }
            for (model, seconds) in case["seconds"].as_object().unwrap() {
                let wait = s.wait(&c, model, now).unwrap_or_default();
                assert_eq!(
                    wait.as_secs_f64().round() as u64,
                    seconds.as_u64().unwrap(),
                    "{name} {model} wait"
                );
                let quota = case["quota"][model].as_bool().unwrap();
                // Go keeps `Quota.Exceeded` on an expired state; only a live one matters.
                assert_eq!(
                    s.quota_cooling(&c, model, now),
                    quota && wait > Duration::ZERO,
                    "{name} {model} quota"
                );
            }
        }
    }

    /// Go `SessionAffinitySelector` (`Enrich`, `Pick` with fill-first, `OnResult`):
    /// goldens from tests/reference/server/main.go.
    #[test]
    fn session_affinity_matches_go_selector() {
        let fixture: Value = serde_json::from_str(include_str!("../tests/fixtures/server_go.json")).unwrap();
        let cases = fixture["affinity"].as_array().unwrap();
        assert_eq!(cases.len(), 25);
        for case in cases {
            let name = case["name"].as_str().unwrap();
            let lcp_case = case["lcp"].as_bool().unwrap_or_default();
            let creds: Vec<Credential> = ["a", "b", "c"]
                .iter()
                .map(|id| {
                    let mut c = cred(id, serde_json::json!({}));
                    if let Some(p) = case["priorities"][*id].as_str() {
                        c.attributes.insert("priority".into(), p.into());
                    }
                    c
                })
                .collect();
            let policy = Policy {
                strategy: Strategy::FillFirst,
                session_affinity: true,
                session_affinity_subagents: case["subagent_affinity"].as_bool().unwrap(),
                ..Default::default()
            };
            let mut s = Scheduler::default();
            let now = Instant::now();
            for (i, step) in case["steps"].as_array().unwrap().iter().enumerate() {
                let mut headers = axum::http::HeaderMap::new();
                for pair in step["headers"].as_array().into_iter().flatten() {
                    headers.append(
                        axum::http::HeaderName::from_bytes(pair[0].as_str().unwrap().as_bytes()).unwrap(),
                        pair[1].as_str().unwrap().parse().unwrap(),
                    );
                }
                let payload = step["payload"].as_str().unwrap_or_default().as_bytes();
                let format = cpa_core::format::Format::parse(case["format"].as_str().unwrap_or("openai")).unwrap();
                let caller = match step["caller"].as_str() {
                    Some(c) => c,
                    None => case["caller"].as_str().unwrap_or_default(),
                };
                let caller = if lcp_case { caller } else { "" };
                let session = crate::session::resolve(format, &headers, payload, None, caller);
                // dispatch::run's LCP step.
                let lcp = (lcp_case && !session.explicit)
                    .then(|| crate::lcp::Request::new(format.as_str(), payload, caller))
                    .flatten()
                    .map(std::sync::Arc::new);
                let sel = Selection {
                    session: session.id,
                    session_parent: session.parent,
                    session_fork: session.fork,
                    lcp,
                    ..selection("m")
                };
                let find = |id: &str| creds.iter().find(|c| c.id == id).unwrap();
                let outcome = |op: &str| {
                    if op.ends_with("ok") {
                        Outcome::Success
                    } else {
                        let status = step["status"].as_u64().unwrap() as u16;
                        let scope = if status >= 500 {
                            FailureScope::Credential
                        } else {
                            FailureScope::Request
                        };
                        let mut e = ExecError::local(status, scope, step["message"].as_str().unwrap());
                        e.headers.insert("content-type", "text/plain".parse().unwrap());
                        Outcome::Failure(e)
                    }
                };
                match step["op"].as_str().unwrap() {
                    op @ ("pick" | "pick_ok" | "pick_fail") => {
                        let available: Vec<&Credential> = step["available"]
                            .as_array()
                            .unwrap()
                            .iter()
                            .map(|id| find(id.as_str().unwrap()))
                            .collect();
                        let (picked, bound) = s
                            .pick_session(&tag(&available), &sel, &policy, &|_| Windows::default(), now)
                            .unwrap();
                        assert_eq!(picked.id, step["picked"].as_str().unwrap(), "{name} step {i}");
                        // The metadata Go's pickLCP wrote, as the attempt reports it.
                        let got = bound.as_ref().map(|m| {
                            let (node_kind, fork, compaction) = m.node();
                            serde_json::json!({
                                "session": m.session, "parent": m.parent, "node_kind": node_kind,
                                "fork": fork, "compaction": compaction, "generation": m.access,
                            })
                        });
                        assert_eq!(got.as_ref(), step.get("lcp"), "{name} step {i} lcp");
                        if op != "pick" {
                            let picked = picked.clone();
                            s.session_result(&picked, &sel, &outcome(op), &policy, bound.as_ref(), now);
                        }
                    }
                    op => {
                        s.session_result(
                            find(step["auth"].as_str().unwrap()),
                            &sel,
                            &outcome(op),
                            &policy,
                            None,
                            now,
                        );
                    }
                }
            }
        }
    }

    /// The failure of a request picked before a policy change removes nothing bound
    /// after it, though the change replaced the matcher.
    #[test]
    fn delayed_failure_keeps_binding_made_after_policy_change() {
        let a = cred("a", serde_json::json!({}));
        let p = Policy {
            session_affinity: true,
            ..Default::default()
        };
        let next = Policy {
            session_affinity_ttl: Duration::from_secs(600),
            ..p.clone()
        };
        let now = Instant::now();
        let mut s = Scheduler::default();
        let body = br#"{"messages":[{"role":"user","content":"hello"}]}"#;
        let request = crate::lcp::Request::new("openai", body, "client-key").unwrap();
        let sel = Selection {
            lcp: Some(std::sync::Arc::new(request.clone())),
            ..selection("m")
        };
        let ranks = |_: &Credential| Windows::default();
        let mut old = None;
        for _ in 0..5 {
            old = s.pick_session(&tag(&[&a]), &sel, &p, &ranks, now).unwrap().1;
        }
        let old = old.unwrap();
        s.configure(&p, &next);
        let fresh = s
            .pick_session(&tag(&[&a]), &sel, &next, &ranks, now)
            .unwrap()
            .1
            .unwrap();
        assert!(fresh.access > old.access, "{} after {}", fresh.access, old.access);
        let failure = Outcome::Failure(ExecError::local(500, FailureScope::Credential, "boom"));
        s.session_result(&a, &sel, &failure, &p, Some(&old), now);
        let namespace = request.namespace("m");
        let kept = s.lcp.find(&namespace, request.prepared(), now);
        assert_eq!(kept.map(|m| m.auth), Some("a".to_owned()));
    }

    /// After session affinity is turned off, a request picked while it was on binds
    /// nothing: neither its result nor a retry under its policy.
    #[test]
    fn leases_from_before_disabling_affinity_bind_nothing() {
        let a = cred("a", serde_json::json!({}));
        let on = Policy {
            session_affinity: true,
            ..Default::default()
        };
        let off = Policy::default();
        let now = Instant::now();
        let mut s = Scheduler::default();
        s.configure(&off, &on);
        let body = br#"{"messages":[{"role":"user","content":"hello"}]}"#;
        let request = crate::lcp::Request::new("openai", body, "client-key").unwrap();
        let namespace = request.namespace("m");
        let lcp = Selection {
            lcp: Some(std::sync::Arc::new(request.clone())),
            ..selection("m")
        };
        let explicit = Selection {
            session: Some("s1".into()),
            ..selection("m")
        };
        let ranks = |_: &Credential| Windows::default();
        let bound = s.pick_session(&tag(&[&a]), &lcp, &on, &ranks, now).unwrap().1;
        assert!(bound.is_some());
        s.configure(&on, &off);
        assert!(s.lcp.find(&namespace, request.prepared(), now).is_none());
        s.session_result(&a, &lcp, &Outcome::Success, &on, bound.as_ref(), now);
        s.session_result(&a, &explicit, &Outcome::Success, &on, None, now);
        let retry = s.pick_session(&tag(&[&a]), &lcp, &on, &ranks, now).unwrap().1;
        assert!(retry.is_none());
        s.pick_session(&tag(&[&a]), &explicit, &on, &ranks, now);
        assert!(s.lcp.find(&namespace, request.prepared(), now).is_none());
        assert!(s.affinity.get(&session_keys(&explicit).unwrap().primary, now).is_none());
        // Turning it back on binds again.
        s.configure(&off, &on);
        assert!(s.pick_session(&tag(&[&a]), &lcp, &on, &ranks, now).unwrap().1.is_some());
    }

    #[test]
    fn policy_change_resets_rotation_and_affinity_but_keeps_cooldowns() {
        let a = cred("a", serde_json::json!({}));
        let b = cred("b", serde_json::json!({}));
        let p = Policy {
            session_affinity: true,
            ..Default::default()
        };
        let now = Instant::now();
        let mut s = Scheduler::default();
        let mut selection = selection("m");
        selection.session = Some("session".into());
        assert_eq!(s.pick(&tag(&[&a, &b]), &selection, &p, now).unwrap().id, "a");
        s.record(
            &a,
            "other",
            &Outcome::Failure(ExecError::local(429, FailureScope::Model, "quota")),
            &p,
            now,
        );
        let mut next = p.clone();
        next.request_retry = 2;
        s.configure(&p, &next);
        assert!(!s.rotations.is_empty(), "retry-only change preserves selector state");
        assert!(!s.affinity.is_empty());
        next.session_affinity_ttl = Duration::from_secs(5);
        s.configure(&p, &next);
        assert!(s.rotations.is_empty());
        assert!(s.affinity.is_empty());
        assert_eq!(s.wait(&a, "other", now), Some(Duration::from_secs(1)));
    }

    #[test]
    fn forced_cooling_uses_fallback_and_quota_does_not_reuse_transient_state() {
        let now = Instant::now();
        // Go: a forced cooldown re-enables the normal policy despite disable_cooling
        // (conductor_cooldown.go MarkResult), so the status decides the duration.
        for (status, seconds) in [(401, 1800), (404, 43200), (429, 1), (503, 60)] {
            let c = cred(
                "a",
                serde_json::json!({"disable_cooling":true,
                "request_scoped_errors":[{"status":status,"match":["forced"],"action":"stop-and-cooldown"}]}),
            );
            let mut s = Scheduler::default();
            s.record(
                &c,
                "m",
                &Outcome::Failure(ExecError::local(status, FailureScope::Credential, "forced")),
                &Policy::default(),
                now,
            );
            assert_eq!(
                s.wait(&c, "m", now),
                Some(Duration::from_secs(seconds)),
                "status {status}"
            );
        }
        // With transient cooldowns disabled, the forced cooldown falls back to one minute.
        let c = cred(
            "a",
            serde_json::json!({"request_scoped_errors":[{"status":503,"match":["forced"],"action":"continue-and-cooldown"}]}),
        );
        let no_transient = Policy {
            transient_error_cooldown_seconds: -1,
            ..Policy::default()
        };
        let mut s = Scheduler::default();
        let forced = Outcome::Failure(ExecError::local(503, FailureScope::Credential, "forced"));
        s.record(&c, "m", &forced, &no_transient, now);
        assert_eq!(s.wait(&c, "m", now), Some(Duration::from_secs(60)));
        let plain = Outcome::Failure(ExecError::local(503, FailureScope::Credential, "plain"));
        let mut s = Scheduler::default();
        s.record(&c, "m", &plain, &no_transient, now);
        assert_eq!(
            s.wait(&c, "m", now),
            None,
            "negative transient seconds disable 503 cooldowns"
        );
        let c = cred("a", serde_json::json!({}));
        let p = Policy::default();
        let mut s = Scheduler::default();
        s.record(
            &c,
            "m",
            &Outcome::Failure(ExecError::local(503, FailureScope::Model, "transient")),
            &p,
            now,
        );
        s.record(
            &c,
            "m",
            &Outcome::Failure(ExecError::local(429, FailureScope::Model, "quota")),
            &p,
            now,
        );
        assert!(
            s.quota_cooling(&c, "m", now),
            "quota must replace transient classification"
        );
        assert_eq!(
            s.wait(&c, "m", now),
            Some(Duration::from_secs(60)),
            "live deadline never shortened"
        );
        let mut hint = ExecError::local(418, FailureScope::Model, "other status");
        hint.retry_after = Some(Duration::from_secs(900));
        s.record(&c, "other", &Outcome::Failure(hint), &p, now);
        assert_eq!(
            s.wait(&c, "other", now),
            Some(Duration::from_secs(60)),
            "unlisted statuses ignore hint"
        );
    }

    /// Go matches openai-compatibility groups by `EqualFold` against the credential's
    /// compat name, provider key and provider (`resolveOpenAICompatConfig`), where σ and ς
    /// are equal. Group `ΟΣ` now registers `openai-compatible-οσ` (Go `strings.ToLower`),
    /// and the earlier group `openai-compatible-ος` still supplies its proxy and its
    /// disable-cooling setting.
    #[test]
    fn compat_groups_match_with_go_equal_fold() {
        let cfg = cpa_core::config::Config::parse(
            "openai-compatibility:\n  - name: openai-compatible-ος\n    base-url: http://127.0.0.1:9\n    disable-cooling: true\n    api-key-entries: [{api-key: k, proxy-url: 'http://127.0.0.1:8080'}]\n  - name: ΟΣ\n    base-url: http://127.0.0.1:9\n    api-key-entries: [{api-key: k}]\n",
        )
        .unwrap();
        let creds = cpa_core::config::credentials::from_config(&cfg);
        let c = creds
            .iter()
            .find(|c| c.attributes.get("compat_name").is_some_and(|n| n == "ΟΣ"))
            .unwrap();
        assert_eq!(cpa_core::registry::dynamic::provider_key(c), "openai-compatible-οσ");
        assert_eq!(
            cpa_core::config::credentials::api_key_config_proxy(&cfg, c),
            "http://127.0.0.1:8080"
        );
        let mut policy = crate::management::policy(&cfg);
        policy.compat_disable_cooling = compat_cooling(&cfg);
        assert_eq!(policy.provider_cooling(c), Some(true));
    }

    /// OAuth request-scoped-error channels and the credential provider they are looked up
    /// by are both Go `strings.ToLower` (U+0130 becomes a plain `i`): rules keyed `HİGH`
    /// apply to a `"type":"HİGH"` file credential.
    #[test]
    fn oauth_rule_channels_lowercase_like_go() {
        let cfg = cpa_core::config::Config::parse(
            "oauth:\n  request-scoped-errors:\n    HİGH:\n      - {status: 503, match: [boom], action: stop}\n",
        )
        .unwrap();
        let c = cpa_core::config::credentials::from_file(
            &cfg,
            std::path::Path::new("/auth"),
            std::path::Path::new("/auth/a.json"),
            r#"{"type":"HİGH","access_token":"t"}"#.as_bytes(),
        )
        .unwrap()
        .unwrap();
        let action =
            crate::management::policy(&cfg).error_action(&c, &ExecError::local(503, FailureScope::Request, "boom"));
        assert!(action.matched && action.stop);
    }

    /// Go matches rules against a local missing-thread error's structured body, which
    /// names `not_found_error`; its message does not.
    #[test]
    fn rules_match_the_structured_body_of_a_missing_thread() {
        let c = cred(
            "a",
            serde_json::json!({"request_scoped_errors":[{"status":404,"match":["not_found_error"],"action":"continue"}]}),
        );
        let e = ExecError::local(404, FailureScope::Request, cpa_exec::claude::THREAD_NOT_FOUND);
        let action = Policy::default().error_action(&c, &e);
        assert!(action.matched && !action.stop);
        // Any other 404 is matched by its text, including an upstream answer with the
        // same words.
        let other = ExecError::local(404, FailureScope::Request, "not found");
        assert!(!Policy::default().error_action(&c, &other).matched);
        let mut upstream = e.clone();
        upstream
            .headers
            .insert(axum::http::header::CONTENT_TYPE, "text/plain".parse().unwrap());
        assert!(!Policy::default().error_action(&c, &upstream).matched);
    }

    #[test]
    fn rules_are_ordered_status_and_body_matches_with_canonical_precedence() {
        let c = cred(
            "a",
            serde_json::json!({"disable_cooling":true, "request_scoped_errors":[
            {"status":429,"match":[],"action":"stop"},
            {"status":500,"match":["quota"],"action":"stop"},
            {"status":429,"match-regexr":["[", "quota [0-9]+"],"action":" CONTINUE-AND-COOLDOWN "},
            {"status":429,"match":["quota"],"action":"stop"}]}),
        );
        let p = Policy::default();
        let e = ExecError::local(429, FailureScope::Request, "quota 12");
        let action = p.error_action(&c, &e);
        assert!(!action.stop);
        assert!(action.cooldown && action.force_cooldown);
        let now = Instant::now();
        let mut s = Scheduler::default();
        s.record(&c, "m", &Outcome::Failure(e), &p, now);
        assert!(
            s.wait(&c, "m", now).is_some(),
            "force cooling bypasses disabled cooling"
        );
        let c = cred(
            "a",
            serde_json::json!({"request_retry":0, "request-retry":5,
            "disable_cooling":false,"disable-cooling":true}),
        );
        assert_eq!(p.retry_limit(&c), 0);
        assert!(!p.cooling_disabled(&c));
        let legacy = cred(
            "legacy",
            serde_json::json!({"request_retry":null,"request-retry":2,
            "disable_cooling":"TrUe","disable-cooling":1}),
        );
        assert_eq!(
            p.retry_limit(&legacy),
            2,
            "invalid canonical override permits legacy fallback"
        );
        assert!(
            p.cooling_disabled(&legacy),
            "Go ParseBool rejects mixed case; numeric legacy accepted"
        );
        assert!(!p.cooling_disabled(&cred("numeric", serde_json::json!({"disable_cooling":0}))));
        assert_eq!(
            Policy {
                request_retry: 3,
                ..Default::default()
            }
            .retry_limit(&cred("b", serde_json::json!({"request_retry":"-1"}))),
            3
        );
        // Go `providerCoolingOverrideForAuth`: the first enabled compat entry named by
        // compat_name, provider_key or provider; credential metadata still wins.
        let cfg = cpa_core::config::Config::parse(
            "openai-compatibility:\n  - {name: skipped, base-url: '', disable-cooling: false}\n  - {name: Router, base-url: http://r.invalid, disable-cooling: true}\n  - {name: plain, base-url: http://p.invalid}\n",
        )
        .unwrap();
        let mut p = Policy {
            compat_disable_cooling: compat_cooling(&cfg),
            ..Default::default()
        };
        assert_eq!(
            p.compat_disable_cooling,
            [("Router".to_owned(), Some(true)), ("plain".to_owned(), None)],
            "legacy openai-compatibility is read through the v8 document"
        );
        let compat = |attrs: &[(&str, &str)], meta: Value| {
            let mut c = cred("compat", meta);
            c.provider = "openai-compatibility".into();
            for (k, v) in attrs {
                c.attributes.insert((*k).into(), (*v).into());
            }
            c
        };
        assert!(p.cooling_disabled(&compat(&[("compat_name", "router")], serde_json::json!({}))));
        assert!(p.cooling_disabled(&compat(&[("provider_key", "ROUTER")], serde_json::json!({}))));
        assert!(!p.cooling_disabled(&compat(
            &[("compat_name", "router")],
            serde_json::json!({"disable_cooling": false})
        )));
        p.disable_cooling = true;
        assert!(
            p.cooling_disabled(&compat(&[("compat_name", "plain")], serde_json::json!({}))),
            "an entry without disable-cooling defers to global"
        );
        p.disable_cooling = false;
        assert!(
            !p.cooling_disabled(&cred("claude-file", serde_json::json!({}))),
            "credentials outside OpenAI compatibility ignore entries"
        );
        for scope in [FailureScope::Request, FailureScope::Transport] {
            let mut s = Scheduler::default();
            s.record(
                &c,
                "m",
                &Outcome::Failure(ExecError::local(502, scope, "fault")),
                &p,
                now,
            );
            assert_eq!(s.wait(&c, "m", now), None);
        }
    }

    // ---- soonest-reset (cliproxy-rs addition) ----

    const DAY: u64 = 24 * 3600;

    fn epoch(t: SystemTime) -> String {
        t.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs().to_string()
    }

    /// Claude's measured headers observed at `now`: the 7-day reset `weekly` seconds
    /// later and the 5-hour window's status and reset.
    fn claude(now: SystemTime, weekly: u64, five_hour: &str, five_hour_reset: SystemTime) -> Windows {
        claude_at(now, now, weekly, five_hour, five_hour_reset)
    }

    /// [`claude`] observed at `observed` and read at `now`.
    fn claude_at(
        observed: SystemTime,
        now: SystemTime,
        weekly: u64,
        five_hour: &str,
        five_hour_reset: SystemTime,
    ) -> Windows {
        let signals: std::collections::BTreeMap<String, String> = [
            ("Anthropic-Ratelimit-Unified-7d-Status", "allowed".to_owned()),
            ("Anthropic-Ratelimit-Unified-7d-Utilization", "0.40".to_owned()),
            (
                "Anthropic-Ratelimit-Unified-7d-Reset",
                epoch(observed + Duration::from_secs(weekly)),
            ),
            ("Anthropic-Ratelimit-Unified-5h-Status", five_hour.to_owned()),
            ("Anthropic-Ratelimit-Unified-5h-Reset", epoch(five_hour_reset)),
        ]
        .into_iter()
        .map(|(k, v)| (k.to_owned(), v))
        .collect();
        Windows::observed("claude", &signals, observed, now)
    }

    fn soonest() -> Policy {
        Policy {
            strategy: Strategy::SoonestReset,
            ..Default::default()
        }
    }

    /// The asymmetric case: `a` resets in four days and `b` in one, so fill-first (id
    /// order) would take `a` and round-robin would alternate.
    #[test]
    fn soonest_reset_spends_the_earliest_weekly_window_first() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let in_an_hour = wall + Duration::from_secs(3600);
        // Both observed at `wall`, read at `now`; `b`'s 5-hour window in `b_five_hour`.
        let ranks_at = |b_five_hour: &'static str, now: SystemTime| {
            let (wa, wb) = (
                claude_at(wall, now, 4 * DAY, "allowed", in_an_hour),
                claude_at(wall, now, DAY, b_five_hour, in_an_hour),
            );
            move |cred: &Credential| if cred.id == "a" { wa } else { wb }
        };
        let policy = soonest();
        let mut s = Scheduler::default();
        let now = Instant::now();
        let ranks = ranks_at("allowed", wall);
        for _ in 0..3 {
            let picked = s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &policy, &ranks, now);
            assert_eq!(picked.unwrap().id, "b", "the sooner weekly reset is used up first");
        }
        // `b` cools on a 429: the next soonest takes over.
        s.record(
            &b,
            "m",
            &Outcome::Failure(ExecError::local(429, FailureScope::Credential, "quota")),
            &policy,
            now,
        );
        let ready: Vec<&Credential> = [&a, &b].into_iter().filter(|x| s.wait(x, "m", now).is_none()).collect();
        assert_eq!(ready.iter().map(|x| x.id.as_str()).collect::<Vec<_>>(), ["a"]);
        assert_eq!(
            s.pick_ranked(&tag(&ready), &selection("m"), &policy, &ranks, now)
                .unwrap()
                .id,
            "a"
        );
        // Without the cooldown, `b`'s used-up 5-hour window also hands over to `a`...
        let mut s = Scheduler::default();
        let exhausted = ranks_at("rejected", wall);
        for _ in 0..2 {
            assert_eq!(
                s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &policy, &exhausted, now)
                    .unwrap()
                    .id,
                "a"
            );
        }
        // ...until that window resets, an hour later.
        let reset = ranks_at("rejected", in_an_hour + Duration::from_secs(1));
        assert_eq!(
            s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &policy, &reset, now)
                .unwrap()
                .id,
            "b"
        );
    }

    /// Accounts with no known reset get one probe request each, then rank by what they
    /// reported; one that reported nothing comes after the known resets.
    #[test]
    fn soonest_reset_probes_unknown_accounts_once_then_ranks_them() {
        let wall = SystemTime::now();
        let creds: Vec<Credential> = ["a", "b", "c", "d"]
            .iter()
            .map(|id| cred(id, serde_json::json!({})))
            .collect();
        let [a, b, c, d] = [&creds[0], &creds[1], &creds[2], &creds[3]];
        let in_an_hour = wall + Duration::from_secs(3600);
        // `d` resets in 3 days, `c` is used up, `a` and `b` were never observed.
        let known = |x: &Credential| match x.id.as_str() {
            "c" => claude(wall, DAY, "rejected", in_an_hour),
            "d" => claude(wall, 3 * DAY, "allowed", in_an_hour),
            _ => Windows::default(),
        };
        let policy = soonest();
        let mut s = Scheduler::default();
        let now = Instant::now();
        let pick = |s: &mut Scheduler, set: &[&Credential], ranks: &Ranks<'_>| {
            s.pick_ranked(&tag(set), &selection("m"), &policy, ranks, now)
                .unwrap()
                .id
                .clone()
        };
        // One probe each for the unknown accounts, then the known reset.
        let order: Vec<String> = (0..4).map(|_| pick(&mut s, &[a, b, c, d], &known)).collect();
        assert_eq!(order, ["a", "b", "d", "d"]);
        // `a`'s probe answered with a reset in 12 hours: it now ranks by it, ahead of
        // `d`; `b`'s answer reported nothing, so it stays behind the known resets.
        let learned = |x: &Credential| match x.id.as_str() {
            "a" => claude(wall + Duration::from_secs(1), 12 * 3600, "allowed", in_an_hour),
            _ => known(x),
        };
        assert_eq!(pick(&mut s, &[a, b, c, d], &learned), "a");
        assert_eq!(pick(&mut s, &[b, c, d], &learned), "d");
        assert_eq!(
            pick(&mut s, &[b, c], &learned),
            "b",
            "probed and unknown, still before used-up"
        );
        assert_eq!(
            pick(&mut s, &[c], &learned),
            "c",
            "a used-up account is the last resort"
        );
        // When `d`'s weekly reset passes, it is unknown again and gets a new probe.
        let passed = |x: &Credential| match x.id.as_str() {
            "d" => claude_at(
                wall,
                wall + Duration::from_secs(3 * DAY + 1),
                3 * DAY,
                "allowed",
                in_an_hour,
            ),
            _ => learned(x),
        };
        assert_eq!(pick(&mut s, &[a, b, d], &passed), "d");
        assert_eq!(pick(&mut s, &[a, b, d], &passed), "a");
    }

    #[test]
    fn soonest_reset_rotates_equal_ranks() {
        let wall = SystemTime::now();
        let creds: Vec<Credential> = ["a", "b"].iter().map(|id| cred(id, serde_json::json!({}))).collect();
        let in_an_hour = wall + Duration::from_secs(3600);
        // The same reset for both.
        let ranks = |_: &Credential| claude(wall, 2 * DAY, "allowed", in_an_hour);
        let mut s = Scheduler::default();
        let now = Instant::now();
        let turns: Vec<String> = (0..4)
            .map(|_| {
                s.pick_ranked(&tag(&[&creds[0], &creds[1]]), &selection("m"), &soonest(), &ranks, now)
                    .unwrap()
                    .id
                    .clone()
            })
            .collect();
        assert_eq!(turns, ["a", "b", "a", "b"]);
    }

    #[test]
    fn soonest_reset_never_moves_a_session_pin() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let in_an_hour = wall + Duration::from_secs(3600);
        let ranks = |x: &Credential| claude(wall, if x.id == "a" { 4 * DAY } else { DAY }, "allowed", in_an_hour);
        let policy = Policy {
            session_affinity: true,
            ..soonest()
        };
        let mut s = Scheduler::default();
        let now = Instant::now();
        let mut pinned = selection("m");
        pinned.session = Some("claude:thread-1".into());
        // The thread starts while only `a` is available, and keeps `a` afterwards.
        assert_eq!(
            s.pick_ranked(&tag(&[&a]), &pinned, &policy, &ranks, now).unwrap().id,
            "a"
        );
        for _ in 0..2 {
            assert_eq!(
                s.pick_ranked(&tag(&[&a, &b]), &pinned, &policy, &ranks, now)
                    .unwrap()
                    .id,
                "a"
            );
        }
        // A new thread starts on the soonest reset.
        let mut other = selection("m");
        other.session = Some("claude:thread-2".into());
        assert_eq!(
            s.pick_ranked(&tag(&[&a, &b]), &other, &policy, &ranks, now).unwrap().id,
            "b"
        );
    }

    #[test]
    fn default_strategy_stays_round_robin_and_ignores_resets() {
        assert_eq!(Policy::default().strategy, Strategy::RoundRobin);
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let ranks = |x: &Credential| claude(wall, if x.id == "a" { 4 * DAY } else { DAY }, "allowed", wall);
        let mut s = Scheduler::default();
        let now = Instant::now();
        let turns: Vec<String> = (0..4)
            .map(|_| {
                s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &Policy::default(), &ranks, now)
                    .unwrap()
                    .id
                    .clone()
            })
            .collect();
        assert_eq!(turns, ["a", "b", "a", "b"]);
    }

    #[test]
    fn soonest_reset_in_mixed_providers_stays_in_the_first_group() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let x = cred("x", serde_json::json!({}));
        let in_an_hour = wall + Duration::from_secs(3600);
        // The second provider's `x` resets soonest, but fill-first order picks the group.
        let ranks = |c: &Credential| {
            let days = match c.id.as_str() {
                "a" => 4,
                "b" => 2,
                _ => 1,
            };
            claude(wall, days * DAY, "allowed", in_an_hour)
        };
        let sel = Selection {
            providers: vec!["claude".into(), "codex".into()],
            model: "m".into(),
            ..Default::default()
        };
        let candidates = [(&a, "claude"), (&b, "claude"), (&x, "codex")];
        let mut s = Scheduler::default();
        let picked = s.pick_ranked(&candidates, &sel, &soonest(), &ranks, Instant::now());
        assert_eq!(picked.unwrap().id, "b");
    }

    #[test]
    fn windows_read_claude_and_codex_signals() {
        let now = SystemTime::now();
        let map = |pairs: &[(&str, String)]| -> std::collections::BTreeMap<String, String> {
            pairs.iter().map(|(k, v)| ((*k).to_owned(), v.clone())).collect()
        };
        // A weekly reset already behind is unknown, not "soonest".
        let stale = map(&[(
            "Anthropic-Ratelimit-Unified-7d-Reset",
            epoch(now - Duration::from_secs(60)),
        )]);
        let w = Windows::observed("claude", &stale, now, now);
        assert_eq!(
            (
                w.weekly_reset,
                w.rolled_over.map(|r| (r.window, epoch(r.at))),
                w.exhausted
            ),
            (
                None,
                Some((WindowId::Claude7d, epoch(now - Duration::from_secs(60)))),
                false
            )
        );
        // Utilization 1.0 exhausts the 5-hour window until its reset.
        let full = map(&[
            ("Anthropic-Ratelimit-Unified-5h-Utilization", "1.0".into()),
            (
                "Anthropic-Ratelimit-Unified-5h-Reset",
                epoch(now + Duration::from_secs(600)),
            ),
        ]);
        assert!(Windows::observed("claude", &full, now, now).exhausted);
        assert!(!Windows::observed("claude", &full, now, now + Duration::from_secs(601)).exhausted);
        // Codex: the longer window is weekly whichever header carries it; reset-after
        // counts from the observation.
        let observed = now - Duration::from_secs(100);
        let codex = map(&[
            ("X-Codex-Primary-Used-Percent", "100".into()),
            ("X-Codex-Primary-Window-Minutes", "300".into()),
            ("X-Codex-Primary-Reset-After-Seconds", "3600".into()),
            ("X-Codex-Secondary-Used-Percent", "40".into()),
            ("X-Codex-Secondary-Window-Minutes", "10080".into()),
            ("X-Codex-Secondary-Reset-At", epoch(now + Duration::from_secs(2 * DAY))),
        ]);
        let w = Windows::observed("codex", &codex, observed, now);
        assert!(w.exhausted, "the 5-hour primary is used up");
        assert_eq!(
            w.weekly_reset.map(epoch),
            Some(epoch(now + Duration::from_secs(2 * DAY)))
        );
        assert!(!Windows::observed("codex", &codex, observed, observed + Duration::from_secs(3601)).exhausted);
        let weekly_primary = map(&[
            ("X-Codex-Primary-Used-Percent", "51".into()),
            ("X-Codex-Primary-Window-Minutes", "10080".into()),
            ("X-Codex-Primary-Reset-At", epoch(now + Duration::from_secs(3 * DAY))),
        ]);
        let w = Windows::observed("codex", &weekly_primary, now, now);
        assert_eq!(
            (w.weekly_reset.map(epoch), w.exhausted),
            (Some(epoch(now + Duration::from_secs(3 * DAY))), false)
        );
        let reached = map(&[("X-Codex-Limit-Reached", "True".into())]);
        assert!(Windows::observed("codex", &reached, now, now).exhausted);
        // Another provider's headers mean nothing here.
        let w = Windows::observed("gemini", &codex, now, now);
        assert_eq!((w.weekly_reset, w.exhausted), (None, false));
    }

    // ---- soonest-reset review fixes ----

    fn signals(pairs: &[(&str, String)]) -> std::collections::BTreeMap<String, String> {
        pairs.iter().map(|(k, v)| ((*k).to_owned(), v.clone())).collect()
    }

    /// P1: an account answering with quota signals but no weekly reset is probed once;
    /// each new answer (a newer observation) must not re-arm the probe, or it would take
    /// every request ahead of an account with a known reset.
    #[test]
    fn soonest_reset_answers_without_a_reset_do_not_re_arm_the_probe() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let policy = soonest();
        let mut s = Scheduler::default();
        let now = Instant::now();
        let b_known = claude(wall, 2 * DAY, "allowed", wall + Duration::from_secs(3600));
        let mut picks = Vec::new();
        for i in 0..5u64 {
            // `a` has answered `i` times, each time with a fresh 5-hour status only.
            let a_seen = (i > 0).then(|| {
                let seen = wall + Duration::from_secs(i);
                Windows::observed(
                    "claude",
                    &signals(&[("Anthropic-Ratelimit-Unified-5h-Status", "allowed".into())]),
                    seen,
                    seen,
                )
            });
            let ranks = |x: &Credential| {
                if x.id == "a" {
                    a_seen.unwrap_or_default()
                } else {
                    b_known
                }
            };
            picks.push(
                s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &policy, &ranks, now)
                    .unwrap()
                    .id
                    .clone(),
            );
        }
        assert_eq!(picks, ["a", "b", "b", "b", "b"]);
    }

    /// The probe is reserved when picked: one that fails before any header arrives (no
    /// new observation) is spent, so the next picks go to the known reset.
    #[test]
    fn soonest_reset_a_probe_that_fails_before_headers_is_spent() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let b_known = claude(wall, 2 * DAY, "allowed", wall + Duration::from_secs(3600));
        let ranks = |x: &Credential| if x.id == "a" { Windows::default() } else { b_known };
        let mut s = Scheduler::default();
        let now = Instant::now();
        let mut pick = || {
            s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &soonest(), &ranks, now)
                .unwrap()
                .id
                .clone()
        };
        assert_eq!(pick(), "a", "the probe");
        assert_eq!([pick(), pick()], ["b", "b"]);
    }

    /// A known reset that passes rolls the window over and re-arms exactly one probe.
    #[test]
    fn soonest_reset_re_arms_only_on_a_rollover() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let b_known = claude(wall, 2 * DAY, "allowed", wall + Duration::from_secs(3600));
        let a_reset = wall + Duration::from_secs(60);
        // `a` reported a weekly reset in 60 s; read 61 s later it has rolled over.
        let a_rolled = |seen: SystemTime| {
            Windows::observed(
                "claude",
                &signals(&[("Anthropic-Ratelimit-Unified-7d-Reset", epoch(a_reset))]),
                seen,
                wall + Duration::from_secs(61),
            )
        };
        let mut s = Scheduler::default();
        let now = Instant::now();
        let mut picks = Vec::new();
        for i in 0..3u64 {
            let a_now = a_rolled(wall + Duration::from_secs(i));
            let ranks = |x: &Credential| if x.id == "a" { a_now } else { b_known };
            picks.push(
                s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &soonest(), &ranks, now)
                    .unwrap()
                    .id
                    .clone(),
            );
        }
        assert_eq!(picks, ["a", "b", "b"], "one probe per rollover");
        // The next weekly rollover re-arms it once more.
        let next = a_reset + Duration::from_secs(7 * DAY);
        let later = Windows::observed(
            "claude",
            &signals(&[("Anthropic-Ratelimit-Unified-7d-Reset", epoch(next))]),
            wall,
            next + Duration::from_secs(1),
        );
        let ranks = |x: &Credential| if x.id == "a" { later } else { b_known };
        let mut pick = || {
            s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &soonest(), &ranks, now)
                .unwrap()
                .id
                .clone()
        };
        assert_eq!([pick(), pick()], ["a", "b"]);
    }

    /// Codex answers with `Reset-After-Seconds: 0` and no `Reset-At`: the reset is the
    /// observation time itself, which must not count as a new rollover on every answer.
    #[test]
    fn soonest_reset_zero_relative_resets_do_not_re_arm_the_probe() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let b_known = claude(wall, 2 * DAY, "allowed", wall + Duration::from_secs(3600));
        let zero = signals(&[
            ("X-Codex-Secondary-Used-Percent", "0".into()),
            ("X-Codex-Secondary-Window-Minutes", "10080".into()),
            ("X-Codex-Secondary-Reset-After-Seconds", "0".into()),
        ]);
        let mut s = Scheduler::default();
        let now = Instant::now();
        let mut picks = Vec::new();
        for i in 0..4u64 {
            // `a` has answered `i` times, each identical answer observed a second later
            // and read a second after that.
            let a_now = (i > 0).then(|| {
                let seen = wall + Duration::from_secs(2 * i);
                Windows::observed("codex", &zero, seen, seen + Duration::from_secs(1))
            });
            let ranks = |x: &Credential| {
                if x.id == "a" {
                    a_now.unwrap_or_default()
                } else {
                    b_known
                }
            };
            picks.push(
                s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &soonest(), &ranks, now)
                    .unwrap()
                    .id
                    .clone(),
            );
        }
        assert_eq!(picks, ["a", "b", "b", "b"]);
        // The zero delay still ends a used-up window now.
        let used_up = signals(&[
            ("X-Codex-Primary-Used-Percent", "100".into()),
            ("X-Codex-Primary-Reset-After-Seconds", "0".into()),
        ]);
        assert!(!Windows::observed("codex", &used_up, wall, wall).exhausted);
    }

    /// Relative resets read around a boundary differ by rounding; rollovers less than
    /// half a window apart are the same one, so they never re-arm the probe twice.
    #[test]
    fn soonest_reset_rollovers_within_half_a_window_are_one_rollover() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let b_known = claude(wall, 2 * DAY, "allowed", wall + Duration::from_secs(3600));
        // `a` reports its weekly reset `after` seconds from an observation at `seen`.
        let codex = |seen: SystemTime, after: u64, read: SystemTime| {
            Windows::observed(
                "codex",
                &signals(&[
                    ("X-Codex-Secondary-Window-Minutes", "10080".into()),
                    ("X-Codex-Secondary-Reset-After-Seconds", after.to_string()),
                ]),
                seen,
                read,
            )
        };
        let mut s = Scheduler::default();
        let now = Instant::now();
        let mut pick = |a_now: Windows| {
            let ranks = |x: &Credential| if x.id == "a" { a_now } else { b_known };
            s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &soonest(), &ranks, now)
                .unwrap()
                .id
                .clone()
        };
        let boundary = wall + Duration::from_secs(600);
        // Reset reached: probed once.
        assert_eq!(pick(codex(wall, 600, boundary + Duration::from_secs(1))), "a");
        // Lagging answers keep reporting one second left, each from a later observation.
        for i in 1..4u64 {
            let seen = boundary + Duration::from_secs(i);
            assert_eq!(pick(codex(seen, 1, seen + Duration::from_secs(2))), "b", "answer {i}");
        }
        // A week later the window really rolls over again: one more probe.
        let next = boundary + Duration::from_secs(7 * DAY);
        assert_eq!(
            pick(codex(next - Duration::from_secs(5), 5, next + Duration::from_secs(1))),
            "a"
        );
        assert_eq!(
            pick(codex(next - Duration::from_secs(5), 5, next + Duration::from_secs(1))),
            "b"
        );
    }

    /// The oracle's sequence: a probe made for one window's rollover must not suppress a
    /// later rollover of another window.
    #[test]
    fn soonest_reset_probe_markers_are_kept_per_window() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let b_known = claude(wall, 5 * DAY, "allowed", wall + Duration::from_secs(3600));
        let r = wall + Duration::from_secs(600);
        let after = |t: SystemTime, secs: u64| t + Duration::from_secs(secs);
        // 1. Only `Secondary-Reset-At: R` and no lengths: secondary is taken as weekly.
        let partial = signals(&[("X-Codex-Secondary-Reset-At", epoch(r))]);
        // 2. The probe's answer is complete: the weekly window is primary, resetting at
        //    R + 1 day; secondary is the 5-hour window.
        let complete = signals(&[
            ("X-Codex-Primary-Used-Percent", "10".into()),
            ("X-Codex-Primary-Window-Minutes", "10080".into()),
            ("X-Codex-Primary-Reset-At", epoch(after(r, DAY))),
            ("X-Codex-Secondary-Used-Percent", "10".into()),
            ("X-Codex-Secondary-Window-Minutes", "300".into()),
            ("X-Codex-Secondary-Reset-At", epoch(after(r, 5 * 3600))),
        ]);
        let mut s = Scheduler::default();
        let now = Instant::now();
        let mut pick = |a_now: Windows| {
            let ranks = |x: &Credential| if x.id == "a" { a_now } else { b_known };
            s.pick_ranked(&tag(&[&a, &b]), &selection("m"), &soonest(), &ranks, now)
                .unwrap()
                .id
                .clone()
        };
        // Just after R, `a`'s secondary rolled over: its probe.
        let w = Windows::observed("codex", &partial, wall, after(r, 1));
        assert_eq!(w.rolled_over.map(|r| r.window), Some(WindowId::CodexSecondary));
        assert_eq!(pick(w), "a");
        // The answer reports primary resetting in a day, sooner than `b`: `a` keeps it.
        assert_eq!(
            pick(Windows::observed("codex", &complete, after(r, 1), after(r, 2))),
            "a"
        );
        // 3. Just after R + 1 day, primary rolls over: one probe, not suppressed by the
        //    secondary's marker R (R + 1 day is within half of primary's week of R).
        let w = Windows::observed("codex", &complete, after(r, 1), after(r, DAY + 1));
        assert_eq!(w.rolled_over.map(|r| r.window), Some(WindowId::CodexPrimary));
        assert_eq!(pick(w), "a");
        assert_eq!(pick(w), "b", "probed for that rollover");
    }

    /// P2: a management reset clears the probe record (the account is probed again) and
    /// leaves session affinity alone.
    #[test]
    fn soonest_reset_management_reset_re_arms_the_probe_and_keeps_affinity() {
        let wall = SystemTime::now();
        let (a, b) = (cred("a", serde_json::json!({})), cred("b", serde_json::json!({})));
        let b_known = claude(wall, 2 * DAY, "allowed", wall + Duration::from_secs(3600));
        let ranks = |x: &Credential| if x.id == "a" { Windows::default() } else { b_known };
        let policy = Policy {
            session_affinity: true,
            ..soonest()
        };
        let mut s = Scheduler::default();
        let now = Instant::now();
        let mut thread = selection("m");
        thread.session = Some("claude:thread-1".into());
        let mut pick = |sel: &Selection, set: &[&Credential]| {
            s.pick_ranked(&tag(set), sel, &policy, &ranks, now).unwrap().id.clone()
        };
        // The thread starts on `a` while it is the only account: that was `a`'s probe.
        assert_eq!(pick(&thread, &[&a]), "a");
        let fresh = selection("m");
        assert_eq!(pick(&fresh, &[&a, &b]), "b");
        s.reset_cooldowns("a");
        let mut pick = |sel: &Selection, set: &[&Credential]| {
            s.pick_ranked(&tag(set), sel, &policy, &ranks, now).unwrap().id.clone()
        };
        assert_eq!(pick(&fresh, &[&a, &b]), "a", "probed again");
        assert_eq!(pick(&fresh, &[&a, &b]), "b");
        assert_eq!(pick(&thread, &[&a, &b]), "a", "the pin is kept");
    }

    /// P2: an oversized finite window length neither panics nor holds a used-up window
    /// past the missing-length policy (five hours).
    #[test]
    fn windows_survive_unrepresentable_window_lengths() {
        let now = SystemTime::now();
        for (used, exhausted) in [("0", false), ("100", true)] {
            let huge = signals(&[
                ("X-Codex-Primary-Used-Percent", used.into()),
                ("X-Codex-Primary-Window-Minutes", "1e308".into()),
            ]);
            assert_eq!(
                Windows::observed("codex", &huge, now, now).exhausted,
                exhausted,
                "{used}%"
            );
            let later = now + Duration::from_secs(5 * 3600 + 1);
            assert!(
                !Windows::observed("codex", &huge, now, later).exhausted,
                "{used}% after five hours"
            );
        }
        // Long but representable lengths still count.
        let long = signals(&[
            ("X-Codex-Primary-Used-Percent", "100".into()),
            ("X-Codex-Primary-Window-Minutes", "1e9".into()),
        ]);
        assert!(Windows::observed("codex", &long, now, now + Duration::from_secs(DAY)).exhausted);
    }

    /// P2: `X-Codex-Limit-Reached` ends with the used-up window's own reset; the
    /// five-hour fallback applies only when no window reports a reset.
    #[test]
    fn codex_limit_reached_expires_with_the_window_reset() {
        let now = SystemTime::now();
        let flagged = signals(&[
            ("X-Codex-Limit-Reached", "true".into()),
            ("X-Codex-Primary-Used-Percent", "100".into()),
            ("X-Codex-Primary-Window-Minutes", "300".into()),
            ("X-Codex-Primary-Reset-At", epoch(now + Duration::from_secs(60))),
        ]);
        assert!(Windows::observed("codex", &flagged, now, now).exhausted);
        assert!(!Windows::observed("codex", &flagged, now, now + Duration::from_secs(61)).exhausted);
        // No window used up: the flag holds until the soonest reported reset.
        let below = signals(&[
            ("X-Codex-Limit-Reached", "true".into()),
            ("X-Codex-Primary-Used-Percent", "40".into()),
            ("X-Codex-Primary-Reset-At", epoch(now + Duration::from_secs(600))),
        ]);
        assert!(Windows::observed("codex", &below, now, now + Duration::from_secs(599)).exhausted);
        assert!(!Windows::observed("codex", &below, now, now + Duration::from_secs(601)).exhausted);
        // No reset anywhere: five hours.
        let bare = signals(&[("X-Codex-Limit-Reached", "true".into())]);
        assert!(Windows::observed("codex", &bare, now, now + Duration::from_secs(5 * 3600 - 1)).exhausted);
        assert!(!Windows::observed("codex", &bare, now, now + Duration::from_secs(5 * 3600 + 1)).exhausted);
    }

    /// P2: Codex usage, reset and length are parsed independently, so a window whose
    /// usage is missing still reports its reset.
    #[test]
    fn codex_windows_parse_usage_reset_and_length_independently() {
        let now = SystemTime::now();
        let weekly = now + Duration::from_secs(3 * DAY);
        let no_usage = signals(&[
            ("X-Codex-Secondary-Window-Minutes", "10080".into()),
            ("X-Codex-Secondary-Reset-At", epoch(weekly)),
        ]);
        let w = Windows::observed("codex", &no_usage, now, now);
        assert_eq!((w.weekly_reset.map(epoch), w.exhausted), (Some(epoch(weekly)), false));
        // Reset only, no usage or length: secondary is weekly.
        let reset_only = signals(&[("X-Codex-Secondary-Reset-After-Seconds", "7200".into())]);
        let w = Windows::observed("codex", &reset_only, now, now);
        assert_eq!(w.weekly_reset.map(epoch), Some(epoch(now + Duration::from_secs(7200))));
    }

    /// Both Codex windows, the primary the longer one: the primary is weekly.
    #[test]
    fn codex_primary_longer_than_secondary_is_the_weekly_window() {
        let now = SystemTime::now();
        let weekly = now + Duration::from_secs(3 * DAY);
        let both = signals(&[
            ("X-Codex-Primary-Used-Percent", "51".into()),
            ("X-Codex-Primary-Window-Minutes", "10080".into()),
            ("X-Codex-Primary-Reset-At", epoch(weekly)),
            ("X-Codex-Secondary-Used-Percent", "100".into()),
            ("X-Codex-Secondary-Window-Minutes", "300".into()),
            ("X-Codex-Secondary-Reset-After-Seconds", "3600".into()),
        ]);
        let w = Windows::observed("codex", &both, now, now);
        assert_eq!((w.weekly_reset.map(epoch), w.exhausted), (Some(epoch(weekly)), true));
        let w = Windows::observed("codex", &both, now, now + Duration::from_secs(3601));
        assert_eq!((w.weekly_reset.map(epoch), w.exhausted), (Some(epoch(weekly)), false));
    }
}