frink-server 0.42.0

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

use std::collections::BTreeSet;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use crate::policy::anchor::{decode_slide, AnchorState, SlidingRequest, WindowPolicy};
use crate::policy::pool_budget::SWA_RETAIN_GAP;
use crate::policy::radix::{align_down, Handle, SaltedRadix};
use frink_core::cache::{
    KvBlockPool, KvCache, KvPoolExhausted as CacheKvPoolExhausted, PageGroup, PagedKvCache,
    PagedStoreExhausted, SharedPagedKv,
};
use frink_models::sampling::SamplingParams;
use frink_models::tokenizer::{prepend_bos, SpecialTokens, StopTokens};
use frink_models::{Ceiling, Decoder, Engine, KvElem, KvShape, PrefixCache, TextTokenizer};

use crate::budget::ContextCeiling;

use crate::model::ServerTokenizer;

#[derive(Debug, thiserror::Error)]
pub enum DecodeError {
    #[error("prompt encoded to token id {token}, which is outside this model's vocabulary of {vocab_size} (its tokenizer does not match this checkpoint)")]
    TokenOutOfVocab { token: usize, vocab_size: usize },
    #[error("server is at capacity: the shared KV cache block pool has no free blocks for a new request; retry shortly")]
    KvPoolExhausted,
    /// A well-formed request this deployment cannot serve, named rather
    /// than approximated. `prompt_logprobs` and `cache_salt` on the
    /// paged store are the standing cases, and `n` > 1 there when the
    /// checkpoint slides a window or the store has no pages left for
    /// the fork.
    #[error("{0}")]
    Unsupported(String),
    /// The batch scheduler's admission queue is full. Distinct from
    /// `KvPoolExhausted`: nothing is exhausted, the server is simply
    /// further behind than it is willing to queue. Naming the depth and
    /// the cap keeps a retry storm diagnosable -- an operator can tell
    /// "too many clients" from "one request too big".
    #[error("server is at capacity: {queued} requests are already queued for the batch scheduler (limit {cap}); retry shortly")]
    QueueFull { queued: usize, cap: usize },
    /// The request cannot fit a ceiling that will not move: a typed
    /// refusal rather than an out-of-memory kill somewhere downstream.
    ///
    /// `binding` names *which* ceiling, because the two send an
    /// operator to different knobs -- `context_length_exceeded` is the
    /// request's size against what this deployment admits per request,
    /// `device_memory_budget_exceeded` is the whole server's KV budget.
    /// `estimated_bytes` and `limit_bytes` are the KV cost of the
    /// request and of the ceiling, so the arithmetic is checkable
    /// rather than asserted.
    ///
    /// Deliberately not a 503: an idle server refuses this identically,
    /// so "retry shortly" would be a lie.
    #[error("{binding}: {detail}")]
    KvBudgetExceeded {
        /// Machine-readable ceiling code from
        /// [`frink_models::Ceiling::code`].
        binding: &'static str,
        /// KV bytes this request would cost at its full length.
        estimated_bytes: u64,
        /// KV bytes the binding ceiling allows.
        limit_bytes: u64,
        /// Token positions the request asked for (prompt + max_tokens).
        positions: usize,
        /// Token positions the binding ceiling allows.
        positions_limit: usize,
        detail: String,
    },
    /// Grammar-constrained decoding could not continue.
    ///
    /// Two live causes, both properties of the *request* rather than of
    /// the server: a grammar this model's vocabulary cannot spell, so
    /// every logit was masked; and a sampled token the grammar refused,
    /// which means the mask and the accept disagreed. Either way the
    /// only alternative is to emit a token the caller's grammar forbids
    /// and report it as constrained output, so this stops instead. See
    /// [`frink_models::grammar_sampler::ConstraintError`], whose text
    /// is carried in `detail`.
    #[error("grammar-constrained decoding stopped: {detail}")]
    GrammarConstraint { detail: String },
    /// A reasoning budget reached the sampler that it could not
    /// enforce: unresolved (a path skipped the tokenizer seam that
    /// turns the caller's number into token sequences) or asked of a
    /// family whose closer cannot be forced. Either way the only
    /// alternative is an unbounded thought served as a bounded one, so
    /// this stops instead. Both causes are meant to be caught earlier
    /// -- the route refuses the family with a 501 -- which is why this
    /// is an error and not a fallback.
    #[error("reasoning budget could not be enforced: {detail}")]
    ReasoningBudget { detail: String },
}

impl DecodeError {
    /// Seconds to advise a client to wait before retrying, or `None`
    /// for an error retrying cannot fix.
    pub fn retry_after_secs(&self) -> Option<u64> {
        match self {
            DecodeError::TokenOutOfVocab { .. } => None,
            // A feature this deployment does not have does not appear
            // on a retry.
            DecodeError::Unsupported(_) => None,
            // The same grammar against the same vocabulary fails the
            // same way on every retry.
            DecodeError::GrammarConstraint { .. } => None,
            DecodeError::ReasoningBudget { .. } => None,
            // Retrying an over-budget request changes nothing: the
            // ceiling it hit is the whole server, not the current load.
            DecodeError::KvBudgetExceeded { .. } => None,
            DecodeError::KvPoolExhausted | DecodeError::QueueFull { .. } => Some(1),
        }
    }
}

/// A shared `KvBlockPool` plus this server's admission-control wait
/// policy: how long a request is willing to retry acquiring its
/// per-layer caches before giving up, when the pool is momentarily
/// exhausted. `queue_wait: Duration::ZERO` (the default) means "try
/// once, reject immediately" -- the original reject-only behavior.
#[derive(Clone)]
pub struct KvPoolConfig {
    pub pool: Arc<Mutex<KvBlockPool>>,
    pub queue_wait: Duration,
}

/// The paged counterpart to [`KvPoolConfig`]: per-layer
/// [`SharedPagedKv`] storage every request draws pages from, plus the
/// same admission wait policy.
///
/// Mutually exclusive with `KvPoolConfig` -- they are two answers to
/// the same question, and a deployment picks one.
#[derive(Clone)]
pub struct PagedKvConfig {
    pub store: Arc<SharedPagedKv>,
    pub queue_wait: Duration,
    /// `Some` when prefix sharing is on: a radix tree from token
    /// prefixes to the page groups holding their KV.
    ///
    /// This is what `frink-models::prefix_cache` could not be. That
    /// one CLONES a `Vec<KvCache>` per entry, so N conversations off
    /// one system prompt hold N copies of its KV. The radix tree stores
    /// page groups and reference-counts them, so they hold one.
    pub radix: Option<Arc<Mutex<SaltedRadix>>>,
    /// The single token that opens a tool call for this checkpoint, when
    /// its family has one and it encodes to exactly one token.
    ///
    /// `None` costs nothing but the anchor: the slide still runs, it
    /// just follows the cursor instead of stopping short of the position
    /// the next agentic turn will rejoin at.
    pub anchor_token: Option<u32>,
    /// Decode steps between window slides.
    ///
    /// Sliding every step would cost a page operation per token for a
    /// page's worth of pages every `block_size` tokens; the admission
    /// bound pays for what accumulates in between, which is why this
    /// number appears in [`slide_hold_bound`] as well as here. Zero is
    /// clamped to one by [`WindowPolicy::with_eviction_interval`], since
    /// it would otherwise divide by zero on the cadence check.
    pub slide_interval: usize,
}

/// The sliding-window state one paged request carries.
///
/// Present only when [`ModelConfig::uniform_sliding_window`] said every
/// layer slides. A page group holds one block in every layer, so a model
/// with even one full-attention layer cannot give a group back -- see
/// that method for why the narrowest window is the wrong answer there.
///
/// [`ModelConfig::uniform_sliding_window`]: frink_models::ModelConfig::uniform_sliding_window
struct WindowSlide {
    policy: WindowPolicy,
    /// Positions whose pages this request has already recycled. Behind
    /// the window, so nothing reads them again.
    released: usize,
    /// The prefix the radix tree owns, which is shared with every other
    /// request holding it and so is never recycled however far behind
    /// the window it falls.
    locked_prefix: usize,
    /// Decode steps taken. `decode_slide` skips step 0, whose state may
    /// still be in flight from the prefill that produced it.
    decode_step: usize,
    anchor: AnchorState,
    anchor_token: Option<u32>,
}

/// Positions a sliding request may hold beyond its prompt.
///
/// This is the ceiling `frink-edge`'s own bound test asserts at every
/// step of a 100_000-token run, and it is what makes admission by the
/// window sound rather than hopeful. Each term is a real reason the
/// slide lags the cursor:
///
/// - `window` is what attention still reads;
/// - a second `window + gap` is the anchor's, which caps the threshold
///   at `anchor - window - gap` until the cursor drifts a whole window
///   past it and the anchor is dropped;
/// - `eviction_interval` is the cadence -- positions accumulate between
///   slides, which is the point of not sliding every step;
/// - two pages cover the `- page` in the threshold and the alignment
///   down to a page boundary.
fn slide_hold_bound(window: usize, policy: &WindowPolicy) -> usize {
    2 * (window + SWA_RETAIN_GAP) + policy.eviction_interval + 2 * policy.page_size
}

/// Why a [`PagedLease::fork`] could not be taken.
///
/// Two reasons, and they send an operator to different places: one is
/// a model this deployment cannot fork at all, the other is a store
/// that is momentarily full. A single "cannot fork" would read as the
/// first when it was the second, and the second clears on its own.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ForkRefused {
    /// The request runs a sliding window, whose slide recycles pages
    /// into a private spare list. Two sequences recycling out of one
    /// set of shared pages would read each other's later positions.
    SlidingWindow,
    /// No page groups left. Retryable, unlike the other.
    StoreExhausted,
}

impl std::fmt::Display for ForkRefused {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ForkRefused::SlidingWindow => write!(
                f,
                "this checkpoint slides a window over its KV, and a slid page is recycled \
                 privately rather than shared, so two sequences cannot be forked from one. \
                 Serve it without `--paged-kv`, or send the request n times"
            ),
            ForkRefused::StoreExhausted => write!(
                f,
                "the paged KV store has no free page groups for the fork; each choice past the \
                 first reserves its own generation pages up front, as admission does for the \
                 first. Retry shortly, lower `n`, or raise the page budget"
            ),
        }
    }
}

/// One request's paged KV, which returns its pages when dropped.
///
/// `PagedKvCache` has no `Drop` of its own -- releasing needs a
/// `&mut PagedKvStore` it does not hold a reference to -- so without
/// this, every refusal path and every `?` between admission and the end
/// of generation leaks the whole request's pages until the process
/// exits. `KvCache::with_pool` gets this for free from its own `Drop`;
/// the paged side has to be given it.
pub struct PagedLease {
    caches: Vec<PagedKvCache>,
    store: Arc<SharedPagedKv>,
    /// This sequence's page groups, in position order: `groups[i]`
    /// holds positions `[i * block_size, (i+1) * block_size)`.
    ///
    /// Held as groups rather than per-layer block ids because that is
    /// the unit the radix tree shares and reference-counts. Every entry
    /// here is one the lease must release exactly once.
    /// `None` where a sliding window has recycled the group away: that
    /// index is behind the window, nothing reads it, and its group is
    /// waiting in `spare` to back a later position.
    groups: Vec<Option<PageGroup>>,
    /// Groups this request recycled, kept PRIVATE rather than handed
    /// back to the store.
    ///
    /// Handing them back would be the generous thing and it would also
    /// make `push` fallible again: another request could take the page
    /// this one is about to need, mid-answer, with nowhere to report it.
    /// The generosity happens once, at admission, where a sliding
    /// request asks for a window's worth instead of a whole context's --
    /// which is the larger saving anyway, and one that a refusal can
    /// still be returned from.
    spare: Vec<PageGroup>,
    /// How many leading groups came from the radix tree rather than
    /// from a fresh allocation, and the node they were matched at.
    ///
    /// The node stays locked against eviction for as long as this lease
    /// lives, because those pages are being attended over.
    adopted: Option<(usize, Handle)>,
    radix: Option<Arc<Mutex<SaltedRadix>>>,
    /// This request's prefix namespace, carried so the publish at the
    /// end lands in the same tree the match at the start read from. A
    /// lease that matched in one namespace and published into another
    /// would hand a caller's continuation to everybody.
    salt: Option<u64>,
    window: Option<WindowSlide>,
}

impl Drop for PagedLease {
    fn drop(&mut self) {
        // Unlock first: while the node is locked its pages are
        // protected from eviction, and releasing our own hold before
        // unlocking would let the tree believe a page is free while
        // this lease still names it.
        if let (Some((_, node)), Some(radix)) = (self.adopted, self.radix.as_ref()) {
            radix.lock().unwrap_or_else(|p| p.into_inner()).unlock(node);
        }
        // Every group this sequence held, adopted or fresh. A group the
        // tree also holds survives this, because its refcount does not
        // reach zero.
        //
        // `flatten` rather than `unwrap`: a slid request left holes
        // where it recycled, and those groups are in `spare`. Both
        // halves are drained, and a group is in exactly one of them, so
        // each is still released exactly once.
        for group in self.groups.drain(..).flatten() {
            self.store.release_group(group);
        }
        for group in self.spare.drain(..) {
            self.store.release_group(group);
        }
    }
}

impl PagedLease {
    /// This request's per-layer paged caches, for a caller that drives
    /// the forward itself.
    ///
    /// The lease keeps owning the page GROUPS, so taking the caches out
    /// with `mem::take` and putting them back -- which the batched
    /// decode step does, to hand the whole batch to one call -- does
    /// not disturb the accounting. Only `Drop` releases groups.
    pub fn caches_mut(&mut self) -> &mut Vec<PagedKvCache> {
        &mut self.caches
    }

    pub fn store(&self) -> &Arc<SharedPagedKv> {
        &self.store
    }

    /// The store's page size, which every caller needs to turn groups
    /// into positions.
    pub fn block_size(&self) -> usize {
        self.store.read(0).block_size()
    }

    /// Positions this request did not have to compute.
    pub fn adopted_positions(&self, block_size: usize) -> usize {
        self.adopted
            .map(|(groups, _)| groups * block_size)
            .unwrap_or(0)
    }

    /// A second sequence starting from exactly this one's state.
    ///
    /// **Copy-on-write, at the one block that needs it.** The fork
    /// SHARES every page group whose positions are all written -- they
    /// are finished, nothing will write them again, and a refcount is
    /// what keeps them alive for both holders. What it does not share
    /// is the group holding the part-written tail: two forks both
    /// append there, and the second write would land in the first
    /// fork's context. That one group is copied, and the groups beyond
    /// it, which are reserved capacity rather than content, are taken
    /// fresh.
    ///
    /// So the cost is one page group per layer plus the generation
    /// reservation, not the whole prompt, which is the entire reason
    /// `n` exists: a 6000-token prompt forked four ways prefills once
    /// and copies four tail pages.
    ///
    /// **Every group is taken here, before a token is generated.** The
    /// decode loop's forward closure returns `Vec<f32>` and has nowhere
    /// to report a store that ran dry at token 300 of 400, which is why
    /// admission reserves a whole request's pages up front; a fork is
    /// held to the same rule and refuses in the caller's hand instead.
    ///
    /// `Err` names which of the three reasons applies, because they
    /// send an operator to different places.
    pub fn fork(&self) -> Result<PagedLease, ForkRefused> {
        if self.window.is_some() {
            // A fork would slide on its own schedule, and the slide
            // recycles groups into a PRIVATE spare list. Two sequences
            // recycling out of one set of shared pages is a
            // use-after-free that reads as another position's tokens.
            return Err(ForkRefused::SlidingWindow);
        }
        let block_size = self.block_size();
        let seq_len = self.caches.first().map(|c| c.seq_len()).unwrap_or(0);
        // Groups whose every position is written, and so are safe to
        // share. A ragged tail is NOT in this count, which is what
        // makes the copy below the only one needed.
        let shared = seq_len / block_size;
        if shared > self.groups.len() {
            return Err(ForkRefused::SlidingWindow);
        }
        let fresh_needed = self.groups.len() - shared;
        let mut fresh: Vec<PageGroup> = Vec::with_capacity(fresh_needed);
        for _ in 0..fresh_needed {
            let Some(group) = self.store.acquire_group() else {
                // Hand back what this attempt took: a partial fork
                // would hold pages nothing will ever release.
                for group in fresh {
                    self.store.release_group(group);
                }
                return Err(ForkRefused::StoreExhausted);
            };
            fresh.push(group);
        }
        let mut groups: Vec<Option<PageGroup>> = Vec::with_capacity(self.groups.len());
        for slot in &self.groups[..shared] {
            let Some(group) = *slot else {
                // A hole below the written length means a slide took a
                // page this fork would have to read.
                for group in fresh {
                    self.store.release_group(group);
                }
                return Err(ForkRefused::SlidingWindow);
            };
            self.store.retain_group(group);
            groups.push(Some(group));
        }
        // The tail group, when the length is ragged: copied rather than
        // shared, which is the whole of copy-on-write here.
        if !seq_len.is_multiple_of(block_size) {
            if let Some(Some(tail)) = self.groups.get(shared) {
                self.store.copy_group(*tail, fresh[0]);
            }
        }
        groups.extend(fresh.iter().copied().map(Some));

        let mut caches = self.caches.clone();
        for (layer, cache) in caches.iter_mut().enumerate() {
            let table: Vec<usize> = groups
                .iter()
                .map(|g| {
                    self.store
                        .group_blocks(g.expect("every slot was just filled"))[layer]
                })
                .collect();
            cache.retable(table);
        }

        Ok(PagedLease {
            caches,
            store: Arc::clone(&self.store),
            groups,
            spare: Vec::new(),
            // NOT inherited. The lock is one holder's, released by that
            // lease's `Drop`, and a fork that copied the field would
            // unlock a node it never locked. The adopted pages stay
            // alive for the fork through their own refcounts, which is
            // what refcounts are for.
            adopted: None,
            radix: None,
            // A fork publishes nothing and matches nothing, so its
            // namespace is never read. Carried anyway, because a field
            // that says "whose request this is" should not go missing
            // on a copy of the request.
            salt: self.salt,
            window: None,
        })
    }

    /// Whether this request's window has taken any page away.
    ///
    /// Load-bearing at publish time: a slid sequence cannot be published
    /// to the radix tree. The tree keys on a PREFIX and this sequence's
    /// prefix is exactly the part that is gone -- what it still holds is
    /// a suffix. Publishing anyway would hand the next request a page
    /// whose contents belong to a position a thousand tokens later.
    pub fn has_slid(&self) -> bool {
        self.window.as_ref().is_some_and(|w| w.released > 0)
    }

    /// Offer one sampled token to the anchor detector.
    ///
    /// Separate from [`Self::before_step`] because the two happen at
    /// different moments: a token is observed once it has been sampled,
    /// and the slide runs before the forward that consumes it.
    pub fn observe_sampled(&mut self, token: usize, position: usize, finished: bool) {
        if let Some(w) = self.window.as_mut() {
            w.anchor
                .observe(token as u32, w.anchor_token, position, finished);
        }
    }

    /// Run one decode step's window slide, then make sure the page
    /// `position` will be written into exists.
    ///
    /// Both halves here, in this order, because they are two ends of one
    /// mechanism: the slide is what produces the spare page that the
    /// extension then installs. Splitting them would let a caller do the
    /// second without the first and quietly fall back to taking a page
    /// from the store, which is the failure this whole design removes.
    pub fn before_step(&mut self, position: usize) {
        if self.window.is_none() {
            return;
        }
        let block_size = self.block_size();
        self.slide(position, block_size);
        self.extend_to(position, block_size);
        // `debug_assert!` still TYPE-CHECKS its argument in release, so
        // calling a `#[cfg(debug_assertions)]` method from inside one
        // does not compile without debug assertions. The cfg has to be
        // on the call, not only on the definition.
        #[cfg(debug_assertions)]
        debug_assert!(
            self.tables_match_groups(),
            "a sliding lease must own every block its tables name"
        );
    }

    fn slide(&mut self, position: usize, block_size: usize) {
        let Some(w) = self.window.as_mut() else {
            return;
        };
        w.decode_step += 1;
        let request = SlidingRequest {
            position,
            already_released: w.released,
            locked_prefix: w.locked_prefix,
            decode_step: w.decode_step,
        };
        // `forward_iter` and `decode_step` are the same counter here:
        // one request's cadence is its own step count. A batched engine
        // that wanted every row to slide on the same iteration would
        // pass the batcher's counter instead, and the policy would
        // still be this one.
        let Some(decision) =
            decode_slide(&request, w.anchor.anchor_len(), &w.policy, w.decode_step)
        else {
            return;
        };
        if decision.drop_anchor {
            w.anchor.clear();
        }
        if decision.frees_nothing() {
            return;
        }
        // Both ends are page-aligned -- `free_from` is the previous
        // `free_to` or the locked prefix, `free_to` is aligned down --
        // so this divides exactly and never half-frees a page.
        let (from, to) = (
            decision.free_from / block_size,
            decision.free_to / block_size,
        );
        for slot in &mut self.groups[from..to] {
            if let Some(group) = slot.take() {
                self.spare.push(group);
            }
        }
        w.released = decision.free_to;
    }

    /// Installs a page at the index `position` belongs to, if the block
    /// tables do not reach that far yet.
    ///
    /// Recycled spare first, a fresh group from the store second. The
    /// order matters more than the fallback ever firing: taking the
    /// spare is what keeps a long generation's footprint flat, and the
    /// store acquire is there so that `groups` stays the *only* owner of
    /// every block in the tables. Letting `PagedKvCache::push` grow a
    /// table by itself would acquire a block this lease never records,
    /// and `Drop` releases what `groups` names -- so that block would be
    /// gone until the process exits.
    ///
    /// Extending nothing when the store is empty too is the one clean
    /// answer left: the tables are unchanged, and the caller's `reserve`
    /// refuses having taken nothing.
    fn extend_to(&mut self, position: usize, block_size: usize) {
        let index = position / block_size;
        while self.groups.len() <= index {
            let Some(group) = self.spare.pop().or_else(|| self.store.acquire_group()) else {
                return;
            };
            // A recycled group's physical blocks now back two table
            // indices: the stale one the slide emptied, and this one.
            // Safe precisely because the stale index is behind the
            // window and the kernel never reads it -- see
            // `PagedKvCache::append_block`.
            let blocks = self.store.group_blocks(group);
            for (cache, &block) in self.caches.iter_mut().zip(&blocks) {
                cache.append_block(block);
            }
            self.groups.push(Some(group));
        }
    }

    /// Every cache's block table names exactly the groups this lease
    /// holds, in order.
    ///
    /// The property the whole recycling scheme rests on: `Drop` releases
    /// `groups`, so a table entry that no group accounts for is a leaked
    /// page and a group no table names is a page nothing can read.
    #[cfg(debug_assertions)]
    fn tables_match_groups(&self) -> bool {
        self.caches
            .iter()
            .all(|c| c.block_table().len() == self.groups.len())
    }
}

/// How many page groups a request must hold to run to `max_seq_len`
/// without ever asking the store for another one.
///
/// Without a window that is the whole sequence, and reserving it all up
/// front is not an optimisation -- it is what makes the decode loop's
/// signature honest. `sample_until_stop` takes a closure returning
/// `Vec<f32>`, with nowhere to report a store that ran dry at token 300
/// of 400, the same reason `acquire_pooled_caches` sizes for
/// `max_seq_len` rather than growing (a real panic in live testing).
///
/// WITH a window the answer is the prompt plus a bound, because the
/// slide gives pages back to this request faster than decode consumes
/// them. The prompt term is not a window's worth: prefill materialises
/// positions `0..prompt_len` to reuse the one prefill kernel
/// (`PagedKvCache::to_contiguous`), so every prompt page must still be
/// there while it runs. What the window removes is the *generation*
/// term -- a 4k-window model answering 100k tokens holds its prompt and
/// a window, not a prompt and 100k.
///
/// `prompt_len + bound` is safe at every position, in two cases. Below
/// `prompt_len + bound` it is trivially safe, since nothing is ever
/// released. Above it, `frink-edge`'s own ceiling applies -- the one
/// its bound test asserts at every step of a 100_000-token run -- and
/// the live span is at most `bound` on its own.
fn paged_groups_needed(
    max_seq_len: usize,
    prompt_len: usize,
    block_size: usize,
    window: Option<&WindowPolicy>,
) -> usize {
    paged_hold_positions(max_seq_len, prompt_len, block_size, window)
        .div_ceil(block_size)
        .max(1)
}

/// The same answer in POSITIONS, which is the unit the batch
/// scheduler's block budget speaks.
///
/// One function for both because they are one decision. The budget
/// bounds how many requests the server admits at once and the store
/// bounds whether each of them can run; a budget that priced a windowed
/// request at its whole context would keep refusing admissions the
/// store would happily serve, and the two would disagree about the same
/// server.
///
/// The extra page is slack over the bound, and the `min` is what stops
/// a short request from being made *more* expensive by being windowed.
pub(crate) fn paged_hold_positions(
    max_seq_len: usize,
    prompt_len: usize,
    block_size: usize,
    window: Option<&WindowPolicy>,
) -> usize {
    let Some(policy) = window else {
        return max_seq_len;
    };
    (prompt_len + slide_hold_bound(policy.sliding_window, policy) + block_size).min(max_seq_len)
}

/// The window policy a paged request runs under on this model, or
/// `None` when it may not slide at all.
pub(crate) fn paged_window_policy(
    decoder: &Decoder,
    config: &PagedKvConfig,
) -> Option<WindowPolicy> {
    let block_size = config.store.read(0).block_size();
    decoder
        .config
        .uniform_sliding_window()
        .map(|w| WindowPolicy::new(w, block_size).with_eviction_interval(config.slide_interval))
}

/// Reserves everything this request can need up front, retrying until
/// `config.queue_wait` elapses.
///
/// "Everything it can need" is [`paged_groups_needed`], which is the
/// whole sequence for a full-attention model and prompt-plus-a-window
/// for a sliding one. A request that cannot fit is refused here, before
/// any work, rather than dying halfway through an answer.
pub(crate) fn acquire_paged_caches(
    decoder: &Decoder,
    config: &PagedKvConfig,
    tokens: &[usize],
    max_seq_len: usize,
    // Which caller's prefix namespace this request may match in
    // (`crate::cache_salt`). `None` is the shared one, which is what
    // every request got before the field existed.
    salt: Option<u64>,
) -> Result<PagedLease, PagedStoreExhausted> {
    let block_size = config.store.read(0).block_size();
    let deadline = Instant::now() + config.queue_wait;

    // Consult the tree ONCE, before the retry loop. A match locks the
    // node, so re-matching per attempt would take a second lock on the
    // same node and the unlock on drop would balance only one of them,
    // leaving the prefix pinned forever.
    let adopted = match config.radix.as_ref() {
        Some(radix) => {
            let ids: Vec<u32> = tokens.iter().map(|&t| t as u32).collect();
            let mut tree = radix.lock().unwrap_or_else(|p| p.into_inner());
            let m = tree.match_prefix(salt, &ids);
            // Never adopt the WHOLE prompt. Prefill has to run over at
            // least one token to produce the logits that predict the
            // next one, and a fully-adopted prompt leaves nothing to
            // run. Backing off one page is the cheap answer; the old
            // contiguous prefix cache hit the same wall and backed off
            // one POSITION, which paging cannot do because a partly
            // shared page cannot be written.
            let cap = align_down(ids.len().saturating_sub(1), block_size);
            let cached_len = m.cached_len.min(cap);
            if cached_len == 0 {
                None
            } else {
                tree.lock(m.handle);
                // One index per TOKEN, so consecutive tokens in a page
                // repeat its group. Step by `block_size` to get each
                // group once, in position order.
                let per_token = tree.matched_indices(m.handle);
                let groups: Vec<PageGroup> = per_token[..cached_len]
                    .iter()
                    .step_by(block_size)
                    .map(|&g| PageGroup(g))
                    .collect();
                Some((cached_len, m.handle, groups))
            }
        }
        None => None,
    };
    // Every adopted group gains a holder for the life of this lease.
    if let Some((_, _, groups)) = adopted.as_ref() {
        for &g in groups {
            config.store.retain_group(g);
        }
    }
    let (cached_len, node, adopted_groups) = match adopted {
        Some((len, node, groups)) => (len, Some(node), groups),
        None => (0, None, Vec::new()),
    };

    // A model whose layers do not all slide by the same window cannot
    // give a page group back at all: the group holds a block in every
    // layer, and a full-attention layer still reads position 0.
    let policy = paged_window_policy(decoder, config);

    // Only what the adopted prefix does not already cover.
    let total_groups = paged_groups_needed(max_seq_len, tokens.len(), block_size, policy.as_ref());
    let need = total_groups.saturating_sub(adopted_groups.len());
    let make_window = || {
        policy.map(|policy| WindowSlide {
            policy,
            released: 0,
            // The adopted prefix is the tree's, shared with every other
            // request holding it, so the slide floors here rather than
            // at zero.
            locked_prefix: cached_len,
            decode_step: 0,
            anchor: AnchorState::new(),
            anchor_token: config.anchor_token,
        })
    };

    loop {
        let mut fresh: Vec<PageGroup> = Vec::with_capacity(need);
        while fresh.len() < need {
            match config.store.acquire_group() {
                Some(g) => fresh.push(g),
                None => break,
            }
        }
        if fresh.len() == need {
            let mut groups = adopted_groups;
            groups.extend(fresh);
            let caches = seed_caches(decoder, config, &groups, cached_len, block_size);
            return Ok(PagedLease {
                caches,
                store: Arc::clone(&config.store),
                groups: groups.into_iter().map(Some).collect(),
                spare: Vec::new(),
                adopted: node.map(|n| (cached_len / block_size, n)),
                radix: config.radix.clone(),
                salt,
                window: make_window(),
            });
        }
        // Give back what this attempt took before waiting, or a
        // request that never fits holds pages the requests that would
        // fit are waiting for.
        let short = need - fresh.len();
        for g in fresh {
            config.store.release_group(g);
        }
        // THEN reclaim from the tree, which is the only thing that ever
        // gives these pages back.
        //
        // `publish_to_radix` retains a group for every page it hands
        // the tree, and nothing released them, so the pool shrank
        // monotonically: a long-running server ended up refusing
        // requests that fit, while the tree sat on pages no request was
        // reading. `evict` was written for exactly this call (its own
        // doc says asking for more than `evictable_size` means "the
        // admission arithmetic promised memory the cache never had")
        // and had no caller at all.
        //
        // Only unlocked nodes are evictable, so a prefix some live
        // lease adopted cannot be taken out from under it. The adopted
        // node above is locked before this point for that reason.
        let mut reclaimed = 0usize;
        if let Some(radix) = config.radix.as_ref() {
            let freed = {
                let mut tree = radix.lock().unwrap_or_else(|p| p.into_inner());
                // The tree counts TOKENS and the shortfall is in
                // GROUPS, one block per group. Never ask for more than
                // it holds unlocked: `evict` panics on that, by design,
                // because it means the caller's arithmetic was wrong.
                let want = (short * block_size).min(tree.evictable_size());
                // One index per token, so a page repeats across its
                // block. Release each group ONCE or the store's
                // refcount underflows.
                let per_token = tree.evict(want);
                per_token.into_iter().collect::<BTreeSet<u32>>()
            };
            for g in freed {
                config.store.release_group(PageGroup(g));
                reclaimed += 1;
            }
        }
        // Reclaiming is PROGRESS, not waiting, so retry immediately
        // rather than charging it against the deadline. A caller with
        // `queue_wait = 0` would otherwise evict and then give up
        // without ever trying the pages it just freed, which is a
        // refusal with the memory sitting right there.
        //
        // This terminates: every pass either frees at least one group
        // or falls through to the deadline, and `evictable_size` only
        // shrinks.
        if reclaimed > 0 {
            continue;
        }
        let now = Instant::now();
        if now >= deadline {
            // The adopted groups and the tree lock go back through the
            // lease's own Drop, which is why they are handed to one
            // here rather than released by hand: one release path, not
            // two that must agree.
            drop(PagedLease {
                caches: Vec::new(),
                store: Arc::clone(&config.store),
                groups: adopted_groups.into_iter().map(Some).collect(),
                spare: Vec::new(),
                adopted: node.map(|n| (cached_len / block_size, n)),
                radix: config.radix.clone(),
                salt,
                window: make_window(),
            });
            return Err(PagedStoreExhausted);
        }
        std::thread::sleep(Duration::from_millis(10).min(deadline - now));
    }
}

/// Installs the per-layer block tables for `groups`, with `cached_len`
/// positions already computed.
fn seed_caches(
    decoder: &Decoder,
    config: &PagedKvConfig,
    groups: &[PageGroup],
    cached_len: usize,
    block_size: usize,
) -> Vec<PagedKvCache> {
    // A group holds one block per layer, so layer `l`'s table is the
    // `l`th block of each group, in order.
    let per_group: Vec<Vec<usize>> = groups
        .iter()
        .map(|&g| config.store.group_blocks(g))
        .collect();
    (0..decoder.layers.len())
        .map(|layer| {
            let table: Vec<usize> = per_group.iter().map(|blocks| blocks[layer]).collect();
            let mut cache = PagedKvCache::new();
            cache.adopt_blocks(table, cached_len, block_size);
            cache
        })
        .collect()
}

/// Publishes this request's pages under its full token sequence, so the
/// next request sharing the prefix adopts them instead of recomputing.
///
/// The duplicate count `insert_prefix` returns is the load-bearing
/// part: it is not "how much I stored", it is "how much you must free".
/// Another request published the same prefix while this one was
/// generating, the tree kept ITS pages, and ours for that span are now
/// unreferenced by the tree. Dropping them on the floor is the classic
/// leak in this shape of cache.
pub(crate) fn publish_to_radix(lease: &mut PagedLease, tokens: &[usize], block_size: usize) {
    let Some(radix) = lease.radix.clone() else {
        return;
    };
    // A sequence whose window slid has given its prefix away, and a
    // prefix is exactly what the tree keys on. What it still holds is a
    // suffix at the cursor; publishing it would hand the next request
    // pages whose contents belong to positions far past the ones it
    // matched on.
    if lease.has_slid() {
        return;
    }
    let ids: Vec<u32> = tokens.iter().map(|&t| t as u32).collect();
    // One index per token: each position names the group holding it.
    let mut per_token: Vec<u32> = Vec::with_capacity(ids.len());
    for (i, group) in lease.groups.iter().enumerate() {
        let group = group.expect("a lease that has not slid holds every group it names");
        let covered = block_size.min(ids.len().saturating_sub(i * block_size));
        for _ in 0..covered {
            per_token.push(group.0);
        }
    }
    if per_token.len() < ids.len() {
        // More tokens than pages held: nothing coherent to publish.
        return;
    }
    let result = {
        let mut tree = radix.lock().unwrap_or_else(|p| p.into_inner());
        tree.insert_prefix(lease.salt, &ids, &per_token[..ids.len()])
    };
    // The tree now holds a reference to every group it kept, so those
    // survive this lease's own release.
    let kept = result.cached_len / block_size..result.inserted_len / block_size;
    for i in kept {
        if let Some(Some(g)) = lease.groups.get(i) {
            lease.store.retain_group(*g);
        }
    }
}

/// Which KV representation this request is running on.
///
/// One enum rather than a second `generate`: the decode loop already
/// takes an "advance one token" closure, so paging changes three
/// expressions inside `generate` and nothing else. A parallel function
/// would duplicate the sampling, the stop matching and the usage
/// accounting, which is how the paged DECODER path lost five model
/// features one at a time.
pub(crate) enum Kv {
    Contiguous(Vec<KvCache>),
    Paged(PagedLease),
}

impl Kv {
    /// Prefill, returning the last position's logits.
    ///
    /// `host_kv` only reaches the contiguous arm: the paged arm always
    /// needs real host rows, because scattering them into the page
    /// store IS reading them back.
    /// Prefill AND score every prompt position, for
    /// `prompt_logprobs`.
    ///
    /// A separate entry point rather than a flag on [`Self::prefill`],
    /// because the two do measurably different work:
    /// `forward_batch_last` projects the lm_head ONCE, at the final
    /// position, while this projects it at every one. On a 6000-token
    /// prompt that is 6000 vocabulary-wide matmuls instead of one, and
    /// a request that did not ask for prompt logprobs must not pay for
    /// them.
    ///
    /// `None` for the paged store: its prefill skips whatever the
    /// radix tree already computed, so the positions it returns are
    /// the ones it recomputed rather than the whole prompt, and
    /// scoring a prefix nobody ran would be an invention. Refused by
    /// name at the route instead.
    fn prefill_scored(&mut self, decoder: &Decoder, tokens: &[usize]) -> Option<Vec<Vec<f32>>> {
        match self {
            Kv::Contiguous(caches) => Some(decoder.forward_batch(tokens, 0, caches)),
            Kv::Paged(_) => None,
        }
    }

    fn prefill(&mut self, decoder: &Decoder, tokens: &[usize], host_kv: bool) -> Vec<f32> {
        match self {
            Kv::Contiguous(caches) => forward_prompt_batch(decoder, tokens, 0, caches, host_kv),
            Kv::Paged(lease) => {
                // Skip whatever the radix tree already computed. Those
                // positions are already in the block table with their
                // KV written, so prefill starts where they end.
                let done = lease.adopted_positions(lease.block_size());
                decoder
                    .forward_batch_last_paged(
                        &tokens[done..],
                        done,
                        &mut lease.caches,
                        &lease.store,
                    )
                    .expect("the whole request's pages were reserved at admission")
            }
        }
    }

    /// One decode step.
    /// Feed a whole block and return one logit row per token, which
    /// is what verifying `k` drafted tokens costs instead of `k`
    /// forwards ([`Decoder::forward_batch`]).
    ///
    /// `None` means this store cannot serve speculation, and the
    /// caller takes the ordinary path rather than guessing. Two
    /// reasons, both real:
    ///
    /// * **Paged.** Rejecting a draft means releasing the positions it
    ///   wrote, and a paged store hands those out through a block
    ///   table that the radix cache may already have published a
    ///   prefix of. Undoing that is its own row
    ///   (`docs/plans/server-speculative-decoding.md`); refusing is the
    ///   honest answer until it lands.
    /// * **Recurrent.** A Mamba-style state is a REDUCTION over the
    ///   whole prefix rather than a row per position, so there is
    ///   nothing to drop. `KvCache::can_truncate_to` says so, and
    ///   [`Self::truncate_to`] asks it rather than assuming.
    #[allow(
        dead_code,
        reason = "called by the speculative loop; see sampling_loop::verify_block"
    )]
    fn step_batch(
        &mut self,
        decoder: &Decoder,
        tokens: &[usize],
        pos: usize,
    ) -> Option<Vec<Vec<f32>>> {
        match self {
            Kv::Contiguous(caches) => {
                // Asked BEFORE the forward, not after: a store that
                // cannot roll back must not be written speculatively
                // in the first place.
                if !caches.iter().all(|c| c.can_truncate_to(pos)) {
                    return None;
                }
                Some(decoder.forward_batch(tokens, pos, caches))
            }
            Kv::Paged(_) => None,
        }
    }

    /// Drop every position past `pos`, which is how a rejected draft is
    /// undone. `false` when this store cannot, and the caller must then
    /// never have speculated -- [`Self::step_batch`] is the gate.
    #[allow(
        dead_code,
        reason = "called by the speculative loop; see sampling_loop::verify_block"
    )]
    fn truncate_to(&mut self, pos: usize) -> bool {
        match self {
            Kv::Contiguous(caches) => {
                if !caches.iter().all(|c| c.can_truncate_to(pos)) {
                    return false;
                }
                for c in caches.iter_mut() {
                    c.truncate(pos);
                }
                true
            }
            Kv::Paged(_) => false,
        }
    }

    fn step(&mut self, decoder: &Decoder, token: usize, pos: usize) -> Vec<f32> {
        match self {
            Kv::Contiguous(caches) => decoder.forward_token(token, pos, caches),
            Kv::Paged(lease) => {
                // Slides the window and installs the page this position
                // writes into, before the forward that writes it. A
                // no-op for a model without a uniform window.
                lease.before_step(pos);
                decoder
                    .forward_token_paged(token, pos, &mut lease.caches, &lease.store)
                    .expect("admission reserved the prompt plus this request's window bound")
            }
        }
    }

    /// The contiguous caches, when there are any.
    ///
    /// `prefix_cache` stores `Vec<KvCache>` snapshots, so a paged
    /// request has nothing to hand it. That is why the two are refused
    /// together at startup rather than silently producing a cache that
    /// never hits -- and it is what `wire-radix-prefix-cache` removes.
    pub(crate) fn contiguous_mut(&mut self) -> Option<&mut Vec<KvCache>> {
        match self {
            Kv::Contiguous(caches) => Some(caches),
            Kv::Paged(_) => None,
        }
    }

    pub(crate) fn into_contiguous(self) -> Option<Vec<KvCache>> {
        match self {
            Kv::Contiguous(caches) => Some(caches),
            Kv::Paged(_) => None,
        }
    }
}

/// Retries acquiring one `KvCache` per layer from `config.pool` until
/// either all of them succeed or `config.queue_wait` has elapsed since
/// the first attempt. Sleeping between attempts happens on whichever
/// thread calls this -- fine here since generation already runs on
/// tokio's blocking-thread pool (`spawn_blocking`), not an async
/// reactor thread that a `std::thread::sleep` would otherwise stall.
///
/// `max_seq_len` (this request's real worst-case sequence length --
/// prompt length plus `max_tokens`) is passed straight through to
/// `KvCache::with_pool`, so each layer's cache reserves enough blocks
/// for the *whole* request up front rather than growing mid-decode.
/// This isn't just an optimization: `Decoder::forward_token`/
/// `forward_batch` treat `KvCache::push` as infallible, so a pooled
/// cache that under-reserves at construction and then fails to
/// acquire another block later (because some other request took the
/// pool's remaining capacity in the meantime) would panic mid-decode
/// instead of failing this request cleanly at admission time -- caught
/// by a real panic during live testing before this fix.
/// The typed refusal for a request the KV pool can *never* satisfy, or
/// `None` when the pool could serve it once enough blocks come back.
///
/// A request holds one `KvCache` per layer and each reserves
/// `ceil(max_seq_len / block_size)` blocks up front (see
/// [`acquire_pooled_caches`]), so its whole-pool cost is
/// `n_layers * blocks_per_layer`. When that exceeds
/// `KvBlockPool::total_blocks` the request is refused by arithmetic
/// rather than by exhaustion: an *empty* pool would refuse it
/// identically, which is precisely the test for whether "retry shortly"
/// is a true statement. Before this existed, such a request slept
/// through `FRINK_KV_POOL_QUEUE_TIMEOUT_MS` and then got a 503 with a
/// `Retry-After` that could never come good.
///
/// Priced in real KV bytes through `ceiling`'s `KvShape` when one is
/// available, so the refusal names bytes rather than an opaque block
/// count. Positions, not bytes, decide it: the pool's own ledger is in
/// positions and this must agree with the acquisition it is predicting.
fn pool_immovable_refusal(
    decoder: &Decoder,
    config: &KvPoolConfig,
    max_seq_len: usize,
) -> Option<DecodeError> {
    let (block_size, total_blocks) = {
        let pool = config.pool.lock().unwrap_or_else(|p| p.into_inner());
        (pool.block_size(), pool.total_blocks())
    };
    if block_size == 0 || decoder.layers.is_empty() {
        return None;
    }
    let blocks_per_layer = max_seq_len.div_ceil(block_size).max(1);
    let needed = blocks_per_layer.saturating_mul(decoder.layers.len());
    if needed <= total_blocks {
        return None;
    }
    // The largest per-layer reservation the whole pool could cover, and
    // therefore the longest sequence it can ever hold. Floors to zero
    // for a pool too small for even one block per layer, which is an
    // honest answer: such a pool serves nothing.
    let blocks_per_layer_limit = total_blocks / decoder.layers.len();
    let positions_limit = blocks_per_layer_limit * block_size;
    let shape = KvShape::from_config(&decoder.config, KvElem::F32);
    Some(DecodeError::KvBudgetExceeded {
        binding: Ceiling::DeviceMemory.code(),
        estimated_bytes: shape.kv_bytes_for_tokens(max_seq_len),
        limit_bytes: shape.kv_bytes_for_tokens(positions_limit),
        positions: max_seq_len,
        positions_limit,
        detail: format!(
            "request needs {needed} KV pool blocks ({max_seq_len} token positions at \
             {block_size} per block, across {} layers) but the whole pool is {total_blocks} \
             blocks; an idle server would refuse it identically",
            decoder.layers.len()
        ),
    })
}

fn acquire_pooled_caches(
    decoder: &Decoder,
    config: &KvPoolConfig,
    max_seq_len: usize,
) -> Result<Vec<KvCache>, CacheKvPoolExhausted> {
    let deadline = Instant::now() + config.queue_wait;
    loop {
        let attempt: Result<Vec<KvCache>, CacheKvPoolExhausted> = decoder
            .config
            .new_kv_caches_with_pool(&config.pool, max_seq_len);
        let now = Instant::now();
        if attempt.is_ok() || now >= deadline {
            return attempt;
        }
        std::thread::sleep(Duration::from_millis(10).min(deadline - now));
    }
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub enum FinishReason {
    Stop,
    /// A caller-supplied stop STRING matched, and this is the one that
    /// did.
    ///
    /// Separate from [`Stop`](FinishReason::Stop), which is the model
    /// ending its own turn, because two protocols ask which it was:
    /// Anthropic reports `stop_reason: "stop_sequence"` with the text
    /// beside it, and an agent branches on that to tell "I hit the
    /// fence I put up" from "the model was done". Both still answer
    /// `"stop"` on the OpenAI surface, which has no field for the
    /// distinction.
    ///
    /// The string is the stop as the CALLER spelled it, not the text
    /// that matched it -- they are the same today and the caller's
    /// spelling is the one it can compare against.
    StopSequence(String),
    Length,
    /// A canceller asked for this generation to stop; see the `cancel`
    /// module. Deliberately not folded into `Stop`: the tokens that did
    /// arrive are a partial answer, and a client that cannot tell a
    /// completed answer from an interrupted one will show the second as
    /// the first.
    Cancelled,
}

impl FinishReason {
    pub fn as_str(&self) -> &'static str {
        match self {
            // A caller's stop string is still "stop" here: OpenAI's
            // vocabulary has no other value for it, and inventing one
            // would make a completed answer look like a failure to a
            // client that checks against the documented set. The
            // surfaces that CAN say more read the variant instead.
            FinishReason::Stop | FinishReason::StopSequence(_) => "stop",
            FinishReason::Length => "length",
            // Not an OpenAI-defined value -- OpenAI has no cancel
            // endpoint to produce one. A client that does not know the
            // string still sees a terminated stream with *a* finish
            // reason, which is what stops it being read as truncation.
            FinishReason::Cancelled => "cancelled",
        }
    }

    /// The caller-supplied stop string this generation ran into, if it
    /// ran into one.
    ///
    /// `None` covers every other ending, a stop TOKEN included: a
    /// control token in the caller's stop set is not a string the
    /// caller can compare its own `stop` list against, so naming one
    /// here would report a match the caller never asked for.
    pub fn matched_stop(&self) -> Option<&str> {
        match self {
            FinishReason::StopSequence(stop) => Some(stop),
            _ => None,
        }
    }

    /// Proof that this generation produced a WHOLE answer, or `None`
    /// if it did not.
    ///
    /// The match is exhaustive with no `_` arm on purpose: it is the
    /// one place in the server that decides what "the answer is
    /// finished" means, and a variant added to this enum later must
    /// stop this crate compiling here rather than defaulting to
    /// whichever side of the question its author never considered.
    ///
    /// Anything that stores an answer for a LATER caller needs this,
    /// because storing a partial answer republishes it as a finished
    /// one. Today that is the whole-response cache; see
    /// [`crate::response_cache::CachedCompletion::cacheable`], which is
    /// the only holder of a [`Completed`] outside this module and
    /// cannot mint one itself.
    pub fn completed(&self) -> Option<Completed> {
        match self {
            // Every one of these is the generation reaching an end the
            // request asked for: the model's own turn end, a stop
            // string the caller supplied, or the caller's own token
            // budget (`max_tokens` is part of the cache key, so
            // replaying a `Length` answer replays it under the same
            // budget that produced it).
            FinishReason::Stop | FinishReason::StopSequence(_) | FinishReason::Length => {
                Some(Completed(()))
            }
            // A canceller cut this short. The tokens that arrived are
            // the honest answer to THIS request and a truncation of
            // every other one.
            FinishReason::Cancelled => None,
        }
    }
}

/// Evidence that a generation ran to an end of its own, produced only
/// by [`FinishReason::completed`].
///
/// The unit field is private to this module, so no other module can
/// build one, clone one out of thin air, or forget to obtain one: a
/// function that requires a `Completed` is a function a partial answer
/// cannot be passed to. That is the difference between this and a
/// `bool` beside the data, which the next caller does not have to look
/// at (#57).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Completed(());

/// OpenAI-convention token accounting, reported in the response's
/// `usage` field. Counted from the exact token ids the generation loop
/// processed (prompt after BOS insertion, and every generated id), not
/// re-tokenized after the fact.
///
/// Defined in `frink-api` rather than here because the shape is part
/// of the public wire contract: the UI, `frink chat` and any external
/// client read these fields, so exactly one definition may exist (see
/// that crate's module docs for why the prefill and decode phases stay
/// separate).
pub use frink_api::Usage;

#[derive(Clone)]
pub struct GenerationParams {
    /// The reasoning split this request's checkpoint implies, resolved
    /// once by whoever holds the model name
    /// (`ReasoningFormat::infer`). `None` when the checkpoint has no
    /// reasoning format at all, which is what makes
    /// `usage.completion_tokens_details` ABSENT rather than a zero the
    /// model did not earn.
    ///
    /// Whether the prompt already opened the block is not carried here:
    /// it is a property of the rendered prompt, which `generate` has,
    /// so deriving it there keeps one source instead of two.
    pub reasoning: Option<crate::policy::parser::ReasoningFormat>,
    pub max_tokens: usize,
    pub sampling: SamplingParams,
    pub seed: u64,
    /// OpenAI's `n`: how many completions to return for ONE prompt.
    ///
    /// `1` is every request that says nothing, and is the path this
    /// server has always taken. Above 1 the prompt is prefilled ONCE
    /// and the KV cache forked per choice, which is the only reason
    /// the field exists -- a caller who wanted `k` independent
    /// generations could already send `k` requests
    /// (`docs/plans/several-completions-per-request.md`).
    ///
    /// Choice `i` samples from `seed + i`, derived rather than drawn,
    /// so a seeded request is reproducible and choice 0 of `n = 4` is
    /// byte-identical to the single answer of `n = 1`.
    pub n: usize,
    /// Whether the choices must arrive INTERLEAVED, a token at a time,
    /// rather than one completion after another.
    ///
    /// What a streaming `n` needs, and the only thing that was ever
    /// missing for it: a client reading `choices[].index` expects the
    /// choices together, and the sequential order gives it choice 0's
    /// whole answer before choice 1 says anything.
    ///
    /// It costs the drafter (`crate::round_robin`): a speculative
    /// round commits a BLOCK, and delivering five tokens of one choice
    /// and then five of the next is the bursty delivery this exists to
    /// avoid. A buffered request keeps the sequential order and keeps
    /// speculation with it, because the order it collects in cannot be
    /// observed.
    pub interleave_choices: bool,
    /// Whether this request will READ the per-token distribution.
    ///
    /// A flag rather than "report always", because reporting costs the
    /// greedy fast path: `Sampler::sample_reporting` has to build the
    /// full filtered distribution even for a chain that would have
    /// answered with an argmax. A request that will not read it should
    /// not pay for it.
    pub wants_logprobs: bool,
    /// How many alternatives to report per PROMPT position, or `None`
    /// for a request that did not ask.
    ///
    /// Separate from `wants_logprobs` because the costs are different
    /// and so are the refusals: scoring the prompt projects the
    /// lm_head at every prompt position rather than once, and the
    /// paged store cannot do it at all (its prefill skips what the
    /// radix tree already holds, so it has no rows for those
    /// positions).
    pub prompt_logprobs: Option<usize>,
    /// vLLM's `cache_salt`: which caller's namespace this request's
    /// prefix cache entries belong to, hashed to a `u64`.
    ///
    /// `None` is the shared namespace, which is what every request got
    /// before this field existed and what every request that names no
    /// salt still gets. A prefix cache is shared state keyed by token
    /// ids, so WITHOUT a salt one caller's prompt can be answered from
    /// another's cached prefix -- that is not a performance question
    /// but an isolation one, and naming a salt is how a caller asks
    /// for their own.
    pub cache_salt: Option<u64>,
    pub stop: Vec<String>,
    /// Stop strings that are exactly one token in this model's
    /// vocabulary, resolved once by whoever holds the tokenizer.
    ///
    /// Layer 1 of [`crate::stop`]: matched on the id, before the token
    /// is detokenized, so a control token stops the answer whatever it
    /// renders as. Empty when no stop string is a single token, or when
    /// the caller has no tokenizer to resolve them with.
    pub stop_token_ids: Vec<usize>,
    /// When true, constrain sampling toward JSON-safe token pieces and
    /// validate the emitted text is a JSON object (best-effort; see
    /// `json_mode` module).
    pub json_object: bool,
    /// A GBNF grammar every sampled token must keep parseable.
    ///
    /// The *initial* machine, shared: a request's live parse state is
    /// per-generation and lives in `sample_step::SampleState`, which
    /// clones this on its first constrained step. An `Arc` because
    /// `GenerationParams` is cloned per request and a compiled grammar
    /// is a rule table, not a flag.
    ///
    /// This is the real constraint; `json_object` above is the
    /// stateless character-class approximation that predates it. They
    /// compose (both masks run, neither can unmask), so a request may
    /// set both, and a `json_object` request whose grammar is
    /// `json.gbnf` is simply the strict version of itself.
    pub grammar: Option<std::sync::Arc<frink_models::grammar::Grammar>>,
    /// Cooperative stop flag, polled once per decoded token.
    ///
    /// It rides on the params rather than being a separate argument
    /// because every path that already threads params through -- the
    /// streaming and non-streaming chat handlers, `/v1/completions`,
    /// the Anthropic surface -- then gets cancellation without a new
    /// parameter each, and a caller that has no cancellation to offer
    /// leaves it `None`. See the `cancel` module for the two tiers that
    /// set it.
    pub cancel: Option<crate::cancel::CancelToken>,
    /// Run past the model's own end-of-generation tokens.
    ///
    /// For benchmarking, and for nothing else. A serving run needs every
    /// request to produce EXACTLY `max_tokens`, or the slowest
    /// percentile is whichever request happened to be asked for the
    /// most tokens -- a fact about the prompts, reported as a fact about
    /// the server.
    ///
    /// Suppresses the MODEL's set only. A stop string or stop token the
    /// caller supplied still ends the answer: the caller asking to
    /// ignore the model's opinion about length is not the caller
    /// withdrawing their own fence.
    pub ignore_eos: bool,
    /// llama.cpp's `reasoning_budget_tokens`: a token budget for the
    /// chain of thought, enforced in the sampler by forcing the closer
    /// once it is spent. `Unrestricted` (llama.cpp's `-1`) is no
    /// machine at all. A `Requested` number becomes an `Armed` plan at
    /// the same seam that resolves `stop_token_ids`, because both need
    /// the tokenizer -- and the sampler refuses a `Requested` budget
    /// rather than run without it. See [`crate::reasoning_budget`].
    pub reasoning_budget: crate::reasoning_budget::ReasoningBudget,
    /// A per-request override of every LoRA adapter's scale, by id --
    /// the request's `lora: [{id, scale}]` field resolved against the
    /// loaded adapters (`crate::lora::resolve_request`). `None` runs
    /// with the scales `POST /lora-adapters` (or the command line) set;
    /// `Some` makes the generation exclusive for its duration.
    pub lora: Option<Vec<f32>>,
}

impl GenerationParams {
    /// Whether a canceller has asked this generation to stop.
    ///
    /// `None` -- no cancellation wired up -- is never cancelled, so a
    /// caller that does not care pays one branch and no atomic.
    pub(crate) fn is_cancelled(&self) -> bool {
        self.cancel.as_ref().is_some_and(|c| c.is_cancelled())
    }

    /// Whether anything about this request has to LOOK at the logits of
    /// the whole vocabulary before a token is chosen.
    ///
    /// The question exists because a decode backend is allowed to skip
    /// producing a vocabulary-shaped vector at all: the Metal dense and
    /// MoE stacks fold `final_norm + lm_head + argmax` onto the device
    /// and hand `forward_token` back a ONE-element vector holding the
    /// chosen id (`frink_models::decoder`, guarded by
    /// `FoldedLmHead::permit`). That is a large win, and it is only
    /// sound when nothing downstream needs the vocabulary.
    ///
    /// JSON-object mode needs it: [`crate::json_mode::mask_logits_for_json`]
    /// scores every vocabulary entry. Handed a one-element vector it
    /// masks one number that is not a logit, the constraint silently
    /// does nothing, and the caller gets a 200 carrying unstructured
    /// text. That was the live bug -- and `temperature: 0` with
    /// `response_format: {"type":"json_object"}` is the ORDINARY way to
    /// ask for structured output, so it was the common case rather than
    /// a corner.
    ///
    /// It is a predicate rather than a second clause on the fold's own
    /// condition so that the next thing to need the vocabulary -- a
    /// grammar, `logit_bias`, a min-p that must see the full
    /// distribution -- is one arm added here and is then true everywhere
    /// the question is asked. Both askers are named in the doc of
    /// [`greedy_gpu_fold_allowed`].
    ///
    /// The grammar arm is that next thing, and it needs the vocabulary
    /// even harder than JSON mode does: a grammar's mask is the ONLY
    /// reason its output parses, so a folded argmax under a grammar is
    /// unconstrained text served against a `response_format` the caller
    /// was told was honoured.
    ///
    /// The sampler arm is the third such thing, and it arrived with
    /// llama.cpp's `xtc` and `typ_p`. Both of those can remove the
    /// MAXIMUM from the candidate list, and `dry` can move which logit
    /// the maximum is, so at `temperature <= 0` the answer is no longer
    /// the argmax of the raw logits. A device that folded the argmax
    /// away would hand the sampler one precomputed id with no
    /// vocabulary left for XTC to remove anything from, and XTC would
    /// silently not run. `SamplingParams::greedy_equals_raw_argmax` is
    /// the single predicate deciding that, shared with
    /// `frink_cli::run`'s copy of this gate.
    ///
    /// The repetition / presence / frequency penalties are the FOURTH,
    /// and they were missing from that predicate until GitHub issue
    /// #170. They move logits over the whole vocabulary before the
    /// candidate list exists, so a device argmax over raw logits skips
    /// them entirely -- a wrong token, not a wrong distribution. A
    /// request carrying any of `repeat_penalty`, `presence_penalty` or
    /// `frequency_penalty` therefore needs the vocabulary even at
    /// `temperature: 0`.
    pub(crate) fn needs_vocab_logits(&self) -> bool {
        self.json_object
            || self.grammar.is_some()
            || self.reasoning_budget.needs_vocab_logits()
            || !self.sampling.greedy_equals_raw_argmax()
    }
}

/// Whether this request may let a backend fold `lm_head + argmax` into
/// its decode stack and return a token id instead of logits.
///
/// Greedy decoding is a necessary condition -- an argmax computed on
/// device cannot be re-sampled at temperature -- but it is NOT a
/// sufficient one, and treating it as sufficient is what broke JSON mode
/// at `temperature: 0`. The fold is sound only when the answer to
/// [`GenerationParams::needs_vocab_logits`] is also no.
///
/// A free function, taking the params, so it can be asserted in the
/// default (non-Metal) build the gates actually run: the fold itself is
/// `#[cfg(feature = "metal")]`, and a condition that only type-checks
/// under a feature flag is a condition nothing tests. Same `cfg` shape,
/// and for the same reason, as `FoldedLmHead` on the models side.
#[cfg(any(feature = "metal", test))]
pub(crate) fn greedy_gpu_fold_allowed(params: &GenerationParams) -> bool {
    params.sampling.temperature <= 0.0 && !params.needs_vocab_logits()
}

fn chunked_prefill_tokens() -> Option<usize> {
    std::env::var(
        crate::prefill_batch::PREFILL_CHUNK_ENV_KEYS[crate::prefill_batch::PRIVATE_PATH_KEY],
    )
    .ok()
    .and_then(|v| v.parse().ok())
    .filter(|&n| n > 0)
}

#[cfg(feature = "metal")]
fn cpu_kv_offload_enabled() -> bool {
    matches!(
        std::env::var("FRINK_CPU_KV_OFFLOAD").ok().as_deref(),
        Some("1")
    )
}

/// Batched prompt prefill, optionally split into `FRINK_CHUNKED_PREFILL`-sized
/// chunks that append into the same KV caches.
/// `host_kv`: whether these caches will be READ afterwards rather than
/// only decoded from. A Metal prefill otherwise leaves the real K/V on
/// the device and the host rows zero-filled, and
/// `sync_metal_attn_kv_to_host` cannot repair that -- it appends past
/// `seq_len`, which the zero fill has already advanced. The prefix
/// cache reads them, so a stored snapshot was all zeros and the next
/// request restoring it answered fluent nonsense. Paid for only when a
/// prefix cache is configured, because it costs one KV download per
/// layer and nothing else in this path reads the rows back.
pub(crate) fn forward_prompt_batch(
    decoder: &Decoder,
    tokens: &[usize],
    start_pos: usize,
    caches: &mut [KvCache],
    host_kv: bool,
) -> Vec<f32> {
    let run = |part: &[usize], pos: usize, caches: &mut [KvCache]| {
        if host_kv {
            decoder.forward_batch_last_host_kv(part, pos, caches)
        } else {
            decoder.forward_batch_last(part, pos, caches)
        }
    };
    if let Some(chunk) = chunked_prefill_tokens() {
        let mut pos = start_pos;
        let mut last = Vec::new();
        for part in tokens.chunks(chunk) {
            last = run(part, pos, caches);
            pos += part.len();
        }
        last
    } else {
        run(tokens, start_pos, caches)
    }
}

/// The server's decode engine: one forward, and speculation when the
/// store and the request allow it.
///
/// It owns the `&mut Kv` because a speculative round batches and rolls
/// back the same store a step writes to, and two borrows of it cannot
/// coexist (`sampling_loop::DecodeEngine` says why).
///
/// The drafter is `PromptLookupSpeculator`: an n-gram match over the
/// history with no second checkpoint, so it costs no memory, no load
/// time and no second tokenizer, and it is useful exactly where a
/// coding agent lives -- output that quotes its input. A model-based
/// drafter is another `Drafter` impl, not another engine.
struct ServerEngine<'a> {
    decoder: &'a Decoder,
    kv: &'a mut Kv,
    /// Shared by every choice's engine, so a `Cell` rather than a
    /// `&mut`: `n` completions decode in one request and the time to
    /// FIRST token is the request's, whichever choice produced it.
    first_token_at: &'a std::cell::Cell<Option<std::time::Instant>>,
    #[cfg(feature = "metal")]
    kv_offload: bool,
    drafter: Option<frink_models::speculative::PromptLookupSpeculator>,
    accepted: usize,
    drafted: usize,
    /// FORWARD PASSES, not speculative rounds.
    ///
    /// `acceptance_length` divides the answer by this, and a round
    /// that drafted nothing still costs a forward. Counting only the
    /// speculative rounds reported 24 tokens over 3 rounds as 8.0
    /// tokens per step while the run had actually taken more forwards
    /// than that -- a number that flatters the drafter. Tokens per
    /// forward is both honest and the speedup itself: 1.0 means
    /// speculation bought nothing.
    forwards: usize,
}

impl ServerEngine<'_> {
    fn sync_after_step(&mut self) {
        #[cfg(feature = "metal")]
        if self.kv_offload {
            if let Some(caches) = self.kv.contiguous_mut() {
                self.decoder.sync_metal_attn_kv_to_host(caches);
            }
        }
    }
}

impl crate::sampling_loop::DecodeEngine for ServerEngine<'_> {
    fn step(&mut self, next: usize, pos: usize) -> Vec<f32> {
        if self.first_token_at.get().is_none() {
            self.first_token_at.set(Some(std::time::Instant::now()));
        }
        // The anchor is offered the token that is about to be fed
        // forward, at the length that includes it. A token that ended
        // the generation never reaches here, which is the `finished`
        // case `observe` refuses -- there would be no continuation to
        // rejoin at.
        if let Kv::Paged(lease) = &mut *self.kv {
            lease.observe_sampled(next, pos + 1, false);
        }
        self.forwards += 1;
        let l = self.kv.step(self.decoder, next, pos);
        self.sync_after_step();
        l
    }

    fn draft(&mut self, _prompt: &[usize], history: &[usize], max: usize) -> Vec<usize> {
        let Some(d) = self.drafter.as_ref() else {
            return Vec::new();
        };
        let mut proposal = d.propose_tokens(history);
        proposal.truncate(max);
        proposal
    }

    fn batch(&mut self, tokens: &[usize], pos: usize) -> Option<Vec<Vec<f32>>> {
        let rows = self.kv.step_batch(self.decoder, tokens, pos)?;
        self.forwards += 1;
        self.sync_after_step();
        Some(rows)
    }

    fn truncate(&mut self, pos: usize) {
        // `step_batch` refused the stores that cannot do this, so a
        // failure here would mean the two have drifted apart.
        debug_assert!(
            self.kv.truncate_to(pos),
            "a store that accepted a speculative batch must be able to roll it back"
        );
        self.sync_after_step();
    }

    fn observe(&mut self, accepted: usize, drafted: usize) {
        self.accepted += accepted;
        self.drafted += drafted;
    }
}

/// Runs the prompt through `decoder`, then generates up to
/// `params.max_tokens` new tokens, calling `emit` with each newly-safe-
/// to-flush chunk of decoded text as it becomes available (see the
/// stop-sequence buffering note below). Returns the reason generation
/// stopped.
///
/// Stop-sequence correctness: a stop string can span more than one
/// generated token (e.g. stop=" END" while the tokenizer emits " "
/// and "END" as separate pieces), so text can't simply be flushed
/// token-by-token as soon as it's decoded -- the tail end of the
/// buffer might still turn into part of a stop match once the next
/// token arrives. This holds back the last `longest_stop_len - 1`
/// bytes of decoded text (respecting UTF-8 char boundaries) until
/// they're confirmed clean, the same buffering approach real
/// inference servers use for this exact reason.
/// What a generation hands back before a wire shapes it: one
/// `(finish reason, per-token distributions)` per choice, the prompt's
/// logit rows and the ids they came from, and the usage.
///
/// A named type because clippy is right that the tuple had become
/// unreadable, and because it is the fourth thing to be added to it in
/// a day -- see `Generated`, which is this plus the text the emit
/// callback collected.
pub(crate) type GenerationOutput = (
    Vec<(FinishReason, crate::sampling_loop::PerTokenProbs)>,
    Vec<Vec<f32>>,
    Vec<usize>,
    Usage,
);

/// One completion of a request, with everything a wire may render for
/// it.
///
/// A struct rather than a tuple because this grew three times in one
/// day -- `FinishReason`, then `(FinishReason, String)`, then a third
/// member for the distributions -- and each widening was a mechanical
/// edit across every caller. A named field is added once and read by
/// the wires that want it.
/// Everything one request produced.
///
/// A struct because the tuple it replaces grew FOUR times in one day
/// -- `FinishReason`, then with the text, then with the per-choice
/// distributions, then with the prompt's. Each widening was a
/// mechanical edit across every caller, and the fourth is the one that
/// should have been the first.
///
/// `prompt_rows` sits here rather than on a choice because it belongs
/// to the REQUEST: the prompt was prefilled once and scored once,
/// whatever `n` was.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Generated {
    pub(crate) choices: Vec<GeneratedChoice>,
    /// One logit row per prompt position, EMPTY unless the request
    /// asked to score the prompt.
    pub(crate) prompt_rows: Vec<Vec<f32>>,
    /// The prompt ids those rows were computed from, INCLUDING the BOS
    /// the tokenizer prepended.
    ///
    /// Carried beside the rows rather than re-derived at the route,
    /// for the same reason `PerTokenProbs` carries its ids: a renderer
    /// that re-tokenized the prompt could line up a different sequence
    /// with these rows and report a score for a token the model never
    /// saw.
    pub(crate) prompt_ids: Vec<usize>,
    pub(crate) usage: Usage,
}

#[derive(Debug, Clone, PartialEq)]
pub(crate) struct GeneratedChoice {
    pub(crate) finish: FinishReason,
    pub(crate) text: String,
    /// Per-token `(id, distribution it was drawn from)`, EMPTY unless
    /// the request set `GenerationParams::wants_logprobs`.
    pub(crate) logprobs: crate::sampling_loop::PerTokenProbs,
}

#[allow(clippy::too_many_arguments)]
// one clear parameter per concern; a
// bundling struct here would just be GenerationParams's fields plus
// decoder/tokenizer/stop_tokens/bos_id/prompt/kv_pool/prefix_cache/emit
// re-wrapped for no real benefit at this call depth (two call sites,
// both in this crate).
pub fn generate(
    decoder: &Decoder,
    tokenizer: &ServerTokenizer,
    stop_tokens: &StopTokens,
    bos_id: Option<usize>,
    prompt: &str,
    params: &GenerationParams,
    kv_pool: Option<&KvPoolConfig>,
    paged_kv: Option<&PagedKvConfig>,
    prefix_cache: Option<&Mutex<PrefixCache>>,
    ceiling: Option<&ContextCeiling>,
    mut emit: impl FnMut(usize, &str),
) -> Result<GenerationOutput, DecodeError> {
    let vocab_size = decoder.config.vocab_size;

    // Metal greedy GPU argmax: fold final_norm+lm_head+argmax into the
    // dense-stack CB and download one token id instead of hidden/vocab.
    // Thread-local so concurrent Arc<Decoder> requests do not race.
    //
    // `greedy_gpu_fold_allowed` and not `temperature <= 0.0`: the fold
    // returns a token id where a caller-supplied logit constraint
    // expects a vocabulary, and the constraint would then apply to
    // nothing at all. See `GenerationParams::needs_vocab_logits`.
    #[cfg(feature = "metal")]
    let _metal_greedy_guard = {
        struct Guard;
        impl Drop for Guard {
            fn drop(&mut self) {
                frink_models::set_metal_greedy_argmax(false);
            }
        }
        if greedy_gpu_fold_allowed(params) {
            frink_models::set_metal_greedy_argmax(true);
            Some(Guard)
        } else {
            None
        }
    };

    // `Parse`: the prompt is what a chat template rendered, or a raw
    // completion, and llama.cpp's server tokenizes both with
    // `parse_special = true`. See `Model::encode`.
    let mut tokens = tokenizer.encode(prompt, SpecialTokens::Parse);
    prepend_bos(&mut tokens, bos_id);
    let prompt_tokens = tokens.len();
    if let Some(&bad) = tokens.iter().find(|&&t| t >= vocab_size) {
        return Err(DecodeError::TokenOutOfVocab {
            token: bad,
            vocab_size,
        });
    }

    // With a shared pool configured, admission control happens here:
    // each layer's cache reserves enough blocks up front for this
    // request's real worst case (prompt length + max_tokens), not just
    // one block -- see `acquire_pooled_caches`'s doc comment for why
    // under-reserving here would let a request panic mid-decode
    // instead of failing cleanly at admission time. If any layer can't
    // get that many blocks, `acquire_pooled_caches` retries (bounded by
    // `config.queue_wait`) before the request is rejected. Each failed
    // attempt's partial `Vec` is dropped immediately (via `collect`),
    // releasing any blocks it did acquire through `KvCache`'s `Drop`
    // impl before the next retry, so a request that ultimately gives up
    // leaves the pool exactly as it found it.
    //
    // Prefix-cache restoration only applies when there's no shared KV
    // block pool: a restored cache is a plain, unpooled clone (see
    // `KvCache`'s `Clone` doc comment), so combining it with pool-based
    // admission control would let a request's real memory usage
    // silently bypass the pool's bounded-memory guarantee. Not
    // supported together yet.
    // The per-request context ceiling, checked before any KV is
    // acquired and before a single forward pass runs. This path used to
    // have no context ceiling at all: an oversized request either ran
    // until something else killed it, or -- with a pool configured --
    // waited out `queue_wait` and left with a 503 "retry shortly" about
    // a request an idle server would refuse identically. The ceiling is
    // the same `ContextCeiling` the batch scheduler admits on (see
    // `crate::budget`), so the two paths cannot disagree.
    //
    // It has TWO outcomes, not one. A prompt at or past the ceiling has
    // no output budget left to give it and is refused. A prompt that
    // fits is SERVED, with `max_tokens` clamped down to what remains --
    // refusing that case instead turns a servable 100k-prompt request
    // into a 400 over a `max_tokens` the caller very likely never set.
    // The clamp is what makes a large default output budget safe.
    let clamped;
    let params = match ceiling {
        // `ContextCeiling::fit` IS the two outcomes above, and it is
        // shared with the continuous batcher's admission so the two
        // decode paths cannot answer the same request differently.
        Some(ceiling) => match ceiling.fit(prompt_tokens, params.max_tokens)? {
            fitted if fitted == params.max_tokens => params,
            fitted => {
                let mut p = params.clone();
                tracing::debug!(
                    "max_tokens clamped from {} to {fitted} by the context ceiling",
                    p.max_tokens
                );
                p.max_tokens = fitted;
                clamped = p;
                &clamped
            }
        },
        None => params,
    };
    // `params` is the clamped copy by now, so this cannot exceed the
    // ceiling when one exists. `checked_add` covers the case where none
    // does: an unbounded deployment must still not wrap into a small
    // number and pass every check below it.
    let Some(max_seq_len) = prompt_tokens.checked_add(params.max_tokens) else {
        return Err(DecodeError::KvBudgetExceeded {
            binding: frink_models::Ceiling::ContextLength.code(),
            estimated_bytes: 0,
            limit_bytes: 0,
            positions: usize::MAX,
            positions_limit: usize::MAX,
            detail: format!(
                "prompt of {prompt_tokens} tokens plus max_tokens of {} overflows the position \
                 counter, so this request cannot be served by any deployment",
                params.max_tokens
            ),
        });
    };

    // A request whose worst case exceeds the *whole* pool is not a
    // request to retry: no amount of waiting frees blocks that do not
    // exist. Separated from `KvPoolExhausted` (503) for exactly the
    // reason the batch scheduler separates `kv_rejected_too_large` from
    // `queue_rejected` -- one says "come back later", the other says
    // "this will never work", and an operator sent to the wrong one
    // tunes the wrong knob.
    if let Some(config) = kv_pool {
        if let Some(err) = pool_immovable_refusal(decoder, config, max_seq_len) {
            return Err(err);
        }
    }

    let restored = if kv_pool.is_none() {
        prefix_cache.and_then(|pc| {
            let m = pc
                .lock()
                .unwrap_or_else(|p| p.into_inner())
                .find_longest_prefix_salted(&tokens, params.cache_salt);
            (m.matched_len > 0).then_some(m)
        })
    } else {
        None
    };

    // Prompt tokens this request will *not* have to recompute. Reported
    // as `usage.cached_tokens` -- `Some(0)` when a prefix cache exists
    // and missed, `None` when there is no prefix cache to consult, so a
    // client can tell "no hit" from "no cache".
    let cached_tokens = restored
        .as_ref()
        .map(|m| m.matched_len)
        .or_else(|| (prefix_cache.is_some() && kv_pool.is_none()).then_some(0));

    // One logit row per prompt position, only when the request asked
    // to score the prompt.
    let mut prompt_rows: Vec<Vec<f32>> = Vec::new();
    // Kept because the tail consumes `tokens`.
    let mut tokens_for_scoring: Vec<usize> = Vec::new();
    let prefill_start = std::time::Instant::now();
    let mut pos;
    let mut logits: Vec<f32>;
    let mut kv: Kv;
    if let Some(m) = restored {
        // Only the contiguous path reaches here: a prefix cache and a
        // paged store are refused together at startup, because
        // `PrefixCache` stores `Vec<KvCache>` snapshots a paged request
        // cannot produce.
        kv = Kv::Contiguous(
            m.kv_caches
                .expect("matched_len > 0 always carries kv_caches"),
        );
        let caches = &mut *kv
            .contiguous_mut()
            .expect("a restored prefix is contiguous by construction");
        let suffix = &tokens[m.matched_len..];
        if suffix.is_empty() {
            if let Some(pl) = m.pending_logits {
                // The whole query was already processed and stored
                // verbatim before (e.g. an exact-repeat prompt with
                // unseeded sampling, so the whole-response cache
                // couldn't serve it) -- zero forward passes needed.
                pos = m.matched_len;
                logits = pl;
            } else {
                // Rare: our query exactly matches a strict PREFIX of a
                // longer stored entry (someone else's conversation
                // continued past this point), so there's no stored
                // "what comes next" for our shorter query. Back the
                // restored cache off by one position and reprocess
                // just that last matched token to get real logits,
                // rather than guessing.
                let back_to = m.matched_len - 1;
                for c in caches.iter_mut() {
                    c.truncate(back_to);
                }
                pos = back_to;
                logits = decoder.forward_token(tokens[back_to], pos, caches);
                pos += 1;
            }
        } else {
            pos = m.matched_len;
            let mut l = Vec::new();
            for &tok in suffix {
                l = decoder.forward_token(tok, pos, caches);
                pos += 1;
            }
            logits = l;
        }
    } else {
        kv = match (paged_kv, kv_pool) {
            (Some(config), _) => Kv::Paged(
                acquire_paged_caches(decoder, config, &tokens, max_seq_len, params.cache_salt)
                    .map_err(|_| DecodeError::KvPoolExhausted)?,
            ),
            (None, Some(config)) => Kv::Contiguous(
                acquire_pooled_caches(decoder, config, max_seq_len)
                    .map_err(|_| DecodeError::KvPoolExhausted)?,
            ),
            (None, None) => Kv::Contiguous(decoder.config.new_kv_caches()),
        };
        // Process the prompt once, capturing the *last* call's logits
        // (which already predict the first generated token) instead of
        // discarding them. A prompt of zero tokens (bos_id unset and an
        // empty-string prompt encoding to nothing) has no real token to
        // seed from, so a synthetic id 0 bootstraps decoding.
        pos = 0;
        logits = if tokens.is_empty() {
            let l = kv.step(decoder, 0, pos);
            pos += 1;
            l
        } else {
            // Batched prefill: one pass over the prompt (shared weight
            // traffic on CPU; fewer per-token bookkeeping costs). Last
            // row's logits predict the first generated token — same as
            // the sequential loop this replaces (see unit test below).
            // When `FRINK_CHUNKED_PREFILL` is set, split long prompts
            // into chunks that reuse the same KV caches.
            pos = tokens.len();
            // Scoring the prompt needs a logit row per POSITION, which
            // is a separate entry point because it projects the
            // lm_head at every one instead of once
            // (`Kv::prefill_scored`). A request that did not ask keeps
            // the cheap path exactly as it was.
            if params.prompt_logprobs.is_some() {
                let Some(rows) = kv.prefill_scored(decoder, &tokens) else {
                    // The paged store's prefill SKIPS whatever the
                    // radix tree already holds, so it has no rows for
                    // those positions and scoring them would be an
                    // invention. Refused by name rather than reported
                    // with holes.
                    return Err(DecodeError::Unsupported(
                        "`prompt_logprobs` needs a logit row for every prompt position, and a \
                         paged request's prefill skips the positions the prefix tree already \
                         holds. Serve it without `--paged-kv`."
                            .to_string(),
                    ));
                };
                let last = rows.last().cloned().unwrap_or_default();
                tokens_for_scoring = tokens.clone();
                prompt_rows = rows;
                last
            } else {
                // The prefix cache is the only thing here that reads
                // these caches back; without one, the download is pure
                // cost.
                kv.prefill(decoder, &tokens, prefix_cache.is_some())
            }
        };
    }

    let prefill_secs = prefill_start.elapsed().as_secs_f64();
    let decode_start = std::time::Instant::now();
    #[cfg(feature = "metal")]
    let kv_offload = cpu_kv_offload_enabled();
    let decode_token = |id: usize| tokenizer.decode(&[id]);

    // `logits` becomes the prediction for the position after each
    // generated token; every generated token gets exactly one
    // corresponding cache entry via the `step` closure below, matching
    // `prefix_cache`'s `pending_logits` expectations regardless of
    // whether the loop goes on to hit a stop sequence or max_tokens.
    // Time-to-first-token is stamped inside the `step` closure rather
    // than approximated as "prefill time": `step` is called immediately
    // after the first token is sampled, so this is the real moment the
    // user could have seen something. Threading it through the closure
    // instead of `sample_until_stop`'s signature keeps that function's
    // (already long) argument list unchanged, and the closure's mutable
    // borrow ends when it is dropped at the call's return.
    let first_token_at: std::cell::Cell<Option<std::time::Instant>> = std::cell::Cell::new(None);
    // Prompt-lookup drafting, when the request and the store allow it.
    //
    // No second checkpoint, so it costs no memory and no load time;
    // `Kv::step_batch` refuses the stores that cannot roll a rejected
    // draft back, which leaves this decision about the REQUEST alone.
    //
    // A grammar is refused here rather than inside the loop: the
    // verification draws through the same grammar machine, and a
    // rejected block would leave it advanced over tokens that were
    // never emitted. That is the one sampler feature the verify-by-
    // agreement rule does not get for free, and refusing is honest
    // where a silent fallback would not be.
    let draft_max = if params.grammar.is_some() || params.json_object {
        0
    } else {
        crate::sampling_loop::DEFAULT_DRAFT_MAX
    };
    // `n` completions of ONE prompt, which is the whole reason the
    // field exists: the prefill above ran once and the forks below
    // start from it (`docs/plans/several-completions-per-request.md`).
    let n = params.n.max(1);
    // The forks are taken from the POST-PREFILL state, before choice 0
    // decodes into `kv` and mutates it. Cloning after would give
    // choices 1.. a cache that already holds choice 0's tokens, which
    // is a different prompt.
    let mut forks: Vec<Kv> = Vec::new();
    if n > 1 {
        // Forked rather than re-prefilled per choice. A caller who
        // asked for four and silently got four prefills paid four
        // times for the thing the field exists to avoid, with nothing
        // in the response saying so.
        match &mut kv {
            Kv::Contiguous(caches) => {
                forks = (1..n).map(|_| Kv::Contiguous(caches.clone())).collect();
            }
            Kv::Paged(lease) => {
                // Copy-on-write: the full pages are shared and only the
                // part-written tail is copied, so the prompt is held
                // ONCE however many choices were asked for.
                for _ in 1..n {
                    match lease.fork() {
                        Ok(fork) => forks.push(Kv::Paged(fork)),
                        // The forks taken so far drop here, returning
                        // their groups, so a refusal costs nothing.
                        Err(why) => {
                            return Err(DecodeError::Unsupported(format!(
                                "`n` > 1 on the paged KV store: {why}"
                            )))
                        }
                    }
                }
            }
        }
    }

    let mut finishes: Vec<FinishReason> = Vec::with_capacity(n);
    // One entry per choice, each the per-token distributions that
    // choice was drawn from. EMPTY vectors when the request did not
    // ask for logprobs, which is every request until a route wires
    // `wants_logprobs`.
    let mut choice_logprobs: Vec<crate::sampling_loop::PerTokenProbs> = Vec::with_capacity(n);
    let mut completion_tokens = 0usize;
    let mut speculation = (0usize, 0usize, 0usize);
    // Choice 0's ids and final logits are what the request tail
    // publishes: the prefix cache stores ONE continuation, and choice 0
    // is the one a later `n = 1` with the same seed reproduces.
    let mut first_generated_ids: Vec<usize> = Vec::new();
    let mut first_logits: Vec<f32> = Vec::new();

    // Derived, not drawn: `n: 4, seed: 7` is stable across runs and
    // across `n`, so choice 0 of four is byte-identical to the single
    // answer of one. Built once here because both schedules below read
    // the same list.
    let per_choice_params: Vec<GenerationParams> = (0..n)
        .map(|choice| {
            let mut p = params.clone();
            p.seed = params.seed.wrapping_add(choice as u64);
            p
        })
        .collect();

    if n > 1 && params.interleave_choices {
        // A client reading `choices[].index` asked for the choices
        // together, so they are stepped a token at a time rather than
        // one completion after another (`crate::round_robin`). No
        // drafting: a speculative round commits a BLOCK, and bursts of
        // five tokens per choice are what this schedule exists to
        // avoid.
        let mut kvs: Vec<&mut Kv> = std::iter::once(&mut kv).chain(forks.iter_mut()).collect();
        let mut engines: Vec<ServerEngine> = kvs
            .drain(..)
            .map(|choice_kv| ServerEngine {
                decoder,
                kv: choice_kv,
                first_token_at: &first_token_at,
                #[cfg(feature = "metal")]
                kv_offload,
                drafter: None,
                accepted: 0,
                drafted: 0,
                forwards: 0,
            })
            .collect();
        let contexts: Vec<crate::choice_stream::StepContext<'_>> = per_choice_params
            .iter()
            .map(|p| crate::choice_stream::StepContext {
                params: p,
                prompt_ids: &tokens,
                stop_tokens,
                decode_token: &decode_token,
            })
            .collect();
        let streams: Vec<crate::choice_stream::ChoiceStream> = per_choice_params
            .iter()
            .map(|p| crate::choice_stream::ChoiceStream::new(logits.clone(), pos, p))
            .collect();
        let outcomes = {
            let mut dyns: Vec<&mut dyn crate::sampling_loop::DecodeEngine> = engines
                .iter_mut()
                .map(|e| e as &mut dyn crate::sampling_loop::DecodeEngine)
                .collect();
            crate::round_robin::sample_round_robin(
                streams,
                &mut dyns,
                &contexts,
                &mut |ids| tokenizer.decode_bytes(ids),
                &mut emit,
            )?
        };
        for engine in &engines {
            speculation.0 += engine.forwards;
            speculation.1 += engine.accepted;
            speculation.2 += engine.drafted;
        }
        drop(engines);
        for (choice, (finish, generated_ids, final_logits, choice_probs)) in
            outcomes.into_iter().enumerate()
        {
            completion_tokens += generated_ids.len();
            finishes.push(finish);
            choice_logprobs.push(choice_probs);
            if choice == 0 {
                first_generated_ids = generated_ids;
                first_logits = final_logits;
            }
        }
    } else {
        for choice in 0..n {
            let choice_params = &per_choice_params[choice];

            let choice_kv: &mut Kv = if choice == 0 {
                &mut kv
            } else {
                &mut forks[choice - 1]
            };
            let mut engine = ServerEngine {
                decoder,
                kv: choice_kv,
                first_token_at: &first_token_at,
                #[cfg(feature = "metal")]
                kv_offload,
                drafter: (draft_max > 0).then(|| {
                    frink_models::speculative::PromptLookupSpeculator::new(
                        crate::sampling_loop::DRAFT_NGRAM,
                        draft_max,
                    )
                }),
                accepted: 0,
                drafted: 0,
                forwards: 0,
            };
            let (finish, generated_ids, final_logits, choice_probs) =
                crate::sampling_loop::sample_until_stop(
                    logits.clone(),
                    pos,
                    &tokens,
                    stop_tokens,
                    choice_params,
                    |ids| tokenizer.decode_bytes(ids),
                    &mut engine,
                    &mut |text: &str| emit(choice, text),
                    &decode_token,
                    draft_max,
                )?;
            // Read the counters out BEFORE the borrow ends, because this is
            // the engine's last use and it borrows `kv` and
            // `first_token_at` mutably.
            speculation.0 += engine.forwards;
            speculation.1 += engine.accepted;
            speculation.2 += engine.drafted;
            completion_tokens += generated_ids.len();
            finishes.push(finish);
            choice_logprobs.push(choice_probs);
            if choice == 0 {
                first_generated_ids = generated_ids;
                first_logits = final_logits;
            }
        }
    }
    let decode_secs = decode_start.elapsed().as_secs_f64();
    let generated_ids = first_generated_ids;
    logits = first_logits;
    // Everything after the last token -- the usage block and the three
    // places this request's KV may be published -- is
    // `crate::request_tail`. It is lifted out unchanged, and the seam
    // is not arbitrary: it is exactly the part that runs ONCE per
    // request rather than once per completion, which is what
    // `docs/plans/several-completions-per-request.md` needs next.
    let usage = crate::request_tail::RequestTail {
        decoder,
        tokenizer,
        params,
        prompt,
        tokens,
        generated_ids,
        completion_tokens,
        logits,
        kv,
        prompt_tokens,
        vocab_size,
        prefill_secs,
        decode_secs,
        prefill_start,
        first_token_at: first_token_at.get(),
        cached_tokens,
        speculation,
        kv_pool_configured: kv_pool.is_some(),
        radix_enabled: paged_kv.is_some_and(|c| c.radix.is_some()),
        prefix_cache,
        cache_salt: params.cache_salt,
    }
    .finish();

    let scored_ids = if prompt_rows.is_empty() {
        Vec::new()
    } else {
        tokens_for_scoring
    };
    Ok((
        finishes.into_iter().zip(choice_logprobs).collect(),
        prompt_rows,
        scored_ids,
        usage,
    ))
}

/// Shared sampling + stop-sequence-aware emission loop, given already-
/// primed `logits`/`pos` (the prompt has already been processed by the
/// caller) and a `step` closure that advances one position and returns
/// the new logits. Used by both `generate` (the GGUF path, whose own
/// KV-pool/prefix-cache-aware prompt priming stays separate above) and
/// `generate_engine` (any other `Engine`, with simpler priming and no
/// pooling/restoration) so the actual sampling/stop-sequence
/// correctness -- the part most worth not duplicating -- lives in
/// exactly one place. Returns the finish reason, the generated token
/// ids, and the final logits (the prediction for whatever would come
/// next), since `generate`'s prefix-cache storage needs both.
///
/// The generic counterpart to `generate`, for any `Engine` other than
/// `Decoder` (in practice, Kimi K3's `KimiEngine`). Deliberately
/// simpler: no KV block pool, no `PrefixCache` restoration -- Kimi's
/// KDA state is a fixed-size recurrent matrix that collapses history
/// irreversibly, so it can't support the truncate/restore operations
/// those features need (see `frink_models::engine`'s module docs). Every
/// request processes its full prompt from scratch against fresh
/// engine state.
pub fn generate_engine<E: Engine, T: TextTokenizer>(
    engine: &E,
    tokenizer: &T,
    stop_tokens: &StopTokens,
    bos_id: Option<usize>,
    prompt: &str,
    params: &GenerationParams,
    mut emit: impl FnMut(&str),
    // A `Vec` for the same reason `generate` returns one: two shapes
    // for "what this request produced" is one more chance for a caller
    // to handle the simple case and forget the other. These engines
    // hold a recurrent state that collapses history irreversibly, so
    // they cannot fork a prefix and always answer with exactly one.
) -> Result<GenerationOutput, DecodeError> {
    // These engines cannot fork: a KDA / MLA recurrent state is a
    // reduction over the whole prefix, so there is no cache to clone
    // into a second choice. Refused by name rather than served as one.
    if params.n > 1 {
        return Err(DecodeError::Unsupported(
            "`n` > 1 needs a forkable KV cache, and this checkpoint runs on an engine whose \
             state collapses history irreversibly. Send the request n times."
                .to_string(),
        ));
    }

    let vocab_size = engine.vocab_size();
    // `Parse`, as the GGUF path above. See `Model::encode`.
    let mut tokens = tokenizer.encode(prompt, SpecialTokens::Parse);
    prepend_bos(&mut tokens, bos_id);
    let prompt_tokens = tokens.len();
    if let Some(&bad) = tokens.iter().find(|&&t| t >= vocab_size) {
        return Err(DecodeError::TokenOutOfVocab {
            token: bad,
            vocab_size,
        });
    }

    let mut state = engine.new_state();
    let mut pos = 0;
    let prefill_start = std::time::Instant::now();
    let logits = if tokens.is_empty() {
        let l = engine.forward_token(0, pos, &mut state);
        pos += 1;
        l
    } else {
        let mut l = Vec::new();
        for &tok in tokens.iter() {
            l = engine.forward_token(tok, pos, &mut state);
            pos += 1;
        }
        l
    };
    let prefill_secs = prefill_start.elapsed().as_secs_f64();
    let decode_start = std::time::Instant::now();

    // Timed for the same reason the `Decoder` path is: a UI that has to
    // wall-clock these engines instead cannot separate prefill from
    // decode, and Kimi/MLA prefill is sequential (one forward per prompt
    // token), so the two phases differ by more here, not less.
    let mut first_token_at: Option<std::time::Instant> = None;
    let (finish, generated_ids, _final_logits, _probs) = crate::sampling_loop::sample_until_stop(
        logits,
        pos,
        &tokens,
        stop_tokens,
        params,
        |ids| tokenizer.decode_bytes(ids),
        &mut |next: usize, pos: usize| {
            if first_token_at.is_none() {
                first_token_at = Some(std::time::Instant::now());
            }
            engine.forward_token(next, pos, &mut state)
        },
        &mut emit,
        &|id: usize| tokenizer.decode(&[id]),
        0,
    )?;
    let decode_secs = decode_start.elapsed().as_secs_f64();

    let mut usage =
        Usage::new(prompt_tokens, generated_ids.len()).with_timings(prefill_secs, decode_secs);
    if let Some(at) = first_token_at {
        usage = usage.with_ttft(at.duration_since(prefill_start).as_secs_f64());
    }
    // No distributions: these engines refuse `n` > 1 above and their
    // logprobs row is not wired either.
    Ok((vec![(finish, Vec::new())], Vec::new(), Vec::new(), usage))
}

#[cfg(test)]
mod tests {

    use super::*;
    use frink_models::config::test_dense_fixture;

    /// Which endings may be replayed to a LATER caller, enumerated so
    /// the answer is on the record rather than inferred from whichever
    /// handler happens to hold a cancel token (#57).
    ///
    /// `Length` is on the completed side deliberately: `max_tokens` is
    /// part of the response cache's key, so an answer truncated at the
    /// budget is only ever replayed under the same budget that produced
    /// it. `Cancelled` is the whole of the other side -- it kept the
    /// tokens that had arrived, which answers the cancelled request and
    /// truncates every other one.
    #[test]
    fn only_a_generation_that_reached_an_end_of_its_own_counts_as_completed() {
        for reason in [
            FinishReason::Stop,
            FinishReason::StopSequence("<END>".to_string()),
            FinishReason::Length,
        ] {
            assert!(
                reason.completed().is_some(),
                "{reason:?} produced the whole answer the request asked for"
            );
        }
        assert!(
            FinishReason::Cancelled.completed().is_none(),
            "a cancelled generation is a partial answer and may not be stored \
             for anybody else"
        );
    }

    fn small_decoder() -> Decoder {
        Decoder::new_random_small(test_dense_fixture(), 2, 256)
    }

    /// The server really speculates, and says so in `usage`.
    ///
    /// What this test does NOT do is compare the speculative answer
    /// against an unspeculated one, and the reason is worth stating:
    /// there is no way to turn drafting off for one request without
    /// changing something else about it. The first version used
    /// `json_object` as the control and was wrong -- that turns on a
    /// grammar, so it compared two different requests and the text
    /// differed for that reason rather than because speculation had
    /// changed anything.
    ///
    /// The equivalence claim is proven where it can be proven cleanly:
    /// `sampling_loop::speculation_changes_the_forward_count_and_not_
    /// the_answer` runs the same scripted engine with three drafters
    /// and demands identical ids and text, and the KV seam's own test
    /// pins a batched forward against a per-token one at 1e-6. This
    /// test covers what those cannot: that the wiring reaches a real
    /// request at all.
    /// The derived seeds do two things, and both need pinning: the
    /// other choices must DIFFER from choice 0 (otherwise `n` returns
    /// the same answer `k` times and buys nothing), and the whole set
    /// must be REPRODUCIBLE (otherwise a seeded request is not seeded).
    ///
    /// This is the test that catches a change to `seed + i`.
    #[test]
    fn the_derived_seeds_make_the_other_choices_differ_reproducibly() {
        let decoder = small_decoder();
        let prompt = "abcabcabcabcabcabc";
        let mut params = greedy_params(8);
        params.sampling.temperature = 1.0;
        params.n = 4;

        let run = || {
            let mut per_choice = vec![String::new(); 4];
            generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                prompt,
                &params,
                None,
                None,
                None,
                None,
                |choice, s| per_choice[choice].push_str(s),
            )
            .expect("n = 4");
            per_choice
        };

        let first = run();
        let second = run();
        assert_eq!(
            first, second,
            "the same seeded request gave two different sets of choices"
        );
        assert!(
            first[1..].iter().any(|t| *t != first[0]),
            "every choice matched choice 0, so the derived seeds did nothing: {first:?}"
        );
    }

    /// **`n` > 1 prefills once and forks.** The acceptance number is
    /// not wall clock, it is `prompt_tokens`: four choices of one
    /// prompt must be billed for ONE prompt, because that is the only
    /// thing the field buys. If this reads four times the `n = 1`
    /// figure, the fork did not happen and the feature is a loop
    /// (`docs/plans/several-completions-per-request.md`).
    #[test]
    fn several_choices_prefill_the_prompt_once() {
        let decoder = small_decoder();
        let prompt = "abcabcabcabcabcabc";

        let mut one_text = String::new();
        let (one_finish, _rows, _ids, one_usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            prompt,
            &greedy_params(6),
            None,
            None,
            None,
            None,
            |_, s| one_text.push_str(s),
        )
        .expect("n = 1");
        assert_eq!(one_finish.len(), 1);

        let mut four = greedy_params(6);
        four.n = 4;
        let mut per_choice = vec![String::new(); 4];
        let (finishes, _rows, _ids, usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            prompt,
            &four,
            None,
            None,
            None,
            None,
            |choice, s| per_choice[choice].push_str(s),
        )
        .expect("n = 4");

        assert_eq!(finishes.len(), 4, "one finish reason per choice");
        // THE acceptance test: the prompt was prefilled once, so it is
        // billed once.
        assert_eq!(
            usage.prompt_tokens, one_usage.prompt_tokens,
            "n = 4 billed the prompt more than once, so the KV was not forked"
        );
        // And the completion side is the sum, not one choice's.
        assert_eq!(usage.completion_tokens, one_usage.completion_tokens * 4);
        for (i, text) in per_choice.iter().enumerate() {
            assert!(!text.is_empty(), "choice {i} produced nothing");
        }
    }

    /// Choice 0 is byte-identical to the single answer at the same
    /// seed. That is what makes the derived seeds reproducible rather
    /// than merely different: a caller can raise `n` without the answer
    /// they already had changing underneath them.
    ///
    /// **Sampled, not greedy, on purpose.** At temperature 0 the seed
    /// cannot change anything.
    ///
    /// This pins choice 0 ONLY, and deliberately: choice 0 runs on the
    /// request's own `params`, so no change to the derivation can move
    /// it -- confirmed by sabotage, `seed + i` shifted to `seed + i + 1`
    /// leaves this green. The derivation itself is pinned by
    /// [`the_derived_seeds_make_the_other_choices_differ_reproducibly`],
    /// which is the test that goes red for that mutation.
    #[test]
    fn choice_zero_is_the_answer_n_equals_one_would_have_given() {
        let decoder = small_decoder();
        let prompt = "abcabcabcabcabcabc";
        let sampled = |max_tokens: usize| {
            let mut p = greedy_params(max_tokens);
            p.sampling.temperature = 1.0;
            p
        };

        let mut one = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            prompt,
            &sampled(6),
            None,
            None,
            None,
            None,
            |_, s| one.push_str(s),
        )
        .expect("n = 1");

        let mut three = sampled(6);
        three.n = 3;
        let mut per_choice = vec![String::new(); 3];
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            prompt,
            &three,
            None,
            None,
            None,
            None,
            |choice, s| per_choice[choice].push_str(s),
        )
        .expect("n = 3");

        assert_eq!(
            per_choice[0], one,
            "choice 0 must be the answer n = 1 gives at the same seed"
        );
    }

    #[test]
    fn the_server_speculates_and_reports_it() {
        let decoder = small_decoder();
        let params = greedy_params(24);

        // Repetition is what a prompt-lookup drafter matches on, and
        // the byte tokenizer makes the text the token ids.
        let mut text = String::new();
        let (finish, _rows, _ids, usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            "abcabcabcabcabcabcabcabcabcabcabcabc",
            &params,
            None,
            None,
            None,
            None,
            |_, s| text.push_str(s),
        )
        .expect("speculative");

        assert_eq!(finish[0].0, FinishReason::Length);
        assert_eq!(usage.completion_tokens, 24);
        assert!(
            usage.draft_tokens.is_some_and(|d| d > 0),
            "a repetitive prompt drafted nothing, so this test proves nothing"
        );
        assert!(
            usage.accepted_draft_tokens.is_some_and(|a| a > 0),
            "nothing was accepted, so no forward was saved"
        );
        // Tokens per forward: 1.0 is "speculation bought nothing".
        let speedup = usage.acceptance_length.expect("acceptance length");
        assert!(
            speedup > 1.0,
            "speculation saved no forwards (tokens per forward {speedup})"
        );

        // A request with no room to draft reports NOTHING rather than
        // zeros: an absent field reads as "this request did not
        // speculate", where a zero reads as "the drafter was useless".
        //
        // One token is the case where the budget guard forbids
        // drafting outright -- a block always commits its accepted
        // drafts PLUS one, so a round needs at least two tokens of
        // room. That guard is also what keeps `max_tokens` honest, and
        // it is the bug this row's loop test caught.
        //
        // Note the prompt is repetitive here too: the drafter would
        // happily propose from it, and does not get the chance.
        let single = greedy_params(1);
        let mut one_text = String::new();
        let (_, _rows, _ids, one_usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            "abcabcabcabcabcabcabcabcabcabcabcabc",
            &single,
            None,
            None,
            None,
            None,
            |_, s| one_text.push_str(s),
        )
        .expect("single token");
        assert_eq!(one_usage.completion_tokens, 1);
        assert!(
            one_usage.draft_tokens.is_none(),
            "a request with no room to draft must not report speculation"
        );
    }

    /// A batch of `k` tokens leaves the cache exactly where feeding
    /// them one at a time would, and hands back one row per token.
    /// Both halves matter: the rows are what verification reads, and
    /// the cache state is what the next step continues from.
    #[test]
    fn step_batch_matches_stepping_one_token_at_a_time() {
        let decoder = small_decoder();
        let tokens = [1usize, 5, 9, 2];

        let mut batched = Kv::Contiguous(decoder.config.new_kv_caches());
        let rows = batched
            .step_batch(&decoder, &tokens, 0)
            .expect("a contiguous store speculates");
        assert_eq!(rows.len(), tokens.len(), "one row per token");

        let mut stepped = Kv::Contiguous(decoder.config.new_kv_caches());
        let one_at_a_time: Vec<Vec<f32>> = tokens
            .iter()
            .enumerate()
            .map(|(pos, &t)| stepped.step(&decoder, t, pos))
            .collect();

        for (pos, (b, o)) in rows.iter().zip(one_at_a_time.iter()).enumerate() {
            assert_eq!(b.len(), o.len(), "row {pos} width");
            for (i, (x, y)) in b.iter().zip(o.iter()).enumerate() {
                // The measured batched-vs-sequential ulp, the same
                // bound `frink-models` pins that difference at.
                assert!(
                    (x - y).abs() < 1e-6,
                    "row {pos} logit {i}: batched={x} sequential={y}"
                );
            }
        }
    }

    /// Rejecting a draft means the rows it wrote are gone, and the
    /// store continues as if they never happened. Checked by walking
    /// the same tokens again after the rollback and demanding the same
    /// logits: a stale row left behind would change them.
    #[test]
    fn truncate_to_undoes_the_rows_a_rejected_draft_wrote() {
        let decoder = small_decoder();
        let committed = [1usize, 5];
        let draft = [9usize, 2];

        let mut kv = Kv::Contiguous(decoder.config.new_kv_caches());
        kv.step_batch(&decoder, &committed, 0).expect("committed");
        let after_commit = kv
            .step_batch(&decoder, &draft, committed.len())
            .expect("draft");

        // Reject the whole draft.
        assert!(
            kv.truncate_to(committed.len()),
            "a contiguous store rolls back"
        );

        // Feeding the same draft again must land on the same rows.
        let again = kv
            .step_batch(&decoder, &draft, committed.len())
            .expect("draft again");
        for (pos, (a, b)) in after_commit.iter().zip(again.iter()).enumerate() {
            for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() {
                assert!(
                    (x - y).abs() < 1e-6,
                    "row {pos} logit {i} after rollback: {x} vs {y}"
                );
            }
        }
    }

    /// The gate is asked BEFORE anything is written. A store that
    /// cannot roll back must not be speculated into at all, which is
    /// why `step_batch` returns `None` rather than `truncate_to`
    /// failing after the damage is done.
    #[test]
    fn a_paged_store_refuses_to_speculate_rather_than_failing_to_undo() {
        // Constructed without a pool: the point is the arm, not the
        // pages, and `step_batch` decides on the variant alone.
        let decoder = small_decoder();
        let mut kv = Kv::Contiguous(decoder.config.new_kv_caches());
        assert!(
            kv.step_batch(&decoder, &[1], 0).is_some(),
            "the contiguous arm serves speculation"
        );
        assert!(kv.truncate_to(0), "and can undo it");
    }

    fn greedy_params(max_tokens: usize) -> GenerationParams {
        GenerationParams {
            cache_salt: None,
            prompt_logprobs: None,
            wants_logprobs: false,
            reasoning: None,
            max_tokens,
            sampling: SamplingParams::default(),
            seed: 1,
            n: 1,
            interleave_choices: false,
            stop: Vec::new(),
            stop_token_ids: Vec::new(),
            json_object: false,
            grammar: None,
            cancel: None,
            ignore_eos: false,
            reasoning_budget: crate::reasoning_budget::ReasoningBudget::Unrestricted,
            lora: None,
        }
    }

    /// Regression test for a real bug caught by close reading, not by
    /// any earlier test (the earlier tests only checked `generate`
    /// against a test helper that replicated the same buggy pattern,
    /// so they never could have caught it): `generate`'s original
    /// prompt-processing loop pushed every prompt token into the KV
    /// cache once (correct), then its first generation-loop iteration
    /// re-processed the *last* prompt token a second time via another
    /// `forward_token` call at the wrong position (`tokens.len()`
    /// instead of its real position `tokens.len() - 1`) just to obtain
    /// logits -- silently duplicating that token in the cache with a
    /// different RoPE rotation applied, corrupting every subsequent
    /// position's attention. Fixed by capturing the prompt loop's own
    /// last-iteration logits instead of discarding and re-deriving
    /// them. This locks in the fixed pattern (which `generate` uses
    /// internally) against `forward_batch`'s independent ground truth.
    #[test]
    fn prompt_processing_matches_forward_batch_ground_truth_with_no_duplicate_position() {
        let decoder = small_decoder();
        let tokens = vec![1usize, 2, 3, 4];

        let mut fresh_caches: Vec<KvCache> = decoder.config.new_kv_caches();
        let batch_logits = decoder.forward_batch(&tokens, 0, &mut fresh_caches);
        let ground_truth_next_logits = batch_logits.last().unwrap().clone();

        // The exact pattern `generate` now uses: one forward_token call
        // per prompt token, keeping the last call's logits.
        let mut caches: Vec<KvCache> = decoder.config.new_kv_caches();
        let mut logits = Vec::new();
        for (pos, &tok) in tokens.iter().enumerate() {
            logits = decoder.forward_token(tok, pos, &mut caches);
        }

        assert_eq!(
            caches[0].positions(),
            fresh_caches[0].positions(),
            "must not push any position beyond the real prompt length"
        );
        // Tolerance, not bit equality. `forward_token` goes through
        // `WeightMatrix::apply` (one activation) and `forward_batch`
        // through `apply_batch`, and since the CPU batch GEMM kernels
        // landed those are different kernels with different f32
        // accumulation orders -- on aarch64 the batched path uses the
        // i8mm interleave-8 GEMM. So they agree to last-ulp, not
        // bit-for-bit. llama.cpp has the same batch-vs-sequential
        // property. The engine-side twin of this test was relaxed for
        // the same reason; this one only shows up on a host where the
        // aarch64 kernels actually run.
        assert_eq!(logits.len(), ground_truth_next_logits.len());
        for (i, (a, b)) in logits.iter().zip(&ground_truth_next_logits).enumerate() {
            assert!(
                (a - b).abs() <= 1e-5 * a.abs().max(1.0),
                "logit {i} predicting the first generated token: sequential {a} vs forward_batch {b}"
            );
        }
    }

    /// End-to-end version of the same property: `generate`'s full
    /// greedy decode loop (prompt processing + iterative generation)
    /// must produce exactly the token sequence an independent
    /// step-by-step computation (via `forward_batch` for the prompt,
    /// then `forward_token` once per new position, argmax at each
    /// step) would produce. Restricted to ASCII byte values so
    /// `ServerTokenizer::Byte`'s `decode` is lossless in both
    /// directions and the generated text can be compared back to
    /// token ids exactly.
    #[test]
    fn generate_greedy_output_matches_independent_step_by_step_computation() {
        let decoder = small_decoder();
        let prompt_ids = vec![1usize, 2, 3];
        let prompt = String::from_utf8(prompt_ids.iter().map(|&b| b as u8).collect()).unwrap();
        let max_tokens = 8;

        // Independent computation: forward_batch over the prompt, then
        // one forward_token + argmax per new position, decoding each
        // generated id one at a time and concatenating -- exactly
        // `generate`'s own decode granularity (`ServerTokenizer::Byte`
        // is lossy per non-ASCII byte, so decoding token-by-token vs.
        // decoding the whole sequence at once are not equivalent; this
        // must replicate the real call pattern, not just the ids).
        let mut caches: Vec<KvCache> = decoder.config.new_kv_caches();
        let mut logits = decoder
            .forward_batch(&prompt_ids, 0, &mut caches)
            .pop()
            .unwrap();
        let mut expected_text = String::new();
        for pos in (prompt_ids.len()..).take(max_tokens) {
            let next = logits
                .iter()
                .enumerate()
                .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
                .map(|(i, _)| i)
                .unwrap();
            expected_text.push_str(&ServerTokenizer::Byte.decode(&[next]));
            logits = decoder.forward_token(next, pos, &mut caches);
        }

        let mut actual_text = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(max_tokens),
            None,
            None,
            None,
            None,
            |_, s| actual_text.push_str(s),
        )
        .unwrap();

        assert_eq!(actual_text, expected_text);
    }

    #[test]
    fn rejects_out_of_vocab_prompt_tokens() {
        // ByteTokenizer emits raw bytes; vocab 32 makes ASCII letters OOV.
        let decoder = Decoder::new_random_small(test_dense_fixture(), 2, 32);
        let result = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            "hello",
            &greedy_params(4),
            None,
            None,
            None,
            None,
            |_, _| {},
        );
        assert!(matches!(result, Err(DecodeError::TokenOutOfVocab { .. })));
    }

    #[test]
    fn greedy_generation_hits_length_without_eos() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let mut chunks = String::new();
        let (finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            None,
            None,
            |_, s| chunks.push_str(s),
        )
        .unwrap();
        assert_eq!(finish[0].0, FinishReason::Length);
    }

    /// A cancel raised while the loop is running must actually stop it,
    /// keep whatever was already decoded, and say `Cancelled` -- not
    /// `Stop`, which a client would render as a finished answer.
    #[test]
    fn a_cancelled_generation_stops_early_and_keeps_its_tokens() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let cancel = crate::cancel::CancelToken::new();

        let mut params = greedy_params(200);
        params.cancel = Some(cancel.clone());

        let mut chunks = String::new();
        let mut emitted = 0usize;
        let (finish, _rows, _ids, usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &params,
            None,
            None,
            None,
            None,
            |_, s| {
                chunks.push_str(s);
                emitted += 1;
                // Stands in for the socket dropping (or `/v1/cancel`
                // arriving) a few tokens into the answer.
                if emitted == 3 {
                    cancel.cancel();
                }
            },
        )
        .unwrap();

        assert_eq!(finish[0].0, FinishReason::Cancelled);
        assert!(
            usage.completion_tokens < 200,
            "cancelling did not shorten the decode: {} tokens",
            usage.completion_tokens
        );
        assert!(
            !chunks.is_empty(),
            "the tokens decoded before the cancel must survive it"
        );
    }

    /// A generation nobody cancelled must be untouched by the machinery
    /// -- the flag is polled every token, so a bug here would shorten
    /// every answer on the server.
    #[test]
    fn an_uncancelled_generation_runs_to_its_normal_end() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let mut params = greedy_params(5);
        params.cancel = Some(crate::cancel::CancelToken::new());

        let (finish, _rows, _ids, usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &params,
            None,
            None,
            None,
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(finish[0].0, FinishReason::Length);
        assert_eq!(usage.completion_tokens, 5);
    }

    /// Discovers the real greedy-argmax next-token id after `prompt_ids`
    /// via `forward_batch` (ground truth: its last returned row predicts
    /// the token immediately after the full prompt, exactly what
    /// `generate` computes as its first generation-loop `logits` value)
    /// -- not by decoding it to text and reading bytes back, which is
    /// lossy for `ByteTokenizer`: a standalone byte >= 128 is not valid
    /// UTF-8 on its own, so `String::from_utf8_lossy` replaces it with
    /// the 3-byte U+FFFD replacement character, and reading
    /// `s.bytes().next()` off that recovers 0xEF (239), not the
    /// original token id.
    fn greedy_next_token_after(decoder: &Decoder, prompt_ids: &[usize]) -> usize {
        let mut caches: Vec<KvCache> = decoder.config.new_kv_caches();
        let logits = decoder
            .forward_batch(prompt_ids, 0, &mut caches)
            .pop()
            .unwrap();
        logits
            .iter()
            .enumerate()
            .max_by(|a, b| a.1.partial_cmp(b.1).unwrap())
            .map(|(i, _)| i)
            .unwrap()
    }

    #[test]
    fn eos_token_stops_generation_before_max_tokens() {
        let decoder = small_decoder();
        let prompt_ids = vec![1usize, 2];
        let prompt = String::from_utf8(prompt_ids.iter().map(|&b| b as u8).collect()).unwrap();

        // ByteTokenizer::encode is a lossless direct byte->id mapping
        // (only decode is lossy, see greedy_next_token_after's doc
        // comment), so `generate`'s internal prompt replay reaches
        // exactly the same state as this direct computation.
        let eos = greedy_next_token_after(&decoder, &prompt_ids);

        let (finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::from_eos(Some(eos)),
            None,
            &prompt,
            &greedy_params(50),
            None,
            None,
            None,
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(
            finish[0].0,
            FinishReason::Stop,
            "generation must stop as soon as the greedy-chosen token matches eos_id, not run to max_tokens"
        );
    }

    /// The bug this replaced: every server decode loop carried a single
    /// `eos_id`, so a Llama-3 checkpoint (whose `eos_token_id` is
    /// `<|end_of_text|>` while turns end with `<|eot_id|>`) or a gemma-2
    /// one (`<end_of_turn>`) ran past the end of its own turn to
    /// `max_tokens` over HTTP even after `eog_token_ids` landed for the
    /// CLI. Here the metadata EOS is deliberately a token the model will
    /// never pick, and the *turn ender* is the greedy next token: only a
    /// loop that consults the whole set stops.
    #[test]
    fn a_turn_ender_that_is_not_the_metadata_eos_still_stops_generation() {
        let decoder = small_decoder();
        let prompt_ids = vec![1usize, 2];
        let prompt = String::from_utf8(prompt_ids.iter().map(|&b| b as u8).collect()).unwrap();
        let turn_ender = greedy_next_token_after(&decoder, &prompt_ids);
        let never_sampled = (turn_ender + 1) % decoder.config.vocab_size;

        let stop = StopTokens::from_eos(Some(never_sampled)).with_id(Some(turn_ender));
        let (finish, _rows, _ids, usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &stop,
            None,
            &prompt,
            &greedy_params(50),
            None,
            None,
            None,
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(finish[0].0, FinishReason::Stop);
        assert_eq!(
            usage.completion_tokens, 0,
            "the very first sampled token was the turn ender"
        );
    }

    #[test]
    fn a_stop_sequence_that_never_matches_does_not_drop_any_generated_content() {
        // The hold-back buffering (needed so a stop sequence spanning
        // more than one token is never partially flushed) must not
        // silently swallow output when no stop sequence ever matches:
        // the final emitted text must be byte-for-byte identical to an
        // otherwise-identical run with no stop sequences configured at
        // all, since the buffering is purely about *when* text is
        // flushed, never *whether* it is.
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8]).unwrap();

        let mut baseline = String::new();
        let (baseline_finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(20),
            None,
            None,
            None,
            None,
            |_, s| baseline.push_str(s),
        )
        .unwrap();

        let mut with_unmatchable_stop = String::new();
        let (stop_finish, _rows, _ids, _usage2) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &GenerationParams {
                cache_salt: None,
                prompt_logprobs: None,
                wants_logprobs: false,
                reasoning: None,
                max_tokens: 20,
                sampling: SamplingParams::default(),
                seed: 1,
                n: 1,
                interleave_choices: false,
                stop: vec!["ZZ_NEVER_MATCHES_ZZ".to_string()],
                stop_token_ids: Vec::new(),
                json_object: false,
                grammar: None,
                cancel: None,
                ignore_eos: false,
                reasoning_budget: crate::reasoning_budget::ReasoningBudget::Unrestricted,
                lora: None,
            },
            None,
            None,
            None,
            None,
            |_, s| with_unmatchable_stop.push_str(s),
        )
        .unwrap();

        assert_eq!(baseline_finish[0].0, FinishReason::Length);
        assert_eq!(stop_finish[0].0, FinishReason::Length);
        assert_eq!(with_unmatchable_stop, baseline);
    }

    /// A decode loop whose next token is scripted, so the two stop
    /// layers can be exercised on exactly the token sequence they are
    /// meant to react to rather than on whatever a random decoder
    /// happens to emit.
    ///
    /// `script` is the token id produced at each step; `render` is how
    /// each id detokenizes.
    fn run_scripted(
        script: &[usize],
        render: impl Fn(usize) -> String,
        params: &GenerationParams,
    ) -> (FinishReason, Vec<usize>, Vec<String>) {
        run_scripted_with_stops(script, render, params, StopTokens::from_eos(None))
    }

    fn run_scripted_with_stops(
        script: &[usize],
        render: impl Fn(usize) -> String,
        params: &GenerationParams,
        stop_tokens: StopTokens,
    ) -> (FinishReason, Vec<usize>, Vec<String>) {
        try_run_scripted_with_stops(script, render, params, stop_tokens)
            .expect("an unconstrained script cannot fail to decode")
    }

    /// As [`run_scripted_with_stops`], for the tests that are ABOUT a
    /// generation that stops with an error -- a grammar with no legal
    /// continuation. Everything else goes through the unwrapping
    /// version, so a decode that starts failing is a test failure
    /// rather than a quietly different return value.
    fn try_run_scripted_with_stops(
        script: &[usize],
        render: impl Fn(usize) -> String,
        params: &GenerationParams,
        stop_tokens: StopTokens,
    ) -> Result<(FinishReason, Vec<usize>, Vec<String>), DecodeError> {
        let vocab = script.iter().copied().max().unwrap_or(0) + 2;
        let logits_for = |id: usize| {
            let mut v = vec![0.0f32; vocab];
            v[id] = 10.0;
            v
        };
        let mut next = 0usize;
        let mut take = || {
            let id = script
                .get(next)
                .copied()
                .unwrap_or(script[script.len() - 1]);
            next += 1;
            id
        };
        let first = logits_for(take());
        let mut chunks: Vec<String> = Vec::new();
        let (finish, ids, _, _probs) = crate::sampling_loop::sample_until_stop(
            first,
            0,
            // A scripted-logits harness with no real prompt: the empty
            // half is the truth here, not an omission.
            &[],
            &stop_tokens,
            params,
            |ids| {
                ids.iter()
                    .copied()
                    .map(&render)
                    .collect::<String>()
                    .into_bytes()
            },
            &mut |_tok: usize, _pos: usize| logits_for(take()),
            |chunk| chunks.push(chunk.to_string()),
            &render,
            0,
        )?;
        Ok((finish, ids, chunks))
    }

    /// The wiring #124 was about, end to end through the decode loop.
    ///
    /// The unit tests in `crate::utf8_stream` prove the buffer works.
    /// They cannot prove `sample_until_stop` USES it, and that call is
    /// the whole fix -- the same gap that let a batched row ship with
    /// no timings. So this drives the real loop with a tokenizer whose
    /// tokens are single bytes, which is exactly what a byte-fallback
    /// vocabulary does to an emoji.
    ///
    /// It cannot go through `run_scripted`: that helper's `render`
    /// returns `String`, and half a character has no `String`.
    #[test]
    fn a_character_split_across_tokens_is_emitted_whole_by_the_decode_loop() {
        let smiley = "😊".as_bytes().to_vec();
        assert_eq!(smiley.len(), 4, "the point of this test");
        let vocab = smiley.len() + 2;
        let logits_for = |id: usize| {
            let mut v = vec![0.0f32; vocab];
            v[id] = 10.0;
            v
        };
        let len = smiley.len();
        let mut next = 0usize;
        let mut take = move || {
            let id = next.min(len - 1);
            next += 1;
            id
        };
        let first = logits_for(take());
        let bytes = smiley;
        let mut chunks: Vec<String> = Vec::new();
        let (_finish, ids, _, _probs) = crate::sampling_loop::sample_until_stop(
            first,
            0,
            &[],
            &StopTokens::default(),
            &scripted_params(4),
            // One byte per token: each of the middle ones is invalid
            // UTF-8 on its own, which is the whole problem.
            |ids| ids.iter().map(|&id| bytes[id]).collect(),
            &mut |_tok: usize, _pos: usize| logits_for(take()),
            |chunk| chunks.push(chunk.to_string()),
            &|_id| String::new(),
            0,
        )
        .expect("decode");

        assert_eq!(ids.len(), 4, "four tokens, one per byte");
        assert_eq!(
            chunks.concat(),
            "😊",
            "a character split across tokens must not be decoded per token"
        );
        assert!(
            !chunks.concat().contains(char::REPLACEMENT_CHARACTER),
            "the bytes were valid UTF-8 together; only the split made them look invalid"
        );
    }

    fn scripted_params(max_tokens: usize) -> GenerationParams {
        GenerationParams {
            cache_salt: None,
            prompt_logprobs: None,
            wants_logprobs: false,
            reasoning: None,
            max_tokens,
            sampling: SamplingParams {
                temperature: 0.0,
                ..SamplingParams::default()
            },
            seed: 1,
            n: 1,
            interleave_choices: false,
            stop: Vec::new(),
            stop_token_ids: Vec::new(),
            json_object: false,
            grammar: None,
            cancel: None,
            ignore_eos: false,
            reasoning_budget: crate::reasoning_budget::ReasoningBudget::Unrestricted,
            lora: None,
        }
    }

    /// A grammar reaches the PRIVATE decode loop -- the one `generate`
    /// runs -- and both of its halves do.
    ///
    /// The script wants token 2 ("c") at every step, and `root ::= "ab"`
    /// makes that illegal at every step. The mask alone would give
    /// `[0, 0]`, because without the accept the grammar keeps answering
    /// "what may the FIRST token be?"; both halves give `[0, 1]`, which
    /// is the only string this grammar admits.
    ///
    /// The vacuity check is the unconstrained run below it: the same
    /// script with no grammar must produce the token the grammar had to
    /// take away, or this proves nothing.
    #[test]
    fn a_grammar_constrains_the_private_decode_loop() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        let script = [2usize, 2, 2, 2];

        let unconstrained = run_scripted(&script, render, &scripted_params(2));
        assert_eq!(
            unconstrained.1,
            vec![2, 2],
            "the model wants \"cc\", so a grammar forbidding it has work to do"
        );

        let mut params = scripted_params(2);
        params.grammar = Some(std::sync::Arc::new(
            frink_models::grammar::Grammar::from_str_with_root(r#"root ::= "ab""#, "root")
                .expect("test grammar parses"),
        ));
        let (finish, ids, chunks) = run_scripted(&script, render, &params);
        assert_eq!(
            ids,
            vec![0, 1],
            "the grammar was not applied token by token"
        );
        assert_eq!(chunks.concat(), "ab");
        assert_eq!(finish, FinishReason::Length);
    }

    /// The same loop, where the grammar finishes before `max_tokens`
    /// does and this vocabulary has no end-of-generation token to end
    /// on: a COMPLETE answer, reported as `Stop` rather than as an
    /// error or as 8 tokens of whatever came next.
    #[test]
    fn a_completed_grammar_ends_the_private_decode_loop() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        let mut params = scripted_params(8);
        params.grammar = Some(std::sync::Arc::new(
            frink_models::grammar::Grammar::from_str_with_root(r#"root ::= "ab""#, "root")
                .expect("test grammar parses"),
        ));
        let (finish, ids, chunks) = run_scripted(&[2usize; 8], render, &params);
        assert_eq!(ids, vec![0, 1]);
        assert_eq!(chunks.concat(), "ab");
        assert_eq!(finish, FinishReason::Stop);
    }

    /// And a grammar this vocabulary cannot spell STOPS, with an error
    /// naming the constraint -- rather than serving text that does not
    /// satisfy the grammar the caller was told was applied.
    #[test]
    fn a_grammar_the_vocabulary_cannot_spell_fails_the_generation() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        let mut params = scripted_params(4);
        params.grammar = Some(std::sync::Arc::new(
            frink_models::grammar::Grammar::from_str_with_root(r#"root ::= "z""#, "root")
                .expect("test grammar parses"),
        ));
        let err =
            try_run_scripted_with_stops(&[2usize; 4], render, &params, StopTokens::from_eos(None))
                .expect_err("no token in this vocabulary renders as \"z\"");
        assert!(
            matches!(err, DecodeError::GrammarConstraint { .. }),
            "{err}"
        );
    }

    /// Layer 1: a stop *token* ends the answer, and the token itself
    /// never appears in it -- the same treatment EOS already gets,
    /// which is the point. A control token the client named is no more
    /// part of the output than the end-of-sequence token is.
    ///
    /// Confirmed to FAIL (runs to all 6 tokens, emitting "aabaab") when
    /// the `is_stop_token` check is removed from `sample_until_stop`.
    #[test]
    fn a_token_level_stop_ends_generation_and_never_reaches_the_output() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        let script = [0usize, 0, 1, 0, 0, 1];

        let (finish, ids, chunks) = run_scripted(&script, render, &scripted_params(6));
        assert_eq!(finish, FinishReason::Length);
        assert_eq!(ids, script.to_vec());
        assert_eq!(chunks.concat(), "aabaab");

        let (finish, ids, chunks) = run_scripted(
            &script,
            render,
            &GenerationParams {
                stop_token_ids: vec![1],
                ..scripted_params(6)
            },
        );
        assert_eq!(finish, FinishReason::Stop);
        assert_eq!(ids, vec![0, 0], "the stop token is not part of the answer");
        assert_eq!(
            chunks.concat(),
            "aa",
            "the stop token must not be rendered into the output"
        );
    }

    /// The case layer 2 provably cannot cover: a control token that
    /// renders as the empty string. The text scan has nothing to match
    /// on, so only matching the id ends the answer.
    #[test]
    fn a_stop_token_that_renders_as_nothing_is_still_a_stop() {
        // Token 1 detokenizes to "", as real special tokens can.
        let render = |id: usize| {
            if id == 1 {
                String::new()
            } else {
                char::from(b'a' + id as u8).to_string()
            }
        };
        let script = [0usize, 1, 0, 0];

        // The text layer alone: the stop string never appears, so
        // generation runs to its limit.
        let (finish, _, chunks) = run_scripted(
            &script,
            render,
            &GenerationParams {
                stop: vec!["<|end|>".to_string()],
                ..scripted_params(4)
            },
        );
        assert_eq!(finish, FinishReason::Length);
        assert_eq!(chunks.concat(), "aaa");

        // With the id resolved, it stops where it should.
        let (finish, ids, chunks) = run_scripted(
            &script,
            render,
            &GenerationParams {
                stop: vec!["<|end|>".to_string()],
                stop_token_ids: vec![1],
                ..scripted_params(4)
            },
        );
        assert_eq!(finish, FinishReason::Stop);
        assert_eq!(ids, vec![0]);
        assert_eq!(chunks.concat(), "a");
    }

    /// Layer 2: nothing that turns out to be part of the stop string
    /// is ever emitted. `emit` is append-only -- SSE has no way to take
    /// a chunk back -- so over-emitting is not a display glitch, it is
    /// the stop sequence failing to do the one thing it promises.
    ///
    /// The script spells "ab" (a partial match that is disproved) and
    /// then "abc" (the real one), so the buffer has to hold, release,
    /// and hold again.
    #[test]
    fn nothing_that_becomes_part_of_the_stop_is_ever_emitted() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        let script = [0usize, 1, 0, 1, 2, 0];
        let params = GenerationParams {
            stop: vec!["abc".to_string()],
            ..scripted_params(6)
        };

        let (finish, _, chunks) = run_scripted(&script, render, &params);
        assert_eq!(
            finish,
            FinishReason::StopSequence("abc".to_string()),
            "the reason names the stop that fired, not merely that one did"
        );
        assert_eq!(
            chunks.concat(),
            "ab",
            "the answer is everything before the stop, and nothing after it"
        );

        // Append-only means every intermediate state must already be a
        // prefix of that: emitting one character too many can never be
        // undone.
        let mut seen = String::new();
        for chunk in &chunks {
            seen.push_str(chunk);
            assert!(
                "ab".starts_with(&seen),
                "the stream ran ahead of the answer: {seen:?} (chunks: {chunks:?})"
            );
        }
    }

    /// A partial match that is disproved is released with the very
    /// token that disproves it, not carried to the end of the answer.
    ///
    /// The conservative alternative -- always withhold
    /// `longest_stop - 1` bytes -- is equally safe and permanently
    /// leaves the stream that many bytes behind the model, for a match
    /// that in most chunks is not even beginning.
    ///
    /// Confirmed to FAIL (first chunk is "a", not "abd") when
    /// `partial_suffix_len` is replaced by that fixed hold-back.
    #[test]
    fn a_disproved_partial_is_released_by_the_token_that_disproves_it() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        // "a", "b" are held as a possible "abc"; "d" settles it.
        let script = [0usize, 1, 3, 0];
        let (_, _, chunks) = run_scripted(
            &script,
            render,
            &GenerationParams {
                stop: vec!["abc".to_string()],
                ..scripted_params(4)
            },
        );
        assert_eq!(chunks.concat(), "abda", "no output is lost");
        assert_eq!(
            chunks.first().map(String::as_str),
            Some("abd"),
            "the whole disproved partial goes out at once: {chunks:?}"
        );
    }

    #[test]
    fn a_stop_sequence_that_does_match_truncates_output_before_it() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8]).unwrap();

        // Discover what greedy decode actually produces, then use a
        // substring of it (starting after the first character, so at
        // least one character of real output precedes the match) as a
        // stop sequence guaranteed to match.
        let mut baseline = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(20),
            None,
            None,
            None,
            None,
            |_, s| baseline.push_str(s),
        )
        .unwrap();
        let Some((cut, _)) = baseline.char_indices().nth(1) else {
            // Degenerate case for this decoder/seed: fewer than 2 chars
            // generated: nothing meaningful to truncate, skip.
            return;
        };
        // This decoder emits arbitrary bytes through `ServerTokenizer::
        // Byte`, so the tail can end in a U+FFFD that `Utf8Stream::flush`
        // produced -- a character whose remaining bytes never arrived
        // because generation hit `max_tokens` mid-sequence.
        //
        // That one is NOT matchable, and correctly so: while the loop is
        // running, those bytes may still be completed by the next token,
        // so the replacement does not exist yet. It is only knowable
        // once generation has ended, which is after every stop decision
        // has been made. Trimming it keeps this test about what it says
        // it is about -- a stop sequence that DOES match.
        let stop_str = baseline[cut..]
            .trim_end_matches(char::REPLACEMENT_CHARACTER)
            .to_string();
        if stop_str.is_empty() {
            return;
        }

        let mut truncated = String::new();
        let (finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &GenerationParams {
                cache_salt: None,
                prompt_logprobs: None,
                wants_logprobs: false,
                reasoning: None,
                max_tokens: 20,
                sampling: SamplingParams::default(),
                seed: 1,
                n: 1,
                interleave_choices: false,
                stop: vec![stop_str.clone()],
                stop_token_ids: Vec::new(),
                json_object: false,
                grammar: None,
                cancel: None,
                ignore_eos: false,
                reasoning_budget: crate::reasoning_budget::ReasoningBudget::Unrestricted,
                lora: None,
            },
            None,
            None,
            None,
            None,
            |_, s| truncated.push_str(s),
        )
        .unwrap();

        assert_eq!(finish[0].0, FinishReason::StopSequence(stop_str));
        assert_eq!(truncated, baseline[..cut]);
    }

    #[test]
    fn usage_reports_both_phases_and_a_time_to_first_token() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let (_finish, _rows, _ids, usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            None,
            None,
            |_, _| {},
        )
        .unwrap();

        assert_eq!(usage.prompt_tokens, 3);
        assert_eq!(usage.completion_tokens, 5);
        let prefill = usage.prompt_eval_duration_ms.expect("prefill timed");
        let decode = usage.generation_duration_ms.expect("decode timed");
        let ttft = usage.time_to_first_token_ms.expect("first token timed");
        // TTFT is measured from the start of prefill, so it can never be
        // shorter than prefill, and the first of five tokens must land
        // before the decode loop finishes all five.
        assert!(ttft >= prefill, "ttft {ttft} < prefill {prefill}");
        assert!(
            ttft <= prefill + decode + 1.0,
            "ttft {ttft} exceeds the whole request"
        );
    }

    #[test]
    fn cached_tokens_distinguishes_a_miss_from_an_absent_prefix_cache() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();

        let (_f, _rows, _ids, no_cache) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(2),
            None,
            None,
            None,
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(no_cache.cached_tokens, None, "no prefix cache configured");

        let pc = Mutex::new(PrefixCache::new(4));
        let (_f, _rows, _ids, miss) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(2),
            None,
            None,
            Some(&pc),
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(miss.cached_tokens, Some(0), "cache consulted, missed");

        // Second turn extends the first: three prompt tokens are reused.
        let longer = String::from_utf8(vec![1u8, 2, 3, 9]).unwrap();
        let (_f, _rows, _ids, hit) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &longer,
            &greedy_params(2),
            None,
            None,
            Some(&pc),
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(hit.cached_tokens, Some(3));
    }

    #[test]
    fn prefix_cache_reuses_a_shared_prefix_and_produces_the_same_output_as_a_fresh_run() {
        let decoder = small_decoder();
        let prompt1 = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let pc = Mutex::new(PrefixCache::new(4));

        let mut out1 = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt1,
            &greedy_params(5),
            None,
            None,
            Some(&pc),
            None,
            |_, s| out1.push_str(s),
        )
        .unwrap();
        assert_eq!(pc.lock().unwrap().stats().misses, 1);

        // prompt2's tokens (raw bytes, via ByteTokenizer) start with
        // prompt1's exact bytes -- the common multi-turn-chat shape.
        let prompt2 = String::from_utf8(vec![1u8, 2, 3, 9, 9]).unwrap();

        let mut out2_with_cache = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt2,
            &greedy_params(5),
            None,
            None,
            Some(&pc),
            None,
            |_, s| out2_with_cache.push_str(s),
        )
        .unwrap();
        let stats = pc.lock().unwrap().stats();
        assert_eq!(stats.hits, 1, "prompt2 must hit the stored prompt1 entry");
        assert_eq!(stats.total_positions_reused, 3);

        let mut out2_fresh = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt2,
            &greedy_params(5),
            None,
            None,
            None,
            None,
            |_, s| out2_fresh.push_str(s),
        )
        .unwrap();

        assert_eq!(
            out2_with_cache, out2_fresh,
            "restoring from the prefix cache must produce identical output to processing the whole prompt from scratch"
        );
    }

    #[test]
    fn prefix_cache_exact_repeat_skips_prompt_processing_via_pending_logits() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let pc = Mutex::new(PrefixCache::new(4));

        let mut out1 = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            Some(&pc),
            None,
            |_, s| out1.push_str(s),
        )
        .unwrap();

        // The exact same prompt again: the stored entry's `tokens` (the
        // full prompt+completion from the first call) starts with this
        // exact prompt, so this only matches the prompt-length prefix
        // of a longer stored entry, not an exact full-entry match --
        // covering the "no pending_logits available" fallback path,
        // not the zero-forward-pass shortcut. Still must produce
        // identical output to a from-scratch run.
        let mut out2_with_cache = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            Some(&pc),
            None,
            |_, s| out2_with_cache.push_str(s),
        )
        .unwrap();

        let mut out2_fresh = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            None,
            None,
            |_, s| out2_fresh.push_str(s),
        )
        .unwrap();

        assert_eq!(out2_with_cache, out2_fresh);
    }

    #[test]
    fn prefix_cache_is_not_consulted_when_a_kv_pool_is_configured() {
        let decoder = small_decoder(); // 2 layers
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let pc = Mutex::new(PrefixCache::new(4));
        let pool = Arc::new(Mutex::new(KvBlockPool::new(64, 2)));
        let config = pool_config(pool, Duration::ZERO);

        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            Some(&config),
            None,
            Some(&pc),
            None,
            |_, _| {},
        )
        .unwrap();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            Some(&config),
            None,
            Some(&pc),
            None,
            |_, _| {},
        )
        .unwrap();

        let stats = pc.lock().unwrap().stats();
        assert_eq!(
            stats.hits + stats.misses,
            0,
            "prefix cache must never be consulted while a KV pool is configured"
        );
    }

    fn pool_config(pool: Arc<Mutex<KvBlockPool>>, queue_wait: Duration) -> KvPoolConfig {
        KvPoolConfig { pool, queue_wait }
    }

    /// Published prefixes must come BACK, or a long-running server
    /// refuses requests that fit.
    ///
    /// `publish_to_radix` retains a page group for every page it hands
    /// the tree, and nothing released them: `RadixCache::evict` had no
    /// caller anywhere. So the pool shrank monotonically, and a server
    /// that had been up long enough started answering
    /// `PagedStoreExhausted` while the tree sat on pages no request was
    /// reading.
    ///
    /// This asserts the POOL recovers, not that `evict` was called. A
    /// test that only checks the call happened proves nothing about a
    /// leak, which is how this one survived having 33 tests around it.
    #[test]
    fn published_prefixes_are_reclaimed_under_pressure() {
        let decoder = small_decoder();
        let block_size = 4;
        // Small on purpose: enough for a few prompts at once, so the
        // pool must be recycled rather than merely large.
        let config = paged_config_with_radix(&decoder, block_size, 24, true);
        let free_before = config.store.free_groups();
        assert!(free_before > 0, "the fixture must start with free pages");

        // Each prompt is distinct, so every one publishes a NEW prefix
        // and none of them can be adopted from an earlier one. Without
        // eviction the tree accumulates all of them and the pool runs
        // dry.
        for round in 0..40u32 {
            let tokens: Vec<usize> = (0..12).map(|i| (round * 100 + i) as usize).collect();
            let mut lease =
                acquire_paged_caches(&decoder, &config, &tokens, tokens.len() + 8, None)
                    .expect("admission must keep succeeding once the tree can be evicted");
            publish_to_radix(&mut lease, &tokens, block_size);
            drop(lease);
        }

        // The pool is whole again: every page either sits in the tree
        // as evictable or is free, and nothing has leaked.
        let tree_holds = {
            let tree = config
                .radix
                .as_ref()
                .expect("configured with a tree")
                .lock()
                .unwrap_or_else(|p| p.into_inner());
            tree.total_size().div_ceil(block_size)
        };
        assert_eq!(
            config.store.free_groups() + tree_holds,
            free_before,
            "every page must be free or accounted for in the tree: \
             free={} tree={} started={free_before}",
            config.store.free_groups(),
            tree_holds
        );
    }

    fn paged_config(decoder: &Decoder, block_size: usize, blocks: usize) -> PagedKvConfig {
        paged_config_with_radix(decoder, block_size, blocks, false)
    }

    fn paged_config_with_radix(
        decoder: &Decoder,
        block_size: usize,
        blocks: usize,
        share_prefixes: bool,
    ) -> PagedKvConfig {
        PagedKvConfig {
            store: Arc::new(decoder.config.new_paged_kv(block_size, blocks)),
            queue_wait: Duration::ZERO,
            radix: share_prefixes.then(|| {
                Arc::new(Mutex::new(crate::policy::radix::SaltedRadix::new(
                    block_size,
                )))
            }),
            anchor_token: None,
            slide_interval: crate::policy::pool_budget::DEFAULT_SWA_EVICTION_INTERVAL,
        }
    }

    /// Serving on paged KV must produce the SAME TEXT as serving on
    /// contiguous KV.
    ///
    /// This is the property the whole paged path exists to preserve,
    /// and the one no lower-level test can state: the decoder tests pin
    /// bit-identity of logits, but a caller only ever sees tokens, and
    /// between the two sit admission, prefill, the decode loop and
    /// sampling. If any of those dispatched differently, the logits
    /// could match and the answer still change.
    ///
    /// Greedy sampling makes the comparison exact rather than
    /// distributional.
    #[test]
    fn a_paged_request_generates_the_same_text_as_a_contiguous_one() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();

        let mut contiguous = String::new();
        let (finish_a, _rows, _ids, usage_a) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(6),
            None,
            None,
            None,
            None,
            |_, s| contiguous.push_str(s),
        )
        .unwrap();

        let config = paged_config(&decoder, /* block_size = */ 4, /* blocks = */ 64);
        let mut paged = String::new();
        let (finish_b, _rows, _ids, usage_b) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(6),
            None,
            Some(&config),
            None,
            None,
            |_, s| paged.push_str(s),
        )
        .unwrap();

        assert_eq!(paged, contiguous, "paged serving changed the answer");
        assert_eq!(finish_b, finish_a);
        assert_eq!(usage_b.completion_tokens, usage_a.completion_tokens);
        assert_eq!(usage_b.prompt_tokens, usage_a.prompt_tokens);
    }

    /// Every page comes back when the request ends.
    ///
    /// `PagedKvCache` has no `Drop`, so releasing is `PagedLease`'s job
    /// alone. Without it the store bleeds a whole request's pages per
    /// request and a long-running server stops admitting anything, with
    /// nothing in the logs to say why. Checked per layer, since the
    /// lease releases in a loop and a bound that skipped the last layer
    /// would still look right on layer 0.
    #[test]
    fn a_finished_paged_request_returns_every_page_it_held() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let config = paged_config(&decoder, 4, 64);
        let before: Vec<usize> = (0..decoder.layers.len())
            .map(|l| config.store.free_blocks(l))
            .collect();

        for _ in 0..3 {
            let mut out = String::new();
            generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                &prompt,
                &greedy_params(5),
                None,
                Some(&config),
                None,
                None,
                |_, s| out.push_str(s),
            )
            .unwrap();
        }

        for (l, expected) in before.iter().enumerate() {
            assert_eq!(
                config.store.free_blocks(l),
                *expected,
                "layer {l} leaked pages across repeated requests"
            );
        }
    }

    /// **`n` > 1 on the PAGED store answers what the contiguous store
    /// answers**, and prefills once.
    ///
    /// The end-to-end property the page arithmetic is in service of. A
    /// fork that shared the ragged tail page produces plausible text
    /// with one choice's token in another's context, which nothing but
    /// a comparison against the unforked path can see; and a fork that
    /// re-prefilled per choice produces the RIGHT text while billing
    /// four prompts for one, which nothing but the usage can see. Both
    /// are checked here, because each hides the other's failure.
    #[test]
    fn several_choices_on_the_paged_store_match_the_contiguous_ones() {
        let decoder = small_decoder();
        let prompt = "abcabcabcabcabcabc";
        let mut four = greedy_params(6);
        // Temperature, not greedy: at temperature 0 every choice is the
        // same sequence by design, and four identical answers cannot
        // show a tail page one choice wrote into another's.
        four.sampling.temperature = 1.0;
        four.n = 4;

        let run = |paged: Option<&PagedKvConfig>, params: &GenerationParams| {
            let mut per_choice = vec![String::new(); 4];
            let (finishes, _rows, _ids, usage) = generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                prompt,
                params,
                None,
                paged,
                None,
                None,
                |choice, s| per_choice[choice].push_str(s),
            )
            .expect("n = 4");
            assert_eq!(finishes.len(), 4, "one finish reason per choice");
            (per_choice, usage)
        };

        let config = paged_config(
            &decoder, /* block_size = */ 4, /* blocks = */ 4_000,
        );
        let (contiguous, contiguous_usage) = run(None, &four);
        let (paged, paged_usage) = run(Some(&config), &four);

        assert_eq!(
            paged, contiguous,
            "the paged forks answered differently from the contiguous ones"
        );
        // The premise, or the comparison above holds trivially.
        assert!(
            contiguous[1..].iter().any(|t| *t != contiguous[0]),
            "every choice matched choice 0, so the derived seeds did nothing: {contiguous:?}"
        );
        // Prefilled ONCE, which is the whole reason the field exists.
        assert_eq!(
            paged_usage.prompt_tokens, contiguous_usage.prompt_tokens,
            "the paged path billed a different number of prompts"
        );

        // **The sensitive half.** At temperature 0 the sampler is
        // deterministic, so every choice MUST be the same sequence: a
        // fork whose pages carry anything but the original's KV shows
        // up here as a choice that differs from choice 0, which the
        // comparison above can miss -- a small logit change crosses a
        // sampling boundary only sometimes, and this one has none to
        // cross.
        let mut greedy = greedy_params(6);
        greedy.n = 4;
        let (greedy_paged, _) = run(Some(&config), &greedy);
        assert!(
            greedy_paged.iter().all(|t| *t == greedy_paged[0]),
            "at temperature 0 the choices must be identical, so a fork read pages that were \
             not the original's: {greedy_paged:?}"
        );
        let (greedy_contiguous, _) = run(None, &greedy);
        assert_eq!(
            greedy_paged[0], greedy_contiguous[0],
            "the paged answer differs from the contiguous one before any fork is involved"
        );
        assert!(!greedy_paged[0].is_empty(), "nothing was generated");
    }

    /// **A fork predicts exactly what the original predicts.**
    ///
    /// The property every other test here is circumstantial evidence
    /// for, stated where it is sharp: feed the same token to the
    /// original and to its fork and the logit vectors must be
    /// BIT-IDENTICAL, because the two sequences have the same history.
    ///
    /// Text cannot say this. A tiny random decoder's greedy answer
    /// survives a zeroed page -- measured, not assumed: with the tail
    /// copy removed, four choices still agreed on every token -- so a
    /// text comparison is evidence that the plumbing runs and no
    /// evidence at all about which bytes it read.
    #[test]
    fn a_fork_predicts_exactly_what_the_original_predicts() {
        let decoder = small_decoder();
        let block_size = 4;
        let config = paged_config(&decoder, block_size, /* blocks = */ 400);
        // Ragged on purpose: the tail page is the only one a fork
        // copies, so a prompt ending on a boundary cannot see the copy.
        let tokens: Vec<usize> = (1..=9).collect();
        assert_ne!(tokens.len() % block_size, 0, "a ragged tail is the point");
        let max_seq_len = tokens.len() + 8;

        let mut kv = Kv::Paged(
            acquire_paged_caches(&decoder, &config, &tokens, max_seq_len, None)
                .expect("the store is large enough"),
        );
        let _ = kv.prefill(&decoder, &tokens, false);
        let Kv::Paged(lease) = kv else { unreachable!() };

        let mut fork = Kv::Paged(lease.fork().expect("the store has room"));
        let mut original = Kv::Paged(lease);

        let next = 7usize;
        let pos = tokens.len();
        let want = original.step(&decoder, next, pos);
        let got = fork.step(&decoder, next, pos);

        assert!(
            want.iter().any(|x| *x != 0.0),
            "no logits, so nothing is pinned"
        );
        assert_eq!(
            want, got,
            "the fork predicted something else, so its pages do not carry the original's KV"
        );
    }

    /// **The fork SHARES the full pages and COPIES only the tail.**
    ///
    /// The whole point of copy-on-write, stated as page arithmetic
    /// rather than as text: a fork that copied the prompt would cost
    /// one group per prompt page, and a fork that shared everything
    /// would put the second choice's tokens in the first's context.
    /// The right answer is in between, and only an accounting test can
    /// see which one happened -- both wrong versions generate
    /// plausible text.
    #[test]
    fn a_fork_shares_the_full_pages_and_copies_only_the_ragged_tail() {
        let decoder = small_decoder();
        let block_size = 4;
        let config = paged_config(&decoder, block_size, /* blocks = */ 400);
        // Nine positions over pages of four: two full pages and a tail
        // holding one. A prompt that ended on a boundary could not tell
        // a copied tail from a shared one.
        let tokens: Vec<usize> = (1..=9).collect();
        let max_seq_len = tokens.len() + 8;

        // Prefill really writes the positions, so `seq_len` is ragged.
        let mut kv = Kv::Paged(
            acquire_paged_caches(&decoder, &config, &tokens, max_seq_len, None)
                .expect("the store is large enough"),
        );
        let _ = kv.prefill(&decoder, &tokens, false);
        let Kv::Paged(lease) = kv else { unreachable!() };
        assert_eq!(
            lease.caches[0].seq_len(),
            tokens.len(),
            "prefill did not run"
        );

        let held = lease.groups.len();
        let free_before = config.store.free_groups();
        let fork = lease.fork().expect("the store has room");

        // Every page but the shared prefix is fresh, and the shared
        // prefix cost nothing.
        let shared = tokens.len() / block_size;
        assert_eq!(
            free_before - config.store.free_groups(),
            held - shared,
            "the fork took the wrong number of pages: it should share {shared} full pages \
             of {held} and take the rest"
        );
        assert!(shared > 0, "no page was shared, so this proved nothing");

        // The shared ones are the SAME groups, with two holders each.
        for i in 0..shared {
            assert_eq!(
                lease.groups[i], fork.groups[i],
                "page {i} was copied where it should have been shared"
            );
            assert_eq!(
                config.store.group_refs(lease.groups[i].unwrap()),
                2,
                "shared page {i} has the wrong holder count"
            );
        }
        // The tail is NOT, or the two choices write into one page.
        assert_ne!(
            lease.groups[shared], fork.groups[shared],
            "the part-written tail page was shared, so the second choice's token would \
             land in the first choice's context"
        );
    }

    /// **The copied tail carries the original's CONTENT.**
    ///
    /// A fork that allocated a fresh tail page and forgot to copy it
    /// would pass the accounting test above exactly -- same page
    /// counts, same sharing -- and answer from zeroed keys for the
    /// positions inside that page. So the bytes are compared directly.
    #[test]
    fn the_copied_tail_page_holds_the_same_keys_as_the_original() {
        let decoder = small_decoder();
        let block_size = 4;
        let config = paged_config(&decoder, block_size, /* blocks = */ 400);
        let tokens: Vec<usize> = (1..=9).collect();
        let max_seq_len = tokens.len() + 8;

        let mut kv = Kv::Paged(
            acquire_paged_caches(&decoder, &config, &tokens, max_seq_len, None)
                .expect("the store is large enough"),
        );
        let _ = kv.prefill(&decoder, &tokens, false);
        let Kv::Paged(lease) = kv else { unreachable!() };

        let fork = lease.fork().expect("the store has room");
        let tail = tokens.len() / block_size;
        let (src, dst) = (lease.groups[tail].unwrap(), fork.groups[tail].unwrap());
        assert_ne!(src, dst, "the tail was shared, so this proved nothing");

        let a = config.store.group_blocks(src);
        let b = config.store.group_blocks(dst);
        let mut compared = 0usize;
        for layer in 0..a.len() {
            let store = config.store.read(layer);
            // Only the written offsets: the rest of the page is
            // whatever it was, and a copy of it is not load-bearing.
            for offset in 0..tokens.len() % block_size {
                assert_eq!(
                    store.k_row(a[layer], offset),
                    store.k_row(b[layer], offset),
                    "layer {layer} offset {offset}: the fork's tail page was not copied"
                );
                assert_eq!(store.v_row(a[layer], offset), store.v_row(b[layer], offset),);
                compared += 1;
            }
        }
        assert!(compared > 0, "nothing was compared");
        // And the rows really carry something, or two zeroed pages
        // would compare equal and prove nothing.
        let store = config.store.read(0);
        assert!(
            store.k_row(a[0], 0).iter().any(|x| *x != 0.0),
            "the original's tail page is all zeros, so equality proves nothing"
        );
    }

    /// A fork releases its pages when it drops, like every other lease.
    ///
    /// The failure this catches is the expensive one: a fork that held
    /// the shared prefix without retaining it would let the ORIGINAL's
    /// drop free pages the fork still reads, and a fork that retained
    /// without releasing would leak the whole prompt once per choice.
    #[test]
    fn forks_return_every_page_they_took_and_no_more() {
        let decoder = small_decoder();
        let block_size = 4;
        let config = paged_config(&decoder, block_size, /* blocks = */ 400);
        let tokens: Vec<usize> = (1..=9).collect();
        let max_seq_len = tokens.len() + 8;
        let free_at_rest = config.store.free_groups();

        {
            let mut kv = Kv::Paged(
                acquire_paged_caches(&decoder, &config, &tokens, max_seq_len, None).unwrap(),
            );
            let _ = kv.prefill(&decoder, &tokens, false);
            let Kv::Paged(lease) = kv else { unreachable!() };
            let forks: Vec<PagedLease> = (0..3).map(|_| lease.fork().expect("room")).collect();
            assert_eq!(forks.len(), 3);
            assert_eq!(
                config.store.group_refs(lease.groups[0].unwrap()),
                4,
                "one original and three forks should hold the first page"
            );
        }

        assert_eq!(
            config.store.free_groups(),
            free_at_rest,
            "a fork leaked pages, or released one it did not hold"
        );
    }

    /// A windowed request refuses to fork BY NAME rather than forking
    /// into two sequences that recycle out of one set of pages.
    #[test]
    fn a_sliding_window_request_refuses_to_fork() {
        let decoder = windowed_decoder(8);
        let config = paged_config(&decoder, 4, /* blocks = */ 200);
        let tokens: Vec<usize> = vec![1, 2, 3];
        let lease = acquire_paged_caches(&decoder, &config, &tokens, tokens.len() + 64, None)
            .expect("the store is large enough");
        assert!(
            lease.window.is_some(),
            "not a windowed request, so this proved nothing"
        );
        assert_eq!(lease.fork().err(), Some(ForkRefused::SlidingWindow));
    }

    /// A store with no room refuses in the caller's hand and gives back
    /// what the attempt took.
    ///
    /// The half that matters is the second: a fork that ran out on its
    /// last page and returned early would strand every page it had
    /// already taken, and the server would lose them until it restarted.
    #[test]
    fn a_fork_that_cannot_be_taken_returns_the_pages_it_took() {
        let decoder = small_decoder();
        let block_size = 4;
        let tokens: Vec<usize> = (1..=9).collect();
        let max_seq_len = tokens.len() + 40;
        // Exactly one request's worth, so the first fork cannot be
        // taken and must try, take some, and give them back.
        let need = {
            let sized = paged_config(&decoder, block_size, 1_000);
            let lease = acquire_paged_caches(&decoder, &sized, &tokens, max_seq_len, None).unwrap();
            lease.groups.len()
        };
        let config = paged_config(&decoder, block_size, need);
        let mut kv = Kv::Paged(
            acquire_paged_caches(&decoder, &config, &tokens, max_seq_len, None)
                .expect("sized to exactly one request"),
        );
        let _ = kv.prefill(&decoder, &tokens, false);
        let Kv::Paged(lease) = kv else { unreachable!() };

        assert_eq!(config.store.free_groups(), 0, "the store should be full");
        assert_eq!(lease.fork().err(), Some(ForkRefused::StoreExhausted));
        assert_eq!(
            config.store.free_groups(),
            0,
            "the failed fork should have given back exactly what it took"
        );
        // And the original is untouched: it still holds every page.
        assert_eq!(lease.groups.len(), need);
    }

    /// A model where EVERY layer slides by the same window, which is
    /// what makes a page group releasable: the group holds one block in
    /// every layer, so one full-attention layer would still be reading
    /// the block the slide gave away.
    fn windowed_decoder(window: usize) -> Decoder {
        let mut cfg = test_dense_fixture();
        cfg.sliding_window = Some(window);
        cfg.swa_layers = frink_models::swa_layers::SwaLayers::All;
        Decoder::new_random_small(cfg, 2, 256)
    }

    fn run_to_completion(max_tokens: usize) -> GenerationParams {
        GenerationParams {
            // Every one of these tokens must actually be generated, or
            // a run that stopped at token 5 would "pass" a test about
            // what happens after 200.
            ignore_eos: true,
            ..greedy_params(max_tokens)
        }
    }

    /// The arithmetic both admission paths share, on its own.
    ///
    /// A unit test because the two callers price the same request in
    /// different units -- the store in page groups, the batch
    /// scheduler's budget in positions -- and a formula that drifted
    /// between them would have the two components refusing and
    /// admitting the same request.
    #[test]
    fn a_window_prices_a_request_at_its_prompt_plus_a_bound() {
        let block_size = 4;
        let policy = WindowPolicy::new(8, block_size);
        let bound = slide_hold_bound(8, &policy);
        assert_eq!(bound, 2 * (8 + SWA_RETAIN_GAP) + 128 + 2 * block_size);

        // Full attention: the whole sequence, however long.
        assert_eq!(paged_hold_positions(10_000, 3, block_size, None), 10_000);
        assert_eq!(paged_groups_needed(10_000, 3, block_size, None), 2_500);

        // Windowed: prompt plus the bound plus a page of slack, and
        // flat in `max_seq_len` -- ten times the generation costs the
        // same pages, which IS the feature.
        let held = paged_hold_positions(10_000, 3, block_size, Some(&policy));
        assert_eq!(held, 3 + bound + block_size);
        assert_eq!(
            paged_hold_positions(100_000, 3, block_size, Some(&policy)),
            held,
            "a longer generation must not cost more pages"
        );
        // But a longer PROMPT does, because prefill materialises
        // positions 0..prompt_len to reuse the one prefill kernel.
        assert!(paged_hold_positions(10_000, 900, block_size, Some(&policy)) > held);

        // And a request shorter than the bound is not made more
        // expensive by being windowed.
        assert_eq!(paged_hold_positions(20, 3, block_size, Some(&policy)), 20);
        assert_eq!(
            paged_groups_needed(20, 3, block_size, Some(&policy)),
            paged_groups_needed(20, 3, block_size, None)
        );
    }

    /// THE ACCEPTANCE PROPERTY of this feature: a window model holds its
    /// prompt and a window, not its whole context.
    ///
    /// Stated as the only thing an operator can actually observe --
    /// whether the request is served. One store, one prompt, one length,
    /// two models: the windowed one runs, the full-attention one is
    /// refused by the same store. The window is the whole difference.
    #[test]
    fn a_window_model_runs_on_a_store_too_small_for_its_whole_context() {
        let window = 8;
        let windowed = windowed_decoder(window);
        assert_eq!(
            windowed.config.uniform_sliding_window(),
            Some(window),
            "the fixture must be uniformly windowed or this test proves nothing"
        );
        let full = small_decoder();
        assert_eq!(full.config.uniform_sliding_window(), None);

        let block_size = 4;
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let max_tokens = 400;
        // Between the two answers: 48 groups for the windowed request,
        // 101 for the same request without a window.
        let blocks = 60;
        let params = run_to_completion(max_tokens);

        let win_config = paged_config(&windowed, block_size, blocks);
        let mut out = String::new();
        let (_, _rows, _ids, usage) = generate(
            &windowed,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &params,
            None,
            Some(&win_config),
            None,
            None,
            |_, s| out.push_str(s),
        )
        .expect("a window model must fit a store sized for its window");
        assert_eq!(usage.completion_tokens, max_tokens);

        // Full attention, and ALTERNATING attention, are both refused by
        // the same store. The alternating case is the one worth spelling
        // out: its `kv_block_window` is `Some(8)`, so a slide keyed on
        // that question would have admitted it and then freed pages its
        // full-attention layers were still reading -- a wrong answer
        // rather than a refusal.
        let mut alternating_cfg = test_dense_fixture();
        alternating_cfg.sliding_window = Some(window);
        alternating_cfg.swa_layers = frink_models::swa_layers::SwaLayers::period(2, false);
        let alternating = Decoder::new_random_small(alternating_cfg, 2, 256);
        assert_eq!(alternating.config.kv_block_window(), Some(window));
        assert_eq!(alternating.config.uniform_sliding_window(), None);

        for (name, model) in [("full attention", &full), ("alternating", &alternating)] {
            let config = paged_config(model, block_size, blocks);
            let err = generate(
                model,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                &prompt,
                &params,
                None,
                Some(&config),
                None,
                None,
                |_, _| {},
            )
            .unwrap_err();
            assert!(matches!(err, DecodeError::KvPoolExhausted), "{name}: {err}");
        }
    }

    /// Sliding must not change what the model says.
    ///
    /// The sharp end of the whole design: a recycled page is overwritten
    /// by a later position, so a page freed one token too early does not
    /// fail -- it answers with another position's keys. Greedy sampling
    /// against the contiguous path, which applies the same window in
    /// attention and frees nothing, makes that visible as different
    /// text.
    ///
    /// Long enough to slide many times over: the default cadence is 128
    /// steps, so a six-token test would exercise none of this.
    #[test]
    fn a_sliding_paged_request_says_the_same_thing_as_a_contiguous_one() {
        let decoder = windowed_decoder(8);
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let params = run_to_completion(400);

        let mut contiguous = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &params,
            None,
            None,
            None,
            None,
            |_, s| contiguous.push_str(s),
        )
        .unwrap();

        let config = paged_config(&decoder, /* block_size = */ 4, /* blocks = */ 60);
        let mut paged = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &params,
            None,
            Some(&config),
            None,
            None,
            |_, s| paged.push_str(s),
        )
        .unwrap();

        assert_eq!(paged, contiguous, "the window slide changed the answer");
    }

    /// Once the window is sliding, a request stops asking the store for
    /// pages: it reuses its own.
    ///
    /// This is what keeps the footprint flat, and it is measured as the
    /// STORE's free-group count rather than as anything the lease
    /// reports about itself. A request that quietly kept acquiring would
    /// still answer correctly and would still bring a busy server down.
    #[test]
    fn a_long_windowed_generation_stops_taking_pages_from_the_store() {
        let decoder = windowed_decoder(8);
        let block_size = 4;
        let config = paged_config(&decoder, block_size, /* blocks = */ 200);
        let tokens: Vec<usize> = vec![1, 2, 3];

        let mut lease =
            acquire_paged_caches(&decoder, &config, &tokens, tokens.len() + 4_000, None)
                .expect("the store holds a window's worth");
        let after_admission = config.store.free_groups();
        let held = lease.groups.len();

        for pos in tokens.len()..tokens.len() + 4_000 {
            lease.before_step(pos);
            assert_eq!(
                config.store.free_groups(),
                after_admission,
                "position {pos} took a page from the store instead of recycling"
            );
        }
        assert!(
            lease.window.as_ref().unwrap().released > 0,
            "4000 positions at a window of 8 must have slid"
        );
        // Live pages plus spares is what admission reserved: recycling
        // moves groups between the two, it does not create or lose them.
        assert_eq!(
            lease.groups.iter().flatten().count() + lease.spare.len(),
            held
        );
    }

    /// A tool call holds the window back at the position the next turn
    /// will rejoin at.
    ///
    /// An agentic turn does not end the conversation: the harness runs
    /// the tool and comes back with the same context up to the call and
    /// a different one after it. So the position where the call opened
    /// is where the next request rejoins, and a window that followed the
    /// cursor would have thrown it away by then.
    ///
    /// Two identical runs, one with the checkpoint's anchor token
    /// configured and one without, differing only in that. The anchored
    /// one must hold strictly more, and must then let go once the cursor
    /// has drifted a whole window past -- because holding the cursor and
    /// an unbounded-distance anchor is what the pool sizing cannot pay
    /// for.
    #[test]
    fn a_tool_call_anchor_holds_the_window_back_and_then_lets_go() {
        let anchor_token = 77;
        let block_size = 4;
        let decoder = windowed_decoder(8);
        let tokens: Vec<usize> = vec![1, 2, 3];

        // Released positions at `check`, with the anchor token offered
        // at each of `at`, when `armed`.
        let released_at = |armed: bool, at: &[usize], check: usize| -> usize {
            let mut config = paged_config(&decoder, block_size, 400);
            // Every four steps rather than every 128: the cadence is
            // what makes the anchor's effect observable at a chosen
            // position rather than at the next multiple of the default.
            config.slide_interval = 4;
            config.anchor_token = armed.then_some(anchor_token as u32);
            let mut lease =
                acquire_paged_caches(&decoder, &config, &tokens, tokens.len() + 1_000, None)
                    .expect("the store is large enough");
            for pos in tokens.len()..check {
                if at.contains(&(pos + 1)) {
                    lease.observe_sampled(anchor_token, pos + 1, false);
                }
                lease.before_step(pos);
            }
            lease.window.as_ref().unwrap().released
        };

        // Just after the call: the anchored run has held on to the
        // pages around it, the unanchored one has moved past them.
        let first = 200;
        assert!(
            released_at(true, &[first], 208) < released_at(false, &[first], 208),
            "the anchor did not hold the window back"
        );

        // Far past it: the anchored run has caught up, because the turn
        // is clearly not about to end and holding two windows an
        // unbounded distance apart needs an unbounded pool.
        assert_eq!(
            released_at(true, &[first], 600),
            released_at(false, &[first], 600),
            "the anchor was never dropped, so the hold is unbounded"
        );

        // And a LATER call anchors again. Only the first call of a turn
        // is the anchor, so a request that had one and dropped it must
        // be able to take another -- otherwise one early tool call
        // spends the anchor for the whole rest of the conversation.
        let second = 600;
        assert!(
            released_at(true, &[first, second], 610) < released_at(false, &[], 610),
            "a dropped anchor left the request unable to take another"
        );
    }

    /// A slid request returns its recycled pages too.
    ///
    /// `Drop` releases what `groups` names, and a slide takes groups OUT
    /// of `groups` -- so a lease that forgot its spare list would give
    /// back only the live window and leak everything the slide had
    /// recycled, which on a window model is most of what it held.
    /// Checked per layer, since a bound that skipped the last layer
    /// would still look right on layer 0.
    #[test]
    fn a_slid_request_returns_the_pages_it_recycled_as_well_as_the_ones_it_held() {
        let decoder = windowed_decoder(8);
        let prompt = String::from_utf8(vec![1u8, 2, 3]).unwrap();
        let config = paged_config(&decoder, /* block_size = */ 4, /* blocks = */ 60);
        let before: Vec<usize> = (0..decoder.layers.len())
            .map(|l| config.store.free_blocks(l))
            .collect();

        // Three runs on a store that could not serve even one of them
        // twice over: a leak shows as the second run being refused.
        for run in 0..3 {
            let mut out = String::new();
            generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                &prompt,
                &run_to_completion(400),
                None,
                Some(&config),
                None,
                None,
                |_, s| out.push_str(s),
            )
            .unwrap_or_else(|e| panic!("run {run} was refused: {e}"));
        }

        for (l, expected) in before.iter().enumerate() {
            assert_eq!(
                config.store.free_blocks(l),
                *expected,
                "layer {l} kept the pages a slid request recycled"
            );
        }
    }

    /// THE SAFETY BOUNDARY: every page the kernel still reads is one the
    /// lease still owns.
    ///
    /// The paged attention kernel indexes `block_table[t / block_size]`
    /// for `t` from `seq_len - window`, so an index in that range whose
    /// group has been recycled is a page some LATER position has been
    /// writing into. That does not fail -- it answers with another
    /// position's keys.
    ///
    /// Checked as a property at every step rather than at the end,
    /// because a page freed one step too early is back to being safe a
    /// few steps later once the window has moved past it. And checked
    /// here rather than through generated text, which cannot see it: the
    /// reserve slack means a recycled page is not physically reused
    /// until long after the window has left it, so a slide freeing a
    /// whole window too much still produces the right answer on this
    /// fixture. Robust, but not evidence -- so the invariant is stated
    /// where it is true rather than where it happens to show.
    #[test]
    fn every_page_the_kernel_still_reads_is_one_the_lease_still_owns() {
        let window = 8;
        let block_size = 4;
        let decoder = windowed_decoder(window);
        let config = paged_config(&decoder, block_size, /* blocks = */ 200);
        let tokens: Vec<usize> = vec![1, 2, 3];

        let mut lease =
            acquire_paged_caches(&decoder, &config, &tokens, tokens.len() + 4_000, None)
                .expect("the store holds a window's worth");

        for pos in tokens.len()..tokens.len() + 4_000 {
            lease.before_step(pos);
            // What attention sees once this position is written.
            let seq_len = pos + 1;
            let first = seq_len.saturating_sub(window) / block_size;
            for i in first..=pos / block_size {
                assert!(
                    lease.groups[i].is_some(),
                    "at position {pos} the window reaches page {i}, which was recycled"
                );
            }
        }
        assert!(
            lease.window.as_ref().unwrap().released > 0,
            "nothing was recycled, so this proved nothing"
        );
    }

    /// A slid request never gives away the prefix the tree owns.
    ///
    /// Those pages are shared: another request is attending over them
    /// right now, and a third will adopt them tomorrow. The slide floors
    /// at the locked prefix rather than at zero for exactly that reason,
    /// and this checks the floor by looking at which page groups are
    /// still there rather than at what the policy returned.
    #[test]
    fn a_slid_request_never_recycles_the_prefix_the_tree_owns() {
        let decoder = windowed_decoder(8);
        let block_size = 4;
        let config = paged_config_with_radix(&decoder, block_size, 400, true);

        // Publish a prefix: a short request, which does not slide, so
        // its pages reach the tree.
        let shared: Vec<u8> = (1u8..=16).collect();
        let prompt = String::from_utf8(shared.clone()).unwrap();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(2),
            None,
            Some(&config),
            None,
            None,
            |_, _| {},
        )
        .unwrap();

        // A second request off that prefix, driven long past the window.
        let tokens: Vec<usize> = shared.iter().map(|&b| b as usize).collect();
        let mut lease =
            acquire_paged_caches(&decoder, &config, &tokens, tokens.len() + 3_000, None)
                .expect("the store is large enough");
        let locked = lease.adopted_positions(block_size);
        assert!(locked > 0, "this test needs a real prefix match");

        for pos in tokens.len()..tokens.len() + 3_000 {
            lease.before_step(pos);
        }
        let released = lease.window.as_ref().unwrap().released;
        assert!(released >= locked, "the slide must have passed the prefix");
        for i in 0..locked / block_size {
            assert!(
                lease.groups[i].is_some(),
                "page {i} of the shared prefix was recycled out from under the tree"
            );
        }
    }

    /// A sequence whose window slid is NOT published to the tree.
    ///
    /// The tree keys on a prefix, and a slid sequence's prefix is
    /// precisely the part it gave away -- what it still holds is a
    /// suffix at the cursor. Publishing anyway would hand the next
    /// request pages whose contents belong a thousand positions later,
    /// and it would match on them, and the answer would be wrong rather
    /// than slow.
    ///
    /// The control is the same prompt on a request too short to slide,
    /// which does publish. Without it this test would pass against a
    /// tree that never publishes anything.
    #[test]
    fn a_slid_sequence_is_not_published_to_the_tree() {
        let decoder = windowed_decoder(8);
        let block_size = 4;
        let prompt = String::from_utf8((1u8..=16).collect::<Vec<u8>>()).unwrap();

        let cached_after = |first: GenerationParams| -> usize {
            let config = paged_config_with_radix(&decoder, block_size, 400, true);
            let mut out = String::new();
            generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                &prompt,
                &first,
                None,
                Some(&config),
                None,
                None,
                |_, s| out.push_str(s),
            )
            .unwrap();
            // What the SECOND request adopted, which on a fresh tree is
            // only ever what the first published.
            let mut probe = String::new();
            let (_, _rows, _ids, usage) = generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                &prompt,
                &greedy_params(2),
                None,
                Some(&config),
                None,
                None,
                |_, s| probe.push_str(s),
            )
            .unwrap();
            usage.cached_tokens.unwrap_or(0)
        };

        assert!(
            cached_after(greedy_params(2)) > 0,
            "a request that never slid must publish its pages"
        );
        assert_eq!(
            cached_after(run_to_completion(600)),
            0,
            "a slid sequence published a prefix it no longer holds"
        );
    }

    /// THE ACCEPTANCE PROPERTY: two prompts sharing a prefix hold ONE
    /// copy of its pages, not two.
    ///
    /// This is what `frink-models::prefix_cache` structurally cannot
    /// do. That one clones a `Vec<KvCache>` per entry, so the second
    /// conversation off a shared system prompt holds its own copy of
    /// that prompt's KV. Here the second adopts the first's page
    /// groups and the refcount goes to two, so the pages consumed by
    /// two requests are strictly fewer than twice one request's.
    ///
    /// Measured as free groups, which is the store's own count rather
    /// than a number this test computes.
    #[test]
    fn two_prompts_sharing_a_prefix_hold_one_copy_of_it() {
        let decoder = small_decoder();
        // A shared prefix of 8 bytes, then one differing byte each.
        let shared: Vec<u8> = (1u8..=8).collect();
        let mut a = shared.clone();
        a.push(40);
        let mut b = shared.clone();
        b.push(50);
        let prompt_a = String::from_utf8(a).unwrap();
        let prompt_b = String::from_utf8(b).unwrap();

        let run = |config: &PagedKvConfig, prompt: &str| -> String {
            let mut out = String::new();
            generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                prompt,
                &greedy_params(2),
                None,
                Some(config),
                None,
                None,
                |_, s| out.push_str(s),
            )
            .unwrap();
            out
        };

        // Measured as what each request COSTS the store, not what is
        // free afterwards: without a tree every group is released at
        // the end, so free-afterwards is identical either way and
        // measures nothing. With a tree, pages it keeps stay held, so
        // the drop in free groups is what each request added.
        let cfg = paged_config_with_radix(&decoder, 4, 64, true);
        let start = cfg.store.free_groups();
        let text_a = run(&cfg, &prompt_a);
        let after_a = cfg.store.free_groups();
        let text_b = run(&cfg, &prompt_b);
        let after_b = cfg.store.free_groups();

        let cost_a = start - after_a;
        let cost_b = after_a - after_b;
        assert!(cost_a > 0, "the first request must publish something");
        assert!(
            cost_b < cost_a,
            "the second request shares A's prefix and must cost less \
             (first {cost_a} groups, second {cost_b})"
        );

        // And the saving is REPORTED, not merely real: a caller sees
        // the adopted positions as `cached_tokens`, the same field the
        // contiguous prefix cache uses for the same meaning.
        let cfg2 = paged_config_with_radix(&decoder, 4, 64, true);
        let mut sink = String::new();
        let (_f, _rows, _ids, first_usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt_a,
            &greedy_params(2),
            None,
            Some(&cfg2),
            None,
            None,
            |_, s| sink.push_str(s),
        )
        .unwrap();
        assert_eq!(
            first_usage.cached_tokens,
            Some(0),
            "a cold tree reuses nothing"
        );
        let (_f, _rows, _ids, second_usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt_b,
            &greedy_params(2),
            None,
            Some(&cfg2),
            None,
            None,
            |_, s| sink.push_str(s),
        )
        .unwrap();
        let reused = second_usage.cached_tokens.expect("a tree is configured");
        assert!(
            reused >= 8,
            "the 8-token shared prefix must be reported as reused, got {reused}"
        );

        // And the answers are unchanged: adopting a prefix must not
        // change what the model says. A cache that is fast and wrong is
        // worse than no cache.
        let plain = paged_config(&decoder, 4, 64);
        assert_eq!(text_a, run(&plain, &prompt_a), "prompt A changed");
        assert_eq!(text_b, run(&plain, &prompt_b), "prompt B changed");
    }

    /// Repeated requests off one prefix do not exhaust the store.
    ///
    /// The leak this guards is specific: `insert_prefix` reports how
    /// much of the span the tree ALREADY had, and those pages of ours
    /// are the ones the tree did not take. Retaining the wrong range
    /// either leaks them (retained but never released) or frees pages
    /// the tree still points at.
    #[test]
    fn many_requests_off_one_prefix_neither_leak_nor_free_the_trees_pages() {
        let decoder = small_decoder();
        let config = paged_config_with_radix(&decoder, 4, 64, true);
        let shared: Vec<u8> = (1u8..=8).collect();

        let mut lows = Vec::new();
        for suffix in 0..6u8 {
            let mut p = shared.clone();
            p.push(60 + suffix);
            let prompt = String::from_utf8(p).unwrap();
            let mut out = String::new();
            generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                &prompt,
                &greedy_params(2),
                None,
                Some(&config),
                None,
                None,
                |_, s| out.push_str(s),
            )
            .expect("the store is sized for many of these");
            lows.push(config.store.free_groups());
        }

        // The tree keeps some pages forever, so free groups settle to a
        // floor rather than returning to the start. What must NOT
        // happen is a monotone slide toward zero: after the prefix is
        // published once, later requests off it cost only their own
        // suffix, so the last two runs must leave the same amount free.
        assert_eq!(
            lows[lows.len() - 1],
            lows[lows.len() - 2],
            "steady state expected once the shared prefix is published; \
             free groups per run were {lows:?}"
        );
        assert!(
            lows[lows.len() - 1] > 0,
            "the store must not have been consumed: {lows:?}"
        );
    }

    /// A request too big for the store is refused at admission, having
    /// emitted nothing and taken no page.
    ///
    /// The refusal must happen BEFORE any work: `sample_until_stop`
    /// takes a closure returning `Vec<f32>` with nowhere to report a
    /// store that ran dry at token 300 of 400, which is why
    /// `acquire_paged_caches` reserves the whole worst-case length up
    /// front. The same reasoning `acquire_pooled_caches` records having
    /// learned from a live panic.
    #[test]
    fn a_paged_request_too_big_for_the_store_is_refused_before_emitting_anything() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3, 4]).unwrap();
        // One block of 2 positions per layer against a prompt of 4 plus
        // 8 more tokens.
        let config = paged_config(&decoder, /* block_size = */ 2, /* blocks = */ 1);

        let result = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(8),
            None,
            Some(&config),
            None,
            None,
            |_, _| panic!("a refused request must not emit"),
        );
        assert!(
            matches!(result, Err(DecodeError::KvPoolExhausted)),
            "expected a typed refusal, got {result:?}"
        );
        for l in 0..decoder.layers.len() {
            assert_eq!(
                config.store.free_blocks(l),
                1,
                "layer {l} must keep every page after a refusal"
            );
        }
    }

    #[test]
    fn generate_succeeds_with_a_pool_that_has_enough_blocks() {
        let decoder = small_decoder(); // 2 layers
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let pool = Arc::new(Mutex::new(KvBlockPool::new(64, 2)));
        let config = pool_config(pool.clone(), Duration::ZERO);

        let mut out = String::new();
        let (finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            Some(&config),
            None,
            None,
            None,
            |_, s| out.push_str(s),
        )
        .unwrap();
        assert_eq!(finish[0].0, FinishReason::Length);
        assert_eq!(
            pool.lock().unwrap().free_blocks(),
            2,
            "every acquired block must be released once the request finishes"
        );
    }

    /// Regression test for a real bug caught by live testing (not by
    /// any unit test): with a small `block_size`, a request whose
    /// prompt + max_tokens exceeds one block used to reserve only one
    /// block per layer at admission time, then panic deep inside
    /// `Decoder::forward_token` once decode outgrew that block and the
    /// pool had nothing left to grow into (`KvCache::push` returning
    /// `Err` where `forward_token` assumes it can't fail). Fixed by
    /// having `acquire_pooled_caches` reserve blocks for the whole
    /// worst-case sequence length up front. This test must not panic --
    /// it must either succeed cleanly or fail at admission with
    /// `KvPoolExhausted`, never partway through decode.
    #[test]
    fn generate_reserves_enough_blocks_up_front_for_a_sequence_spanning_multiple_blocks() {
        let decoder = small_decoder(); // 2 layers
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap(); // 2 tokens via ByteTokenizer
        let max_tokens = 10;
        let block_size = 2;
        // prompt (2) + max_tokens (10) = 12 positions -> 6 blocks/layer * 2 layers = 12 blocks.
        let pool = Arc::new(Mutex::new(KvBlockPool::new(block_size, 12)));
        let config = pool_config(pool.clone(), Duration::ZERO);

        let (finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(max_tokens),
            Some(&config),
            None,
            None,
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(finish[0].0, FinishReason::Length);
        assert_eq!(pool.lock().unwrap().free_blocks(), 12);
    }

    /// One block short of the worst case means the pool can *never*
    /// serve this request, so the refusal is the immovable one -- a 400
    /// naming `device_memory_budget_exceeded`, not a 503 inviting a
    /// retry that an empty pool would refuse identically.
    ///
    /// Confirmed to FAIL when `pool_immovable_refusal` is removed from
    /// `generate` (the request falls through to the acquisition and
    /// comes back as the retryable `KvPoolExhausted`), and when its
    /// `needed <= total_blocks` comparison is loosened to `<`.
    #[test]
    fn generate_fails_at_admission_not_mid_decode_when_the_pool_cannot_cover_the_worst_case() {
        let decoder = small_decoder(); // 2 layers
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let max_tokens = 10;
        let block_size = 2;
        // One block short of the 12 the worst case (see the test
        // above) actually needs.
        let pool = Arc::new(Mutex::new(KvBlockPool::new(block_size, 11)));
        let config = pool_config(pool.clone(), Duration::ZERO);

        let result = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(max_tokens),
            Some(&config),
            None,
            None,
            None,
            |_, _| {},
        );
        let err = result.expect_err("11 blocks cannot cover a 12-block worst case");
        assert!(
            matches!(
                &err,
                DecodeError::KvBudgetExceeded { binding, positions, .. }
                    if *binding == frink_models::Ceiling::DeviceMemory.code()
                        && *positions == 12
            ),
            "expected an immovable device-memory refusal, got {err:?}"
        );
        assert_eq!(
            err.retry_after_secs(),
            None,
            "no wait frees blocks that do not exist"
        );
        assert_eq!(
            pool.lock().unwrap().free_blocks(),
            11,
            "a rejected request must leave the pool exactly as it found it"
        );
    }

    /// A refusal's two halves must describe ONE reservation.
    ///
    /// `pool_immovable_refusal` reports `estimated_bytes` from
    /// `KvShape::kv_bytes_for_tokens` beside a block count of
    /// `max_seq_len.div_ceil(block_size) * n_layers`. The block count
    /// has never had a window in it -- `KvCache::with_pool` reserves
    /// `max_seq_len` positions for every layer -- while the byte figure
    /// used to discount the sliding layers. For gpt-oss at 8192
    /// positions the message therefore said ~390 MiB next to a ~768 MiB
    /// reservation the same function had just rejected (#33).
    ///
    /// Two decoders, same shape, one with a window: the refusal must
    /// price them identically, because the pool reserves for them
    /// identically.
    #[test]
    fn the_pool_refusals_byte_figure_does_not_discount_a_window_the_pool_still_reserves() {
        let full = small_decoder();
        let mut alternating_cfg = test_dense_fixture();
        alternating_cfg.sliding_window = Some(4);
        alternating_cfg.swa_layers = frink_models::swa_layers::SwaLayers::period(2, false);
        let alternating = Decoder::new_random_small(alternating_cfg, 2, 256);
        assert_eq!(
            alternating.config.uniform_sliding_window(),
            None,
            "an alternating model is the case no store may recycle"
        );

        // A pool that cannot cover one block per layer, so both models
        // reach the immovable refusal.
        let pool = Arc::new(Mutex::new(KvBlockPool::new(8, 1)));
        let config = pool_config(pool, Duration::ZERO);
        let max_seq_len = 64;

        let bytes_of =
            |decoder: &Decoder| match pool_immovable_refusal(decoder, &config, max_seq_len) {
                Some(DecodeError::KvBudgetExceeded {
                    estimated_bytes, ..
                }) => estimated_bytes,
                other => panic!("expected an immovable refusal, got {other:?}"),
            };

        let windowed_bytes = bytes_of(&alternating);
        assert_eq!(
            windowed_bytes,
            bytes_of(&full),
            "the window discounts nothing the pool reserves"
        );
        // And that figure is the whole reservation the refusal names:
        // every layer, every position.
        assert_eq!(
            windowed_bytes,
            KvShape::from_config(&full.config, KvElem::F32).per_token_kv_bytes()
                * max_seq_len as u64
        );
    }

    /// A one-block pool cannot hold a two-layer model's caches under any
    /// schedule, so this is the immovable refusal too.
    #[test]
    fn generate_rejects_the_request_without_leaking_blocks_when_the_pool_is_too_small() {
        let decoder = small_decoder(); // 2 layers -> needs 2 blocks, one per layer's cache
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let pool = Arc::new(Mutex::new(KvBlockPool::new(64, 1)));
        let config = pool_config(pool.clone(), Duration::ZERO);

        let result = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            Some(&config),
            None,
            None,
            None,
            |_, _| {},
        );
        let err = result.expect_err("one block cannot hold two layers' caches");
        assert!(
            matches!(
                &err,
                DecodeError::KvBudgetExceeded { binding, .. }
                    if *binding == frink_models::Ceiling::DeviceMemory.code()
            ),
            "expected an immovable device-memory refusal, got {err:?}"
        );
        assert_eq!(
            pool.lock().unwrap().free_blocks(),
            1,
            "a rejected request must leave the pool exactly as it found it"
        );
    }

    #[test]
    fn generate_releases_blocks_so_back_to_back_requests_do_not_starve_the_pool() {
        let decoder = small_decoder(); // 2 layers
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        // Just enough for one request's caches at a time -- a second,
        // concurrent request would be rejected, but a *sequential*
        // second request must succeed once the first has returned its
        // blocks.
        let pool = Arc::new(Mutex::new(KvBlockPool::new(64, 2)));
        let config = pool_config(pool.clone(), Duration::ZERO);

        for _ in 0..3 {
            let (finish, _rows, _ids, _usage) = generate(
                &decoder,
                &ServerTokenizer::Byte,
                &StopTokens::default(),
                None,
                &prompt,
                &greedy_params(5),
                Some(&config),
                None,
                None,
                None,
                |_, _| {},
            )
            .unwrap();
            assert_eq!(finish[0].0, FinishReason::Length);
        }
        assert_eq!(pool.lock().unwrap().free_blocks(), 2);
    }

    /// `queue_wait = 0` must reject on the first failed attempt rather
    /// than retry.
    ///
    /// The pool here is big enough for the request and *momentarily*
    /// held by someone else, which is the only situation in which
    /// `KvPoolExhausted` is an honest answer: a pool permanently too
    /// small is refused earlier, by `pool_immovable_refusal`, and would
    /// make the timing assertion below vacuous.
    #[test]
    fn generate_with_zero_queue_wait_rejects_immediately() {
        let decoder = small_decoder(); // 2 layers -> needs 2 blocks
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let pool = Arc::new(Mutex::new(KvBlockPool::new(64, 2)));

        // Another in-flight request holding both blocks for longer than
        // this request is willing to wait for them.
        let holder_pool = pool.clone();
        let holder = std::thread::spawn(move || {
            let mut held = KvCache::with_pool(1, 1, holder_pool, 0).unwrap();
            held.push(&[0.0], &[0.0]).unwrap(); // crosses into the second block
            std::thread::sleep(Duration::from_millis(200));
            drop(held);
        });
        std::thread::sleep(Duration::from_millis(15));

        let config = pool_config(pool, Duration::ZERO);
        let started = Instant::now();
        let result = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            Some(&config),
            None,
            None,
            None,
            |_, _| {},
        );
        assert!(
            matches!(result, Err(DecodeError::KvPoolExhausted)),
            "a pool that could serve this request once its holder lets go is momentary \
             exhaustion, which is retryable"
        );
        assert!(
            started.elapsed() < Duration::from_millis(50),
            "queue_wait=0 must reject on the first attempt, not retry: took {:?}",
            started.elapsed()
        );
        holder.join().unwrap();
    }

    /// The private path's context ceiling, and the property that makes
    /// it worth having: the refusal happens before any KV is acquired
    /// and before a single forward pass runs.
    ///
    /// The refusal is for a prompt that does not fit BY ITSELF. That is
    /// the only case with no output budget left to clamp to; see the
    /// test below for the other outcome.
    ///
    /// Confirmed to FAIL when the `prompt_refusal` block is removed
    /// from `generate` -- the request then runs to completion and
    /// returns `Ok`.
    #[test]
    fn a_prompt_past_the_context_ceiling_is_refused_before_any_kv_is_acquired() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2, 3, 4, 5]).unwrap();
        let pool = Arc::new(Mutex::new(KvBlockPool::new(64, 64)));
        let config = pool_config(pool.clone(), Duration::ZERO);
        let shape = KvShape::from_config(&decoder.config, KvElem::F32);
        // The prompt alone is 5 tokens against a ceiling of 4, so no
        // output budget exists that would make it servable.
        let ceiling = ContextCeiling::new(Some(4), shape);

        let err = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            Some(&config),
            None,
            None,
            Some(&ceiling),
            |_, _| panic!("no token may be emitted by a refused request"),
        )
        .expect_err("a 5-token prompt must not be admitted under a 4-position ceiling");
        match &err {
            DecodeError::KvBudgetExceeded {
                binding,
                positions,
                positions_limit,
                detail,
                ..
            } => {
                assert_eq!(*binding, frink_models::Ceiling::ContextLength.code());
                assert_eq!(*positions, 5);
                assert_eq!(*positions_limit, 4);
                // Load-bearing wording: Claude Code and OpenClaw match
                // on this text to recognise a blown context window,
                // because the Anthropic wire carries no error code.
                assert_eq!(detail, "prompt is too long: 5 tokens > 4 maximum");
            }
            other => panic!("expected a context-length refusal, got {other:?}"),
        }
        assert_eq!(err.retry_after_secs(), None, "a 400, not a retryable 503");
        assert_eq!(
            pool.lock().unwrap().free_blocks(),
            64,
            "the refusal must land before any block is taken"
        );
        assert_eq!(ceiling.refused(), 1);
    }

    /// The other outcome, and the one this test used to get wrong: a
    /// prompt that FITS is served with `max_tokens` clamped to what
    /// remains, not refused.
    ///
    /// This test previously asserted the refusal. Refusing here turns a
    /// servable request into a 400 over a `max_tokens` the caller very
    /// likely never set -- which is exactly what a large default output
    /// budget would make happen on every long prompt.
    #[test]
    fn a_prompt_that_fits_is_served_with_its_budget_clamped_rather_than_refused() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let shape = KvShape::from_config(&decoder.config, KvElem::F32);
        // 2 prompt tokens under a 4-position ceiling leaves room for 2,
        // and the request asks for 5.
        let ceiling = ContextCeiling::new(Some(4), shape);

        let mut emitted = 0usize;
        let (finish, _rows, _ids, usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            None,
            Some(&ceiling),
            |_, _| emitted += 1,
        )
        .expect("a prompt that fits must be served");
        assert_eq!(usage.completion_tokens, 2, "clamped to the room left");
        assert_eq!(finish[0].0, FinishReason::Length);
        assert_eq!(ceiling.refused(), 0, "a clamp is not a refusal");
        assert!(emitted > 0);
    }

    /// A request that fits the ceiling is untouched by it: the same
    /// request that runs without a ceiling runs with one.
    ///
    /// Without this, a ceiling that refused everything would still pass
    /// the test above.
    #[test]
    fn a_request_inside_the_ceiling_is_admitted_unchanged() {
        let decoder = small_decoder();
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let shape = KvShape::from_config(&decoder.config, KvElem::F32);
        let ceiling = ContextCeiling::new(Some(7), shape);

        let mut with = String::new();
        let (finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            None,
            Some(&ceiling),
            |_, s| with.push_str(s),
        )
        .expect("7 positions fits a 7-position ceiling exactly");
        assert_eq!(finish[0].0, FinishReason::Length);

        let mut without = String::new();
        generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            None,
            None,
            None,
            None,
            |_, s| without.push_str(s),
        )
        .unwrap();
        assert_eq!(with, without, "an unbinding ceiling must change nothing");
        assert_eq!(ceiling.refused(), 0);
    }

    #[test]
    fn generate_with_a_queue_wait_succeeds_once_another_holder_releases_its_blocks() {
        let decoder = small_decoder(); // 2 layers, needs 2 blocks
        let prompt = String::from_utf8(vec![1u8, 2]).unwrap();
        let pool = Arc::new(Mutex::new(KvBlockPool::new(64, 2)));

        // Hold both blocks on another thread for a short while, then
        // release them -- simulating another in-flight request that's
        // about to finish.
        let holder_pool = pool.clone();
        let holder = std::thread::spawn(move || {
            let mut held = KvCache::with_pool(1, 1, holder_pool.clone(), 0).unwrap();
            held.push(&[0.0], &[0.0]).unwrap(); // crosses into needing the second block
            std::thread::sleep(Duration::from_millis(80));
            drop(held); // returns both blocks to the pool
        });
        // Give the holder a moment to actually acquire before we try.
        std::thread::sleep(Duration::from_millis(15));

        let config = pool_config(pool.clone(), Duration::from_millis(500));
        let (finish, _rows, _ids, _usage) = generate(
            &decoder,
            &ServerTokenizer::Byte,
            &StopTokens::default(),
            None,
            &prompt,
            &greedy_params(5),
            Some(&config),
            None,
            None,
            None,
            |_, _| {},
        )
        .unwrap();
        assert_eq!(
            finish[0].0,
            FinishReason::Length,
            "a sufficiently long queue_wait must let the request succeed once the holder releases"
        );
        holder.join().unwrap();
        assert_eq!(pool.lock().unwrap().free_blocks(), 2);
    }

    /// `ignore_eos` is what makes a serving benchmark's requests do the
    /// same amount of work as each other. Without it they finish at
    /// different lengths and the slowest percentile is whichever
    /// request happened to be asked for the most tokens -- a fact about
    /// the prompts, reported as a fact about the server.
    #[test]
    fn ignore_eos_runs_a_request_out_to_its_full_budget() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        // The model tries to end its turn on its third token.
        let script = [0usize, 1, 7, 2, 3, 4];
        let eos = StopTokens::from_eos(Some(7));

        let stops_early =
            run_scripted_with_stops(&script, render, &scripted_params(6), eos.clone());
        assert_eq!(stops_early.0, FinishReason::Stop);
        assert_eq!(stops_early.1.len(), 2, "the model ended its own turn");

        let runs_on = run_scripted_with_stops(
            &script,
            render,
            &GenerationParams {
                ignore_eos: true,
                ..scripted_params(6)
            },
            eos,
        );
        assert_eq!(runs_on.0, FinishReason::Length);
        assert_eq!(
            runs_on.1.len(),
            6,
            "exactly the budget, which is the whole point"
        );
    }

    /// `ignore_eos` suppresses the MODEL's set and only that. A caller
    /// asking to run past the model's opinion about length is not a
    /// caller withdrawing their own fence, and a benchmark that could
    /// not be stopped by its own sentinel would be a footgun rather
    /// than a knob.
    #[test]
    fn ignore_eos_does_not_withdraw_the_callers_own_stop() {
        let render = |id: usize| char::from(b'a' + id as u8).to_string();
        let script = [0usize, 1, 2, 3, 4, 5];

        let (finish, ids, _) = run_scripted_with_stops(
            &script,
            render,
            &GenerationParams {
                ignore_eos: true,
                stop_token_ids: vec![2],
                ..scripted_params(6)
            },
            StopTokens::from_eos(Some(7)),
        );
        assert_eq!(finish, FinishReason::Stop);
        assert_eq!(ids.len(), 2, "the caller's stop token still ends it");
    }
}