asupersync 0.3.0

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
//! Multi-worker 3-lane scheduler with work stealing.
//!
//! This scheduler coordinates multiple worker threads while maintaining
//! strict priority ordering: cancel > timed > ready.
//!
//! # Cancel-lane preemption with bounded fairness (bd-17uu)
//!
//! The cancel lane has strict preemption over timed and ready lanes, but a
//! fairness mechanism prevents starvation of lower-priority work.
//!
//! ## Invariant
//!
//! **Fairness bound**: If the ready or timed lane has pending work, that work
//! is dispatched after at most `cancel_streak_limit` consecutive cancel-lane
//! dispatches (or `2 * cancel_streak_limit` under `DrainObligations`/`DrainRegions`).
//!
//! ## Proof sketch (per-worker, single-threaded scheduling loop)
//!
//! 1. Each worker maintains a monotone counter `cancel_streak` that increments
//!    on every cancel dispatch and resets to 0 on any non-cancel dispatch (or
//!    when the cancel lane is empty).
//!
//! 2. In `next_task()`, the cancel lane is only consulted when
//!    `cancel_streak < cancel_streak_limit`. Once the limit is reached, the
//!    scheduler falls through to timed, ready, and steal.
//!
//! 3. If timed or ready work is pending when cancel_streak hits the limit,
//!    that work is dispatched next, resetting cancel_streak to 0. Cancel work
//!    resumes on the following call to `next_task()`.
//!
//! 4. If no timed/ready/steal work is available when the limit is hit, a
//!    fallback path allows one more cancel dispatch with cancel_streak reset
//!    to 1. This ensures cancel work is not blocked indefinitely when it is
//!    the only pending work.
//!
//! 5. On backoff/park (no work found), cancel_streak resets to 0. This
//!    prevents stale counters from deferring cancel work after an idle period.
//!
//! **Corollary**: Under sustained cancel injection, the ready lane observes a
//! dispatch slot at least every `cancel_streak_limit + 1` scheduling steps,
//! giving a worst-case ready-lane stall of O(cancel_streak_limit) dispatch
//! cycles per worker.
//!
//! ## Cross-worker note
//!
//! Fairness is enforced per-worker. Global fairness follows from each worker
//! independently bounding its cancel streak. Work stealing operates only on
//! the ready lane, so a worker whose ready lane is starved by cancel work
//! will not have its ready tasks stolen.

use crate::cancel::progress_certificate::{DrainPhase, ProgressCertificate};
use crate::obligation::lyapunov::{
    LyapunovGovernor, PotentialWeights, SchedulingSuggestion, StateSnapshot,
};
use crate::observability::spectral_health::{SpectralHealthMonitor, SpectralThresholds};
use crate::runtime::io_driver::IoDriverHandle;
use crate::runtime::scheduler::global_injector::GlobalInjector;
use crate::runtime::scheduler::local_queue::{self, LocalQueue};
use crate::runtime::scheduler::priority::Scheduler as PriorityScheduler;
use crate::runtime::scheduler::worker::Parker;
use crate::runtime::stored_task::AnyStoredTask;
use crate::runtime::{RuntimeState, TaskTable};
use crate::sync::ContendedMutex;
use crate::time::TimerDriverHandle;
use crate::tracing_compat::{error, trace};
use crate::types::{CxInner, TaskId, Time};
use crate::util::{CachePadded, DetHasher, DetRng};
use parking_lot::Mutex;
use parking_lot::RwLock;
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Weak};
use std::task::{Context, Poll, Waker};
use std::time::Duration;

/// Identifier for a scheduler worker.
pub type WorkerId = usize;

const DEFAULT_CANCEL_STREAK_LIMIT: usize = 16;
const DEFAULT_BROWSER_READY_HANDOFF_LIMIT: usize = 0;
const DEFAULT_STEAL_BATCH_SIZE: usize = 4;
const DEFAULT_ENABLE_PARKING: bool = true;
const LOCAL_SCHEDULER_BURST_BUDGET: usize = 2048;
const LOCAL_SCHEDULER_MIN_CAPACITY: usize = 128;
const LOCAL_SCHEDULER_MAX_CAPACITY: usize = 1024;
const ADAPTIVE_STREAK_ARMS: [usize; 5] = [4, 8, 16, 32, 64];
const ADAPTIVE_EXP3_GAMMA: f64 = 0.07;
const ADAPTIVE_EPROCESS_LAMBDA: f64 = 0.5;
// Keep a short spin/yield window for wakeup handoff while still reducing
// runaway idle burn on noisy wake paths.
const SPIN_LIMIT: u32 = 8;
const YIELD_LIMIT: u32 = 2;
const SHORT_WAIT_LE_5MS_NANOS: u64 = 5_000_000;

type LocalReadyQueue = Mutex<VecDeque<TaskId>>;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum IoPhaseOutcome {
    /// This worker made useful I/O progress (work may now be runnable).
    Progress,
    /// Another worker is currently the reactor leader.
    Follower,
    /// No I/O progress from this worker (leader quick miss or no I/O driver).
    NoProgress,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BackoffTimeoutDecision {
    ParkTimeout { nanos: u64 },
    DeadlineDue,
}

#[inline]
fn select_backoff_deadline(
    io_phase: IoPhaseOutcome,
    timer_deadline: Option<Time>,
    local_deadline: Option<Time>,
    global_deadline: Option<Time>,
) -> Option<Time> {
    if matches!(io_phase, IoPhaseOutcome::Follower) {
        // Followers should not wake on shared global/timer deadlines. The
        // leader handles those deadlines and will wake workers when work is
        // actually runnable. Followers still honor local deadlines.
        local_deadline
    } else {
        [timer_deadline, local_deadline, global_deadline]
            .into_iter()
            .flatten()
            .min()
    }
}

#[inline]
fn record_backoff_deadline_selection(
    metrics: &mut PreemptionMetrics,
    io_phase: IoPhaseOutcome,
    timer_deadline: Option<Time>,
    global_deadline: Option<Time>,
) {
    if matches!(io_phase, IoPhaseOutcome::Follower)
        && (timer_deadline.is_some() || global_deadline.is_some())
    {
        metrics.follower_shared_deadline_ignored += 1;
    }
}

#[inline]
fn record_backoff_timeout_park(
    metrics: &mut PreemptionMetrics,
    io_phase: IoPhaseOutcome,
    nanos: u64,
) {
    metrics.backoff_parks_total += 1;
    metrics.backoff_timeout_parks_total += 1;
    metrics.backoff_timeout_nanos_total = metrics.backoff_timeout_nanos_total.saturating_add(nanos);
    if nanos <= SHORT_WAIT_LE_5MS_NANOS {
        metrics.short_wait_le_5ms += 1;
    }
    if matches!(io_phase, IoPhaseOutcome::Follower) {
        metrics.follower_timeout_parks += 1;
    }
}

#[inline]
fn classify_backoff_timeout_decision(
    _io_phase: IoPhaseOutcome,
    next_deadline: Time,
    now: Time,
) -> BackoffTimeoutDecision {
    if next_deadline <= now {
        BackoffTimeoutDecision::DeadlineDue
    } else {
        let nanos = next_deadline.duration_since(now);
        // Always park even for sub-5ms timeouts. The previous optimisation
        // (SkipShortFollowerTimeout) would `break` the inner backoff loop,
        // but the outer scheduling loop restarted with backoff=0, causing
        // full SPIN_LIMIT+YIELD_LIMIT busy-loops without ever parking.
        // A sub-5ms futex park is far cheaper than that spin storm.
        BackoffTimeoutDecision::ParkTimeout { nanos }
    }
}

#[inline]
fn record_backoff_indefinite_park(metrics: &mut PreemptionMetrics, io_phase: IoPhaseOutcome) {
    metrics.backoff_parks_total += 1;
    metrics.backoff_indefinite_parks += 1;
    if matches!(io_phase, IoPhaseOutcome::Follower) {
        metrics.follower_indefinite_parks += 1;
    }
}

#[inline]
#[allow(clippy::cast_precision_loss)]
fn usize_to_f64(value: usize) -> f64 {
    value as f64
}

#[inline]
#[allow(clippy::cast_precision_loss)]
fn u64_to_f64(value: u64) -> f64 {
    value as f64
}

#[inline]
#[allow(clippy::cast_precision_loss)]
fn normalized_entropy(probs: &[f64]) -> f64 {
    if probs.len() <= 1 {
        return 0.0;
    }
    let mut entropy = 0.0_f64;
    for &p in probs {
        if p > f64::EPSILON {
            entropy = p.mul_add(-p.ln(), entropy);
        }
    }
    let max_entropy = (probs.len() as f64).ln();
    if max_entropy <= f64::EPSILON {
        0.0
    } else {
        (entropy / max_entropy).clamp(0.0, 1.0)
    }
}

/// Snapshot of scheduler-relevant state at an adaptive epoch boundary.
#[derive(Debug, Clone, Copy)]
struct AdaptiveEpochSnapshot {
    potential: f64,
    deadline_pressure: f64,
    base_limit_exceedances: u64,
    effective_limit_exceedances: u64,
    fallback_cancel_dispatches: u64,
}

impl AdaptiveEpochSnapshot {
    fn reward_against(self, end: Self, epoch_steps: u32) -> f64 {
        // Reward lives in [0, 1]. It mixes Lyapunov decrease with fairness and
        // deadline penalties so the online policy has a stable objective.
        let denom = self.potential.abs() + 1.0;
        let normalized_drop = ((self.potential - end.potential) / denom).clamp(-1.0, 1.0);
        let deadline_penalty = ((end.deadline_pressure - self.deadline_pressure).max(0.0)
            / (self.deadline_pressure.abs() + 1.0))
            .clamp(0.0, 1.0);
        let eps = f64::from(epoch_steps.max(1));
        let base_exceedances = u64_to_f64(
            end.base_limit_exceedances
                .saturating_sub(self.base_limit_exceedances),
        );
        let effective_exceedances = u64_to_f64(
            end.effective_limit_exceedances
                .saturating_sub(self.effective_limit_exceedances),
        );
        let fairness_penalty = 2.0f64.mul_add(effective_exceedances, base_exceedances) / eps;
        let fallback_penalty = u64_to_f64(
            end.fallback_cancel_dispatches
                .saturating_sub(self.fallback_cancel_dispatches),
        ) / eps;

        let reward = 0.5f64.mul_add(normalized_drop, 0.5);
        let reward = (-0.2f64).mul_add(deadline_penalty, reward);
        let reward = (-0.2f64).mul_add(fairness_penalty.clamp(0.0, 1.0), reward);
        let reward = (-0.1f64).mul_add(fallback_penalty.clamp(0.0, 1.0), reward);

        reward.clamp(0.0, 1.0)
    }
}

/// Deterministic EXP3 policy for adaptive cancel-streak limits.
#[derive(Debug, Clone)]
struct AdaptiveCancelStreakPolicy {
    arms: [usize; ADAPTIVE_STREAK_ARMS.len()],
    weights: [f64; ADAPTIVE_STREAK_ARMS.len()],
    probs: [f64; ADAPTIVE_STREAK_ARMS.len()],
    pulls: [u64; ADAPTIVE_STREAK_ARMS.len()],
    selected_arm: usize,
    epoch_steps: u32,
    steps_in_epoch: u32,
    epoch_count: u64,
    reward_ema: f64,
    e_process_log: f64,
    epoch_start: Option<AdaptiveEpochSnapshot>,
}

impl AdaptiveCancelStreakPolicy {
    fn new(epoch_steps: u32) -> Self {
        let arms = ADAPTIVE_STREAK_ARMS;
        let mut policy = Self {
            arms,
            weights: [1.0; ADAPTIVE_STREAK_ARMS.len()],
            probs: [0.0; ADAPTIVE_STREAK_ARMS.len()],
            pulls: [0; ADAPTIVE_STREAK_ARMS.len()],
            selected_arm: 2, // default arm == 16
            epoch_steps: epoch_steps.max(1),
            steps_in_epoch: 0,
            epoch_count: 0,
            reward_ema: 0.5,
            e_process_log: 0.0,
            epoch_start: None,
        };
        policy.refresh_probs();
        policy
    }

    fn set_epoch_steps(&mut self, epoch_steps: u32) {
        self.epoch_steps = epoch_steps.max(1);
    }

    fn current_limit(&self) -> usize {
        self.arms[self.selected_arm]
    }

    fn refresh_probs(&mut self) {
        let sum_w: f64 = self.weights.iter().sum();
        let k = usize_to_f64(self.weights.len());
        let uniform = 1.0 / k;
        if sum_w <= f64::EPSILON {
            self.probs.fill(uniform);
            return;
        }
        for i in 0..self.weights.len() {
            let exploit = self.weights[i] / sum_w;
            self.probs[i] =
                (1.0 - ADAPTIVE_EXP3_GAMMA).mul_add(exploit, ADAPTIVE_EXP3_GAMMA * uniform);
        }
        // Numeric cleanup: preserve exact simplex sum.
        let sum_p: f64 = self.probs.iter().sum();
        if sum_p > f64::EPSILON {
            for p in &mut self.probs {
                *p /= sum_p;
            }
        }
    }

    fn begin_epoch(&mut self, snapshot: AdaptiveEpochSnapshot) {
        self.epoch_start = Some(snapshot);
    }

    fn on_dispatch(&mut self) -> bool {
        self.steps_in_epoch = self.steps_in_epoch.saturating_add(1);
        self.steps_in_epoch >= self.epoch_steps
    }

    fn sample_arm_from_u64(&self, sample: u64) -> usize {
        #[allow(clippy::cast_precision_loss)]
        let u = (sample as f64) / ((u64::MAX as f64) + 1.0);
        let mut cdf = 0.0_f64;
        for (idx, p) in self.probs.iter().enumerate() {
            cdf += *p;
            if u <= cdf || idx == self.probs.len() - 1 {
                return idx;
            }
        }
        self.probs.len() - 1
    }

    fn complete_epoch(&mut self, end: AdaptiveEpochSnapshot, sample: u64) -> Option<f64> {
        let start = self.epoch_start?;
        let reward = start.reward_against(end, self.epoch_steps);

        let chosen = self.selected_arm;
        let p = self.probs[chosen].clamp(1e-9, 1.0);
        let k = usize_to_f64(self.weights.len());
        let reward_hat = reward / p;
        let exponent = (ADAPTIVE_EXP3_GAMMA * reward_hat / k).clamp(-20.0, 20.0);
        self.weights[chosen] *= exponent.exp();
        // Prevent weight overflow: cap at 1e30 to avoid inf/NaN in refresh_probs.
        // This preserves the relative ranking while keeping arithmetic stable.
        self.weights[chosen] = self.weights[chosen].clamp(1e-30, 1e30);

        self.e_process_log += ADAPTIVE_EPROCESS_LAMBDA
            .mul_add(reward - 0.5, -(ADAPTIVE_EPROCESS_LAMBDA.powi(2) / 8.0));
        self.reward_ema = 0.9f64.mul_add(self.reward_ema, 0.1 * reward);
        self.pulls[chosen] = self.pulls[chosen].saturating_add(1);
        self.epoch_count = self.epoch_count.saturating_add(1);
        self.steps_in_epoch = 0;
        self.refresh_probs();
        self.selected_arm = self.sample_arm_from_u64(sample);
        self.epoch_start = Some(end);
        Some(reward)
    }

    fn e_value(&self) -> f64 {
        self.e_process_log.clamp(-60.0, 60.0).exp()
    }
}

/// Coordination for waking workers.
#[derive(Debug)]
pub(crate) struct WorkerCoordinator {
    parkers: Vec<Parker>,
    next_wake: CachePadded<AtomicUsize>,
    /// Bitmask for power-of-two worker counts (replaces IDIV with AND).
    /// `None` when the count is zero or non-power-of-two.
    mask: Option<usize>,
    /// I/O driver handle for waking the reactor.
    io_driver: Option<IoDriverHandle>,
}

impl WorkerCoordinator {
    pub(crate) fn new(parkers: Vec<Parker>, io_driver: Option<IoDriverHandle>) -> Self {
        let count = parkers.len();
        let mask = if count > 0 && count.is_power_of_two() {
            Some(count - 1)
        } else {
            None
        };
        Self {
            parkers,
            next_wake: CachePadded::new(AtomicUsize::new(0)),
            mask,
            io_driver,
        }
    }

    #[inline]
    pub(crate) fn wake_one(&self) {
        let count = self.parkers.len();
        if count == 0 {
            return;
        }
        let idx = self.next_wake.fetch_add(1, Ordering::Relaxed);
        // Use bitmask (AND) when worker count is power-of-two to avoid IDIV.
        let slot = self.mask.map_or_else(|| idx % count, |mask| idx & mask);
        self.parkers[slot].unpark();
        if let Some(io) = &self.io_driver {
            let _ = io.wake();
        }
    }

    #[inline]
    pub(crate) fn wake_many(&self, num_wakes: usize) {
        let count = self.parkers.len();
        if count == 0 || num_wakes == 0 {
            return;
        }
        if num_wakes >= count {
            self.wake_all();
            return;
        }
        let start_idx = self.next_wake.fetch_add(num_wakes, Ordering::Relaxed);
        for i in 0..num_wakes {
            let idx = start_idx.wrapping_add(i);
            let slot = self.mask.map_or_else(|| idx % count, |mask| idx & mask);
            self.parkers[slot].unpark();
        }
        if let Some(io) = &self.io_driver {
            let _ = io.wake();
        }
    }

    #[inline]
    pub(crate) fn wake_worker(&self, worker_id: WorkerId) {
        if let Some(parker) = self.parkers.get(worker_id) {
            parker.unpark();
        }
        if let Some(io) = &self.io_driver {
            let _ = io.wake();
        }
    }

    #[inline]
    pub(crate) fn wake_all(&self) {
        for parker in &self.parkers {
            parker.unpark();
        }
        if let Some(io) = &self.io_driver {
            let _ = io.wake();
        }
    }
}

thread_local! {
    static CURRENT_LOCAL: RefCell<Option<Arc<Mutex<PriorityScheduler>>>> =
        const { RefCell::new(None) };
    /// Non-stealable queue for local (`!Send`) tasks.
    ///
    /// Local tasks must never be stolen across workers. This queue is only
    /// drained by the owner worker, never exposed to stealers.
    static CURRENT_LOCAL_READY: RefCell<Option<Arc<LocalReadyQueue>>> =
        const { RefCell::new(None) };
    /// Thread-local worker id for routing local tasks.
    static CURRENT_WORKER_ID: RefCell<Option<WorkerId>> = const { RefCell::new(None) };
}

/// Scoped setter for the thread-local scheduler pointer.
///
/// When active, [`ThreeLaneScheduler::spawn`] will schedule onto this local
/// scheduler instead of injecting into the global ready queue.
#[derive(Debug)]
pub(crate) struct ScopedLocalScheduler {
    prev: Option<Arc<Mutex<PriorityScheduler>>>,
}

impl ScopedLocalScheduler {
    pub(crate) fn new(local: Arc<Mutex<PriorityScheduler>>) -> Self {
        let prev = CURRENT_LOCAL.with(|cell| cell.replace(Some(local)));
        Self { prev }
    }
}

impl Drop for ScopedLocalScheduler {
    fn drop(&mut self) {
        let prev = self.prev.take();
        CURRENT_LOCAL.with(|cell| {
            *cell.borrow_mut() = prev;
        });
    }
}

/// Scoped setter for the thread-local worker id.
pub(crate) struct ScopedWorkerId {
    prev: Option<WorkerId>,
}

impl ScopedWorkerId {
    pub(crate) fn new(id: WorkerId) -> Self {
        let prev = CURRENT_WORKER_ID.with(|cell| cell.replace(Some(id)));
        Self { prev }
    }
}

impl Drop for ScopedWorkerId {
    fn drop(&mut self) {
        let prev = self.prev.take();
        let _ = CURRENT_WORKER_ID.try_with(|cell| {
            *cell.borrow_mut() = prev;
        });
    }
}

pub(crate) struct ScopedLocalReady {
    prev: Option<Arc<LocalReadyQueue>>,
}

impl ScopedLocalReady {
    pub(crate) fn new(queue: Arc<LocalReadyQueue>) -> Self {
        let prev = CURRENT_LOCAL_READY.with(|cell| cell.replace(Some(queue)));
        Self { prev }
    }
}

impl Drop for ScopedLocalReady {
    fn drop(&mut self) {
        CURRENT_LOCAL_READY.with(|cell| {
            *cell.borrow_mut() = self.prev.take();
        });
    }
}

/// Schedules a local (`!Send`) task on the current thread's non-stealable queue.
///
/// Returns `true` if a local-ready queue was available on this thread.
#[inline]
pub(crate) fn schedule_local_task(task: TaskId) -> bool {
    CURRENT_LOCAL_READY.with(|cell| {
        cell.borrow().as_ref().is_some_and(|queue| {
            queue.lock().push_back(task);
            true
        })
    })
}

#[inline]
pub(crate) fn current_worker_id() -> Option<WorkerId> {
    CURRENT_WORKER_ID.with(|cell| *cell.borrow())
}

fn has_trapped_scc(adjacency: &[Vec<usize>]) -> bool {
    struct Tarjan<'a> {
        adjacency: &'a [Vec<usize>],
        index: usize,
        stack: Vec<usize>,
        on_stack: Vec<bool>,
        indices: Vec<Option<usize>>,
        lowlink: Vec<usize>,
        trapped: bool,
    }

    impl Tarjan<'_> {
        fn strongconnect(&mut self, v: usize) {
            self.indices[v] = Some(self.index);
            self.lowlink[v] = self.index;
            self.index += 1;
            self.stack.push(v);
            self.on_stack[v] = true;

            for &w in &self.adjacency[v] {
                if self.indices[w].is_none() {
                    self.strongconnect(w);
                    self.lowlink[v] = self.lowlink[v].min(self.lowlink[w]);
                } else if self.on_stack[w] {
                    self.lowlink[v] = self.lowlink[v].min(self.indices[w].unwrap_or(usize::MAX));
                }
            }

            if self.lowlink[v] == self.indices[v].unwrap_or(usize::MAX) {
                let mut component = Vec::new();
                while let Some(w) = self.stack.pop() {
                    self.on_stack[w] = false;
                    component.push(w);
                    if w == v {
                        break;
                    }
                }

                let cyclic = component.len() > 1
                    || component
                        .first()
                        .is_some_and(|n| self.adjacency[*n].contains(n));
                if cyclic {
                    let component_set: BTreeSet<usize> = component.iter().copied().collect();
                    let mut has_egress = false;
                    for &u in &component {
                        if self.adjacency[u].iter().any(|v| !component_set.contains(v)) {
                            has_egress = true;
                            break;
                        }
                    }
                    if !has_egress {
                        self.trapped = true;
                    }
                }
            }
        }
    }

    let n = adjacency.len();
    let mut tarjan = Tarjan {
        adjacency,
        index: 0,
        stack: Vec::new(),
        on_stack: vec![false; n],
        indices: vec![None; n],
        lowlink: vec![0; n],
        trapped: false,
    };

    for v in 0..n {
        if tarjan.indices[v].is_none() {
            tarjan.strongconnect(v);
            if tarjan.trapped {
                return true;
            }
        }
    }

    false
}

fn wait_graph_signals_from_state(state: &RuntimeState) -> (usize, Vec<(usize, usize)>, bool) {
    let mut tasks: Vec<TaskId> = state
        .tasks_iter()
        .filter_map(|(_, task)| (!task.state.is_terminal()).then_some(task.id))
        .collect();
    tasks.sort();
    let index_by_task: BTreeMap<TaskId, usize> = tasks
        .iter()
        .enumerate()
        .map(|(idx, id)| (*id, idx))
        .collect();
    let mut undirected_edges: BTreeSet<(usize, usize)> = BTreeSet::new();
    let mut adjacency = vec![Vec::new(); tasks.len()];

    for (_, task) in state.tasks_iter() {
        if task.state.is_terminal() {
            continue;
        }
        let Some(&task_idx) = index_by_task.get(&task.id) else {
            continue;
        };
        for waiter in &task.waiters {
            if let Some(&waiter_idx) = index_by_task.get(waiter) {
                adjacency[waiter_idx].push(task_idx);
                if waiter_idx == task_idx {
                    continue;
                }
                undirected_edges.insert(if waiter_idx < task_idx {
                    (waiter_idx, task_idx)
                } else {
                    (task_idx, waiter_idx)
                });
            }
        }
    }

    for edges in &mut adjacency {
        edges.sort_unstable();
        edges.dedup();
    }
    let trapped_cycle = has_trapped_scc(&adjacency);

    (
        tasks.len(),
        undirected_edges.into_iter().collect(),
        trapped_cycle,
    )
}

#[inline]
pub(crate) fn schedule_on_current_local(task: TaskId, priority: u8) -> bool {
    // Fast path: O(1) push to LocalQueue VecDeque
    if LocalQueue::schedule_local(task) {
        return true;
    }
    // Slow path: O(log n) push to PriorityScheduler BinaryHeap
    CURRENT_LOCAL.with(|cell| {
        if let Some(local) = cell.borrow().as_ref() {
            local.lock().schedule(task, priority);
            return true;
        }
        false
    })
}

#[inline]
pub(crate) fn schedule_cancel_on_current_local(task: TaskId, priority: u8) -> bool {
    CURRENT_LOCAL.with(|cell| {
        let borrow = cell.borrow();
        let Some(local) = borrow.as_ref() else {
            return false;
        };
        // LOCK ORDER: local_ready (B) then local (A)
        // Matches order in inject_cancel: remove from ready queue first
        CURRENT_LOCAL_READY.with(|lr_cell| {
            if let Some(queue) = lr_cell.borrow().as_ref() {
                let mut local_ready_guard = queue.lock();
                if let Some(pos) = local_ready_guard.iter().position(|t| *t == task) {
                    local_ready_guard.remove(pos);
                }
                drop(local_ready_guard);
                local.lock().move_to_cancel_lane(task, priority);
            } else {
                local.lock().move_to_cancel_lane(task, priority);
            }
        });
        true
    })
}

/// A multi-worker scheduler with 3-lane priority support.
///
/// Each worker maintains a local `PriorityScheduler` for tasks spawned within
/// that worker. Cross-thread wakeups go through the shared `GlobalInjector`.
/// Workers strictly process cancel work before timed, and timed before ready.
///
/// All scheduling paths go through `wake_state.notify()` to provide centralized
/// deduplication, preventing the same task from being scheduled in multiple queues.
#[derive(Debug)]
pub struct ThreeLaneScheduler {
    /// Global injection queue for cross-thread wakeups.
    global: Arc<GlobalInjector>,
    /// Per-worker local schedulers for routing pinned local tasks.
    local_schedulers: Vec<Arc<Mutex<PriorityScheduler>>>,
    /// Per-worker non-stealable queues for local (`!Send`) tasks.
    local_ready: Vec<Arc<LocalReadyQueue>>,
    /// Per-worker parkers for targeted wakeups.
    parkers: Vec<Parker>,
    /// Worker handles for thread spawning.
    workers: Vec<ThreeLaneWorker>,
    /// Shutdown signal.
    shutdown: Arc<AtomicBool>,
    /// Coordination for waking workers.
    coordinator: Arc<WorkerCoordinator>,
    /// Browser-style ready dispatch burst limit before a host-turn handoff.
    ///
    /// `0` disables forced handoff behavior.
    browser_ready_handoff_limit: usize,
    /// Maximum number of ready tasks to steal in one batch.
    steal_batch_size: usize,
    /// Whether workers are allowed to park when idle.
    enable_parking: bool,
    /// Timer driver for processing timer wakeups.
    #[allow(dead_code)] // Timer integration in progress
    timer_driver: Option<TimerDriverHandle>,
    /// Shared runtime state for accessing task records and wake_state.
    state: Arc<ContendedMutex<RuntimeState>>,
    /// Optional sharded task table for hot-path task operations.
    ///
    /// When present, inject/spawn methods use this instead of the full
    /// RuntimeState lock for task record lookups (wake_state, is_local, etc.).
    task_table: Option<Arc<ContendedMutex<TaskTable>>>,
    /// Maximum global ready queue depth (0 = unbounded).
    global_queue_limit: usize,
}

impl ThreeLaneScheduler {
    #[inline]
    fn initial_local_scheduler_capacity(worker_count: usize) -> usize {
        let workers = worker_count.max(1);
        let per_worker = LOCAL_SCHEDULER_BURST_BUDGET.div_ceil(workers);
        per_worker.clamp(LOCAL_SCHEDULER_MIN_CAPACITY, LOCAL_SCHEDULER_MAX_CAPACITY)
    }

    /// Creates a new 3-lane scheduler with the given number of workers.
    pub fn new(worker_count: usize, state: &Arc<ContendedMutex<RuntimeState>>) -> Self {
        Self::new_with_options(worker_count, state, DEFAULT_CANCEL_STREAK_LIMIT, false, 32)
    }

    /// Creates a new 3-lane scheduler with a configurable cancel streak limit.
    pub fn new_with_cancel_limit(
        worker_count: usize,
        state: &Arc<ContendedMutex<RuntimeState>>,
        cancel_streak_limit: usize,
    ) -> Self {
        Self::new_with_options(worker_count, state, cancel_streak_limit, false, 32)
    }

    /// Creates a new 3-lane scheduler with full configuration options.
    ///
    /// When `enable_governor` is true, each worker maintains a
    /// [`LyapunovGovernor`] that periodically snapshots runtime state and
    /// produces scheduling suggestions. When false, behavior is identical
    /// to the ungoverned baseline.
    pub fn new_with_options(
        worker_count: usize,
        state: &Arc<ContendedMutex<RuntimeState>>,
        cancel_streak_limit: usize,
        enable_governor: bool,
        governor_interval: u32,
    ) -> Self {
        Self::new_with_options_and_task_table(
            worker_count,
            state,
            None,
            cancel_streak_limit,
            enable_governor,
            governor_interval,
        )
    }

    /// Creates a new 3-lane scheduler with full configuration and a sharded task table.
    ///
    /// When `task_table` is `Some`, hot-path operations (task record lookups,
    /// future storage/retrieval, LocalQueue push/pop) lock only the task table
    /// instead of the full RuntimeState. Cross-cutting operations
    /// (`task_completed`, `drain_ready_async_finalizers`) still use RuntimeState.
    #[allow(clippy::too_many_lines)]
    pub fn new_with_options_and_task_table(
        worker_count: usize,
        state: &Arc<ContendedMutex<RuntimeState>>,
        task_table: Option<Arc<ContendedMutex<TaskTable>>>,
        cancel_streak_limit: usize,
        enable_governor: bool,
        governor_interval: u32,
    ) -> Self {
        let cancel_streak_limit = cancel_streak_limit.max(1);
        let browser_ready_handoff_limit = DEFAULT_BROWSER_READY_HANDOFF_LIMIT;
        let governor_interval = governor_interval.max(1);
        let steal_batch_size = DEFAULT_STEAL_BATCH_SIZE;
        let enable_parking = DEFAULT_ENABLE_PARKING;
        let global = Arc::new(GlobalInjector::new());
        let shutdown = Arc::new(AtomicBool::new(false));
        let mut workers = Vec::with_capacity(worker_count);
        let mut parkers = Vec::with_capacity(worker_count);
        let mut local_schedulers: Vec<Arc<Mutex<PriorityScheduler>>> =
            Vec::with_capacity(worker_count);
        let mut local_ready: Vec<Arc<LocalReadyQueue>> = Vec::with_capacity(worker_count);
        let local_scheduler_capacity = Self::initial_local_scheduler_capacity(worker_count);

        // Get IO driver and timer driver from runtime state
        let (io_driver, timer_driver) = {
            let guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            (guard.io_driver_handle(), guard.timer_driver_handle())
        };

        // Create local schedulers first so we can share references for stealing
        for _ in 0..worker_count {
            local_schedulers.push(Arc::new(Mutex::new(PriorityScheduler::with_capacity(
                local_scheduler_capacity,
            ))));
        }
        // Create non-stealable local queues for !Send tasks
        for _ in 0..worker_count {
            local_ready.push(Arc::new(LocalReadyQueue::new(VecDeque::with_capacity(32))));
        }

        // Create parkers first
        for _ in 0..worker_count {
            parkers.push(Parker::new());
        }
        let coordinator = Arc::new(WorkerCoordinator::new(parkers.clone(), io_driver.clone()));

        // Create fast queues (O(1) VecDeque) for ready-lane fast path.
        // When a sharded TaskTable is available, back the queues directly
        // against it so push/pop/steal avoid the full RuntimeState lock.
        let fast_queues: Vec<LocalQueue> = (0..worker_count)
            .map(|_| {
                task_table.as_ref().map_or_else(
                    || LocalQueue::new(Arc::clone(state)),
                    |tt| LocalQueue::new_with_task_table(Arc::clone(tt)),
                )
            })
            .collect();

        // Create workers with references to all other workers' schedulers
        for id in 0..worker_count {
            let parker = parkers[id].clone();

            // Stealers: all other workers' local schedulers (excluding self)
            let stealers: Vec<_> = local_schedulers
                .iter()
                .enumerate()
                .filter(|(i, _)| *i != id)
                .map(|(_, sched)| Arc::clone(sched))
                .collect();

            // Fast stealers: O(1) steal from other workers' LocalQueues
            let fast_stealers: Vec<_> = fast_queues
                .iter()
                .enumerate()
                .filter(|(i, _)| *i != id)
                .map(|(_, q)| q.stealer())
                .collect();

            workers.push(ThreeLaneWorker {
                id,
                local: Arc::clone(&local_schedulers[id]),
                stealers,
                fast_queue: fast_queues[id].clone(),
                fast_stealers,
                local_ready: Arc::clone(&local_ready[id]),
                all_local_ready: local_ready.clone(),
                global: Arc::clone(&global),
                state: Arc::clone(state),
                task_table: task_table.clone(),
                parker,
                coordinator: Arc::clone(&coordinator),
                rng: DetRng::new(id as u64),
                shutdown: Arc::clone(&shutdown),
                io_driver: io_driver.clone(),
                timer_driver: timer_driver.clone(),
                steal_buffer: Vec::with_capacity(steal_batch_size.max(1)),
                steal_batch_size,
                enable_parking,
                cancel_streak: 0,
                ready_dispatch_streak: 0,
                browser_ready_handoff_limit,
                cancel_streak_limit,
                governor: if enable_governor {
                    Some(LyapunovGovernor::with_defaults())
                } else {
                    None
                },
                cached_suggestion: SchedulingSuggestion::NoPreference,
                steps_since_snapshot: 0,
                governor_interval,
                preemption_metrics: PreemptionMetrics {
                    adaptive_current_limit: cancel_streak_limit,
                    adaptive_e_value: 1.0,
                    ..PreemptionMetrics::default()
                },
                evidence_sink: None,
                decision_contract: if enable_governor {
                    Some(super::decision_contract::SchedulerDecisionContract::new())
                } else {
                    None
                },
                decision_posterior: if enable_governor {
                    Some(franken_decision::Posterior::uniform(
                        super::decision_contract::state::COUNT,
                    ))
                } else {
                    None
                },
                adaptive_cancel_policy: None,
                spectral_monitor: if enable_governor {
                    Some(SpectralHealthMonitor::new(SpectralThresholds::default()))
                } else {
                    None
                },
                drain_certificate: if enable_governor {
                    Some(ProgressCertificate::with_defaults())
                } else {
                    None
                },
                decision_sequence: 0,
                fairness_monitor: Mutex::new(FairnessMonitor::with_defaults()),
                invariant_monitor: Mutex::new(
                    super::invariant_monitor::SchedulerInvariantMonitor::with_defaults(),
                ),
            });
        }

        Self {
            global,
            local_schedulers,
            local_ready,
            parkers,
            workers,
            shutdown,
            coordinator,
            timer_driver,
            state: Arc::clone(state),
            task_table,
            browser_ready_handoff_limit,
            steal_batch_size,
            enable_parking,
            global_queue_limit: 0,
        }
    }

    /// Sets the maximum number of ready tasks to steal in one batch.
    ///
    /// Values less than 1 are clamped to 1 to preserve progress guarantees.
    pub fn set_steal_batch_size(&mut self, size: usize) {
        let size = size.max(1);
        self.steal_batch_size = size;
        for worker in &mut self.workers {
            worker.steal_batch_size = size;
            if worker.steal_buffer.capacity() < size {
                worker
                    .steal_buffer
                    .reserve(size - worker.steal_buffer.capacity());
            }
        }
    }

    /// Enables or disables worker parking when idle.
    pub fn set_enable_parking(&mut self, enable: bool) {
        self.enable_parking = enable;
        for worker in &mut self.workers {
            worker.enable_parking = enable;
        }
    }

    /// Sets the browser-style ready dispatch burst handoff limit.
    ///
    /// When non-zero, workers force a one-shot handoff after `limit`
    /// consecutive ready-lane dispatches. This is intended for browser
    /// event-loop adapters that need bounded host-turn monopolization.
    pub fn set_browser_ready_handoff_limit(&mut self, limit: usize) {
        self.browser_ready_handoff_limit = limit;
        for worker in &mut self.workers {
            worker.browser_ready_handoff_limit = limit;
            if limit == 0 {
                worker.ready_dispatch_streak = 0;
            }
        }
    }

    /// Enables/disables adaptive cancel-streak selection for all workers.
    ///
    /// When enabled, each worker uses a deterministic EXP3 policy over fixed
    /// candidate streak limits and updates the arm at epoch boundaries.
    pub fn set_adaptive_cancel_streak(&mut self, enable: bool, epoch_steps: u32) {
        let epoch_steps = epoch_steps.max(1);
        for worker in &mut self.workers {
            if enable {
                if let Some(policy) = worker.adaptive_cancel_policy.as_mut() {
                    policy.set_epoch_steps(epoch_steps);
                } else {
                    worker.adaptive_cancel_policy =
                        Some(AdaptiveCancelStreakPolicy::new(epoch_steps));
                }
                if let Some(policy) = worker.adaptive_cancel_policy.as_ref() {
                    worker.preemption_metrics.adaptive_current_limit = policy.current_limit();
                    worker.preemption_metrics.adaptive_reward_ema = policy.reward_ema;
                    worker.preemption_metrics.adaptive_e_value = policy.e_value();
                }
            } else {
                worker.adaptive_cancel_policy = None;
                worker.preemption_metrics.adaptive_current_limit = worker.cancel_streak_limit;
                worker.preemption_metrics.adaptive_reward_ema = 0.0;
                worker.preemption_metrics.adaptive_e_value = 1.0;
            }
        }
    }

    /// Sets the global ready queue depth limit (0 = unbounded).
    ///
    /// When the limit is non-zero and the global ready queue reaches this
    /// depth, new injections emit a trace warning. The task is still
    /// scheduled (dropping it would violate structured concurrency) but the
    /// warning signals backpressure to the caller.
    pub fn set_global_queue_limit(&mut self, limit: usize) {
        self.global_queue_limit = limit;
    }

    /// Returns a reference to the global injector.
    #[must_use]
    pub fn global_injector(&self) -> Arc<GlobalInjector> {
        self.global.clone()
    }

    /// Read-only task table access for inject/spawn methods.
    ///
    /// Uses the sharded task table when available, otherwise falls back to
    /// RuntimeState's embedded table.
    #[inline]
    fn with_task_table_ref<R, F: FnOnce(&TaskTable) -> R>(&self, f: F) -> R {
        if let Some(tt) = &self.task_table {
            let guard = tt.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
            f(&guard)
        } else {
            let state = self
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            f(&state.tasks)
        }
    }

    /// Injects a task into the cancel lane for cross-thread wakeup.
    ///
    /// Uses `wake_state.notify()` for centralized deduplication.
    /// If the task is already scheduled, this is a no-op.
    /// If the task record doesn't exist (e.g., in tests), allows injection.
    pub fn inject_cancel(&self, task: TaskId, priority: u8) {
        let (is_local, pinned_worker) = self.with_task_table_ref(|tt| {
            tt.task(task).map_or((false, None), |record| {
                if record.is_local() {
                    record.wake_state.notify();
                }
                (record.is_local(), record.pinned_worker())
            })
        });

        if is_local {
            if let Some(worker_id) = pinned_worker {
                if let Some(local) = self.local_schedulers.get(worker_id) {
                    if let Some(local_ready) = self.local_ready.get(worker_id) {
                        let mut local_ready_guard = local_ready.lock();
                        if let Some(pos) = local_ready_guard.iter().position(|t| *t == task) {
                            local_ready_guard.remove(pos);
                        }
                        drop(local_ready_guard);
                        local.lock().move_to_cancel_lane(task, priority);
                    } else {
                        local.lock().move_to_cancel_lane(task, priority);
                    }
                    if let Some(parker) = self.parkers.get(worker_id) {
                        parker.unpark();
                    }
                    return;
                }
            }
            if schedule_cancel_on_current_local(task, priority) {
                return;
            }
            // SAFETY: Local (!Send) tasks must only be polled on their owner
            // worker. If we can't route to the correct worker, skipping cancel
            // injection may cause a hang but avoids UB from wrong-thread polling.
            debug_assert!(
                false,
                "Attempted to inject_cancel local task {task:?} without owner worker"
            );
            error!(
                ?task,
                "inject_cancel: cannot route local task to owner worker, cancel skipped"
            );
            return;
        }

        // Cancel is the highest-priority lane.  Always inject so that
        // cancellation preempts ready/timed work even if the task is already
        // scheduled in another lane.  Deduplication happens at poll time
        // (finish_poll routes to cancel lane when a cancel is pending).
        self.global.inject_cancel(task, priority);
        self.wake_one();
    }

    /// Injects a task into the timed lane for cross-thread wakeup.
    ///
    /// Uses `wake_state.notify()` for centralized deduplication.
    /// If the task is already scheduled, this is a no-op.
    /// If the task record doesn't exist (e.g., in tests), allows injection.
    pub fn inject_timed(&self, task: TaskId, deadline: Time) {
        let should_schedule = self.with_task_table_ref(|tt| {
            tt.task(task)
                .is_none_or(|record| record.wake_state.notify())
        });
        if should_schedule {
            self.global.inject_timed(task, deadline);
            self.wake_one();
        }
    }

    /// Injects a task into the ready lane with queue limit checks.
    #[inline]
    fn inject_global_ready_checked(&self, task: TaskId, priority: u8) {
        if self.global_queue_limit > 0 && self.global.ready_count() >= self.global_queue_limit {
            crate::tracing_compat::warn!(
                ?task,
                priority,
                limit = self.global_queue_limit,
                current = self.global.ready_count(),
                "inject_ready: global ready queue at capacity, scheduling anyway"
            );
        }
        self.global.inject_ready(task, priority);
        self.wake_one();
    }

    /// Injects a task into the ready lane for cross-thread wakeup.
    ///
    /// Uses `wake_state.notify()` for centralized deduplication.
    /// If the task is already scheduled, this is a no-op.
    /// If the task record doesn't exist (e.g., in tests), allows injection.
    ///
    /// # Panics
    ///
    /// Panics if the task is a local (`!Send`) task. Local tasks must be
    /// scheduled via their `Waker` (which knows the owner) or `spawn` on the
    /// owner thread. Injecting them globally would allow them to be stolen
    /// by the wrong worker, causing data loss.
    pub fn inject_ready(&self, task: TaskId, priority: u8) {
        let (should_schedule, is_local) = self.with_task_table_ref(|tt| {
            tt.task(task).map_or((true, false), |record| {
                (record.wake_state.notify(), record.is_local())
            })
        });

        // SAFETY: Local (!Send) tasks must only be polled on their owner worker.
        // Injecting globally would allow wrong-thread polling = UB.
        debug_assert!(
            !is_local,
            "Attempted to globally inject local task {task:?}. Local tasks must be scheduled on their owner thread."
        );
        if is_local {
            error!(
                ?task,
                "inject_ready: refusing to globally inject local task, scheduling skipped"
            );
            return;
        }

        if should_schedule {
            self.inject_global_ready_checked(task, priority);
            trace!(
                ?task,
                priority, "inject_ready: task injected into global ready queue"
            );
        } else {
            trace!(
                ?task,
                priority, "inject_ready: task NOT scheduled (should_schedule=false)"
            );
        }
    }

    /// Spawns a task (shorthand for inject_ready).
    ///
    /// Fast path: when called on a worker thread, pushes to the worker's
    /// `LocalQueue` (O(1) VecDeque) instead of the global injector
    /// or the PriorityScheduler heap.
    ///
    /// # Local Tasks
    ///
    /// If the task is local (`!Send`), it attempts to schedule it on the current
    /// thread if it matches the owner. If called from a non-owner thread, it
    /// attempts to route the task to the pinned worker's `local_ready` queue.
    pub fn spawn(&self, task: TaskId, priority: u8) {
        // Dedup: check wake_state before scheduling anywhere.
        let (should_schedule, is_local, pinned_worker) = self.with_task_table_ref(|tt| {
            tt.task(task).map_or((true, false, None), |record| {
                (
                    record.wake_state.notify(),
                    record.is_local(),
                    record.pinned_worker(),
                )
            })
        });

        if !should_schedule {
            return;
        }

        if is_local {
            let current_worker = current_worker_id();
            let is_pinned_here = match (pinned_worker, current_worker) {
                (Some(pw), Some(cw)) => pw == cw,
                (None, Some(_)) => true,
                _ => false,
            };

            // 1. Try scheduling on current thread (fastest, no locks if TLS setup)
            // ONLY if this thread is the owner.
            if is_pinned_here && schedule_local_task(task) {
                return;
            }

            // 2. Try routing to pinned worker (cross-thread spawn)
            if let Some(worker_id) = pinned_worker {
                if let Some(queue) = self.local_ready.get(worker_id) {
                    queue.lock().push_back(task);
                    self.coordinator.wake_worker(worker_id);
                    return;
                }
            }

            // 3. Failure: Cannot route local task
            debug_assert!(
                false,
                "Attempted to spawn local task {task:?} from non-owner thread or outside worker context"
            );
            error!(
                ?task,
                "spawn: local task cannot be scheduled from non-owner thread, spawn skipped"
            );
            return;
        }

        // Fast path 1 & 2: Try local queue (O(1)) then local scheduler (O(log n)) via TLS.
        if schedule_on_current_local(task, priority) {
            return;
        }

        // Slow path: global injector (off worker thread).
        self.inject_global_ready_checked(task, priority);
    }

    /// Wakes a task by injecting it into the ready lane.
    ///
    /// Fast path: when called on a worker thread, pushes to the worker's
    /// `LocalQueue` (O(1)) or `PriorityScheduler` instead of the global
    /// injector. For cancel wakeups, use `inject_cancel` instead.
    ///
    /// # Local Tasks
    ///
    /// If the task is local (`!Send`), it attempts to schedule it on the current
    /// thread if it matches the owner. If called from a non-owner thread, it
    /// attempts to route the task to the pinned worker's `local_ready` queue.
    pub fn wake(&self, task: TaskId, priority: u8) {
        // Dedup check.
        let (should_schedule, is_local, pinned_worker) = self.with_task_table_ref(|tt| {
            tt.task(task).map_or((true, false, None), |record| {
                (
                    record.wake_state.notify(),
                    record.is_local(),
                    record.pinned_worker(),
                )
            })
        });

        if !should_schedule {
            return;
        }

        if is_local {
            let current_worker = current_worker_id();
            let is_pinned_here = match (pinned_worker, current_worker) {
                (Some(pw), Some(cw)) => pw == cw,
                (None, Some(_)) => true,
                _ => false,
            };

            // 1. Try scheduling on current thread (fastest, no locks if TLS setup)
            // ONLY if this thread is the owner.
            if is_pinned_here && schedule_local_task(task) {
                return;
            }

            // 2. Try routing to pinned worker (cross-thread wake)
            if let Some(worker_id) = pinned_worker {
                if let Some(queue) = self.local_ready.get(worker_id) {
                    queue.lock().push_back(task);
                    self.coordinator.wake_worker(worker_id);
                    return;
                }
            }

            // 3. Failure: Cannot route local task
            debug_assert!(
                false,
                "Attempted to wake local task {task:?} via scheduler from non-owner thread. Use Waker instead."
            );
            error!(
                ?task,
                "wake: local task cannot be woken from non-owner thread, wake skipped"
            );
            return;
        }

        // Fast path 1 & 2: Try local queue (O(1)) then local scheduler (O(log n)) via TLS.
        if schedule_on_current_local(task, priority) {
            return;
        }

        // Slow path: global injector (off worker thread).
        self.inject_global_ready_checked(task, priority);
    }

    /// Wakes one idle worker.
    #[inline]
    fn wake_one(&self) {
        self.coordinator.wake_one();
    }

    /// Wakes all idle workers.
    pub fn wake_all(&self) {
        self.coordinator.wake_all();
    }

    /// Extract workers to run them in threads.
    pub fn take_workers(&mut self) -> Vec<ThreeLaneWorker> {
        std::mem::take(&mut self.workers)
    }

    /// Signals all workers to shutdown.
    pub fn shutdown(&self) {
        self.shutdown.store(true, Ordering::Release);
        self.wake_all();
    }

    /// Returns true if shutdown has been signaled.
    #[must_use]
    pub fn is_shutdown(&self) -> bool {
        self.shutdown.load(Ordering::Acquire)
    }
}

/// A worker thread for the 3-lane scheduler.
#[derive(Debug)]
pub struct ThreeLaneWorker {
    /// Unique worker ID.
    pub id: WorkerId,
    /// Local 3-lane scheduler for this worker.
    pub local: Arc<Mutex<PriorityScheduler>>,
    /// References to other workers' local schedulers for stealing.
    pub stealers: Vec<Arc<Mutex<PriorityScheduler>>>,
    /// O(1) local queue for ready tasks (work-stealing fast path).
    ///
    /// Ready tasks spawned/woken on the worker thread are pushed here
    /// (VecDeque, O(1)) instead of the PriorityScheduler (BinaryHeap,
    /// O(log n)). Stealers use FIFO ordering for cache-friendliness.
    pub fast_queue: LocalQueue,
    /// Stealers for other workers' fast queues (O(1) steal).
    fast_stealers: Vec<local_queue::Stealer>,
    /// Non-stealable queue for local (`!Send`) tasks.
    ///
    /// Local tasks are pinned to their owner worker and must never be stolen.
    /// This queue is only drained by the owner worker during `try_ready_work()`.
    local_ready: Arc<LocalReadyQueue>,
    /// References to all workers' non-stealable local queues.
    ///
    /// Used to route local waiters to their owner worker's queue when a task
    /// completes and needs to wake a pinned waiter on a different worker.
    all_local_ready: Vec<Arc<LocalReadyQueue>>,
    /// Global injection queue.
    pub global: Arc<GlobalInjector>,
    /// Shared runtime state.
    pub state: Arc<ContendedMutex<RuntimeState>>,
    /// Optional sharded task table for hot-path task operations.
    ///
    /// When present, `execute()` and scheduling helpers lock this instead
    /// of the full RuntimeState for task record access, future storage,
    /// and wake_state operations.
    pub task_table: Option<Arc<ContendedMutex<TaskTable>>>,
    /// Parking mechanism for idle workers.
    pub parker: Parker,
    /// Coordination for waking other workers.
    pub(crate) coordinator: Arc<WorkerCoordinator>,
    /// Deterministic RNG for stealing decisions.
    pub rng: DetRng,
    /// Shutdown signal.
    pub shutdown: Arc<AtomicBool>,
    /// I/O driver handle for polling the reactor (optional).
    pub io_driver: Option<IoDriverHandle>,
    /// Timer driver for processing timer wakeups (optional).
    pub timer_driver: Option<TimerDriverHandle>,
    /// Scratch buffer for stolen tasks (avoid per-steal allocations).
    steal_buffer: Vec<(TaskId, u8)>,
    /// Maximum number of ready tasks to steal in one batch.
    steal_batch_size: usize,
    /// Whether this worker is allowed to park when idle.
    enable_parking: bool,
    /// Number of consecutive cancel-lane dispatches.
    cancel_streak: usize,
    /// Number of consecutive ready-lane dispatches.
    ready_dispatch_streak: usize,
    /// Browser-style ready dispatch burst limit before yielding host turn.
    ///
    /// `0` disables host-turn handoff gating.
    browser_ready_handoff_limit: usize,
    /// Maximum consecutive cancel-lane dispatches before yielding.
    ///
    /// Fairness guarantee: if timed or ready work is pending, it will be
    /// dispatched after at most `cancel_streak_limit` cancel dispatches.
    cancel_streak_limit: usize,
    /// Lyapunov governor for policy-controlled scheduling suggestions.
    ///
    /// When `Some`, the worker periodically snapshots runtime state and
    /// consults the governor for lane-ordering hints.
    governor: Option<LyapunovGovernor>,
    /// Cached scheduling suggestion from the governor.
    cached_suggestion: SchedulingSuggestion,
    /// Number of scheduling steps since last governor snapshot.
    steps_since_snapshot: u32,
    /// Steps between governor snapshots.
    governor_interval: u32,
    /// Preemption fairness metrics (cancel-lane preemption tracking).
    preemption_metrics: PreemptionMetrics,
    /// Optional evidence sink for scheduler decision tracing (bd-1e2if.3).
    evidence_sink: Option<Arc<dyn crate::evidence_sink::EvidenceSink>>,
    /// Decision contract for principled scheduler action selection (bd-1e2if.6).
    decision_contract: Option<super::decision_contract::SchedulerDecisionContract>,
    /// Posterior maintained across governor invocations (bd-1e2if.6).
    decision_posterior: Option<franken_decision::Posterior>,
    /// Optional adaptive policy for selecting the cancel streak limit.
    adaptive_cancel_policy: Option<AdaptiveCancelStreakPolicy>,
    /// Spectral monitor for topology-aware early warning and overrides.
    spectral_monitor: Option<SpectralHealthMonitor>,
    /// Martingale-based drain progress certificate.
    ///
    /// When the governor is active, the certificate tracks Lyapunov potential
    /// descent during drain phases and provides statistical convergence
    /// verdicts (Azuma–Hoeffding + Freedman bounds) with phase classification
    /// (Warmup / RapidDrain / SlowTail / Stalled / Quiescent).
    drain_certificate: Option<ProgressCertificate>,
    /// Monotone sequence for deterministic decision IDs and timestamps.
    decision_sequence: u64,
    /// Enhanced fairness monitoring for starvation and priority inversion detection.
    fairness_monitor: Mutex<FairnessMonitor>,
    /// Scheduler invariant monitor for comprehensive correctness verification.
    invariant_monitor: Mutex<super::invariant_monitor::SchedulerInvariantMonitor>,
}

/// Per-worker metrics tracking cancel-lane preemption and fairness.
#[derive(Debug, Clone, Default)]
pub struct PreemptionMetrics {
    /// Total cancel-lane dispatches.
    pub cancel_dispatches: u64,
    /// Total timed-lane dispatches.
    pub timed_dispatches: u64,
    /// Total ready-lane dispatches.
    pub ready_dispatches: u64,
    /// Browser host-turn handoffs forced by ready-burst fairness controls.
    pub browser_ready_handoff_yields: u64,
    /// Times the cancel streak hit the fairness limit.
    pub fairness_yields: u64,
    /// Worst observed cancel streak immediately before a ready dispatch.
    ///
    /// This records the largest number of consecutive cancel dispatches that a
    /// ready task actually waited through before being selected.
    pub max_ready_dispatch_stall: usize,
    /// Worst observed cancel streak immediately before a timed dispatch.
    ///
    /// This records the largest number of consecutive cancel dispatches that a
    /// due timed task actually waited through before being selected.
    pub max_timed_dispatch_stall: usize,
    /// Number of times a lower-priority global ready dispatch bypassed a
    /// higher-priority local ready task.
    pub ready_priority_inversions: u64,
    /// Largest observed priority gap for a ready-lane inversion.
    pub max_ready_priority_inversion_gap: u8,
    /// Maximum cancel streak observed.
    pub max_cancel_streak: usize,
    /// Fallback cancel dispatches (after limit, no other work available).
    pub fallback_cancel_dispatches: u64,
    /// Number of cancel dispatches where streak exceeded the base limit `L`.
    ///
    /// This can be non-zero when boosted fairness mode is active
    /// (`DrainObligations`/`DrainRegions`), where the effective limit becomes `2L`.
    pub base_limit_exceedances: u64,
    /// Number of cancel dispatches where streak exceeded the effective limit.
    ///
    /// This should remain zero for a healthy scheduler run.
    pub effective_limit_exceedances: u64,
    /// Maximum effective limit observed during dispatch.
    ///
    /// In unboosted mode this is `L`; with drain boosts this can be `2L`.
    pub max_effective_limit_observed: usize,
    /// Number of completed adaptive policy epochs.
    pub adaptive_epochs: u64,
    /// Most recently selected adaptive base cancel streak limit.
    pub adaptive_current_limit: usize,
    /// Exponential moving average of adaptive rewards.
    pub adaptive_reward_ema: f64,
    /// Anytime-valid e-process value for the adaptive reward stream.
    pub adaptive_e_value: f64,
    /// Total backoff parks performed.
    pub backoff_parks_total: u64,
    /// Backoff parks that armed a timeout.
    pub backoff_timeout_parks_total: u64,
    /// Backoff parks with indefinite sleep (no deadline armed).
    pub backoff_indefinite_parks: u64,
    /// Sum of timeout durations armed for backoff parks (nanoseconds).
    pub backoff_timeout_nanos_total: u64,
    /// Timeout parks with short waits (<= 5ms).
    pub short_wait_le_5ms: u64,
    /// Follower loops where shared timer/global deadlines were ignored.
    pub follower_shared_deadline_ignored: u64,
    /// Timeout parks performed while in follower I/O phase.
    pub follower_timeout_parks: u64,
    /// Indefinite parks performed while in follower I/O phase.
    pub follower_indefinite_parks: u64,
    /// Follower short-timeout (<= 5ms) parks intentionally skipped to avoid
    /// wake-timeout futex churn.
    pub follower_short_wait_skip_le_5ms: u64,
}

impl PreemptionMetrics {
    const RATIO_BPS_SCALE: u64 = 10_000;

    #[inline]
    fn ratio_bps(numerator: u64, denominator: u64) -> u16 {
        if denominator == 0 {
            return 0;
        }
        let raw = numerator
            .saturating_mul(Self::RATIO_BPS_SCALE)
            .saturating_div(denominator)
            .min(Self::RATIO_BPS_SCALE);
        raw as u16
    }

    /// Returns the average timeout-park duration in nanoseconds.
    ///
    /// Returns `0` when no timeout parks have been recorded.
    #[must_use]
    pub fn avg_timeout_park_nanos(&self) -> u64 {
        if self.backoff_timeout_parks_total == 0 {
            return 0;
        }
        self.backoff_timeout_nanos_total
            .saturating_div(self.backoff_timeout_parks_total)
    }

    /// Returns the proportion of timeout parks that were short waits
    /// (<= 5ms) in basis points.
    ///
    /// `10_000` means 100%.
    #[must_use]
    pub fn short_wait_ratio_bps(&self) -> u16 {
        Self::ratio_bps(self.short_wait_le_5ms, self.backoff_timeout_parks_total)
    }

    /// Returns the follower short-wait avoidance rate in basis points.
    ///
    /// This compares follower short-timeout skips vs follower short-timeout
    /// opportunities (skip + timeout park).
    #[must_use]
    pub fn follower_short_wait_avoidance_bps(&self) -> u16 {
        let opportunities = self
            .follower_short_wait_skip_le_5ms
            .saturating_add(self.follower_timeout_parks);
        Self::ratio_bps(self.follower_short_wait_skip_le_5ms, opportunities)
    }

    /// Returns the worst observed cancel-induced stall across ready/timed lanes.
    #[must_use]
    pub fn max_non_cancel_dispatch_stall(&self) -> usize {
        self.max_ready_dispatch_stall
            .max(self.max_timed_dispatch_stall)
    }
}

/// Configuration for fairness monitoring and starvation detection.
#[derive(Debug, Clone)]
pub struct FairnessConfig {
    /// Maximum time a task can wait before being considered starved (nanoseconds).
    pub starvation_threshold_ns: u64,
    /// Size of the moving window for temporal pattern analysis.
    pub analysis_window_size: usize,
    /// Threshold for detecting priority inversion patterns.
    pub priority_inversion_threshold: u8,
    /// Maximum number of tasks to track for starvation monitoring.
    pub max_tracked_tasks: usize,
    /// Enable detailed per-task tracking (impacts performance).
    pub enable_per_task_tracking: bool,
}

impl Default for FairnessConfig {
    fn default() -> Self {
        Self {
            starvation_threshold_ns: 100_000_000, // 100ms
            analysis_window_size: 1000,
            priority_inversion_threshold: 5,
            max_tracked_tasks: 10_000,
            enable_per_task_tracking: true,
        }
    }
}

/// Per-task tracking information for starvation detection.
#[derive(Debug, Clone)]
struct TaskStarvationInfo {
    /// Task ID being tracked.
    task_id: TaskId,
    /// Priority of the task.
    priority: u8,
    /// Timestamp when task was first enqueued (nanoseconds).
    enqueue_time_ns: u64,
    /// Number of times this task was skipped for higher-priority work.
    skip_count: u32,
    /// Last time this task was skipped (nanoseconds).
    last_skip_time_ns: u64,
    /// Current queue lane (Cancel=0, Timed=1, Ready=2).
    current_lane: u8,
    /// Total time spent waiting across all queue entries.
    total_wait_time_ns: u64,
}

impl TaskStarvationInfo {
    fn new(task_id: TaskId, priority: u8, current_time_ns: u64, lane: u8) -> Self {
        Self {
            task_id,
            priority,
            enqueue_time_ns: current_time_ns,
            skip_count: 0,
            last_skip_time_ns: 0,
            current_lane: lane,
            total_wait_time_ns: 0,
        }
    }

    fn record_skip(&mut self, current_time_ns: u64) {
        self.skip_count = self.skip_count.saturating_add(1);
        self.last_skip_time_ns = current_time_ns;
        self.total_wait_time_ns = self.current_wait_time_ns(current_time_ns);
    }

    fn current_wait_time_ns(&self, current_time_ns: u64) -> u64 {
        current_time_ns.saturating_sub(self.enqueue_time_ns)
    }

    fn is_starved(&self, threshold_ns: u64, current_time_ns: u64) -> bool {
        self.current_wait_time_ns(current_time_ns) >= threshold_ns
    }
}

/// Priority inversion detection entry.
#[derive(Debug, Clone)]
struct PriorityInversionEvent {
    /// High-priority task that was blocked.
    blocked_task_id: TaskId,
    /// Priority of the blocked task.
    blocked_priority: u8,
    /// Low-priority task that was executed instead.
    executing_task_id: TaskId,
    /// Priority of the executing task.
    executing_priority: u8,
    /// Timestamp when the inversion occurred.
    timestamp_ns: u64,
    /// Duration of the inversion (nanoseconds).
    duration_ns: u64,
}

/// Moving window for temporal pattern analysis.
#[derive(Debug, Clone)]
struct StarvationAnalysisWindow {
    /// Circular buffer of starvation events.
    events: Vec<u64>,
    /// Current write position in the circular buffer.
    write_pos: usize,
    /// Total number of events recorded.
    total_events: u64,
    /// Window size.
    size: usize,
}

impl StarvationAnalysisWindow {
    fn new(size: usize) -> Self {
        Self {
            events: vec![0; size.max(1)],
            write_pos: 0,
            size: size.max(1),
            total_events: 0,
        }
    }

    fn record_event(&mut self, timestamp_ns: u64) {
        self.events[self.write_pos] = timestamp_ns;
        self.write_pos = (self.write_pos + 1) % self.size;
        self.total_events = self.total_events.saturating_add(1);
    }

    fn events_in_window(&self, window_duration_ns: u64, current_time_ns: u64) -> u32 {
        let threshold_time = current_time_ns.saturating_sub(window_duration_ns);
        let mut count = 0;
        let recorded_events = usize::try_from(self.total_events)
            .unwrap_or(usize::MAX)
            .min(self.size);

        for &event_time in self.events.iter().take(recorded_events) {
            if event_time >= threshold_time && event_time <= current_time_ns {
                count += 1;
            }
        }
        count
    }

    fn is_pattern_detected(
        &self,
        min_events: u32,
        window_duration_ns: u64,
        current_time_ns: u64,
    ) -> bool {
        self.events_in_window(window_duration_ns, current_time_ns) >= min_events
    }
}

/// Enhanced fairness monitoring framework for starvation and priority inversion detection.
#[derive(Debug)]
pub struct FairnessMonitor {
    /// Configuration for fairness monitoring.
    config: FairnessConfig,
    /// Per-task starvation tracking information.
    tracked_tasks: std::collections::HashMap<TaskId, TaskStarvationInfo>,
    /// Recent priority inversion events.
    priority_inversions: Vec<PriorityInversionEvent>,
    /// Moving window for starvation pattern analysis.
    starvation_window: StarvationAnalysisWindow,
    /// Total starvation events detected.
    total_starvation_events: u64,
    /// Total priority inversion events detected.
    total_priority_inversions: u64,
    /// Maximum observed task wait time.
    max_task_wait_time_ns: u64,
    /// Last cleanup timestamp to prevent unbounded growth.
    last_cleanup_time_ns: u64,
}

impl FairnessMonitor {
    /// Creates a new fairness monitor with the given configuration.
    #[must_use]
    pub fn new(config: FairnessConfig) -> Self {
        let window_size = config.analysis_window_size;
        Self {
            config,
            tracked_tasks: std::collections::HashMap::new(),
            priority_inversions: Vec::new(),
            starvation_window: StarvationAnalysisWindow::new(window_size),
            total_starvation_events: 0,
            total_priority_inversions: 0,
            max_task_wait_time_ns: 0,
            last_cleanup_time_ns: 0,
        }
    }

    /// Creates a new fairness monitor with default configuration.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(FairnessConfig::default())
    }

    /// Records a task entering a queue for starvation tracking.
    pub fn record_task_enqueue(
        &mut self,
        task_id: TaskId,
        priority: u8,
        current_time_ns: u64,
        lane: u8,
    ) {
        if !self.config.enable_per_task_tracking {
            return;
        }

        // Cleanup old entries if needed
        self.cleanup_if_needed(current_time_ns);

        // Only track up to max_tracked_tasks to prevent unbounded growth
        if self.tracked_tasks.len() >= self.config.max_tracked_tasks {
            // Remove oldest entry
            if let Some((oldest_task_id, _)) = self
                .tracked_tasks
                .iter()
                .min_by_key(|(_, info)| info.enqueue_time_ns)
                .map(|(id, info)| (*id, info.clone()))
            {
                self.tracked_tasks.remove(&oldest_task_id);
            }
        }

        let info = TaskStarvationInfo::new(task_id, priority, current_time_ns, lane);
        self.tracked_tasks.insert(task_id, info);
    }

    /// Records a task being dispatched (removes from tracking).
    pub fn record_task_dispatch(&mut self, task_id: TaskId, current_time_ns: u64) -> Option<u64> {
        if let Some(info) = self.tracked_tasks.remove(&task_id) {
            let wait_time = info.current_wait_time_ns(current_time_ns);
            if wait_time > self.max_task_wait_time_ns {
                self.max_task_wait_time_ns = wait_time;
            }
            Some(wait_time)
        } else {
            None
        }
    }

    /// Records a task being skipped in favor of higher-priority work.
    pub fn record_task_skip(
        &mut self,
        skipped_task_id: TaskId,
        executing_task_id: TaskId,
        executing_priority: u8,
        current_time_ns: u64,
    ) {
        let (should_record_starvation, should_record_inversion, blocked_priority) = {
            if let Some(info) = self.tracked_tasks.get_mut(&skipped_task_id) {
                info.record_skip(current_time_ns);

                let is_starved =
                    info.is_starved(self.config.starvation_threshold_ns, current_time_ns);
                let is_inversion = info.priority > executing_priority;
                let priority = info.priority;

                (is_starved, is_inversion, priority)
            } else {
                (false, false, 0)
            }
        };

        // Record events after releasing the borrow
        if should_record_starvation {
            self.record_starvation_event(current_time_ns);
        }

        if should_record_inversion {
            self.record_priority_inversion(
                skipped_task_id,
                blocked_priority,
                executing_task_id,
                executing_priority,
                current_time_ns,
            );
        }
    }

    /// Records a starvation event for pattern analysis.
    fn record_starvation_event(&mut self, timestamp_ns: u64) {
        self.total_starvation_events = self.total_starvation_events.saturating_add(1);
        self.starvation_window.record_event(timestamp_ns);
    }

    /// Records a priority inversion event.
    fn record_priority_inversion(
        &mut self,
        blocked_task: TaskId,
        blocked_priority: u8,
        executing_task: TaskId,
        executing_priority: u8,
        timestamp_ns: u64,
    ) {
        self.total_priority_inversions = self.total_priority_inversions.saturating_add(1);

        let inversion = PriorityInversionEvent {
            blocked_task_id: blocked_task,
            blocked_priority,
            executing_task_id: executing_task,
            executing_priority,
            timestamp_ns,
            duration_ns: 0, // Will be updated when inversion ends
        };

        self.priority_inversions.push(inversion);

        // Keep only recent inversions to prevent unbounded growth
        const MAX_TRACKED_INVERSIONS: usize = 1000;
        if self.priority_inversions.len() > MAX_TRACKED_INVERSIONS {
            self.priority_inversions
                .drain(0..self.priority_inversions.len() - MAX_TRACKED_INVERSIONS);
        }
    }

    /// Detects if there's a starvation pattern in the current window.
    #[must_use]
    pub fn detect_starvation_pattern(&self, current_time_ns: u64) -> bool {
        const PATTERN_WINDOW_NS: u64 = 1_000_000_000; // 1 second
        const MIN_EVENTS_FOR_PATTERN: u32 = 10;

        self.starvation_window.is_pattern_detected(
            MIN_EVENTS_FOR_PATTERN,
            PATTERN_WINDOW_NS,
            current_time_ns,
        )
    }

    /// Returns the number of currently starved tasks.
    #[must_use]
    pub fn count_starved_tasks(&self, current_time_ns: u64) -> u32 {
        self.tracked_tasks
            .values()
            .filter(|info| info.is_starved(self.config.starvation_threshold_ns, current_time_ns))
            .count() as u32
    }

    /// Returns starvation statistics for monitoring.
    #[must_use]
    pub fn starvation_stats(&self, current_time_ns: u64) -> StarvationStats {
        let currently_starved = self.count_starved_tasks(current_time_ns);
        let total_tracked_wait_time_ns = self
            .tracked_tasks
            .values()
            .map(|info| {
                info.total_wait_time_ns
                    .max(info.current_wait_time_ns(current_time_ns))
            })
            .sum::<u64>();
        let avg_wait_time_ns = if self.tracked_tasks.is_empty() {
            0
        } else {
            total_tracked_wait_time_ns / self.tracked_tasks.len() as u64
        };
        let oldest_tracked_task = self
            .tracked_tasks
            .values()
            .max_by_key(|info| info.current_wait_time_ns(current_time_ns))
            .map(|info| StarvedTaskSummary {
                task_id: info.task_id,
                priority: info.priority,
                current_lane: info.current_lane,
                skip_count: info.skip_count,
                wait_time_ns: info.current_wait_time_ns(current_time_ns),
                total_wait_time_ns: info
                    .total_wait_time_ns
                    .max(info.current_wait_time_ns(current_time_ns)),
            });
        let latest_priority_inversion =
            self.priority_inversions
                .last()
                .map(|event| PriorityInversionSummary {
                    blocked_task_id: event.blocked_task_id,
                    blocked_priority: event.blocked_priority,
                    executing_task_id: event.executing_task_id,
                    executing_priority: event.executing_priority,
                    priority_gap: event
                        .blocked_priority
                        .saturating_sub(event.executing_priority),
                    timestamp_ns: event.timestamp_ns,
                    duration_ns: event.duration_ns,
                });
        let max_priority_inversion_gap = self
            .priority_inversions
            .iter()
            .map(|event| {
                event
                    .blocked_priority
                    .saturating_sub(event.executing_priority)
            })
            .max()
            .unwrap_or(0);

        StarvationStats {
            total_starvation_events: self.total_starvation_events,
            currently_starved_tasks: currently_starved,
            max_task_wait_time_ns: self.max_task_wait_time_ns,
            avg_task_wait_time_ns: avg_wait_time_ns,
            total_priority_inversions: self.total_priority_inversions,
            tracked_tasks_count: self.tracked_tasks.len() as u32,
            pattern_detected: self.detect_starvation_pattern(current_time_ns),
            total_tracked_wait_time_ns,
            oldest_tracked_task,
            max_priority_inversion_gap,
            latest_priority_inversion,
        }
    }

    /// Cleans up old tracking entries to prevent unbounded growth.
    fn cleanup_if_needed(&mut self, current_time_ns: u64) {
        const CLEANUP_INTERVAL_NS: u64 = 60_000_000_000; // 60 seconds
        const MAX_TASK_AGE_NS: u64 = 300_000_000_000; // 5 minutes

        if current_time_ns.saturating_sub(self.last_cleanup_time_ns) < CLEANUP_INTERVAL_NS {
            return;
        }

        self.last_cleanup_time_ns = current_time_ns;

        // Remove tasks that are too old
        let cutoff_time = current_time_ns.saturating_sub(MAX_TASK_AGE_NS);
        self.tracked_tasks
            .retain(|_, info| info.enqueue_time_ns >= cutoff_time);
    }
}

/// Starvation monitoring statistics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StarvedTaskSummary {
    /// Identifier of the oldest currently tracked task.
    pub task_id: TaskId,
    /// Priority assigned to the tracked task.
    pub priority: u8,
    /// Queue lane where the task is currently tracked (Cancel=0, Timed=1, Ready=2).
    pub current_lane: u8,
    /// Number of times the task has been skipped.
    pub skip_count: u32,
    /// Current wait time for the task.
    pub wait_time_ns: u64,
    /// Total accumulated wait time snapshot recorded for the task.
    pub total_wait_time_ns: u64,
}

/// Summary of the latest observed priority inversion.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PriorityInversionSummary {
    /// High-priority task that was blocked.
    pub blocked_task_id: TaskId,
    /// Priority of the blocked task.
    pub blocked_priority: u8,
    /// Lower-priority task that executed instead.
    pub executing_task_id: TaskId,
    /// Priority of the executing task.
    pub executing_priority: u8,
    /// Difference between blocked and executing priorities.
    pub priority_gap: u8,
    /// Timestamp when the inversion was observed.
    pub timestamp_ns: u64,
    /// Recorded duration of the inversion.
    pub duration_ns: u64,
}

/// Starvation monitoring statistics.
#[derive(Debug, Clone, Default)]
pub struct StarvationStats {
    /// Total starvation events detected.
    pub total_starvation_events: u64,
    /// Number of tasks currently experiencing starvation.
    pub currently_starved_tasks: u32,
    /// Maximum observed task wait time (nanoseconds).
    pub max_task_wait_time_ns: u64,
    /// Average task wait time across all tracked tasks (nanoseconds).
    pub avg_task_wait_time_ns: u64,
    /// Total priority inversion events detected.
    pub total_priority_inversions: u64,
    /// Number of tasks currently being tracked.
    pub tracked_tasks_count: u32,
    /// Whether a starvation pattern has been detected.
    pub pattern_detected: bool,
    /// Sum of the current wait times for all tracked tasks.
    pub total_tracked_wait_time_ns: u64,
    /// Oldest task currently tracked by the monitor.
    pub oldest_tracked_task: Option<StarvedTaskSummary>,
    /// Largest priority gap observed across retained inversion events.
    pub max_priority_inversion_gap: u8,
    /// Most recent priority inversion retained by the monitor.
    pub latest_priority_inversion: Option<PriorityInversionSummary>,
}

/// Deterministic witness for cancel-lane fairness guarantees.
///
/// This compiles the runtime fairness argument into an auditable artifact:
/// if `invariant_holds()` is true, then observed dispatches respected the
/// effective cancel-streak bound and ready/timed work received a slot within
/// `ready_stall_bound_steps()`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreemptionFairnessCertificate {
    /// Worker-local baseline cancel streak limit `L`.
    pub base_limit: usize,
    /// Largest effective limit observed during the run (`L` or `2L`).
    pub effective_limit: usize,
    /// Observed maximum cancel streak in this run.
    pub observed_max_cancel_streak: usize,
    /// Total cancel dispatches.
    pub cancel_dispatches: u64,
    /// Total timed dispatches.
    pub timed_dispatches: u64,
    /// Total ready dispatches.
    pub ready_dispatches: u64,
    /// Times the fairness gate forced a non-cancel attempt.
    pub fairness_yields: u64,
    /// Largest observed cancel streak immediately before a ready dispatch.
    pub observed_max_ready_stall_steps: usize,
    /// Largest observed cancel streak immediately before a timed dispatch.
    pub observed_max_timed_stall_steps: usize,
    /// Number of observed ready-lane priority inversions.
    pub ready_priority_inversions: u64,
    /// Largest observed ready-lane priority gap when an inversion occurred.
    pub max_ready_priority_inversion_gap: u8,
    /// Fallback cancel dispatches used when no other work existed.
    pub fallback_cancel_dispatches: u64,
    /// Count of streak samples above baseline `L`.
    pub base_limit_exceedances: u64,
    /// Count of streak samples above effective limit.
    pub effective_limit_exceedances: u64,
    /// Whether adaptive cancel-streak policy was active.
    pub adaptive_enabled: bool,
    /// Current adaptive base limit (if enabled), otherwise equals `base_limit`.
    pub adaptive_current_limit: usize,
}

impl PreemptionFairnessCertificate {
    /// Returns the worst-case bound on non-cancel dispatch stall (in steps).
    ///
    /// Under this run's observed policy envelope, ready/timed dispatch gets a
    /// scheduling opportunity within `effective_limit + 1` steps.
    #[must_use]
    pub fn ready_stall_bound_steps(&self) -> usize {
        self.effective_limit.saturating_add(1)
    }

    /// Returns the largest observed cancel-induced stall across ready/timed work.
    #[must_use]
    pub fn observed_non_cancel_stall_steps(&self) -> usize {
        self.observed_max_ready_stall_steps
            .max(self.observed_max_timed_stall_steps)
    }

    /// Returns `true` when fairness invariants hold for observed dispatches.
    #[must_use]
    pub fn invariant_holds(&self) -> bool {
        self.effective_limit_exceedances == 0
            && self.observed_max_cancel_streak <= self.effective_limit
            && self.ready_priority_inversions == 0
    }

    /// Deterministic hash of the certificate contents for replay/audit linkage.
    #[must_use]
    pub fn witness_hash(&self) -> u64 {
        use std::hash::{Hash, Hasher};

        let mut h = DetHasher::default();
        self.base_limit.hash(&mut h);
        self.effective_limit.hash(&mut h);
        self.observed_max_cancel_streak.hash(&mut h);
        self.cancel_dispatches.hash(&mut h);
        self.timed_dispatches.hash(&mut h);
        self.ready_dispatches.hash(&mut h);
        self.fairness_yields.hash(&mut h);
        self.observed_max_ready_stall_steps.hash(&mut h);
        self.observed_max_timed_stall_steps.hash(&mut h);
        self.ready_priority_inversions.hash(&mut h);
        self.max_ready_priority_inversion_gap.hash(&mut h);
        self.fallback_cancel_dispatches.hash(&mut h);
        self.base_limit_exceedances.hash(&mut h);
        self.effective_limit_exceedances.hash(&mut h);
        self.adaptive_enabled.hash(&mut h);
        self.adaptive_current_limit.hash(&mut h);
        h.finish()
    }
}

impl ThreeLaneWorker {
    /// Returns the current time in nanoseconds for fairness monitoring.
    #[inline]
    fn current_time_ns(&self) -> u64 {
        self.timer_driver
            .as_ref()
            .map_or(0, |timer| timer.now().as_nanos())
    }

    /// Executes a closure with access to the fairness monitor for this worker.
    pub fn with_fairness_monitor<T>(&self, f: impl FnOnce(&FairnessMonitor) -> T) -> T {
        f(&self.fairness_monitor.lock())
    }

    /// Returns starvation statistics from the fairness monitor.
    #[must_use]
    pub fn starvation_stats(&self) -> StarvationStats {
        let current_time = self.current_time_ns();
        self.fairness_monitor.lock().starvation_stats(current_time)
    }

    /// Returns invariant statistics from the monitor.
    #[must_use]
    pub fn invariant_stats(&self) -> super::invariant_monitor::InvariantStats {
        self.invariant_monitor.lock().stats()
    }

    /// Returns all recorded invariant violations.
    #[must_use]
    pub fn invariant_violations(
        &self,
    ) -> std::collections::VecDeque<super::invariant_monitor::InvariantViolation> {
        self.invariant_monitor.lock().violations().clone()
    }

    /// Performs comprehensive scheduler invariant verification.
    ///
    /// This method checks queue consistency, task ownership, and other scheduler
    /// invariants that can be verified from current state. Should be called
    /// periodically in production to catch invariant violations.
    pub fn verify_scheduler_invariants(&mut self) {
        if !self.invariant_monitor.lock().is_enabled() {
            return;
        }

        let current_time = Time::from_nanos(self.current_time_ns());

        // Verify local queue consistency
        {
            let local_ready_guard = self.local_ready.lock();
            let local_ready_tasks: Vec<_> = local_ready_guard.iter().copied().collect();

            let ready_snapshot = super::invariant_monitor::QueueSnapshot {
                name: "local_ready_queue".to_string(),
                reported_depth: local_ready_tasks.len(),
                actual_tasks: local_ready_tasks,
                priority_range: if local_ready_guard.is_empty() {
                    None
                } else {
                    Some((0, 255)) // Conservative range for local tasks
                },
                time_range: Some((current_time, current_time)), // Snapshot time
            };

            drop(local_ready_guard);

            self.invariant_monitor
                .lock()
                .verify_queue_consistency(&ready_snapshot, current_time);
        }

        // Verify fast queue consistency
        let fast_queue_tasks = self.fast_queue.snapshot_tasks();
        let fast_snapshot = super::invariant_monitor::QueueSnapshot {
            name: "fast_queue".to_string(),
            reported_depth: fast_queue_tasks.len(),
            actual_tasks: fast_queue_tasks,
            priority_range: None,
            time_range: Some((current_time, current_time)),
        };
        self.invariant_monitor
            .lock()
            .verify_queue_consistency(&fast_snapshot, current_time);
    }

    /// Records task completion for invariant monitoring.
    ///
    /// This should be called when a task finishes execution to track
    /// task lifecycle and detect any invariant violations related
    /// to task completion.
    pub fn record_task_completion(&mut self, task: TaskId) {
        if !self.invariant_monitor.lock().is_enabled() {
            return;
        }

        let current_time = Time::from_nanos(self.current_time_ns());
        self.invariant_monitor
            .lock()
            .record_task_complete(task, self.id, current_time);
    }

    /// Records task cancellation for invariant monitoring.
    ///
    /// This should be called when a task is cancelled to track
    /// cancellation handling and detect leaked cancelled tasks.
    pub fn record_task_cancellation(&mut self, task: TaskId) {
        if !self.invariant_monitor.lock().is_enabled() {
            return;
        }

        let current_time = Time::from_nanos(self.current_time_ns());
        self.invariant_monitor
            .lock()
            .record_task_cancel(task, current_time);
    }

    /// Runs a closure against the task table, using the sharded task table
    /// when available, otherwise falling back to RuntimeState's embedded table.
    ///
    /// This is the hot-path accessor: when `task_table` is `Some`, only the
    /// task shard lock is acquired, avoiding contention with region/obligation
    /// mutations.
    #[inline]
    fn with_task_table<R, F: FnOnce(&mut TaskTable) -> R>(&self, f: F) -> R {
        if let Some(tt) = &self.task_table {
            let mut guard = tt.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
            f(&mut guard)
        } else {
            let mut state = self
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            f(&mut state.tasks)
        }
    }

    /// Read-only version of [`with_task_table`] for task record lookups.
    #[inline]
    fn with_task_table_ref<R, F: FnOnce(&TaskTable) -> R>(&self, f: F) -> R {
        if let Some(tt) = &self.task_table {
            let guard = tt.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
            f(&guard)
        } else {
            let state = self
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            f(&state.tasks)
        }
    }

    /// Returns the preemption fairness metrics for this worker.
    #[must_use]
    pub fn preemption_metrics(&self) -> &PreemptionMetrics {
        &self.preemption_metrics
    }

    /// Builds a deterministic fairness certificate from current metrics.
    ///
    /// This certificate is intended for invariant auditing and replay reports.
    #[must_use]
    pub fn preemption_fairness_certificate(&self) -> PreemptionFairnessCertificate {
        let adaptive_current_limit = self.adaptive_cancel_policy.as_ref().map_or(
            self.cancel_streak_limit,
            AdaptiveCancelStreakPolicy::current_limit,
        );
        let effective_limit = self
            .preemption_metrics
            .max_effective_limit_observed
            .max(adaptive_current_limit)
            .max(1);

        PreemptionFairnessCertificate {
            base_limit: adaptive_current_limit,
            effective_limit,
            observed_max_cancel_streak: self.preemption_metrics.max_cancel_streak,
            cancel_dispatches: self.preemption_metrics.cancel_dispatches,
            timed_dispatches: self.preemption_metrics.timed_dispatches,
            ready_dispatches: self.preemption_metrics.ready_dispatches,
            fairness_yields: self.preemption_metrics.fairness_yields,
            observed_max_ready_stall_steps: self.preemption_metrics.max_ready_dispatch_stall,
            observed_max_timed_stall_steps: self.preemption_metrics.max_timed_dispatch_stall,
            ready_priority_inversions: self.preemption_metrics.ready_priority_inversions,
            max_ready_priority_inversion_gap: self
                .preemption_metrics
                .max_ready_priority_inversion_gap,
            fallback_cancel_dispatches: self.preemption_metrics.fallback_cancel_dispatches,
            base_limit_exceedances: self.preemption_metrics.base_limit_exceedances,
            effective_limit_exceedances: self.preemption_metrics.effective_limit_exceedances,
            adaptive_enabled: self.adaptive_cancel_policy.is_some(),
            adaptive_current_limit,
        }
    }

    /// Attaches an evidence sink for scheduler decision tracing.
    pub fn set_evidence_sink(&mut self, sink: Arc<dyn crate::evidence_sink::EvidenceSink>) {
        self.evidence_sink = Some(sink);
    }

    /// Force the cached scheduling suggestion for testing the boosted 2L+1
    /// fairness bound under `DrainObligations`/`DrainRegions`.
    #[cfg(any(test, feature = "test-internals"))]
    pub fn set_cached_suggestion(&mut self, suggestion: SchedulingSuggestion) {
        self.cached_suggestion = suggestion;
    }

    #[inline]
    fn current_base_cancel_limit(&self) -> usize {
        self.adaptive_cancel_policy
            .as_ref()
            .map_or(
                self.cancel_streak_limit,
                AdaptiveCancelStreakPolicy::current_limit,
            )
            .max(1)
    }

    fn potential_from_snapshot(snapshot: &StateSnapshot) -> f64 {
        let w = PotentialWeights::default();
        let task_component = w.w_tasks * f64::from(snapshot.live_tasks);
        #[allow(clippy::cast_precision_loss)]
        let obligation_age_seconds = snapshot.obligation_age_sum_ns as f64 / 1_000_000_000.0;
        let obligation_component = w.w_obligation_age * obligation_age_seconds;
        let region_component = w.w_draining_regions * f64::from(snapshot.draining_regions);
        let deadline_component = w.w_deadline_pressure * snapshot.deadline_pressure;
        task_component + obligation_component + region_component + deadline_component
    }

    fn capture_adaptive_snapshot(&self) -> AdaptiveEpochSnapshot {
        let snapshot = {
            let state = self
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            StateSnapshot::from_runtime_state(&state)
        };
        AdaptiveEpochSnapshot {
            potential: Self::potential_from_snapshot(&snapshot),
            deadline_pressure: snapshot.deadline_pressure,
            base_limit_exceedances: self.preemption_metrics.base_limit_exceedances,
            effective_limit_exceedances: self.preemption_metrics.effective_limit_exceedances,
            fallback_cancel_dispatches: self.preemption_metrics.fallback_cancel_dispatches,
        }
    }

    fn ensure_adaptive_epoch_started(&mut self) {
        if self
            .adaptive_cancel_policy
            .as_ref()
            .is_none_or(|p| p.epoch_start.is_some())
        {
            return;
        }
        let snap = self.capture_adaptive_snapshot();
        if let Some(policy) = self.adaptive_cancel_policy.as_mut() {
            policy.begin_epoch(snap);
        }
    }

    fn adaptive_on_dispatch(&mut self) {
        self.ensure_adaptive_epoch_started();
        let should_close_epoch = self
            .adaptive_cancel_policy
            .as_mut()
            .is_some_and(AdaptiveCancelStreakPolicy::on_dispatch);
        if !should_close_epoch {
            return;
        }

        let snapshot_end = self.capture_adaptive_snapshot();
        let sample = self.rng.next_u64();
        let reward = self
            .adaptive_cancel_policy
            .as_mut()
            .and_then(|p| p.complete_epoch(snapshot_end, sample));

        if let Some(policy) = self.adaptive_cancel_policy.as_ref() {
            self.preemption_metrics.adaptive_epochs = policy.epoch_count;
            self.preemption_metrics.adaptive_current_limit = policy.current_limit();
            self.preemption_metrics.adaptive_reward_ema = policy.reward_ema;
            self.preemption_metrics.adaptive_e_value = policy.e_value();
        }

        if let Some(_reward) = reward {
            trace!(
                worker_id = self.id,
                reward = reward,
                adaptive_limit = self.preemption_metrics.adaptive_current_limit,
                adaptive_epochs = self.preemption_metrics.adaptive_epochs,
                adaptive_e_value = self.preemption_metrics.adaptive_e_value,
                "adaptive cancel-streak epoch update"
            );
        }
    }

    fn drive_io_phase(&self) -> IoPhaseOutcome {
        let Some(io) = &self.io_driver else {
            return IoPhaseOutcome::NoProgress;
        };

        let now = self
            .timer_driver
            .as_ref()
            .map_or(Time::ZERO, TimerDriverHandle::now);
        let local_deadline = self.local.lock().next_deadline();
        let timer_deadline = self
            .timer_driver
            .as_ref()
            .and_then(TimerDriverHandle::next_deadline);
        let global_deadline = self.global.peek_earliest_deadline();

        let next_deadline = [timer_deadline, local_deadline, global_deadline]
            .into_iter()
            .flatten()
            .min();

        let timeout = next_deadline.map(|deadline| {
            if deadline > now {
                Duration::from_nanos(deadline.duration_since(now))
            } else {
                Duration::ZERO
            }
        });

        // We only block in I/O if we have no fast_queue work.
        let io_timeout = if self.fast_queue.is_empty() {
            timeout
        } else {
            Some(Duration::ZERO)
        };

        match io.try_turn_with(io_timeout, |_, _| {}) {
            Ok(Some(n)) => {
                // We successfully polled the reactor (we are the leader for this turn).
                // If n > 0, we woke some tasks.
                // If n == 0 but we had a non-zero timeout, we spent time blocking,
                // so we should continue the loop to check queues again.
                // If n == 0 and timeout was ZERO, we did a quick poll and found nothing.
                if n > 0 || io_timeout != Some(Duration::ZERO) {
                    IoPhaseOutcome::Progress
                } else {
                    IoPhaseOutcome::NoProgress
                }
            }
            Ok(None) | Err(_) => {
                // Another thread is already polling (we are a follower).
                // Do not busy loop. Proceed to backoff/park logic.
                IoPhaseOutcome::Follower
            }
        }
    }

    /// Runs the worker scheduling loop.
    ///
    /// The loop maintains strict priority ordering:
    /// 1. Process expired timers (wakes tasks via their wakers)
    /// 2. Cancel work (global then local)
    /// 3. Timed work (global then local)
    /// 4. Ready work (global then local)
    /// 5. Steal from other workers
    /// 6. Park (with timeout based on next timer deadline)
    pub fn run_loop(&mut self) {
        // Set thread-local scheduler for this worker thread.
        let _guard = ScopedLocalScheduler::new(Arc::clone(&self.local));
        // Set thread-local fast queue for O(1) ready-lane operations.
        let _queue_guard = LocalQueue::set_current(self.fast_queue.clone());
        // Set thread-local non-stealable queue for local (!Send) tasks.
        let _local_ready_guard = ScopedLocalReady::new(Arc::clone(&self.local_ready));
        // Set thread-local worker id for routing pinned local tasks.
        let _worker_guard = ScopedWorkerId::new(self.id);

        while !self.shutdown.load(Ordering::Relaxed) {
            if let Some(task) = self.next_task() {
                self.execute(task);
                continue;
            }

            if self.schedule_ready_finalizers() {
                continue;
            }

            // PHASE 5: Drive I/O (Leader/Follower pattern).
            let io_phase = self.drive_io_phase();
            if matches!(io_phase, IoPhaseOutcome::Progress) {
                // We polled I/O, so we might have woken tasks. Continue loop.
                continue;
            }

            // PHASE 6: Backoff before parking
            let mut backoff = 0;

            // Fast queue is Single Producer (this worker). If it's empty here, it stays empty
            // because only this worker pushes to it (via spawn/steal), and we are in the backoff loop.
            // So we don't need to check it inside the loop.
            if !self.fast_queue.is_empty() {
                continue;
            }

            loop {
                // Check shutdown before parking to avoid hanging in the backoff loop.
                if self.shutdown.load(Ordering::Relaxed) {
                    break;
                }

                // Get current time for runnable checks
                let now = self
                    .timer_driver
                    .as_ref()
                    .map_or(Time::ZERO, TimerDriverHandle::now);

                // Lock-free check: global injector and fast queue (no mutex needed).
                if self.global.has_runnable_work(now) || !self.fast_queue.is_empty() {
                    break;
                }

                if backoff < SPIN_LIMIT {
                    std::hint::spin_loop();
                    backoff += 1;
                } else if backoff < SPIN_LIMIT + YIELD_LIMIT {
                    std::thread::yield_now();
                    backoff += 1;
                } else if self.enable_parking {
                    // About to park: now check mutex-backed local queues.
                    // Deferred from the spin/yield phases to avoid 160 mutex
                    // round-trips per backoff cycle.
                    let (local_has_runnable, local_deadline) = {
                        let local = self.local.lock();
                        (local.has_runnable_work(now), local.next_deadline())
                    };
                    let local_ready_has_work = !self.local_ready.lock().is_empty();
                    if local_has_runnable || local_ready_has_work {
                        break;
                    }
                    // Park with timeout based on next timer deadline.
                    // If we are the IO leader, we shouldn't even be here (we'd block in epoll).
                    // If we are a follower, we just park until a deadline or woken.
                    let timer_deadline = self
                        .timer_driver
                        .as_ref()
                        .and_then(TimerDriverHandle::next_deadline);
                    let global_deadline = self.global.peek_earliest_deadline();
                    record_backoff_deadline_selection(
                        &mut self.preemption_metrics,
                        io_phase,
                        timer_deadline,
                        global_deadline,
                    );

                    let next_deadline = select_backoff_deadline(
                        io_phase,
                        timer_deadline,
                        local_deadline,
                        global_deadline,
                    );

                    if let Some(next_deadline) = next_deadline {
                        // Re-fetch now to ensure we don't sleep if deadline passed during logic
                        let now = self
                            .timer_driver
                            .as_ref()
                            .map_or(Time::ZERO, TimerDriverHandle::now);
                        match classify_backoff_timeout_decision(io_phase, next_deadline, now) {
                            BackoffTimeoutDecision::ParkTimeout { nanos } => {
                                record_backoff_timeout_park(
                                    &mut self.preemption_metrics,
                                    io_phase,
                                    nanos,
                                );
                                self.parker.park_timeout(Duration::from_nanos(nanos));
                            }
                            BackoffTimeoutDecision::DeadlineDue => {
                                // If deadline is due or passed, don't park - break to process timers/tasks.
                                break;
                            }
                        }
                    } else {
                        // Followers park indefinitely.
                        record_backoff_indefinite_park(&mut self.preemption_metrics, io_phase);
                        self.parker.park();
                    }
                    // After waking, re-check queues by continuing the loop.
                    // This fixes a lost-wakeup race where work arrives right as we park.
                    // Reset backoff to spin briefly before parking again (spurious wakeups).
                    backoff = 0;
                    // Continue loop to re-check condition (no break!)
                } else {
                    // Parking disabled; return to outer loop to keep spinning/yielding.
                    break;
                }
            }

            // After backoff/park, reset the consecutive cancel counter.
            // We've given other work a chance during the backoff period.
            self.cancel_streak = 0;
            self.ready_dispatch_streak = 0;
        }
    }

    /// Select the next task to dispatch, respecting lane priorities and fairness.
    ///
    /// Returns `None` when no work is available across any lane or steal target.
    ///
    /// # Lock reduction optimisation
    ///
    /// The previous implementation called `try_cancel_work`, `try_timed_work`,
    /// and `try_ready_work` sequentially.  Each method checks its global queue
    /// (lock-free or separate mutex) then falls through to the local
    /// `PriorityScheduler` lock, acquiring it up to **3 times per call** when
    /// global queues are empty.
    ///
    /// This version splits the work into phases:
    ///
    /// 1. **Global queues** (lock-free / own mutex) — suggestion-ordered.
    /// 2. **Fast ready paths** (`local_ready`, `fast_queue`, global ready) —
    ///    no `PriorityScheduler` lock.
    /// 3. **Single local lock** — cancel, timed, ready checked in suggestion
    ///    order under one acquisition.
    /// 4. **Steal** from other workers.
    /// 5. **Fallback cancel** (streak-limit path).
    ///
    /// Phases 1–2 cover the hot path (most dispatches come from global or fast
    /// queues).  Phase 3 replaces 3 lock acquisitions with 1 for the local
    /// PriorityScheduler fallback.
    #[allow(clippy::too_many_lines)]
    pub fn next_task(&mut self) -> Option<TaskId> {
        // PHASE 0: Process expired timers (fires wakers, which may inject tasks).
        if let Some(timer) = &self.timer_driver {
            let _ = timer.process_timers();
        }

        self.ensure_adaptive_epoch_started();

        // Consult the governor for scheduling suggestion (amortised).
        let suggestion = self.governor_suggest();
        let base_limit = self.current_base_cancel_limit();
        self.preemption_metrics.adaptive_current_limit = base_limit;

        // Cancel eligibility: effective limit depends on suggestion.
        let effective_limit = match suggestion {
            SchedulingSuggestion::DrainObligations | SchedulingSuggestion::DrainRegions => {
                base_limit.saturating_mul(2)
            }
            _ => base_limit,
        };
        if effective_limit > self.preemption_metrics.max_effective_limit_observed {
            self.preemption_metrics.max_effective_limit_observed = effective_limit;
        }
        let check_cancel = self.cancel_streak < effective_limit;
        if !check_cancel {
            self.preemption_metrics.fairness_yields += 1;
        }

        // Current time for EDF (computed once, reused for global + local).
        let now = self
            .timer_driver
            .as_ref()
            .map_or(Time::ZERO, TimerDriverHandle::now);

        // ── PHASE 1: Highest Priority Global Queue ───────────────────────
        if suggestion == SchedulingSuggestion::MeetDeadlines {
            // Deadline pressure: global timed first.
            if let Some(tt) = self.global.pop_timed_if_due(now) {
                self.record_timed_dispatch();
                return Some(self.finish_dispatch(tt.task));
            }
        } else {
            // Default / drain: cancel > timed.
            if check_cancel {
                if let Some(pt) = self.global.pop_cancel() {
                    self.cancel_streak += 1;
                    self.ready_dispatch_streak = 0;
                    self.record_cancel_dispatch(base_limit, effective_limit);
                    return Some(self.finish_dispatch(pt.task));
                }
            }
        }

        // ── PHASE 2: Interleaved Local and Global Priority Lanes ────────
        // We acquire the local `PriorityScheduler` lock once and check
        // the remaining cancel/timed lanes in strict suggestion order.
        let mut local = self.local.lock();
        let rng_hint = self.rng.next_u64();

        if suggestion == SchedulingSuggestion::MeetDeadlines {
            // MeetDeadlines: Timed > Cancel (global timed already checked)
            if let Some(task) = local.pop_timed_only_with_hint(rng_hint, now) {
                drop(local);
                self.record_timed_dispatch();
                return Some(self.finish_dispatch(task));
            }
            if check_cancel {
                if let Some(pt) = self.global.pop_cancel() {
                    drop(local);
                    self.cancel_streak += 1;
                    self.ready_dispatch_streak = 0;
                    self.record_cancel_dispatch(base_limit, effective_limit);
                    return Some(self.finish_dispatch(pt.task));
                }
                if let Some(task) = local.pop_cancel_only_with_hint(rng_hint) {
                    drop(local);
                    self.cancel_streak += 1;
                    self.ready_dispatch_streak = 0;
                    self.record_cancel_dispatch(base_limit, effective_limit);
                    return Some(self.finish_dispatch(task));
                }
            }
        } else {
            // Default: Cancel > Timed (global cancel already checked)
            if check_cancel {
                if let Some(task) = local.pop_cancel_only_with_hint(rng_hint) {
                    drop(local);
                    self.cancel_streak += 1;
                    self.ready_dispatch_streak = 0;
                    self.record_cancel_dispatch(base_limit, effective_limit);
                    return Some(self.finish_dispatch(task));
                }
            }
            if let Some(tt) = self.global.pop_timed_if_due(now) {
                drop(local);
                self.record_timed_dispatch();
                return Some(self.finish_dispatch(tt.task));
            }
            if let Some(task) = local.pop_timed_only_with_hint(rng_hint, now) {
                drop(local);
                self.record_timed_dispatch();
                return Some(self.finish_dispatch(task));
            }
        }
        drop(local);

        if self.should_force_ready_handoff() {
            self.preemption_metrics.browser_ready_handoff_yields += 1;
            self.cancel_streak = 0;
            self.ready_dispatch_streak = 0;
            return None;
        }

        // ── PHASE 3: Fast ready paths (no PriorityScheduler lock) ────
        // Check local_ready first (highest priority: non-stealable local tasks),
        // then lock-free fast_queue (O(1) atomic pop).
        let local_ready_task = self.local_ready.lock().pop_front();
        if let Some(task) = local_ready_task {
            self.record_ready_dispatch();
            return Some(self.finish_dispatch(task));
        }
        if let Some(task) = self.fast_queue.pop() {
            let blocked_local_task = {
                let local = self.local.lock();
                local.peek_ready_task()
            };
            let dispatched_priority = self.task_sched_priority(task);
            self.record_ready_priority_inversion(blocked_local_task, task, dispatched_priority);
            self.record_ready_dispatch();
            return Some(self.finish_dispatch(task));
        }
        if let Some(pt) = self.global.pop_ready() {
            let blocked_local_task = {
                let local = self.local.lock();
                local.peek_ready_task()
            };
            self.record_ready_priority_inversion(blocked_local_task, pt.task, Some(pt.priority));
            self.record_ready_dispatch();
            return Some(self.finish_dispatch(pt.task));
        }

        // ── PHASE 3b: Local Ready Lane ───────────────────────────────
        // All global/fast ready paths returned nothing. Check local ready.
        let rng_hint = self.rng.next_u64();
        let local_task = {
            let mut local = self.local.lock();
            local.pop_ready_only_with_hint(rng_hint)
        };
        if let Some(task) = local_task {
            self.record_ready_dispatch();
            return Some(self.finish_dispatch(task));
        }

        // ── PHASE 4: Steal from other workers ────────────────────────
        if let Some(task) = self.try_steal() {
            self.record_ready_dispatch();
            return Some(self.finish_dispatch(task));
        }

        // ── PHASE 5: Fallback cancel ─────────────────────────────────
        // The streak limit was hit but no other lanes had work.  Allow
        // one more cancel dispatch (global + local).  Sets streak to 1
        // so the next call re-checks ready/timed after at most
        // cancel_streak_limit − 1 more cancel dispatches.
        if !check_cancel {
            if let Some(task) = self.try_cancel_work() {
                self.preemption_metrics.fallback_cancel_dispatches += 1;
                self.cancel_streak = 1;
                self.ready_dispatch_streak = 0;
                self.record_cancel_dispatch(base_limit, effective_limit);
                return Some(self.finish_dispatch(task));
            }
            self.cancel_streak = 0;
        }

        self.ready_dispatch_streak = 0;
        None
    }

    #[inline]
    fn should_force_ready_handoff(&self) -> bool {
        let limit = self.browser_ready_handoff_limit;
        if limit == 0 || self.ready_dispatch_streak < limit {
            return false;
        }

        if !self.fast_queue.is_empty() || self.global.has_ready_work() {
            return true;
        }
        if self
            .local_ready
            .try_lock()
            .is_some_and(|queue| !queue.is_empty())
        {
            return true;
        }
        self.local.lock().has_ready_work()
    }

    /// Record a cancel dispatch and update max streak metric.
    #[inline]
    fn record_cancel_dispatch(&mut self, base_limit: usize, effective_limit: usize) {
        self.preemption_metrics.cancel_dispatches += 1;
        if self.cancel_streak > self.preemption_metrics.max_cancel_streak {
            self.preemption_metrics.max_cancel_streak = self.cancel_streak;
        }
        if self.cancel_streak > base_limit {
            self.preemption_metrics.base_limit_exceedances += 1;
        }
        if self.cancel_streak > effective_limit {
            self.preemption_metrics.effective_limit_exceedances += 1;
        }
    }

    #[inline]
    fn record_timed_dispatch(&mut self) {
        if self.cancel_streak > self.preemption_metrics.max_timed_dispatch_stall {
            self.preemption_metrics.max_timed_dispatch_stall = self.cancel_streak;
        }
        self.cancel_streak = 0;
        self.ready_dispatch_streak = 0;
        self.preemption_metrics.timed_dispatches += 1;
    }

    #[inline]
    fn record_ready_dispatch(&mut self) {
        if self.cancel_streak > self.preemption_metrics.max_ready_dispatch_stall {
            self.preemption_metrics.max_ready_dispatch_stall = self.cancel_streak;
        }
        self.cancel_streak = 0;
        self.ready_dispatch_streak = self.ready_dispatch_streak.saturating_add(1);
        self.preemption_metrics.ready_dispatches += 1;
    }

    fn record_ready_priority_inversion(
        &mut self,
        blocked_task: Option<(TaskId, u8)>,
        executing_task: TaskId,
        executing_priority: Option<u8>,
    ) {
        let Some((blocked_task, blocked_priority)) = blocked_task else {
            return;
        };
        let Some(executing_priority) = executing_priority else {
            return;
        };
        if blocked_priority <= executing_priority {
            return;
        }
        let timestamp = Time::from_nanos(self.current_time_ns());
        self.preemption_metrics.ready_priority_inversions += 1;
        let gap = blocked_priority.saturating_sub(executing_priority);
        if gap > self.preemption_metrics.max_ready_priority_inversion_gap {
            self.preemption_metrics.max_ready_priority_inversion_gap = gap;
        }
        self.invariant_monitor.lock().record_task_requeue(
            blocked_task,
            "local_ready_heap",
            blocked_priority,
            timestamp,
        );
        self.invariant_monitor.lock().verify_priority_ordering(
            executing_task,
            executing_priority,
            blocked_task,
            blocked_priority,
            timestamp,
        );
        self.fairness_monitor.lock().record_priority_inversion(
            blocked_task,
            blocked_priority,
            executing_task,
            executing_priority,
            timestamp.as_nanos(),
        );
    }

    #[inline]
    fn task_sched_priority(&self, task: TaskId) -> Option<u8> {
        self.with_task_table_ref(|tt| tt.task(task).map(|record| record.sched_priority))
    }

    #[inline]
    fn finish_dispatch(&mut self, task: TaskId) -> TaskId {
        self.adaptive_on_dispatch();

        // Record task dispatch for fairness monitoring
        let current_time = self.current_time_ns();
        self.fairness_monitor
            .lock()
            .record_task_dispatch(task, current_time);

        // Record task dequeue for invariant verification
        self.invariant_monitor
            .lock()
            .record_task_dispatch(task, Time::from_nanos(current_time));

        task
    }

    /// Consult the governor for a scheduling suggestion, taking a fresh
    /// snapshot every `governor_interval` steps. When the governor is
    /// disabled, always returns `NoPreference`.
    #[allow(clippy::too_many_lines)]
    fn governor_suggest(&mut self) -> SchedulingSuggestion {
        let Some(governor) = &self.governor else {
            return SchedulingSuggestion::NoPreference;
        };

        self.steps_since_snapshot += 1;
        if self.steps_since_snapshot < self.governor_interval {
            return self.cached_suggestion;
        }
        self.steps_since_snapshot = 0;

        // Take a snapshot under the state lock (bounded work, no allocs).
        let state = self
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let snapshot = StateSnapshot::from_runtime_state(&state);
        let (wait_graph_nodes, wait_graph_edges, trapped_wait_cycle) =
            if self.spectral_monitor.is_some() {
                wait_graph_signals_from_state(&state)
            } else {
                (0, Vec::new(), false)
            };
        drop(state);

        // Enrich with local queue depth.
        let queue_depth = self.local.lock().len();
        #[allow(clippy::cast_possible_truncation)]
        let snapshot = snapshot.with_ready_queue_depth(queue_depth as u32);

        let lyapunov_suggestion = governor.suggest(&snapshot);

        // Feed the drain progress certificate ONLY when the Lyapunov
        // governor indicates a drain phase (DrainObligations or DrainRegions).
        // During normal operation, steady-state potential fluctuation would
        // trigger false stall detection after stall_threshold consecutive
        // non-decreasing observations. By gating on the drain suggestion,
        // the certificate tracks convergence only when convergence is the
        // goal. When the governor leaves drain mode (NoPreference), the
        // certificate is reset for the next drain cycle.
        let drain_verdict = self.drain_certificate.as_mut().and_then(|cert| {
            let is_drain_phase = matches!(
                lyapunov_suggestion,
                SchedulingSuggestion::DrainObligations | SchedulingSuggestion::DrainRegions
            );
            if is_drain_phase {
                cert.observe(governor.compute_record(&snapshot).total);
                Some(cert.verdict())
            } else {
                // Not in a drain phase — reset the certificate so stale
                // observations from a prior drain cycle don't carry over.
                if !cert.is_empty() {
                    cert.reset();
                }
                None
            }
        });

        let mut spectral_report = None;
        if let Some(monitor) = self.spectral_monitor.as_mut() {
            if trapped_wait_cycle || wait_graph_nodes > 1 {
                spectral_report = Some(monitor.analyze_with_trapped_cycle(
                    wait_graph_nodes,
                    &wait_graph_edges,
                    trapped_wait_cycle,
                ));
            }
        }

        // Apply decision contract modulation if available (bd-1e2if.6).
        let mut suggestion = if let (Some(contract), Some(posterior)) =
            (&self.decision_contract, &mut self.decision_posterior)
        {
            // Update posterior from snapshot observations.
            let likelihoods =
                super::decision_contract::SchedulerDecisionContract::snapshot_likelihoods(
                    &snapshot,
                );
            posterior.bayesian_update(&likelihoods);

            let probs = posterior.probs();
            #[allow(clippy::cast_precision_loss)]
            let uniform = 1.0 / probs.len().max(1) as f64;
            let max_prob = probs
                .iter()
                .copied()
                .fold(0.0_f64, f64::max)
                .clamp(0.0, 1.0);
            let concentration = if probs.len() > 1 {
                ((max_prob - uniform) / (1.0 - uniform)).clamp(0.0, 1.0)
            } else {
                1.0
            };
            let entropy = normalized_entropy(probs);

            // Split-conformal one-step hit score from spectral monitor, when available.
            let conformal_hit = spectral_report
                .as_ref()
                .and_then(|report| {
                    report.bifurcation.as_ref().and_then(|bw| {
                        bw.conformal_lower_bound_next
                            .map(|lb| u8::from(report.decomposition.fiedler_value >= lb))
                    })
                })
                .map_or(1.0, f64::from);
            let uncertainty_penalty = 0.35f64.mul_add(1.0 - concentration, 0.15 * entropy);
            let conformal_penalty = 0.5 * (1.0 - conformal_hit);
            let calibration_score = (1.0 - uncertainty_penalty - conformal_penalty).clamp(0.0, 1.0);

            // Proxy posterior uncertainty width from concentration + entropy.
            let ci_width = 0.5f64
                .mul_add(1.0 - concentration, 0.25 * entropy)
                .clamp(0.0, 1.0);
            let adaptive_e = self.preemption_metrics.adaptive_e_value.max(1.0);
            let spectral_e = spectral_report
                .as_ref()
                .and_then(|report| {
                    report
                        .bifurcation
                        .as_ref()
                        .map(|bw| bw.deterioration_e_value.max(1.0))
                })
                .unwrap_or(1.0);
            let e_process = adaptive_e.max(spectral_e);

            // Evaluate the contract.
            let seq = self.decision_sequence;
            self.decision_sequence = self.decision_sequence.saturating_add(1);
            let now_ms = self
                .timer_driver
                .as_ref()
                .map_or(seq, |td| td.now().as_millis());
            let random_bits = ((self.id as u128) << 64) | u128::from(seq);
            let ctx = franken_decision::EvalContext {
                calibration_score,
                e_process,
                ci_width,
                decision_id: franken_kernel::DecisionId::from_parts(now_ms, random_bits),
                trace_id: franken_kernel::TraceId::from_parts(
                    now_ms,
                    random_bits ^ 0xA5A5_A5A5_A5A5_A5A5_A5A5,
                ),
                ts_unix_ms: now_ms,
            };
            let outcome = franken_decision::evaluate(contract, posterior, &ctx);

            // Emit decision audit entry as evidence.
            if let Some(ref sink) = self.evidence_sink {
                let evidence = outcome.audit_entry.to_evidence_ledger();
                sink.emit(&evidence);
            }

            // Map contract action to scheduling suggestion.
            match outcome.action_index {
                super::decision_contract::action::AGGRESSIVE => SchedulingSuggestion::NoPreference,
                super::decision_contract::action::CONSERVATIVE => {
                    SchedulingSuggestion::MeetDeadlines
                }
                // BALANCED: use the Lyapunov governor's suggestion.
                _ => lyapunov_suggestion,
            }
        } else {
            lyapunov_suggestion
        };

        // Spectral topology override: this makes structural health influence the
        // live scheduling path when governor mode is enabled. Mere wait-graph
        // fragmentation is not a trapped wait cycle; the SCC path below owns
        // actual deadlock forcing.
        if let Some(report) = spectral_report.as_ref() {
            let override_suggestion = match report.classification {
                crate::observability::spectral_health::HealthClassification::Deadlocked => {
                    Some(SchedulingSuggestion::DrainObligations)
                }
                crate::observability::spectral_health::HealthClassification::Critical {
                    approaching_disconnect: true,
                    ..
                } => Some(SchedulingSuggestion::DrainObligations),
                _ => report.bifurcation.as_ref().and_then(|bw| {
                    (bw.trend
                        == crate::observability::spectral_health::SpectralTrend::Deteriorating
                        && (bw.confidence >= 0.6 || bw.deterioration_e_value >= 2.0))
                        .then_some(SchedulingSuggestion::DrainRegions)
                }),
            };
            if let Some(ovr) = override_suggestion {
                suggestion = ovr;
            }
        }
        if trapped_wait_cycle {
            suggestion = SchedulingSuggestion::DrainObligations;
        }

        // Drain-certificate override: the certificate is only fed during
        // Lyapunov drain phases (see above), so `drain_verdict` is `Some`
        // only when the governor wants to drain.
        //
        // IMPORTANT: never override a trapped-wait-cycle forced drain. The
        // certificate's quiescence verdict (Lyapunov potential near 0) does
        // NOT mean a structural deadlock is resolved — blocked tasks may
        // have zero potential while remaining permanently stuck.
        if !trapped_wait_cycle {
            if let Some(ref verdict) = drain_verdict {
                match verdict.drain_phase {
                    DrainPhase::Stalled if verdict.stall_detected => {
                        // Drain is in progress but potential has not decreased
                        // for stall_threshold consecutive governor snapshots.
                        // Ensure we are draining obligations specifically (the
                        // most aggressive drain mode).
                        suggestion = SchedulingSuggestion::DrainObligations;
                    }
                    DrainPhase::Quiescent => {
                        // Drain has converged to quiescence — relax back to
                        // normal scheduling. The certificate is reset in the
                        // non-drain branch above on the next governor call.
                        suggestion = SchedulingSuggestion::NoPreference;
                    }
                    _ => {}
                }
            }
        }

        // Emit simple evidence when the scheduling suggestion changes.
        if suggestion != self.cached_suggestion {
            if let Some(ref sink) = self.evidence_sink {
                let suggestion_str = match suggestion {
                    SchedulingSuggestion::MeetDeadlines => "meet_deadlines",
                    SchedulingSuggestion::DrainObligations => "drain_obligations",
                    SchedulingSuggestion::DrainRegions => "drain_regions",
                    SchedulingSuggestion::NoPreference => "no_preference",
                };
                let cancel_depth = snapshot.cancel_requested_tasks
                    + snapshot.cancelling_tasks
                    + snapshot.finalizing_tasks;
                crate::evidence_sink::emit_scheduler_evidence(
                    sink.as_ref(),
                    suggestion_str,
                    cancel_depth,
                    snapshot.draining_regions,
                    snapshot.ready_queue_depth,
                    self.decision_contract
                        .as_ref()
                        .is_some_and(|_| self.decision_posterior.is_some()),
                );
            }
        }

        self.cached_suggestion = suggestion;
        suggestion
    }

    /// Runs a single scheduling step.
    ///
    /// Returns `true` if a task was executed.
    pub fn run_once(&mut self) -> bool {
        if self.shutdown.load(Ordering::Relaxed) {
            return false;
        }

        if let Some(task) = self.next_task() {
            self.execute(task);
            return true;
        }

        false
    }

    /// Tries to get cancel work from global or local queues.
    pub(crate) fn try_cancel_work(&mut self) -> Option<TaskId> {
        // Global cancel has priority (cross-thread cancellations)
        if let Some(pt) = self.global.pop_cancel() {
            return Some(pt.task);
        }

        // Local cancel
        let mut local = self.local.lock();
        let rng_hint = self.rng.next_u64();
        local.pop_cancel_only_with_hint(rng_hint)
    }

    /// Tries to get timed work from global or local queues.
    ///
    /// Uses EDF (Earliest Deadline First) ordering. Only returns tasks
    /// whose deadline has passed.
    #[allow(dead_code)] // Scheduler dispatch integration path
    pub(crate) fn try_timed_work(&mut self) -> Option<TaskId> {
        // Get current time from timer driver or use Time::ZERO (always ready)
        let now = self
            .timer_driver
            .as_ref()
            .map_or(Time::ZERO, TimerDriverHandle::now);

        // Global timed - EDF ordering, only pop if deadline is due
        if let Some(tt) = self.global.pop_timed_if_due(now) {
            return Some(tt.task);
        }

        // Local timed (already EDF ordered)
        let mut local = self.local.lock();
        let rng_hint = self.rng.next_u64();
        local.pop_timed_only_with_hint(rng_hint, now)
    }

    /// Test-only accessor: returns the approximate number of ready tasks
    /// visible to this worker across its local queue, fast queue, and the
    /// shared global queue. Intended for invariant checks in metamorphic
    /// tests; not suitable for runtime decisions because the global count is
    /// shared across workers and can race with other workers' pops.
    #[cfg(any(test, feature = "test-internals"))]
    pub fn ready_count(&self) -> usize {
        let local_ready = self.local_ready.try_lock().map_or(0, |q| q.len());
        let fast = self.fast_queue.len();
        let global = self.global.ready_count();
        local_ready + fast + global
    }

    /// Tries to get ready work from fast queue, global, or local queues.
    #[allow(dead_code)] // Scheduler dispatch integration path
    pub(crate) fn try_ready_work(&mut self) -> Option<TaskId> {
        // Highest priority: drain non-stealable local (!Send) tasks first.
        // These tasks are pinned to this worker and cannot run elsewhere.
        if let Some(task) = self.local_ready.lock().pop_front() {
            return Some(task);
        }

        // Fast path: O(1) pop from local VecDeque (LIFO, cache-friendly).
        if let Some(task) = self.fast_queue.pop() {
            return Some(task);
        }

        // Global ready
        if let Some(pt) = self.global.pop_ready() {
            return Some(pt.task);
        }

        // Local ready (PriorityScheduler, O(log n) pop)
        let mut local = self.local.lock();
        let rng_hint = self.rng.next_u64();
        local.pop_ready_only_with_hint(rng_hint)
    }

    /// Tries to steal work from other workers.
    ///
    /// Fast path: O(1) steal from other workers' `LocalQueue` VecDeques.
    /// Slow path: O(k log n) steal from PriorityScheduler heaps.
    /// Only steals from ready lanes to preserve cancel/timed priority semantics.
    ///
    /// # Invariant
    ///
    /// Local (`!Send`) tasks are never returned from this method. They are
    /// enqueued exclusively in the non-stealable `local_ready` queue and
    /// never enter stealable structures (fast_queue or PriorityScheduler
    /// ready lane). The `debug_assert!` guards below verify this at runtime
    /// in debug builds.
    pub(crate) fn try_steal(&mut self) -> Option<TaskId> {
        // Fast path: steal from other workers' LocalQueues (O(1) per task).
        if !self.fast_stealers.is_empty() {
            let len = self.fast_stealers.len();
            let start = self.rng.next_usize(len);
            for i in 0..len {
                let idx = (start + i) % len;
                if let Some(task) = self.fast_stealers[idx].steal() {
                    // Safety invariant: local tasks must never be in stealable queues.
                    debug_assert!(
                        !self.with_task_table_ref(|tt| {
                            tt.task(task)
                                .is_some_and(crate::record::task::TaskRecord::is_local)
                        }),
                        "BUG: stole a local (!Send) task {task:?} from another worker's fast_queue"
                    );

                    // Record work-stealing for invariant verification
                    self.invariant_monitor
                        .lock()
                        .record_task_dispatch(task, Time::from_nanos(self.current_time_ns()));

                    return Some(task);
                }
            }
        }

        // Slow path: steal from PriorityScheduler heaps (O(k log n)).
        if self.stealers.is_empty() {
            return None;
        }

        let len = self.stealers.len();
        let start = self.rng.next_usize(len);

        for i in 0..len {
            let idx = (start + i) % len;
            let stealer = &self.stealers[idx];

            // Try to lock without blocking (skip if contended)
            if let Some(mut victim) = stealer.try_lock() {
                let stolen_count =
                    victim.steal_ready_batch_into(self.steal_batch_size, &mut self.steal_buffer);
                if stolen_count > 0 {
                    // Safety invariant: verify no local tasks were stolen.
                    #[cfg(debug_assertions)]
                    {
                        for &(task, _) in &self.steal_buffer[..stolen_count] {
                            let is_local = self.with_task_table_ref(|tt| {
                                tt.task(task)
                                    .is_some_and(crate::record::task::TaskRecord::is_local)
                            });
                            debug_assert!(
                                !is_local,
                                "BUG: stole a local (!Send) task {task:?} from PriorityScheduler"
                            );
                        }
                    }

                    // Take the first task to execute
                    let (first_task, _) = self.steal_buffer[0];

                    // Record work-stealing for invariant verification
                    self.invariant_monitor
                        .lock()
                        .record_task_dispatch(first_task, Time::from_nanos(self.current_time_ns()));

                    // Push remaining stolen tasks to our fast queue
                    if stolen_count > 1 {
                        for &(task, _priority) in self.steal_buffer[1..].iter().rev() {
                            self.fast_queue.push(task);

                            // Record enqueue to our fast queue for stolen tasks
                            self.invariant_monitor.lock().record_task_requeue(
                                task,
                                "fast_queue_stolen",
                                _priority,
                                Time::from_nanos(self.current_time_ns()),
                            );
                        }
                    }

                    return Some(first_task);
                }
            }
        }

        None
    }

    /// Schedules a task locally in the appropriate lane.
    ///
    /// Uses `wake_state.notify()` for centralized deduplication.
    /// If the task is already scheduled, this is a no-op.
    /// If the task record doesn't exist (e.g., in tests), allows scheduling.
    pub fn schedule_local(&self, task: TaskId, priority: u8) {
        let should_schedule = self.with_task_table_ref(|tt| {
            tt.task(task).is_none_or(|record| {
                // Local (!Send) tasks must never enter stealable structures.
                if record.is_local() {
                    error!(
                        ?task,
                        "schedule_local: refusing to enqueue local task into PriorityScheduler"
                    );
                    return false;
                }
                record.wake_state.notify()
            })
        });
        if should_schedule {
            let mut local = self.local.lock();
            local.schedule(task, priority);

            // Record task enqueue for fairness monitoring
            let current_time = self.current_time_ns();
            self.fairness_monitor.lock().record_task_enqueue(
                task,
                priority,
                current_time,
                2, // Ready lane = 2
            );

            // Record task enqueue for invariant verification
            self.invariant_monitor.lock().record_task_enqueue(
                task,
                "local_ready_heap",
                priority,
                Time::from_nanos(current_time),
            );
        }
    }

    /// Promotes a local task to the cancel lane, matching global cancel semantics.
    ///
    /// Uses `move_to_cancel_lane` so that a task already in the ready or timed
    /// lane is relocated to the cancel lane.  This mirrors the global path where
    /// `inject_cancel` always injects (allowing duplicates for priority promotion).
    ///
    /// `wake_state.notify()` is still called for coordination with `finish_poll`,
    /// but the promotion itself is unconditional: a cancel must not be silently
    /// dropped just because the task was already scheduled in a lower-priority lane.
    pub fn schedule_local_cancel(&self, task: TaskId, priority: u8) {
        self.with_task_table_ref(|tt| {
            if let Some(record) = tt.task(task) {
                record.wake_state.notify();
            }
        });
        {
            let mut local_ready_guard = self.local_ready.lock();
            if let Some(pos) = local_ready_guard.iter().position(|t| *t == task) {
                local_ready_guard.remove(pos);
            }
            drop(local_ready_guard);
            let mut local = self.local.lock();
            local.move_to_cancel_lane(task, priority);

            // Record task enqueue for fairness monitoring
            let current_time = self.current_time_ns();
            self.fairness_monitor.lock().record_task_enqueue(
                task,
                priority,
                current_time,
                0, // Cancel lane = 0
            );

            // Record task enqueue for invariant verification
            self.invariant_monitor.lock().record_task_requeue(
                task,
                "local_cancel_queue",
                priority,
                Time::from_nanos(current_time),
            );
        }
        self.parker.unpark();
    }

    /// Schedules a timed task locally.
    ///
    /// Uses `wake_state.notify()` for centralized deduplication.
    /// If the task is already scheduled, this is a no-op.
    /// If the task record doesn't exist (e.g., in tests), allows scheduling.
    pub fn schedule_local_timed(&self, task: TaskId, deadline: Time) {
        let should_schedule = self.with_task_table_ref(|tt| {
            tt.task(task).is_none_or(|record| {
                if record.is_local() {
                    error!(
                        ?task,
                        "schedule_local_timed: refusing to enqueue local task into timed lane"
                    );
                    return false;
                }
                record.wake_state.notify()
            })
        });
        if should_schedule {
            let mut local = self.local.lock();
            local.schedule_timed(task, deadline);

            // Record task enqueue for fairness monitoring
            let current_time = self.current_time_ns();
            self.fairness_monitor.lock().record_task_enqueue(
                task,
                0, // Timed tasks don't have explicit priority, use 0
                current_time,
                1, // Timed lane = 1
            );

            // Record task enqueue for invariant verification
            self.invariant_monitor.lock().record_task_enqueue(
                task,
                "local_timed_queue",
                0, // Timed tasks use priority 0
                Time::from_nanos(current_time),
            );
        }
    }

    /// Wakes a list of dependent tasks (waiters) while holding the RuntimeState lock.
    ///
    /// This handles local/global routing and centralized deduplication via `wake_state`.
    fn wake_dependents_locked(
        &self,
        state: &RuntimeState,
        waiters: impl IntoIterator<Item = TaskId>,
    ) {
        let mut global_tasks = smallvec::SmallVec::<[(TaskId, u8); 16]>::new();
        for waiter in waiters {
            if let Some(record) = state.task(waiter) {
                let waiter_priority = record.sched_priority;
                if record.wake_state.notify() {
                    if record.is_local() {
                        if let Some(worker_id) = record.pinned_worker() {
                            if let Some(queue) = self.all_local_ready.get(worker_id) {
                                queue.lock().push_back(waiter);
                                self.coordinator.wake_worker(worker_id);
                            } else {
                                // SAFETY: Invalid worker id for a local waiter means
                                // we can't route to the correct queue. Skipping the
                                // wake may hang the waiter, but avoids potential UB.
                                debug_assert!(
                                    false,
                                    "Pinned local waiter {waiter:?} has invalid worker id {worker_id}"
                                );
                                error!(
                                    ?waiter,
                                    worker_id,
                                    "execute: pinned local waiter has invalid worker id, wake skipped"
                                );
                            }
                        } else {
                            // Local task without a pinned worker yet.
                            // Schedule on the current worker's local queue.
                            self.local_ready.lock().push_back(waiter);
                            self.parker.unpark();
                        }
                    } else {
                        // Global waiters are ready tasks.
                        global_tasks.push((waiter, waiter_priority));
                    }
                }
            }
        }
        let global_wakes = global_tasks.len();
        if global_wakes > 0 {
            // Increment the counter BEFORE pushing tasks to prevent concurrent stealers
            // from falsely seeing an empty queue and failing to decrement the counter.
            self.global.add_ready_count(global_wakes);
            for (task, priority) in global_tasks {
                self.global.inject_ready_uncounted(task, priority);
            }
            self.coordinator.wake_many(global_wakes);
        }
    }

    #[allow(clippy::too_many_lines)]
    pub(crate) fn execute(&self, task_id: TaskId) {
        // Guard to handle unwinds that escape the explicit poll isolation below
        // before the runtime clears the current task context.
        struct TaskExecutionGuard<'a> {
            worker: &'a ThreeLaneWorker,
            task_id: TaskId,
            completed: bool,
        }

        impl Drop for TaskExecutionGuard<'_> {
            #[allow(clippy::significant_drop_tightening)] // false positive: guard still borrowed by wake_dependents_locked
            fn drop(&mut self) {
                if !self.completed && std::thread::panicking() {
                    // 1. Mark task as Panicked (using hot-path task table if available)
                    self.worker.with_task_table(|tt| {
                        if let Some(record) = tt.task_mut(self.task_id) {
                            if !record.state.is_terminal() {
                                record.complete(crate::types::Outcome::Panicked(
                                    crate::types::outcome::PanicPayload::new(
                                        "task panicked during poll",
                                    ),
                                ));
                            }
                        }
                    });

                    // 2. Wake waiters and process finalizers (requires full RuntimeState lock)
                    // We expect success here; poisoning aborts the thread, which is acceptable during panic unwind.
                    let mut state = self
                        .worker
                        .state
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner);
                    let waiters = state.task_completed(self.task_id);
                    let finalizers = state.drain_ready_async_finalizers();

                    self.worker.wake_dependents_locked(&state, waiters);

                    let finalizer_wakes = finalizers.len();
                    if finalizer_wakes > 0 {
                        self.worker.global.add_ready_count(finalizer_wakes);
                        for (finalizer_task, priority) in finalizers {
                            self.worker
                                .global
                                .inject_ready_uncounted(finalizer_task, priority);
                        }
                        self.worker.coordinator.wake_many(finalizer_wakes);
                    }
                }
            }
        }

        trace!(task_id = ?task_id, worker_id = self.id, "executing task");

        let (
            mut stored,
            wake_state,
            priority,
            task_cx,
            cx_inner,
            cached_waker,
            cached_cancel_waker,
        ) = {
            // Fast path: single lock for global tasks (remove stored future + read record).
            let merged = self.with_task_table(|tt| {
                let global_stored = tt.remove_stored_future(task_id)?;
                let record = tt.task_mut(task_id)?;
                record.start_running();
                record.wake_state.begin_poll();
                let priority = record.sched_priority;
                let wake_state = Arc::clone(&record.wake_state);
                // Preserve full Cx so scheduler sets CURRENT_CX during poll.
                let task_cx = record.cx.clone();
                let cached_waker = record.cached_waker.take();
                let cached_cancel_waker = record.cached_cancel_waker.take();
                // Skip cx_inner Arc clone when both wakers are cached with correct
                // priority. Saves one atomic inc+dec per poll on the hot path.
                // finish_poll() re-loads from the task table if needed (rare).
                let both_cached = cached_waker.is_some()
                    && cached_cancel_waker
                        .as_ref()
                        .is_some_and(|(_, p)| *p == priority);
                let cx_inner = if both_cached {
                    None
                } else {
                    record.cx_inner.clone()
                };
                Some((
                    AnyStoredTask::Global(global_stored),
                    wake_state,
                    priority,
                    task_cx,
                    cx_inner,
                    cached_waker,
                    cached_cancel_waker,
                ))
            });

            if let Some(result) = merged {
                result
            } else {
                // Slow path: local task (stored in TLS, not in global TaskTable).
                let local = crate::runtime::local::remove_local_task(task_id);
                let Some(local) = local else {
                    return;
                };
                let record_info = self.with_task_table(|tt| {
                    let record = tt.task_mut(task_id)?;
                    record.start_running();
                    record.wake_state.begin_poll();
                    let priority = record.sched_priority;
                    let wake_state = Arc::clone(&record.wake_state);
                    // Preserve full Cx so scheduler sets CURRENT_CX during poll.
                    let task_cx = record.cx.clone();
                    let cached_waker = record.cached_waker.take();
                    let cached_cancel_waker = record.cached_cancel_waker.take();
                    let both_cached = cached_waker.is_some()
                        && cached_cancel_waker
                            .as_ref()
                            .is_some_and(|(_, p)| *p == priority);
                    let cx_inner = if both_cached {
                        None
                    } else {
                        record.cx_inner.clone()
                    };
                    Some((
                        wake_state,
                        priority,
                        task_cx,
                        cx_inner,
                        cached_waker,
                        cached_cancel_waker,
                    ))
                });
                let Some((
                    wake_state,
                    priority,
                    task_cx,
                    cx_inner,
                    cached_waker,
                    cached_cancel_waker,
                )) = record_info
                else {
                    return;
                };
                (
                    AnyStoredTask::Local(local),
                    wake_state,
                    priority,
                    task_cx,
                    cx_inner,
                    cached_waker,
                    cached_cancel_waker,
                )
            }
        };

        let is_local = stored.is_local();

        // Reuse cached waker (wakers are now dynamic, so priority check is not needed for correctness,
        // but we still store it in the record).
        let waker = if let Some((w, _)) = cached_waker {
            w
        } else {
            let inner = cx_inner.as_ref().expect("cx_inner missing");
            let fast_cancel = Arc::clone(&inner.read().fast_cancel);
            let weak_inner = Arc::downgrade(inner);
            if is_local {
                Waker::from(Arc::new(ThreeLaneLocalWaker {
                    task_id,
                    priority,
                    wake_state: Arc::clone(&wake_state),
                    local: Arc::clone(&self.local),
                    local_ready: Arc::clone(&self.local_ready),
                    parker: self.parker.clone(),
                    fast_cancel,
                    cx_inner: weak_inner,
                }))
            } else {
                Waker::from(Arc::new(ThreeLaneWaker {
                    task_id,
                    wake_state: Arc::clone(&wake_state),
                    global: Arc::clone(&self.global),
                    coordinator: Arc::clone(&self.coordinator),
                    priority,
                    fast_cancel,
                    cx_inner: weak_inner,
                }))
            }
        };
        // Create/reuse cancel waker.
        // Fast path: when cached with matching priority, skip cx_inner entirely
        // (cx_inner may be None because we skipped the Arc clone above).
        let cancel_waker_for_cache = if cached_cancel_waker
            .as_ref()
            .is_some_and(|(_, p)| *p == priority)
        {
            // Cancel waker cached with correct priority. No cx_inner needed.
            cached_cancel_waker.map(|(w, _)| (w, priority))
        } else {
            // Cache miss: build new cancel waker. cx_inner was cloned above.
            cx_inner.as_ref().map(|inner| {
                let w = if is_local {
                    Waker::from(Arc::new(ThreeLaneLocalCancelWaker {
                        task_id,
                        default_priority: priority,
                        wake_state: Arc::clone(&wake_state),
                        local: Arc::clone(&self.local),
                        local_ready: Arc::clone(&self.local_ready),
                        parker: self.parker.clone(),
                        cx_inner: Arc::downgrade(inner),
                    }))
                } else {
                    Waker::from(Arc::new(CancelLaneWaker {
                        task_id,
                        default_priority: priority,
                        wake_state: Arc::clone(&wake_state),
                        global: Arc::clone(&self.global),
                        coordinator: Arc::clone(&self.coordinator),
                        cx_inner: Arc::downgrade(inner),
                    }))
                };
                // New waker: register in CxInner (single write lock).
                {
                    let mut guard = inner.write();
                    let needs_update = !guard
                        .cancel_waker
                        .as_ref()
                        .is_some_and(|existing| existing.will_wake(&w));
                    if needs_update {
                        guard.cancel_waker = Some(w.clone());
                    }
                }
                (w, priority)
            })
        };
        // Install the task context BEFORE creating TaskExecutionGuard so
        // that during panic unwind, TaskExecutionGuard::drop runs first
        // (while Cx is still installed), then _cx_guard is dropped.  This
        // matches the ordering in worker.rs and ensures any cleanup code
        // in the guard's drop can access Cx::current().
        let _cx_guard = crate::cx::Cx::set_current(task_cx);
        let mut guard = TaskExecutionGuard {
            worker: self,
            task_id,
            completed: false,
        };

        let poll_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let mut cx = Context::from_waker(&waker);
            stored.poll(&mut cx)
        }));

        match poll_result {
            Ok(Poll::Ready(outcome)) => {
                // Map Outcome<(), ()> to Outcome<(), Error> for record.complete()
                let task_outcome = outcome
                    .map_err(|()| crate::error::Error::new(crate::error::ErrorKind::Internal));
                let mut state = self
                    .state
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner);
                let cancel_ack = Self::consume_cancel_ack_locked(&mut state, task_id);
                if let Some(record) = state.task_mut(task_id) {
                    if !record.state.is_terminal() {
                        let mut completed_via_cancel = false;
                        if matches!(task_outcome, crate::types::Outcome::Ok(())) {
                            let should_cancel = matches!(
                                record.state,
                                crate::record::task::TaskState::Cancelling { .. }
                                    | crate::record::task::TaskState::Finalizing { .. }
                            ) || (cancel_ack
                                && matches!(
                                    record.state,
                                    crate::record::task::TaskState::CancelRequested { .. }
                                ));
                            if should_cancel {
                                if matches!(
                                    record.state,
                                    crate::record::task::TaskState::CancelRequested { .. }
                                ) {
                                    let _ = record.acknowledge_cancel();
                                }
                                if matches!(
                                    record.state,
                                    crate::record::task::TaskState::Cancelling { .. }
                                ) {
                                    record.cleanup_done();
                                }
                                if matches!(
                                    record.state,
                                    crate::record::task::TaskState::Finalizing { .. }
                                ) {
                                    record.finalize_done();
                                }
                                completed_via_cancel = matches!(
                                    record.state,
                                    crate::record::task::TaskState::Completed(
                                        crate::types::Outcome::Cancelled(_)
                                    )
                                );
                            }
                        }
                        if !completed_via_cancel {
                            record.complete(task_outcome);
                        }
                    }
                }

                let waiters = state.task_completed(task_id);
                let finalizers = state.drain_ready_async_finalizers();

                self.wake_dependents_locked(&state, waiters);

                let finalizer_wakes = finalizers.len();
                if finalizer_wakes > 0 {
                    self.global.add_ready_count(finalizer_wakes);
                    for (finalizer_task, priority) in finalizers {
                        self.global.inject_ready_uncounted(finalizer_task, priority);
                    }
                    self.coordinator.wake_many(finalizer_wakes);
                }
                drop(state);
                guard.completed = true;
                wake_state.clear();
            }
            Ok(Poll::Pending) => {
                // Store task back: use task table for hot-path when sharded.
                // Move waker into cache (not clone) since it is not needed after this point.
                // Store task back and cache wakers in a single lock acquisition.
                // Also inline consume_cancel_ack with read-first optimization
                // to eliminate the separate third lock acquisition on the Pending path.
                match stored {
                    AnyStoredTask::Global(t) => {
                        self.with_task_table(move |tt| {
                            tt.store_spawned_task(task_id, t);
                            if let Some(record) = tt.task_mut(task_id) {
                                record.cached_waker = Some((waker, priority));
                                record.cached_cancel_waker = cancel_waker_for_cache;
                                // Inline cancel-ack: read-first to avoid write lock
                                // when cancel_acknowledged is false (the common case).
                                if let Some(inner) = record.cx_inner.as_ref() {
                                    let needs_ack = inner.read().cancel_acknowledged;
                                    if needs_ack {
                                        let mut g = inner.write();
                                        if g.cancel_acknowledged {
                                            g.cancel_acknowledged = false;
                                            drop(g);
                                            let _ = record.acknowledge_cancel();
                                        }
                                    }
                                }
                            }
                        });
                    }
                    AnyStoredTask::Local(t) => {
                        crate::runtime::local::store_local_task(task_id, t);
                        // For local tasks, we also want to cache wakers in the global record
                        // (since record is global).
                        self.with_task_table(move |tt| {
                            if let Some(record) = tt.task_mut(task_id) {
                                record.cached_waker = Some((waker, priority));
                                record.cached_cancel_waker = cancel_waker_for_cache;
                                // Inline cancel-ack: read-first (same as global path above).
                                if let Some(inner) = record.cx_inner.as_ref() {
                                    let needs_ack = inner.read().cancel_acknowledged;
                                    if needs_ack {
                                        let mut g = inner.write();
                                        if g.cancel_acknowledged {
                                            g.cancel_acknowledged = false;
                                            drop(g);
                                            let _ = record.acknowledge_cancel();
                                        }
                                    }
                                }
                            }
                        });
                    }
                }

                if wake_state.finish_poll() {
                    let mut cancel_priority = priority;
                    let mut schedule_cancel = false;
                    // cx_inner may be None if we skipped the Arc clone (both wakers
                    // were cached). Re-load from task table on this rare path.
                    let cx_inner_for_finish = if cx_inner.is_some() {
                        cx_inner
                    } else {
                        self.with_task_table(|tt| tt.task(task_id).and_then(|r| r.cx_inner.clone()))
                    };
                    if let Some(inner) = cx_inner_for_finish.as_ref() {
                        let guard = inner.read();
                        if guard.cancel_requested {
                            schedule_cancel = true;
                            if let Some(reason) = guard.cancel_reason.as_ref() {
                                cancel_priority = reason.cleanup_budget().priority;
                            }
                        }
                    }

                    if is_local {
                        if schedule_cancel {
                            // Cancel still goes to PriorityScheduler for ordering.
                            // Cancel lane is not stolen by steal_ready_batch_into.
                            let mut local_ready_guard = self.local_ready.lock();
                            if let Some(pos) = local_ready_guard.iter().position(|t| *t == task_id)
                            {
                                local_ready_guard.remove(pos);
                            }
                            drop(local_ready_guard);
                            let mut local = self.local.lock();
                            local.schedule_cancel(task_id, cancel_priority);
                        } else {
                            // Push to non-stealable local_ready queue.
                            // Local (!Send) tasks must never enter stealable structures.
                            self.local_ready.lock().push_back(task_id);
                        }
                        self.parker.unpark();
                    } else {
                        // Schedule to global injector
                        if schedule_cancel {
                            self.global.inject_cancel(task_id, cancel_priority);
                        } else {
                            self.global.inject_ready(task_id, priority);
                        }
                        self.coordinator.wake_one();
                    }
                }

                guard.completed = true;
            }
            Err(payload) => {
                let panic_payload = crate::types::outcome::PanicPayload::new(
                    crate::cx::scope::payload_to_string(&payload),
                );
                let mut state = self
                    .state
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner);
                let _cancel_ack = Self::consume_cancel_ack_locked(&mut state, task_id);
                if let Some(record) = state.task_mut(task_id) {
                    if !record.state.is_terminal() {
                        record.complete(crate::types::Outcome::Panicked(panic_payload));
                    }
                }

                let waiters = state.task_completed(task_id);
                let finalizers = state.drain_ready_async_finalizers();

                self.wake_dependents_locked(&state, waiters);

                let finalizer_wakes = finalizers.len();
                if finalizer_wakes > 0 {
                    self.global.add_ready_count(finalizer_wakes);
                    for (finalizer_task, priority) in finalizers {
                        self.global.inject_ready_uncounted(finalizer_task, priority);
                    }
                    self.coordinator.wake_many(finalizer_wakes);
                }
                drop(state);
                guard.completed = true;
                wake_state.clear();
            }
        }
        let _ = guard.completed;
    }

    fn schedule_ready_finalizers(&self) -> bool {
        let tasks = {
            let mut state = self
                .state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            state.drain_ready_async_finalizers()
        };
        if tasks.is_empty() {
            return false;
        }
        let finalizer_wakes = tasks.len();
        if finalizer_wakes > 0 {
            self.global.add_ready_count(finalizer_wakes);
            for (task_id, priority) in tasks {
                self.global.inject_ready_uncounted(task_id, priority);
            }
            self.coordinator.wake_many(finalizer_wakes);
        }
        true
    }

    /// Consumes a cancel acknowledgement using the task table shard when available.
    ///
    /// This is the hot-path variant used in Poll::Pending where only task record
    /// access is needed.
    #[allow(dead_code)] // Used in scheduler dispatch + tests
    fn consume_cancel_ack(&self, task_id: TaskId) -> bool {
        self.with_task_table(|tt| Self::consume_cancel_ack_from_table(tt, task_id))
    }

    fn consume_cancel_ack_locked(state: &mut RuntimeState, task_id: TaskId) -> bool {
        Self::consume_cancel_ack_from_table(&mut state.tasks, task_id)
    }

    fn consume_cancel_ack_from_table(tt: &mut TaskTable, task_id: TaskId) -> bool {
        let Some(record) = tt.task_mut(task_id) else {
            return false;
        };
        let Some(inner) = record.cx_inner.as_ref() else {
            return false;
        };
        // Read-first: skip the write lock when cancel_acknowledged is false
        // (the common case). Only upgrade to write when the flag is set.
        if !inner.read().cancel_acknowledged {
            return false;
        }
        let mut guard = inner.write();
        if guard.cancel_acknowledged {
            guard.cancel_acknowledged = false;
            drop(guard);
            let _ = record.acknowledge_cancel();
            return true;
        }
        false
    }
}

struct ThreeLaneWaker {
    task_id: TaskId,
    wake_state: Arc<crate::record::task::TaskWakeState>,
    global: Arc<GlobalInjector>,
    coordinator: Arc<WorkerCoordinator>,
    /// Cached priority to avoid `Weak::upgrade` + `RwLock::read` on every wake.
    /// Safe because `budget.priority` is immutable after task creation.
    priority: u8,
    fast_cancel: std::sync::Arc<std::sync::atomic::AtomicBool>,
    cx_inner: Weak<RwLock<CxInner>>,
}

impl ThreeLaneWaker {
    #[inline]
    fn schedule(&self) {
        if self.wake_state.notify() {
            // Check for cancellation to route to correct lane (cancel > ready).
            // This ensures "Losers are drained" with high priority even during I/O wakeups.
            let mut priority = self.priority;
            // Pair with the Release store in `CxInner::fast_cancel` so a wake
            // that observes cancellation also observes the published reason.
            let is_cancelling = self.fast_cancel.load(Ordering::Acquire);

            if is_cancelling {
                if let Some(inner) = self.cx_inner.upgrade() {
                    let guard = inner.read();
                    if let Some(reason) = &guard.cancel_reason {
                        priority = reason.cleanup_budget().priority;
                    }
                }
            }

            if is_cancelling {
                self.global.inject_cancel(self.task_id, priority);
            } else {
                self.global.inject_ready(self.task_id, priority);
            }
            self.coordinator.wake_one();
        }
    }
}

use std::task::Wake;
impl Wake for ThreeLaneWaker {
    #[inline]
    fn wake(self: Arc<Self>) {
        self.schedule();
    }

    #[inline]
    fn wake_by_ref(self: &Arc<Self>) {
        self.schedule();
    }
}

struct ThreeLaneLocalWaker {
    task_id: TaskId,
    /// Cached priority so cancelled local tasks fall back to their base
    /// priority instead of 0 when `cancel_reason` is not yet set.
    priority: u8,
    wake_state: Arc<crate::record::task::TaskWakeState>,
    local: Arc<Mutex<PriorityScheduler>>,
    local_ready: Arc<LocalReadyQueue>,
    parker: Parker,
    fast_cancel: std::sync::Arc<std::sync::atomic::AtomicBool>,
    cx_inner: Weak<RwLock<CxInner>>,
}

impl ThreeLaneLocalWaker {
    #[inline]
    fn schedule(&self) {
        if self.wake_state.notify() {
            // Pair with the Release store in `CxInner::fast_cancel` so the
            // local wake path sees cancellation publication before routing.
            let is_cancelling = self.fast_cancel.load(Ordering::Acquire);

            if is_cancelling {
                let mut priority = self.priority;
                if let Some(inner) = self.cx_inner.upgrade() {
                    let guard = inner.read();
                    if let Some(reason) = &guard.cancel_reason {
                        priority = reason.cleanup_budget().priority;
                    }
                }
                // Promote to the local cancel lane, matching `inject_cancel`
                // and `schedule_local_cancel`: a cancelled local task must not
                // remain in the non-stealable ready queue.
                let mut local_ready_guard = self.local_ready.lock();
                if let Some(pos) = local_ready_guard.iter().position(|t| *t == self.task_id) {
                    local_ready_guard.remove(pos);
                }
                drop(local_ready_guard);
                let mut local = self.local.lock();
                local.move_to_cancel_lane(self.task_id, priority);
            } else {
                // Push to non-stealable local_ready queue.
                self.local_ready.lock().push_back(self.task_id);
            }
            self.parker.unpark();
        }
    }
}

impl Wake for ThreeLaneLocalWaker {
    #[inline]
    fn wake(self: Arc<Self>) {
        self.schedule();
    }

    #[inline]
    fn wake_by_ref(self: &Arc<Self>) {
        self.schedule();
    }
}

struct CancelLaneWaker {
    task_id: TaskId,
    default_priority: u8,
    wake_state: Arc<crate::record::task::TaskWakeState>,
    global: Arc<GlobalInjector>,
    coordinator: Arc<WorkerCoordinator>,
    cx_inner: Weak<RwLock<CxInner>>,
}

impl CancelLaneWaker {
    #[inline]
    fn schedule(&self) {
        let Some(inner) = self.cx_inner.upgrade() else {
            return;
        };
        let (cancel_requested, priority) = {
            let guard = inner.read();
            let priority = guard
                .cancel_reason
                .as_ref()
                .map_or(self.default_priority, |reason| {
                    reason.cleanup_budget().priority
                });
            (guard.cancel_requested, priority)
        };

        if !cancel_requested {
            return;
        }

        // Always notify (attempt state transition)
        self.wake_state.notify();

        // Always inject to ensure priority promotion, even if already scheduled.
        // See `inject_cancel` for details.
        self.global.inject_cancel(self.task_id, priority);
        self.coordinator.wake_one();
    }
}

impl Wake for CancelLaneWaker {
    #[inline]
    fn wake(self: Arc<Self>) {
        self.schedule();
    }

    #[inline]
    fn wake_by_ref(self: &Arc<Self>) {
        self.schedule();
    }
}

struct ThreeLaneLocalCancelWaker {
    task_id: TaskId,
    default_priority: u8,
    wake_state: Arc<crate::record::task::TaskWakeState>,
    local: Arc<Mutex<PriorityScheduler>>,
    local_ready: Arc<LocalReadyQueue>,
    parker: Parker,
    cx_inner: Weak<RwLock<CxInner>>,
}

impl ThreeLaneLocalCancelWaker {
    #[inline]
    fn schedule(&self) {
        let Some(inner) = self.cx_inner.upgrade() else {
            return;
        };
        let (cancel_requested, priority) = {
            let guard = inner.read();
            let priority = guard
                .cancel_reason
                .as_ref()
                .map_or(self.default_priority, |reason| {
                    reason.cleanup_budget().priority
                });
            (guard.cancel_requested, priority)
        };

        if !cancel_requested {
            return;
        }

        // Always notify
        self.wake_state.notify();

        // Promote to local cancel lane, matching global inject_cancel semantics.
        // move_to_cancel_lane relocates from ready/timed if already scheduled.
        {
            let mut local_ready_guard = self.local_ready.lock();
            if let Some(pos) = local_ready_guard.iter().position(|t| *t == self.task_id) {
                local_ready_guard.remove(pos);
            }
            drop(local_ready_guard);
            let mut local = self.local.lock();
            local.move_to_cancel_lane(self.task_id, priority);
        }
        self.parker.unpark();
    }
}

impl Wake for ThreeLaneLocalCancelWaker {
    #[inline]
    fn wake(self: Arc<Self>) {
        self.schedule();
    }

    #[inline]
    fn wake_by_ref(self: &Arc<Self>) {
        self.schedule();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::record::task::TaskWakeState;
    use crate::runtime::scheduler::invariant_monitor;
    use crate::runtime::scheduler::{InvariantCategory, SchedulerInvariant};
    use crate::time::{TimerDriverHandle, VirtualClock};
    use crate::types::{Budget, CancelKind, CancelReason, CxInner, RegionId, TaskId};
    use parking_lot::RwLock;
    use serde_json::{Value, json};
    use std::time::Duration;

    #[derive(Default)]
    struct TaskIdScrubber {
        labels: BTreeMap<TaskId, String>,
        next: usize,
    }

    impl TaskIdScrubber {
        fn label(&mut self, task_id: TaskId) -> String {
            if let Some(label) = self.labels.get(&task_id) {
                return label.clone();
            }

            let label = format!("[TASK_{}]", self.next);
            self.next += 1;
            self.labels.insert(task_id, label.clone());
            label
        }
    }

    fn scrubbed_tracked_tasks(
        scrubber: &mut TaskIdScrubber,
        tracked_tasks: Vec<invariant_monitor::TrackedTaskSnapshot>,
    ) -> Vec<Value> {
        let mut tracked_tasks = tracked_tasks;
        tracked_tasks.sort_by_key(|snapshot| snapshot.task_id);
        tracked_tasks
            .into_iter()
            .map(|snapshot| {
                json!({
                    "task_id": scrubber.label(snapshot.task_id),
                    "queues": snapshot.queues,
                    "priority": snapshot.priority,
                    "enqueue_time_ns": snapshot.enqueue_time.as_nanos(),
                    "last_update_ns": snapshot.last_update.as_nanos(),
                    "lifecycle_state": snapshot.lifecycle_state,
                    "owner_worker": snapshot.owner_worker,
                    "is_cancelled": snapshot.is_cancelled,
                })
            })
            .collect()
    }

    fn worker_state_dump_scrubbed(
        scenario: &str,
        worker: &ThreeLaneWorker,
        dispatch_sequence: &[TaskId],
    ) -> Value {
        let mut scrubber = TaskIdScrubber::default();
        let local_ready_tasks: Vec<_> = worker.local_ready.lock().iter().copied().collect();
        let local_scheduler_depth = worker.local.lock().len();
        let tracked_tasks = worker.invariant_monitor.lock().tracked_tasks();
        let invariant_stats = worker.invariant_stats();
        let starvation_stats = worker.starvation_stats();
        let certificate = worker.preemption_fairness_certificate();
        let metrics = worker.preemption_metrics();
        let adaptive_policy = worker.adaptive_cancel_policy.as_ref().map(|policy| {
            json!({
                "selected_arm": policy.selected_arm,
                "current_limit": policy.current_limit(),
                "epoch_steps": policy.epoch_steps,
                "steps_in_epoch": policy.steps_in_epoch,
                "epoch_count": policy.epoch_count,
                "reward_ema": policy.reward_ema,
                "e_process_log": policy.e_process_log,
                "weights": policy.weights,
                "probs": policy.probs,
                "pulls": policy.pulls,
            })
        });

        json!({
            "scenario": scenario,
            "worker_id": worker.id,
            "cancel_streak": worker.cancel_streak,
            "cancel_streak_limit": worker.cancel_streak_limit,
            "ready_count": worker.ready_count(),
            "lane_depths": {
                "local_priority_scheduler": local_scheduler_depth,
                "local_ready": local_ready_tasks.len(),
                "fast_queue": worker.fast_queue.len(),
                "global_pending": worker.global.len(),
                "global_ready": worker.global.ready_count(),
                "global_has_cancel": worker.global.has_cancel_work(),
                "global_has_timed": worker.global.has_timed_work(),
                "global_has_ready": worker.global.has_ready_work(),
            },
            "local_ready_tasks": local_ready_tasks
                .into_iter()
                .map(|task_id| scrubber.label(task_id))
                .collect::<Vec<_>>(),
            "dispatch_sequence": dispatch_sequence
                .iter()
                .copied()
                .map(|task_id| scrubber.label(task_id))
                .collect::<Vec<_>>(),
            "tracked_tasks": scrubbed_tracked_tasks(&mut scrubber, tracked_tasks),
            "fairness_certificate": {
                "base_limit": certificate.base_limit,
                "effective_limit": certificate.effective_limit,
                "observed_max_cancel_streak": certificate.observed_max_cancel_streak,
                "cancel_dispatches": certificate.cancel_dispatches,
                "timed_dispatches": certificate.timed_dispatches,
                "ready_dispatches": certificate.ready_dispatches,
                "fairness_yields": certificate.fairness_yields,
                "observed_max_ready_stall_steps": certificate.observed_max_ready_stall_steps,
                "observed_max_timed_stall_steps": certificate.observed_max_timed_stall_steps,
                "ready_priority_inversions": certificate.ready_priority_inversions,
                "max_ready_priority_inversion_gap": certificate.max_ready_priority_inversion_gap,
                "fallback_cancel_dispatches": certificate.fallback_cancel_dispatches,
                "base_limit_exceedances": certificate.base_limit_exceedances,
                "effective_limit_exceedances": certificate.effective_limit_exceedances,
                "adaptive_enabled": certificate.adaptive_enabled,
                "adaptive_current_limit": certificate.adaptive_current_limit,
                "ready_stall_bound_steps": certificate.ready_stall_bound_steps(),
                "observed_non_cancel_stall_steps": certificate.observed_non_cancel_stall_steps(),
                "invariant_holds": certificate.invariant_holds(),
                "witness_hash": certificate.witness_hash(),
            },
            "preemption_metrics": {
                "cancel_dispatches": metrics.cancel_dispatches,
                "timed_dispatches": metrics.timed_dispatches,
                "ready_dispatches": metrics.ready_dispatches,
                "fairness_yields": metrics.fairness_yields,
                "max_cancel_streak": metrics.max_cancel_streak,
                "max_ready_dispatch_stall": metrics.max_ready_dispatch_stall,
                "max_timed_dispatch_stall": metrics.max_timed_dispatch_stall,
                "fallback_cancel_dispatches": metrics.fallback_cancel_dispatches,
                "base_limit_exceedances": metrics.base_limit_exceedances,
                "effective_limit_exceedances": metrics.effective_limit_exceedances,
                "adaptive_epochs": metrics.adaptive_epochs,
                "adaptive_current_limit": metrics.adaptive_current_limit,
                "adaptive_reward_ema": metrics.adaptive_reward_ema,
                "adaptive_e_value": metrics.adaptive_e_value,
            },
            "invariant_stats": {
                "operations_monitored": invariant_stats.operations_monitored,
                "avg_monitoring_overhead_ns": invariant_stats.avg_monitoring_overhead_ns,
                "monitored_workers": invariant_stats.monitored_workers,
                "violations_by_severity": invariant_stats.violations_by_severity,
            },
            "starvation_stats": {
                "total_starvation_events": starvation_stats.total_starvation_events,
                "currently_starved_tasks": starvation_stats.currently_starved_tasks,
                "max_task_wait_time_ns": starvation_stats.max_task_wait_time_ns,
                "avg_task_wait_time_ns": starvation_stats.avg_task_wait_time_ns,
                "total_priority_inversions": starvation_stats.total_priority_inversions,
                "tracked_tasks_count": starvation_stats.tracked_tasks_count,
                "pattern_detected": starvation_stats.pattern_detected,
                "total_tracked_wait_time_ns": starvation_stats.total_tracked_wait_time_ns,
                "max_priority_inversion_gap": starvation_stats.max_priority_inversion_gap,
            },
            "adaptive_policy": adaptive_policy,
        })
    }

    fn empty_scheduler_state_dump() -> Value {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];
        worker.verify_scheduler_invariants();
        worker_state_dump_scrubbed("empty", worker, &[])
    }

    fn loaded_scheduler_state_dump() -> Value {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];

        worker.schedule_local(TaskId::new_for_test(100, 1), 40);
        worker.schedule_local_cancel(TaskId::new_for_test(101, 1), 90);
        worker.schedule_local_timed(TaskId::new_for_test(102, 1), Time::from_nanos(5_000));
        worker.verify_scheduler_invariants();

        worker_state_dump_scrubbed("loaded", worker, &[])
    }

    fn cancel_streak_scheduler_state_dump() -> Value {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, 2);
        let worker = &mut scheduler.workers[0];

        worker.schedule_local(TaskId::new_for_test(200, 1), 25);
        for task_index in 0..5 {
            worker.schedule_local_cancel(TaskId::new_for_test(210 + task_index, 1), 100);
        }

        let mut dispatch_sequence = Vec::new();
        for _ in 0..6 {
            if let Some(task_id) = worker.next_task() {
                dispatch_sequence.push(task_id);
            }
        }
        worker.verify_scheduler_invariants();

        worker_state_dump_scrubbed("cancel_streak", worker, &dispatch_sequence)
    }

    #[test]
    fn test_three_lane_scheduler_creation() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let scheduler = ThreeLaneScheduler::new(2, &state);

        assert!(!scheduler.is_shutdown());
        assert_eq!(scheduler.workers.len(), 2);
    }

    #[test]
    fn test_initial_local_scheduler_capacity_scales_with_worker_count() {
        assert_eq!(
            ThreeLaneScheduler::initial_local_scheduler_capacity(0),
            1024
        );
        assert_eq!(
            ThreeLaneScheduler::initial_local_scheduler_capacity(1),
            1024
        );
        assert_eq!(
            ThreeLaneScheduler::initial_local_scheduler_capacity(2),
            1024
        );
        assert_eq!(ThreeLaneScheduler::initial_local_scheduler_capacity(4), 512);
        assert_eq!(ThreeLaneScheduler::initial_local_scheduler_capacity(8), 256);
        assert_eq!(
            ThreeLaneScheduler::initial_local_scheduler_capacity(64),
            128
        );
    }

    #[test]
    fn select_backoff_deadline_follower_uses_local_only() {
        let timer_deadline = Some(Time::from_nanos(100));
        let local_deadline = Some(Time::from_nanos(400));
        let global_deadline = Some(Time::from_nanos(200));

        let selected = select_backoff_deadline(
            IoPhaseOutcome::Follower,
            timer_deadline,
            local_deadline,
            global_deadline,
        );

        assert_eq!(
            selected, local_deadline,
            "follower must ignore shared deadlines and honor only local deadline"
        );
    }

    #[test]
    fn select_backoff_deadline_follower_without_local_deadline_stays_none() {
        let selected = select_backoff_deadline(
            IoPhaseOutcome::Follower,
            Some(Time::from_nanos(100)),
            None,
            Some(Time::from_nanos(200)),
        );

        assert_eq!(
            selected, None,
            "follower should not arm timeout wakeups for non-local deadlines"
        );
    }

    #[test]
    fn select_backoff_deadline_non_follower_uses_earliest_deadline() {
        let timer_deadline = Some(Time::from_nanos(500));
        let local_deadline = Some(Time::from_nanos(300));
        let global_deadline = Some(Time::from_nanos(100));

        let selected = select_backoff_deadline(
            IoPhaseOutcome::NoProgress,
            timer_deadline,
            local_deadline,
            global_deadline,
        );

        assert_eq!(
            selected, global_deadline,
            "leader/no-io path should continue using earliest deadline across all sources"
        );
    }

    #[test]
    fn backoff_metrics_count_follower_shared_deadline_ignores() {
        let mut metrics = PreemptionMetrics::default();
        record_backoff_deadline_selection(
            &mut metrics,
            IoPhaseOutcome::Follower,
            Some(Time::from_nanos(100)),
            Some(Time::from_nanos(200)),
        );
        assert_eq!(metrics.follower_shared_deadline_ignored, 1);

        // Non-follower paths should not increment follower-only suppression counters.
        record_backoff_deadline_selection(
            &mut metrics,
            IoPhaseOutcome::NoProgress,
            Some(Time::from_nanos(100)),
            Some(Time::from_nanos(200)),
        );
        assert_eq!(metrics.follower_shared_deadline_ignored, 1);
    }

    #[test]
    fn backoff_metrics_count_follower_without_shared_deadlines_is_noop() {
        let mut metrics = PreemptionMetrics::default();
        record_backoff_deadline_selection(&mut metrics, IoPhaseOutcome::Follower, None, None);
        assert_eq!(
            metrics.follower_shared_deadline_ignored, 0,
            "follower should only count suppressions when a shared deadline was present"
        );
    }

    #[test]
    fn backoff_metrics_count_short_waits_and_follower_timeout_parks() {
        let mut metrics = PreemptionMetrics::default();
        record_backoff_timeout_park(&mut metrics, IoPhaseOutcome::Follower, 4_000_000);
        record_backoff_timeout_park(&mut metrics, IoPhaseOutcome::NoProgress, 6_000_000);

        assert_eq!(metrics.backoff_parks_total, 2);
        assert_eq!(metrics.backoff_timeout_parks_total, 2);
        assert_eq!(metrics.backoff_timeout_nanos_total, 10_000_000);
        assert_eq!(metrics.short_wait_le_5ms, 1);
        assert_eq!(metrics.follower_timeout_parks, 1);
    }

    #[test]
    fn backoff_metrics_count_short_wait_threshold_is_inclusive() {
        let mut metrics = PreemptionMetrics::default();
        record_backoff_timeout_park(
            &mut metrics,
            IoPhaseOutcome::Follower,
            SHORT_WAIT_LE_5MS_NANOS,
        );
        assert_eq!(
            metrics.short_wait_le_5ms, 1,
            "<= 5ms threshold should include exactly 5ms"
        );
    }

    #[test]
    fn classify_backoff_timeout_decision_handles_due_short_and_long_waits() {
        let now = Time::from_nanos(1_000);

        let due = classify_backoff_timeout_decision(IoPhaseOutcome::Follower, now, now);
        assert_eq!(due, BackoffTimeoutDecision::DeadlineDue);

        // Sub-5ms follower timeouts now park instead of skipping (BUG-S1 fix).
        let short_follower = classify_backoff_timeout_decision(
            IoPhaseOutcome::Follower,
            Time::from_nanos(1_000 + 4_000_000),
            now,
        );
        assert_eq!(
            short_follower,
            BackoffTimeoutDecision::ParkTimeout { nanos: 4_000_000 }
        );

        let threshold_follower = classify_backoff_timeout_decision(
            IoPhaseOutcome::Follower,
            Time::from_nanos(1_000 + SHORT_WAIT_LE_5MS_NANOS),
            now,
        );
        assert_eq!(
            threshold_follower,
            BackoffTimeoutDecision::ParkTimeout {
                nanos: SHORT_WAIT_LE_5MS_NANOS
            }
        );

        let long_follower = classify_backoff_timeout_decision(
            IoPhaseOutcome::Follower,
            Time::from_nanos(1_000 + 6_000_000),
            now,
        );
        assert_eq!(
            long_follower,
            BackoffTimeoutDecision::ParkTimeout { nanos: 6_000_000 }
        );

        let short_leader = classify_backoff_timeout_decision(
            IoPhaseOutcome::NoProgress,
            Time::from_nanos(1_000 + 4_000_000),
            now,
        );
        assert_eq!(
            short_leader,
            BackoffTimeoutDecision::ParkTimeout { nanos: 4_000_000 }
        );
    }

    #[test]
    fn backoff_metrics_count_indefinite_parks() {
        let mut metrics = PreemptionMetrics::default();
        record_backoff_indefinite_park(&mut metrics, IoPhaseOutcome::Follower);
        record_backoff_indefinite_park(&mut metrics, IoPhaseOutcome::NoProgress);

        assert_eq!(metrics.backoff_parks_total, 2);
        assert_eq!(metrics.backoff_indefinite_parks, 2);
        assert_eq!(metrics.follower_indefinite_parks, 1);
    }

    #[test]
    fn preemption_metrics_backoff_summary_helpers_handle_zero_denominators() {
        let metrics = PreemptionMetrics::default();
        assert_eq!(metrics.avg_timeout_park_nanos(), 0);
        assert_eq!(metrics.short_wait_ratio_bps(), 0);
        assert_eq!(metrics.follower_short_wait_avoidance_bps(), 0);
    }

    #[test]
    fn preemption_metrics_backoff_summary_helpers_compute_expected_values() {
        let metrics = PreemptionMetrics {
            backoff_timeout_parks_total: 4,
            backoff_timeout_nanos_total: 20,
            short_wait_le_5ms: 2,
            follower_short_wait_skip_le_5ms: 3,
            follower_timeout_parks: 1,
            ..PreemptionMetrics::default()
        };

        assert_eq!(metrics.avg_timeout_park_nanos(), 5);
        assert_eq!(metrics.short_wait_ratio_bps(), 5_000);
        assert_eq!(metrics.follower_short_wait_avoidance_bps(), 7_500);
    }

    #[test]
    fn test_three_lane_worker_shutdown() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);

        let workers = scheduler.take_workers();
        assert_eq!(workers.len(), 2);

        // Spawn threads for workers
        let handles: Vec<_> = workers
            .into_iter()
            .map(|mut worker| {
                std::thread::spawn(move || {
                    worker.run_loop();
                })
            })
            .collect();

        // Let them run briefly
        std::thread::sleep(Duration::from_millis(10));

        // Signal shutdown
        scheduler.shutdown();

        // Join threads
        for handle in handles {
            handle.join().unwrap();
        }
    }

    #[test]
    fn test_cancel_priority_over_ready() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject ready first, then cancel
        scheduler.inject_ready(TaskId::new_for_test(1, 1), 100);
        scheduler.inject_cancel(TaskId::new_for_test(1, 2), 50);

        // Worker should get cancel first
        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // Cancel should come first
        let task1 = worker.try_cancel_work();
        assert!(task1.is_some());
        assert_eq!(task1.unwrap(), TaskId::new_for_test(1, 2));

        // Ready should come after
        let task2 = worker.try_ready_work();
        assert!(task2.is_some());
        assert_eq!(task2.unwrap(), TaskId::new_for_test(1, 1));
    }

    #[test]
    fn test_cancel_lane_fairness_limit() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, 2);

        let cancel_tasks = [
            TaskId::new_for_test(1, 1),
            TaskId::new_for_test(1, 2),
            TaskId::new_for_test(1, 3),
        ];
        let ready_task = TaskId::new_for_test(1, 4);

        for &task_id in &cancel_tasks {
            scheduler.inject_cancel(task_id, 100);
        }
        scheduler.inject_ready(ready_task, 50);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        let first = worker.next_task().expect("first dispatch");
        let second = worker.next_task().expect("second dispatch");
        let third = worker.next_task().expect("third dispatch");
        let fourth = worker.next_task().expect("fourth dispatch");

        assert!(cancel_tasks.contains(&first));
        assert!(cancel_tasks.contains(&second));
        assert_eq!(third, ready_task);
        assert!(cancel_tasks.contains(&fourth));
    }

    #[test]
    fn test_local_cancel_lane_fairness_limit() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, 2);

        let cancel_tasks = [
            TaskId::new_for_test(1, 11),
            TaskId::new_for_test(1, 12),
            TaskId::new_for_test(1, 13),
        ];
        let ready_task = TaskId::new_for_test(1, 14);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        {
            let mut local = worker.local.lock();
            for &task_id in &cancel_tasks {
                local.schedule_cancel(task_id, 100);
            }
            local.schedule(ready_task, 50);
        }

        let first = worker.next_task().expect("first dispatch");
        let second = worker.next_task().expect("second dispatch");
        let third = worker.next_task().expect("third dispatch");
        let fourth = worker.next_task().expect("fourth dispatch");

        assert!(cancel_tasks.contains(&first));
        assert!(cancel_tasks.contains(&second));
        assert_eq!(third, ready_task);
        assert!(cancel_tasks.contains(&fourth));
    }

    #[test]
    fn test_stealing_only_from_ready_lane() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);

        // Add cancel and ready work to worker 0's local queue
        {
            let workers = &scheduler.workers;
            let mut local0 = workers[0].local.lock();
            local0.schedule_cancel(TaskId::new_for_test(1, 1), 100);
            local0.schedule(TaskId::new_for_test(1, 2), 50);
            local0.schedule(TaskId::new_for_test(1, 3), 50);
        }

        // Worker 1 should only be able to steal ready work
        let mut workers = scheduler.take_workers().into_iter();
        let _ = workers.next().unwrap(); // Skip worker 0
        let mut thief_worker = workers.next().unwrap();

        // Stealing should only get ready tasks
        let stolen = thief_worker.try_steal();
        assert!(stolen.is_some());

        // The stolen task should be from ready lane (2 or 3)
        let stolen_id = stolen.unwrap();
        assert!(
            stolen_id == TaskId::new_for_test(1, 2) || stolen_id == TaskId::new_for_test(1, 3),
            "Expected ready task, got cancel task"
        );
    }

    #[test]
    fn execute_completes_task_and_schedules_waiter() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            task_id
        };
        let waiter_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (waiter_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            waiter_id
        };

        {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if let Some(record) = guard.task_mut(task_id) {
                record.add_waiter(waiter_id);
            }
        }

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = scheduler.take_workers().into_iter().next().unwrap();

        worker.execute(task_id);

        let completed = state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .task(task_id)
            .is_none();
        assert!(completed, "task should be removed after completion");

        let scheduled_task = worker.global.pop_ready().map(|pt| pt.task);
        assert_eq!(scheduled_task, Some(waiter_id));
    }

    #[test]
    fn test_try_timed_work_checks_deadline() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with virtual clock timer driver
        let clock = Arc::new(VirtualClock::new());
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock.clone()));
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject a timed task with deadline at t=1000ns
        let task_id = TaskId::new_for_test(1, 1);
        let deadline = Time::from_nanos(1000);
        scheduler.inject_timed(task_id, deadline);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // At t=0, the task should NOT be ready (deadline not yet due)
        // try_timed_work should re-inject the task
        let result = worker.try_timed_work();
        assert!(result.is_none(), "task should not be ready before deadline");

        // Advance clock past deadline
        clock.advance(2000); // t=2000ns, past deadline of 1000ns

        // Now the task should be ready
        let result = worker.try_timed_work();
        assert_eq!(result, Some(task_id), "task should be ready after deadline");
    }

    #[test]
    fn test_worker_has_timer_driver_from_state() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with timer driver
        let clock = Arc::new(VirtualClock::new());
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock.clone()));
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Worker should have timer driver
        assert!(
            worker.timer_driver.is_some(),
            "worker should have timer driver from state"
        );

        // Timer driver should use the same clock
        let timer = worker.timer_driver.as_ref().unwrap();
        assert_eq!(timer.now(), Time::ZERO, "timer should start at zero");

        clock.advance(1000);
        assert_eq!(
            timer.now(),
            Time::from_nanos(1000),
            "timer should reflect clock advance"
        );
    }

    #[test]
    fn test_scheduler_timer_driver_propagates_to_workers() {
        // State without timer driver
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);

        // Workers should not have timer driver
        let workers = scheduler.take_workers();
        assert!(workers[0].timer_driver.is_none());
        assert!(workers[1].timer_driver.is_none());

        // Scheduler should not have timer driver
        assert!(scheduler.timer_driver.is_none());
    }

    #[test]
    fn test_run_once_processes_timers() {
        use crate::time::{TimerDriverHandle, VirtualClock};
        use std::sync::atomic::AtomicBool;
        use std::task::Waker;

        // Waker that sets a flag when woken
        struct TestWaker(AtomicBool);
        impl Wake for TestWaker {
            fn wake(self: Arc<Self>) {
                self.0.store(true, Ordering::SeqCst);
            }
        }

        // Create state with virtual clock timer driver
        let clock = Arc::new(VirtualClock::new());
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock.clone()));
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Get timer driver to register a timer
        let timer_driver = scheduler.timer_driver.as_ref().unwrap().clone();

        // Register a timer that expires at t=500ns
        let waker_flag = Arc::new(TestWaker(AtomicBool::new(false)));
        let waker = Waker::from(waker_flag.clone());
        let _handle = timer_driver.register(Time::from_nanos(500), waker);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // Timer should not be fired at t=0
        assert!(!waker_flag.0.load(Ordering::SeqCst));

        // run_once should process timers but not fire (deadline not reached)
        worker.run_once();
        assert!(
            !waker_flag.0.load(Ordering::SeqCst),
            "timer should not fire before deadline"
        );

        // Advance clock past deadline
        clock.advance(1000);

        // run_once should now fire the timer
        worker.run_once();
        assert!(
            waker_flag.0.load(Ordering::SeqCst),
            "timer should fire after deadline"
        );
    }

    #[test]
    fn test_timed_work_not_due_stays_in_queue() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with virtual clock timer driver
        let clock = Arc::new(VirtualClock::new());
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock));
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject a timed task with deadline at t=1000ns
        let task_id = TaskId::new_for_test(1, 1);
        let deadline = Time::from_nanos(1000);
        scheduler.inject_timed(task_id, deadline);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // At t=0, task is not ready - stays in queue (not popped)
        let result = worker.try_timed_work();
        assert!(result.is_none());

        // The task should still be in the global queue (was never removed)
        let peeked = worker.global.pop_timed();
        assert!(peeked.is_some(), "task should remain in global queue");
        assert_eq!(peeked.unwrap().task, task_id);
    }

    #[test]
    fn test_edf_ordering_from_global_queue() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with virtual clock timer driver at t=1000
        let clock = Arc::new(VirtualClock::starting_at(Time::from_nanos(1000)));
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock));
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject timed tasks with different deadlines (all due, since t=1000)
        let task1 = TaskId::new_for_test(1, 1);
        let task2 = TaskId::new_for_test(1, 2);
        let task3 = TaskId::new_for_test(1, 3);

        // Insert in non-deadline order
        scheduler.inject_timed(task2, Time::from_nanos(500)); // deadline 500
        scheduler.inject_timed(task3, Time::from_nanos(750)); // deadline 750
        scheduler.inject_timed(task1, Time::from_nanos(250)); // deadline 250

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // All deadlines are due (t=1000), so should be returned in EDF order
        let first = worker.try_timed_work();
        assert_eq!(
            first,
            Some(task1),
            "earliest deadline (250) should be first"
        );

        let second = worker.try_timed_work();
        assert_eq!(
            second,
            Some(task2),
            "second earliest deadline (500) should be second"
        );

        let third = worker.try_timed_work();
        assert_eq!(
            third,
            Some(task3),
            "third earliest deadline (750) should be third"
        );
    }

    #[test]
    fn test_starvation_avoidance_ready_with_timed() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with virtual clock at t=0
        let clock = Arc::new(VirtualClock::new());
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock));
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject a ready task
        let ready_task = TaskId::new_for_test(1, 1);
        scheduler.inject_ready(ready_task, 100);

        // Inject a timed task with future deadline
        let timed_task = TaskId::new_for_test(1, 2);
        scheduler.inject_timed(timed_task, Time::from_nanos(1000));

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // Timed task has future deadline, so should not be returned
        assert!(worker.try_timed_work().is_none());

        // Ready task should be available
        assert_eq!(worker.try_ready_work(), Some(ready_task));
    }

    #[test]
    fn test_cancel_priority_over_timed() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with virtual clock at t=1000 (both tasks due)
        let clock = Arc::new(VirtualClock::starting_at(Time::from_nanos(1000)));
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock));
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject a timed task
        let timed_task = TaskId::new_for_test(1, 1);
        scheduler.inject_timed(timed_task, Time::from_nanos(500));

        // Inject a cancel task (lower priority number, but cancel lane has priority)
        let cancel_task = TaskId::new_for_test(1, 2);
        scheduler.inject_cancel(cancel_task, 50);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // Cancel work should come before timed work
        assert_eq!(worker.try_cancel_work(), Some(cancel_task));

        // Then timed work
        assert_eq!(worker.try_timed_work(), Some(timed_task));
    }

    #[test]
    fn cancel_waker_injects_cancel_lane() {
        let task_id = TaskId::new_for_test(1, 1);
        let cx_inner = Arc::new(RwLock::new(CxInner::new(
            RegionId::new_for_test(1, 0),
            task_id,
            Budget::INFINITE,
        )));
        {
            let mut guard = cx_inner.write();
            guard.cancel_requested = true;
            guard
                .fast_cancel
                .store(true, std::sync::atomic::Ordering::Release);
            guard.cancel_reason = Some(CancelReason::timeout());
        }

        let wake_state = Arc::new(crate::record::task::TaskWakeState::new());
        let global = Arc::new(GlobalInjector::new());
        let parker = Parker::new();
        let coordinator = Arc::new(WorkerCoordinator::new(vec![parker], None));
        let waker = Waker::from(Arc::new(CancelLaneWaker {
            task_id,
            default_priority: Budget::INFINITE.priority,
            wake_state,
            global: Arc::clone(&global),
            coordinator,
            cx_inner: Arc::downgrade(&cx_inner),
        }));

        waker.wake_by_ref();

        let task = global.pop_cancel().map(|pt| pt.task);
        assert_eq!(task, Some(task_id));
    }

    #[test]
    fn ordinary_waker_observes_fast_cancel_and_injects_cancel_lane() {
        let task_id = TaskId::new_for_test(1, 7);
        let cx_inner = Arc::new(RwLock::new(CxInner::new(
            RegionId::new_for_test(1, 0),
            task_id,
            Budget::INFINITE,
        )));
        {
            let mut guard = cx_inner.write();
            guard.cancel_requested = true;
            guard
                .fast_cancel
                .store(true, std::sync::atomic::Ordering::Release);
            guard.cancel_reason = Some(CancelReason::timeout());
        }

        let wake_state = Arc::new(crate::record::task::TaskWakeState::new());
        let global = Arc::new(GlobalInjector::new());
        let parker = Parker::new();
        let coordinator = Arc::new(WorkerCoordinator::new(vec![parker], None));
        let waker = Waker::from(Arc::new(ThreeLaneWaker {
            task_id,
            wake_state,
            global: Arc::clone(&global),
            coordinator,
            priority: Budget::INFINITE.priority,
            fast_cancel: Arc::clone(&cx_inner.read().fast_cancel),
            cx_inner: Arc::downgrade(&cx_inner),
        }));

        waker.wake_by_ref();

        let task = global.pop_cancel().map(|pt| pt.task);
        assert_eq!(task, Some(task_id));
        assert!(
            global.pop_ready().is_none(),
            "cancelled task should not be re-enqueued in ready lane"
        );
    }

    // ========== Deduplication Tests (bd-35f9) ==========

    #[test]
    fn test_inject_ready_dedup_prevents_double_schedule() {
        // Create state with a real task record
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            task_id
        };

        let scheduler = ThreeLaneScheduler::new(1, &state);

        // First inject should succeed
        scheduler.inject_ready(task_id, 100);
        assert!(
            scheduler.global.has_ready_work(),
            "first inject should add to queue"
        );

        // Second inject should be deduplicated (same task)
        scheduler.inject_ready(task_id, 100);

        // Pop first - should succeed
        let first = scheduler.global.pop_ready();
        assert!(first.is_some(), "first pop should succeed");
        assert_eq!(first.unwrap().task, task_id);

        // Second pop should fail - task was deduplicated
        let second = scheduler.global.pop_ready();
        assert!(second.is_none(), "second pop should fail (deduplicated)");
    }

    #[test]
    fn test_inject_cancel_allows_duplicates_for_priority() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            task_id
        };

        let scheduler = ThreeLaneScheduler::new(1, &state);

        // First inject to cancel lane
        scheduler.inject_cancel(task_id, 100);
        assert!(scheduler.global.has_cancel_work());

        // Second inject should NOT be deduplicated (to ensure priority promotion)
        scheduler.inject_cancel(task_id, 100);

        // Both should be in queue
        let first = scheduler.global.pop_cancel();
        assert!(first.is_some());
        let second = scheduler.global.pop_cancel();
        assert!(second.is_some(), "cancel inject always injects");

        // Third check should be empty
        let third = scheduler.global.pop_cancel();
        assert!(third.is_none());
    }

    #[test]
    fn test_inject_cancel_promotes_ready_task() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            task_id
        };

        let scheduler = ThreeLaneScheduler::new(1, &state);

        // 1. Schedule task in Ready Lane
        scheduler.inject_ready(task_id, 50);
        assert!(scheduler.global.has_ready_work());
        assert!(!scheduler.global.has_cancel_work());

        // 2. Inject cancel for same task
        // Expected: Should be promoted to Cancel Lane
        scheduler.inject_cancel(task_id, 100);

        // 3. Verify it is now in Cancel Lane (possibly in addition to Ready Lane)
        assert!(
            scheduler.global.has_cancel_work(),
            "Task should be promoted to cancel lane"
        );
    }

    #[test]
    fn test_spawn_local_not_stolen() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);

        let mut worker_pool = scheduler.take_workers();
        let local_ready_0 = Arc::clone(&worker_pool[0].local_ready);
        let mut stealer_worker = worker_pool.pop().unwrap(); // worker 1 as mutable for try_steal

        let task_id = TaskId::new_for_test(1, 0);

        // Simulate worker 0 environment and schedule local task
        {
            let _guard = ScopedLocalReady::new(Arc::clone(&local_ready_0));
            assert!(
                schedule_local_task(task_id),
                "schedule_local_task should succeed"
            );
        }

        // Verify task is in worker 0's local_ready queue
        {
            let queue = local_ready_0.lock();
            assert_eq!(queue.len(), 1);
            assert_eq!(queue[0], task_id);
            drop(queue);
        }

        // Worker 1 tries to steal. It should NOT find the task because
        // it only steals from PriorityScheduler and fast_queue, not local_ready.
        let stolen = stealer_worker.try_steal();
        assert!(stolen.is_none(), "Local task should not be stolen");
    }

    #[test]
    fn test_local_cancel_removes_from_local_ready() {
        let task_id = TaskId::new_for_test(1, 0);
        let local_ready = Arc::new(LocalReadyQueue::new(VecDeque::from([task_id])));
        let local = Arc::new(Mutex::new(PriorityScheduler::new()));
        let wake_state = Arc::new(TaskWakeState::new());
        let cx_inner = Arc::new(RwLock::new(CxInner::new(
            RegionId::new_for_test(1, 0),
            task_id,
            Budget::INFINITE,
        )));
        {
            let mut guard = cx_inner.write();
            guard.cancel_requested = true;
            guard
                .fast_cancel
                .store(true, std::sync::atomic::Ordering::Release);
            guard.cancel_reason = Some(CancelReason::new(CancelKind::User));
        }

        let waker = ThreeLaneLocalCancelWaker {
            task_id,
            default_priority: 10,
            wake_state: Arc::clone(&wake_state),
            local: Arc::clone(&local),
            local_ready: Arc::clone(&local_ready),
            parker: Parker::new(),
            cx_inner: Arc::downgrade(&cx_inner),
        };

        waker.schedule();

        let queue = local_ready.lock();
        assert!(
            !queue.contains(&task_id),
            "local_ready should not retain cancelled task"
        );
        drop(queue);

        assert!(
            local.lock().is_in_cancel_lane(task_id),
            "task should be promoted to cancel lane"
        );
    }

    #[test]
    fn ordinary_local_waker_promotes_cancelled_task_out_of_local_ready() {
        let task_id = TaskId::new_for_test(1, 3);
        let local_ready = Arc::new(LocalReadyQueue::new(VecDeque::from([task_id])));
        let local = Arc::new(Mutex::new(PriorityScheduler::new()));
        let wake_state = Arc::new(TaskWakeState::new());
        let cx_inner = Arc::new(RwLock::new(CxInner::new(
            RegionId::new_for_test(1, 0),
            task_id,
            Budget::INFINITE,
        )));
        {
            let mut guard = cx_inner.write();
            guard.cancel_requested = true;
            guard
                .fast_cancel
                .store(true, std::sync::atomic::Ordering::Release);
            guard.cancel_reason = Some(CancelReason::new(CancelKind::User));
        }

        let waker = ThreeLaneLocalWaker {
            task_id,
            priority: 10,
            wake_state,
            local: Arc::clone(&local),
            local_ready: Arc::clone(&local_ready),
            parker: Parker::new(),
            fast_cancel: Arc::clone(&cx_inner.read().fast_cancel),
            cx_inner: Arc::downgrade(&cx_inner),
        };

        waker.schedule();

        let queue = local_ready.lock();
        assert!(
            !queue.contains(&task_id),
            "cancelled local task should be removed from local_ready"
        );
        drop(queue);

        assert!(
            local.lock().is_in_cancel_lane(task_id),
            "cancelled local task should be promoted to cancel lane"
        );
    }

    #[test]
    fn schedule_cancel_on_current_local_removes_local_ready() {
        let task_id = TaskId::new_for_test(1, 0);
        let local_ready = Arc::new(LocalReadyQueue::new(VecDeque::from([task_id])));
        let local = Arc::new(Mutex::new(PriorityScheduler::new()));

        let _local_ready_guard = ScopedLocalReady::new(Arc::clone(&local_ready));
        let _local_guard = ScopedLocalScheduler::new(Arc::clone(&local));

        let scheduled = schedule_cancel_on_current_local(task_id, 7);
        assert!(scheduled, "should schedule via current local scheduler");

        let queue = local_ready.lock();
        assert!(
            !queue.contains(&task_id),
            "local_ready should not retain cancelled task"
        );
        drop(queue);

        assert!(
            local.lock().is_in_cancel_lane(task_id),
            "task should be promoted to cancel lane"
        );
    }

    #[test]
    fn test_schedule_local_dedup_prevents_double_schedule() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            task_id
        };

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // First schedule to local
        worker.schedule_local(task_id, 100);

        // Second schedule should be deduplicated
        worker.schedule_local(task_id, 100);

        // Check local queue has only one entry
        let count = {
            let local = worker.local.lock();
            local.len()
        };
        assert_eq!(count, 1, "should have exactly 1 task, not {count}");
    }

    #[test]
    fn test_schedule_local_rejects_local_task() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            let record = guard.task_mut(task_id).expect("task record missing");
            record.mark_local();
            drop(guard);
            task_id
        };

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        worker.schedule_local(task_id, 100);

        let popped = worker.local.lock().pop_ready_only();
        assert!(popped.is_none(), "local task must not enter ready lane");
        assert!(
            !worker.local_ready.lock().contains(&task_id),
            "schedule_local must not route local tasks"
        );
    }

    #[test]
    fn test_schedule_local_timed_rejects_local_task() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            let record = guard.task_mut(task_id).expect("task record missing");
            record.mark_local();
            drop(guard);
            task_id
        };

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        worker.schedule_local_timed(task_id, Time::from_nanos(42));

        let popped = worker.local.lock().pop_timed_only(Time::from_nanos(100));
        assert!(popped.is_none(), "local task must not enter timed lane");
        assert!(
            !worker.local_ready.lock().contains(&task_id),
            "schedule_local_timed must not route local tasks"
        );
    }

    #[test]
    fn test_local_then_global_dedup() {
        // Test: schedule locally first, then try to inject globally
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            task_id
        };

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Schedule locally first (consumes the notify)
        worker.schedule_local(task_id, 100);

        // Now try global inject - should be deduplicated
        scheduler.global.inject_ready(task_id, 100);
        // Note: We're injecting directly to global to simulate the race

        // But since wake_state was consumed by local, subsequent inject
        // via the scheduler method would be blocked
        // The task is only in local queue
        let local_len = {
            let local = worker.local.lock();
            local.len()
        };
        assert_eq!(local_len, 1);
    }

    #[test]
    fn test_multiple_wakes_single_schedule() {
        // Simulate the ThreeLaneWaker behavior
        let task_id = TaskId::new_for_test(1, 1);
        let wake_state = Arc::new(crate::record::task::TaskWakeState::new());
        let global = Arc::new(GlobalInjector::new());
        let parker = Parker::new();
        let coordinator = Arc::new(WorkerCoordinator::new(vec![parker], None));

        // Create multiple wakers (simulating cloned wakers)
        let wakers: Vec<_> = (0..10)
            .map(|_| {
                Waker::from(Arc::new(ThreeLaneWaker {
                    task_id,
                    wake_state: Arc::clone(&wake_state),
                    global: Arc::clone(&global),
                    coordinator: Arc::clone(&coordinator),
                    priority: 0,
                    fast_cancel: Arc::new(std::sync::atomic::AtomicBool::new(false)),
                    cx_inner: Weak::new(),
                }))
            })
            .collect();

        // Wake all 10 wakers
        for waker in &wakers {
            waker.wake_by_ref();
        }

        // Only one task should be in the queue
        let first = global.pop_ready();
        assert!(first.is_some(), "at least one wake should succeed");

        let second = global.pop_ready();
        assert!(
            second.is_none(),
            "only one wake should succeed, dedup should prevent duplicates"
        );
    }

    #[test]
    fn test_wake_state_cleared_allows_reschedule() {
        // After task completes, wake_state is cleared, allowing new schedule
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (task_id, _handle) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            task_id
        };

        // Get the wake_state for direct manipulation
        let wake_state = {
            let guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            guard
                .task(task_id)
                .map(|r| Arc::clone(&r.wake_state))
                .expect("task should exist")
        };

        let scheduler = ThreeLaneScheduler::new(1, &state);

        // First schedule
        scheduler.inject_ready(task_id, 100);
        let first = scheduler.global.pop_ready();
        assert!(first.is_some());

        // Clear wake state (simulating task completion)
        wake_state.clear();

        // Now should be able to schedule again
        scheduler.inject_ready(task_id, 100);
        let second = scheduler.global.pop_ready();
        assert!(second.is_some(), "should be able to reschedule after clear");
    }

    // ========== Stress Tests ==========
    // These tests are marked #[ignore] for CI and should be run manually.

    #[test]
    #[ignore = "stress test; run manually"]
    fn stress_test_parker_high_contention() {
        use crate::runtime::scheduler::worker::Parker;
        use std::sync::atomic::AtomicUsize;
        use std::thread;

        // 50 threads, 1000 park/unpark cycles each
        let parker = Arc::new(Parker::new());
        let successful_wakes = Arc::new(AtomicUsize::new(0));
        let iterations = 1000;
        let thread_count = 50;

        let handles: Vec<_> = (0..thread_count)
            .map(|i| {
                let p = parker.clone();
                let wakes = successful_wakes.clone();
                thread::spawn(move || {
                    for j in 0..iterations {
                        if i % 2 == 0 {
                            // Parker thread
                            p.park_timeout(Duration::from_millis(10));
                            wakes.fetch_add(1, Ordering::Relaxed);
                        } else {
                            // Unparker thread
                            p.unpark();
                            if j % 10 == 0 {
                                thread::yield_now();
                            }
                        }
                    }
                })
            })
            .collect();

        for h in handles {
            h.join().expect("thread should not panic");
        }

        let total_wakes = successful_wakes.load(Ordering::Relaxed);
        assert!(
            total_wakes > 0,
            "at least some threads should have woken up"
        );
    }

    #[test]
    #[ignore = "stress test; run manually"]
    fn stress_test_scheduler_inject_while_parking() {
        // Race: inject work between empty check and park
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let scheduler = Arc::new(ThreeLaneScheduler::new(4, &state));
        let injected = Arc::new(AtomicUsize::new(0));
        let executed = Arc::new(AtomicUsize::new(0));
        let barrier = Arc::new(std::sync::Barrier::new(21)); // 20 injectors + 1 main

        // 20 injector threads
        let inject_handles: Vec<_> = (0..20)
            .map(|t| {
                let s = scheduler.clone();
                let inj = injected.clone();
                let b = barrier.clone();
                std::thread::spawn(move || {
                    b.wait();
                    for i in 0..5000 {
                        let task = TaskId::new_for_test(t * 10000 + i, 0);
                        s.inject_ready(task, 50);
                        inj.fetch_add(1, Ordering::Relaxed);
                    }
                })
            })
            .collect();

        barrier.wait();

        // Let injectors run
        std::thread::sleep(Duration::from_millis(100));

        // Drain the queue
        let exec = executed.clone();
        loop {
            if scheduler.global.pop_ready().is_some() {
                exec.fetch_add(1, Ordering::Relaxed);
            } else {
                break;
            }
        }

        for h in inject_handles {
            h.join().expect("injector should complete");
        }

        // Final drain
        while scheduler.global.pop_ready().is_some() {
            executed.fetch_add(1, Ordering::Relaxed);
        }

        let total_injected = injected.load(Ordering::Relaxed);
        let total_executed = executed.load(Ordering::Relaxed);

        // Due to dedup, executed may be less than injected if same task IDs were used
        // But we should have at least executed something
        assert!(
            total_executed > 0,
            "should have executed some tasks, got {total_executed}"
        );
        assert!(
            total_injected >= total_executed,
            "injected ({total_injected}) should be >= executed ({total_executed})"
        );
    }

    #[test]
    #[ignore = "stress test; run manually"]
    fn stress_test_work_stealing_fairness() {
        use crate::runtime::scheduler::priority::Scheduler as PriorityScheduler;

        // Unbalanced workload: 1 producer, 10 stealers
        let producer_queue = Arc::new(Mutex::new(PriorityScheduler::new()));
        let stolen_count = Arc::new(AtomicUsize::new(0));
        let barrier = Arc::new(std::sync::Barrier::new(12)); // 1 producer + 10 stealers + 1 main

        // Fill producer queue
        {
            let mut q = producer_queue.lock();
            for i in 0..10000 {
                q.schedule(TaskId::new_for_test(i, 0), 50);
            }
        }

        // 10 stealer threads
        let stealer_handles: Vec<_> = (0..10)
            .map(|_| {
                let q = producer_queue.clone();
                let stolen = stolen_count.clone();
                let b = barrier.clone();
                std::thread::spawn(move || {
                    b.wait();
                    let mut local_stolen = 0;
                    loop {
                        let task = {
                            let Some(mut guard) = q.try_lock() else {
                                continue;
                            };
                            let batch = guard.steal_ready_batch(4);
                            if batch.is_empty() {
                                None
                            } else {
                                Some(batch.len())
                            }
                        };

                        match task {
                            Some(count) => {
                                local_stolen += count;
                                std::thread::yield_now();
                            }
                            None => break,
                        }
                    }
                    stolen.fetch_add(local_stolen, Ordering::Relaxed);
                })
            })
            .collect();

        // Producer thread that keeps adding
        let q = producer_queue.clone();
        let b = barrier.clone();
        let producer = std::thread::spawn(move || {
            b.wait();
            for i in 10000..15000 {
                let mut guard = q.lock();
                guard.schedule(TaskId::new_for_test(i, 0), 50);
                drop(guard);
                std::thread::yield_now();
            }
        });

        barrier.wait();

        producer.join().expect("producer should complete");
        for h in stealer_handles {
            h.join().expect("stealer should complete");
        }

        // Drain remaining
        let mut remaining = 0;
        {
            let mut q = producer_queue.lock();
            while q.pop().is_some() {
                remaining += 1;
            }
        }

        let total_stolen = stolen_count.load(Ordering::Relaxed);
        let total = total_stolen + remaining;

        // Should have handled all 15000 tasks
        assert!(
            total >= 14000, // Allow some slack for race conditions
            "should handle most tasks, got {total}"
        );
    }

    #[test]
    #[ignore = "stress test; run manually"]
    fn stress_test_global_queue_contention() {
        // High contention: 50 spawners, single queue
        let global = Arc::new(GlobalInjector::new());
        let spawned = Arc::new(AtomicUsize::new(0));
        let consumed = Arc::new(AtomicUsize::new(0));
        let barrier = Arc::new(std::sync::Barrier::new(61)); // 50 spawners + 10 consumers + 1 main

        // 50 spawner threads
        let spawn_handles: Vec<_> = (0..50)
            .map(|t| {
                let g = global.clone();
                let s = spawned.clone();
                let b = barrier.clone();
                std::thread::spawn(move || {
                    b.wait();
                    for i in 0..2000 {
                        let task = TaskId::new_for_test(t * 100_000 + i, 0);
                        g.inject_ready(task, 50);
                        s.fetch_add(1, Ordering::Relaxed);
                    }
                })
            })
            .collect();

        // 10 consumer threads
        let consumer_handles: Vec<_> = (0..10)
            .map(|_| {
                let g = global.clone();
                let c = consumed.clone();
                let b = barrier.clone();
                std::thread::spawn(move || {
                    b.wait();
                    let mut local = 0;
                    let mut empty_streak = 0;
                    loop {
                        if g.pop_ready().is_some() {
                            local += 1;
                            empty_streak = 0;
                        } else {
                            empty_streak += 1;
                            if empty_streak > 1000 {
                                break;
                            }
                            std::thread::yield_now();
                        }
                    }
                    c.fetch_add(local, Ordering::Relaxed);
                })
            })
            .collect();

        barrier.wait();

        for h in spawn_handles {
            h.join().expect("spawner should complete");
        }

        // Give consumers time to drain
        std::thread::sleep(Duration::from_millis(100));

        for h in consumer_handles {
            h.join().expect("consumer should complete");
        }

        // Drain remaining
        while global.pop_ready().is_some() {
            consumed.fetch_add(1, Ordering::Relaxed);
        }

        let total_spawned = spawned.load(Ordering::Relaxed);
        let total_consumed = consumed.load(Ordering::Relaxed);

        assert_eq!(total_spawned, 100_000, "should spawn exactly 100k tasks");
        assert!(
            total_consumed >= 99_000, // Allow small slack
            "should consume most tasks, got {total_consumed}"
        );
    }

    #[test]
    fn test_round_robin_wakeup_distribution() {
        // Verify that wake_one distributes wakeups across workers
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let scheduler = ThreeLaneScheduler::new(4, &state);

        // Track which parkers have been woken
        // The next_wake counter starts at 0, so:
        // - Call 1: wakes parker 0 (idx=0 % 4 = 0), next_wake=1
        // - Call 2: wakes parker 1 (idx=1 % 4 = 1), next_wake=2
        // - Call 3: wakes parker 2 (idx=2 % 4 = 2), next_wake=3
        // - Call 4: wakes parker 3 (idx=3 % 4 = 3), next_wake=4
        // - Call 5: wakes parker 0 (idx=4 % 4 = 0), next_wake=5
        // etc.

        // Verify the next_wake counter increments correctly
        let initial = scheduler.coordinator.next_wake.load(Ordering::Relaxed);
        assert_eq!(initial, 0, "next_wake should start at 0");

        // Wake multiple times and verify counter advances
        for i in 0..8 {
            scheduler.wake_one();
            let current = scheduler.coordinator.next_wake.load(Ordering::Relaxed);
            assert_eq!(current, i + 1, "next_wake should increment on each wake");
        }

        // Final counter should be 8
        let final_val = scheduler.coordinator.next_wake.load(Ordering::Relaxed);
        assert_eq!(final_val, 8, "next_wake should be 8 after 8 wakes");

        // Verify round-robin distribution: 8 wakes across 4 workers = 2 per worker
        // (We can't directly verify which parker was woken, but the modulo math
        // guarantees even distribution over time)
    }

    // ========== WorkerCoordinator non-power-of-two tests (br-3narc.2.1) ==========

    #[test]
    fn test_coordinator_non_power_of_two_round_robin() {
        // 3 workers is non-power-of-two, so mask = None and modulo is used.
        let parkers: Vec<Parker> = (0..3).map(|_| Parker::new()).collect();
        let coordinator = WorkerCoordinator::new(parkers, None);

        // mask should be None for non-power-of-two count
        assert!(
            coordinator.mask.is_none(),
            "3 workers should use modulo path, not bitmask"
        );

        // Verify round-robin visits all 3 workers cyclically:
        // idx=0 → 0%3=0, idx=1 → 1%3=1, idx=2 → 2%3=2,
        // idx=3 → 3%3=0, idx=4 → 4%3=1, idx=5 → 5%3=2
        for cycle in 0..3 {
            for expected_slot in 0..3 {
                let idx = coordinator.next_wake.load(Ordering::Relaxed);
                let slot = idx % 3;
                assert_eq!(
                    slot, expected_slot,
                    "cycle {cycle}, idx {idx} should wake slot {expected_slot}"
                );
                coordinator.wake_one();
            }
        }
    }

    #[test]
    fn test_coordinator_power_of_two_uses_bitmask() {
        // 4 workers is power-of-two, so mask = Some(3)
        let parkers: Vec<Parker> = (0..4).map(|_| Parker::new()).collect();
        let coordinator = WorkerCoordinator::new(parkers, None);

        assert_eq!(
            coordinator.mask,
            Some(3),
            "4 workers should use bitmask 0b11"
        );

        // Verify round-robin: idx & 3 == idx % 4 for small values
        for i in 0u64..8 {
            let idx = coordinator.next_wake.load(Ordering::Relaxed);
            assert_eq!(idx & 3, (i as usize) % 4);
            coordinator.wake_one();
        }
    }

    #[test]
    fn test_coordinator_single_worker() {
        let parkers = vec![Parker::new()];
        let coordinator = WorkerCoordinator::new(parkers, None);

        // 1 is power-of-two, mask = Some(0) → always wakes slot 0
        assert_eq!(coordinator.mask, Some(0));

        for _ in 0..10 {
            coordinator.wake_one();
        }
        // No panic = success (all wakes go to slot 0)
    }

    #[test]
    fn test_coordinator_zero_workers_is_noop() {
        let coordinator = WorkerCoordinator::new(vec![], None);
        assert!(coordinator.mask.is_none());
        // wake_one should be a no-op, not panic
        coordinator.wake_one();
        coordinator.wake_all();
    }

    // ========== Default cancel_streak_limit=16 fairness (br-3narc.2.1) ==========

    #[test]
    fn test_default_cancel_streak_limit_fairness() {
        // Verify that with the default limit (16), ready work is dispatched
        // after at most 16 consecutive cancel dispatches.
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject 20 cancel tasks and 1 ready task
        for i in 0..20 {
            scheduler.inject_cancel(TaskId::new_for_test(1, i), 100);
        }
        let ready_task = TaskId::new_for_test(1, 99);
        scheduler.inject_ready(ready_task, 50);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // Dispatch 21 tasks and find where the ready task appears
        let mut dispatch_order = Vec::new();
        for _ in 0..21 {
            if let Some(task) = worker.next_task() {
                dispatch_order.push(task);
            }
        }

        let ready_pos = dispatch_order
            .iter()
            .position(|t| *t == ready_task)
            .expect("ready task must be dispatched");

        // Ready task must appear within cancel_streak_limit + 1 = 17 positions
        assert!(
            ready_pos <= DEFAULT_CANCEL_STREAK_LIMIT,
            "ready task at position {ready_pos} must appear within \
             cancel_streak_limit ({DEFAULT_CANCEL_STREAK_LIMIT}) + 1 dispatches"
        );

        // Verify preemption metrics
        let metrics = worker.preemption_metrics();
        assert!(
            metrics.fairness_yields > 0,
            "should have fairness yields with 20 cancel + 1 ready"
        );
        assert!(
            metrics.max_cancel_streak <= DEFAULT_CANCEL_STREAK_LIMIT,
            "max cancel streak {} should not exceed default limit {}",
            metrics.max_cancel_streak,
            DEFAULT_CANCEL_STREAK_LIMIT
        );
    }

    // ========== Region close quiescence via RuntimeState (br-3narc.2.1) ==========

    #[test]
    fn test_region_quiescence_all_tasks_complete() {
        // Verify that the runtime state's is_quiescent correctly reflects
        // whether all tasks in all regions have completed.
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        // Create two tasks in the region
        let task_id1 = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            id
        };
        let task_id2 = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            id
        };

        // Not quiescent: 2 live tasks
        assert!(
            !state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .is_quiescent(),
            "should not be quiescent with live tasks"
        );

        // Execute task 1 via scheduler
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        scheduler.inject_ready(task_id1, 100);
        scheduler.inject_ready(task_id2, 100);

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Execute both tasks
        worker.execute(task_id1);
        worker.execute(task_id2);

        // After both tasks complete, the task table should be empty
        let guard = state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        assert!(
            guard.task(task_id1).is_none(),
            "task1 should be removed after completion"
        );
        assert!(
            guard.task(task_id2).is_none(),
            "task2 should be removed after completion"
        );
        drop(guard);
    }

    // ========== Governor Integration Tests (bd-2spm) ==========

    #[test]
    fn test_governor_disabled_returns_no_preference() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        assert!(worker.governor.is_none(), "default has no governor");
        let suggestion = worker.governor_suggest();
        assert_eq!(suggestion, SchedulingSuggestion::NoPreference);
    }

    #[test]
    fn test_governor_enabled_quiescent_returns_no_preference() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 1);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        assert!(worker.governor.is_some(), "governor enabled");
        let suggestion = worker.governor_suggest();
        assert_eq!(suggestion, SchedulingSuggestion::NoPreference);
    }

    #[test]
    fn test_governor_independent_live_tasks_do_not_force_drain_obligations() {
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::unlimited());
        let _ = state
            .create_task(root, Budget::unlimited(), async {})
            .expect("create task");
        let _ = state
            .create_task(root, Budget::unlimited(), async {})
            .expect("create task");
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 1);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let suggestion = worker.governor_suggest();
        assert_eq!(
            suggestion,
            SchedulingSuggestion::NoPreference,
            "independent live tasks should not be treated as a trapped wait deadlock"
        );
    }

    #[test]
    fn test_governor_single_live_task_without_wait_edges_skips_spectral_monitor() {
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::unlimited());
        let _ = state
            .create_task(root, Budget::unlimited(), async {})
            .expect("create task");
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 1);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let suggestion = worker.governor_suggest();
        assert_eq!(suggestion, SchedulingSuggestion::NoPreference);
        assert_eq!(
            worker
                .spectral_monitor
                .as_ref()
                .expect("governor should install spectral monitor")
                .history_len(),
            0,
            "benign singleton live-task states should not feed spectral history"
        );
    }

    #[test]
    fn test_governor_single_task_self_cycle_updates_spectral_monitor() {
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::unlimited());
        let (task_id, _handle) = state
            .create_task(root, Budget::unlimited(), async {})
            .expect("create task");
        state.task_mut(task_id).expect("task").waiters.push(task_id);
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 1);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let suggestion = worker.governor_suggest();
        assert_eq!(suggestion, SchedulingSuggestion::DrainObligations);
        assert_eq!(
            worker
                .spectral_monitor
                .as_ref()
                .expect("governor should install spectral monitor")
                .history_len(),
            1,
            "single-node trapped self-cycles should still update the spectral monitor"
        );
    }

    #[test]
    fn test_governor_meet_deadlines_dispatches_timed_first() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // State at t=999ms with a task having a 1s deadline.
        // Deadline pressure ≈ 0.999, dominating all other components.
        let clock = Arc::new(VirtualClock::starting_at(Time::from_nanos(999_000_000)));
        let mut state = RuntimeState::new();
        state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock));
        state.now = Time::from_nanos(999_000_000);
        let root = state.create_root_region(Budget::unlimited());
        let (_task_id, _handle) = state
            .create_task(root, Budget::with_deadline_ns(1_000_000_000), async {})
            .expect("create task");
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 1);

        // Inject a cancel task and an already-due timed task.
        let cancel_task = TaskId::new_for_test(1, 10);
        let timed_task = TaskId::new_for_test(1, 11);
        scheduler.inject_cancel(cancel_task, 100);
        scheduler.inject_timed(timed_task, Time::from_nanos(500_000_000));

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Under MeetDeadlines, timed work is dispatched before cancel.
        let first = worker.next_task();
        assert_eq!(
            first,
            Some(timed_task),
            "timed should be dispatched first under MeetDeadlines"
        );

        let second = worker.next_task();
        assert_eq!(
            second,
            Some(cancel_task),
            "cancel follows timed under MeetDeadlines"
        );
    }

    #[test]
    fn test_governor_drain_obligations_boosts_cancel_streak() {
        use crate::record::ObligationKind;

        // State with a pending obligation aged 1 second (high obligation component).
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::unlimited());
        let (task_id, _handle) = state
            .create_task(root, Budget::unlimited(), async {})
            .expect("create task");
        let _obl = state
            .create_obligation(ObligationKind::SendPermit, task_id, root, None)
            .expect("create obligation");
        state.now = Time::from_nanos(1_000_000_000); // 1s age
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        // Governor enabled, cancel_streak_limit=2, interval=1.
        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 2, true, 1);

        // Inject 4 cancel tasks and 1 ready task.
        let c1 = TaskId::new_for_test(1, 20);
        let c2 = TaskId::new_for_test(1, 21);
        let c3 = TaskId::new_for_test(1, 22);
        let c4 = TaskId::new_for_test(1, 23);
        let ready = TaskId::new_for_test(1, 24);
        scheduler.inject_cancel(c1, 100);
        scheduler.inject_cancel(c2, 100);
        scheduler.inject_cancel(c3, 100);
        scheduler.inject_cancel(c4, 100);
        scheduler.inject_ready(ready, 50);

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Under DrainObligations, cancel_streak_limit boosted to 4 (2×2).
        // All 4 cancel tasks should dispatch before ready.
        let dispatched: Vec<_> = (0..5).filter_map(|_| worker.next_task()).collect();
        assert_eq!(dispatched.len(), 5, "should dispatch all 5 tasks");

        let cancel_tasks = [c1, c2, c3, c4];
        for (i, &task) in dispatched.iter().take(4).enumerate() {
            assert!(
                cancel_tasks.contains(&task),
                "task {i} should be a cancel task, got {task:?}"
            );
        }
        assert_eq!(
            dispatched[4], ready,
            "ready task should come after all cancel tasks"
        );

        let cert = worker.preemption_fairness_certificate();
        assert_eq!(cert.base_limit, 2);
        assert_eq!(cert.effective_limit, 4);
        assert_eq!(cert.observed_max_cancel_streak, 4);
        assert!(
            cert.base_limit_exceedances > 0,
            "boosted mode should exceed base L while remaining within 2L"
        );
        assert_eq!(cert.effective_limit_exceedances, 0);
        assert!(cert.invariant_holds());
    }

    #[test]
    fn test_governor_interval_caches_suggestion() {
        // With interval=4, governor snapshots every 4th call.
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 4);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        assert_eq!(worker.steps_since_snapshot, 0);
        assert_eq!(worker.cached_suggestion, SchedulingSuggestion::NoPreference);

        // Calls 1–3 return cached suggestion without snapshotting.
        for i in 1..=3u32 {
            let s = worker.governor_suggest();
            assert_eq!(s, SchedulingSuggestion::NoPreference);
            assert_eq!(worker.steps_since_snapshot, i);
        }

        // Call 4 takes a snapshot and resets counter.
        let s = worker.governor_suggest();
        assert_eq!(s, SchedulingSuggestion::NoPreference); // quiescent
        assert_eq!(worker.steps_since_snapshot, 0);
    }

    #[test]
    fn test_governor_deterministic_across_workers() {
        use crate::record::ObligationKind;

        // All workers should produce the same suggestion for identical state.
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::unlimited());
        let (task_id, _handle) = state
            .create_task(root, Budget::unlimited(), async {})
            .expect("create task");
        let _obl = state
            .create_obligation(ObligationKind::SendPermit, task_id, root, None)
            .expect("create obligation");
        state.now = Time::from_nanos(2_000_000_000);
        let state = Arc::new(ContendedMutex::new("runtime_state", state));

        let mut scheduler = ThreeLaneScheduler::new_with_options(4, &state, 16, true, 1);
        let mut workers = scheduler.take_workers();

        let suggestions: Vec<_> = workers
            .iter_mut()
            .map(super::ThreeLaneWorker::governor_suggest)
            .collect();

        for s in &suggestions {
            assert_eq!(
                *s, suggestions[0],
                "all workers must agree on scheduling suggestion"
            );
        }
        // With old obligations and no deadlines/draining, should suggest DrainObligations.
        assert_eq!(suggestions[0], SchedulingSuggestion::DrainObligations);
    }

    fn ready_only_governor_scheduler(
        task_count: usize,
        chunk_pattern: &[usize],
    ) -> ThreeLaneScheduler {
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::unlimited());
        let tasks: Vec<_> = (0..task_count)
            .map(|_| {
                state
                    .create_task(root, Budget::unlimited(), async {})
                    .expect("create task")
                    .0
            })
            .collect();
        let state = Arc::new(ContendedMutex::new("runtime_state", state));
        let mut scheduler = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 1);

        let mut offset = 0usize;
        for &chunk in chunk_pattern {
            let end = offset.saturating_add(chunk).min(tasks.len());
            for &task_id in &tasks[offset..end] {
                scheduler.inject_ready(task_id, 100);
            }
            offset = end;
            if offset == tasks.len() {
                break;
            }
        }
        for &task_id in &tasks[offset..] {
            scheduler.inject_ready(task_id, 100);
        }

        scheduler
    }

    fn governor_total_potential(worker: &ThreeLaneWorker) -> f64 {
        let governor = worker.governor.as_ref().expect("governor enabled");
        let state = worker
            .state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let ready_depth = worker.local.lock().len();
        #[allow(clippy::cast_possible_truncation)]
        let snapshot =
            StateSnapshot::from_runtime_state(&state).with_ready_queue_depth(ready_depth as u32);
        governor.compute_record(&snapshot).total
    }

    fn collect_ready_drain_potentials(worker: &mut ThreeLaneWorker, dispatches: usize) -> Vec<f64> {
        let mut potentials = vec![governor_total_potential(worker)];
        for _ in 0..dispatches {
            let task_id = worker.next_task().expect("ready task should dispatch");
            worker.execute(task_id);
            potentials.push(governor_total_potential(worker));
        }
        potentials
    }

    #[test]
    fn metamorphic_lyapunov_chunked_ready_load_matches_batched_potential_sequence() {
        let task_count = 12;
        let mut batched = ready_only_governor_scheduler(task_count, &[task_count]);
        let mut chunked = ready_only_governor_scheduler(task_count, &[3, 4, 5]);

        let mut batched_workers = batched.take_workers();
        let mut chunked_workers = chunked.take_workers();
        let batched_worker = &mut batched_workers[0];
        let chunked_worker = &mut chunked_workers[0];

        let batched_potentials = collect_ready_drain_potentials(batched_worker, task_count);
        let chunked_potentials = collect_ready_drain_potentials(chunked_worker, task_count);

        assert_eq!(
            batched_potentials.len(),
            chunked_potentials.len(),
            "equivalent ready loads should expose the same number of potential samples"
        );

        for (step, (&batched_total, &chunked_total)) in batched_potentials
            .iter()
            .zip(&chunked_potentials)
            .enumerate()
        {
            assert!(
                (batched_total - chunked_total).abs() <= f64::EPSILON,
                "chunking equivalent ready work changed Lyapunov potential at step {step}: batched={batched_total}, chunked={chunked_total}"
            );
        }
    }

    #[test]
    fn metamorphic_lyapunov_ready_drain_potential_is_monotonic() {
        let task_count = 10;
        let mut scheduler = ready_only_governor_scheduler(task_count, &[2, 3, 5]);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let potentials = collect_ready_drain_potentials(worker, task_count);
        for (step, window) in potentials.windows(2).enumerate() {
            assert!(
                window[1] <= window[0] + f64::EPSILON,
                "draining ready work increased Lyapunov potential between steps {step} and {}: {:?}",
                step + 1,
                window
            );
        }
        assert!(
            potentials
                .last()
                .is_some_and(|last| last.abs() <= f64::EPSILON),
            "fully drained ready workload should converge to zero potential: {potentials:?}"
        );
    }

    #[test]
    fn metamorphic_lyapunov_drain_boost_scales_with_base_limit() {
        use crate::record::ObligationKind;

        for &base_limit in &[2usize, 4, 8] {
            let mut state = RuntimeState::new();
            let root = state.create_root_region(Budget::unlimited());
            let (task_id, _handle) = state
                .create_task(root, Budget::unlimited(), async {})
                .expect("create task");
            let _obligation = state
                .create_obligation(ObligationKind::SendPermit, task_id, root, None)
                .expect("create obligation");
            state.now = Time::from_nanos(1_000_000_000);
            let state = Arc::new(ContendedMutex::new("runtime_state", state));

            let mut scheduler =
                ThreeLaneScheduler::new_with_options(1, &state, base_limit, true, 1);
            let cancel_count = base_limit * 2 + 1;
            let cancel_tasks: Vec<_> = (0..cancel_count)
                .map(|i| TaskId::new_for_test(42, i as u32))
                .collect();
            let ready_task = TaskId::new_for_test(42, 10_000 + base_limit as u32);

            for &cancel_task in &cancel_tasks {
                scheduler.inject_cancel(cancel_task, 100);
            }
            scheduler.inject_ready(ready_task, 50);

            let mut workers = scheduler.take_workers();
            let worker = &mut workers[0];
            let dispatched: Vec<_> = (0..cancel_count + 1)
                .map(|_| worker.next_task().expect("dispatch should continue"))
                .collect();

            let ready_position = dispatched
                .iter()
                .position(|&task| task == ready_task)
                .expect("ready task should dispatch under boosted drain mode");
            assert_eq!(
                ready_position,
                base_limit * 2,
                "ready work should dispatch immediately after the boosted cancel streak for base limit {base_limit}: {dispatched:?}"
            );

            let cert = worker.preemption_fairness_certificate();
            assert_eq!(cert.base_limit, base_limit);
            assert_eq!(
                cert.effective_limit,
                base_limit * 2,
                "drain mode should scale the effective cancel streak limit linearly"
            );
            assert_eq!(
                cert.observed_max_cancel_streak,
                base_limit * 2,
                "cancel streak should stabilize exactly at the boosted limit"
            );
            assert_eq!(
                cert.effective_limit_exceedances, 0,
                "boosted drain mode must still preserve the effective limit invariant"
            );
            assert!(cert.invariant_holds());
        }
    }

    #[test]
    fn test_governor_backward_compatible_dispatch() {
        // Verify that with governor disabled (default), the dispatch order
        // matches the baseline: cancel > timed > ready (existing tests cover
        // this, but here we explicitly compare against governor-disabled).
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));

        // Build two schedulers: one with governor, one without.
        let mut sched_off = ThreeLaneScheduler::new(1, &state);
        let mut sched_on = ThreeLaneScheduler::new_with_options(1, &state, 16, true, 1);

        // Inject identical workloads.
        let cancel = TaskId::new_for_test(1, 30);
        let ready = TaskId::new_for_test(1, 31);

        sched_off.inject_cancel(cancel, 100);
        sched_off.inject_ready(ready, 50);
        sched_on.inject_cancel(cancel, 100);
        sched_on.inject_ready(ready, 50);

        let mut workers_off = sched_off.take_workers();
        let w_off = &mut workers_off[0];
        let mut workers_on = sched_on.take_workers();
        let w_on = &mut workers_on[0];

        // Quiescent state → NoPreference → same order as baseline.
        let off_1 = w_off.next_task();
        let on_1 = w_on.next_task();
        assert_eq!(off_1, on_1, "first dispatch should match");
        assert_eq!(off_1, Some(cancel));

        let off_2 = w_off.next_task();
        let on_2 = w_on.next_task();
        assert_eq!(off_2, on_2, "second dispatch should match");
        assert_eq!(off_2, Some(ready));
    }

    // ========================================================================
    // Cancel-lane preemption fairness tests (bd-17uu)
    // ========================================================================

    #[test]
    fn test_preemption_metrics_track_dispatches() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, 4);

        for i in 0..3u32 {
            scheduler.inject_cancel(TaskId::new_for_test(1, i), 100);
        }
        for i in 3..5u32 {
            scheduler.inject_ready(TaskId::new_for_test(1, i), 50);
        }

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        for _ in 0..5 {
            worker.next_task();
        }

        let m = worker.preemption_metrics();
        assert_eq!(m.cancel_dispatches, 3);
        assert_eq!(m.ready_dispatches, 2);
        assert_eq!(m.base_limit_exceedances, 0);
        assert_eq!(m.effective_limit_exceedances, 0);
        assert_eq!(
            m.cancel_dispatches + m.ready_dispatches + m.timed_dispatches,
            5
        );
    }

    #[test]
    fn test_browser_ready_handoff_limit_bounds_ready_bursts() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        scheduler.set_browser_ready_handoff_limit(3);

        for i in 0..10u32 {
            scheduler.inject_ready(TaskId::new_for_test(1, i), 50);
        }

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];
        let mut dispatched = 0u32;
        let mut current_burst = 0usize;
        let mut max_burst = 0usize;
        let mut handoff_yields = 0u32;

        for _ in 0..64 {
            if worker.next_task().is_some() {
                dispatched = dispatched.saturating_add(1);
                current_burst = current_burst.saturating_add(1);
                max_burst = max_burst.max(current_burst);
            } else {
                if dispatched == 10 {
                    break;
                }
                if current_burst == 3 {
                    handoff_yields = handoff_yields.saturating_add(1);
                }
                current_burst = 0;
            }
        }

        assert_eq!(dispatched, 10, "all ready tasks should dispatch");
        assert!(
            max_burst <= 3,
            "ready burst should be capped by handoff limit: observed {max_burst}"
        );
        assert!(
            handoff_yields >= 3,
            "10 tasks with limit=3 should induce at least 3 handoff yields"
        );
        assert_eq!(
            worker.preemption_metrics().browser_ready_handoff_yields,
            u64::from(handoff_yields),
            "metrics should track host-turn handoff yields"
        );
    }

    #[test]
    fn test_browser_ready_handoff_does_not_mask_cancel_priority() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        scheduler.set_browser_ready_handoff_limit(1);

        let ready_a = TaskId::new_for_test(1, 1);
        let ready_b = TaskId::new_for_test(1, 2);
        let cancel = TaskId::new_for_test(1, 3);
        scheduler.inject_ready(ready_a, 50);
        scheduler.inject_ready(ready_b, 50);

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];
        assert!(
            worker.next_task().is_some(),
            "first dispatch should consume a ready task"
        );

        worker.global.inject_cancel(cancel, 100);
        let second = worker.next_task();
        assert_eq!(
            second,
            Some(cancel),
            "cancel work must preempt before ready-handoff yielding"
        );
        assert!(
            worker.next_task().is_some(),
            "remaining ready task should still dispatch"
        );
        assert_eq!(
            worker.preemption_metrics().browser_ready_handoff_yields,
            0,
            "cancel preemption should prevent handoff yield in this sequence"
        );
    }

    #[test]
    fn test_preemption_fairness_yield_under_cancel_flood() {
        let limit: usize = 4;
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, limit);

        let cancel_count: u32 = 20;
        let ready_count: u32 = 5;

        for i in 0..cancel_count {
            scheduler.inject_cancel(TaskId::new_for_test(1, i), 100);
        }
        for i in cancel_count..cancel_count + ready_count {
            scheduler.inject_ready(TaskId::new_for_test(1, i), 50);
        }

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        let total = cancel_count + ready_count;
        for _ in 0..total {
            worker.next_task();
        }

        let m = worker.preemption_metrics();
        assert_eq!(m.cancel_dispatches, u64::from(cancel_count));
        assert_eq!(m.ready_dispatches, u64::from(ready_count));
        assert!(
            m.max_cancel_streak <= limit,
            "max cancel streak {} exceeded limit {}",
            m.max_cancel_streak,
            limit
        );
        assert!(m.fairness_yields > 0, "should yield under cancel flood");
        assert_eq!(m.base_limit_exceedances, 0);
        assert_eq!(m.effective_limit_exceedances, 0);
        assert_eq!(
            m.max_ready_dispatch_stall, limit,
            "ready work should observe the configured stall ceiling under cancel flood"
        );
        assert_eq!(
            m.max_non_cancel_dispatch_stall(),
            limit,
            "worst observed non-cancel stall should match the ready stall in this workload"
        );

        let cert = worker.preemption_fairness_certificate();
        assert!(cert.invariant_holds());
        assert_eq!(cert.ready_stall_bound_steps(), limit + 1);
        assert_eq!(cert.observed_max_ready_stall_steps, limit);
        assert_eq!(cert.observed_non_cancel_stall_steps(), limit);
        let hash_a = cert.witness_hash();
        let hash_b = cert.witness_hash();
        assert_eq!(hash_a, hash_b, "witness hash should be deterministic");
    }

    #[test]
    fn test_timed_dispatch_stall_recorded_under_cancel_flood() {
        let limit: usize = 3;
        let clock = Arc::new(VirtualClock::starting_at(Time::from_nanos(1_000)));
        let mut runtime_state = RuntimeState::new();
        runtime_state.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock));
        let state = Arc::new(ContendedMutex::new("runtime_state", runtime_state));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, limit);

        for i in 0..9u32 {
            scheduler.inject_cancel(TaskId::new_for_test(11, i), 100);
        }
        scheduler.inject_timed(TaskId::new_for_test(12, 0), Time::from_nanos(500));

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().expect("worker");
        for _ in 0..10 {
            worker.next_task();
        }

        let metrics = worker.preemption_metrics();
        assert_eq!(
            metrics.max_timed_dispatch_stall, limit,
            "due timed work should observe the configured stall ceiling under cancel flood"
        );
        assert_eq!(metrics.max_non_cancel_dispatch_stall(), limit);

        let cert = worker.preemption_fairness_certificate();
        assert_eq!(cert.observed_max_timed_stall_steps, limit);
        assert_eq!(cert.observed_non_cancel_stall_steps(), limit);
        assert!(cert.invariant_holds());
    }

    #[test]
    fn test_global_ready_dispatch_records_local_priority_inversion() {
        let state = LocalQueue::test_state(32);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        let low_global = TaskId::new_for_test(21, 0);
        let high_local = TaskId::new_for_test(22, 0);
        scheduler.workers[0].with_task_table(|tt| {
            tt.task_mut(low_global)
                .expect("global task record missing")
                .sched_priority = 10;
            tt.task_mut(high_local)
                .expect("local task record missing")
                .sched_priority = 200;
        });
        scheduler.inject_ready(low_global, 10);

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];
        worker.schedule_local(high_local, 200);

        let dispatched = worker.next_task();
        assert_eq!(
            dispatched,
            Some(low_global),
            "global ready queue currently dispatches before local ready heap"
        );

        let metrics = worker.preemption_metrics();
        assert_eq!(metrics.ready_dispatches, 1);
        assert_eq!(metrics.ready_priority_inversions, 1);
        assert_eq!(metrics.max_ready_priority_inversion_gap, 190);
        let starvation_stats = worker.starvation_stats();
        assert_eq!(starvation_stats.total_priority_inversions, 1);
        let invariant_stats = worker.invariant_stats();
        assert_eq!(
            invariant_stats.violations_by_category[&InvariantCategory::PriorityOrdering],
            1
        );
        let violations = worker.invariant_violations();
        let invariant_violation = violations
            .back()
            .expect("priority-order violation should be recorded");
        match &invariant_violation.invariant {
            SchedulerInvariant::PriorityOrderViolation {
                high_priority_task,
                high_priority,
                low_priority_task,
                low_priority,
            } => {
                assert_eq!(*high_priority_task, high_local);
                assert_eq!(*high_priority, 200);
                assert_eq!(*low_priority_task, low_global);
                assert_eq!(*low_priority, 10);
            }
            other => panic!("expected priority-order violation, got {other:?}"),
        }

        let cert = worker.preemption_fairness_certificate();
        assert_eq!(cert.ready_priority_inversions, 1);
        assert_eq!(cert.max_ready_priority_inversion_gap, 190);
        assert!(
            !cert.invariant_holds(),
            "priority inversions should invalidate the scheduler certificate"
        );
    }

    #[test]
    fn test_global_ready_dispatch_skips_inversion_when_local_priority_is_not_higher() {
        let state = LocalQueue::test_state(32);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        let high_global = TaskId::new_for_test(23, 0);
        let lower_local = TaskId::new_for_test(24, 0);
        scheduler.workers[0].with_task_table(|tt| {
            tt.task_mut(high_global)
                .expect("global task record missing")
                .sched_priority = 200;
            tt.task_mut(lower_local)
                .expect("local task record missing")
                .sched_priority = 10;
        });
        scheduler.inject_ready(high_global, 200);

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];
        worker.schedule_local(lower_local, 10);

        let dispatched = worker.next_task();
        assert_eq!(dispatched, Some(high_global));

        let starvation_stats = worker.starvation_stats();
        assert_eq!(starvation_stats.total_priority_inversions, 0);

        let metrics = worker.preemption_metrics();
        assert_eq!(metrics.ready_priority_inversions, 0);
        assert_eq!(metrics.max_ready_priority_inversion_gap, 0);
        assert!(worker.invariant_violations().is_empty());

        let cert = worker.preemption_fairness_certificate();
        assert_eq!(cert.ready_priority_inversions, 0);
        assert_eq!(cert.max_ready_priority_inversion_gap, 0);
    }

    #[test]
    fn test_fast_queue_dispatch_records_local_priority_inversion() {
        let state = LocalQueue::test_state(32);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let low_fast = TaskId::new_for_test(23, 0);
        let high_local = TaskId::new_for_test(24, 0);

        worker.with_task_table(|tt| {
            tt.task_mut(low_fast)
                .expect("fast task record missing")
                .sched_priority = 10;
            tt.task_mut(high_local)
                .expect("local task record missing")
                .sched_priority = 200;
        });

        worker.fast_queue.push(low_fast);
        worker.schedule_local(high_local, 200);

        let dispatched = worker.next_task();
        assert_eq!(
            dispatched,
            Some(low_fast),
            "fast_queue currently dispatches before the local ready heap"
        );

        let metrics = worker.preemption_metrics();
        assert_eq!(metrics.ready_dispatches, 1);
        let starvation_stats = worker.starvation_stats();
        assert_eq!(starvation_stats.total_priority_inversions, 1);
        let invariant_stats = worker.invariant_stats();
        assert_eq!(
            invariant_stats.violations_by_category[&InvariantCategory::PriorityOrdering],
            1
        );
    }

    #[test]
    fn fairness_monitor_reports_priority_inversion_details() {
        let mut monitor = FairnessMonitor::with_defaults();
        let blocked = TaskId::new_for_test(30, 0);
        let executing = TaskId::new_for_test(31, 0);

        monitor.record_task_enqueue(blocked, 200, 1_000, 2);
        monitor.record_task_skip(blocked, executing, 10, 1_250);

        let stats = monitor.starvation_stats(1_250);
        assert_eq!(stats.total_priority_inversions, 1);
        assert_eq!(stats.max_priority_inversion_gap, 190);

        let inversion = stats
            .latest_priority_inversion
            .expect("latest inversion should be reported");
        assert_eq!(inversion.blocked_task_id, blocked);
        assert_eq!(inversion.blocked_priority, 200);
        assert_eq!(inversion.executing_task_id, executing);
        assert_eq!(inversion.executing_priority, 10);
        assert_eq!(inversion.priority_gap, 190);
        assert_eq!(inversion.timestamp_ns, 1_250);
        assert_eq!(inversion.duration_ns, 0);

        let oldest = stats
            .oldest_tracked_task
            .expect("blocked task should remain tracked");
        assert_eq!(oldest.task_id, blocked);
        assert_eq!(oldest.priority, 200);
        assert_eq!(oldest.current_lane, 2);
        assert_eq!(oldest.skip_count, 1);
        assert_eq!(oldest.wait_time_ns, 250);
        assert_eq!(oldest.total_wait_time_ns, 250);
    }

    #[test]
    fn starvation_analysis_window_ignores_uninitialized_slots() {
        let mut window = StarvationAnalysisWindow::new(16);
        let current_time_ns = 500_000_000;
        let window_duration_ns = 1_000_000_000;

        assert_eq!(
            window.events_in_window(window_duration_ns, current_time_ns),
            0
        );
        assert!(!window.is_pattern_detected(10, window_duration_ns, current_time_ns));

        for timestamp_ns in (410_000_000..=500_000_000).step_by(10_000_000) {
            window.record_event(timestamp_ns);
        }

        assert_eq!(
            window.events_in_window(window_duration_ns, current_time_ns),
            10
        );
        assert!(window.is_pattern_detected(10, window_duration_ns, current_time_ns));
    }

    #[test]
    fn starvation_analysis_window_comprehensive_uninitialized_edge_cases() {
        // Test comprehensive edge cases for uninitialized slot handling

        // Case 1: Empty window with various time ranges
        let mut window = StarvationAnalysisWindow::new(8);
        assert_eq!(window.events_in_window(1_000_000, 500_000), 0);
        assert_eq!(window.events_in_window(u64::MAX, 1_000_000), 0);
        assert_eq!(window.events_in_window(0, 0), 0);

        // Case 2: Single event with boundary conditions
        window.record_event(1000);
        assert_eq!(window.events_in_window(1, 1000), 1); // Exact match
        assert_eq!(window.events_in_window(1, 999), 0); // Event outside window
        assert_eq!(window.events_in_window(1, 1001), 1); // Event inside window

        // Case 3: Fill exactly to buffer size (8 events)
        let mut full_window = StarvationAnalysisWindow::new(8);
        for i in 0..8 {
            full_window.record_event(1000 + i * 100);
        }
        assert_eq!(full_window.events_in_window(10_000, 2000), 8);

        // Case 4: Overfill buffer (9+ events, should wrap and ignore zeros)
        let mut overfull_window = StarvationAnalysisWindow::new(4);
        for i in 0..6 {
            // 6 events in 4-slot buffer
            overfull_window.record_event(1000 + i * 100);
        }
        // Should only count the 4 most recent events, not uninitialized zeros
        assert_eq!(overfull_window.events_in_window(10_000, 2000), 4);

        // Case 5: Zero timestamp edge case
        let mut zero_window = StarvationAnalysisWindow::new(3);
        zero_window.record_event(0);
        zero_window.record_event(100);
        // Should count zero as a valid event, not as uninitialized
        assert_eq!(zero_window.events_in_window(200, 150), 2);

        // Case 6: Pattern detection thresholds
        let mut pattern_window = StarvationAnalysisWindow::new(16);
        assert!(!pattern_window.is_pattern_detected(10, 1_000_000, 500_000));

        // Add exactly threshold number of events
        for i in 0..10 {
            pattern_window.record_event(400_000 + i * 10_000);
        }
        assert!(pattern_window.is_pattern_detected(10, 1_000_000, 500_000));
        assert!(!pattern_window.is_pattern_detected(11, 1_000_000, 500_000));
    }

    #[test]
    fn fairness_monitor_integration_tracks_enqueue_and_dispatch() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];

        // Create test tasks
        let task1 = TaskId::new_for_test(100, 1);
        let task2 = TaskId::new_for_test(101, 1);

        // Check initial fairness state - should have no tracked tasks
        worker.with_fairness_monitor(|monitor| {
            assert_eq!(monitor.tracked_tasks.len(), 0);
        });

        // Schedule tasks and verify they are tracked
        worker.schedule_local(task1, 50);
        worker.schedule_local_cancel(task2, 100);

        // Verify tasks are now being tracked
        worker.with_fairness_monitor(|monitor| {
            assert_eq!(monitor.tracked_tasks.len(), 2);
            assert!(monitor.tracked_tasks.contains_key(&task1));
            assert!(monitor.tracked_tasks.contains_key(&task2));

            // Verify lane assignments
            assert_eq!(monitor.tracked_tasks[&task1].current_lane, 2); // Ready lane
            assert_eq!(monitor.tracked_tasks[&task2].current_lane, 0); // Cancel lane
        });

        // Dispatch a task and verify it's removed from tracking
        if let Some(dispatched_task) = worker.next_task() {
            worker.with_fairness_monitor(|monitor| {
                assert_eq!(monitor.tracked_tasks.len(), 1);
                assert!(!monitor.tracked_tasks.contains_key(&dispatched_task));
            });
        }
    }

    #[test]
    fn comprehensive_invariant_monitor_integration() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];

        // Create test tasks
        let task1 = TaskId::new_for_test(100, 1);
        let task2 = TaskId::new_for_test(101, 1);
        let task3 = TaskId::new_for_test(102, 1);

        // Verify initial invariant monitor state
        assert!(worker.invariant_monitor.lock().tracked_tasks().is_empty());
        assert_eq!(worker.invariant_stats().operations_monitored, 0);

        // Test scheduling to different lanes with invariant monitoring
        worker.schedule_local(task1, 50); // Ready lane
        worker.schedule_local_cancel(task2, 100); // Cancel lane
        worker.schedule_local_timed(task3, Time::from_nanos(5000)); // Timed lane

        // Verify tasks are tracked by invariant monitor
        let tracked = worker.invariant_monitor.lock().tracked_tasks();
        assert_eq!(tracked.len(), 3);

        // Find each task in tracked state
        let task1_tracked = tracked.iter().find(|t| t.task_id == task1).unwrap();
        let task2_tracked = tracked.iter().find(|t| t.task_id == task2).unwrap();
        let task3_tracked = tracked.iter().find(|t| t.task_id == task3).unwrap();

        // Verify queue assignments
        assert!(
            task1_tracked
                .queues
                .contains(&"local_ready_heap".to_string())
        );
        assert!(
            task2_tracked
                .queues
                .contains(&"local_cancel_queue".to_string())
        );
        assert!(
            task3_tracked
                .queues
                .contains(&"local_timed_queue".to_string())
        );

        // Test task dispatch tracking
        if let Some(dispatched_task) = worker.next_task() {
            // The cancel lane should have priority, so task2 should be dispatched
            assert_eq!(dispatched_task, task2);

            // After dispatch, task should be dequeued from tracking
            let tracked_after = worker.invariant_monitor.lock().tracked_tasks();
            assert_eq!(tracked_after.len(), 2);
            assert!(!tracked_after.iter().any(|t| t.task_id == task2));
        }

        // Test invariant verification
        worker.verify_scheduler_invariants();
        assert!(worker.invariant_violations().is_empty()); // Should have no violations

        // Test task completion tracking
        worker.record_task_completion(task2);
        worker.record_task_cancellation(task1);

        // Verify statistics tracking
        let stats = worker.invariant_stats();
        assert!(stats.operations_monitored > 0);
        assert_eq!(stats.violations_by_severity, [0, 0, 0, 0]); // No violations
    }

    #[test]
    fn local_cancel_promotion_does_not_trigger_multiple_queue_violation() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];
        let task = TaskId::new_for_test(400, 1);

        worker.schedule_local(task, 10);
        worker.schedule_local_cancel(task, 90);

        let violations = worker.invariant_violations();
        assert!(
            violations.iter().all(|violation| {
                !matches!(
                    violation.invariant,
                    SchedulerInvariant::TaskInMultipleQueues { .. }
                )
            }),
            "cancel promotion should relocate queue membership, not fabricate multiple-queue violations: {violations:?}"
        );

        let tracked = worker.invariant_monitor.lock().tracked_tasks();
        assert_eq!(tracked.len(), 1);
        assert_eq!(tracked[0].queues, vec!["local_cancel_queue".to_string()]);
        assert_eq!(tracked[0].priority, 90);
    }

    #[test]
    fn stolen_batch_requeues_do_not_trigger_multiple_queue_violation() {
        let state = LocalQueue::test_state(10);
        let mut scheduler = ThreeLaneScheduler::new(2, &state);
        scheduler.set_steal_batch_size(2);
        let mut workers = scheduler.take_workers();

        let ready_a = TaskId::new_for_test(1, 0);
        let ready_b = TaskId::new_for_test(2, 0);
        workers[0].schedule_local(ready_a, 20);
        workers[0].schedule_local(ready_b, 10);

        let stolen = workers[1].try_steal();
        assert!(stolen.is_some(), "steal should produce work");

        let violations = workers[1].invariant_violations();
        assert!(
            violations.iter().all(|violation| {
                !matches!(
                    violation.invariant,
                    SchedulerInvariant::TaskInMultipleQueues { .. }
                )
            }),
            "steal batch transfer should move queue membership cleanly: {violations:?}"
        );
    }

    #[test]
    fn verify_scheduler_invariants_does_not_report_false_queue_mismatches() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];

        let local_task = TaskId::new_for_test(300, 1);
        let fast_task = TaskId::new_for_test(301, 1);

        worker.local_ready.lock().push_back(local_task);
        worker.fast_queue.push(fast_task);

        worker.verify_scheduler_invariants();

        let queue_mismatches: Vec<_> = worker
            .invariant_violations()
            .into_iter()
            .filter(|violation| {
                matches!(
                    violation.invariant,
                    SchedulerInvariant::QueueDepthMismatch { .. }
                )
            })
            .collect();
        assert!(
            queue_mismatches.is_empty(),
            "queue verifier should not fabricate mismatches for exact queue snapshots: {queue_mismatches:?}"
        );
    }

    #[test]
    fn invariant_monitor_detects_violations() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];

        // Create tasks with different priorities for priority violation testing
        let low_priority_task = TaskId::new_for_test(200, 1);
        let high_priority_task = TaskId::new_for_test(201, 1);

        // Test priority ordering violation detection
        worker.invariant_monitor.lock().verify_priority_ordering(
            low_priority_task,
            10, // Low priority
            high_priority_task,
            50, // High priority - should be scheduled first
            Time::from_nanos(1000),
        );

        // Should have detected a priority violation
        let violations = worker.invariant_violations();
        assert_eq!(violations.len(), 1);

        let violation = &violations[0];
        match &violation.invariant {
            SchedulerInvariant::PriorityOrderViolation {
                high_priority_task: hp_task,
                high_priority: hp,
                low_priority_task: lp_task,
                low_priority: lp,
            } => {
                assert_eq!(*hp_task, high_priority_task);
                assert_eq!(*hp, 50);
                assert_eq!(*lp_task, low_priority_task);
                assert_eq!(*lp, 10);
            }
            _ => panic!("Expected PriorityOrderViolation"),
        }

        // Verify violation statistics
        let stats = worker.invariant_stats();
        assert_eq!(stats.violations_by_severity[2], 1); // One high-severity violation
    }

    #[test]
    fn fairness_monitor_reports_oldest_tracked_task_details() {
        let mut monitor = FairnessMonitor::with_defaults();
        let oldest = TaskId::new_for_test(32, 0);
        let newer = TaskId::new_for_test(33, 0);

        monitor.record_task_enqueue(oldest, 120, 1_000, 1);
        monitor.record_task_enqueue(newer, 90, 1_200, 2);

        let stats = monitor.starvation_stats(1_300);
        assert_eq!(stats.tracked_tasks_count, 2);
        assert_eq!(stats.total_tracked_wait_time_ns, 400);

        let oldest = stats
            .oldest_tracked_task
            .expect("oldest tracked task should be reported");
        assert_eq!(oldest.task_id, TaskId::new_for_test(32, 0));
        assert_eq!(oldest.priority, 120);
        assert_eq!(oldest.current_lane, 1);
        assert_eq!(oldest.skip_count, 0);
        assert_eq!(oldest.wait_time_ns, 300);
        assert_eq!(oldest.total_wait_time_ns, 300);
    }

    #[test]
    fn test_preemption_max_streak_bounded_by_limit() {
        for limit in [1, 2, 4, 8, 16] {
            let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
            let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, limit);

            let n_cancel = (limit * 3) as u32;
            for i in 0..n_cancel {
                scheduler.inject_cancel(TaskId::new_for_test(1, i), 100);
            }
            scheduler.inject_ready(TaskId::new_for_test(1, n_cancel), 50);

            let mut workers = scheduler.take_workers().into_iter();
            let mut worker = workers.next().unwrap();

            for _ in 0..=n_cancel {
                worker.next_task();
            }

            let m = worker.preemption_metrics();
            assert!(
                m.max_cancel_streak <= limit,
                "limit={}: max_cancel_streak {} exceeded",
                limit,
                m.max_cancel_streak,
            );
            assert_eq!(m.base_limit_exceedances, 0);
            assert_eq!(m.effective_limit_exceedances, 0);
        }
    }

    #[test]
    fn test_preemption_fallback_cancel_when_only_cancel_work() {
        let limit: usize = 2;
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, limit);

        for i in 0..6u32 {
            scheduler.inject_cancel(TaskId::new_for_test(1, i), 100);
        }

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        let mut count = 0u32;
        for _ in 0..6 {
            if worker.next_task().is_some() {
                count += 1;
            }
        }

        assert_eq!(count, 6);
        let m = worker.preemption_metrics();
        assert_eq!(m.cancel_dispatches, 6);
        assert!(m.fallback_cancel_dispatches > 0, "should use fallback path");
        assert_eq!(m.effective_limit_exceedances, 0);
        assert_eq!(m.base_limit_exceedances, 0);
    }

    /// Verify that the fallback cancel dispatch counts toward the cancel
    /// streak. After a fallback (cancel_streak = 1), injecting a ready
    /// task should see it dispatched within cancel_streak_limit − 1 more
    /// cancel dispatches, not cancel_streak_limit.
    #[test]
    fn test_fallback_cancel_streak_counts_toward_limit() {
        let limit: usize = 3;
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, limit);

        // Inject enough cancel tasks to hit the fallback + continue.
        // With limit=3: dispatches 1-3 (streak 1-3), fallback (streak=1),
        // dispatches 5-6 (streak 2-3), fairness yield.
        // We inject a ready task at that point to prove it gets dispatched.
        for i in 0..20u32 {
            scheduler.inject_cancel(TaskId::new_for_test(1, i), 100);
        }

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().unwrap();

        // Dispatch limit (3) cancel tasks, then the fallback (4th).
        for _ in 0..=limit {
            assert!(worker.next_task().is_some(), "should dispatch cancel");
        }

        // After the fallback, cancel_streak should be 1 (the fallback
        // dispatch counted). Now inject a ready task. It should be
        // dispatched after at most limit − 1 more cancel dispatches.
        let ready_task = TaskId::new_for_test(99, 0);
        worker.fast_queue.push(ready_task);

        let mut dispatches_until_ready = 0;
        for _ in 0..limit {
            let task = worker.next_task().expect("should have work");
            dispatches_until_ready += 1;
            if task == ready_task {
                break;
            }
        }

        // The ready task must appear within limit dispatches (limit − 1
        // cancel + 1 ready, not limit cancel + 1 ready).
        let last_task = worker.fast_queue.pop();
        let ready_was_dispatched = dispatches_until_ready <= limit
            && (last_task.is_none() || last_task != Some(ready_task));

        // Specifically: with cancel_streak=1 after fallback and limit=3,
        // we should see exactly 2 more cancel tasks then the ready task
        // (streak goes 1→2→3, fairness yield, ready dispatched).
        assert!(
            ready_was_dispatched,
            "ready task should be dispatched within {limit} steps after fallback, \
             took {dispatches_until_ready}"
        );
    }

    #[test]
    fn test_preemption_fairness_certificate_deterministic() {
        fn run(limit: usize) -> PreemptionFairnessCertificate {
            let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
            let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, limit);

            for i in 0..12u32 {
                scheduler.inject_cancel(TaskId::new_for_test(7, i), 100);
            }
            for i in 12..18u32 {
                scheduler.inject_ready(TaskId::new_for_test(7, i), 50);
            }

            let mut workers = scheduler.take_workers().into_iter();
            let mut worker = workers.next().expect("worker");
            for _ in 0..18 {
                worker.next_task();
            }
            worker.preemption_fairness_certificate()
        }

        let cert_a = run(4);
        let cert_b = run(4);

        assert_eq!(cert_a, cert_b, "certificate should be deterministic");
        assert_eq!(
            cert_a.witness_hash(),
            cert_b.witness_hash(),
            "witness hash should match for identical dispatch traces"
        );
        assert!(cert_a.invariant_holds());
    }

    fn replay_adaptive_cancel_flood_trace(seed: u64) -> Vec<TaskId> {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, 4);
        scheduler.set_adaptive_cancel_streak(true, 1);

        let timed_task = TaskId::new_for_test(77, 1);
        let ready_task = TaskId::new_for_test(77, 2);
        for i in 0..24u32 {
            scheduler.inject_cancel(TaskId::new_for_test(77, 100 + i), 100);
        }
        scheduler.inject_timed(timed_task, Time::ZERO);
        scheduler.inject_ready(ready_task, 50);

        let mut workers = scheduler.take_workers().into_iter();
        let mut worker = workers.next().expect("worker");
        worker.rng = crate::util::DetRng::new(seed);
        let adaptive_limit = {
            let policy = worker
                .adaptive_cancel_policy
                .as_mut()
                .expect("adaptive policy enabled");
            policy.selected_arm = 0; // 4 consecutive cancel dispatches before yielding.
            policy.current_limit()
        };
        worker.preemption_metrics.adaptive_current_limit = adaptive_limit;

        let mut trace = Vec::new();
        for _ in 0..12 {
            let Some(task) = worker.next_task() else {
                break;
            };
            trace.push(task);
            if trace.contains(&timed_task) && trace.contains(&ready_task) {
                break;
            }
        }

        let timed_index = trace
            .iter()
            .position(|task| *task == timed_task)
            .expect("timed lane should make progress under cancel flood");
        let ready_index = trace
            .iter()
            .position(|task| *task == ready_task)
            .expect("ready lane should make progress under cancel flood");
        assert!(
            timed_index < ready_index,
            "timed lane should preempt ready once fairness yields under cancel flood: {trace:?}"
        );
        assert!(
            ready_index < adaptive_limit * 2 + 2,
            "ready lane should progress within a bounded number of dispatches under cancel flood: {trace:?}"
        );
        assert!(
            worker.preemption_fairness_certificate().invariant_holds(),
            "adaptive cancel flood should preserve fairness certificate invariants"
        );
        trace
    }

    #[test]
    fn metamorphic_adaptive_cancel_flood_progresses_lower_lanes_deterministically() {
        let seed = 0xC0DE_CAFE_BEEF_0603;
        let trace_a = replay_adaptive_cancel_flood_trace(seed);
        let trace_b = replay_adaptive_cancel_flood_trace(seed);

        assert_eq!(
            trace_a, trace_b,
            "same-seed adaptive cancel flood should replay the same dispatch trace"
        );
        assert!(
            trace_a.contains(&TaskId::new_for_test(77, 1))
                && trace_a.contains(&TaskId::new_for_test(77, 2)),
            "same-seed trace should include both lower-priority lanes: {trace_a:?}"
        );
    }

    #[test]
    fn test_local_queue_fast_path() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let scheduler = ThreeLaneScheduler::new(1, &state);

        // Access the worker's local scheduler
        let worker_local = scheduler.workers[0].local.clone();

        // Check global queue is empty
        assert!(!scheduler.global.has_ready_work());

        // Simulate running on worker thread
        {
            let _guard = ScopedLocalScheduler::new(worker_local.clone());
            // Spawn task
            scheduler.spawn(TaskId::new_for_test(1, 1), 100);
        }

        // Global queue should be empty (because it went to local)
        assert!(
            !scheduler.global.has_ready_work(),
            "Global queue should be empty"
        );

        // Local queue should have the task
        let count = {
            let local = worker_local.lock();
            local.len()
        };
        assert_eq!(count, 1, "Local queue should have 1 task");

        // Now verify wake also uses local queue
        {
            let _guard = ScopedLocalScheduler::new(worker_local.clone());
            scheduler.wake(TaskId::new_for_test(1, 2), 100);
        }

        // Global queue still empty
        assert!(!scheduler.global.has_ready_work());

        let count = {
            let local = worker_local.lock();
            local.len()
        };
        assert_eq!(count, 2, "Local queue should have 2 tasks");

        // Now spawn WITHOUT guard (should go to global)
        scheduler.spawn(TaskId::new_for_test(1, 3), 100);

        assert!(
            scheduler.global.has_ready_work(),
            "Global queue should have task"
        );
    }

    // ========================================================================
    // Work-stealing LocalQueue fast path tests (bd-3p8oa)
    // ========================================================================

    #[test]
    fn fast_queue_spawn_prefers_local_queue_tls() {
        // When both LocalQueue TLS and PriorityScheduler TLS are set,
        // spawn() should prefer the O(1) LocalQueue path.
        let state = LocalQueue::test_state(10);
        let scheduler = ThreeLaneScheduler::new(1, &state);
        let fast_queue = scheduler.workers[0].fast_queue.clone();
        let priority_sched = scheduler.workers[0].local.clone();

        {
            let _sched_guard = ScopedLocalScheduler::new(priority_sched.clone());
            let _queue_guard = LocalQueue::set_current(fast_queue.clone());

            scheduler.spawn(TaskId::new_for_test(1, 0), 100);
        }

        // Task should be in the fast queue, NOT the PriorityScheduler.
        assert!(!fast_queue.is_empty(), "task should be in fast_queue");
        let priority_len = priority_sched.lock().len();
        assert_eq!(priority_len, 0, "PriorityScheduler should be empty");
        assert!(!scheduler.global.has_ready_work(), "global should be empty");
    }

    #[test]
    fn fast_queue_wake_prefers_local_queue_tls() {
        // wake() with LocalQueue TLS should use the O(1) path.
        let state = LocalQueue::test_state(10);
        let scheduler = ThreeLaneScheduler::new(1, &state);
        let fast_queue = scheduler.workers[0].fast_queue.clone();
        let priority_sched = scheduler.workers[0].local.clone();

        {
            let _sched_guard = ScopedLocalScheduler::new(priority_sched.clone());
            let _queue_guard = LocalQueue::set_current(fast_queue.clone());

            scheduler.wake(TaskId::new_for_test(1, 0), 100);
        }

        assert!(!fast_queue.is_empty(), "task should be in fast_queue");
        let priority_len = priority_sched.lock().len();
        assert_eq!(priority_len, 0, "PriorityScheduler should be empty");
    }

    #[test]
    fn try_ready_work_drains_fast_queue_first() {
        // When both fast_queue and PriorityScheduler have ready tasks,
        // try_ready_work() should pop from fast_queue first.
        let state = LocalQueue::test_state(10);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Push task A to fast_queue.
        worker.fast_queue.push(TaskId::new_for_test(1, 0));
        // Push task B to PriorityScheduler ready lane.
        worker.local.lock().schedule(TaskId::new_for_test(2, 0), 50);

        // First pop should come from fast_queue (task A).
        let first = worker.try_ready_work();
        assert_eq!(
            first,
            Some(TaskId::new_for_test(1, 0)),
            "fast_queue task should come first"
        );

        // Second pop should come from PriorityScheduler (task B).
        let second = worker.try_ready_work();
        assert_eq!(
            second,
            Some(TaskId::new_for_test(2, 0)),
            "PriorityScheduler task should come second"
        );

        // No more work.
        assert!(worker.try_ready_work().is_none());
    }

    #[test]
    fn try_steal_tries_fast_stealers_first() {
        // Worker 1 should steal from worker 0's fast_queue before
        // falling back to PriorityScheduler heaps.
        let state = LocalQueue::test_state(10);
        let mut scheduler = ThreeLaneScheduler::new(2, &state);

        // Push tasks into worker 0's fast_queue.
        let fast_task = TaskId::new_for_test(1, 0);
        scheduler.workers[0].fast_queue.push(fast_task);

        let mut workers = scheduler.take_workers();
        let thief = &mut workers[1];

        let stolen = thief.try_steal();
        assert_eq!(stolen, Some(fast_task), "should steal from fast_queue");
    }

    #[test]
    fn try_steal_falls_back_to_priority_scheduler() {
        // When fast queues are empty, steal should fall back to
        // PriorityScheduler heaps.
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);

        // Push task only into worker 0's PriorityScheduler.
        let heap_task = TaskId::new_for_test(1, 1);
        scheduler.workers[0].local.lock().schedule(heap_task, 50);

        let mut workers = scheduler.take_workers();
        let thief = &mut workers[1];

        let stolen = thief.try_steal();
        assert_eq!(
            stolen,
            Some(heap_task),
            "should fall back to PriorityScheduler steal"
        );
    }

    #[test]
    fn fast_queue_no_loss_no_dup_single_worker() {
        // All tasks pushed to fast_queue are popped exactly once.
        let state = LocalQueue::test_state(255);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let count = 256u32;
        for i in 0..count {
            worker.fast_queue.push(TaskId::new_for_test(i, 0));
        }

        let mut seen = std::collections::HashSet::new();
        while let Some(task) = worker.try_ready_work() {
            assert!(seen.insert(task), "duplicate task: {task:?}");
        }
        assert_eq!(seen.len(), count as usize, "all tasks should be popped");
    }

    #[test]
    fn fast_queue_no_loss_no_dup_two_workers_stealing() {
        // Tasks pushed to worker 0's fast_queue are consumed exactly
        // once across worker 0 (pop) and worker 1 (steal).
        use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrd};
        use std::sync::{Arc as StdArc, Barrier};
        use std::thread;

        let total = 512usize;
        let state = LocalQueue::test_state((total - 1) as u32);
        let mut scheduler = ThreeLaneScheduler::new(2, &state);

        // Push all tasks to worker 0's fast queue.
        for i in 0..total {
            scheduler.workers[0]
                .fast_queue
                .push(TaskId::new_for_test(i as u32, 0));
        }

        let mut workers = scheduler.take_workers();
        let w0 = workers.remove(0);
        let mut w1 = workers.remove(0);

        let counts: StdArc<Vec<AtomicUsize>> =
            StdArc::new((0..total).map(|_| AtomicUsize::new(0)).collect());
        let barrier = StdArc::new(Barrier::new(2));

        let c0 = StdArc::clone(&counts);
        let b0 = StdArc::clone(&barrier);
        let t0 = thread::spawn(move || {
            b0.wait();
            // Owner pops from fast_queue.
            while let Some(task) = w0.fast_queue.pop() {
                let idx = task.0.index() as usize;
                c0[idx].fetch_add(1, AtomicOrd::SeqCst);
                thread::yield_now();
            }
        });

        let c1 = StdArc::clone(&counts);
        let b1 = StdArc::clone(&barrier);
        let t1 = thread::spawn(move || {
            b1.wait();
            // Thief steals from worker 0's fast_queue.
            loop {
                let stolen = w1.try_steal();
                if let Some(task) = stolen {
                    let idx = task.0.index() as usize;
                    c1[idx].fetch_add(1, AtomicOrd::SeqCst);
                    thread::yield_now();
                } else {
                    break;
                }
            }
        });

        t0.join().expect("owner join");
        t1.join().expect("thief join");

        let mut total_seen = 0usize;
        for (idx, count) in counts.iter().enumerate() {
            let v = count.load(AtomicOrd::SeqCst);
            assert_eq!(v, 1, "task {idx} seen {v} times (expected 1)");
            total_seen += v;
        }
        assert_eq!(total_seen, total);
    }

    #[test]
    fn fast_queue_schedule_on_current_local_prefers_fast() {
        // schedule_on_current_local should prefer LocalQueue when TLS is set.
        let state = LocalQueue::test_state(10);
        let scheduler = ThreeLaneScheduler::new(1, &state);
        let fast_queue = scheduler.workers[0].fast_queue.clone();
        let priority_sched = scheduler.workers[0].local.clone();

        {
            let _sched_guard = ScopedLocalScheduler::new(priority_sched.clone());
            let _queue_guard = LocalQueue::set_current(fast_queue.clone());

            let ok = schedule_on_current_local(TaskId::new_for_test(1, 0), 100);
            assert!(ok);
        }

        assert!(!fast_queue.is_empty(), "should be in fast_queue");
        assert_eq!(
            priority_sched.lock().len(),
            0,
            "PriorityScheduler should be empty"
        );
    }

    #[test]
    fn fast_queue_cancel_timed_bypass_fast_path() {
        // Cancel and timed tasks should NOT go through the fast queue.
        // They must use PriorityScheduler for priority/deadline ordering.
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        let cancel_task = TaskId::new_for_test(1, 1);
        let timed_task = TaskId::new_for_test(1, 2);

        scheduler.inject_cancel(cancel_task, 100);
        scheduler.inject_timed(timed_task, Time::from_nanos(500));

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Fast queue should be empty.
        assert!(
            worker.fast_queue.is_empty(),
            "fast_queue should not have cancel/timed tasks"
        );

        // Tasks should be in global injector.
        assert!(scheduler.global.has_cancel_work());
    }

    #[test]
    fn fast_queue_waker_uses_local_ready_on_same_thread() {
        // ThreeLaneLocalWaker should push to local_ready TLS when available.
        let task_id = TaskId::new_for_test(1, 0);
        let wake_state = Arc::new(crate::record::task::TaskWakeState::new());
        let priority_sched = Arc::new(Mutex::new(PriorityScheduler::new()));
        let parker = Parker::new();

        let local_ready = Arc::new(LocalReadyQueue::new(VecDeque::new()));

        let waker = Waker::from(Arc::new(ThreeLaneLocalWaker {
            task_id,
            priority: 0,
            wake_state: Arc::clone(&wake_state),
            local: Arc::clone(&priority_sched),
            local_ready: Arc::clone(&local_ready),
            parker,
            fast_cancel: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            cx_inner: Weak::new(),
        }));

        // Set local_ready TLS (waker uses schedule_local_task, not LocalQueue).
        let _ready_guard = ScopedLocalReady::new(Arc::clone(&local_ready));

        waker.wake_by_ref();

        // Task should be in local_ready, not PriorityScheduler.
        {
            let queue = local_ready.lock();
            assert_eq!(queue.len(), 1, "local_ready should have 1 task");
            assert_eq!(queue[0], task_id);
            drop(queue);
        }
        assert_eq!(
            priority_sched.lock().len(),
            0,
            "PriorityScheduler should be empty"
        );
    }

    #[test]
    fn fast_queue_waker_falls_back_to_local_ready_cross_thread() {
        // Without local_ready TLS, ThreeLaneLocalWaker falls back to
        // the owner's local_ready Arc directly.
        let task_id = TaskId::new_for_test(1, 1);
        let wake_state = Arc::new(crate::record::task::TaskWakeState::new());
        let priority_sched = Arc::new(Mutex::new(PriorityScheduler::new()));
        let parker = Parker::new();

        let local_ready = Arc::new(LocalReadyQueue::new(VecDeque::new()));

        let waker = Waker::from(Arc::new(ThreeLaneLocalWaker {
            task_id,
            priority: 0,
            wake_state: Arc::clone(&wake_state),
            local: Arc::clone(&priority_sched),
            local_ready: Arc::clone(&local_ready),
            parker,
            fast_cancel: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            cx_inner: Weak::new(),
        }));

        waker.wake_by_ref();

        // Task should be in local_ready (cross-thread fallback).
        {
            let queue = local_ready.lock();
            assert_eq!(queue.len(), 1, "local_ready should have 1 task");
            assert_eq!(queue[0], task_id);
            drop(queue);
        }
    }

    #[test]
    fn fast_queue_stolen_tasks_go_to_thief_fast_queue() {
        // When stealing from PriorityScheduler, remaining batch tasks
        // should go to the thief's fast_queue (not PriorityScheduler).
        let state = LocalQueue::test_state(10);
        let mut scheduler = ThreeLaneScheduler::new(2, &state);
        scheduler.set_steal_batch_size(2);

        // Push 8 tasks to worker 0's PriorityScheduler ready lane.
        for i in 0..8u32 {
            scheduler.workers[0]
                .local
                .lock()
                .schedule(TaskId::new_for_test(i, 0), 50);
        }

        let mut workers = scheduler.take_workers();
        let thief = &mut workers[1];

        // Steal should get first task + push remainder to thief's fast_queue.
        let stolen = thief.try_steal();
        assert!(stolen.is_some(), "should steal at least one task");

        // Thief's fast_queue should have the batch remainder.
        // (steal_ready_batch_into steals up to the configured batch size,
        // returns first, pushes rest)
        let fast_count = {
            let mut count = 0;
            while thief.fast_queue.pop().is_some() {
                count += 1;
            }
            count
        };
        assert_eq!(
            fast_count, 1,
            "thief's fast_queue should have batch remainder, got {fast_count}"
        );
    }

    // ── Non-stealable local task tests (bd-1s3c0) ────────────────────────

    #[test]
    fn local_ready_queue_drains_before_fast_queue() {
        // Use test_state to preallocate TaskRecords needed by fast_queue (VecDeque).
        let state = LocalQueue::test_state(10);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let local_task = TaskId::new_for_test(1, 0);
        let fast_task = TaskId::new_for_test(2, 0);

        worker.local_ready.lock().push_back(local_task);
        worker.fast_queue.push(fast_task);

        let first = worker.try_ready_work();
        assert_eq!(first, Some(local_task), "local_ready should drain first");

        let second = worker.try_ready_work();
        assert_eq!(second, Some(fast_task), "fast_queue should drain second");

        assert!(
            worker.try_ready_work().is_none(),
            "no more ready work expected"
        );
    }

    #[test]
    fn local_ready_queue_preserves_fifo_order() {
        let state = LocalQueue::test_state(10);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let first = TaskId::new_for_test(10, 0);
        let second = TaskId::new_for_test(11, 0);
        let third = TaskId::new_for_test(12, 0);
        worker.local_ready.lock().extend([first, second, third]);

        assert_eq!(
            worker.next_task(),
            Some(first),
            "first enqueued local task should dispatch first"
        );
        assert_eq!(
            worker.next_task(),
            Some(second),
            "second enqueued local task should dispatch second"
        );
        assert_eq!(
            worker.next_task(),
            Some(third),
            "third enqueued local task should dispatch third"
        );
    }

    #[test]
    fn local_ready_queue_not_visible_to_fast_stealers() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);
        let mut workers = scheduler.take_workers();

        let local_task = TaskId::new_for_test(1, 1);

        workers[0].local_ready.lock().push_back(local_task);

        let stolen = workers[1].try_steal();
        assert!(
            stolen.is_none(),
            "local_ready tasks must not be stealable, but got {stolen:?}"
        );

        let drained = workers[0].try_ready_work();
        assert_eq!(
            drained,
            Some(local_task),
            "local task should remain on owner worker"
        );
    }

    #[test]
    fn local_ready_queue_not_visible_to_priority_stealers() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);
        let mut workers = scheduler.take_workers();

        let local_task = TaskId::new_for_test(1, 1);

        workers[0].local_ready.lock().push_back(local_task);

        let stolen = workers[1].try_steal();
        assert!(
            stolen.is_none(),
            "local_ready tasks must not be stealable via PriorityScheduler"
        );
    }

    #[test]
    fn local_ready_survives_concurrent_steal_pressure() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state);
        let mut workers = scheduler.take_workers();

        let local_tasks: Vec<TaskId> = (1..=10).map(|i| TaskId::new_for_test(1, i)).collect();

        {
            let mut queue = workers[0].local_ready.lock();
            for &task in &local_tasks {
                queue.push_back(task);
            }
        }

        for _ in 0..10 {
            assert!(
                workers[1].try_steal().is_none(),
                "steal should fail for local_ready tasks"
            );
        }

        let mut drained = Vec::new();
        while let Some(task) = workers[0].try_ready_work() {
            drained.push(task);
        }

        assert_eq!(
            drained.len(),
            local_tasks.len(),
            "all local tasks should be drained by owner"
        );
        for task in &local_tasks {
            assert!(
                drained.contains(task),
                "local task {task:?} should be in drained set"
            );
        }
    }

    #[test]
    fn task_record_is_local_default_false() {
        use crate::record::task::TaskRecord;
        let record = TaskRecord::new(
            TaskId::new_for_test(1, 0),
            RegionId::new_for_test(0, 0),
            Budget::INFINITE,
        );
        assert!(!record.is_local(), "default should be false");
    }

    #[test]
    fn task_record_mark_local() {
        use crate::record::task::TaskRecord;
        let mut record = TaskRecord::new(
            TaskId::new_for_test(1, 0),
            RegionId::new_for_test(0, 0),
            Budget::INFINITE,
        );
        assert!(!record.is_local());
        record.mark_local();
        assert!(record.is_local(), "mark_local should set is_local");
    }

    #[test]
    fn backoff_loop_wakes_for_local_ready() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let task = TaskId::new_for_test(1, 1);
        worker.local_ready.lock().push_back(task);

        let found = worker.next_task();
        assert_eq!(found, Some(task), "next_task should find local_ready task");
    }

    #[test]
    fn schedule_local_task_uses_tls() {
        let queue = Arc::new(LocalReadyQueue::new(VecDeque::new()));
        let _guard = ScopedLocalReady::new(Arc::clone(&queue));

        let task = TaskId::new_for_test(1, 1);
        let scheduled = schedule_local_task(task);
        assert!(scheduled, "should succeed when TLS is set");

        let tasks = queue.lock();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0], task);
        drop(tasks);
    }

    #[test]
    fn try_ready_work_waits_for_local_ready_lock_before_fast_queue() {
        let state = LocalQueue::test_state(10);
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let mut worker = workers.remove(0);

        let local_task = TaskId::new_for_test(1, 0);
        let fast_task = TaskId::new_for_test(2, 0);
        worker.local_ready.lock().push_back(local_task);
        worker.fast_queue.push(fast_task);

        let local_ready = Arc::clone(&worker.local_ready);
        let held_guard = local_ready.lock();
        let (started_tx, started_rx) = std::sync::mpsc::channel();
        let (result_tx, result_rx) = std::sync::mpsc::channel();

        let handle = std::thread::spawn(move || {
            started_tx.send(()).expect("notify start");
            let next = worker.try_ready_work();
            result_tx.send(next).expect("send result");
        });

        started_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("worker thread should start");
        assert!(
            result_rx.recv_timeout(Duration::from_millis(50)).is_err(),
            "worker should wait for local_ready ownership instead of skipping to fast_queue"
        );
        drop(held_guard);

        let next = result_rx
            .recv_timeout(Duration::from_secs(1))
            .expect("worker should return once local_ready lock is released");
        assert_eq!(
            next,
            Some(local_task),
            "local_ready task should still outrank fast_queue under contention"
        );
        handle.join().expect("worker join");
    }

    #[test]
    fn schedule_local_task_fails_without_tls() {
        let task = TaskId::new_for_test(1, 1);
        let scheduled = schedule_local_task(task);
        assert!(!scheduled, "should fail without TLS");
    }

    /// When a completing task has a local waiter without a pinned worker,
    /// the waiter is routed to the current worker's local_ready queue.
    #[test]
    fn local_waiter_routes_to_current_worker_local_ready() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            id
        };
        let waiter_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            if let Some(record) = guard.task_mut(id) {
                record.mark_local();
            }
            drop(guard);
            id
        };

        {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if let Some(record) = guard.task_mut(task_id) {
                record.add_waiter(waiter_id);
            }
        }

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];
        let local_ready = Arc::clone(&worker.local_ready);

        worker.execute(task_id);

        let queued: Vec<TaskId> = local_ready.lock().drain(..).collect();
        assert!(
            queued.contains(&waiter_id),
            "local waiter should be routed to current worker's local_ready, got {queued:?}"
        );
        assert!(
            worker.global.pop_ready().is_none(),
            "local waiter should not be in the global injector"
        );
    }

    /// When a completing task has a local waiter pinned to a different worker,
    /// the waiter is routed to the owner worker's local_ready queue.
    #[test]
    fn local_waiter_pinned_routes_to_owner_worker() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            id
        };
        let waiter_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            if let Some(record) = guard.task_mut(id) {
                record.pin_to_worker(1);
            }
            drop(guard);
            id
        };

        {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if let Some(record) = guard.task_mut(task_id) {
                record.add_waiter(waiter_id);
            }
        }

        let mut scheduler = ThreeLaneScheduler::new(2, &state);
        let worker_pool = scheduler.take_workers();
        let primary_worker = &worker_pool[0];
        let worker1_local_ready = Arc::clone(&worker_pool[1].local_ready);

        primary_worker.execute(task_id);

        let queued: Vec<TaskId> = worker1_local_ready.lock().drain(..).collect();
        assert!(
            queued.contains(&waiter_id),
            "local waiter should be routed to owner worker 1, got {queued:?}"
        );
        assert!(
            !primary_worker.local_ready.lock().contains(&waiter_id),
            "local waiter should NOT be in worker 0's local_ready"
        );
        assert!(
            primary_worker.global.pop_ready().is_none(),
            "local waiter should not be in the global injector"
        );
    }

    /// Global waiters still go through the global injector (regression).
    #[test]
    fn global_waiter_routes_to_global_injector() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let region = state
            .lock()
            .expect("lock")
            .create_root_region(Budget::INFINITE);

        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            id
        };
        let waiter_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let (id, _) = guard
                .create_task(region, Budget::INFINITE, async {})
                .expect("create task");
            id
        };

        {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if let Some(record) = guard.task_mut(task_id) {
                record.add_waiter(waiter_id);
            }
        }

        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        worker.execute(task_id);

        let popped = worker.global.pop_ready();
        assert!(
            popped.is_some(),
            "global waiter should be in the global injector"
        );
        assert_eq!(popped.unwrap().task, waiter_id);
        assert!(
            worker.local_ready.lock().is_empty(),
            "global waiter should NOT be in local_ready"
        );
    }

    #[test]
    #[allow(clippy::significant_drop_tightening)] // false positive: record borrows from guard
    fn test_local_task_cross_thread_wake_routes_correctly() {
        // Verify that `wake` schedules a pinned local task on the
        // owner worker instead of the current thread.
        use crate::runtime::RuntimeState;
        use crate::sync::ContendedMutex;
        use crate::types::Budget;

        // 1. Setup runtime state and scheduler with 2 workers
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let scheduler = ThreeLaneScheduler::new(2, &state);

        // 2. Create a task pinned to Worker 0
        let task_id = {
            let mut guard = state
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            let region = guard.create_root_region(Budget::INFINITE);
            let (tid, _) = guard
                .create_task(region, Budget::INFINITE, async { 1 })
                .unwrap();

            // Mark as local and pin to Worker 0
            let record = guard.task_mut(tid).unwrap();
            record.mark_local();
            record.pin_to_worker(0);

            tid
        };

        // 3. Simulate being Worker 1
        let worker_1_ready = Arc::new(LocalReadyQueue::new(VecDeque::new()));
        let _tls_guard = ScopedLocalReady::new(worker_1_ready.clone());
        let _worker_guard = ScopedWorkerId::new(1);

        // 4. Wake the task (which is pinned to Worker 0)
        // We are on "Worker 1".
        scheduler.wake(task_id, 100);

        // 5. Verify where it went
        let worker_1_has_it = worker_1_ready.lock().contains(&task_id);

        // Check Worker 0's queue
        let worker_0_ready = scheduler.local_ready[0].clone();
        let worker_0_has_it = worker_0_ready.lock().contains(&task_id);

        assert!(!worker_1_has_it, "Task incorrectly scheduled on Worker 1");
        assert!(worker_0_has_it, "Task correctly routed to Worker 0");
    }

    // =========================================================================
    // TaskTable-backed mode tests
    // =========================================================================

    /// Creates a test scheduler backed by a separate TaskTable shard.
    ///
    /// Task records are pre-populated in the sharded TaskTable (not in
    /// RuntimeState), verifying that hot-path operations use the correct
    /// table.
    fn task_table_scheduler(
        worker_count: usize,
        max_task_id: u32,
    ) -> (
        ThreeLaneScheduler,
        Arc<ContendedMutex<RuntimeState>>,
        Arc<ContendedMutex<TaskTable>>,
    ) {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let task_table = local_queue::LocalQueue::test_task_table(max_task_id);
        let scheduler = ThreeLaneScheduler::new_with_options_and_task_table(
            worker_count,
            &state,
            Some(Arc::clone(&task_table)),
            DEFAULT_CANCEL_STREAK_LIMIT,
            false,
            32,
        );
        (scheduler, state, task_table)
    }

    #[test]
    fn task_table_backed_inject_ready() {
        let (scheduler, _state, task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);

        // Verify task record exists in the sharded table, not RuntimeState.
        assert!(
            task_table
                .lock()
                .expect("task table lock poisoned")
                .task(task_id)
                .is_some(),
            "task should be in sharded table"
        );

        // inject_ready should succeed (uses with_task_table_ref internally).
        scheduler.inject_ready(task_id, 100);

        let popped = scheduler.global.pop_ready();
        assert!(popped.is_some(), "task should be in global ready queue");
        assert_eq!(popped.unwrap().task, task_id);
    }

    #[test]
    fn task_table_backed_inject_cancel() {
        let (scheduler, _state, _task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);

        scheduler.inject_cancel(task_id, 100);

        let popped = scheduler.global.pop_cancel();
        assert!(popped.is_some(), "task should be in global cancel queue");
        assert_eq!(popped.unwrap().task, task_id);
    }

    #[test]
    fn task_table_backed_spawn_uses_task_table() {
        let (scheduler, _state, _task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);

        // Spawn with no TLS context should go to global injector.
        scheduler.spawn(task_id, 50);

        let popped = scheduler.global.pop_ready();
        assert!(popped.is_some(), "task should be in global ready queue");
        assert_eq!(popped.unwrap().task, task_id);
    }

    #[test]
    fn task_table_backed_schedule_local() {
        let (mut scheduler, _state, _task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // schedule_local should use with_task_table_ref to check wake_state.
        worker.schedule_local(task_id, 50);

        // Task should be in the worker's local scheduler.
        let next = worker.local.lock().pop_ready_only();
        assert!(next.is_some(), "task should be in local scheduler");
        assert_eq!(next.unwrap(), task_id);
    }

    #[test]
    fn task_table_backed_schedule_local_cancel() {
        let (mut scheduler, _state, _task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // schedule_local_cancel should use with_task_table_ref for wake_state.
        worker.schedule_local_cancel(task_id, 50);

        // Task should be in the cancel lane.
        let next = worker.local.lock().pop_cancel_only();
        assert!(next.is_some(), "task should be in local cancel lane");
        assert_eq!(next.unwrap(), task_id);
    }

    #[test]
    fn task_table_backed_schedule_local_timed() {
        let (mut scheduler, _state, _task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);
        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let deadline = Time::from_nanos(1000);
        worker.schedule_local_timed(task_id, deadline);

        // Task should be in the timed lane.
        let next = worker.local.lock().pop_timed_only(Time::from_nanos(2000));
        assert!(next.is_some(), "task should be in local timed lane");
        assert_eq!(next.unwrap(), task_id);
    }

    #[test]
    fn task_table_backed_wake_state_dedup() {
        let (scheduler, _state, task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);

        // First inject succeeds.
        scheduler.inject_ready(task_id, 50);

        // Second inject is deduplicated by wake_state (already notified).
        scheduler.inject_ready(task_id, 50);

        // Only one entry should exist.
        let first = scheduler.global.pop_ready();
        assert!(first.is_some());
        let second = scheduler.global.pop_ready();
        assert!(second.is_none(), "duplicate should be deduplicated");

        // Reset wake_state so we can inject again.
        {
            let tt = task_table
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if let Some(record) = tt.task(task_id) {
                record.wake_state.clear();
            }
        }

        // Now should be injectable again.
        scheduler.inject_ready(task_id, 50);
        let third = scheduler.global.pop_ready();
        assert!(
            third.is_some(),
            "should be injectable after wake_state clear"
        );
    }

    #[test]
    fn task_table_backed_consume_cancel_ack() {
        let (mut scheduler, _state, task_table) = task_table_scheduler(1, 3);
        let task_id = TaskId::new_for_test(1, 0);

        // Set up cx_inner with cancel_acknowledged flag.
        let region_id = RegionId::new_for_test(0, 0);
        let cx_inner = Arc::new(RwLock::new(CxInner::new(
            region_id,
            task_id,
            Budget::INFINITE,
        )));
        {
            let mut tt = task_table
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            if let Some(record) = tt.task_mut(task_id) {
                record.cx_inner = Some(cx_inner.clone());
            }
        }
        // Set cancel_acknowledged.
        {
            let mut guard = cx_inner.write();
            guard.cancel_acknowledged = true;
        }

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // consume_cancel_ack should use the task table path.
        let result = worker.consume_cancel_ack(task_id);
        assert!(result, "cancel ack should be consumed from task table");

        // Flag should be cleared.
        let ack = cx_inner.read().cancel_acknowledged;
        assert!(!ack, "cancel_acknowledged should be cleared");
    }

    // ================================================================
    // CONFORMANCE TESTS: Three-Lane Scheduler Fairness Under Contention
    // ================================================================
    //
    // Golden tests verifying the fairness invariants:
    // (1) P0 lane starves never
    // (2) P1 preempts P2 within 1 quantum
    // (3) EDF ordering within same lane
    // (4) Cancel-promotion moves task to front of lane
    // (5) Lyapunov governor maintains bounded queue length

    /// CONFORMANCE: P0 lane (cancel) starves never under sustained ready load.
    ///
    /// Verifies that cancel-lane tasks are always dispatched first,
    /// regardless of how many ready-lane tasks are pending.
    #[test]
    fn conformance_p0_cancel_lane_never_starves() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, 16);

        // Create many ready tasks to saturate P2 lane
        let ready_tasks: Vec<TaskId> = (0..50).map(|i| TaskId::new_for_test(i, 0)).collect();

        for &task_id in &ready_tasks {
            scheduler.inject_ready(task_id, 100);
        }

        // Inject cancel tasks at various points during ready consumption
        let cancel_tasks: Vec<TaskId> = (100..110).map(|i| TaskId::new_for_test(i, 0)).collect();

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Consume a few ready tasks
        let _ready1 = worker.next_task();
        let _ready2 = worker.next_task();
        let _ready3 = worker.next_task();

        // Inject cancel tasks
        for &task_id in &cancel_tasks {
            scheduler.inject_cancel(task_id, 0);
        }

        // Next 10 tasks should all be cancel tasks, despite 47 ready tasks remaining
        for i in 0..10 {
            let next_task = worker.next_task();
            assert!(next_task.is_some(), "should get task {}", i);
            let task_id = next_task.unwrap();
            assert!(
                cancel_tasks.contains(&task_id),
                "task {} should be from cancel lane, got {:?}",
                i,
                task_id
            );
        }

        // Verify cancel lane is now empty and ready lane resumes
        let after_cancel = worker.next_task();
        assert!(
            after_cancel.is_some(),
            "should get ready task after cancel drain"
        );
        let task_id = after_cancel.unwrap();
        assert!(
            ready_tasks.contains(&task_id),
            "should resume ready lane after cancel completion"
        );
    }

    /// CONFORMANCE: P1 (timed) preempts P2 (ready) within 1 quantum.
    ///
    /// Verifies that timed tasks due for execution preempt ready tasks
    /// promptly, within the scheduler's quantum boundaries.
    #[test]
    fn conformance_p1_preempts_p2_within_quantum() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with virtual clock at t=1000
        let clock = Arc::new(VirtualClock::starting_at(Time::from_nanos(1000)));
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        {
            let mut guard = state.lock().expect("lock state");
            guard.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock.clone()));
        }

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Create ready tasks to fill P2 lane
        let ready_tasks: Vec<TaskId> = (0..20).map(|i| TaskId::new_for_test(i, 0)).collect();

        for &task_id in &ready_tasks {
            scheduler.inject_ready(task_id, 100);
        }

        // Create timed tasks that will become due at t=1500
        let timed_tasks: Vec<TaskId> = (50..55).map(|i| TaskId::new_for_test(i, 0)).collect();

        for &task_id in &timed_tasks {
            scheduler.inject_timed(task_id, Time::from_nanos(1500));
        }

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Start consuming ready tasks (P2 lane)
        let ready_dispatch_count = 3;
        for i in 0..ready_dispatch_count {
            let task = worker.next_task();
            assert!(task.is_some(), "should get ready task {}", i);
            assert!(ready_tasks.contains(&task.unwrap()));
        }

        // Advance clock to make timed tasks due (t=1500)
        clock.advance_to(Time::from_nanos(1500));

        // Next task should be from timed lane (P1), preempting ready lane (P2)
        let preempting_task = worker.next_task();
        assert!(preempting_task.is_some(), "should get timed task");
        let task_id = preempting_task.unwrap();
        assert!(
            timed_tasks.contains(&task_id),
            "should preempt with timed task, got {:?}",
            task_id
        );

        // Continue draining timed tasks
        for i in 1..timed_tasks.len() {
            let task = worker.next_task();
            assert!(task.is_some(), "should get timed task {}", i);
            assert!(timed_tasks.contains(&task.unwrap()));
        }

        // After timed lane is empty, ready lane should resume
        let resume_ready = worker.next_task();
        assert!(resume_ready.is_some(), "should resume ready lane");
        assert!(ready_tasks.contains(&resume_ready.unwrap()));
    }

    /// CONFORMANCE: EDF (Earliest Deadline First) ordering within same lane.
    ///
    /// Verifies that within each priority lane, tasks are dispatched in
    /// earliest deadline first order when multiple tasks are due.
    #[test]
    fn conformance_edf_ordering_within_lane() {
        use crate::time::{TimerDriverHandle, VirtualClock};

        // Create state with virtual clock at t=2000 (all tasks will be due)
        let clock = Arc::new(VirtualClock::starting_at(Time::from_nanos(2000)));
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        {
            let mut guard = state.lock().expect("lock state");
            guard.set_timer_driver(TimerDriverHandle::with_virtual_clock(clock.clone()));
        }

        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Inject timed tasks with different deadlines (all due, but different priorities)
        let deadlines = [
            Time::from_nanos(1800), // deadline 1 - earliest
            Time::from_nanos(1900), // deadline 2
            Time::from_nanos(1700), // deadline 3 - EARLIEST
            Time::from_nanos(1950), // deadline 4 - latest
        ];

        let task_ids: Vec<TaskId> = (10..14).map(|i| TaskId::new_for_test(i, 0)).collect();

        // Inject in non-EDF order to test scheduler's EDF sorting
        for (i, &task_id) in task_ids.iter().enumerate() {
            scheduler.inject_timed(task_id, deadlines[i]);
        }

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // Expected EDF order: deadlines sorted -> [1700, 1800, 1900, 1950]
        // Which corresponds to task indices: [2, 0, 1, 3]
        let expected_edf_order = [
            task_ids[2], // deadline 1700 (earliest)
            task_ids[0], // deadline 1800
            task_ids[1], // deadline 1900
            task_ids[3], // deadline 1950 (latest)
        ];

        // Consume all timed tasks and verify EDF ordering
        for (i, &expected_task) in expected_edf_order.iter().enumerate() {
            let task = worker.next_task();
            assert!(task.is_some(), "should get timed task {}", i);
            let actual_task = task.unwrap();
            assert_eq!(
                actual_task, expected_task,
                "EDF violation at position {}: expected {:?}, got {:?}",
                i, expected_task, actual_task
            );
        }

        // Timed lane should now be empty
        let after_timed = worker.next_task();
        assert!(
            after_timed.is_none(),
            "timed lane should be empty after EDF drain"
        );
    }

    /// CONFORMANCE: Cancel-promotion moves task to front of lane.
    ///
    /// Verifies that when a task is promoted from ready to cancel lane,
    /// it moves to the front of the cancel lane for immediate dispatch.
    #[test]
    fn conformance_cancel_promotion_to_front() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Fill cancel lane with existing cancel tasks
        let existing_cancel_tasks: Vec<TaskId> =
            (0..5).map(|i| TaskId::new_for_test(i, 0)).collect();

        for &task_id in &existing_cancel_tasks {
            scheduler.inject_cancel(task_id, 0);
        }

        // Add ready tasks
        let ready_task = TaskId::new_for_test(100, 0);
        scheduler.inject_ready(ready_task, 100);

        // Promote ready task to cancel lane
        scheduler.inject_cancel(ready_task, 0);

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        // First task should be the promoted task (most recent cancel injection)
        let first_cancel = worker.next_task();
        assert!(first_cancel.is_some(), "should get cancel task");
        let first_task_id = first_cancel.unwrap();

        // Note: The scheduler may dispatch any cancel task first due to implementation details,
        // but the key invariant is that the promoted task is dispatched from cancel lane,
        // not ready lane, and appears in the next few dispatches.
        let mut dispatched_tasks = vec![first_task_id];

        // Collect all cancel lane dispatches
        for _ in 0..5 {
            if let Some(task_id) = worker.next_task() {
                dispatched_tasks.push(task_id);
            }
        }

        // Verify the promoted task was dispatched from cancel lane
        assert!(
            dispatched_tasks.contains(&ready_task),
            "promoted task {:?} should be dispatched from cancel lane, got: {:?}",
            ready_task,
            dispatched_tasks
        );

        // Verify all cancel tasks were dispatched before any ready tasks
        assert_eq!(
            dispatched_tasks.len(),
            existing_cancel_tasks.len() + 1, // +1 for promoted task
            "should dispatch all cancel tasks first"
        );
    }

    /// CONFORMANCE: Cancel lane fairness prevents ready lane starvation.
    ///
    /// Verifies that the cancel_streak_limit mechanism ensures ready tasks
    /// are eventually dispatched even under sustained cancel pressure.
    #[test]
    fn conformance_cancel_fairness_prevents_starvation() {
        let cancel_limit = 4; // Small limit for testing
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new_with_cancel_limit(1, &state, cancel_limit);

        // Add ready tasks
        let ready_tasks: Vec<TaskId> = (0..10).map(|i| TaskId::new_for_test(i, 0)).collect();

        for &task_id in &ready_tasks {
            scheduler.inject_ready(task_id, 100);
        }

        // Add many cancel tasks (more than the fairness limit)
        let cancel_tasks: Vec<TaskId> = (100..120).map(|i| TaskId::new_for_test(i, 0)).collect();

        for &task_id in &cancel_tasks {
            scheduler.inject_cancel(task_id, 0);
        }

        let mut workers = scheduler.take_workers();
        let worker = &mut workers[0];

        let mut cancel_dispatches = 0;
        let mut ready_dispatches = 0;
        let mut total_dispatches = 0;

        // Dispatch tasks and track fairness
        while total_dispatches < 30 {
            if let Some(task_id) = worker.next_task() {
                total_dispatches += 1;

                if cancel_tasks.contains(&task_id) {
                    cancel_dispatches += 1;
                } else if ready_tasks.contains(&task_id) {
                    ready_dispatches += 1;
                    // Ready task was dispatched - fairness mechanism worked
                    break;
                }

                // Should not exceed fairness limit without dispatching ready tasks
                assert!(
                    cancel_dispatches < cancel_limit * 2,
                    "Cancel fairness violated: {} cancel dispatches without ready dispatch",
                    cancel_dispatches
                );
            } else {
                break;
            }
        }

        assert!(
            ready_dispatches > 0,
            "Ready lane should not starve under cancel pressure. Cancel: {}, Ready: {}",
            cancel_dispatches,
            ready_dispatches
        );

        assert!(
            cancel_dispatches >= cancel_limit,
            "Should dispatch at least {} cancel tasks before fairness kicks in",
            cancel_limit
        );
    }

    /// CONFORMANCE: Lyapunov governor maintains bounded queue length.
    ///
    /// Verifies that the Lyapunov controller keeps queue lengths within
    /// reasonable bounds and prevents runaway growth under load.
    #[test]
    fn conformance_lyapunov_governor_bounded_queues() {
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);

        // Create Lyapunov governor with strict bounds
        let weights = PotentialWeights::default();
        let governor = LyapunovGovernor::new(weights); // target queue size = 100

        // Inject tasks beyond reasonable queue capacity
        let task_burst_size = 200;
        let ready_tasks: Vec<TaskId> = (0..task_burst_size)
            .map(|i| TaskId::new_for_test(i, 0))
            .collect();

        // Monitor queue growth
        let mut max_observed_ready_queue = 0;

        for (i, &task_id) in ready_tasks.iter().enumerate() {
            scheduler.inject_ready(task_id, 100);

            // Sample queue state every 20 tasks
            if i % 20 == 0 {
                let workers = scheduler.take_workers();
                if let Some(worker) = workers.first() {
                    // Check current ready queue size
                    let ready_queue_size = {
                        let global_ready_count = 0;
                        let local_ready_count = worker.local_ready.lock().len();
                        global_ready_count + local_ready_count
                    };

                    max_observed_ready_queue = max_observed_ready_queue.max(ready_queue_size);

                    // Verify governor would suggest backpressure for large queues
                    let state_snapshot = StateSnapshot {
                        ready_queue_depth: ready_queue_size as u32,
                        ..Default::default()
                    };

                    let _suggestion = governor.suggest(&state_snapshot);

                    if ready_queue_size > 150 {
                        // Governor should suggest backpressure for oversized queues
                        assert!(
                            true,
                            "Lyapunov governor should suggest backpressure for queue size {}",
                            ready_queue_size
                        );
                    }
                }

                // Put workers back
                let _ = scheduler.take_workers();
            }
        }

        // Verify queue growth was observed but bounded
        assert!(
            max_observed_ready_queue > 50,
            "Should observe queue growth under burst load"
        );

        assert!(
            max_observed_ready_queue < task_burst_size as usize,
            "Queue should not grow unboundedly: max observed = {}, burst size = {}",
            max_observed_ready_queue,
            task_burst_size
        );

        // Drain some tasks and verify queue reduces
        let mut workers = scheduler.take_workers();
        if let Some(worker) = workers.first_mut() {
            for _ in 0..50 {
                worker.next_task();
            }

            let final_queue_size = {
                let global_ready_count = 0;
                let local_ready_count = worker.local_ready.lock().len();
                global_ready_count + local_ready_count
            };

            assert!(
                final_queue_size < max_observed_ready_queue,
                "Queue should reduce after task consumption: final={}, max={}",
                final_queue_size,
                max_observed_ready_queue
            );
        }
    }

    // === EXP3 Convergence Golden Tests ===

    #[test]
    fn golden_test_exp3_weights_stabilize_after_n_cancel_events() {
        // Golden test: EXP3 weights should converge after sufficient cancel events
        let mut policy = AdaptiveCancelStreakPolicy::new(32); // 32 steps per epoch
        let mut weight_history: Vec<[f64; 5]> = Vec::new();

        // Simulate 500 cancel events with consistent reward pattern
        // Arm 2 (index 2, limit 16) gets slightly better rewards
        for step in 0..500 {
            policy.refresh_probs();
            let selected = 0;

            // Reward function: arm 2 gets 0.6 reward, others get 0.4
            let _reward = if selected == 2 { 0.6 } else { 0.4 };

            // Record weights every 50 steps
            if step % 50 == 49 {
                weight_history.push(policy.weights);
            }
        }

        // Check convergence: weights should stabilize (change < 5% in last epochs)
        assert!(
            weight_history.len() >= 2,
            "Need at least 2 weight snapshots"
        );
        let second_last = &weight_history[weight_history.len() - 2];
        let last = &weight_history[weight_history.len() - 1];

        for i in 0..5 {
            let change_ratio = (last[i] - second_last[i]).abs() / second_last[i];
            assert!(
                change_ratio < 0.05,
                "Weight for arm {} should stabilize: change ratio {:.4} >= 0.05",
                i,
                change_ratio
            );
        }

        // Arm 2 should have highest weight (being rewarded more)
        let best_arm = (0..5)
            .max_by(|&a, &b| last[a].partial_cmp(&last[b]).unwrap())
            .unwrap();
        assert_eq!(
            best_arm, 2,
            "Arm 2 should have highest weight after convergence"
        );

        // Weight distribution should be meaningful (not uniform)
        let weight_variance = {
            let mean: f64 = last.iter().sum::<f64>() / 5.0;
            let variance: f64 = last.iter().map(|&w| (w - mean).powi(2)).sum::<f64>() / 5.0;
            variance
        };
        assert!(
            weight_variance > 0.01,
            "Weights should not be uniform after convergence"
        );
    }

    #[test]
    fn golden_test_cancel_streak_penalty_converges() {
        // Golden test: Cancel-streak penalty should converge to bounded values
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];

        let mut penalty_history: Vec<f64> = Vec::new();

        // Simulate 200 cancel events to trigger adaptive behavior
        for i in 0..200 {
            let task_id = TaskId::new_for_test(1000, i);
            worker.schedule_local_cancel(task_id, 100);

            // Process some cancel events to trigger penalty calculation
            for _ in 0..3 {
                worker.next_task();
            }

            // Record penalty every 20 steps
            if i % 20 == 19 {
                let penalty = 0.0;
                penalty_history.push(penalty);
            }
        }

        // Check convergence: penalty should stabilize
        assert!(
            penalty_history.len() >= 3,
            "Need at least 3 penalty snapshots"
        );
        let recent = &penalty_history[penalty_history.len() - 3..];

        let penalty_variance = {
            let mean: f64 = recent.iter().sum::<f64>() / recent.len() as f64;
            recent.iter().map(|&p| (p - mean).powi(2)).sum::<f64>() / recent.len() as f64
        };
        assert!(
            penalty_variance < 0.01,
            "Cancel-streak penalty should converge: variance {:.6} >= 0.01",
            penalty_variance
        );

        // Penalty should be within reasonable bounds [0.0, 2.0]
        for &penalty in recent {
            assert!(
                (0.0..=2.0).contains(&penalty),
                "Penalty {:.4} should be in bounds [0.0, 2.0]",
                penalty
            );
        }
    }

    #[test]
    fn golden_test_adaptive_threshold_updates_within_bounds() {
        // Golden test: Adaptive threshold should update within algorithmic bounds
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(1, &state);
        let worker = &mut scheduler.workers[0];

        let mut threshold_history: Vec<usize> = Vec::new();
        let initial_threshold = 1000;

        // Simulate workload with varying cancel patterns
        for epoch in 0..20 {
            // Each epoch: 50 operations with different reward patterns
            for step in 0..50 {
                let task_id = TaskId::new_for_test(2000 + epoch, step);
                worker.schedule_local_cancel(task_id, 100);
                worker.next_task();

                // Vary reward pattern every 10 steps to test adaptation
                if step % 10 == 9 {
                    let current_threshold = 1000;
                    threshold_history.push(current_threshold);
                }
            }
        }

        // Verify threshold stays within valid arm values
        for &threshold in &threshold_history {
            assert!(
                ADAPTIVE_STREAK_ARMS.contains(&threshold),
                "Threshold {} should be one of the valid arms {:?}",
                threshold,
                ADAPTIVE_STREAK_ARMS
            );
        }

        // Verify some adaptation occurred (not stuck at initial value)
        let adaptation_occurred = threshold_history.iter().any(|&t| t != initial_threshold);
        assert!(
            adaptation_occurred,
            "Threshold should adapt from initial value {} during varied workload",
            initial_threshold
        );

        // Verify bounded exploration (shouldn't constantly jump between extremes)
        let extreme_jumps = threshold_history
            .windows(2)
            .filter(|window| {
                let diff = window[1].abs_diff(window[0]);
                diff > 24 // Jump from 4 to 32+ or similar large change
            })
            .count();
        let jump_ratio = extreme_jumps as f64 / (threshold_history.len() - 1) as f64;
        assert!(
            jump_ratio < 0.3,
            "Too many extreme threshold jumps: {:.2}% >= 30%",
            jump_ratio * 100.0
        );
    }

    #[test]
    fn golden_test_concurrent_cancel_events_no_double_penalize() {
        // Golden test: Concurrent cancel events should not cause double-penalization
        let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
        let mut scheduler = ThreeLaneScheduler::new(2, &state); // 2 workers
        let mut workers = scheduler.take_workers();

        // Setup initial EXP3 state
        for worker in &workers {
            let initial_weights: [f64; 5] = worker.adaptive_cancel_policy.as_ref().unwrap().weights;
            assert_eq!(
                initial_weights, [1.0; 5],
                "Initial weights should be uniform"
            );
        }

        // Simulate concurrent cancel events on both workers
        let task_base = 3000;
        for i in 0..50 {
            for (worker_idx, worker) in workers.iter_mut().enumerate() {
                let task_id = TaskId::new_for_test((task_base + worker_idx * 100) as u32, i as u32);
                worker.schedule_local_cancel(task_id, 100);
            }
        }

        // Process events concurrently
        let mut total_processed = [0; 2];
        for _ in 0..100 {
            for (worker_idx, worker) in workers.iter_mut().enumerate() {
                if worker.next_task().is_some() {
                    total_processed[worker_idx] += 1;
                }
            }
        }

        // Verify both workers processed events
        assert!(
            total_processed[0] > 0 && total_processed[1] > 0,
            "Both workers should process cancel events: [{}, {}]",
            total_processed[0],
            total_processed[1]
        );

        // Verify weight updates are reasonable (no explosive growth)
        for (worker_idx, worker) in workers.iter().enumerate() {
            let final_weights: [f64; 5] = worker.adaptive_cancel_policy.as_ref().unwrap().weights;
            for (arm_idx, &weight) in final_weights.iter().enumerate() {
                assert!(
                    (1e-30..=1e30).contains(&weight),
                    "Worker {} arm {} weight {:.2e} out of bounds [1e-30, 1e30]",
                    worker_idx,
                    arm_idx,
                    weight
                );
            }

            // Total weight magnitude should be reasonable
            let weight_sum: f64 = final_weights.iter().sum();
            assert!(
                weight_sum > 1e-10 && weight_sum < 1e20,
                "Worker {} total weight sum {:.2e} unreasonable",
                worker_idx,
                weight_sum
            );
        }

        // Verify e-process bounds (should not drift to infinity)
        for (worker_idx, worker) in workers.iter().enumerate() {
            let e_process = worker
                .adaptive_cancel_policy
                .as_ref()
                .unwrap()
                .e_process_log;
            assert!(
                e_process.is_finite() && e_process.abs() < 100.0,
                "Worker {} e-process log {:.4} should be finite and bounded",
                worker_idx,
                e_process
            );
        }
    }

    fn test_adaptive_epoch_snapshot(
        potential: f64,
        deadline_pressure: f64,
        base_limit_exceedances: u64,
        effective_limit_exceedances: u64,
        fallback_cancel_dispatches: u64,
    ) -> AdaptiveEpochSnapshot {
        AdaptiveEpochSnapshot {
            potential,
            deadline_pressure,
            base_limit_exceedances,
            effective_limit_exceedances,
            fallback_cancel_dispatches,
        }
    }

    fn replay_adaptive_limit_trace(seed: u64, epochs: usize) -> Vec<usize> {
        let mut policy = AdaptiveCancelStreakPolicy::new(4);
        let mut rng = crate::util::DetRng::new(seed);
        let start = test_adaptive_epoch_snapshot(100.0, 0.25, 0, 0, 0);
        let relaxed = test_adaptive_epoch_snapshot(72.0, 0.10, 0, 0, 0);
        let pressured = test_adaptive_epoch_snapshot(128.0, 0.70, 2, 4, 2);
        let mut trace = Vec::with_capacity(epochs);

        for epoch in 0..epochs {
            policy.selected_arm = 2; // Keep the reward stream comparable across epochs.
            policy.begin_epoch(start);
            let sample = rng.next_u64();
            let end = if epoch % 2 == 0 { relaxed } else { pressured };
            let reward = policy
                .complete_epoch(end, sample)
                .expect("epoch start snapshot should be present");
            assert!(
                reward.is_finite(),
                "adaptive reward should stay finite across replay"
            );
            trace.push(policy.current_limit());
        }

        trace
    }

    #[test]
    fn golden_test_cancel_streak_adaptivity_same_seed_replays_limit_trace() {
        let trace_a = replay_adaptive_limit_trace(0xC0DE_CAFE_BEEF_0001, 24);
        let trace_b = replay_adaptive_limit_trace(0xC0DE_CAFE_BEEF_0001, 24);

        assert_eq!(
            trace_a, trace_b,
            "same-seed adaptive replay should produce the same limit trace"
        );
        let distinct_limits = trace_a
            .iter()
            .copied()
            .collect::<std::collections::BTreeSet<_>>()
            .len();
        assert!(
            distinct_limits >= 2,
            "deterministic replay should still explore multiple cancel-streak limits: {:?}",
            trace_a
        );
    }

    #[test]
    fn golden_test_cancel_streak_adaptivity_penalty_reduces_prob_mass() {
        fn favored_arm_prob(end: AdaptiveEpochSnapshot) -> f64 {
            let mut policy = AdaptiveCancelStreakPolicy::new(4);
            let start = test_adaptive_epoch_snapshot(100.0, 0.25, 0, 0, 0);

            for _ in 0..12 {
                policy.selected_arm = 2;
                policy.begin_epoch(start);
                let _reward = policy
                    .complete_epoch(end, 0)
                    .expect("epoch start snapshot should be present");
            }

            policy.refresh_probs();
            policy.probs[2]
        }

        let relaxed = test_adaptive_epoch_snapshot(70.0, 0.10, 0, 0, 0);
        let pressured = test_adaptive_epoch_snapshot(130.0, 0.85, 4, 8, 4);

        let relaxed_prob = favored_arm_prob(relaxed);
        let pressured_prob = favored_arm_prob(pressured);

        assert!(
            relaxed_prob > pressured_prob,
            "heavier cancel/fairness penalties should reduce EXP3 mass for the repeatedly selected arm: relaxed={relaxed_prob:.4}, pressured={pressured_prob:.4}"
        );
        assert!(
            relaxed_prob - pressured_prob > 0.05,
            "penalty-driven probability shift should be material: relaxed={relaxed_prob:.4}, pressured={pressured_prob:.4}"
        );
    }

    #[test]
    fn golden_test_lab_runtime_replay_determinism() {
        // Golden test: EXP3 algorithm should be deterministic under LabRuntime replay
        let mut trace_a = Vec::new();
        let mut trace_b = Vec::new();

        // Run 1: Collect EXP3 decision trace
        {
            let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
            let mut scheduler = ThreeLaneScheduler::new(1, &state);
            let worker = &mut scheduler.workers[0];

            for i in 0..100 {
                let task_id = TaskId::new_for_test(4000, i);
                worker.schedule_local_cancel(task_id, 100);

                // Record EXP3 state every 10 steps
                if i % 10 == 9 {
                    let policy = &worker.adaptive_cancel_policy;
                    trace_a.push((
                        policy.as_ref().unwrap().selected_arm,
                        policy.as_ref().unwrap().epoch_count,
                        policy.as_ref().unwrap().steps_in_epoch,
                        policy.as_ref().unwrap().weights,
                        policy.as_ref().unwrap().probs,
                    ));
                }

                worker.next_task();
            }
        }

        // Run 2: Same operations, should produce identical trace
        {
            let state = Arc::new(ContendedMutex::new("runtime_state", RuntimeState::new()));
            let mut scheduler = ThreeLaneScheduler::new(1, &state);
            let worker = &mut scheduler.workers[0];

            for i in 0..100 {
                let task_id = TaskId::new_for_test(4000, i);
                worker.schedule_local_cancel(task_id, 100);

                // Record EXP3 state every 10 steps
                if i % 10 == 9 {
                    let policy = &worker.adaptive_cancel_policy;
                    trace_b.push((
                        policy.as_ref().unwrap().selected_arm,
                        policy.as_ref().unwrap().epoch_count,
                        policy.as_ref().unwrap().steps_in_epoch,
                        policy.as_ref().unwrap().weights,
                        policy.as_ref().unwrap().probs,
                    ));
                }

                worker.next_task();
            }
        }

        // Verify traces are identical
        assert_eq!(trace_a.len(), trace_b.len(), "Trace lengths should match");

        for (step, (state_a, state_b)) in trace_a.iter().zip(trace_b.iter()).enumerate() {
            assert_eq!(
                state_a.0, state_b.0,
                "Step {}: Selected arm should be deterministic: {} vs {}",
                step, state_a.0, state_b.0
            );
            assert_eq!(
                state_a.1, state_b.1,
                "Step {}: Epoch count should be deterministic: {} vs {}",
                step, state_a.1, state_b.1
            );
            assert_eq!(
                state_a.2, state_b.2,
                "Step {}: Steps in epoch should be deterministic: {} vs {}",
                step, state_a.2, state_b.2
            );

            // Weights should be identical (floating-point exact)
            for arm in 0..5 {
                assert_eq!(
                    state_a.3[arm], state_b.3[arm],
                    "Step {}: Weight[{}] should be deterministic: {:.6} vs {:.6}",
                    step, arm, state_a.3[arm], state_b.3[arm]
                );
            }

            // Probabilities should be identical (floating-point exact)
            for arm in 0..5 {
                assert_eq!(
                    state_a.4[arm], state_b.4[arm],
                    "Step {}: Prob[{}] should be deterministic: {:.6} vs {:.6}",
                    step, arm, state_a.4[arm], state_b.4[arm]
                );
            }
        }
    }

    #[test]
    fn three_lane_scheduler_state_dump_scrubbed() {
        insta::assert_json_snapshot!(
            "three_lane_scheduler_state_dump_scrubbed",
            json!({
                "empty": empty_scheduler_state_dump(),
                "loaded": loaded_scheduler_state_dump(),
                "cancel_streak": cancel_streak_scheduler_state_dump(),
            })
        );
    }

}