hf2q 0.1.1

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
//! Scheduler trait + FIFO adapter + InflightBatched FSM
//! (ADR-040 Phase B iter-1 + iter-1.5 + iter-B3 + iter-2.5 + iter-C2.5 +
//! **iter-A5**).
//!
//! # iter-A5 (this commit) — per-slot OOM + budget enforcement
//!
//! ADR-040 §3.5 — "`kv_cache_budget_bytes` (existing field on
//! `Engine::spawn`) divides equally across slots in SeparateSlots layout.
//! Per-slot budget = `total / max_slots`. Per-slot OOM returns 429 to the
//! admitting handler (Decision #19 contract preserved)."
//!
//! iter-A5 implements the **admit-time** half of this contract. The
//! complementary append-time half (defense-in-depth at the per-model
//! `MultiSeqKvCache::append_for_seq` site) is deliberately NOT shipped:
//! today's per-model impls (Qwen35 `HybridKvCache`, Gemma 4
//! `MultiSeqHbKvBuffers`) pre-allocate full `max_seq_len` K/V buffers per
//! slot at `new` / `alloc_hb_kv_for_layer` time, so a per-token append
//! cannot OOM at the buffer layer — the only place OOM can be
//! operator-actionable is before the work starts, at admission.
//!
//! Concretely iter-A5 adds:
//!
//! - [`AdmitRequest::kv_bytes_needed`] — caller-computed byte cost the
//!   request would put on the per-slot KV cache (typically
//!   `(prompt_tokens + max_tokens) * kv_bytes_per_token`).  `0` means
//!   "caller did not compute; do not enforce" — preserves byte-equivalence
//!   for callers that have not yet wired the byte-cost computation
//!   (today: every caller — Phase C2c will wire it).
//! - [`FifoSchedulerAdapter::new_with_kv_budget`] +
//!   [`InflightBatchedScheduler::new_with_kv_budget`] — opt-in
//!   constructors that take the per-slot byte budget. The 0-budget
//!   default reached via `new` (no env, no flag) preserves
//!   byte-equivalence.
//! - [`AdmitError::SlotBudgetExceeded`] — typed admit-time rejection
//!   mapped to HTTP 429 + `Retry-After: 1` upstream (parallel to
//!   `QueueFull`; ADR-040 §3.5 explicitly preserves the Decision #19
//!   wire-level contract).
//!
//! **Why bytes, not tokens** (Step 4 design seam — see ADR-040 §6.1.13
//! for the full closure block): the scheduler tracks BYTES directly,
//! and callers compute `kv_bytes_needed` per-arch.  Two alternatives
//! were considered:
//!
//! 1. Tokens-via-conversion: scheduler stores `per_slot_budget_tokens:
//!    u32`; the worker converts `kv_cache_budget_bytes →
//!    per_slot_budget_tokens` using a per-arch `kv_bytes_per_token`
//!    constant at engine-spawn time.  Rejected because: (a) it bakes
//!    per-arch math into the scheduler (a pure data primitive should
//!    stay arch-agnostic — same reason `SlotId` and `RequestId` are not
//!    arch-typed); (b) `kv_bytes_per_token` varies per layer for hybrid
//!    architectures (Gemma 4: full vs sliding layers have different
//!    head_dim × n_kv; Qwen3.5: full vs linear-attn layers diverge
//!    further) so a single scalar would either under-count (false
//!    accepts) or wildly over-count (false rejects) — neither is
//!    operator-honest.
//! 2. Bytes-direct (chosen): scheduler stores `per_slot_kv_budget_bytes:
//!    u64`; caller passes `kv_bytes_needed: u64` on each `AdmitRequest`.
//!    Caller (Phase C2c+ for per-arch SlotAware workers) computes the
//!    byte cost using the per-arch `KvSpillDescriptor` / per-layer
//!    `head_dim × n_kv × dtype_size × max_seq_len` math the existing
//!    `kv_spill_descriptor.rs` already does.  Scheduler stays arch-
//!    agnostic; the conversion lives at the per-arch seam where it
//!    belongs.
//!
//! Per-arch `kv_bytes_per_token` wiring is **deferred to Phase C2c+**
//! when the SlotAware runtime lands for each arch.  iter-A5 ships the
//! scheduler-side enforcement + the typed error + the schema-side 429
//! mapping; the actual per-arch byte-cost computation lands when the
//! worker arm that needs it (Qwen35 SlotAware iter-C2c, Gemma 4
//! SlotAware iter-C2d) wires `kv_bytes_needed` into the per-request
//! `AdmitRequest`.  Until then, `kv_bytes_needed: 0` ⇒ enforcement
//! disabled ⇒ byte-equivalent to pre-A5.
//!
//! (ADR-040 Phase B iter-1 + iter-1.5 + iter-B3 + iter-2.5 + iter-C2.5.)
//!
//! This module is the **pure data primitive** that ADR-040 Phase C (iter-2)
//! wires into `serve::api::engine::Engine`. It contains *no* engine load,
//! *no* GPU code, *no* `AppState` wiring — those land in Phase C. The
//! pattern mirrors `serve::multi_model` (W74 iter-206): a synthetic-fixture-
//! tested data structure that later iters glue into the live serve path.
//!
//! # What this module does (iter-1 + iter-1.5 + iter-B3 + iter-2.5)
//!
//! - Declares the `Scheduler` trait surface (`admit`, `step`, `release`,
//!   `stats`, `policy`) — see ADR-040 §3.2 + AC-2.
//! - Ships `FifoSchedulerAdapter` — the **byte-equivalent** wrapper of the
//!   existing ADR-005 Phase 2 Decision #2 contract (one in-flight request,
//!   bounded queue, 429 on overflow).
//! - Ships the production `InflightBatchedScheduler` with a real `step()`
//!   FSM body that mirrors llama.cpp's `-cb` (continuous-batching)
//!   admission-during-decode semantics per ADR-040 §3.3.
//! - **iter-2.5 (this commit)** closes 3 CRITICAL + 1 MAJOR adversarial
//!   findings (cfa-iter2.5 #C1/C2/C3/M2) by:
//!   - **C1 — SlotHandle (TOCTOU race fix)**: the driver-callback APIs
//!     (`advance_after_prefill`/`advance_after_decode`/`release`) no longer
//!     key off a raw `SlotId`. They take an opaque `SlotHandle` that carries
//!     both the physical `SlotId` AND a per-slot generation counter. The
//!     scheduler validates generation == current on every callback; a stale
//!     handle (the previous occupant of a recycled slot) is silently dropped
//!     as a no-op. Without this, an auto-release of `SlotId(0)` followed
//!     by an immediate promote of a queued request onto `SlotId(0)` left
//!     in-flight callbacks for the OLD occupant free to mutate the NEW
//!     occupant's state.
//!   - **C2 — RequestId (queued-sentinel collision fix)**: queued requests
//!     no longer share the sentinel `SlotId(u32::MAX)`. They carry a fresh
//!     `RequestId` (a monotonic u64) so `cancel_queued(request_id)` removes
//!     exactly ONE queued request (the previous `release(SlotId(u32::MAX))`
//!     used `retain != SlotId(u32::MAX)` which removed ALL queued requests
//!     — silent data loss).
//!   - **C3 — step() priority (older-prefilling-wins fix)**: after promoting
//!     one queued slot into `in_flight`, `step()` picks the FIRST
//!     `Prefilling` slot in insertion order (FIFO across in_flight). The
//!     previous code special-cased "if I just promoted, emit Mixed for
//!     the promoted slot" which starved an OLDER mid-chunk Prefilling slot.
//!     Now: Mixed/Prefill always uses `first_prefilling_idx()`; the promoted
//!     slot is just the slot that lands at the end of the `in_flight`
//!     vector and waits its turn.
//!   - **M2 — prompt_tokens=0 deadlock fix**: a request admitted with
//!     `prompt_tokens == 0` now transitions DIRECTLY to `Decoding` (skips
//!     `Prefilling` entirely — a zero-token prompt has nothing to prefill).
//!     Without this, `step()` would emit `Prefill { n_tokens: 0 }` and a
//!     driver that treated zero-token prefill as no-work would deadlock.
//!     A request admitted with `max_tokens == 0` is recognized at admit
//!     time as zero-budget: the scheduler returns
//!     `RequestSlot { handle: None, .. }` and bumps `completed_total`
//!     without creating an in-flight slot. (cfa-iter-C2.5 M1: closes the
//!     stuck-Decoding-slot risk Codex flagged — the prior implementation
//!     was a documentation lie: the comment claimed "auto-releases at
//!     admit time" but `initial_phase` still pushed a `Decoding{ tokens_
//!     produced:0, max_tokens:0 }` slot into in_flight, and only the
//!     Embed worker arm's explicit post-prefill `release` avoided the
//!     leak. Now the scheduler enforces it structurally: a zero-budget
//!     admit allocates no physical slot, and the caller observes
//!     `handle.is_none()` and skips the drive loop.)
//! - Pins the prefill chunk-size default at `DEFAULT_PREFILL_CHUNK_TOKENS
//!   = 512` — matches llama.cpp's `-ub` (ubatch) default.
//!
//! # What this module does NOT do (iter-2.5 scope)
//!
//! - Hold an `Engine` (or any `mlx_native` buffers) — `RequestSlot` is a
//!   pure descriptor. Phase C iter-2's `Engine::spawn` will accept an
//!   injected `Box<dyn Scheduler>` and dispatch against this trait.
//! - Touch `serve::api::engine`, `serve::mod::cmd_serve`, or any handler —
//!   wiring is Phase C.
//! - Build paged-KV blocks — ADR-040 §3.1 picks `SeparateSlots` first.
//! - Execute forward passes — `step()` returns a `SchedulerStep`
//!   discriminant describing what the driver loop SHOULD do; the driver
//!   loop (Phase C iter-2) actually calls `forward_prefill` /
//!   `forward_decode` and reports back via `advance_after_*`.
//! - Thread `slot_id` through `forward_prefill.rs` /
//!   `forward_prefill_batched.rs` — Phase B iter-4.
//! - Handle multi-prefill batching in `Mixed` — `Mixed` carries exactly
//!   ONE prefill (the first `Prefilling` slot in FIFO order) batched with
//!   N decode slots.
//!
//! # Backward-compat contract (ADR-040 §3.6)
//!
//! `FifoSchedulerAdapter` MUST behave bit-equivalently to the pre-ADR-040
//! `Engine::spawn` channel + worker thread. Specifically:
//!
//! 1. At most one in-flight request (Decision #2 — `max_slots == 1`).
//! 2. Bounded queue with capacity = `queue_capacity` from `Engine::spawn`
//!    (Decision #19 — channel buffer at `queue_capacity.max(1)`). The
//!    `.max(1)` normalization is mirrored in `FifoSchedulerAdapter::new`.
//! 3. Overflow returns `AdmitError::QueueFull`, which the handler layer
//!    maps to HTTP 429 + `Retry-After: 1`.
//! 4. FIFO ordering preserved: pop order == push order. Physical `SlotId`
//!    returned from `admit()` is **always `SlotId(0)`** for the FIFO
//!    policy (iter-1.5 F3b) — single physical slot reused as queued
//!    requests get promoted. Per-admit identity is now disambiguated via
//!    the new `RequestId` field carried inside `RequestSlot` (iter-2.5 C2).
//!
//! # iter-2.5 adversarial-review findings addressed
//!
//! - **C1 (Codex+Claude CRITICAL — SlotId TOCTOU race)**: see above.
//! - **C2 (Codex+Claude CRITICAL — queued-sentinel collision)**: see above.
//! - **C3 (Codex+Claude CRITICAL — step priority bug)**: see above.
//! - **M2 (Codex MAJOR — prompt_tokens=0 deadlock risk)**: see above.

use std::collections::VecDeque;
use std::fmt;
use std::time::Instant;

// ---------------------------------------------------------------------------
// SlotId — re-exported from Phase A iter-1.
// ---------------------------------------------------------------------------

pub use crate::serve::multi_seq_kv::SlotId;

// ---------------------------------------------------------------------------
// RequestId — per-admit logical identifier (iter-2.5 C2 fix).
// ---------------------------------------------------------------------------

/// Logical request identifier assigned at admit time, distinct from the
/// physical `SlotId`. Used to cancel queued requests before they get
/// promoted to a physical slot.
///
/// (cfa-iter2.5 C2: closes the `SlotId(u32::MAX)` collision among queued
/// requests. The prior implementation used `SlotId(u32::MAX)` as a
/// sentinel for ALL queued slots and `release(SlotId(u32::MAX))` used
/// `retain != SlotId(u32::MAX)` which removed ALL queued requests at
/// once — silent data loss for any concurrent client. Now: each admit
/// gets a unique monotonic `RequestId`, and `cancel_queued(request_id)`
/// removes exactly one.)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RequestId(pub u64);

// ---------------------------------------------------------------------------
// SlotHandle — per-admit-or-promote handle (iter-2.5 C1 fix).
// ---------------------------------------------------------------------------

/// Per-admit-or-promote handle for an in-flight slot. Callers MUST pass
/// this back to [`InflightBatchedScheduler::advance_after_prefill`],
/// [`InflightBatchedScheduler::advance_after_decode`], and
/// [`InflightBatchedScheduler::release`] (and the equivalent FIFO entry
/// points) so the scheduler can validate the callback is for the
/// CURRENT occupant of that physical slot, not a recycled occupant.
///
/// (cfa-iter2.5 C1: closes the TOCTOU race where auto-release on
/// `max_tokens` + immediate promote of a queued request onto the same
/// `SlotId` let stale in-flight callbacks for the OLD occupant mutate
/// the NEW occupant's state. The generation counter is bumped on every
/// release; any callback with a generation that does not match the
/// scheduler's current generation for that slot is silently dropped as
/// a no-op.)
///
/// Stale callbacks are NOT an error condition — they happen legitimately
/// under any locking discipline that drops the scheduler lock between
/// `step()` and the driver-side `advance_after_*` callback. The scheduler
/// is defensive by construction; the driver does not need to know
/// whether its callback "won" or was superseded.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SlotHandle {
    /// Physical slot index — always in `[0, max_slots)` for
    /// `InflightBatched`, always `SlotId(0)` for `FifoSerial`.
    pub slot_id: SlotId,
    /// Monotonic per-slot generation. Bumped on every release. A
    /// callback whose `generation` does not match the scheduler's
    /// current generation for `slot_id` is a no-op.
    pub generation: u64,
}

// ---------------------------------------------------------------------------
// Scheduler policy (ADR-040 §3.2)
// ---------------------------------------------------------------------------

/// Which scheduling discipline an `Engine` uses (ADR-040 §3.2).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchedulerPolicy {
    /// One in-flight request, bounded queue, 429 on overflow.
    /// Mirrors `Engine::spawn`'s mpsc-channel + single-worker semantics.
    FifoSerial,
    /// Admission-during-decode with up to `max_slots` concurrent requests.
    /// Mirrors llama.cpp `-cb` (ADR-040 §3.3 reference choice).
    InflightBatched,
}

// ---------------------------------------------------------------------------
// Slot descriptor + admit request
// ---------------------------------------------------------------------------

/// An admitted request's slot handle.
///
/// `RequestSlot` is the **pure descriptor** the scheduler hands back from
/// `admit`.
///
/// iter-2.5 changes:
/// - `request_id` (new): always set, unique per-admit logical id (C2).
/// - `handle` (new): `Some` when the slot is in `in_flight` immediately
///   after admit; `None` when the slot was queued (the real `SlotHandle`
///   is allocated at promotion time and observed via
///   `SchedulerStep::Prefill { handle }`).
/// - `slot_id` (removed): callers now read `handle.slot_id` when present,
///   or treat the request as queued.
#[derive(Debug, Clone)]
pub struct RequestSlot {
    /// Per-admit logical id, unique for the lifetime of the scheduler
    /// (modulo u64 wraparound, which at 1 KHz takes 580M years).
    /// Always set — callers use this to cancel queued requests via
    /// `cancel_queued(request_id)` and to disambiguate two successive
    /// admits in `RequestSlot.handle == None` (queued) state.
    pub request_id: RequestId,
    /// `Some` when the request landed directly in `in_flight` at admit
    /// time. `None` when the request was queued (no physical slot yet);
    /// the handle is allocated at promotion time and observed via
    /// `SchedulerStep::Prefill { handle: SlotHandle, .. }`.
    pub handle: Option<SlotHandle>,
    /// Wall-clock instant `admit()` returned `Ok` for this slot.
    pub admitted_at: Instant,
    /// Prompt-token count as passed to `admit()` (post-template, pre-prefill).
    pub prompt_tokens: u32,
    /// Maximum new tokens the request may emit (sampler stop budget).
    pub max_tokens: u32,
}

/// Caller-supplied request bookkeeping handed to `Scheduler::admit`.
#[derive(Debug, Clone)]
pub struct AdmitRequest {
    /// Tokenized prompt length after chat-template rendering.
    pub prompt_tokens: u32,
    /// Maximum new tokens the request may emit.
    pub max_tokens: u32,
    /// **ADR-040 §3.5 iter-A5** — caller-computed byte cost the request
    /// would put on the per-slot KV cache, typically
    /// `(prompt_tokens + max_tokens) * per_arch_kv_bytes_per_token`.
    ///
    /// Set to `0` to opt out of per-slot KV-budget enforcement (the
    /// scheduler treats `0` as "caller did not compute; do not enforce").
    /// Today every caller in `worker_run` passes `0` — byte-equivalence
    /// with pre-A5 is preserved.  Phase C2c (Qwen35 SlotAware) and
    /// C2d (Gemma 4 SlotAware) wire the real per-arch byte cost so
    /// admission can fail-fast under aggregate budget pressure (per
    /// ADR-040 §3.5: "per-slot OOM returns 429 to the admitting
    /// handler").
    ///
    /// `0`-default also handles the per-iter back-compat surface for
    /// pre-A5 callsites that still construct `AdmitRequest { prompt_tokens,
    /// max_tokens }` literally — those callsites compile against the new
    /// field only if they switch to functional update (`..Default::default()`)
    /// or explicit `kv_bytes_needed: 0`; iter-A5's worker_run edits add
    /// the explicit field at each of the 4 admit sites.
    pub kv_bytes_needed: u64,
}

impl Default for AdmitRequest {
    fn default() -> Self {
        Self {
            prompt_tokens: 0,
            max_tokens: 0,
            kv_bytes_needed: 0,
        }
    }
}

// ---------------------------------------------------------------------------
// Scheduler step variant (ADR-040 AC-2)
// ---------------------------------------------------------------------------

/// What the scheduler decided the next forward pass should do.
///
/// iter-2.5 shape change: all variant fields that previously carried
/// `SlotId` now carry `SlotHandle` (C1 — TOCTOU race). The driver
/// callbacks (`advance_after_prefill` / `advance_after_decode`) take
/// the handle directly off this discriminant and pass it back to the
/// scheduler; the scheduler validates generation and silently drops
/// stale callbacks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SchedulerStep {
    /// No work available; the engine loop should park.
    Idle,
    /// Run prefill for one slot for `n_tokens` tokens. The driver MUST
    /// call `Scheduler::advance_after_prefill(handle, n_consumed)` after
    /// running the forward pass.
    Prefill { handle: SlotHandle, n_tokens: u32 },
    /// Run decode for the listed slots (one forward, batched in slot dim).
    /// The driver MUST call `Scheduler::advance_after_decode(handle)` for
    /// each handle per emitted token.
    Decode { handles: Vec<SlotHandle> },
    /// Run prefill for one slot AND decode for the listed slots in one
    /// forward. The driver MUST call `advance_after_prefill` for the
    /// prefill handle AND `advance_after_decode` for each decode handle.
    ///
    /// iter-2.5 C3 fix: `prefill` is now the FIRST `Prefilling` slot in
    /// insertion (FIFO) order — NOT necessarily the just-promoted slot.
    /// An older mid-chunk prefill always wins over a freshly-promoted
    /// queued slot so chunk continuation cannot be starved.
    Mixed {
        prefill: SlotHandle,
        n_prefill_tokens: u32,
        decode_handles: Vec<SlotHandle>,
    },
}

// ---------------------------------------------------------------------------
// Prefill chunk size — (ADR-040 §3.3 llama.cpp -cb mirror)
// ---------------------------------------------------------------------------

/// Default number of prompt tokens consumed per `Prefill` step.
///
/// Pinned at 512 to mirror llama.cpp's `-ub` (ubatch) default.
pub const DEFAULT_PREFILL_CHUNK_TOKENS: u32 = 512;

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// Why `Scheduler::admit` rejected a request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdmitError {
    /// Queue + in-flight slots are at the configured cap. Maps to HTTP 429.
    QueueFull {
        queue_capacity: u32,
        total_admissible: u32,
        in_flight: u32,
    },
    /// Scheduler is no longer accepting work (post-shutdown).
    SchedulerStopped,
    /// **ADR-040 §3.5 iter-A5** — the request's caller-computed KV-byte
    /// cost (`AdmitRequest::kv_bytes_needed`) exceeds the configured
    /// per-slot budget (`per_slot_kv_budget_bytes`).  Maps to HTTP 429 +
    /// `Retry-After: 1` upstream per Decision #19, parallel to
    /// `QueueFull`.  Distinct from `QueueFull` so observability +
    /// alerting can differentiate "too many concurrent requests"
    /// (transient — capacity will free) from "this single request asks
    /// for more KV than any single slot can hold" (operator-actionable
    /// — reduce `max_tokens` or use a shorter prompt).
    ///
    /// The fields are populated so the operator-facing 429 body can
    /// name what was attempted vs what was permitted — same shape as
    /// `MultiSeqError::SlotOom`'s `needed_bytes` / `budget_bytes` pair
    /// (`src/serve/multi_seq_kv.rs:265`).
    SlotBudgetExceeded {
        /// Bytes the admit attempt would need (from `AdmitRequest::kv_bytes_needed`).
        needed_bytes: u64,
        /// Per-slot KV budget configured on the scheduler.
        budget_bytes: u64,
    },
}

impl fmt::Display for AdmitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AdmitError::QueueFull {
                queue_capacity,
                total_admissible,
                in_flight,
            } => write!(
                f,
                "queue full (queue_capacity={}, total_admissible={}, in_flight={})",
                queue_capacity, total_admissible, in_flight,
            ),
            AdmitError::SchedulerStopped => write!(f, "scheduler stopped"),
            AdmitError::SlotBudgetExceeded {
                needed_bytes,
                budget_bytes,
            } => write!(
                f,
                "per-slot KV budget exceeded (needed_bytes={}, budget_bytes={}) \
                 — ADR-040 §3.5: reduce max_tokens or request a smaller prompt",
                needed_bytes, budget_bytes,
            ),
        }
    }
}

/// Why `Scheduler::step` failed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StepError {
    /// The underlying engine forward-pass returned an error. Reserved
    /// for Phase C iter-2 wiring; the scheduler itself is structurally
    /// infallible.
    EngineFailed(String),
}

// ---------------------------------------------------------------------------
// Stats
// ---------------------------------------------------------------------------

/// Lifetime counters + current resident state, for `/metrics` + ops view.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchedulerStats {
    pub policy: SchedulerPolicy,
    pub in_flight_slots: u32,
    pub queue_capacity: u32,
    pub admitted_total: u64,
    pub rejected_429_total: u64,
    pub completed_total: u64,
}

// ---------------------------------------------------------------------------
// Trait
// ---------------------------------------------------------------------------

/// The ADR-040 §2.1 scheduler surface.
///
/// iter-2.5 changes the callback identities on `release` from `SlotId`
/// to `SlotHandle` (C1 — TOCTOU race fix). The
/// `advance_after_prefill`/`advance_after_decode` driver callbacks live
/// on the concrete `InflightBatchedScheduler` / `FifoSchedulerAdapter`
/// types because their input types differ slightly (FIFO has no real
/// chunking) and the Phase C iter-2 engine loop dispatches against the
/// concrete type after the policy enum branch.
pub trait Scheduler: Send {
    /// Which discipline this scheduler implements.
    fn policy(&self) -> SchedulerPolicy;
    /// Admit a new request. Returns the slot handle on success or an
    /// `AdmitError::QueueFull` when the queue + in-flight set is at cap.
    fn admit(&mut self, req: AdmitRequest) -> Result<RequestSlot, AdmitError>;
    /// Decide what the next forward pass should do.
    fn step(&mut self) -> Result<SchedulerStep, StepError>;
    /// Drop an in-flight slot (completion, error, or client-disconnect).
    /// `handle` is the handle observed at admit (or at promotion via the
    /// `SchedulerStep::Prefill` discriminant). Stale handles are silently
    /// dropped as no-ops (iter-2.5 C1).
    fn release(&mut self, handle: SlotHandle);
    /// Snapshot of lifetime counters + current resident state.
    fn stats(&self) -> SchedulerStats;
}

// ---------------------------------------------------------------------------
// FifoSchedulerAdapter — byte-equivalent wrap of pre-ADR-040 Engine
// ---------------------------------------------------------------------------

/// Byte-equivalent wrap of the existing `Engine::spawn` FIFO contract
/// (Decision #2 + Decision #19).
///
/// `max_slots` is always 1 under this policy. The physical `SlotId` is
/// always `SlotId(0)`; the generation counter on `SlotId(0)` bumps on
/// every release so stale callbacks across recycled occupants are
/// rejected (iter-2.5 C1). Queued requests carry a `RequestId` (not a
/// sentinel SlotId — iter-2.5 C2) and are cancellable via
/// [`Self::cancel_queued`].
pub struct FifoSchedulerAdapter {
    queue_capacity: u32,
    /// Queued requests not yet promoted. Each carries a unique
    /// `RequestId` so cancellation removes exactly one. The
    /// `prompt_tokens` / `max_tokens` are captured at admit time.
    queue: VecDeque<QueuedFifoRequest>,
    /// Current in-flight slot (FIFO has max_slots=1).
    in_flight: Option<InFlightFifoSlot>,
    /// Generation counter for SlotId(0). Bumped on every release; the
    /// new occupant after a recycle observes a generation one higher
    /// than the prior occupant's.
    slot_generation: u64,
    next_request_id: u64,
    admitted_total: u64,
    rejected_429_total: u64,
    completed_total: u64,
    /// **ADR-040 §3.5 iter-A5** — per-slot KV budget in bytes.  `0`
    /// disables enforcement (preserves byte-equivalence for pre-A5
    /// callers).  When > 0, `admit` rejects requests whose
    /// `AdmitRequest::kv_bytes_needed` exceeds this value with
    /// [`AdmitError::SlotBudgetExceeded`] (HTTP 429 + Retry-After
    /// upstream per Decision #19).
    per_slot_kv_budget_bytes: u64,
}

/// Per-queued-request state for FIFO.
#[derive(Debug, Clone)]
struct QueuedFifoRequest {
    request_id: RequestId,
    admitted_at: Instant,
    prompt_tokens: u32,
    max_tokens: u32,
}

/// Per-in-flight-slot state for FIFO.
#[derive(Debug, Clone)]
struct InFlightFifoSlot {
    request_id: RequestId,
    handle: SlotHandle,
    admitted_at: Instant,
    prompt_tokens: u32,
    max_tokens: u32,
    /// `Some(remaining)` when prefill is incomplete (multi-chunk).
    /// `None` after prefill finishes (slot is Decoding).
    prefill_remaining: Option<u32>,
    /// Decode tokens emitted so far.
    tokens_produced: u32,
}

impl FifoSchedulerAdapter {
    /// Build a FIFO scheduler with the given queue cap and no per-slot
    /// KV budget enforcement (iter-A5: `per_slot_kv_budget_bytes = 0`
    /// preserves byte-equivalence with pre-A5 callers).
    pub fn new(queue_capacity: u32) -> Self {
        Self::new_with_kv_budget(queue_capacity, 0)
    }

    /// **ADR-040 §3.5 iter-A5** — build a FIFO scheduler with explicit
    /// per-slot KV byte budget.  `0` disables enforcement (equivalent
    /// to [`Self::new`]).  When > 0, `admit` rejects requests whose
    /// `AdmitRequest::kv_bytes_needed` exceeds `per_slot_kv_budget_bytes`
    /// with [`AdmitError::SlotBudgetExceeded`].
    pub fn new_with_kv_budget(queue_capacity: u32, per_slot_kv_budget_bytes: u64) -> Self {
        let queue_capacity = queue_capacity.max(1);
        Self {
            queue_capacity,
            queue: VecDeque::new(),
            in_flight: None,
            slot_generation: 0,
            next_request_id: 0,
            admitted_total: 0,
            rejected_429_total: 0,
            completed_total: 0,
            per_slot_kv_budget_bytes,
        }
    }

    /// **ADR-040 §3.5 iter-A5** — read the configured per-slot KV
    /// budget (bytes).  `0` means enforcement disabled.
    pub fn per_slot_kv_budget_bytes(&self) -> u64 {
        self.per_slot_kv_budget_bytes
    }

    fn in_flight_count(&self) -> u32 {
        if self.in_flight.is_some() {
            1
        } else {
            0
        }
    }

    fn total_admissible(&self) -> u32 {
        self.queue_capacity.saturating_add(1)
    }

    fn next_request_id(&mut self) -> RequestId {
        let id = RequestId(self.next_request_id);
        self.next_request_id = self.next_request_id.wrapping_add(1);
        id
    }

    /// Construct the in-flight slot for the given admit, using the current
    /// generation counter. Honours iter-2.5 M2: a `prompt_tokens == 0`
    /// request transitions directly to decoding (no prefill).
    ///
    /// Caller MUST have already short-circuited the iter-C2.5 M1
    /// `max_tokens == 0` case (see `classify_admit`) — this constructor
    /// always allocates a physical slot and assumes a non-zero decode
    /// budget. Passing `max_tokens == 0` would build a `Decoding {
    /// tokens_produced: 0, max_tokens: 0 }` slot that auto-releases on
    /// the very first `advance_after_decode`, i.e. the stuck-state bug
    /// the M1 short-circuit eliminates.
    fn build_in_flight(
        &self,
        request_id: RequestId,
        admitted_at: Instant,
        prompt_tokens: u32,
        max_tokens: u32,
    ) -> InFlightFifoSlot {
        InFlightFifoSlot {
            request_id,
            handle: SlotHandle {
                slot_id: SlotId(0),
                generation: self.slot_generation,
            },
            admitted_at,
            prompt_tokens,
            max_tokens,
            // iter-2.5 M2: prompt_tokens==0 skips prefill entirely.
            prefill_remaining: if prompt_tokens == 0 {
                None
            } else {
                Some(prompt_tokens)
            },
            tokens_produced: 0,
        }
    }

    /// Cancel a queued request. Returns `true` if found, `false` otherwise.
    /// Does NOT affect the in-flight slot. (iter-2.5 C2 fix — the prior
    /// `release(SlotId(u32::MAX))` removed ALL queued requests at once.)
    pub fn cancel_queued(&mut self, request_id: RequestId) -> bool {
        let before = self.queue.len();
        self.queue.retain(|q| q.request_id != request_id);
        let removed = before > self.queue.len();
        if removed {
            self.completed_total = self.completed_total.saturating_add(1);
        }
        removed
    }

    /// Test-only generation accessor for SlotId(0). Always `SlotId(0)`
    /// for FIFO; ignores the argument's slot_id field.
    pub fn slot_generation(&self, _slot_id: SlotId) -> u64 {
        self.slot_generation
    }

    /// Promote the next queued request into the in-flight slot. Caller
    /// guarantees `self.in_flight.is_none()` (asserted in debug builds).
    ///
    /// iter-C2.5 M1: if the next queued request has `max_tokens == 0`,
    /// it completes-at-promote (bump `completed_total`, do NOT allocate
    /// a physical slot) and the loop continues to the NEXT queued
    /// request. This mirrors the admit-time short-circuit so a future
    /// caller that enqueues zero-budget work directly cannot leak a
    /// stuck slot. Under normal use (`admit` short-circuits FIRST), the
    /// branch is defensive.
    fn promote_one(&mut self) {
        debug_assert!(
            self.in_flight.is_none(),
            "promote_one called with in_flight occupied"
        );
        while let Some(q) = self.queue.pop_front() {
            if classify_admit(q.prompt_tokens, q.max_tokens)
                == InitialAdmitOutcome::CompletedAtAdmit
            {
                self.completed_total = self.completed_total.saturating_add(1);
                continue;
            }
            self.in_flight = Some(self.build_in_flight(
                q.request_id,
                q.admitted_at,
                q.prompt_tokens,
                q.max_tokens,
            ));
            return;
        }
    }

    /// Driver callback: report that `n_consumed` tokens of prefill were
    /// just executed against the handle. Stale handles are no-ops.
    /// Transitions to decoding once `prefill_remaining` hits 0.
    pub fn advance_after_prefill(&mut self, handle: SlotHandle, n_consumed: u32) {
        let Some(slot) = self.in_flight.as_mut() else {
            return;
        };
        if slot.handle != handle {
            return;
        }
        let Some(remaining) = slot.prefill_remaining else {
            return;
        };
        let new_remaining = remaining.saturating_sub(n_consumed);
        slot.prefill_remaining = if new_remaining == 0 {
            None
        } else {
            Some(new_remaining)
        };
    }

    /// Driver callback: report that the slot just emitted one decode
    /// token. Auto-releases when `tokens_produced >= max_tokens`. Stale
    /// handles are no-ops.
    pub fn advance_after_decode(&mut self, handle: SlotHandle) {
        let should_release = {
            let Some(slot) = self.in_flight.as_mut() else {
                return;
            };
            if slot.handle != handle {
                return;
            }
            // Reject decode advance for a slot that is still prefilling.
            if slot.prefill_remaining.is_some() {
                return;
            }
            slot.tokens_produced = slot.tokens_produced.saturating_add(1);
            slot.tokens_produced >= slot.max_tokens
        };
        if should_release {
            self.release(handle);
        }
    }
}

impl Scheduler for FifoSchedulerAdapter {
    fn policy(&self) -> SchedulerPolicy {
        SchedulerPolicy::FifoSerial
    }

    fn admit(&mut self, req: AdmitRequest) -> Result<RequestSlot, AdmitError> {
        // ADR-040 §3.5 iter-A5 — per-slot KV budget check FIRST.  A
        // request that cannot fit in any single slot's KV budget
        // cannot be served regardless of queue state; rejecting before
        // the queue check is operator-honest (the 429 names the per-
        // request structural problem, not transient capacity).  Budget
        // == 0 disables the check entirely (pre-A5 byte-equivalence).
        if self.per_slot_kv_budget_bytes > 0 && req.kv_bytes_needed > self.per_slot_kv_budget_bytes
        {
            self.rejected_429_total = self.rejected_429_total.saturating_add(1);
            return Err(AdmitError::SlotBudgetExceeded {
                needed_bytes: req.kv_bytes_needed,
                budget_bytes: self.per_slot_kv_budget_bytes,
            });
        }

        if self.queue.len() as u32 >= self.queue_capacity && self.in_flight.is_some() {
            self.rejected_429_total = self.rejected_429_total.saturating_add(1);
            return Err(AdmitError::QueueFull {
                queue_capacity: self.queue_capacity,
                total_admissible: self.total_admissible(),
                in_flight: self.in_flight_count(),
            });
        }

        let request_id = self.next_request_id();
        let admitted_at = Instant::now();
        self.admitted_total = self.admitted_total.saturating_add(1);

        // cfa-iter-C2.5 M1: zero-budget (`max_tokens == 0`) short-circuits.
        // No physical slot allocated, no queue entry, no generation bump
        // (no slot lifecycle to track). The caller observes
        // `handle.is_none()` and skips the drive loop.
        if classify_admit(req.prompt_tokens, req.max_tokens)
            == InitialAdmitOutcome::CompletedAtAdmit
        {
            self.completed_total = self.completed_total.saturating_add(1);
            return Ok(RequestSlot {
                request_id,
                handle: None,
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            });
        }

        let public = if self.in_flight.is_none() {
            let slot =
                self.build_in_flight(request_id, admitted_at, req.prompt_tokens, req.max_tokens);
            let public = RequestSlot {
                request_id,
                handle: Some(slot.handle),
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            };
            self.in_flight = Some(slot);
            public
        } else {
            self.queue.push_back(QueuedFifoRequest {
                request_id,
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            });
            RequestSlot {
                request_id,
                handle: None,
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            }
        };
        Ok(public)
    }

    fn step(&mut self) -> Result<SchedulerStep, StepError> {
        let Some(slot) = self.in_flight.as_ref() else {
            return Ok(SchedulerStep::Idle);
        };
        match slot.prefill_remaining {
            Some(remaining) => Ok(SchedulerStep::Prefill {
                handle: slot.handle,
                n_tokens: remaining,
            }),
            None => Ok(SchedulerStep::Decode {
                handles: vec![slot.handle],
            }),
        }
    }

    fn release(&mut self, handle: SlotHandle) {
        // Stale or unknown handle — silent no-op (iter-2.5 C1).
        let Some(slot) = self.in_flight.as_ref() else {
            return;
        };
        if slot.handle != handle {
            return;
        }
        self.in_flight = None;
        // Bump generation so the next admit/promote sees a fresh value.
        self.slot_generation = self.slot_generation.saturating_add(1);
        self.completed_total = self.completed_total.saturating_add(1);
        // Promote next queued if any.
        self.promote_one();
    }

    fn stats(&self) -> SchedulerStats {
        SchedulerStats {
            policy: SchedulerPolicy::FifoSerial,
            in_flight_slots: self.in_flight_count(),
            queue_capacity: self.queue_capacity,
            admitted_total: self.admitted_total,
            rejected_429_total: self.rejected_429_total,
            completed_total: self.completed_total,
        }
    }
}

// ---------------------------------------------------------------------------
// InflightBatchedScheduler — production FSM (ADR-040 §3.3)
// ---------------------------------------------------------------------------

/// Per-slot lifecycle state for the InflightBatched FSM.
///
/// iter-2.5 M2 change: a request with `prompt_tokens == 0` admits
/// directly to `Decoding` (no `Prefilling` phase). The Decoding variant
/// is reachable from admit/promote for that case in addition to the
/// normal Prefilling → Decoding transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SlotPhase {
    Prefilling {
        tokens_remaining: u32,
    },
    Decoding {
        tokens_produced: u32,
        max_tokens: u32,
    },
}

/// Outcome of evaluating a fresh admit request's prompt/budget shape.
///
/// (cfa-iter-C2.5 M1 fix.) `initial_admit_outcome` returns this so the
/// admit body can dispatch:
///
/// - `PhaseToPrefilling`: normal admit — slot transitions through
///   `SlotPhase::Prefilling` first.
/// - `PhaseToDecoding`: `prompt_tokens == 0` admit (iter-2.5 M2) — slot
///   skips prefill, transitions directly to `SlotPhase::Decoding`.
/// - `CompletedAtAdmit`: `max_tokens == 0` admit — no decode budget.
///   The scheduler does NOT allocate a physical slot; admit bumps
///   `admitted_total` + `completed_total` and returns
///   `RequestSlot { handle: None, .. }`. The caller observes
///   `handle.is_none()` and skips the drive loop entirely.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum InitialAdmitOutcome {
    PhaseToPrefilling,
    PhaseToDecoding,
    CompletedAtAdmit,
}

/// Shared admit-shape classifier used by both `FifoSchedulerAdapter` +
/// `InflightBatchedScheduler` (cfa-iter-C2.5 M1). Pure function — no
/// scheduler state touched — so the two adapters share one classifier
/// without a cross-type method call indirection.
///
/// `max_tokens == 0` takes priority over `prompt_tokens == 0`: a
/// request with no decode budget has nothing to do regardless of
/// prompt length. See [`InitialAdmitOutcome`] variant docs.
fn classify_admit(prompt_tokens: u32, max_tokens: u32) -> InitialAdmitOutcome {
    if max_tokens == 0 {
        return InitialAdmitOutcome::CompletedAtAdmit;
    }
    if prompt_tokens == 0 {
        return InitialAdmitOutcome::PhaseToDecoding;
    }
    InitialAdmitOutcome::PhaseToPrefilling
}

/// In-flight slot bookkeeping.
#[derive(Debug, Clone)]
struct InflightSlot {
    request_id: RequestId,
    handle: SlotHandle,
    admitted_at: Instant,
    prompt_tokens: u32,
    max_tokens: u32,
    phase: SlotPhase,
}

/// Queued request bookkeeping (no physical slot yet — iter-2.5 C2).
#[derive(Debug, Clone)]
struct QueuedInflightRequest {
    request_id: RequestId,
    admitted_at: Instant,
    prompt_tokens: u32,
    max_tokens: u32,
}

/// Production continuous-batching scheduler (ADR-040 §3.3).
pub struct InflightBatchedScheduler {
    queue_capacity: u32,
    max_slots: u32,
    in_flight: Vec<InflightSlot>,
    queue: VecDeque<QueuedInflightRequest>,
    slot_id_free_list: Vec<SlotId>,
    next_fresh_slot_id: u32,
    /// Generation counter per physical slot index — bumped on every
    /// release. Length is always exactly `max_slots`. (iter-2.5 C1.)
    slot_generations: Vec<u64>,
    next_request_id: u64,
    admitted_total: u64,
    rejected_429_total: u64,
    completed_total: u64,
    /// **ADR-040 §3.5 iter-A5** — per-slot KV budget in bytes.  `0`
    /// disables enforcement (preserves byte-equivalence for pre-A5
    /// callers).  When > 0, `admit` rejects requests whose
    /// `AdmitRequest::kv_bytes_needed` exceeds this value with
    /// [`AdmitError::SlotBudgetExceeded`] (HTTP 429 + Retry-After
    /// upstream per Decision #19).
    ///
    /// Per ADR-040 §3.5: "per-slot budget = total / max_slots" — the
    /// engine spawn site is responsible for dividing the operator-
    /// supplied `kv_cache_budget_bytes` by `max_slots` before handing
    /// the per-slot value to this constructor.
    per_slot_kv_budget_bytes: u64,
}

impl InflightBatchedScheduler {
    /// Build an InflightBatched scheduler with the given queue cap +
    /// per-slot concurrency cap and no per-slot KV budget enforcement
    /// (iter-A5: `per_slot_kv_budget_bytes = 0` preserves
    /// byte-equivalence with pre-A5 callers).
    pub fn new(queue_capacity: u32, max_slots: u32) -> Self {
        Self::new_with_kv_budget(queue_capacity, max_slots, 0)
    }

    /// **ADR-040 §3.5 iter-A5** — build an InflightBatched scheduler
    /// with explicit per-slot KV byte budget.  `0` disables enforcement
    /// (equivalent to [`Self::new`]).  When > 0, `admit` rejects
    /// requests whose `AdmitRequest::kv_bytes_needed` exceeds
    /// `per_slot_kv_budget_bytes` with [`AdmitError::SlotBudgetExceeded`].
    ///
    /// Per ADR-040 §3.5 ("per-slot budget = total / max_slots") the
    /// caller — `Engine::spawn` / `spawn_with_mode` at the engine seam
    /// — is responsible for performing the division.  This constructor
    /// stores the value verbatim and enforces against it; it does NOT
    /// re-divide by `max_slots`.  Independence per-slot is structural:
    /// each admit checks against the same `per_slot_kv_budget_bytes`
    /// scalar (not an aggregate), so N admits each at exactly the
    /// per-slot budget all succeed (iter-A5 test
    /// `inflight_per_slot_budget_independent_per_slot`).
    pub fn new_with_kv_budget(
        queue_capacity: u32,
        max_slots: u32,
        per_slot_kv_budget_bytes: u64,
    ) -> Self {
        let queue_capacity = queue_capacity.max(1);
        let max_slots = max_slots.max(1);
        Self {
            queue_capacity,
            max_slots,
            in_flight: Vec::with_capacity(max_slots as usize),
            queue: VecDeque::new(),
            slot_id_free_list: Vec::new(),
            next_fresh_slot_id: 0,
            slot_generations: vec![0; max_slots as usize],
            next_request_id: 0,
            admitted_total: 0,
            rejected_429_total: 0,
            completed_total: 0,
            per_slot_kv_budget_bytes,
        }
    }

    /// **ADR-040 §3.5 iter-A5** — read the configured per-slot KV
    /// budget (bytes).  `0` means enforcement disabled.
    pub fn per_slot_kv_budget_bytes(&self) -> u64 {
        self.per_slot_kv_budget_bytes
    }

    /// Allocate a SlotId — prefer a recycled id, otherwise allocate fresh.
    /// Capped at `max_slots` distinct values.
    fn alloc_slot_id(&mut self) -> SlotId {
        if let Some(id) = self.slot_id_free_list.pop() {
            return id;
        }
        debug_assert!(
            self.next_fresh_slot_id < self.max_slots,
            "alloc_slot_id called past max_slots — caller violated in_flight cap invariant"
        );
        let id = SlotId(self.next_fresh_slot_id);
        self.next_fresh_slot_id += 1;
        id
    }

    fn total_admissible(&self) -> u32 {
        self.queue_capacity.saturating_add(self.max_slots)
    }

    fn next_request_id(&mut self) -> RequestId {
        let id = RequestId(self.next_request_id);
        self.next_request_id = self.next_request_id.wrapping_add(1);
        id
    }

    /// Test-only: read the current generation counter for a slot.
    pub fn slot_generation(&self, slot_id: SlotId) -> u64 {
        let idx = slot_id.0 as usize;
        debug_assert!(
            idx < self.slot_generations.len(),
            "slot_generation called with out-of-bounds slot_id"
        );
        self.slot_generations[idx]
    }

    /// Compute the admit outcome for a request. Thin associated-fn
    /// wrapper around the module-level [`classify_admit`] free function
    /// — kept on the impl so call sites read symmetrically against the
    /// FIFO + InflightBatched adapters.
    fn initial_admit_outcome(prompt_tokens: u32, max_tokens: u32) -> InitialAdmitOutcome {
        classify_admit(prompt_tokens, max_tokens)
    }

    /// Try to promote one queued request into `in_flight`. Returns the
    /// new handle if a promotion happened.
    ///
    /// iter-2.5 C2: queued requests no longer carry sentinel slot ids.
    /// The real handle (slot_id + generation) is freshly allocated here.
    /// iter-2.5 M2: zero-prompt requests promote directly to decoding.
    /// iter-C2.5 M1: zero-budget (`max_tokens == 0`) queued requests
    /// complete-at-promote without allocating a physical slot. This
    /// path is reachable when admit was queued (in_flight was at cap)
    /// for a zero-budget request — `admit` short-circuits zero-budget
    /// FIRST so under normal use this branch is defensive, but the
    /// invariant is enforced here too so future call sites cannot leak
    /// a stuck slot by queuing zero-budget work directly.
    fn try_promote_one_queued(&mut self) -> Option<SlotHandle> {
        if (self.in_flight.len() as u32) >= self.max_slots {
            return None;
        }
        // iter-C2.5 M1: skip past zero-budget queued entries (they
        // complete-at-promote without consuming a physical slot) until
        // we find a real promotion candidate or the queue is empty.
        // Under normal use the admit-time short-circuit prevents zero-
        // budget entries from ever entering the queue, so this loop is
        // defensive; the test
        // `inflight_promote_queued_with_max_tokens_0_does_not_leak`
        // synthesizes the direct-push case to pin the invariant.
        while let Some(q) = self.queue.pop_front() {
            match classify_admit(q.prompt_tokens, q.max_tokens) {
                InitialAdmitOutcome::CompletedAtAdmit => {
                    self.completed_total = self.completed_total.saturating_add(1);
                    continue;
                }
                outcome @ (InitialAdmitOutcome::PhaseToPrefilling
                | InitialAdmitOutcome::PhaseToDecoding) => {
                    let slot_id = self.alloc_slot_id();
                    let generation = self.slot_generations[slot_id.0 as usize];
                    let handle = SlotHandle {
                        slot_id,
                        generation,
                    };
                    let phase = match outcome {
                        InitialAdmitOutcome::PhaseToPrefilling => SlotPhase::Prefilling {
                            tokens_remaining: q.prompt_tokens,
                        },
                        InitialAdmitOutcome::PhaseToDecoding => SlotPhase::Decoding {
                            tokens_produced: 0,
                            max_tokens: q.max_tokens,
                        },
                        InitialAdmitOutcome::CompletedAtAdmit => {
                            unreachable!("inner match already discriminated CompletedAtAdmit")
                        }
                    };
                    self.in_flight.push(InflightSlot {
                        request_id: q.request_id,
                        handle,
                        admitted_at: q.admitted_at,
                        prompt_tokens: q.prompt_tokens,
                        max_tokens: q.max_tokens,
                        phase,
                    });
                    return Some(handle);
                }
            }
        }
        None
    }

    /// Find the index of the first Prefilling slot (insertion / FIFO order).
    fn first_prefilling_idx(&self) -> Option<usize> {
        self.in_flight
            .iter()
            .position(|s| matches!(s.phase, SlotPhase::Prefilling { .. }))
    }

    /// Collect handles for all currently-Decoding slots.
    fn collect_decoding_handles(&self) -> Vec<SlotHandle> {
        self.in_flight
            .iter()
            .filter(|s| matches!(s.phase, SlotPhase::Decoding { .. }))
            .map(|s| s.handle)
            .collect()
    }

    /// Driver callback: report `n_consumed` tokens of prefill were
    /// executed against `handle`. Stale handles, wrong-phase slots, and
    /// unknown handles are silent no-ops.
    pub fn advance_after_prefill(&mut self, handle: SlotHandle, n_consumed: u32) {
        let Some(idx) = self.in_flight.iter().position(|s| s.handle == handle) else {
            return;
        };
        let slot = &mut self.in_flight[idx];
        let SlotPhase::Prefilling { tokens_remaining } = slot.phase else {
            return;
        };
        let new_remaining = tokens_remaining.saturating_sub(n_consumed);
        slot.phase = if new_remaining == 0 {
            SlotPhase::Decoding {
                tokens_produced: 0,
                max_tokens: slot.max_tokens,
            }
        } else {
            SlotPhase::Prefilling {
                tokens_remaining: new_remaining,
            }
        };
    }

    /// Driver callback: report that `handle` just emitted one decode
    /// token. Auto-releases when `tokens_produced >= max_tokens`. Stale
    /// handles, wrong-phase slots, and unknown handles are silent no-ops.
    pub fn advance_after_decode(&mut self, handle: SlotHandle) {
        let Some(idx) = self.in_flight.iter().position(|s| s.handle == handle) else {
            return;
        };
        let SlotPhase::Decoding {
            tokens_produced,
            max_tokens,
        } = self.in_flight[idx].phase
        else {
            return;
        };
        let new_produced = tokens_produced.saturating_add(1);
        if new_produced >= max_tokens {
            // Auto-release: bump generation, recycle id.
            let slot_id = handle.slot_id;
            self.in_flight.remove(idx);
            let gen_idx = slot_id.0 as usize;
            self.slot_generations[gen_idx] = self.slot_generations[gen_idx].saturating_add(1);
            self.slot_id_free_list.push(slot_id);
            self.completed_total = self.completed_total.saturating_add(1);
        } else {
            self.in_flight[idx].phase = SlotPhase::Decoding {
                tokens_produced: new_produced,
                max_tokens,
            };
        }
    }

    /// Cancel a queued (not-yet-promoted) request. Returns `true` if
    /// found, `false` otherwise. Does NOT affect in-flight slots.
    /// (iter-2.5 C2 fix.)
    pub fn cancel_queued(&mut self, request_id: RequestId) -> bool {
        let before = self.queue.len();
        self.queue.retain(|q| q.request_id != request_id);
        let removed = before > self.queue.len();
        if removed {
            self.completed_total = self.completed_total.saturating_add(1);
        }
        removed
    }
}

impl Scheduler for InflightBatchedScheduler {
    fn policy(&self) -> SchedulerPolicy {
        SchedulerPolicy::InflightBatched
    }

    fn admit(&mut self, req: AdmitRequest) -> Result<RequestSlot, AdmitError> {
        // ADR-040 §3.5 iter-A5 — per-slot KV budget check FIRST (same
        // ordering as the FIFO adapter; see that admit body for the
        // rationale).  Budget == 0 disables (pre-A5 byte-equivalence).
        if self.per_slot_kv_budget_bytes > 0 && req.kv_bytes_needed > self.per_slot_kv_budget_bytes
        {
            self.rejected_429_total = self.rejected_429_total.saturating_add(1);
            return Err(AdmitError::SlotBudgetExceeded {
                needed_bytes: req.kv_bytes_needed,
                budget_bytes: self.per_slot_kv_budget_bytes,
            });
        }

        let in_flight = self.in_flight.len() as u32;
        let queued = self.queue.len() as u32;
        if in_flight >= self.max_slots && queued >= self.queue_capacity {
            self.rejected_429_total = self.rejected_429_total.saturating_add(1);
            return Err(AdmitError::QueueFull {
                queue_capacity: self.queue_capacity,
                total_admissible: self.total_admissible(),
                in_flight,
            });
        }

        let request_id = self.next_request_id();
        let admitted_at = Instant::now();
        self.admitted_total = self.admitted_total.saturating_add(1);

        // cfa-iter-C2.5 M1: dispatch on admit outcome. CompletedAtAdmit
        // (max_tokens == 0) short-circuits — no physical slot allocated,
        // no queue entry, handle: None. Other outcomes follow the
        // pre-existing in_flight-or-queue path.
        let outcome = Self::initial_admit_outcome(req.prompt_tokens, req.max_tokens);
        if matches!(outcome, InitialAdmitOutcome::CompletedAtAdmit) {
            self.completed_total = self.completed_total.saturating_add(1);
            return Ok(RequestSlot {
                request_id,
                handle: None,
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            });
        }

        let public = if in_flight < self.max_slots {
            // Admit directly to in_flight: allocate handle, set phase
            // (honouring iter-2.5 M2 for zero-prompt requests).
            let slot_id = self.alloc_slot_id();
            let generation = self.slot_generations[slot_id.0 as usize];
            let handle = SlotHandle {
                slot_id,
                generation,
            };
            let phase = match outcome {
                InitialAdmitOutcome::PhaseToPrefilling => SlotPhase::Prefilling {
                    tokens_remaining: req.prompt_tokens,
                },
                InitialAdmitOutcome::PhaseToDecoding => SlotPhase::Decoding {
                    tokens_produced: 0,
                    max_tokens: req.max_tokens,
                },
                InitialAdmitOutcome::CompletedAtAdmit => {
                    unreachable!("CompletedAtAdmit already handled by the early-return above")
                }
            };
            self.in_flight.push(InflightSlot {
                request_id,
                handle,
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
                phase,
            });
            RequestSlot {
                request_id,
                handle: Some(handle),
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            }
        } else {
            self.queue.push_back(QueuedInflightRequest {
                request_id,
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            });
            RequestSlot {
                request_id,
                handle: None,
                admitted_at,
                prompt_tokens: req.prompt_tokens,
                max_tokens: req.max_tokens,
            }
        };
        Ok(public)
    }

    fn step(&mut self) -> Result<SchedulerStep, StepError> {
        // Priority 1: promote one queued request if room. Promotion does
        // NOT short-circuit — it just adds a slot to the back of
        // `in_flight`; selection below uses first_prefilling_idx() so an
        // older mid-chunk Prefilling slot wins (iter-2.5 C3 fix).
        let _ = self.try_promote_one_queued();

        // Priority 2: find FIRST Prefilling slot in FIFO order.
        let prefill_idx = self.first_prefilling_idx();

        // Collect all Decoding handles (always batched in one forward).
        let decode_handles = self.collect_decoding_handles();

        match (prefill_idx, decode_handles.is_empty()) {
            (Some(idx), false) => {
                // Mixed: oldest Prefilling slot batched with all Decoding.
                let SlotPhase::Prefilling { tokens_remaining } = self.in_flight[idx].phase else {
                    // first_prefilling_idx only returns Prefilling slots.
                    return Err(StepError::EngineFailed(
                        "first_prefilling_idx returned non-Prefilling index — invariant violated"
                            .to_string(),
                    ));
                };
                Ok(SchedulerStep::Mixed {
                    prefill: self.in_flight[idx].handle,
                    n_prefill_tokens: tokens_remaining.min(DEFAULT_PREFILL_CHUNK_TOKENS),
                    decode_handles,
                })
            }
            (Some(idx), true) => {
                let SlotPhase::Prefilling { tokens_remaining } = self.in_flight[idx].phase else {
                    return Err(StepError::EngineFailed(
                        "first_prefilling_idx returned non-Prefilling index — invariant violated"
                            .to_string(),
                    ));
                };
                Ok(SchedulerStep::Prefill {
                    handle: self.in_flight[idx].handle,
                    n_tokens: tokens_remaining.min(DEFAULT_PREFILL_CHUNK_TOKENS),
                })
            }
            (None, false) => Ok(SchedulerStep::Decode {
                handles: decode_handles,
            }),
            (None, true) => Ok(SchedulerStep::Idle),
        }
    }

    fn release(&mut self, handle: SlotHandle) {
        // Stale or unknown handle — silent no-op (iter-2.5 C1).
        let Some(idx) = self.in_flight.iter().position(|s| s.handle == handle) else {
            return;
        };
        self.in_flight.remove(idx);
        let gen_idx = handle.slot_id.0 as usize;
        self.slot_generations[gen_idx] = self.slot_generations[gen_idx].saturating_add(1);
        self.slot_id_free_list.push(handle.slot_id);
        self.completed_total = self.completed_total.saturating_add(1);
        // Promotion is the responsibility of step(); not done here to
        // avoid release-vs-step double-promotion races.
    }

    fn stats(&self) -> SchedulerStats {
        SchedulerStats {
            policy: SchedulerPolicy::InflightBatched,
            in_flight_slots: self.in_flight.len() as u32,
            queue_capacity: self.queue_capacity,
            admitted_total: self.admitted_total,
            rejected_429_total: self.rejected_429_total,
            completed_total: self.completed_total,
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn req(prompt_tokens: u32, max_tokens: u32) -> AdmitRequest {
        AdmitRequest {
            prompt_tokens,
            max_tokens,
            kv_bytes_needed: 0,
        }
    }

    /// iter-A5 helper — admit request with explicit KV byte cost so
    /// per-slot KV budget tests can pin the SlotBudgetExceeded path.
    fn req_with_kv(prompt_tokens: u32, max_tokens: u32, kv_bytes_needed: u64) -> AdmitRequest {
        AdmitRequest {
            prompt_tokens,
            max_tokens,
            kv_bytes_needed,
        }
    }

    /// Helper to extract the handle from a RequestSlot, panicking with a
    /// clear message if the slot was queued (handle == None).
    fn handle_of(slot: &RequestSlot) -> SlotHandle {
        slot.handle
            .expect("expected admitted-in-flight slot (handle == Some)")
    }

    // -----------------------------------------------------------------------
    // FIFO contract preservation — load-bearing.
    // -----------------------------------------------------------------------

    #[test]
    fn fifo_admit_then_step_returns_prefill_for_the_admitted_slot() {
        let mut s = FifoSchedulerAdapter::new(4);
        let slot = s.admit(req(11, 32)).expect("admit ok");
        match s.step().expect("step ok") {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, handle_of(&slot));
                assert_eq!(n_tokens, 11);
            }
            other => panic!("expected Prefill, got {:?}", other),
        }
    }

    #[test]
    fn fifo_admit_twice_queues_second_until_first_releases() {
        let mut s = FifoSchedulerAdapter::new(4);
        let a = s.admit(req(10, 8)).expect("admit a");
        let b = s.admit(req(20, 16)).expect("admit b");
        assert!(
            a.handle.is_some(),
            "a admitted to in_flight has Some(handle)"
        );
        assert!(
            b.handle.is_none(),
            "b queued has None handle until promoted"
        );
        assert_ne!(
            a.request_id, b.request_id,
            "request ids are unique per admit"
        );
        assert_eq!(s.stats().in_flight_slots, 1);

        let a_handle = handle_of(&a);
        assert_eq!(a_handle.slot_id, SlotId(0));

        // First step is prefill for slot a.
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_handle);
                assert_eq!(n_tokens, 10);
            }
            other => panic!("expected Prefill for a, got {:?}", other),
        }

        // Release a — b promotes to in-flight on the same physical slot
        // but with a bumped generation.
        s.release(a_handle);
        assert_eq!(s.stats().in_flight_slots, 1);
        assert_eq!(s.stats().completed_total, 1);

        // Step returns Prefill for b — same SlotId(0), bumped generation.
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle.slot_id, SlotId(0));
                assert_eq!(
                    handle.generation,
                    a_handle.generation + 1,
                    "post-promote generation bumped by release"
                );
                assert_eq!(n_tokens, 20);
            }
            other => panic!("expected Prefill for b, got {:?}", other),
        }
    }

    #[test]
    fn fifo_admit_at_capacity_returns_queue_full_with_all_three_fields() {
        let mut s = FifoSchedulerAdapter::new(2);
        let _a = s.admit(req(1, 1)).expect("a in-flight");
        let _b = s.admit(req(1, 1)).expect("b queued");
        let _c = s.admit(req(1, 1)).expect("c queued");
        match s.admit(req(1, 1)) {
            Err(AdmitError::QueueFull {
                queue_capacity,
                total_admissible,
                in_flight,
            }) => {
                assert_eq!(queue_capacity, 2);
                assert_eq!(
                    total_admissible, 3,
                    "FIFO total = queue_capacity (2) + 1 in-flight"
                );
                assert_eq!(in_flight, 1, "FIFO max in_flight is 1");
            }
            other => panic!("expected QueueFull, got {:?}", other),
        }
        assert_eq!(s.stats().rejected_429_total, 1);
    }

    #[test]
    fn fifo_release_unknown_slot_is_noop() {
        let mut s = FifoSchedulerAdapter::new(4);
        let _a = s.admit(req(1, 1)).expect("admit a");
        // Bogus handle: SlotId(0) with future generation.
        s.release(SlotHandle {
            slot_id: SlotId(9_999),
            generation: 0,
        });
        assert_eq!(s.stats().completed_total, 0);
        assert_eq!(s.stats().in_flight_slots, 1);
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, .. } => assert_eq!(handle.slot_id, SlotId(0)),
            other => panic!("expected Prefill, got {:?}", other),
        }
    }

    #[test]
    fn fifo_stats_admitted_rejected_completed_counters_advance() {
        let mut s = FifoSchedulerAdapter::new(1);
        let a = s.admit(req(1, 1)).expect("a");
        let _b = s.admit(req(1, 1)).expect("b queued");
        assert!(s.admit(req(1, 1)).is_err(), "c must be rejected");
        let stats = s.stats();
        assert_eq!(stats.admitted_total, 2);
        assert_eq!(stats.rejected_429_total, 1);
        assert_eq!(stats.completed_total, 0);

        s.release(handle_of(&a));
        let stats = s.stats();
        assert_eq!(stats.admitted_total, 2);
        assert_eq!(stats.rejected_429_total, 1);
        assert_eq!(stats.completed_total, 1);
    }

    #[test]
    fn fifo_step_returns_idle_when_no_work() {
        let mut s = FifoSchedulerAdapter::new(4);
        assert_eq!(s.step().unwrap(), SchedulerStep::Idle);
    }

    #[test]
    fn fifo_policy_returns_fifoserial() {
        let s = FifoSchedulerAdapter::new(4);
        assert_eq!(s.policy(), SchedulerPolicy::FifoSerial);
        assert_eq!(s.stats().policy, SchedulerPolicy::FifoSerial);
    }

    #[test]
    fn fifo_decode_phase_returns_single_slot_in_decode_variant() {
        let mut s = FifoSchedulerAdapter::new(4);
        let a = s.admit(req(8, 4)).expect("admit a");
        let a_handle = handle_of(&a);
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, .. } => assert_eq!(handle, a_handle),
            other => panic!("expected Prefill, got {:?}", other),
        }
        // Drive prefill to completion so step() can return Decode.
        s.advance_after_prefill(a_handle, 8);
        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => {
                assert_eq!(handles.len(), 1);
                assert_eq!(handles[0], a_handle);
            }
            other => panic!("expected Decode, got {:?}", other),
        }
    }

    // -----------------------------------------------------------------------
    // iter-1.5 FIFO tests for F3a/F3b/F3c.
    // -----------------------------------------------------------------------

    #[test]
    fn fifo_queue_capacity_zero_normalizes_to_one() {
        let s = FifoSchedulerAdapter::new(0);
        assert_eq!(s.stats().queue_capacity, 1);
    }

    #[test]
    fn fifo_serial_always_assigns_slot_id_0() {
        let mut s = FifoSchedulerAdapter::new(4);
        let a = s.admit(req(1, 1)).expect("admit a");
        let a_handle = handle_of(&a);
        assert_eq!(a_handle.slot_id, SlotId(0));
        s.release(a_handle);
        let b = s.admit(req(1, 1)).expect("admit b");
        let b_handle = handle_of(&b);
        assert_eq!(b_handle.slot_id, SlotId(0), "second admit reuses slot 0");
        assert!(
            b_handle.generation > a_handle.generation,
            "generation bumped on release"
        );
    }

    #[test]
    fn fifo_concurrent_admits_under_mutex_match_429_boundary() {
        use std::sync::{Arc, Mutex};
        use std::thread;
        let sched = Arc::new(Mutex::new(FifoSchedulerAdapter::new(2)));
        let mut handles = vec![];
        for i in 0..4 {
            let s = Arc::clone(&sched);
            handles.push(thread::spawn(move || {
                let mut g = s.lock().unwrap();
                g.admit(AdmitRequest {
                    prompt_tokens: 1,
                    max_tokens: 1,
                    kv_bytes_needed: 0,
                })
                .map(|slot| (i, slot.request_id))
            }));
        }
        let mut admitted = 0;
        let mut rejected = 0;
        for h in handles {
            match h.join().unwrap() {
                Ok(_) => admitted += 1,
                Err(AdmitError::QueueFull { .. }) => rejected += 1,
                Err(e) => panic!("unexpected error: {:?}", e),
            }
        }
        assert_eq!(admitted, 3);
        assert_eq!(rejected, 1);
    }

    // -----------------------------------------------------------------------
    // iter-2.5 C2 — FIFO RequestId + cancel_queued tests.
    // -----------------------------------------------------------------------

    #[test]
    fn fifo_admit_request_id_is_unique_and_monotonic() {
        let mut s = FifoSchedulerAdapter::new(200);
        let first = s.admit(req(1, 1)).expect("first admit");
        let mut last_id = first.request_id;
        // Release first so subsequent admits succeed.
        s.release(handle_of(&first));
        // Each admit yields a fresh RequestId. We capture a sequence
        // (using release between to keep the queue empty).
        let mut ids = vec![last_id];
        for _ in 0..99 {
            let r = s.admit(req(1, 1)).expect("admit ok");
            assert!(r.request_id.0 > last_id.0, "request id monotonic");
            last_id = r.request_id;
            ids.push(last_id);
            s.release(handle_of(&r));
        }
        // All ids unique.
        let mut sorted = ids.clone();
        sorted.sort_by_key(|id| id.0);
        sorted.dedup_by_key(|id| id.0);
        assert_eq!(sorted.len(), ids.len(), "all request ids distinct");
    }

    #[test]
    fn fifo_cancel_queued_removes_exactly_one_not_all() {
        // cfa-iter2.5 C2: previously `release(SlotId(u32::MAX))` removed
        // ALL queued requests. Now: cancel_queued(rid) removes exactly one.
        let mut s = FifoSchedulerAdapter::new(4);
        let _a = s.admit(req(1, 1)).expect("a in_flight");
        let b = s.admit(req(1, 1)).expect("b queued");
        let _c = s.admit(req(1, 1)).expect("c queued");
        let _d = s.admit(req(1, 1)).expect("d queued");
        assert_eq!(s.stats().in_flight_slots, 1);
        assert_eq!(s.queue.len(), 3, "3 queued");

        // Cancel b — exactly one removed.
        let removed = s.cancel_queued(b.request_id);
        assert!(removed, "cancel returns true for known request");
        assert_eq!(
            s.queue.len(),
            2,
            "exactly one queued request removed (was 3)"
        );
        assert_eq!(s.stats().in_flight_slots, 1, "in_flight unaffected");
        assert_eq!(s.stats().completed_total, 1, "cancellation bumps completed");
    }

    #[test]
    fn fifo_cancel_queued_unknown_request_id_returns_false() {
        let mut s = FifoSchedulerAdapter::new(4);
        let _a = s.admit(req(1, 1)).expect("a in_flight");
        assert!(!s.cancel_queued(RequestId(99_999)));
        assert_eq!(s.stats().completed_total, 0);
    }

    // -----------------------------------------------------------------------
    // iter-2.5 C1 — FIFO SlotHandle generation tests.
    // -----------------------------------------------------------------------

    #[test]
    fn fifo_slot_generation_bumps_on_release() {
        let mut s = FifoSchedulerAdapter::new(4);
        assert_eq!(s.slot_generation(SlotId(0)), 0);
        let a = s.admit(req(1, 1)).expect("a");
        let a_handle = handle_of(&a);
        assert_eq!(a_handle.generation, 0);
        s.release(a_handle);
        assert_eq!(
            s.slot_generation(SlotId(0)),
            1,
            "generation bumped on release"
        );
    }

    #[test]
    fn fifo_stale_handle_after_recycle_is_noop() {
        // cfa-iter2.5 C1: stale handle for an auto-released previous
        // occupant must NOT mutate the new occupant.
        let mut s = FifoSchedulerAdapter::new(4);
        let a = s.admit(req(1, 1)).expect("a");
        let a_handle = handle_of(&a);
        // Drive A to completion via advance_after_decode (max_tokens=1
        // auto-release on the very first decode advance after prefill).
        s.advance_after_prefill(a_handle, 1);
        s.advance_after_decode(a_handle); // auto-release
        assert_eq!(s.stats().in_flight_slots, 0);

        // Admit B — promoted onto SlotId(0) with generation 1.
        let b = s.admit(req(5, 3)).expect("b");
        let b_handle = handle_of(&b);
        assert_eq!(b_handle.slot_id, a_handle.slot_id);
        assert_eq!(b_handle.generation, a_handle.generation + 1);

        // Stale advance with a_handle MUST be no-op. If it weren't,
        // it would drop B's prefill_remaining or bump B's tokens_produced.
        s.advance_after_prefill(a_handle, 5);
        // Verify B's state intact.
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, b_handle);
                assert_eq!(n_tokens, 5, "B's prompt_tokens untouched by stale callback");
            }
            other => panic!("expected Prefill for B, got {:?}", other),
        }

        // Stale decode for A also a no-op.
        s.advance_after_decode(a_handle);
        assert_eq!(
            s.stats().completed_total,
            1,
            "stale advance did not bump completed"
        );
    }

    // -----------------------------------------------------------------------
    // InflightBatched — preserved admit/release/stats tests.
    // -----------------------------------------------------------------------

    #[test]
    fn inflight_admit_succeeds_below_max_slots() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(1, 1)).expect("a");
        let b = s.admit(req(1, 1)).expect("b");
        let a_h = handle_of(&a);
        let b_h = handle_of(&b);
        assert_ne!(a_h.slot_id, b_h.slot_id, "distinct physical slots");
        assert_eq!(s.stats().in_flight_slots, 2);
        assert_eq!(s.stats().admitted_total, 2);
    }

    #[test]
    fn inflight_admit_returns_queue_full_at_capacity_plus_max_slots() {
        let mut s = InflightBatchedScheduler::new(2, 2);
        let _ = s.admit(req(1, 1)).expect("in-flight 0");
        let _ = s.admit(req(1, 1)).expect("in-flight 1");
        let _ = s.admit(req(1, 1)).expect("queued 0");
        let _ = s.admit(req(1, 1)).expect("queued 1");
        match s.admit(req(1, 1)) {
            Err(AdmitError::QueueFull {
                queue_capacity,
                total_admissible,
                in_flight,
            }) => {
                assert_eq!(queue_capacity, 2);
                assert_eq!(total_admissible, 4);
                assert_eq!(in_flight, 2);
            }
            other => panic!("expected QueueFull, got {:?}", other),
        }
        assert_eq!(s.stats().rejected_429_total, 1);
    }

    #[test]
    fn inflight_policy_returns_inflightbatched() {
        let s = InflightBatchedScheduler::new(4, 2);
        assert_eq!(s.policy(), SchedulerPolicy::InflightBatched);
        assert_eq!(s.stats().policy, SchedulerPolicy::InflightBatched);
    }

    #[test]
    fn inflight_release_drops_slot_from_in_flight() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(1, 1)).expect("a");
        let b = s.admit(req(1, 1)).expect("b");
        assert_eq!(s.stats().in_flight_slots, 2);
        s.release(handle_of(&a));
        assert_eq!(s.stats().in_flight_slots, 1);
        assert_eq!(s.stats().completed_total, 1);
        s.release(handle_of(&b));
        assert_eq!(s.stats().in_flight_slots, 0);
        assert_eq!(s.stats().completed_total, 2);
        // Stale handle is a no-op.
        s.release(SlotHandle {
            slot_id: SlotId(9_999),
            generation: 0,
        });
        assert_eq!(s.stats().completed_total, 2);
    }

    #[test]
    fn inflight_stats_counters_advance() {
        // Release does not auto-promote — step() must drive promotion.
        let mut s = InflightBatchedScheduler::new(1, 1);
        let a = s.admit(req(1, 1)).expect("a in-flight");
        let _b = s.admit(req(1, 1)).expect("b queued");
        assert!(s.admit(req(1, 1)).is_err(), "c must be rejected");
        let stats = s.stats();
        assert_eq!(stats.admitted_total, 2);
        assert_eq!(stats.rejected_429_total, 1);
        assert_eq!(stats.completed_total, 0);
        assert_eq!(stats.in_flight_slots, 1);

        s.release(handle_of(&a));
        assert_eq!(s.stats().completed_total, 1);
        assert_eq!(s.stats().in_flight_slots, 0);

        // step() promotes b.
        match s.step().unwrap() {
            SchedulerStep::Prefill { .. } => {}
            other => panic!("expected Prefill for promoted b, got {:?}", other),
        }
        let stats = s.stats();
        assert_eq!(stats.completed_total, 1);
        assert_eq!(stats.in_flight_slots, 1);
    }

    // -----------------------------------------------------------------------
    // InflightBatched — step() FSM tests.
    // -----------------------------------------------------------------------

    #[test]
    fn inflight_step_empty_returns_idle() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        assert_eq!(s.step().unwrap(), SchedulerStep::Idle);
    }

    #[test]
    fn inflight_step_admit_then_step_returns_prefill_for_admitted_slot() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(13, 32)).expect("admit a");
        let a_h = handle_of(&a);
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 13);
            }
            other => panic!("expected Prefill, got {:?}", other),
        }
    }

    #[test]
    fn inflight_step_after_prefill_completes_returns_decode() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(3, 8)).expect("admit a");
        let a_h = handle_of(&a);
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 3);
            }
            other => panic!("expected Prefill, got {:?}", other),
        }
        s.advance_after_prefill(a_h, 3);
        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => {
                assert_eq!(handles.len(), 1);
                assert_eq!(handles[0], a_h);
            }
            other => panic!("expected Decode, got {:?}", other),
        }
    }

    #[test]
    fn inflight_step_decode_advances_per_token() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(2, 4)).expect("admit a");
        let a_h = handle_of(&a);

        // Prefill.
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 2);
            }
            other => panic!("expected Prefill, got {:?}", other),
        }
        s.advance_after_prefill(a_h, 2);

        // 4 decodes.
        for i in 1..=4u32 {
            match s.step().unwrap() {
                SchedulerStep::Decode { handles } => {
                    assert_eq!(handles, vec![a_h], "decode iter {}", i);
                }
                other => panic!("expected Decode at iter {}, got {:?}", i, other),
            }
            s.advance_after_decode(a_h);
        }
        assert_eq!(s.stats().in_flight_slots, 0);
        assert_eq!(s.stats().completed_total, 1);
        assert_eq!(s.step().unwrap(), SchedulerStep::Idle);
    }

    #[test]
    fn inflight_step_promotes_queued_when_slot_frees() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(5, 4)).expect("a in-flight");
        let _b = s.admit(req(7, 4)).expect("b in-flight");
        let _c = s.admit(req(11, 4)).expect("c queued");
        assert_eq!(s.stats().in_flight_slots, 2);

        s.release(handle_of(&a));
        assert_eq!(s.stats().in_flight_slots, 1);
        assert_eq!(s.stats().completed_total, 1);

        // step() — priority-1 promotes c. iter-2.5 C3 fix: the FIRST
        // Prefilling slot in FIFO order (b, since c was just promoted to
        // the back) is what step() emits.
        match s.step().unwrap() {
            SchedulerStep::Prefill {
                handle: _,
                n_tokens,
            } => {
                // FIFO across in_flight: b was admitted first, c was just
                // promoted to the END of in_flight, so step picks b (7).
                assert_eq!(n_tokens, 7, "first Prefilling slot in FIFO order is b");
            }
            other => panic!("expected Prefill after promotion, got {:?}", other),
        }
        assert_eq!(
            s.stats().in_flight_slots,
            2,
            "c was promoted into the freed slot"
        );
    }

    #[test]
    fn inflight_step_returns_mixed_when_prefill_and_decode_coexist() {
        // Mixed emits when ANY Prefilling slot + ANY Decoding slot coexist.
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(4, 8)).expect("admit a");
        let _b = s.admit(req(5, 8)).expect("admit b");
        let a_h = handle_of(&a);

        // Drive both to Decoding.
        s.step().unwrap();
        s.advance_after_prefill(a_h, 4);
        // Find b's handle.
        let b_h = s
            .in_flight
            .iter()
            .find(|x| x.handle != a_h)
            .map(|x| x.handle)
            .expect("b in_flight");
        s.step().unwrap();
        s.advance_after_prefill(b_h, 5);

        // Both Decoding now.
        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => assert_eq!(handles.len(), 2),
            other => panic!("expected Decode of 2 slots, got {:?}", other),
        }

        // Admit C queued.
        let c = s.admit(req(9, 8)).expect("admit c queued");
        assert!(
            c.handle.is_none(),
            "queued slot has None handle (no sentinel)"
        );

        // Drain A.
        for _ in 0..8 {
            s.advance_after_decode(a_h);
        }
        assert_eq!(s.stats().in_flight_slots, 1, "A auto-released");

        // step() — promotes C; B Decoding; FIRST Prefilling slot in FIFO
        // order is C (only Prefilling slot). Emits Mixed.
        match s.step().unwrap() {
            SchedulerStep::Mixed {
                prefill,
                n_prefill_tokens,
                decode_handles,
            } => {
                assert_eq!(n_prefill_tokens, 9);
                assert_eq!(decode_handles.len(), 1);
                assert_eq!(decode_handles[0], b_h);
                assert_eq!(prefill.slot_id, SlotId(0), "c got a's recycled slot id");
                assert!(
                    prefill.generation > a_h.generation,
                    "c's generation is bumped past a's"
                );
            }
            other => panic!("expected Mixed, got {:?}", other),
        }
    }

    #[test]
    fn inflight_step_chunks_prefill_at_default_size() {
        assert_eq!(DEFAULT_PREFILL_CHUNK_TOKENS, 512);
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(1500, 4)).expect("admit a");
        let a_h = handle_of(&a);

        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 512);
            }
            other => panic!("expected Prefill(512), got {:?}", other),
        }
        s.advance_after_prefill(a_h, 512);

        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 512);
            }
            other => panic!("expected Prefill(512), got {:?}", other),
        }
        s.advance_after_prefill(a_h, 512);

        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 476);
            }
            other => panic!("expected Prefill(476), got {:?}", other),
        }
        s.advance_after_prefill(a_h, 476);

        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => {
                assert_eq!(handles, vec![a_h]);
            }
            other => panic!("expected Decode after chunked prefill, got {:?}", other),
        }
    }

    #[test]
    fn inflight_step_auto_releases_on_max_tokens() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(1, 2)).expect("admit a");
        let a_h = handle_of(&a);

        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 1);
            }
            other => panic!("expected Prefill, got {:?}", other),
        }
        s.advance_after_prefill(a_h, 1);

        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => assert_eq!(handles, vec![a_h]),
            other => panic!("expected Decode iter 1, got {:?}", other),
        }
        s.advance_after_decode(a_h);
        assert_eq!(s.stats().in_flight_slots, 1);
        assert_eq!(s.stats().completed_total, 0);

        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => assert_eq!(handles, vec![a_h]),
            other => panic!("expected Decode iter 2, got {:?}", other),
        }
        s.advance_after_decode(a_h);
        assert_eq!(s.stats().in_flight_slots, 0);
        assert_eq!(s.stats().completed_total, 1);

        assert_eq!(s.step().unwrap(), SchedulerStep::Idle);
    }

    #[test]
    fn inflight_advance_after_prefill_unknown_slot_is_noop() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(5, 8)).expect("admit a");
        let a_h = handle_of(&a);
        // Unknown handle.
        s.advance_after_prefill(
            SlotHandle {
                slot_id: SlotId(9_999),
                generation: 0,
            },
            100,
        );
        // a's state unchanged.
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 5);
            }
            other => panic!("expected Prefill, got {:?}", other),
        }
        // Wrong-phase: advance after a is Decoding.
        s.advance_after_prefill(a_h, 5);
        s.advance_after_prefill(a_h, 1); // no-op
        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => {
                assert_eq!(handles, vec![a_h]);
            }
            other => panic!("expected Decode, got {:?}", other),
        }
    }

    #[test]
    fn inflight_advance_after_decode_overflow_is_clamped_at_max_tokens() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(1, 3)).expect("admit a, max_tokens=3");
        let a_h = handle_of(&a);
        s.advance_after_prefill(a_h, 1);

        s.advance_after_decode(a_h);
        assert_eq!(s.stats().in_flight_slots, 1);
        s.advance_after_decode(a_h);
        assert_eq!(s.stats().in_flight_slots, 1);
        s.advance_after_decode(a_h);
        assert_eq!(s.stats().in_flight_slots, 0);
        assert_eq!(s.stats().completed_total, 1);

        // Stale advances (handle no longer in_flight) are no-ops.
        s.advance_after_decode(a_h);
        s.advance_after_decode(a_h);
        assert_eq!(s.stats().completed_total, 1);
    }

    #[test]
    fn inflight_max_slots_zero_normalizes_to_one() {
        let mut s = InflightBatchedScheduler::new(4, 0);
        let a = s.admit(req(3, 1)).expect("admit must succeed");
        let a_h = handle_of(&a);
        assert_eq!(s.stats().in_flight_slots, 1);
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 3);
            }
            other => panic!("expected Prefill, got {:?}", other),
        }
    }

    // -----------------------------------------------------------------------
    // iter-2.5 C1 — InflightBatched SlotHandle generation tests.
    // -----------------------------------------------------------------------

    #[test]
    fn inflight_slot_generation_bumps_on_release() {
        let mut s = InflightBatchedScheduler::new(4, 2);
        assert_eq!(s.slot_generation(SlotId(0)), 0);
        let a = s.admit(req(1, 1)).expect("a");
        let a_h = handle_of(&a);
        assert_eq!(a_h.generation, 0);
        s.release(a_h);
        assert_eq!(s.slot_generation(SlotId(0)), 1, "release bumps generation");
    }

    #[test]
    fn inflight_slot_generation_bumps_on_auto_release() {
        // Auto-release (via advance_after_decode max-tokens path) also
        // bumps the generation counter.
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(1, 1)).expect("a");
        let a_h = handle_of(&a);
        s.advance_after_prefill(a_h, 1);
        s.advance_after_decode(a_h); // auto-release
        assert_eq!(
            s.slot_generation(a_h.slot_id),
            1,
            "auto-release bumps generation just like explicit release"
        );
    }

    #[test]
    fn inflight_stale_callback_after_recycle_is_noop_not_corrupt() {
        // cfa-iter2.5 C1: after auto-release recycles SlotId, the OLD
        // handle for that slot must NOT mutate the NEW occupant.
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(1, 1)).expect("a"); // prompt=1 max=1
        let a_h = handle_of(&a);
        // Drive A through to auto-release.
        s.step().unwrap();
        s.advance_after_prefill(a_h, 1);
        s.step().unwrap();
        s.advance_after_decode(a_h); // auto-release
        assert_eq!(s.stats().in_flight_slots, 0);

        // Admit B — gets recycled SlotId with bumped generation.
        let b = s.admit(req(8, 4)).expect("b");
        let b_h = handle_of(&b);
        assert_eq!(b_h.slot_id, a_h.slot_id, "B got A's recycled slot id");
        assert_eq!(
            b_h.generation,
            a_h.generation + 1,
            "B's generation is exactly one past A's"
        );

        // Stale callback for A — MUST be no-op, MUST NOT touch B's state.
        s.advance_after_prefill(a_h, 5);
        // Verify B's tokens_remaining unchanged via step().
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, b_h);
                assert_eq!(n_tokens, 8, "B's prompt untouched by stale callback");
            }
            other => panic!("expected Prefill for B, got {:?}", other),
        }
        // Also a stale decode for A is a no-op.
        s.advance_after_decode(a_h);
        assert_eq!(
            s.stats().completed_total,
            1,
            "stale decode did not double-complete"
        );
    }

    #[test]
    fn inflight_handle_carries_correct_generation_at_promote() {
        // Queued requests carry None handle at admit; the handle observed
        // at promotion time (via SchedulerStep::Prefill { handle, .. })
        // MUST have the correct generation for the freshly-allocated slot.
        let mut s = InflightBatchedScheduler::new(4, 1);
        let a = s.admit(req(1, 1)).expect("a in_flight");
        let a_h = handle_of(&a);
        let b = s.admit(req(7, 1)).expect("b queued");
        assert!(b.handle.is_none(), "b queued has None handle");

        // Auto-release A.
        s.advance_after_prefill(a_h, 1);
        s.advance_after_decode(a_h);
        assert_eq!(s.stats().in_flight_slots, 0);

        // step() promotes B and emits Prefill with B's handle.
        match s.step().unwrap() {
            SchedulerStep::Prefill {
                handle: b_h,
                n_tokens,
            } => {
                assert_eq!(n_tokens, 7);
                assert_eq!(b_h.slot_id, a_h.slot_id, "B recycled A's slot id");
                assert_eq!(
                    b_h.generation,
                    a_h.generation + 1,
                    "B's promoted handle generation == prior slot generation + 1"
                );
                // The slot_generations array now matches.
                assert_eq!(s.slot_generation(b_h.slot_id), b_h.generation);
            }
            other => panic!("expected Prefill for promoted B, got {:?}", other),
        }
    }

    #[test]
    fn inflight_concurrent_advance_under_mutex_with_handle_safe() {
        // Rewrite of the prior `inflight_concurrent_advance_pattern_under_mutex`
        // test to use SlotHandle. With the C1 generation discipline, even
        // a mutex-drop between step() and the driver callback cannot
        // corrupt state — stale callbacks no-op.
        use std::sync::{Arc, Mutex};
        use std::thread;

        let sched = Arc::new(Mutex::new(InflightBatchedScheduler::new(6, 3)));
        let mut handles = vec![];
        for thread_idx in 0..3u32 {
            let s = Arc::clone(&sched);
            handles.push(thread::spawn(move || {
                let _ = {
                    let mut g = s.lock().unwrap();
                    g.admit(AdmitRequest {
                        prompt_tokens: 1,
                        max_tokens: 4,
                        kv_bytes_needed: 0,
                    })
                    .expect("admit ok")
                };
                for _ in 0..5 {
                    let action: SchedulerStep = {
                        let mut g = s.lock().unwrap();
                        g.step().unwrap()
                    };
                    match action {
                        SchedulerStep::Prefill { handle, n_tokens } => {
                            let mut g = s.lock().unwrap();
                            g.advance_after_prefill(handle, n_tokens);
                        }
                        SchedulerStep::Decode { handles } => {
                            let mut g = s.lock().unwrap();
                            for h in handles {
                                g.advance_after_decode(h);
                            }
                        }
                        SchedulerStep::Mixed {
                            prefill,
                            n_prefill_tokens,
                            decode_handles,
                        } => {
                            let mut g = s.lock().unwrap();
                            g.advance_after_prefill(prefill, n_prefill_tokens);
                            for h in decode_handles {
                                g.advance_after_decode(h);
                            }
                        }
                        SchedulerStep::Idle => {}
                    }
                }
                let _ = thread_idx;
            }));
        }
        for h in handles {
            h.join().unwrap();
        }
        let stats = sched.lock().unwrap().stats();
        assert_eq!(stats.admitted_total, 3);
        assert_eq!(stats.completed_total, 3);
        assert_eq!(stats.in_flight_slots, 0);
        assert_eq!(stats.rejected_429_total, 0);
    }

    // -----------------------------------------------------------------------
    // iter-2.5 C2 — InflightBatched RequestId + cancel_queued tests.
    // -----------------------------------------------------------------------

    #[test]
    fn inflight_admit_returns_request_id_and_no_handle_when_queued() {
        let mut s = InflightBatchedScheduler::new(2, 1);
        let a = s.admit(req(1, 1)).expect("a");
        let b = s.admit(req(1, 1)).expect("b queued");
        assert!(a.handle.is_some(), "a in_flight has Some handle");
        assert!(b.handle.is_none(), "b queued has None handle");
        assert_ne!(a.request_id, b.request_id);
    }

    #[test]
    fn inflight_cancel_queued_removes_exactly_one_not_all() {
        let mut s = InflightBatchedScheduler::new(4, 1);
        let _a = s.admit(req(1, 1)).expect("a in_flight");
        let q1 = s.admit(req(1, 1)).expect("q1 queued");
        let q2 = s.admit(req(1, 1)).expect("q2 queued");
        let _q3 = s.admit(req(1, 1)).expect("q3 queued");
        assert_eq!(s.queue.len(), 3);

        // Cancel q2 — exactly one removed.
        let removed = s.cancel_queued(q2.request_id);
        assert!(removed);
        assert_eq!(
            s.queue.len(),
            2,
            "exactly one queued request removed (regression: previously ALL queued removed)"
        );
        assert_eq!(
            s.stats().in_flight_slots,
            1,
            "in_flight unaffected by cancel_queued"
        );

        // Verify q1 + q3 still there; q2 truly gone.
        let remaining_ids: Vec<_> = s.queue.iter().map(|q| q.request_id).collect();
        assert!(remaining_ids.contains(&q1.request_id), "q1 still queued");
        assert!(!remaining_ids.contains(&q2.request_id), "q2 removed");
    }

    #[test]
    fn inflight_cancel_queued_unknown_request_id_returns_false() {
        let mut s = InflightBatchedScheduler::new(4, 1);
        let _a = s.admit(req(1, 1)).expect("a in_flight");
        assert!(!s.cancel_queued(RequestId(99_999)));
        assert_eq!(s.stats().completed_total, 0);
    }

    #[test]
    fn inflight_admit_request_id_is_unique_and_monotonic() {
        let mut s = InflightBatchedScheduler::new(200, 1);
        // Admit 100 (1 in_flight + 99 queued; queue cap 200 covers it).
        let mut last = None;
        let mut ids = vec![];
        for _ in 0..100 {
            let r = s.admit(req(1, 1)).expect("admit ok");
            if let Some(l) = last {
                let prev: RequestId = l;
                assert!(r.request_id.0 > prev.0, "request_id monotonic");
            }
            last = Some(r.request_id);
            ids.push(r.request_id);
        }
        assert_eq!(ids.len(), 100);
        let mut sorted = ids.clone();
        sorted.sort_by_key(|id| id.0);
        sorted.dedup_by_key(|id| id.0);
        assert_eq!(sorted.len(), 100, "all 100 request ids distinct");
    }

    // -----------------------------------------------------------------------
    // iter-2.5 C3 — step() priority: older Prefilling wins over promoted.
    // -----------------------------------------------------------------------

    #[test]
    fn inflight_step_priority_older_prefilling_wins_with_promotion() {
        // cfa-iter2.5 C3: a freshly-promoted slot must NOT jump ahead of
        // an older mid-chunk Prefilling slot. The fix: step() picks the
        // FIRST Prefilling slot in insertion (FIFO) order.
        //
        // Construction:
        //   max_slots=3, queue_capacity=2
        //   Admit A (prompt=1000, max=4) → in_flight slot 0, Prefilling{1000}
        //   step() → Prefill(handle-A, 512); advance(512) → Prefilling{488}
        //   Admit B (prompt=10, max=4) → in_flight slot 1, Prefilling{10}
        //   step() picks A (older Prefilling) → Prefill(A, 488)
        //   Actually we drive B to Decoding to set up the scenario:
        //   Step → emits Prefill for A (FIFO order); advance(488) → A Decoding
        //   Now A Decoding, B Prefilling. Set up admit C also Prefilling.
        //
        // Simpler scenario aligning with the brief:
        //   Admit A (prompt=1000); step → Prefill(A, 512); advance(512)
        //     ⇒ A: Prefilling{488}
        //   Admit B (prompt=10); manually advance B's prefill (10) ⇒ B: Decoding
        //   Admit C (prompt=5) into a remaining in_flight slot ⇒ C: Prefilling{5}
        //   step() — no promotion (no queued); first Prefilling in FIFO = A
        //   ⇒ Mixed { prefill: A, n_prefill_tokens: 488, decode_handles: [B] }
        //   NOT Mixed { prefill: C, ... }.
        let mut s = InflightBatchedScheduler::new(4, 3);
        let a = s.admit(req(1000, 4)).expect("a");
        let a_h = handle_of(&a);
        match s.step().unwrap() {
            SchedulerStep::Prefill { handle, n_tokens } => {
                assert_eq!(handle, a_h);
                assert_eq!(n_tokens, 512);
            }
            other => panic!("expected Prefill(A, 512), got {:?}", other),
        }
        s.advance_after_prefill(a_h, 512);
        // A: Prefilling{488}.

        let b = s.admit(req(10, 4)).expect("b");
        let b_h = handle_of(&b);
        // Need to drive B's prefill via step() so the FSM tracks it.
        // step() will pick the FIRST Prefilling slot — which is A. We
        // want B to land in Decoding before our final assertion, so we
        // bypass step() and use the direct callback API to advance B
        // (representing a multi-step engine driver that interleaves).
        s.advance_after_prefill(b_h, 10);
        // B is now Decoding.

        let c = s.admit(req(5, 4)).expect("c");
        let c_h = handle_of(&c);
        // C: Prefilling{5}.

        // Now: A Prefilling{488}, B Decoding, C Prefilling{5}.
        // step() — no queued (everything in_flight); first Prefilling in
        // FIFO order is A (slot 0). decode = [B].
        match s.step().unwrap() {
            SchedulerStep::Mixed {
                prefill,
                n_prefill_tokens,
                decode_handles,
            } => {
                assert_eq!(
                    prefill, a_h,
                    "older Prefilling slot A wins over newer C (cfa-iter2.5 C3)"
                );
                assert_eq!(
                    n_prefill_tokens, 488,
                    "A's mid-chunk continuation must not be starved"
                );
                assert_eq!(decode_handles, vec![b_h]);
                let _ = c_h; // C waits its turn (still Prefilling{5})
            }
            other => panic!("expected Mixed(A, 488, [B]), got {:?}", other),
        }
    }

    // -----------------------------------------------------------------------
    // iter-2.5 M2 — prompt_tokens=0 / max_tokens=0 edge cases.
    // -----------------------------------------------------------------------

    #[test]
    fn inflight_admit_with_prompt_tokens_0_skips_to_decoding() {
        // cfa-iter2.5 M2: prompt_tokens=0 admits directly to Decoding.
        // step() must emit Decode, NOT Prefill { n_tokens: 0 }.
        let mut s = InflightBatchedScheduler::new(4, 2);
        let a = s.admit(req(0, 3)).expect("admit a with empty prompt");
        let a_h = handle_of(&a);
        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => {
                assert_eq!(
                    handles,
                    vec![a_h],
                    "zero-prompt admit transitions directly to Decoding"
                );
            }
            other => panic!("expected Decode (M2), got {:?}", other),
        }
        // Decode 3 times → auto-release.
        s.advance_after_decode(a_h);
        s.advance_after_decode(a_h);
        s.advance_after_decode(a_h);
        assert_eq!(s.stats().in_flight_slots, 0);
        assert_eq!(s.stats().completed_total, 1);
    }

    #[test]
    fn inflight_promote_queued_with_prompt_tokens_0_skips_to_decoding() {
        // cfa-iter2.5 M2: a queued zero-prompt request, on promotion,
        // also transitions directly to Decoding.
        let mut s = InflightBatchedScheduler::new(4, 1);
        let a = s.admit(req(5, 1)).expect("a in_flight");
        let a_h = handle_of(&a);
        let _b = s.admit(req(0, 2)).expect("b queued with zero prompt");

        // Drain A.
        s.advance_after_prefill(a_h, 5);
        s.advance_after_decode(a_h); // auto-release

        // step() promotes B. B has prompt_tokens=0, so it goes straight
        // to Decoding and step() returns Decode (NOT Prefill).
        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => {
                assert_eq!(
                    handles.len(),
                    1,
                    "zero-prompt promoted slot skips Prefilling, emits Decode"
                );
            }
            other => panic!(
                "expected Decode for promoted zero-prompt B, got {:?}",
                other
            ),
        }
    }

    #[test]
    fn fifo_admit_with_prompt_tokens_0_skips_to_decoding() {
        // cfa-iter2.5 M2: same M2 fix for FIFO adapter.
        let mut s = FifoSchedulerAdapter::new(4);
        let a = s.admit(req(0, 2)).expect("admit a");
        let a_h = handle_of(&a);
        match s.step().unwrap() {
            SchedulerStep::Decode { handles } => {
                assert_eq!(
                    handles,
                    vec![a_h],
                    "FIFO: zero-prompt admit skips Prefilling"
                );
            }
            other => panic!("expected Decode (M2 FIFO), got {:?}", other),
        }
        s.advance_after_decode(a_h);
        s.advance_after_decode(a_h); // auto-release
        assert_eq!(s.stats().in_flight_slots, 0);
    }

    // -----------------------------------------------------------------------
    // iter-C2.5 M1 — `max_tokens == 0` admit short-circuits with
    // handle: None, does NOT leak an in-flight slot.
    //
    // Codex /cfa rev-1 finding M1: prior iter-2.5 inline comment claimed
    // "max_tokens == 0 auto-releases at admit time" but `initial_phase`
    // still pushed a `Decoding { tokens_produced: 0, max_tokens: 0 }`
    // slot into `in_flight` — only the Embed worker arm's explicit post-
    // prefill `release` avoided the stuck-slot bug, and that was
    // accidental: any future SlotAware / generate path that observed a
    // zero-budget admit as Decoding work would leak the slot.
    //
    // iter-C2.5 M1 fix (this iter):
    // - `classify_admit` returns `CompletedAtAdmit` for `max_tokens == 0`
    //   regardless of `prompt_tokens` (max_tokens takes priority — no
    //   decode budget means nothing to do).
    // - FIFO + Inflight `admit` short-circuit: bump `admitted_total` +
    //   `completed_total`, return `RequestSlot { handle: None, .. }`,
    //   NO slot allocated.
    // - `try_promote_one_queued` (Inflight) + `promote_one` (FIFO) also
    //   short-circuit zero-budget queued items so even a future caller
    //   that bypasses the admit-time short-circuit cannot leak a slot.
    // -----------------------------------------------------------------------

    #[test]
    fn fifo_admit_with_max_tokens_0_returns_handle_none() {
        // cfa-iter-C2.5 M1: max_tokens=0 admit → handle: None, no slot
        // allocated, completed_total bumped.
        let mut s = FifoSchedulerAdapter::new(4);
        let r = s.admit(req(8, 0)).expect("zero-budget admit must succeed");
        assert!(
            r.handle.is_none(),
            "max_tokens=0 admit must return handle: None (no slot allocated)"
        );
        assert_eq!(
            r.prompt_tokens, 8,
            "RequestSlot still echoes input prompt_tokens"
        );
        assert_eq!(r.max_tokens, 0, "RequestSlot still echoes input max_tokens");

        let stats = s.stats();
        assert_eq!(stats.admitted_total, 1, "admitted_total bumped");
        assert_eq!(
            stats.completed_total, 1,
            "completed_total bumped at admit time (zero-budget short-circuit)"
        );
        assert_eq!(
            stats.in_flight_slots, 0,
            "no physical slot allocated for zero-budget admit"
        );

        // step() returns Idle — the would-have-been slot doesn't exist.
        assert_eq!(
            s.step().unwrap(),
            SchedulerStep::Idle,
            "no in-flight slot, no queued slot → Idle"
        );

        // A subsequent normal admit lands cleanly in the unoccupied slot.
        let b = s.admit(req(3, 5)).expect("normal admit after zero-budget");
        let b_h = handle_of(&b);
        assert_eq!(
            b_h.slot_id,
            SlotId(0),
            "next admit gets the never-allocated slot 0"
        );
        assert_eq!(
            b_h.generation, 0,
            "no generation bump (no release happened — slot was never allocated)"
        );
    }

    #[test]
    fn inflight_admit_with_max_tokens_0_does_not_leak_slot() {
        // cfa-iter-C2.5 M1: N admits with max_tokens=0 under max_slots=4
        // → in_flight_slots stays at 0; completed_total bumps each time.
        let mut s = InflightBatchedScheduler::new(8, 4);
        for i in 0..16 {
            let r = s.admit(req(3, 0)).expect("zero-budget admit must succeed");
            assert!(
                r.handle.is_none(),
                "iter {}: max_tokens=0 admit must return handle: None",
                i
            );
        }
        let stats = s.stats();
        assert_eq!(
            stats.in_flight_slots, 0,
            "16 zero-budget admits must leak ZERO in-flight slots (regression: \
             prior iter-2.5 pushed Decoding{{0,0}} into in_flight)"
        );
        assert_eq!(stats.admitted_total, 16, "all 16 counted as admitted");
        assert_eq!(
            stats.completed_total, 16,
            "all 16 counted as completed-at-admit (no slot lifecycle)"
        );
        assert_eq!(
            stats.rejected_429_total, 0,
            "no QueueFull — short-circuit happens after capacity check"
        );

        // step() returns Idle — no slots ever allocated.
        assert_eq!(s.step().unwrap(), SchedulerStep::Idle);
    }

    #[test]
    fn fifo_admit_prompt_tokens_0_max_tokens_0_no_leak() {
        // cfa-iter-C2.5 M1: both zeros → CompletedAtAdmit (max_tokens
        // takes priority over prompt_tokens). No slot allocated.
        let mut s = FifoSchedulerAdapter::new(4);
        let r = s.admit(req(0, 0)).expect("both-zeros admit must succeed");
        assert!(
            r.handle.is_none(),
            "prompt_tokens=0 AND max_tokens=0 → handle: None (max_tokens wins)"
        );
        let stats = s.stats();
        assert_eq!(stats.admitted_total, 1);
        assert_eq!(stats.completed_total, 1);
        assert_eq!(stats.in_flight_slots, 0);

        // Inflight side: same shape.
        let mut s2 = InflightBatchedScheduler::new(4, 2);
        let r2 = s2.admit(req(0, 0)).expect("both-zeros inflight admit ok");
        assert!(
            r2.handle.is_none(),
            "inflight prompt_tokens=0 AND max_tokens=0 → handle: None"
        );
        let stats2 = s2.stats();
        assert_eq!(stats2.in_flight_slots, 0);
        assert_eq!(stats2.completed_total, 1);
    }

    #[test]
    fn inflight_promote_queued_with_max_tokens_0_does_not_leak() {
        // cfa-iter-C2.5 M1 defensive-promote pin: a zero-budget request
        // CANNOT enter the queue via the normal `admit` path (the admit-
        // time short-circuit fires FIRST regardless of in_flight/queue
        // occupancy). This test exercises the admit-time short-circuit
        // under the same shape that previously would have queued.
        //
        // Setup: fill max_slots with normal admits so the next admit
        // WOULD have been queued under the pre-M1 behaviour. Now the
        // zero-budget admit still short-circuits → handle: None,
        // in_flight unchanged, no queued entry.
        let mut s = InflightBatchedScheduler::new(4, 2);
        let _a = s
            .admit(req(5, 4))
            .expect("a normal admit, in_flight slot 0");
        let _b = s
            .admit(req(7, 4))
            .expect("b normal admit, in_flight slot 1");
        assert_eq!(s.stats().in_flight_slots, 2);
        assert_eq!(s.queue.len(), 0);

        // Now the would-be-queued admit is zero-budget; it short-circuits.
        let c = s.admit(req(11, 0)).expect("c zero-budget admit ok");
        assert!(
            c.handle.is_none(),
            "zero-budget admit at in_flight-cap still short-circuits — not queued"
        );
        assert_eq!(
            s.queue.len(),
            0,
            "zero-budget request MUST NOT enter the queue (cfa-iter-C2.5 M1)"
        );
        assert_eq!(s.stats().in_flight_slots, 2, "in_flight unchanged");
        assert_eq!(s.stats().completed_total, 1, "c counted as completed");

        // Defensive layer: even if a future caller bypasses `admit` and
        // pushes a zero-budget request directly into the queue, the
        // `try_promote_one_queued` path also short-circuits. Synthesize
        // a queued zero-budget entry and drive promotion via release →
        // step.
        s.queue.push_back(QueuedInflightRequest {
            request_id: RequestId(99_999),
            admitted_at: Instant::now(),
            prompt_tokens: 5,
            max_tokens: 0,
        });
        s.queue.push_back(QueuedInflightRequest {
            request_id: RequestId(99_998),
            admitted_at: Instant::now(),
            prompt_tokens: 7,
            max_tokens: 4, // normal — should be the one promoted
        });
        let completed_before = s.stats().completed_total;
        let in_flight_before = s.stats().in_flight_slots;
        // Drop one normal in_flight slot so promotion has room.
        s.release(handle_of(&_a));
        // step() should promote PAST the zero-budget queued entry to
        // reach the normal one. Zero-budget queued entry completes-at-
        // promote (bumps completed_total) but consumes no slot.
        let _ = s.step().unwrap();
        let stats = s.stats();
        // a's release bumped completed_total by 1; zero-budget queued
        // promote-skip bumped it by 1 more.
        assert!(
            stats.completed_total >= completed_before + 2,
            "release + zero-budget-queued-skip both bump completed_total \
             (was {}, now {})",
            completed_before,
            stats.completed_total
        );
        assert_eq!(
            stats.in_flight_slots, in_flight_before,
            "in_flight unchanged: released a → promoted normal request \
             past skipped zero-budget queued entry"
        );
    }

    // -----------------------------------------------------------------------
    // Cross-cutting
    // -----------------------------------------------------------------------

    #[test]
    fn request_slot_admitted_at_is_monotonic() {
        let mut s = FifoSchedulerAdapter::new(4);
        let a = s.admit(req(1, 1)).expect("a");
        let b = s.admit(req(1, 1)).expect("b");
        assert!(b.admitted_at >= a.admitted_at);
    }

    #[test]
    fn admit_error_queue_full_names_queue_capacity_and_total_admissible_and_in_flight() {
        let err = AdmitError::QueueFull {
            queue_capacity: 7,
            total_admissible: 8,
            in_flight: 3,
        };
        let dbg = format!("{:?}", err);
        assert!(dbg.contains("queue_capacity"));
        assert!(dbg.contains("total_admissible"));
        assert!(dbg.contains("in_flight"));
        assert!(dbg.contains('7'));
        assert!(dbg.contains('8'));
        assert!(dbg.contains('3'));

        let disp = format!("{}", err);
        assert!(disp.contains("queue_capacity=7"));
        assert!(disp.contains("total_admissible=8"));
        assert!(disp.contains("in_flight=3"));
    }

    // -----------------------------------------------------------------------
    // iter-A5 — per-slot OOM + budget enforcement (ADR-040 §3.5)
    //
    // The scheduler tracks the per-slot KV budget in BYTES; callers
    // compute `AdmitRequest::kv_bytes_needed` per-arch (Phase C2c+
    // wiring) and the scheduler enforces at admit time.  Budget == 0
    // ⇒ enforcement disabled ⇒ byte-equivalent to pre-A5.
    // -----------------------------------------------------------------------

    #[test]
    fn fifo_admit_below_per_slot_budget_succeeds() {
        // Budget 1 MiB; admit asks for 512 KiB ⇒ accepted.
        let mut s = FifoSchedulerAdapter::new_with_kv_budget(4, 1024 * 1024);
        let r = s
            .admit(req_with_kv(8, 16, 512 * 1024))
            .expect("admit below budget must succeed");
        assert!(
            r.handle.is_some(),
            "below-budget admit lands in_flight with Some(handle)"
        );
        assert_eq!(s.stats().admitted_total, 1);
        assert_eq!(s.stats().rejected_429_total, 0);
        assert_eq!(s.per_slot_kv_budget_bytes(), 1024 * 1024);
    }

    #[test]
    fn fifo_admit_above_per_slot_budget_errors_with_named_fields() {
        // Budget 1 MiB; admit asks for 2 MiB ⇒ rejected.
        let mut s = FifoSchedulerAdapter::new_with_kv_budget(4, 1024 * 1024);
        let needed = 2 * 1024 * 1024;
        match s.admit(req_with_kv(1024, 64, needed)) {
            Err(AdmitError::SlotBudgetExceeded {
                needed_bytes,
                budget_bytes,
            }) => {
                assert_eq!(
                    needed_bytes, needed,
                    "error names the request's needed bytes"
                );
                assert_eq!(
                    budget_bytes,
                    1024 * 1024,
                    "error names the per-slot budget bytes"
                );
            }
            other => panic!("expected SlotBudgetExceeded, got {:?}", other),
        }
        // Counters: rejected_429_total bumped; admitted_total NOT bumped
        // (admit returned Err before the admitted-counter increment).
        let stats = s.stats();
        assert_eq!(
            stats.rejected_429_total, 1,
            "SlotBudgetExceeded bumps rejected_429_total (maps to 429 upstream)"
        );
        assert_eq!(stats.admitted_total, 0);
        assert_eq!(
            stats.in_flight_slots, 0,
            "no physical slot allocated for over-budget admit"
        );
    }

    #[test]
    fn fifo_per_slot_budget_zero_means_unbounded() {
        // Default constructor (no budget arg) ⇒ enforcement disabled.
        // Even an astronomical kv_bytes_needed admits cleanly.
        let mut s = FifoSchedulerAdapter::new(4);
        assert_eq!(
            s.per_slot_kv_budget_bytes(),
            0,
            "new() defaults to per_slot_kv_budget_bytes = 0 (unbounded)"
        );
        let r = s
            .admit(req_with_kv(1, 1, u64::MAX))
            .expect("zero-budget scheduler must accept any kv_bytes_needed");
        assert!(r.handle.is_some());
        assert_eq!(s.stats().rejected_429_total, 0);
        // Explicit `new_with_kv_budget(.., 0)` equivalent.
        let mut s2 = FifoSchedulerAdapter::new_with_kv_budget(4, 0);
        assert_eq!(s2.per_slot_kv_budget_bytes(), 0);
        s2.admit(req_with_kv(1, 1, u64::MAX))
            .expect("explicit 0-budget also unbounded");
    }

    #[test]
    fn inflight_admit_above_per_slot_budget_errors() {
        // Budget 4 MiB per slot; request asks for 5 MiB ⇒ rejected.
        let mut s = InflightBatchedScheduler::new_with_kv_budget(8, 4, 4 * 1024 * 1024);
        let needed = 5 * 1024 * 1024;
        match s.admit(req_with_kv(2048, 128, needed)) {
            Err(AdmitError::SlotBudgetExceeded {
                needed_bytes,
                budget_bytes,
            }) => {
                assert_eq!(needed_bytes, needed);
                assert_eq!(budget_bytes, 4 * 1024 * 1024);
            }
            other => panic!("expected SlotBudgetExceeded, got {:?}", other),
        }
        assert_eq!(s.stats().rejected_429_total, 1);
        assert_eq!(s.stats().admitted_total, 0);
        assert_eq!(
            s.stats().in_flight_slots,
            0,
            "over-budget admit does not allocate a physical slot"
        );
    }

    #[test]
    fn admit_error_slot_budget_exceeded_display_names_needed_and_budget() {
        let err = AdmitError::SlotBudgetExceeded {
            needed_bytes: 12_345_678,
            budget_bytes: 4_096_000,
        };
        let dbg = format!("{:?}", err);
        assert!(dbg.contains("SlotBudgetExceeded"));
        assert!(dbg.contains("needed_bytes"));
        assert!(dbg.contains("budget_bytes"));
        assert!(dbg.contains("12345678"));
        assert!(dbg.contains("4096000"));

        let disp = format!("{}", err);
        assert!(
            disp.contains("needed_bytes=12345678"),
            "Display names needed_bytes verbatim: {}",
            disp
        );
        assert!(
            disp.contains("budget_bytes=4096000"),
            "Display names budget_bytes verbatim: {}",
            disp
        );
        // Operator-actionable message MUST cite ADR-040 §3.5 + name the
        // remediation paths.
        assert!(
            disp.contains("ADR-040"),
            "Display cites ADR-040 §3.5: {}",
            disp
        );
        assert!(
            disp.contains("max_tokens") || disp.contains("prompt"),
            "Display names the actionable remediation: {}",
            disp
        );
    }

    #[test]
    fn inflight_per_slot_budget_independent_per_slot() {
        // ADR-040 §3.5: per-slot budget is independent per slot, NOT
        // aggregate.  Build a scheduler with max_slots=4, budget=1 MiB
        // per slot; admit 4 requests each asking for exactly 1 MiB.
        // All 4 must admit (each at the per-slot budget; the budget
        // does not sum across slots).
        let per_slot = 1024 * 1024;
        let mut s = InflightBatchedScheduler::new_with_kv_budget(8, 4, per_slot);
        let mut admitted = Vec::new();
        for i in 0..4 {
            let r = s
                .admit(req_with_kv(64, 16, per_slot))
                .unwrap_or_else(|e| panic!("slot {} at-budget admit must succeed; got {:?}", i, e));
            assert!(
                r.handle.is_some(),
                "slot {}: at-budget admit lands in_flight",
                i
            );
            admitted.push(r);
        }
        let stats = s.stats();
        assert_eq!(
            stats.admitted_total, 4,
            "all 4 at-budget admits counted; per-slot budget does not sum"
        );
        assert_eq!(
            stats.in_flight_slots, 4,
            "all 4 physical slots occupied (max_slots=4)"
        );
        assert_eq!(
            stats.rejected_429_total, 0,
            "ZERO 429s — each request fits its own per-slot budget"
        );

        // The 5th admit AT budget queues (in_flight cap reached, queue
        // accepts) — still NOT a budget rejection because the request
        // itself fits the per-slot budget.
        let r5 = s
            .admit(req_with_kv(64, 16, per_slot))
            .expect("5th at-budget admit queues (not a budget violation)");
        assert!(
            r5.handle.is_none(),
            "5th admit is queued (in_flight at max_slots=4)"
        );
        assert_eq!(
            s.stats().rejected_429_total,
            0,
            "queueing is not a budget rejection"
        );

        // But an OVER-budget admit IS rejected even when queue has room.
        match s.admit(req_with_kv(64, 16, per_slot + 1)) {
            Err(AdmitError::SlotBudgetExceeded {
                needed_bytes,
                budget_bytes,
            }) => {
                assert_eq!(needed_bytes, per_slot + 1);
                assert_eq!(budget_bytes, per_slot);
            }
            other => panic!("expected SlotBudgetExceeded, got {:?}", other),
        }
    }

    #[test]
    fn admit_request_default_kv_bytes_needed_is_zero() {
        // Back-compat surface: AdmitRequest::default() leaves
        // kv_bytes_needed at 0 so enforcement is opt-in via the field.
        let r = AdmitRequest::default();
        assert_eq!(r.prompt_tokens, 0);
        assert_eq!(r.max_tokens, 0);
        assert_eq!(r.kv_bytes_needed, 0);
    }
}