fuel-dispatch 0.19.1

Dispatch infrastructure for fuel: KernelBindingTable, per-backend registration wrappers, CompiledNode + compile_node, ExecutionPlan + compile_plan + resolve_kernel, FusedKernelRegistry, PipelinedExecutor. The layer that takes a graph and runs it against registered backends. Extracted from fuel-memory 2026-05-31.
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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Execution plan — per-realize compilation output, Phase 1.5
//! reshape of the picker-work arc.
//!
//! The plan is a topological order plus a sparse `NodeId ->
//! AlternativeSet` map. Each entry is the per-decision-point top-N
//! candidates the optimizer ranker considered, filtered and ranked
//! per the configured `PlanOptions`. The eventual executor reads
//! `alternatives` per node, takes the winner (Picker 2 territory in
//! the long-term shape; Phase 4 wires the executor to this surface),
//! and dispatches the resolved kernel.
//!
//! ## What changed in Phase 1.5
//!
//! Pre-1.5 this module hosted `NodeKernelBinding`, `TolerancePolicy`,
//! and `resolve_kernel` — the v1 picker that fell out of Phase 7.6
//! step 9b. Those types had zero callers (verified by the
//! 2026-05-30 picker-alternatives audit), so Phase 1.5 retires
//! them and ships the AlternativeSet-based plan in their place. The
//! shape composes with everything Phase 1.1–1.4 already shipped:
//! the enumerator builds candidates, the filter chain narrows them,
//! the cost composer scores them, and the plan stores the
//! top-N-after-filter-and-rank.
//!
//! Phase 4 of the picker-work arc migrates the PipelinedExecutor to
//! consume `ExecutionPlan::alternatives` instead of `compile_node`'s
//! first-registered path. Until then this module ships the
//! infrastructure with no production consumer.

use std::collections::HashMap;

use fuel_graph::{Graph, NodeId};
use fuel_ir::dispatch::{OpKind, SizeClass};
use fuel_ir::probe::BackendId;
use fuel_ir::{DType, DeviceLocation, Error, Result, Shape};

use smallvec::SmallVec;

use crate::kernel::KernelBindingTable;
use crate::pipelined::{build_lookup_dtypes, op_to_op_kind};
use crate::ranker::{
    AlternativeSet, CapabilitiesLookup, ChainInput, DecisionContext, FilterContext, JudgeOracle,
    KEEP_PER_DEVICE, PlacementDp, PrecisionRequirement, TransferEstimator, apply_filter_chain,
    apply_inbound_transfer_costs, composite_ns, compute_static_costs, default_backend_rates,
    default_chain, enumerate_candidates,
};

/// Per-realize execution plan. Built by [`compile_plan`].
///
/// `alternatives` is sparse — view ops, `Op::Const`, and ops
/// `op_to_op_kind` returns `None` for get no entry. The executor's
/// per-node dispatch reads `alternatives.get(&id)` and either takes
/// the winner (top entry after rank) or, in the long-term shape,
/// hands the full set to the runtime selector (Picker 2) for
/// layer-3-telemetry-driven choice.
#[derive(Debug)]
pub struct ExecutionPlan {
    /// Topological order — same shape the executor walks.
    pub order: Vec<NodeId>,
    /// One `AlternativeSet` per kernel-bearing node. Sparse.
    pub alternatives: HashMap<NodeId, AlternativeSet>,
    /// `SystemTopology` generation counter snapshotted at
    /// [`compile_plan`] time. The executor (Phase 4.3) checks this
    /// against `crate::dispatch::topology_generation()` at every
    /// dispatch-chunk boundary; mismatch surfaces
    /// [`Error::TopologyChanged`] so the realize layer can rebuild
    /// the plan against the fresh topology.
    ///
    /// `0` for [`ExecutionPlan::empty()`] — empty plans by definition
    /// have no alternatives to invalidate, so the executor's chunk-
    /// boundary check skips them.
    pub generation: u64,
}

impl ExecutionPlan {
    /// Empty plan. Tests + future direct-call sites.
    pub fn empty() -> Self {
        Self {
            order: Vec::new(),
            alternatives: HashMap::new(),
            generation: 0,
        }
    }

    /// Read-only handle to a node's alternative set. `None` for
    /// nodes outside the plan's map.
    pub fn alternatives(&self, node: NodeId) -> Option<&AlternativeSet> {
        self.alternatives.get(&node)
    }

    /// Mutable handle — used by post-plan refinement passes (Phase
    /// 3's Judge integration may re-rank top-N after the plan is
    /// built).
    pub fn alternatives_mut(&mut self, node: NodeId) -> Option<&mut AlternativeSet> {
        self.alternatives.get_mut(&node)
    }
}

/// Default [`DeviceLocation`] for a `BackendId` when the graph has
/// no per-node placement set. Mirrors the convention in
/// `fuel-graph-router` (CPU is the singleton `Cpu`; GPU backends
/// default to ordinal 0).
pub fn default_device_for(backend: BackendId) -> DeviceLocation {
    match backend {
        BackendId::Cpu => DeviceLocation::Cpu,
        BackendId::Cuda => DeviceLocation::Cuda { gpu_id: 0 },
        BackendId::Vulkan => DeviceLocation::Vulkan { gpu_id: 0 },
        BackendId::Metal => DeviceLocation::Metal { gpu_id: 0 },
        _ => DeviceLocation::Cpu,
    }
}

/// The node's **decision device** — the single resolution chain the planner
/// uses, extracted so every pass that must agree with [`compile_plan`] asks
/// exactly the same question instead of approximating it.
///
/// Priority (see [`PlanOptions::pinned_device`]): explicit `Graph::placement`
/// (the scheduler's per-node assignment) → the realize call's `pinned_device`
/// → `default_device_for(target_backend)` (legacy stamped graphs). `None`
/// means the node has no device context — `compile_plan` turns that into a
/// typed error at its call site.
pub(crate) fn decision_device_for(
    graph: &Graph,
    options: &PlanOptions<'_>,
    id: NodeId,
) -> Option<DeviceLocation> {
    graph
        .placement(id)
        .or(options.pinned_device)
        .or_else(|| graph.target_backend(id).map(default_device_for))
}

/// Every `BackendId` [`compile_plan`] may enumerate a kernel from **for this
/// node**.
///
/// This is the authority on "can this node be served?", and **a pass that asks
/// a weaker question will disagree with the planner.** That divergence is not
/// hypothetical — it shipped: `insert_dtype_fixups` used to ask whether *any*
/// registered backend served a key, so a backend the node could never run on
/// suppressed the dtype reconciliation the node actually needed, and planning
/// then dead-ended at `NoBackendForOp`. Concretely, compiling the Vulkan
/// backend in broke CPU-pinned mixed-precision matmul, because Vulkan is the
/// only backend registering `MatMul[F32, BF16, F32]`. Nothing about that is
/// Vulkan-specific: *any* backend registering a mixed-dtype kernel would have
/// broken every other backend's pinned graphs the same way.
///
/// Mirrors the enumeration in `plan_node`: the decision device's backends
/// (cross-backend when a topology enumerator is configured, else the single
/// legacy backend), plus the off-device fallback set when admission allows it
/// — those backends are genuinely reachable for the node, so a native kernel
/// there is a real alternative and must count.
pub(crate) fn candidate_backends_for(
    graph: &Graph,
    options: &PlanOptions<'_>,
    id: NodeId,
    node: &fuel_graph::Node,
) -> Vec<BackendId> {
    let Some(target_device) = decision_device_for(graph, options, id) else {
        return Vec::new();
    };
    let mut backends: Vec<BackendId> = match options.placements_for_device {
        Some(f) => f(target_device),
        None => vec![
            graph
                .target_backend(id)
                .unwrap_or_else(|| backend_for_device(target_device)),
        ],
    };
    // Picker-arc step 4b: the planner admits off-device candidates for a
    // non-destructive node whenever a fallback enumerator is configured, so
    // those backends ARE candidates here too. Destructive ops never move off
    // the device owning their mutation target, matching `fallback_allowed`.
    if node.op.destructive_input().is_none()
        && let Some(fallback) = options.fallback_placements_for
    {
        backends.extend(fallback(target_device).into_iter().map(|(b, _)| b));
    }
    // Dedup without requiring `Ord` on the non-exhaustive `BackendId`.
    let mut unique: Vec<BackendId> = Vec::with_capacity(backends.len());
    for b in backends {
        if !unique.contains(&b) {
            unique.push(b);
        }
    }
    unique
}

/// The `BackendId` that owns a `DeviceLocation`'s storage substrate.
/// Total — `BackendId` mirrors `DeviceLocation` 1:1 (AOCL/MKL are
/// `kernel_source` siblings under `Cpu`, not distinct backends).
/// Inverse of [`default_device_for`] modulo GPU ordinals.
pub fn backend_for_device(loc: DeviceLocation) -> BackendId {
    match loc {
        DeviceLocation::Cpu => BackendId::Cpu,
        DeviceLocation::Cuda { .. } => BackendId::Cuda,
        DeviceLocation::Vulkan { .. } => BackendId::Vulkan,
        DeviceLocation::Metal { .. } => BackendId::Metal,
    }
}

/// Plan-time configuration the caller hands to [`compile_plan`].
///
/// Most fields have sensible defaults via [`PlanOptions::new`] +
/// chained builders. The two callback fields are required for
/// non-trivial usage — the `'env` lifetime ties them to the
/// caller's environment.
///
/// # `placements_for_device`
///
/// When `Some`, called per kernel-bearing node with the node's
/// target `DeviceLocation` and expected to return every
/// `BackendId` the picker is allowed to consider at that device.
/// This is the cross-co-located-backend integration point —
/// callers wire it to `SystemTopology::backends_for(device)` to
/// unlock the AOCL/MKL/CPU competition story.
///
/// When `None`, the planner uses single-backend placement: only
/// `(target_backend, placement)` from the graph node's side-table.
/// Matches the pre-1.5 picker's pinned-backend behavior; useful as
/// a transitional default and for tests.
///
/// # `pinned_device`
///
/// Picker-arc step 4a. When `Some`, nodes WITHOUT an explicit graph
/// placement resolve their decision device to this location — the
/// realize call's pinned DEVICE. This replaces the bridge's
/// pre-plan monolithic per-node `set_target_backend` loop: the
/// graph no longer needs `target_backend` stamped before planning;
/// the plan's per-node winner is stamped back onto the graph AFTER
/// planning (`fuel-core::pipelined_bridge::stamp_plan_backends`).
///
/// Resolution priority per node: explicit `Graph::placement` (the
/// scheduler's per-node assignment) → `pinned_device` →
/// `default_device_for(target_backend)` (legacy stamped graphs) →
/// error.
///
/// # `fallback_placements_for`
///
/// Picker-arc step 4b, relaxed by planner Stage 2. The closure
/// supplies the OFF-DEVICE placements the picker may consider
/// beyond the node's decision device. Two admission regimes:
///
/// - **Priced (Stage 2)** — when a [`Self::transfer_estimator`] is
///   configured (with `populate_costs` + a capabilities lookup),
///   off-device placements ALWAYS enumerate alongside the decision
///   device's, and every candidate carries an inbound-transfer
///   term; locality emerges from the pricing (ties break toward
///   the decision device via the rank's stable sort). After the
///   rank the set is pruned to the winner's device. Nodes with an
///   explicit `Graph::placement` are HARD pins — they stay on the
///   legacy regime below.
/// - **Legacy (unpriced)** — without an estimator, the closure is
///   consulted only when the decision device can't deliver: empty
///   enumeration for the `(op, dtypes)`, or (Stage-2 fix) a hard
///   filter chain that rejects every decision-device registration.
///
/// Constraints enforced by `compile_plan` in both regimes:
///
/// - **Destructive ops never fall back** (`Op::destructive_input()`
///   is `Some`): in-place mutation semantics don't survive moving
///   the op away from the device that owns its mutation target;
///   those ops keep the plan-time `NoBackendForOp` error.
/// - **Each NODE's surviving alternative set lives on one device**
///   (legacy fallback sets freeze to their single ranked winner;
///   relaxed sets prune to *that node's* winner's device): the
///   residency stitch (`Op::Copy` insertion) is a graph rewrite
///   computed from the static winner, so a dispatch-time selector
///   (the `Op::Branch` arm picker) must not be able to swap in a
///   sibling on a *different* device whose inputs were never copied
///   there. This is a **per-node** invariant on the candidate set —
///   NOT a graph-global "everything realizes on one device" claim.
///   Step E A4a-1 confirmed (CireSnave 2026-06-29) device placement
///   is per-node: the surviving graph is almost never single-device,
///   and even a path between decision points may span devices. The
///   planner already honors that — each node's decision device is
///   `Graph::placement` → `pinned_device` → stamp-default
///   (independently per node), `stamp_plan_backends` writes each
///   node's own winner backend, and `insert_residency_copies`
///   bridges every resulting cross-device boundary edge. A
///   single-device plan is just the degenerate case where every
///   node's per-node device happens to coincide; this prune is a
///   no-op there, so single-device realize stays byte-identical.
///
/// # `capabilities_for`
///
/// Required when `populate_costs` is true (the default). The
/// closure resolves a `BackendId` to its `BackendCapabilities` —
/// callers typically wire it to `SystemTopology::capabilities(...)`
/// or the CapabilityRegistry. Returns `None` for backends not in
/// the topology (their candidates retain default zero cost).
pub struct PlanOptions<'env> {
    /// Hard precision floor the picker enforces. Default: empty
    /// (unconstrained — every candidate passes the precision filter).
    pub precision_requirement: PrecisionRequirement,
    /// Whether to invoke [`compute_static_costs`] + rank after
    /// filtering. Default: true. Disable for tests that just want
    /// to verify enumeration + filtering without cost machinery.
    pub populate_costs: bool,
    /// Cross-co-located-backend placement enumerator. See struct
    /// docs.
    pub placements_for_device: Option<&'env (dyn Fn(DeviceLocation) -> Vec<BackendId> + 'env)>,
    /// Realize-call device pin (picker-arc step 4a). See struct
    /// docs.
    pub pinned_device: Option<DeviceLocation>,
    /// Off-device fallback enumerator for missing-impl ops
    /// (picker-arc step 4b). See struct docs.
    pub fallback_placements_for:
        Option<&'env (dyn Fn(DeviceLocation) -> Vec<(BackendId, DeviceLocation)> + 'env)>,
    /// Capabilities lookup. Required when `populate_costs` is
    /// true.
    pub capabilities_for: Option<&'env CapabilitiesLookup<'env>>,
    /// Optional Phase 3 Layer-2 oracle. When `Some` AND
    /// `populate_costs` is true, the cost composer refines each
    /// candidate's Layer-1 estimate with the Judge's measured
    /// latency at that cell. Candidates the oracle hasn't measured
    /// keep the Layer-1 estimate (silent fallback — absence ≠ zero).
    pub judge: Option<&'env dyn JudgeOracle>,
    /// Planner Stage-2 transfer-cost oracle. When `Some` AND
    /// `populate_costs` is true (with a capabilities lookup), the
    /// cost composer adds a per-candidate inbound-transfer term:
    /// the sum over the node's inputs of
    /// `estimate_transfer_ns(input residency, candidate device,
    /// input bytes)`, for every input whose residency is known at
    /// plan time. Producer residencies are committed along the topo
    /// walk (producers rank before consumers); graph-input
    /// residencies come from [`Self::input_residency`].
    ///
    /// Also the gate for the Stage-2 admission relax: with pricing
    /// active, off-device candidates ALWAYS enumerate (priced) for
    /// nodes without an explicit `Graph::placement` — locality
    /// emerges from the numbers, not from the legacy
    /// missing-impl-only fallback gate.
    ///
    /// Production callers wire this to
    /// `SystemTopology::estimate_transfer_ns` (Stage-1
    /// `TransferCalibration` behind it); unit tests use synthetic
    /// estimators — never live calibration.
    pub transfer_estimator: Option<&'env dyn TransferEstimator>,
    /// Guard against silently planning cost-based placement WITHOUT
    /// transfer pricing. When `true`, [`compile_plan`] fails fast with an
    /// error if [`Self::transfer_estimator`] is `None` — i.e. the caller
    /// has declared "I intend to price transfer; if the estimator isn't
    /// wired, that's a bug, not a silent revert to latency-greedy."
    ///
    /// Default `false` so the many unit tests that legitimately plan
    /// *unpriced* (see `compile_plan_without_estimator_prices_nothing`) are
    /// unaffected. The PRODUCTION realize entry (`fuel-core`'s
    /// `build_optimized_graph`) sets this whenever cost-based cross-device
    /// placement is active, so a future edit that drops the
    /// `.with_transfer_estimator(...)` wiring — or a new production planner
    /// that forgets it — fails loudly at plan-build time instead of quietly
    /// pricing every transfer at zero. Opt-in rather than default-on
    /// because "cost placement, unpriced" is a supported mode for callers
    /// that ask for it; production simply never does.
    pub require_transfer_pricing: bool,
    /// Residency resolver for graph INPUTS whose bytes already
    /// exist outside the plan — `Op::Const` storages uploaded by
    /// the const cache and persistent `initial` slots
    /// (InferenceContext). Consulted while threading residency
    /// through the plan walk, with the same priority the bridge's
    /// `effective_placements` gives cached storages: after
    /// residency-declaring ops and explicit placements, before the
    /// plan's own winner. `None` for a node means "not a resident
    /// input" — its residency resolves through the remaining rules
    /// (or stays unknown, in which case no transfer term fires on
    /// its edges).
    pub input_residency: Option<&'env (dyn Fn(NodeId) -> Option<DeviceLocation> + 'env)>,
    /// Planner Stage 4: incremental extension over a previously
    /// compiled plan. Nodes already covered by `reuse_plan` (an
    /// `AlternativeSet` exists for them) skip enumeration / filter /
    /// cost / DP entirely — their stored set is cloned into the new
    /// plan and their residency commits as the stored winner's
    /// device (mirroring the priority-4 "plan winner" rule), so the
    /// delta's pricing sees the same residency picture the original
    /// walk committed.
    ///
    /// The caller (the plan store) is responsible for only reusing
    /// plans built under the SAME topology generation and pinned
    /// device — `compile_plan` does not re-validate the reused sets.
    /// Reused candidates keep their original `inbound_transfer_ns`
    /// terms; a residency shift between builds is repriced only for
    /// delta nodes (acceptable drift — placement correctness is
    /// re-stitched per realize by the bridge's residency passes).
    pub reuse_plan: Option<&'env ExecutionPlan>,
    /// Gate for the Stage-2 relaxed **cross-device** placement (cost-based
    /// off-device enumeration). Default: `true` — a node without an explicit
    /// `Graph::placement` may be priced onto (and moved to) a cheaper sibling
    /// device, the "plan IS the graph" auto-placement.
    ///
    /// Set `false` to keep every node on its pinned/decision device: the
    /// relaxed branch is suppressed, so no off-device candidate is enumerated
    /// on cost grounds.
    ///
    /// This is what a **reference / single-device realize** wants. A CPU-pinned
    /// oracle must genuinely run on CPU — never cost-relocated to the very
    /// backend it validates — and by the always-built coverage commitment
    /// (fuel-cpu-backend ships a `bit_stable_on_same_hardware` kernel for every
    /// primitive op) the CPU decision device can always supply a kernel, so
    /// suppressing off-device placement never strands a node. A single-op
    /// parity probe pinned to backend B likewise stays on B regardless of op
    /// size. Explicit `Graph::placement` is a hard pin either way; this flag
    /// governs only the *un-pinned* nodes.
    pub allow_cost_placement: bool,
    /// Baracuda dispatch-telemetry hooks (behind the `telemetry` cargo
    /// feature). When `Some` AND the config's mode is not `Off`,
    /// `compile_plan` runs a post-plan pass that emits a structural
    /// **miss** for every node whose winning kernel is the FKC generic
    /// fallback (see [`crate::telemetry::TelemetryHooks`]). `None` (the
    /// default) makes the pass a no-op — the branch-predictable off gate,
    /// so a default realize never touches the sink or the provider.
    #[cfg(feature = "telemetry")]
    pub telemetry: Option<&'env crate::telemetry::TelemetryHooks<'env>>,
}

impl Default for PlanOptions<'_> {
    fn default() -> Self {
        Self {
            precision_requirement: PrecisionRequirement::default(),
            populate_costs: true,
            placements_for_device: None,
            pinned_device: None,
            fallback_placements_for: None,
            capabilities_for: None,
            judge: None,
            transfer_estimator: None,
            require_transfer_pricing: false,
            input_residency: None,
            reuse_plan: None,
            allow_cost_placement: true,
            #[cfg(feature = "telemetry")]
            telemetry: None,
        }
    }
}

impl<'env> PlanOptions<'env> {
    /// Builder constructor; same fields as [`Default`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the precision requirement hard filter.
    pub fn with_precision_requirement(mut self, req: PrecisionRequirement) -> Self {
        self.precision_requirement = req;
        self
    }

    /// Disable cost computation + ranking. Sets `populate_costs =
    /// false`; alternatives end up in enumerator order, useful for
    /// tests that don't care about ranking.
    pub fn without_cost_population(mut self) -> Self {
        self.populate_costs = false;
        self
    }

    /// Attach a co-located-backend enumerator. Wire to
    /// `SystemTopology::backends_for(device)` for production.
    pub fn with_placements_for_device(
        mut self,
        f: &'env (dyn Fn(DeviceLocation) -> Vec<BackendId> + 'env),
    ) -> Self {
        self.placements_for_device = Some(f);
        self
    }

    /// Pin the realize call's device. Nodes without an explicit
    /// graph placement enumerate their candidates at this location
    /// — no per-node `target_backend` stamp needed before planning.
    /// Picker-arc step 4a.
    pub fn with_pinned_device(mut self, device: DeviceLocation) -> Self {
        self.pinned_device = Some(device);
        self
    }

    /// Suppress the Stage-2 relaxed cross-device placement (see
    /// [`Self::allow_cost_placement`]): every un-pinned node stays on its
    /// decision device; only the missing-impl fallback may still go
    /// off-device. Use for a reference / single-device realize.
    pub fn without_cost_placement(mut self) -> Self {
        self.allow_cost_placement = false;
        self
    }

    /// Declare that this plan MUST price transfer: [`compile_plan`] errors
    /// if no [`Self::transfer_estimator`] is set. Production realize wires
    /// this whenever cost-based cross-device placement is active, so a
    /// dropped or forgotten `.with_transfer_estimator(...)` fails loudly at
    /// plan-build time rather than silently reverting to latency-greedy
    /// placement (transfers priced at zero). See
    /// [`Self::require_transfer_pricing`].
    pub fn require_transfer_pricing(mut self) -> Self {
        self.require_transfer_pricing = true;
        self
    }

    /// Attach an off-device fallback enumerator. With a Stage-2
    /// transfer estimator configured, off-device placements always
    /// enumerate, priced; without one, the closure is consulted
    /// only when the decision device can't deliver (no impl, or
    /// every impl hard-filtered). Wire to the `SystemTopology`
    /// device list for production. Picker-arc step 4b + planner
    /// Stage 2 — see struct docs for the destructive-op,
    /// hard-pin, and single-device constraints.
    pub fn with_fallback_placements_for(
        mut self,
        f: &'env (dyn Fn(DeviceLocation) -> Vec<(BackendId, DeviceLocation)> + 'env),
    ) -> Self {
        self.fallback_placements_for = Some(f);
        self
    }

    /// Attach a capabilities lookup. Required when `populate_costs`
    /// is true.
    pub fn with_capabilities_for(mut self, f: &'env CapabilitiesLookup<'env>) -> Self {
        self.capabilities_for = Some(f);
        self
    }

    /// Attach a Layer-2 Judge oracle. When set, `compile_plan`'s
    /// cost composer refines Layer-1 estimates with measured
    /// latencies per [`JudgeOracle::measured_latency_ns`].
    pub fn with_judge(mut self, judge: &'env dyn JudgeOracle) -> Self {
        self.judge = Some(judge);
        self
    }

    /// Attach a Stage-2 transfer-cost oracle. See the
    /// [`Self::transfer_estimator`] field docs — enables inbound-
    /// transfer pricing AND the priced off-device admission relax.
    /// Production wires `SystemTopology::estimate_transfer_ns`;
    /// tests use synthetic estimators.
    pub fn with_transfer_estimator(mut self, est: &'env dyn TransferEstimator) -> Self {
        self.transfer_estimator = Some(est);
        self
    }

    /// Attach a residency resolver for already-resident graph
    /// inputs (consts + persistent cache slots). See the
    /// [`Self::input_residency`] field docs.
    pub fn with_input_residency(
        mut self,
        f: &'env (dyn Fn(NodeId) -> Option<DeviceLocation> + 'env),
    ) -> Self {
        self.input_residency = Some(f);
        self
    }

    /// Attach a base plan for incremental extension (planner Stage
    /// 4). See the [`Self::reuse_plan`] field docs.
    pub fn with_reuse_plan(mut self, base: &'env ExecutionPlan) -> Self {
        self.reuse_plan = Some(base);
        self
    }

    /// Attach Baracuda dispatch-telemetry hooks (behind the `telemetry`
    /// cargo feature). With them set and the config not `Off`,
    /// `compile_plan`'s post-plan pass emits a structural miss per node
    /// whose winning kernel is the FKC generic fallback. See the
    /// [`Self::telemetry`] field docs.
    #[cfg(feature = "telemetry")]
    pub fn with_telemetry(mut self, hooks: &'env crate::telemetry::TelemetryHooks<'env>) -> Self {
        self.telemetry = Some(hooks);
        self
    }
}

/// Build an [`ExecutionPlan`] from a topologically-ordered node
/// sequence + binding table + options.
///
/// For each node in `order`:
///
/// - If `op_to_op_kind(&node.op)` returns `None` (view ops,
///   `Op::Const`, ops not yet wired into dispatch), the node gets
///   no entry — view-only adoption + const adoption flow through
///   the legacy paths unchanged.
/// - `Op::Copy` / `Op::Move` nodes get no entry either (picker-arc
///   step 4a): their kernel backend is residency-determined (the
///   SOURCE backend), so transfer-kernel resolution stays with the
///   executor's `compile_node` path keyed by the bridge-maintained
///   source-backend stamp.
/// - Otherwise:
///   1. Resolve `(op_kind, dtypes, device)` from the graph + the
///      options (`device` via `Graph::placement`, then
///      `PlanOptions::pinned_device`, then
///      `default_device_for(target_backend)` for legacy stamped
///      graphs).
///   2. Build the placement list — either via
///      `options.placements_for_device` (cross-backend mode) or
///      `[(target_backend, device)]` (legacy single-backend mode).
///   3. Enumerate candidates via [`enumerate_candidates`].
///   4. Apply the default filter chain (precision floor +
///      strided-input pref + bit-stable pref).
///   5. If `populate_costs`, compute static costs via the
///      capabilities lookup — plus, when a
///      [`PlanOptions::transfer_estimator`] is configured, the
///      Stage-2 inbound-transfer term per candidate — and rank
///      ascending by composite cost.
///   6. Retain the per-ending-device Pareto frontier + crowding cap
///      ([`AlternativeSet::retain_per_device_frontier`], `keep =
///      KEEP_PER_DEVICE`).
///   7. If the set is empty after filtering, return
///      [`Error::NoBackendForOp`] (fail-fast at plan time, not
///      deep in executor dispatch).
///   8. Insert into `plan.alternatives`.
///
/// ## Stage-2 residency threading
///
/// `order` is topological, so each node's producers are planned —
/// and their placements committed — before the node itself ranks.
/// The walk maintains a per-node residency view mirroring the
/// bridge's `effective_placements` priority: residency-declaring
/// ops (`Op::Copy`/`Move`/`Alloc` targets) → explicit
/// `Graph::placement` → caller-supplied input residency
/// ([`PlanOptions::input_residency`], consts + persistent cache
/// slots) → the plan's own committed winner → backend stamp →
/// view-op pass-through inheritance. Inputs whose residency
/// resolves feed the inbound-transfer pricing; unknown residency
/// prices no term (conservative — an unpriceable edge neither
/// justifies nor penalizes a move).
///
/// ## Stage-3 carry-forward placement DP
///
/// Relaxed nodes whose admissible candidates span ≥ 2 devices defer
/// their winner choice to the carry-forward DP
/// ([`crate::ranker::placement_dp`]): the walk opens a state row
/// per such node (`best[d]` = accumulated cost arriving with output
/// on `d`), extends rows along the dominant chain (view-shaped
/// pass-throughs alias through), merges joins per-device by the
/// cheaper producer state, prices the exit crossing back to the
/// realize target on terminal rows, and backtracks the cheapest
/// terminal state into per-node device commitments. Each committed
/// row's `AlternativeSet` is then priced, ranked, and pruned to the
/// committed device — preserving the Stage-2 single-device-set
/// invariant the residency stitch depends on.
///
/// Gating: a row only opens when the node is relaxed (estimator +
/// fallback enumerator configured, no explicit placement, not
/// destructive, not already-resident) AND its candidates span more
/// than one device. Single-device plans never open a row and take
/// the Stage-2 greedy path bit-identically — zero overhead beyond
/// an empty-map check per node.
///
/// Documented debt (see the `placement_dp` module docs): fan-out
/// rows chain into their FIRST consumer only (later consumers price
/// that edge as unknown); diamonds re-anchor the second branch as a
/// fresh chain; hard-pinned consumers of an open row close it
/// toward their device at walk time, which prices the crossing but
/// cannot revisit it later.
pub fn compile_plan(
    graph: &Graph,
    order: &[NodeId],
    bindings_table: &KernelBindingTable,
    options: &PlanOptions<'_>,
) -> Result<ExecutionPlan> {
    // Transfer-pricing guard (opt-in via `PlanOptions::require_transfer_pricing`).
    // A caller that declared it must price transfer, yet wired no estimator,
    // would silently price every cross-device transfer at zero — reverting
    // placement to latency-greedy. That is precisely the drift the production
    // realize path guards against, so fail fast at plan-build time. Result, not
    // panic (never panic on production paths); build-time validation, not a
    // runtime surprise mid-dispatch.
    if options.require_transfer_pricing && options.transfer_estimator.is_none() {
        return Err(Error::Msg(
            "compile_plan: require_transfer_pricing is set but no transfer \
             estimator is wired — placement would silently price transfers at \
             zero (latency-greedy). Wire PlanOptions::with_transfer_estimator(..) \
             or drop the pricing requirement."
                .into(),
        )
        .bt());
    }

    // Snapshot the topology generation at plan-build time. The
    // executor (Phase 4.3) checks this against the live counter at
    // every dispatch-chunk boundary; mismatch surfaces
    // `Error::TopologyChanged` so the realize layer can rebuild
    // against the fresh topology.
    let generation = crate::dispatch::topology_generation();
    let mut alternatives_map = HashMap::with_capacity(order.len());

    // Stage-2 residency view, threaded along the walk (see the
    // doc comment). Nodes matching no resolution rule stay absent —
    // residency unknown, no transfer term fires on their edges.
    let mut residency: HashMap<NodeId, DeviceLocation> = HashMap::with_capacity(order.len());

    // Stage-3 DP state. Empty (and cost-free) unless multi-device
    // placement freedom actually materializes at some node.
    let mut dp = PlacementDp::new();
    let mut dp_drafts: HashMap<NodeId, NodeDraft> = HashMap::new();

    for &id in order {
        let node = graph.node(id);

        // Planner Stage 4: incremental-extension fast path. A node
        // already covered by the base plan reuses its stored set
        // verbatim; its residency commits with the same priority
        // order the full walk uses (early rules, then the stored
        // winner's device — the priority-4 "plan winner" rule).
        if let Some(base) = options.reuse_plan
            && let Some(set) = base.alternatives(id)
        {
            let resident = early_residency(graph, options, id, node)
                .or_else(|| set.winner().map(|w| w.device));
            if let Some(loc) = resident {
                residency.insert(id, loc);
            }
            alternatives_map.insert(id, set.clone());
            continue;
        }

        // Residency priorities 1–3: definitional transfer/alloc
        // targets, explicit placements, already-resident inputs.
        let early = early_residency(graph, options, id, node);

        let mut dp_row_opened = false;
        let winner_device = match build_node_draft(graph, id, node, bindings_table, options)? {
            Some(draft) => {
                let devices = distinct_devices(&draft.set);
                let dp_estimator = options
                    .transfer_estimator
                    .filter(|_| draft.relaxed && early.is_none() && devices.len() >= 2);
                if let Some(est) = dp_estimator {
                    // Stage 3: open a DP row instead of committing a
                    // winner. Residency stays unresolved until the
                    // backtrack commits a device.
                    open_dp_row(
                        &mut dp,
                        graph,
                        id,
                        node,
                        &draft.set,
                        &devices,
                        &residency,
                        est,
                        options.capabilities_for,
                    );
                    dp_drafts.insert(id, draft);
                    dp_row_opened = true;
                    None
                } else {
                    // Stage-2 greedy finalize. When the node's device
                    // is already determined (single-device candidate
                    // set — hard pins, destructive ops, frozen
                    // fallbacks), close any open producer rows toward
                    // it FIRST so the close prices the crossing and
                    // the finalize prices the edge.
                    if let Some(est) = options.transfer_estimator
                        && let [single] = devices.as_slice()
                    {
                        close_open_inputs(&mut dp, graph, node, *single, est, &mut residency);
                    }
                    let set = finalize_node_greedy(
                        graph,
                        node,
                        draft,
                        bindings_table,
                        options,
                        &residency,
                    )?;
                    let dev = set.winner().map(|w| w.device);
                    // Multi-device greedy sets (legacy missing-impl
                    // fallback shapes) learn their device only after
                    // ranking — close producers toward the winner.
                    if devices.len() > 1
                        && let (Some(est), Some(wd)) = (options.transfer_estimator, dev)
                    {
                        close_open_inputs(&mut dp, graph, node, wd, est, &mut residency);
                    }
                    alternatives_map.insert(id, set);
                    dev
                }
            }
            None => {
                // Structural / residency-determined nodes. An
                // `Op::Copy`/`Op::Move` consuming an open row anchors
                // the chain at its transfer target (the production
                // realize-root splice is exactly this shape — closing
                // here IS the exit pricing through an explicit node).
                if let fuel_graph::Op::Copy { target } | fuel_graph::Op::Move { target } = node.op
                    && let Some(est) = options.transfer_estimator
                {
                    close_open_inputs(&mut dp, graph, node, target, est, &mut residency);
                }
                None
            }
        };

        if dp_row_opened {
            // Residency intentionally NOT committed — the DP owns
            // this node's placement; priorities 5–6 must not leak a
            // stamp-derived device downstream.
            continue;
        }

        // Commit this node's residency for downstream consumers.
        let resident = early
            .or(winner_device)
            .or_else(|| {
                // Priority 5: a backend stamp without a plan entry
                // (structural ops + legacy stamps) follows the
                // realize device, or the stamp's default device
                // when no pin is set.
                graph.target_backend(id).map(|b| {
                    options
                        .pinned_device
                        .unwrap_or_else(|| default_device_for(b))
                })
            })
            .or_else(|| {
                // Priority 6: residency-inheriting pass-throughs —
                // view ops, Reshape, Contiguize produce no new
                // residency; they follow their data input (already
                // resolved — the walk is topological).
                if is_passthrough(node) {
                    node.inputs.first().and_then(|i| residency.get(i)).copied()
                } else {
                    None
                }
            });
        if let Some(loc) = resident {
            residency.insert(id, loc);
        } else if is_passthrough(node) {
            // Stage 3: a pass-through of a DP-tracked node keeps the
            // chain connected — alias it to the producer's row so
            // consumers extend through the view. (If the row already
            // committed, surface its device as this view's residency,
            // mirroring priority 6.)
            if let Some(&input) = node.inputs.first() {
                let root = dp.resolve(input);
                if let Some(dev) = dp.committed_device(root) {
                    residency.insert(id, dev);
                } else {
                    dp.add_alias(id, root);
                }
            }
        }
    }

    // Stage 3: commit the DP (exit pricing + backtrack), then
    // finalize each deferred row against the committed placements.
    if !dp_drafts.is_empty() {
        let Some(est) = options.transfer_estimator else {
            // Rows only open with an estimator; this is unreachable
            // in practice but degrades typed rather than panicking.
            return Err(Error::Msg(
                "compile_plan: placement DP rows exist without a transfer \
                 estimator — internal invariant violated"
                    .into(),
            )
            .bt());
        };
        let commits = dp.finish(options.pinned_device, |nid| node_bytes(graph, nid), est);
        for (n, d) in commits {
            residency.insert(n, d);
        }
        // Pass-through residency for view chains the walk left
        // unresolved (views of DP nodes) — topo order, so producers
        // resolve before their views.
        for &id in order {
            if residency.contains_key(&id) {
                continue;
            }
            let node = graph.node(id);
            if is_passthrough(node)
                && let Some(&loc) = node.inputs.first().and_then(|i| residency.get(i))
            {
                residency.insert(id, loc);
            }
        }
        // Finalize deferred rows in topo order: price inbound
        // transfers from the final residencies, rank, prune to the
        // DP-committed device (Stage-2 single-device-set invariant),
        // truncate.
        for &id in order {
            let Some(draft) = dp_drafts.remove(&id) else {
                continue;
            };
            let chosen = dp.committed_device(id).ok_or_else(|| {
                Error::Msg(format!(
                    "compile_plan: placement DP left node {:?} uncommitted — \
                     internal invariant violated",
                    id,
                ))
                .bt()
            })?;
            let node = graph.node(id);
            let mut set = draft.set;
            let inputs = priced_inputs_for(graph, node, &residency);
            apply_inbound_transfer_costs(&mut set, &inputs, est);
            set.rank_by_composite_cost();
            let keep: Vec<usize> = set
                .alternatives()
                .iter()
                .enumerate()
                .filter_map(|(i, c)| (c.device == chosen).then_some(i))
                .collect();
            if keep.len() < set.len() {
                set.retain_indices(&keep);
            }
            // PR-B2: retire the fixed top-N. The set is now pruned to
            // the DP-committed single device, so the per-ending-device
            // Pareto frontier keeps that device's non-dominated paths
            // (crowding-capped at KEEP_PER_DEVICE); the arm-0 winner —
            // the realize pick — is preserved at index 0.
            set.retain_per_device_frontier(KEEP_PER_DEVICE);
            if set.is_empty() {
                // Defensive — the committed device came from the
                // row's own candidates, so this shouldn't fire.
                return Err(missing_binding_error(
                    bindings_table,
                    draft.op_kind,
                    &draft.dtypes,
                    draft.diag_backend,
                ));
            }
            alternatives_map.insert(id, set);
        }
    }

    // Baracuda dispatch + structural-miss telemetry (opt-in, behind the
    // `telemetry` feature). Post-plan pass over the finalized winners; a no-op
    // unless hooks are threaded AND enabled — the emission never perturbs the
    // plan.
    #[cfg(feature = "telemetry")]
    emit_plan_telemetry(graph, &alternatives_map, bindings_table, options);

    Ok(ExecutionPlan {
        order: order.to_vec(),
        alternatives: alternatives_map,
        generation,
    })
}

/// Post-plan Baracuda **dispatch + structural-miss** emission (behind the
/// `telemetry` feature). For every kernel-bearing node it emits a
/// [`crate::telemetry::DispatchRecord`] for the winning kernel (the `chosen`
/// signal, every planned cell) and — when the winner is the FKC generic
/// fallback (`is_generic`) — additionally a
/// [`crate::telemetry::MissRecord`] via the shared
/// [`crate::telemetry::detect_miss_precomputed`] core.
///
/// ## What each mode emits
/// - **Off** (default): nothing (the branch-predictable gate below).
/// - **Coarse**: a `DispatchRecord` per cell with an EMPTY `candidates[]`
///   (chosen + count + structure_key + hw) plus the miss histogram — no Judge
///   reads.
/// - **Detailed**: Coarse plus `candidates[]` filled from the plan-time Judge
///   oracle (the SAME `measured_latency_ns` read the ranker uses), one measured
///   latency per admitted alternative (`None` for unmeasured cells).
///
/// The `chosen`/`candidates`/`fallback` [`crate::telemetry::ImplId`]s carry the
/// FKC revision threaded onto [`crate::kernel::BindingEntry`] (0 = untracked for
/// hand-written kernels). Emission is best-effort and can never break dispatch —
/// a poisoned sink lock is swallowed to a no-op, and a missing provider token
/// forms no miss signal (never a fabricated one). The plan itself is never
/// mutated, so a run with telemetry on is byte-identical to one with it off save
/// for the sink's contents.
#[cfg(feature = "telemetry")]
fn emit_plan_telemetry(
    graph: &Graph,
    alternatives: &HashMap<NodeId, AlternativeSet>,
    bindings_table: &KernelBindingTable,
    options: &PlanOptions<'_>,
) {
    use crate::telemetry::{
        DispatchRecord, FdxOperandDesc, ImplId, TELEMETRY_SCHEMA_VERSION, detect_miss_precomputed,
    };

    // Off gate (branch-predictable, first): no hooks or disabled ⇒ nothing.
    let Some(hooks) = options.telemetry else {
        return;
    };
    if !hooks.config.is_enabled() {
        return;
    }
    let arch = hooks.arch_tag();
    let want_candidates = hooks.config.mode.wants_candidates();

    for (&id, set) in alternatives {
        let Some(winner) = set.winner() else {
            continue;
        };
        let node = graph.node(id);
        let Some(op_kind) = op_to_op_kind(&node.op) else {
            continue;
        };
        let dtypes = build_lookup_dtypes(graph, node);

        // Resolve the winning kernel back to its binding entry to read its
        // registration-time `is_generic` bit + FKC revision (the winning ENTRY
        // is in hand here — the finalize gate guarantees distinct fn-pointers
        // per key, so the pointer match is unambiguous).
        let winner_ptr = winner.kernel as *const () as usize;
        let winner_entry = bindings_table
            .lookup_alternatives(op_kind, &dtypes, winner.backend)
            .iter()
            .find(|e| e.kernel as *const () as usize == winner_ptr)
            .copied();
        let is_generic = winner_entry.is_some_and(|e| e.is_generic);
        let winner_revision = winner_entry.map_or(0, |e| e.kernel_revision_hash);

        // The demand descriptor for the LIVE operands (shared by the structure
        // key + the miss detector). Baracuda's `structure_key` keys on operands
        // in inputs-THEN-OUTPUT order (its `OperandDesc` convention / reply), so
        // the node's own output operand is folded in after the inputs — derived
        // from the node's output layout + dtype exactly as the inputs are.
        let mut operands: Vec<FdxOperandDesc> = node
            .inputs
            .iter()
            .map(|&input_id| {
                FdxOperandDesc::from_layout(&graph.layout(input_id), graph.node(input_id).dtype)
            })
            .collect();
        operands.push(FdxOperandDesc::from_layout(&graph.layout(id), node.dtype));
        let op_class = format!("{op_kind:?}");

        // The structure key for the LIVE operands (Fuel CALLS the provider,
        // never derives). `None` when the callable is unlinked (the v1
        // NullProvider) — the record then omits the key, and no miss forms.
        let structure_key = hooks.provider.structure_key(&op_class, &operands, &arch);

        // The chosen (winner) `ImplId`.
        let chosen = ImplId {
            backend: winner.backend,
            op: op_kind,
            dtypes: dtypes.to_vec(),
            kernel_source: winner.kernel_source.to_string(),
            kernel_revision_hash: winner_revision,
        };

        // `candidates[]` — Detailed mode only; each admitted alternative's own
        // Judge latency (Coarse leaves it empty per the documented semantics).
        let candidates = if want_candidates {
            build_candidates(set, op_kind, &dtypes, bindings_table, options.judge)
        } else {
            Vec::new()
        };

        // Emit the dispatch record for this cell (every planned cell).
        let dispatch = DispatchRecord {
            schema: TELEMETRY_SCHEMA_VERSION,
            structure_key,
            chosen: chosen.clone(),
            candidates,
            count: 1,
            hw: hooks.hw.clone(),
        };
        if let Ok(mut sink) = hooks.sink.lock() {
            sink.record_dispatch(dispatch);
        }

        // Structural miss — only when the winner is the FKC generic fallback.
        // Reuse the shared emission core (single source of truth for the
        // never-fabricate rule); `fallback` is the generic winner's `ImplId`.
        if let Some(miss) = detect_miss_precomputed(
            is_generic,
            chosen,
            &op_class,
            &operands,
            &arch,
            hooks.provider,
            hooks.hw.clone(),
        ) {
            // Never break dispatch: a poisoned sink lock is swallowed to a
            // no-op (best-effort telemetry). `record_miss` itself cannot fail.
            if let Ok(mut sink) = hooks.sink.lock() {
                sink.record_miss(miss);
            }
        }
    }
}

/// Build the `candidates[]` for a dispatch record: one
/// [`crate::telemetry::Candidate`] per admitted alternative in `set`, each
/// carrying its OWN measured latency read from the plan-time Judge oracle (the
/// SAME `measured_latency_ns` lookup the ranker/cost composer uses, keyed by the
/// decision-point context the planner stamped). `None` = unmeasured cell — never
/// a fabricated `0`; the static estimate stood. Each candidate's `ImplId`
/// resolves its FKC revision back through the binding table by fn-pointer
/// identity (0 = untracked).
#[cfg(feature = "telemetry")]
fn build_candidates(
    set: &AlternativeSet,
    op_kind: OpKind,
    dtypes: &[DType],
    bindings_table: &KernelBindingTable,
    judge: Option<&dyn JudgeOracle>,
) -> Vec<crate::telemetry::Candidate> {
    use crate::telemetry::{Candidate as TeleCandidate, ImplId};

    // The decision-point identity the planner stamped (op, principal dtype,
    // size class) — the exact Judge key axes. Absent ⇒ no measured latencies.
    let ctx = set.context();
    set.alternatives()
        .iter()
        .map(|cand| {
            let ptr = cand.kernel as *const () as usize;
            let revision = bindings_table
                .lookup_alternatives(op_kind, dtypes, cand.backend)
                .iter()
                .find(|e| e.kernel as *const () as usize == ptr)
                .map_or(0, |e| e.kernel_revision_hash);
            let impl_id = ImplId {
                backend: cand.backend,
                op: op_kind,
                dtypes: dtypes.to_vec(),
                kernel_source: cand.kernel_source.to_string(),
                kernel_revision_hash: revision,
            };
            let latency_ns = match (judge, ctx) {
                (Some(j), Some(ctx)) => j.measured_latency_ns(
                    ctx.op,
                    ctx.principal_dtype,
                    ctx.size_class,
                    cand.backend,
                    cand.kernel_source,
                    // Same `revision` the `ImplId` above is keyed by (one
                    // binding, two uses) — NOT a literal 0, which would query
                    // the untracked cell and collapse variant siblings, the
                    // exact regression per-variant `kernel_revision_hash` keys
                    // the oracle to prevent.
                    revision,
                ),
                _ => None,
            };
            TeleCandidate {
                impl_id,
                latency_ns,
            }
        })
        .collect()
}

/// Does this op receive an `AlternativeSet` entry from
/// [`compile_plan`]? The single source of truth for the plan-entry
/// gate (used by `build_node_draft` and `PlanOptions::reuse_plan`
/// incremental extension): `true` exactly when `op_to_op_kind` maps
/// the op AND it is not a residency-determined transfer (`Op::Copy` /
/// `Op::Move`, whose kernel backend the executor resolves from the
/// bridge-maintained source-backend stamp).
pub fn node_needs_plan_entry(op: &fuel_graph::Op) -> bool {
    op_to_op_kind(op).is_some()
        && !matches!(
            op,
            fuel_graph::Op::Copy { .. } | fuel_graph::Op::Move { .. }
        )
}

/// Residency-inheriting pass-throughs: view ops, `Reshape`,
/// `Contiguize` produce no new residency — their bytes follow their
/// data input.
fn is_passthrough(node: &fuel_graph::Node) -> bool {
    node.op.is_view_op()
        || matches!(
            node.op,
            fuel_graph::Op::Reshape(_) | fuel_graph::Op::Contiguize
        )
}

/// Distinct candidate devices in first-seen order (the enumerator
/// lists the decision device first, so index 0 is the locality
/// anchor and DP argmin ties break toward it).
fn distinct_devices(set: &AlternativeSet) -> SmallVec<[DeviceLocation; 4]> {
    let mut out: SmallVec<[DeviceLocation; 4]> = SmallVec::new();
    for c in set.alternatives() {
        if !out.contains(&c.device) {
            out.push(c.device);
        }
    }
    out
}

/// Output bytes of a node — element count × dtype size, saturating.
/// Sub-byte dtypes price latency-only (same bounded undercount as
/// [`priced_inputs_for`]).
fn node_bytes(graph: &Graph, id: NodeId) -> u64 {
    let n = graph.node(id);
    (n.shape.elem_count() as u64).saturating_mul(n.dtype.size_in_bytes() as u64)
}

/// Stage 3: open a DP row for a relaxed multi-device node. Inputs
/// partition into known-fixed residencies (priced per device into
/// the row's base cost), open-row chain edges (extended via the DP
/// recurrence), and unknown edges (no term — conservative).
#[allow(clippy::too_many_arguments)]
fn open_dp_row(
    dp: &mut PlacementDp,
    graph: &Graph,
    id: NodeId,
    node: &fuel_graph::Node,
    set: &AlternativeSet,
    devices: &[DeviceLocation],
    residency: &HashMap<NodeId, DeviceLocation>,
    est: &dyn TransferEstimator,
    caps_for: Option<&CapabilitiesLookup<'_>>,
) {
    // Per-device node cost: the cheapest same-device candidate's
    // composite (static + Judge-refined) figure. Each candidate's FLOP
    // / byte pressure is converted to nanoseconds by ITS backend's
    // throughput — the authoritative registered `BackendCapabilities`
    // figure when caps are in hand, else the per-backend prior. This is
    // what makes the cross-device placement DP honest: a GPU candidate
    // is priced on GPU throughput, so it can out-price a CPU candidate
    // for large parallel work despite paying an inbound transfer.
    let rates_for = |backend: BackendId| -> (f64, f64) {
        caps_for
            .and_then(|f| f(backend))
            .map(|caps| {
                (
                    caps.compute_throughput_flops_per_ns,
                    caps.mem_bandwidth_bytes_per_ns,
                )
            })
            .unwrap_or_else(|| default_backend_rates(backend))
    };
    let device_costs: Vec<(DeviceLocation, u64)> = devices
        .iter()
        .map(|&d| {
            let min = set
                .alternatives()
                .iter()
                .filter(|c| c.device == d)
                .map(|c| {
                    let (cr, bw) = rates_for(c.backend);
                    composite_ns(&c.static_cost, cr, bw)
                })
                .min()
                .unwrap_or(u64::MAX);
            (d, min)
        })
        .collect();

    let mut fixed: Vec<(DeviceLocation, u64)> = Vec::new();
    let mut chains: Vec<ChainInput> = Vec::new();
    for &input in &node.inputs {
        let bytes = node_bytes(graph, input);
        if let Some(&src) = residency.get(&input) {
            fixed.push((src, bytes));
            continue;
        }
        let root = dp.resolve(input);
        if dp.is_open(root) {
            match chains.iter_mut().find(|ci| ci.producer == root) {
                Some(ci) => ci.edge_bytes.push(bytes),
                None => chains.push(ChainInput {
                    producer: root,
                    edge_bytes: smallvec::smallvec![bytes],
                }),
            }
        } else if let Some(dev) = dp.committed_device(root) {
            // Producer row already closed toward an earlier consumer
            // — its device is fixed now.
            fixed.push((dev, bytes));
        }
        // else: chained-but-uncommitted or genuinely unknown — no
        // term (documented fan-out debt).
    }
    dp.push_row(id, &device_costs, &fixed, &chains, est);
}

/// Close any open producer rows feeding `node` toward `toward` —
/// the consumer's (already-known) device. Commitments merge into
/// the walk's residency view so the consumer's own pricing and all
/// later consumers see them; direct inputs that alias to a
/// committed row inherit its device (pass-through residency).
fn close_open_inputs(
    dp: &mut PlacementDp,
    graph: &Graph,
    node: &fuel_graph::Node,
    toward: DeviceLocation,
    est: &dyn TransferEstimator,
    residency: &mut HashMap<NodeId, DeviceLocation>,
) {
    for &input in &node.inputs {
        if residency.contains_key(&input) {
            continue;
        }
        let root = dp.resolve(input);
        if dp.is_open(root) {
            let commits = dp.close_toward(root, toward, &[node_bytes(graph, input)], est);
            for (n, d) in commits {
                residency.insert(n, d);
            }
        }
        if !residency.contains_key(&input)
            && let Some(d) = dp.committed_device(root)
        {
            residency.insert(input, d);
        }
    }
}

/// Stage-2 residency, priorities 1–3: rules that resolve a node's
/// placement WITHOUT the plan — residency-declaring ops
/// (`Op::Copy`/`Move`/`Alloc` carry their output's location in the
/// op variant), explicit `Graph::placement` (scheduler
/// assignments), and the caller's already-resident input lookup
/// (consts + persistent cache slots via
/// [`PlanOptions::input_residency`]). Mirrors the top of the
/// bridge's `effective_placements` priority list.
fn early_residency(
    graph: &Graph,
    options: &PlanOptions<'_>,
    id: NodeId,
    node: &fuel_graph::Node,
) -> Option<DeviceLocation> {
    match node.op {
        fuel_graph::Op::Copy { target }
        | fuel_graph::Op::Move { target }
        | fuel_graph::Op::Alloc { target } => return Some(target),
        _ => {}
    }
    if let Some(loc) = graph.placement(id) {
        return Some(loc);
    }
    options.input_residency.and_then(|f| f(id))
}

/// Collect `(resident device, byte size)` for every input of `node`
/// whose residency is known. Bytes = element count × dtype size.
/// Sub-byte dtypes (`size_in_bytes() == 0`) price latency-only — a
/// bounded undercount, acceptable until a packed-bit byte-size
/// helper exists (no such dtype crosses devices today).
fn priced_inputs_for(
    graph: &Graph,
    node: &fuel_graph::Node,
    residency: &HashMap<NodeId, DeviceLocation>,
) -> Vec<(DeviceLocation, u64)> {
    node.inputs
        .iter()
        .filter_map(|&input_id| {
            let src = residency.get(&input_id).copied()?;
            let in_node = graph.node(input_id);
            let bytes = (in_node.shape.elem_count() as u64)
                .saturating_mul(in_node.dtype.size_in_bytes() as u64);
            Some((src, bytes))
        })
        .collect()
}

/// The residency-independent half of one node's plan: the
/// enumerated + filtered + statically-costed candidate set, plus
/// the flags the finalize step (greedy or DP) needs. Produced by
/// [`build_node_draft`]; consumed by [`finalize_node_greedy`] or
/// deferred into the Stage-3 DP.
struct NodeDraft {
    set: AlternativeSet,
    /// Stage-2 priced-admission regime (estimator + fallback +
    /// non-destructive + no explicit placement).
    relaxed: bool,
    /// Set was produced by the legacy missing-impl fallback —
    /// freezes to its single ranked winner.
    from_fallback: bool,
    /// Static costs (Layer 1 + Judge Layer 2) were computed —
    /// gates transfer pricing + ranking in the finalize step,
    /// mirroring the `populate_costs && capabilities_for` gate.
    costed: bool,
    op_kind: OpKind,
    dtypes: Vec<DType>,
    diag_backend: BackendId,
}

/// `compile_plan`'s per-node enumeration phase: enumeration +
/// filter chain + static cost composition for one kernel-bearing
/// node — everything that does NOT depend on producer residency.
/// Returns `Ok(None)` for nodes that get no plan entry (view ops,
/// `Op::Const`, residency-determined `Op::Copy`/`Op::Move`, ops
/// without a dispatch OpKind).
fn build_node_draft(
    graph: &Graph,
    id: NodeId,
    node: &fuel_graph::Node,
    bindings_table: &KernelBindingTable,
    options: &PlanOptions<'_>,
) -> Result<Option<NodeDraft>> {
    // Gate shared with the plan store's coverage check
    // ([`node_needs_plan_entry`]): no OpKind mapping → no entry;
    // Op::Copy / Op::Move are residency-determined, not picker
    // decisions — their kernel runs on the backend that owns the
    // SOURCE bytes (`copy_from_cpu_wrapper` for H2D, the source
    // backend's download wrapper for D2H). Enumerating them
    // against a single decision device would key the lookup at
    // the wrong end of the transfer for placement-carrying
    // copies (the consumer device, where the H2D copy's kernel
    // does NOT run). The executor's legacy `compile_node` path
    // resolves these via the source-backend `target_backend`
    // stamp maintained by the bridge's copy-insertion passes.
    // Picker-arc step 4a; Op::Move added with the executor's
    // Move arm (Op::Move maps to OpKind::Copy — same kernel,
    // destructive release is realize-loop bookkeeping).
    if !node_needs_plan_entry(&node.op) {
        return Ok(None);
    }
    let Some(op_kind) = op_to_op_kind(&node.op) else {
        // Unreachable given the gate above; keep the typed shape.
        return Ok(None);
    };

    // Decision-device resolution (step 4a): explicit per-node
    // placement (scheduler assignments) → the realize call's
    // pinned device → the legacy stamped-backend default.
    let explicit_backend = graph.target_backend(id);
    let target_device = decision_device_for(graph, options, id).ok_or_else(|| {
        Error::Msg(format!(
            "compile_plan: node {:?} ({:?}) has no device context — \
                 set PlanOptions::pinned_device, a graph placement, or \
                 the node's target_backend",
            id, node.op,
        ))
        .bt()
    })?;
    // Diagnostic backend for error reporting; also the legacy
    // single-backend placement when no topology enumerator is
    // configured.
    let diag_backend = explicit_backend.unwrap_or_else(|| backend_for_device(target_device));
    let dtypes = build_lookup_dtypes(graph, node);

    // Build the placement list: cross-backend if a topology
    // enumerator is configured, otherwise single-backend
    // legacy mode.
    let placements: Vec<(BackendId, DeviceLocation)> = match options.placements_for_device {
        Some(f) => f(target_device)
            .into_iter()
            .map(|backend| (backend, target_device))
            .collect(),
        None => vec![(diag_backend, target_device)],
    };

    let op_params = candidate_default_op_params(graph, node);

    // Stage-2 admission relax: when inbound-transfer pricing is
    // active, off-device candidates ALWAYS enumerate — priced;
    // locality emerges from the numbers, not from the legacy
    // missing-impl-only gate. Three hard exclusions keep the
    // relax sound:
    //
    // - **Explicit `Graph::placement` is a hard pin.** A per-node
    //   placement is a scheduler / profiling decision (the Judge
    //   measuring a specific backend pins its profile graphs this
    //   way) — the planner must not silently move it to a
    //   "cheaper" sibling device. The realize-call `pinned_device`
    //   is a soft default the planner may improve on.
    // - **Destructive ops never move** off the device that owns
    //   their mutation target.
    // - **Pricing must actually be active** (estimator +
    //   populate_costs + capabilities): an unpriced rank would
    //   move ops on kernel cost alone, regressing locality.
    let pricing_active = options.transfer_estimator.is_some()
        && options.populate_costs
        && options.capabilities_for.is_some();
    let fallback_allowed =
        node.op.destructive_input().is_none() && options.fallback_placements_for.is_some();
    let relaxed = options.allow_cost_placement
        && pricing_active
        && fallback_allowed
        && graph.placement(id).is_none();

    let mut set;
    let mut from_fallback = false;
    if relaxed {
        // Merged enumeration: decision-device placements FIRST so
        // the rank's stable sort breaks zero-signal ties toward
        // locality, then every off-device placement — all priced
        // by the same cost + transfer composition.
        let mut merged = placements.clone();
        if let Some(fallback) = options.fallback_placements_for {
            merged.extend(fallback(target_device));
        }
        set = enumerate_candidates(op_kind, &dtypes, &merged, &op_params, bindings_table);
    } else {
        set = enumerate_candidates(op_kind, &dtypes, &placements, &op_params, bindings_table);

        // Picker-arc step 4b (legacy, unpriced path): when the
        // decision device has NO implementation for this
        // (op, dtypes), admit off-device candidates from the
        // fallback enumerator. The missing-impl op becomes a
        // plan-time picker decision (the bridge stitches residency
        // via Op::Copy insertion around the off-device winner)
        // instead of a realize-time error.
        if set.is_empty()
            && fallback_allowed
            && let Some(fallback) = options.fallback_placements_for
        {
            let fb_placements = fallback(target_device);
            if !fb_placements.is_empty() {
                set = enumerate_candidates(
                    op_kind,
                    &dtypes,
                    &fb_placements,
                    &op_params,
                    bindings_table,
                );
                from_fallback = !set.is_empty();
            }
        }
    }

    // Fail-fast: if enumeration found nothing (on the decision
    // device AND via fallback), surface the missing-binding
    // error before filters can also empty the set (which would
    // produce a less-specific FilterRejected).
    if set.is_empty() {
        return Err(missing_binding_error(
            bindings_table,
            op_kind,
            &dtypes,
            diag_backend,
        ));
    }

    // Apply the default filter chain.
    let input_layouts: Vec<fuel_ir::Layout> = node
        .inputs
        .iter()
        .map(|&input_id| graph.layout(input_id))
        .collect();
    let ctx = FilterContext::new(op_kind, &dtypes, &input_layouts);
    let chain = default_chain(options.precision_requirement);
    if let Err(err) = apply_filter_chain(&mut set, &chain, &ctx) {
        // Stage-2 fix of the picker-4b verifier minor: a decision
        // device whose registrations all FAIL the hard filter
        // chain falls back exactly like an empty enumeration —
        // the pin can't deliver an admissible kernel either way.
        // Only meaningful when the rejected set couldn't already
        // contain off-device candidates: relaxed merged sets and
        // fallback sets rejected here are genuine global
        // rejections and propagate.
        if relaxed || from_fallback || !fallback_allowed {
            return Err(err);
        }
        let Some(fallback) = options.fallback_placements_for else {
            return Err(err);
        };
        let fb_placements = fallback(target_device);
        if fb_placements.is_empty() {
            return Err(err);
        }
        let mut fb_set =
            enumerate_candidates(op_kind, &dtypes, &fb_placements, &op_params, bindings_table);
        if fb_set.is_empty() {
            return Err(err);
        }
        // The fallback set faces the same chain — a hard reject
        // here is the surfaceable error (nowhere admissible).
        apply_filter_chain(&mut fb_set, &chain, &ctx)?;
        set = fb_set;
        from_fallback = true;
    }

    // Stamp the decision-point identity so dispatch-time
    // selectors (Picker 2) can re-query the Judge per candidate.
    // The derivation mirrors `compute_static_costs`'s Layer-2
    // lookup key exactly: principal dtype = first lookup dtype,
    // size class = `SizeClass::for_op` over the input operand shapes
    // (the shared producer/consumer derivation — matmul keys on its
    // `(m,n,k)` aspect, every other op on the first operand's element
    // count; nullary ops → `SizeClass(0)`). Runs after the
    // filter/fallback resolution so a filter-rejection fallback set is
    // stamped too.
    if let Some(&principal_dtype) = dtypes.first() {
        let input_shapes: Vec<Shape> = node
            .inputs
            .iter()
            .map(|&input_id| graph.node(input_id).shape.clone())
            .collect();
        let size_class = SizeClass::for_op(op_kind, &input_shapes);
        set.set_context(DecisionContext {
            op: op_kind,
            principal_dtype,
            size_class,
        });
    }

    // Static cost composition (optional — tests may skip). Layer-1
    // via the binding-table CostFns + Layer-2 Judge refinement;
    // both residency-independent, so they belong to the draft.
    let costed = if options.populate_costs {
        if let Some(caps_for) = options.capabilities_for {
            let input_shapes: Vec<Shape> = node
                .inputs
                .iter()
                .map(|&input_id| graph.node(input_id).shape.clone())
                .collect();
            compute_static_costs(
                &mut set,
                op_kind,
                &dtypes,
                &input_shapes,
                bindings_table,
                caps_for,
                options.judge,
            );
            true
        } else {
            false
        }
    } else {
        false
    };

    Ok(Some(NodeDraft {
        set,
        relaxed,
        from_fallback,
        costed,
        op_kind,
        dtypes,
        diag_backend,
    }))
}

/// `compile_plan`'s per-node commit phase, Stage-2 greedy regime:
/// inbound-transfer pricing against the committed producer
/// residencies, rank, relaxed winner-device prune, truncation, and
/// the fallback freeze. The Stage-3 DP path replaces this for
/// deferred rows (pricing + prune happen after the backtrack
/// instead).
fn finalize_node_greedy(
    graph: &Graph,
    node: &fuel_graph::Node,
    draft: NodeDraft,
    bindings_table: &KernelBindingTable,
    options: &PlanOptions<'_>,
    residency: &HashMap<NodeId, DeviceLocation>,
) -> Result<AlternativeSet> {
    let NodeDraft {
        mut set,
        relaxed,
        from_fallback,
        costed,
        op_kind,
        dtypes,
        diag_backend,
    } = draft;

    if costed {
        // Stage 2: price the transfers each candidate's placement
        // would induce from its inputs' committed residencies. Runs
        // AFTER the Layer-2 Judge refinement (which REPLACES the
        // kernel-time estimate) so the transfer term survives; the
        // rank adds the two.
        if let Some(estimator) = options.transfer_estimator {
            let inputs = priced_inputs_for(graph, node, residency);
            apply_inbound_transfer_costs(&mut set, &inputs, estimator);
        }
        set.rank_by_composite_cost();
    }

    // Stage 2: after the priced rank, prune the relaxed set to the
    // winner's device. The bridge's residency stitch (Op::Copy
    // insertion) is a graph rewrite computed from the static
    // winner; cross-device siblings left in the set would let a
    // dispatch-time selector pick a candidate whose inputs were
    // never copied to its device. Same-device siblings stay — the
    // stitch covers all of them, and the transfer term is uniform
    // across one device so the selector's relative ranking is
    // unaffected. Pruned BEFORE truncation so same-device
    // alternatives fill the top-N.
    if relaxed && let Some(winner_device) = set.winner().map(|c| c.device) {
        let keep: Vec<usize> = set
            .alternatives()
            .iter()
            .enumerate()
            .filter_map(|(i, c)| (c.device == winner_device).then_some(i))
            .collect();
        if keep.len() < set.len() {
            set.retain_indices(&keep);
        }
    }

    // PR-B2: retire the fixed top-N. Retain the per-ending-device
    // Pareto frontier (crowding-capped at KEEP_PER_DEVICE) instead of
    // a global top-N. The arm-0 winner — the realize pick — stays at
    // index 0. In the `relaxed` path the set was just pruned to the
    // winner's single device, so this reduces to that device's Pareto
    // front; in the legacy multi-backend-at-one-device path it keeps
    // the non-dominated kernels at the decision device.
    set.retain_per_device_frontier(KEEP_PER_DEVICE);

    // Step 4b: off-device fallback sets freeze to their single
    // ranked winner. The bridge's residency stitch (Op::Copy
    // insertion) is a graph rewrite computed from the static
    // winner; leaving siblings on OTHER devices in the set
    // would let a dispatch-time selector pick a candidate whose
    // inputs were never copied to its device.
    if from_fallback && set.len() > 1 {
        set.retain_indices(&[0]);
    }

    // After retention an empty set is the surfaceable error
    // (the filter chain or rank dropped everything). The chain
    // would have raised FilterRejected on hard-empty, so this
    // path is only reachable when populate_costs + ranking
    // pruned a structurally-empty set, which shouldn't happen
    // in practice — defensive only.
    if set.is_empty() {
        return Err(missing_binding_error(
            bindings_table,
            op_kind,
            &dtypes,
            diag_backend,
        ));
    }

    Ok(set)
}

/// Build an `Error::NoBackendForOp` diagnostic for a decision
/// point with no registered alternative. Same shape as
/// [`crate::kernel::KernelBindingTable::lookup_with_caps`]'s error
/// branch so callers see consistent output regardless of which
/// path surfaced the miss.
fn missing_binding_error(
    table: &KernelBindingTable,
    op: OpKind,
    dtypes: &[DType],
    backend: BackendId,
) -> Error {
    // The queried `backend` isn't a NoBackendForOp field; the diagnostic lists
    // the full coverage across all backends (the "what exists" context) so a
    // reader can see which `(op, dtypes)` ARE registered. Formerly this returned
    // empty lists, falsely reading as "no backend registered".
    let _ = backend;
    let (available_backends, supported_combinations) = table.coverage_diagnostic();
    Error::NoBackendForOp {
        op,
        dtypes: dtypes.to_vec(),
        available_backends,
        supported_combinations,
    }
    .bt()
}

/// Helper that produces the `OpParams` to attach to enumerated
/// candidates for one graph node. Phase 1.5 ships with the
/// placeholder `OpParams::None` for every node — the live
/// op-params shape derivation lives in `pipelined::op_to_op_params`
/// but is currently mid-refactor for other reasons. Phase 4's
/// executor integration replaces this with the live derivation so
/// the planner's candidate set matches what dispatch sees.
fn candidate_default_op_params(
    _graph: &Graph,
    _node: &fuel_graph::Node,
) -> crate::kernel::OpParams {
    crate::kernel::OpParams::None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fused::PrecisionGuarantee;
    use crate::kernel::{KernelCaps, OpParams, unknown_cost};
    use fuel_graph::{Node, Op, topo_order};
    use fuel_ir::backend::{BackendCapabilities, SubstrateClass, TransferPath};
    use fuel_ir::{DType, Layout, Result as FuelResult, Shape, StrideVec};
    use fuel_memory::Storage;
    use smallvec::smallvec;
    use std::collections::HashSet;
    use std::sync::{Arc, RwLock};

    fn noop_kernel(
        _i: &[Arc<RwLock<Storage>>],
        _o: &mut [Arc<RwLock<Storage>>],
        _l: &[Layout],
        _p: &OpParams,
    ) -> FuelResult<()> {
        Ok(())
    }

    fn noop_kernel_b(
        _i: &[Arc<RwLock<Storage>>],
        _o: &mut [Arc<RwLock<Storage>>],
        _l: &[Layout],
        _p: &OpParams,
    ) -> FuelResult<()> {
        Ok(())
    }

    fn noop_kernel_c(
        _i: &[Arc<RwLock<Storage>>],
        _o: &mut [Arc<RwLock<Storage>>],
        _l: &[Layout],
        _p: &OpParams,
    ) -> FuelResult<()> {
        Ok(())
    }

    fn register_add_f32(
        table: &mut KernelBindingTable,
        backend: BackendId,
        kernel: crate::kernel::KernelRef,
        precision: PrecisionGuarantee,
    ) {
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            backend,
            kernel,
            KernelCaps::empty(),
            precision,
            unknown_cost,
        );
    }

    fn build_add_graph() -> (Graph, NodeId) {
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let rhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, rhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        g.set_target_backend(add, BackendId::Cpu);
        (g, add)
    }

    fn cpu_caps() -> BackendCapabilities {
        BackendCapabilities {
            backend_id: BackendId::Cpu,
            device_location: DeviceLocation::Cpu,
            op_dtype_support: HashSet::new(),
            required_alignment: 64,
            access_granularity_bits: 8,
            transfer_paths: vec![(DeviceLocation::Cpu, TransferPath::SameDevice)],
            storage_substrate: SubstrateClass::HostBytes,
            compute_throughput_flops_per_ns: 1.0,
            mem_bandwidth_bytes_per_ns: 4.0,
        }
    }

    #[test]
    fn empty_plan_has_no_alternatives() {
        let p = ExecutionPlan::empty();
        assert!(p.order.is_empty());
        assert!(p.alternatives.is_empty());
        assert!(p.alternatives(NodeId(0)).is_none());
    }

    #[test]
    fn default_device_per_backend_matches_router_convention() {
        assert_eq!(default_device_for(BackendId::Cpu), DeviceLocation::Cpu);
        assert_eq!(
            default_device_for(BackendId::Cuda),
            DeviceLocation::Cuda { gpu_id: 0 },
        );
        assert_eq!(
            default_device_for(BackendId::Vulkan),
            DeviceLocation::Vulkan { gpu_id: 0 },
        );
    }

    #[test]
    fn compile_plan_skips_view_and_const_nodes() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );

        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[6]),
            dtype: DType::F32,
        });
        let rhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[6]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, rhs],
            shape: Shape::from_dims(&[6]),
            dtype: DType::F32,
        });
        let reshape = g.push(Node {
            op: Op::Reshape(Shape::from_dims(&[2, 3])),
            inputs: vec![add],
            shape: Shape::from_dims(&[2, 3]),
            dtype: DType::F32,
        });
        g.set_target_backend(add, BackendId::Cpu);
        g.set_target_backend(reshape, BackendId::Cpu);

        let order = topo_order(&g, reshape);
        let opts = PlanOptions::new().without_cost_population();
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");

        assert_eq!(plan.order.len(), 4);
        assert_eq!(
            plan.alternatives.len(),
            1,
            "only Add carries an alternative set",
        );
        let alts = plan.alternatives(add).expect("Add alternatives present");
        assert_eq!(alts.len(), 1);
        assert!(plan.alternatives(lhs).is_none());
        assert!(plan.alternatives(rhs).is_none());
        assert!(plan.alternatives(reshape).is_none());
    }

    #[test]
    fn compile_plan_fails_fast_on_missing_binding() {
        let table = KernelBindingTable::new();
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[2, 3]),
            dtype: DType::F32,
        });
        let rhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3, 2]),
            dtype: DType::F32,
        });
        let mm = g.push(Node {
            op: Op::MatMul,
            inputs: vec![lhs, rhs],
            shape: Shape::from_dims(&[2, 2]),
            dtype: DType::F32,
        });
        g.set_target_backend(mm, BackendId::Cpu);

        let order = topo_order(&g, mm);
        let opts = PlanOptions::new().without_cost_population();
        let err = compile_plan(&g, &order, &table, &opts).unwrap_err();
        match err {
            Error::NoBackendForOp { op, dtypes, .. } => {
                assert_eq!(op, OpKind::MatMul);
                assert_eq!(dtypes, vec![DType::F32, DType::F32, DType::F32]);
            }
            other => panic!("expected NoBackendForOp, got {other:?}"),
        }
    }

    /// Regression: a plan-time binding miss reports the REAL registered coverage,
    /// not a hardcoded `[]`. Reproduces the observed `llama-lazy` failure — an
    /// F32-activation × BF16-weight (mixed-precision) matmul, which the CPU
    /// backend has no kernel for (it registers uniform `[T,T,T]` only) — against
    /// the fully-populated production binding table. Before the fix,
    /// `missing_binding_error` returned empty lists, so a mixed-precision gap
    /// surfaced as the maximally-misleading "available backends: []" (it cost two
    /// people an investigation + a wrong hypothesis). Now it lists Cpu + the
    /// uniform matmul combos, so a reader sees `[F32,F32,F32]` present and
    /// `[F32,BF16,F32]` absent and diagnoses it immediately.
    #[test]
    fn plan_time_miss_reports_real_coverage_not_empty() {
        let bindings = crate::dispatch::global_bindings(); // self-seeded, populated
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[2, 3]),
            dtype: DType::F32,
        });
        // BF16 weight → the matmul dtype key becomes the mixed [F32, BF16, F32].
        let rhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3, 2]),
            dtype: DType::BF16,
        });
        let mm = g.push(Node {
            op: Op::MatMul,
            inputs: vec![lhs, rhs],
            shape: Shape::from_dims(&[2, 2]),
            dtype: DType::F32,
        });
        g.set_target_backend(mm, BackendId::Cpu);

        let order = topo_order(&g, mm);
        let opts = PlanOptions::new().without_cost_population();
        let err = compile_plan(&g, &order, &bindings, &opts).unwrap_err();
        match err {
            Error::NoBackendForOp {
                op,
                dtypes,
                available_backends,
                supported_combinations,
            } => {
                assert_eq!(op, OpKind::MatMul);
                assert_eq!(
                    dtypes,
                    vec![DType::F32, DType::BF16, DType::F32],
                    "the mixed-precision key (F32 activation, BF16 weight)",
                );
                // The fix: the diagnostic is TRUTHFUL, not a hardcoded empty stub.
                assert!(
                    available_backends.contains(&BackendId::Cpu),
                    "a populated table must report Cpu, never []: {available_backends:?}",
                );
                assert!(
                    supported_combinations
                        .iter()
                        .any(|(b, o, d)| *b == BackendId::Cpu

                            && *o == OpKind::MatMul
                            && *d == vec![DType::F32, DType::F32, DType::F32]),
                    "must show uniform F32 matmul IS registered (so the mixed gap is visible): \
                     {supported_combinations:?}",
                );
            }
            other => panic!("expected NoBackendForOp, got {other:?}"),
        }
    }

    #[test]
    fn compile_plan_legacy_single_backend_uses_target_backend() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        // Even with an AOCL alternative registered, the legacy mode
        // (placements_for_device = None) only uses the node's
        // target_backend (Cpu).
        register_add_f32(
            &mut table,
            BackendId::Cuda,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let opts = PlanOptions::new().without_cost_population();
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add_id).unwrap();
        assert_eq!(alts.len(), 1, "legacy mode → single backend → single alt");
        assert_eq!(alts.winner().unwrap().backend, BackendId::Cpu);
    }

    #[test]
    fn compile_plan_cross_co_located_via_placements_callback() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Cuda,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );

        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu, BackendId::Cuda]
            } else {
                vec![]
            }
        };
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_placements_for_device(&placements_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add_id).unwrap();
        assert_eq!(
            alts.len(),
            2,
            "cross-co-located mode aggregates Cpu + Aocl alternatives",
        );
    }

    #[test]
    fn compile_plan_precision_requirement_filters_via_default_chain() {
        let mut table = KernelBindingTable::new();
        // Non-bit-stable.
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee {
                bit_stable_on_same_hardware: false,
                ..PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU
            },
        );

        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_precision_requirement(PrecisionRequirement::BIT_STABLE);
        let err = compile_plan(&g, &order, &table, &opts).unwrap_err();
        match err {
            Error::FilterRejected { filter, .. } => {
                assert_eq!(filter, "precision-floor");
            }
            other => panic!("expected FilterRejected, got {other:?}"),
        }
        // Sanity: drop the requirement → plan succeeds.
        let unconstrained = PlanOptions::new().without_cost_population();
        let plan = compile_plan(&g, &order, &table, &unconstrained).expect("compile");
        assert!(plan.alternatives(add_id).is_some());
        let _ = add_id;
    }

    #[test]
    fn compile_plan_with_cost_ranks_cheaper_winner() {
        fn cheap_cost(
            _: &[Shape],
            _: &[DType],
            _: &OpParams,
            _: &BackendCapabilities,
        ) -> crate::fused::CostEstimate {
            crate::fused::CostEstimate {
                flops: 10,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        fn expensive_cost(
            _: &[Shape],
            _: &[DType],
            _: &OpParams,
            _: &BackendCapabilities,
        ) -> crate::fused::CostEstimate {
            crate::fused::CostEstimate {
                flops: 1_000_000,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        let mut table = KernelBindingTable::new();
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            expensive_cost,
        );
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cuda,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cheap_cost,
        );

        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu, BackendId::Cuda]
            } else {
                vec![]
            }
        };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_placements_for_device(&placements_fn)
            .with_capabilities_for(&caps_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add_id).unwrap();
        assert_eq!(alts.len(), 2);
        assert_eq!(
            alts.winner().unwrap().backend,
            BackendId::Cuda,
            "cheaper static cost wins after ranking",
        );
    }

    #[test]
    fn compile_plan_judge_layer2_can_flip_layer1_winner() {
        // Layer-1 says CPU is cheap, Aocl is expensive.
        // Layer-2 (Judge) measured the opposite: Aocl 20 ns, CPU 5000 ns.
        // After compile_plan's cost composition + rank, Aocl wins.
        use crate::ranker::HashMapJudge;
        use fuel_ir::dispatch::SizeClass;

        fn cpu_layer1(
            _: &[Shape],
            _: &[DType],
            _: &OpParams,
            _: &BackendCapabilities,
        ) -> crate::fused::CostEstimate {
            crate::fused::CostEstimate {
                flops: 10,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        fn aocl_layer1(
            _: &[Shape],
            _: &[DType],
            _: &OpParams,
            _: &BackendCapabilities,
        ) -> crate::fused::CostEstimate {
            crate::fused::CostEstimate {
                flops: 100_000,
                bytes_moved: 0,
                kernel_overhead_ns: 0,
            }
        }
        let mut table = KernelBindingTable::new();
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cpu_layer1,
        );
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cuda,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            aocl_layer1,
        );

        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu, BackendId::Cuda]
            } else {
                vec![]
            }
        };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);

        // Each input is shape [3] (per build_add_graph); the
        // first-input-shape rule gives elem_count 3.
        let sc = SizeClass::from_elem_count(3);
        let mut judge = HashMapJudge::new();
        judge.insert(
            OpKind::AddElementwise,
            DType::F32,
            sc,
            BackendId::Cpu,
            "",
            5_000,
            0,
        );
        judge.insert(
            OpKind::AddElementwise,
            DType::F32,
            sc,
            BackendId::Cuda,
            "",
            20,
            0,
        );

        let opts = PlanOptions::new()
            .with_placements_for_device(&placements_fn)
            .with_capabilities_for(&caps_fn)
            .with_judge(&judge);

        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add_id).unwrap();
        assert_eq!(alts.len(), 2);
        assert_eq!(
            alts.winner().unwrap().backend,
            BackendId::Cuda,
            "Layer-2 measurement reverses Layer-1 verdict via compile_plan",
        );
    }

    /// `compile_plan` stamps each set with the decision-point
    /// context using the same key derivation as the cost composer's
    /// Layer-2 lookup: principal dtype = first lookup dtype, size
    /// class = first input's element count.
    #[test]
    fn compile_plan_stamps_decision_context() {
        use fuel_ir::dispatch::SizeClass;

        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let opts = PlanOptions::new().without_cost_population();
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add_id).expect("Add alternatives present");
        let ctx = alts.context().expect("context stamped");
        assert_eq!(ctx.op, OpKind::AddElementwise);
        assert_eq!(ctx.principal_dtype, DType::F32);
        // build_add_graph inputs are shape [3].
        assert_eq!(ctx.size_class, SizeClass::from_elem_count(3));
    }

    /// PR-B2: the fixed top-N is retired. Three distinct backends
    /// co-located at one ending device, all equally-costed (no cost
    /// population → equal cost vectors → mutually non-dominated), all
    /// survive the per-ending-device Pareto frontier — the
    /// never-strand-last-`(device, backend)` rule keeps each distinct
    /// backend, and the crowding cap (KEEP_PER_DEVICE = 32) is far
    /// above 3. The old `truncate_to_top_n` would have stranded one;
    /// this is the behavior change B2 introduces.
    #[test]
    fn compile_plan_retains_per_device_frontier_no_top_n() {
        let mut table = KernelBindingTable::new();
        // Three CPU-substrate backends competing at one decision point.
        // Distinct kernel fn items so the binding table accepts them.
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Cuda,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Vulkan,
            noop_kernel_c,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );

        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu, BackendId::Cuda, BackendId::Vulkan]
            } else {
                vec![]
            }
        };
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_placements_for_device(&placements_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add_id).unwrap();
        assert_eq!(
            alts.len(),
            3,
            "all three co-located (device, backend) paths survive — no top-N truncation",
        );
    }

    /// Step 4a parity: an UNSTAMPED graph planned with
    /// `with_pinned_device(Cpu)` produces the same alternative sets
    /// as the legacy stamped graph — candidate-for-candidate
    /// (backend, device, kernel, kernel_source).
    #[test]
    fn compile_plan_pinned_device_matches_stamped_plan() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![]
            }
        };

        // Legacy: stamped target_backend, no pinned device.
        let (g_stamped, add_stamped) = build_add_graph();
        let order_stamped = topo_order(&g_stamped, add_stamped);
        let opts_stamped = PlanOptions::new()
            .without_cost_population()
            .with_placements_for_device(&placements_fn);
        let plan_stamped = compile_plan(&g_stamped, &order_stamped, &table, &opts_stamped)
            .expect("stamped compile");

        // New: same graph shape, NO target_backend anywhere, pinned
        // device instead.
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let rhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, rhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, add);
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_placements_for_device(&placements_fn)
            .with_pinned_device(DeviceLocation::Cpu);
        let plan = compile_plan(&g, &order, &table, &opts).expect("pinned compile");

        let a = plan_stamped.alternatives(add_stamped).expect("stamped set");
        let b = plan.alternatives(add).expect("pinned set");
        assert_eq!(a.len(), b.len(), "same candidate count");
        for (ca, cb) in a.alternatives().iter().zip(b.alternatives()) {
            assert_eq!(ca.backend, cb.backend);
            assert_eq!(ca.device, cb.device);
            assert_eq!(ca.kernel as usize, cb.kernel as usize, "same kernel ref");
            assert_eq!(ca.kernel_source, cb.kernel_source);
        }
        assert_eq!(a.context(), b.context(), "same decision context");
    }

    /// Step 4a: a node with neither a placement, nor a pinned
    /// device, nor a target_backend stamp is a plan-time error —
    /// fail-fast with an actionable message.
    #[test]
    fn compile_plan_errors_without_any_device_context() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, lhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, add);
        let opts = PlanOptions::new().without_cost_population();
        let err = compile_plan(&g, &order, &table, &opts).unwrap_err();
        let msg = format!("{err:?}");
        assert!(
            msg.contains("no device context"),
            "expected the device-context diagnostic, got: {msg}",
        );
    }

    /// Step 4a: `Op::Copy` nodes are excluded from the plan map —
    /// their kernel is residency-determined (source backend), so
    /// dispatch flows through the executor's legacy `compile_node`
    /// path keyed by the bridge's source-backend stamp.
    #[test]
    fn compile_plan_skips_op_copy_nodes() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        // A Copy registration exists — the skip must not depend on
        // the table lacking one.
        table.register_full(
            OpKind::Copy,
            &[DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
        );

        let (mut g, add_id) = build_add_graph();
        let copy_id = g.push(Node {
            op: Op::Copy {
                target: DeviceLocation::Cpu,
            },
            inputs: vec![add_id],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        g.set_target_backend(copy_id, BackendId::Cpu);
        let order = topo_order(&g, copy_id);
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_pinned_device(DeviceLocation::Cpu);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        assert!(plan.alternatives(add_id).is_some(), "Add is planned");
        assert!(
            plan.alternatives(copy_id).is_none(),
            "Op::Copy is residency-determined — no plan entry",
        );
    }

    /// `Op::Move` follows `Op::Copy`'s exclusion: it dispatches the
    /// same residency-determined transfer kernel (`OpKind::Copy` on
    /// the SOURCE backend), so it gets no plan entry either.
    #[test]
    fn compile_plan_skips_op_move_nodes() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        // A Copy registration exists (Op::Move keys the same row) —
        // the skip must not depend on the table lacking one.
        table.register_full(
            OpKind::Copy,
            &[DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
        );

        let (mut g, add_id) = build_add_graph();
        let move_id = g.push(Node {
            op: Op::Move {
                target: DeviceLocation::Cpu,
            },
            inputs: vec![add_id],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        g.set_target_backend(move_id, BackendId::Cpu);
        let order = topo_order(&g, move_id);
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_pinned_device(DeviceLocation::Cpu);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        assert!(plan.alternatives(add_id).is_some(), "Add is planned");
        assert!(
            plan.alternatives(move_id).is_none(),
            "Op::Move is residency-determined — no plan entry",
        );
    }

    /// Step 4b: an op with no implementation on the pinned device
    /// falls back to an off-device candidate — the missing-impl op
    /// becomes a plan-time picker decision.
    #[test]
    fn compile_plan_fallback_admits_off_device_candidate() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        // Add f32 registered ONLY on Cpu — the pinned CUDA device
        // has no impl.
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );

        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, lhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, add);

        let placements_fn = move |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == cuda0 {
                vec![BackendId::Cuda]
            } else {
                vec![BackendId::Cpu]
            }
        };
        let fallback_fn = |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![(BackendId::Cpu, DeviceLocation::Cpu)]
        };
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_pinned_device(cuda0)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add).expect("Add planned via fallback");
        assert_eq!(alts.len(), 1, "fallback set frozen to one winner");
        let w = alts.winner().unwrap();
        assert_eq!(w.backend, BackendId::Cpu);
        assert_eq!(w.device, DeviceLocation::Cpu, "off-device placement");
    }

    /// Step 4b locality, UNPRICED regime: without a Stage-2
    /// transfer estimator the fallback enumerator is NOT consulted
    /// while the pinned device has a registered candidate — an
    /// unpriced rank must never move an op cross-device on kernel
    /// cost alone.
    #[test]
    fn compile_plan_fallback_not_consulted_when_pinned_has_impl() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Cuda,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![]
            }
        };
        let fallback_fn = |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            panic!(
                "fallback consulted although the pinned device has an \
                 implementation — locality policy violated",
            );
        };
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add_id).unwrap();
        assert_eq!(alts.len(), 1);
        assert_eq!(alts.winner().unwrap().device, DeviceLocation::Cpu);
    }

    /// Step 4b: destructive ops never fall back — the plan-time
    /// NoBackendForOp error is preserved.
    #[test]
    fn compile_plan_fallback_denied_for_destructive_ops() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        // ReluInplace registered ONLY on Cpu; pinned device is CUDA.
        table.register_full(
            OpKind::ReluInplace,
            &[DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
        );
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let relu = g.push(Node {
            op: Op::ReluInplace,
            inputs: vec![lhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, relu);
        let placements_fn = move |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == cuda0 {
                vec![BackendId::Cuda]
            } else {
                vec![BackendId::Cpu]
            }
        };
        let fallback_fn = |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![(BackendId::Cpu, DeviceLocation::Cpu)]
        };
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_pinned_device(cuda0)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn);
        let err = compile_plan(&g, &order, &table, &opts).unwrap_err();
        assert!(
            matches!(
                err,
                Error::NoBackendForOp {
                    op: OpKind::ReluInplace,
                    ..
                }
            ),
            "destructive op must NOT fall back off-device; got {err:?}",
        );
    }

    /// Step 4b: even when MULTIPLE off-device backends could serve
    /// the op, the fallback set freezes to a single winner so the
    /// dispatch-time selector can't diverge from the residency
    /// stitch.
    #[test]
    fn compile_plan_fallback_freezes_to_single_winner() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let vk0 = DeviceLocation::Vulkan { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Vulkan,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );

        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, lhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, add);
        let placements_fn = move |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == cuda0 {
                vec![BackendId::Cuda]
            } else {
                vec![]
            }
        };
        let fallback_fn = move |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![
                (BackendId::Cpu, DeviceLocation::Cpu),
                (BackendId::Vulkan, vk0),
            ]
        };
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_pinned_device(cuda0)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add).unwrap();
        assert_eq!(
            alts.len(),
            1,
            "fallback set must freeze to its single ranked winner",
        );
    }

    /// Synthetic Stage-2 estimator: zero same-device, flat latency
    /// otherwise. Unit tests must never depend on live calibration.
    struct FlatEstimator {
        latency_ns: u64,
    }

    impl crate::ranker::TransferEstimator for FlatEstimator {
        fn estimate_transfer_ns(
            &self,
            src: DeviceLocation,
            dst: DeviceLocation,
            _bytes: u64,
        ) -> u64 {
            if src == dst { 0 } else { self.latency_ns }
        }
    }

    /// Stage 2 residency threading: a missing-impl fallback set is
    /// priced against the inputs' residency — with both inputs
    /// resident on vk0, the Vulkan fallback candidate beats the
    /// first-enumerated CPU one (both have zero kernel cost, so the
    /// transfer term decides).
    #[test]
    fn compile_plan_prices_fallback_by_input_residency() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let vk0 = DeviceLocation::Vulkan { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        // No CUDA impl — the pinned cuda0 device must fall back.
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Vulkan,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );

        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[4]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, lhs],
            shape: Shape::from_dims(&[4]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, add);

        let placements_fn = move |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == cuda0 {
                vec![BackendId::Cuda]
            } else {
                vec![]
            }
        };
        let fallback_fn = move |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![
                (BackendId::Cpu, DeviceLocation::Cpu),
                (BackendId::Vulkan, vk0),
            ]
        };
        // The const's bytes live on vk0 (e.g. a persistent cache
        // slot) — exactly what the bridge's input-residency callback
        // reports.
        let residency_fn =
            move |id: NodeId| -> Option<DeviceLocation> { (id == lhs).then_some(vk0) };
        let est = FlatEstimator { latency_ns: 1_000 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(cuda0)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add).expect("Add planned via fallback");
        assert_eq!(alts.len(), 1, "fallback narrows to a single winner");
        let w = alts.winner().unwrap();
        assert_eq!(
            w.device, vk0,
            "transfer pricing moves the fallback to the inputs' residency",
        );
        assert_eq!(w.inbound_transfer_ns, 0, "co-resident inputs price zero");
    }

    /// Plan-time cost fns for the Stage-2 relax tests. Composite is
    /// the flops figure directly (no bytes, no overhead).
    fn cost_600(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 600,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    fn cost_10(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 10,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    // 15 000 FLOPs → 500 ns at the GPU throughput prior (30 FLOPs/ns):
    // a kernel whose GPU compute is faster than a 600-ns CPU kernel, but
    // whose 200 ns inbound transfer tips the honest total past it.
    fn cost_15_000(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 15_000,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    fn cost_10_000_000(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 10_000_000,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }

    /// Test fixture for the relax: Add f32 on CPU (cost via
    /// `cpu_cost`) + CUDA at cuda:0 (cost via `cuda_cost`), graph
    /// pinned to CPU, both consts resident on CPU, fallback
    /// enumerator offering cuda:0. Returns the planned set for the
    /// Add node.
    fn relaxed_plan_with_costs(
        cpu_cost: crate::kernel::CostFn,
        cuda_cost: crate::kernel::CostFn,
        estimator: &FlatEstimator,
    ) -> AlternativeSet {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cpu_cost,
        );
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cuda,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cuda_cost,
        );

        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![]
            }
        };
        let fallback_fn = move |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![(BackendId::Cuda, cuda0)]
        };
        // build_add_graph's consts have no cache entry; report them
        // CPU-resident as build_const_cache would for a CPU realize.
        let residency_fn = |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(estimator)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        plan.alternatives(add_id).expect("Add planned").clone()
    }

    /// Stage-2 parity gate: a tiny op stays local although the
    /// remote kernel is "faster" — the transfer term dominates.
    /// Off-device siblings are pruned from the surviving set.
    #[test]
    fn compile_plan_relaxed_tiny_op_stays_local_when_transfer_dominates() {
        // CPU 600 ns vs CUDA 10 ns kernel; 1 ms per input crossing.
        // CUDA total = 10 + 2 × 1_000_000 ≫ CPU 600.
        let est = FlatEstimator {
            latency_ns: 1_000_000,
        };
        let set = relaxed_plan_with_costs(cost_600, cost_10, &est);
        let w = set.winner().unwrap();
        assert_eq!(
            w.device,
            DeviceLocation::Cpu,
            "transfer dominates → stay local"
        );
        assert_eq!(w.inbound_transfer_ns, 0, "local winner pays no transfer");
        assert!(
            set.alternatives()
                .iter()
                .all(|c| c.device == DeviceLocation::Cpu),
            "off-device siblings pruned after rank",
        );
    }

    /// FKC cost unification Part A — born-red for the "uncapped GPU
    /// prices at ZERO" bug.
    ///
    /// The production `capabilities_for` closure is
    /// `topology.capabilities(backend)`, which is backed by the
    /// `CapabilityRegistry`. Before Part A only the CPU auto-registered
    /// `BackendCapabilities`; a GPU backend registered its kernels into
    /// the binding table but NO caps. `compute_static_costs`
    /// (`ranker/cost.rs`) then hits `let Some(caps) =
    /// capabilities_for(backend) else { continue }` and SKIPS the GPU
    /// candidate's cost fn, leaving `static_cost` at the default ZERO.
    /// A CPU-pinned realize with a GPU fallback candidate then sees the
    /// GPU price at `0 + inbound_transfer` and win the priced relax —
    /// spilling onto an unseeded GPU (the observed `Op::Alloc/Copy`
    /// "no CUDA storage in input cache" crash).
    ///
    /// This test models both halves against the real placement DP:
    ///
    /// - **Bug half** (`caps_fn` returns `None` for CUDA, matching the
    ///   pre-Part-A registry): the CUDA fallback candidate is skipped,
    ///   prices at `0 + transfer(200) = 200 < CPU 600`, and wins.
    /// - **Fix half** (`caps_fn` returns the caps DERIVED FROM THE
    ///   BINDING TABLE by `dispatch::derive_backend_caps`, exactly what
    ///   Part A registers into the global registry): the CUDA candidate
    ///   is now costed via its flops fn — `500 + transfer(200) = 700 >
    ///   CPU 600` — so CPU wins and no spurious spill happens.
    ///
    /// The kernel gap (CUDA 500 < CPU 600) is deliberately real: the
    /// GPU is a *cheaper kernel*, so the only thing that makes CPU win
    /// is that the GPU is now HONESTLY priced (flops + transfer) rather
    /// than free. Same fixture as `relaxed_plan_with_costs` but with an
    /// explicit per-backend `caps_fn` so we can toggle the GPU's caps.
    #[test]
    fn compile_plan_uncapped_gpu_no_longer_wins_by_being_free() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };

        // A binding table exactly like production: CPU + CUDA kernels
        // for Add f32. On GPU throughput the CUDA kernel's compute is
        // nominally faster (15 000 FLOPs ≈ 500 ns at 30 FLOPs/ns < the
        // CPU's 600 ns), but its 200 ns inbound transfer tips the honest
        // total to 700 > 600 — so the CPU-pinned realize must stay put
        // once the GPU is priced honestly (the point of Part A).
        fn build_table() -> KernelBindingTable {
            let mut table = KernelBindingTable::new();
            table.register_full(
                OpKind::AddElementwise,
                &[DType::F32, DType::F32, DType::F32],
                BackendId::Cpu,
                noop_kernel,
                KernelCaps::empty(),
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
                cost_600,
            );
            table.register_full(
                OpKind::AddElementwise,
                &[DType::F32, DType::F32, DType::F32],
                BackendId::Cuda,
                noop_kernel_b,
                KernelCaps::empty(),
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
                cost_15_000,
            );
            table
        }

        // Run the CPU-pinned Add + CUDA-fallback relax; `gpu_caps`
        // controls whether `capabilities_for(Cuda)` returns Some (the
        // fix) or None (the pre-Part-A registry bug). All the borrowed
        // `PlanOptions` fields — including the caps closure — are LOCAL
        // to this function so `PlanOptions`'s single lifetime unifies to
        // this body. The caps values are `'static` (leaked) so the
        // closure can be built here from the caller's choice.
        fn winner_device_under(
            table: &KernelBindingTable,
            cpu_caps_val: &'static BackendCapabilities,
            gpu_caps: Option<&'static BackendCapabilities>,
            cuda0: DeviceLocation,
        ) -> DeviceLocation {
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);
            let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
                if dev == DeviceLocation::Cpu {
                    vec![BackendId::Cpu]
                } else {
                    vec![]
                }
            };
            let fallback_fn = move |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
                vec![(BackendId::Cuda, cuda0)]
            };
            // Both consts CPU-resident (a CPU realize) — the crash
            // scenario: the GPU is UNSEEDED, so any spill onto it is a
            // correctness bug downstream.
            let residency_fn =
                |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
            // 100 ns per input crossing → 200 ns inbound transfer for
            // the two-input Add on the CUDA candidate.
            let est = FlatEstimator { latency_ns: 100 };
            let caps_fn = move |b: BackendId| -> Option<&BackendCapabilities> {
                match b {
                    BackendId::Cpu => Some(cpu_caps_val),
                    BackendId::Cuda => gpu_caps,
                    _ => None,
                }
            };
            let opts = PlanOptions::new()
                .with_pinned_device(DeviceLocation::Cpu)
                .with_placements_for_device(&placements_fn)
                .with_fallback_placements_for(&fallback_fn)
                .with_capabilities_for(&caps_fn)
                .with_transfer_estimator(&est)
                .with_input_residency(&residency_fn);
            let plan = compile_plan(&g, &order, table, &opts).expect("compile");
            plan.alternatives(add_id).unwrap().winner().unwrap().device
        }

        let table = build_table();
        // `'static` caps values so the caps closure built inside the
        // helper can capture them without threading a lifetime through
        // `PlanOptions`.
        let cpu_caps_val: &'static BackendCapabilities = Box::leak(Box::new(cpu_caps()));

        // --- Bug half: the pre-Part-A registry returns None for CUDA. ---
        // (Kept as an inner assertion, sharing one fixture with the fix
        // half, so the red→green contrast is a single self-checking test.)
        let winner_bug = winner_device_under(&table, cpu_caps_val, None, cuda0);
        assert_eq!(
            winner_bug, cuda0,
            "PRE-FIX: an uncapped CUDA candidate is skipped by \
             compute_static_costs, prices at 0 + 200 transfer, and \
             spuriously beats the CPU's honest 600 — this IS the bug",
        );

        // --- Fix half: Part A derives CUDA caps from the binding table
        // and registers them, so capabilities_for(Cuda) returns Some. ---
        let cuda_caps_val: &'static BackendCapabilities = Box::leak(Box::new(
            crate::dispatch::derive_backend_caps(BackendId::Cuda, cuda0, &table),
        ));
        // The derivation must advertise the CUDA-registered (op, dtype)
        // pair so the cost fn actually runs.
        assert!(
            cuda_caps_val
                .op_dtype_support
                .contains(&(OpKind::AddElementwise, DType::F32)),
            "derive_backend_caps must mirror the binding table's \
             registered (op, output-dtype) pairs for the backend",
        );
        assert_eq!(cuda_caps_val.backend_id, BackendId::Cuda);
        assert_eq!(cuda_caps_val.device_location, cuda0);

        let winner_fixed = winner_device_under(&table, cpu_caps_val, Some(cuda_caps_val), cuda0);
        assert_eq!(
            winner_fixed,
            DeviceLocation::Cpu,
            "POST-FIX: with GPU caps registered the CUDA candidate is \
             costed honestly (15000 FLOPs ÷ 30/ns = 500 ns compute + 200 \
             transfer = 700 > CPU 600), so the CPU-pinned realize stays \
             on CPU — no spill onto the unseeded GPU",
        );
    }

    /// FKC cost unification Part C — born-red for **per-backend
    /// throughput**. The SAME kernel-cost fn is registered on both CPU
    /// and CUDA (equal FLOP counts — the discriminator is the backend's
    /// throughput, NOT a hand-tuned kernel-cost gap like the huge-op
    /// test below). Inputs are CPU-resident, so the CUDA candidate pays
    /// a fixed 200 ns inbound transfer (100 ns × 2 Add inputs). Expected
    /// crossover:
    ///   - SMALL op (100 FLOPs) → stays on CPU: the GPU's throughput
    ///     win (100 → ~3 ns) can't overcome the 200 ns transfer.
    ///   - LARGE op (10 M FLOPs) → moves to CUDA: the throughput win
    ///     (10 M → ~333 k ns) dwarfs the 200 ns transfer.
    ///
    /// Under the pre-Part-C FIXED 1-FLOP/ns prior the GPU's compute-ns
    /// EQUALS the CPU's for equal FLOPs, so it can never overcome the
    /// transfer and the large-op case wrongly stays on CPU — that is
    /// the red this test is born in.
    #[test]
    fn compile_plan_places_large_compute_on_gpu_small_on_cpu_by_throughput() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };

        fn winner(cost: crate::kernel::CostFn, cuda0: DeviceLocation) -> DeviceLocation {
            let mut table = KernelBindingTable::new();
            table.register_full(
                OpKind::AddElementwise,
                &[DType::F32, DType::F32, DType::F32],
                BackendId::Cpu,
                noop_kernel,
                KernelCaps::empty(),
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
                cost,
            );
            table.register_full(
                OpKind::AddElementwise,
                &[DType::F32, DType::F32, DType::F32],
                BackendId::Cuda,
                noop_kernel_b,
                KernelCaps::empty(),
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
                cost,
            );

            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);
            let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
                if dev == DeviceLocation::Cpu {
                    vec![BackendId::Cpu]
                } else {
                    vec![]
                }
            };
            let fallback_fn = move |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
                vec![(BackendId::Cuda, cuda0)]
            };
            let residency_fn =
                |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
            // 100 ns per input crossing → 200 ns inbound transfer for
            // the two-input Add on the CUDA candidate.
            let est = FlatEstimator { latency_ns: 100 };
            let cpu_caps_val = cpu_caps();
            let cuda_caps_val =
                crate::dispatch::derive_backend_caps(BackendId::Cuda, cuda0, &table);
            let caps_fn = |b: BackendId| -> Option<&BackendCapabilities> {
                match b {
                    BackendId::Cpu => Some(&cpu_caps_val),
                    BackendId::Cuda => Some(&cuda_caps_val),
                    _ => None,
                }
            };
            let opts = PlanOptions::new()
                .with_pinned_device(DeviceLocation::Cpu)
                .with_placements_for_device(&placements_fn)
                .with_fallback_placements_for(&fallback_fn)
                .with_capabilities_for(&caps_fn)
                .with_transfer_estimator(&est)
                .with_input_residency(&residency_fn);
            let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
            plan.alternatives(add_id).unwrap().winner().unwrap().device
        }

        assert_eq!(
            winner(cost_100, cuda0),
            DeviceLocation::Cpu,
            "SMALL op: the GPU's throughput win on 100 FLOPs (~3 ns) \
             can't overcome the 200 ns inbound transfer → stay on CPU",
        );
        assert_eq!(
            winner(cost_10_000_000, cuda0),
            cuda0,
            "LARGE op: the GPU's throughput win on 10 M FLOPs (~333 k ns) \
             dwarfs the 200 ns transfer → move to CUDA. RED under the \
             fixed 1-FLOP/ns prior (equal compute-ns → the transfer \
             always loses the GPU the placement)",
        );
    }

    /// `without_cost_placement()` (the reference / single-device realize
    /// gate) keeps a large, GPU-favorable op on the pinned CPU device. Same
    /// setup as `..._large_compute_on_gpu_small_on_cpu_by_throughput`, whose
    /// LARGE case moves the op to CUDA — here that exact op stays on CPU
    /// because the relaxed cross-device enumeration is suppressed.
    ///
    /// This is the unit-level guard for the phase6b oracle crash: a CPU-pinned
    /// reference realize must not spill onto the present (unseeded) GPU. Born
    /// red before `PlanOptions::allow_cost_placement` existed — the large op
    /// would have moved to CUDA.
    #[test]
    fn compile_plan_without_cost_placement_keeps_large_op_on_cpu() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost_10_000_000,
        );
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cuda,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost_10_000_000,
        );

        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![]
            }
        };
        let fallback_fn = move |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![(BackendId::Cuda, cuda0)]
        };
        let residency_fn = |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
        let est = FlatEstimator { latency_ns: 100 };
        let cpu_caps_val = cpu_caps();
        let cuda_caps_val = crate::dispatch::derive_backend_caps(BackendId::Cuda, cuda0, &table);
        let caps_fn = |b: BackendId| -> Option<&BackendCapabilities> {
            match b {
                BackendId::Cpu => Some(&cpu_caps_val),
                BackendId::Cuda => Some(&cuda_caps_val),
                _ => None,
            }
        };
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn)
            .without_cost_placement();
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let set = plan.alternatives(add_id).unwrap();
        assert_eq!(
            set.winner().unwrap().device,
            DeviceLocation::Cpu,
            "without_cost_placement: the large GPU-favorable op stays on the \
             pinned CPU device (no relaxed cross-device enumeration)",
        );
        assert!(
            set.alternatives()
                .iter()
                .all(|c| c.device == DeviceLocation::Cpu),
            "no off-device candidate enumerated at all",
        );
    }

    /// Stage-2 parity gate: a huge op legitimately moves — the
    /// kernel gap dwarfs the transfer. The surviving set lives
    /// entirely on the winner's device so dispatch-time selectors
    /// can't diverge from the residency stitch.
    #[test]
    fn compile_plan_relaxed_huge_op_moves_when_kernel_gap_dominates() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        // CPU 10 ms vs CUDA 10 ns kernel; 1 µs per input crossing.
        let est = FlatEstimator { latency_ns: 1_000 };
        let set = relaxed_plan_with_costs(cost_10_000_000, cost_10, &est);
        let w = set.winner().unwrap();
        assert_eq!(w.device, cuda0, "kernel gap dominates → move");
        assert_eq!(
            w.inbound_transfer_ns, 2_000,
            "the move's two input crossings are priced on the winner",
        );
        assert!(
            set.alternatives().iter().all(|c| c.device == cuda0),
            "set pruned to the winner's device",
        );
    }

    /// Stage-2 parity gate: zero signal everywhere (zero kernel
    /// costs, zero-cost transfers) must still keep the op local —
    /// the decision device enumerates first and the rank's stable
    /// sort preserves that order on ties.
    #[test]
    fn compile_plan_relaxed_zero_signal_ties_stay_local() {
        let est = FlatEstimator { latency_ns: 0 };
        let set = relaxed_plan_with_costs(unknown_cost, unknown_cost, &est);
        assert_eq!(
            set.winner().unwrap().device,
            DeviceLocation::Cpu,
            "ties break toward the decision device",
        );
    }

    /// Stage-2 parity gate: single-device systems produce
    /// byte-identical plans with the estimator wired — the fallback
    /// enumerator has nothing to offer and no transfer term fires.
    #[test]
    fn compile_plan_relaxed_single_device_plan_unchanged() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![]
            }
        };
        // Single-device system: no other device exists.
        let fallback_fn = |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> { Vec::new() };
        let residency_fn = |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);

        let base_opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn);
        let base = compile_plan(&g, &order, &table, &base_opts).expect("base");

        let est = FlatEstimator {
            latency_ns: 1_000_000,
        };
        let wired_opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let wired = compile_plan(&g, &order, &table, &wired_opts).expect("wired");

        let a = base.alternatives(add_id).unwrap();
        let b = wired.alternatives(add_id).unwrap();
        assert_eq!(a.len(), b.len(), "same candidate count");
        for (ca, cb) in a.alternatives().iter().zip(b.alternatives()) {
            assert_eq!(ca.backend, cb.backend);
            assert_eq!(ca.device, cb.device);
            assert_eq!(ca.kernel as usize, cb.kernel as usize, "same kernel ref");
            assert_eq!(ca.static_cost, cb.static_cost, "same Layer-1 cost");
            assert_eq!(cb.inbound_transfer_ns, 0, "no transfer term fires");
        }
        assert_eq!(a.context(), b.context(), "same decision context");
    }

    /// Stage-2 hard pin: an explicit `Graph::placement` keeps the
    /// node on its device even with pricing active and a free
    /// "faster" remote sibling — the fallback enumerator is never
    /// consulted for the pinned node.
    #[test]
    fn compile_plan_relaxed_respects_explicit_placement_pin() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Cuda,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (mut g, add_id) = build_add_graph();
        // Hard pin: scheduler / Judge-profiling decision.
        g.set_placement(add_id, DeviceLocation::Cpu);
        let order = topo_order(&g, add_id);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![]
            }
        };
        let fallback_fn = |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            panic!(
                "fallback consulted for a hard-pinned node — explicit \
                 placements must not enter the priced relax",
            );
        };
        let residency_fn = |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
        let est = FlatEstimator { latency_ns: 0 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        assert_eq!(
            plan.alternatives(add_id).unwrap().winner().unwrap().device,
            DeviceLocation::Cpu,
        );
    }

    /// Stage-2 hard pin: destructive ops never enter the priced
    /// relax — a free, faster off-device sibling must not win.
    #[test]
    fn compile_plan_relaxed_destructive_never_moves() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        table.register_full(
            OpKind::ReluInplace,
            &[DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost_600,
        );
        table.register_full(
            OpKind::ReluInplace,
            &[DType::F32, DType::F32],
            BackendId::Cuda,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost_10,
        );
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let relu = g.push(Node {
            op: Op::ReluInplace,
            inputs: vec![lhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, relu);
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![]
            }
        };
        let fallback_fn = move |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![(BackendId::Cuda, cuda0)]
        };
        let residency_fn = |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
        let est = FlatEstimator { latency_ns: 0 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        assert_eq!(
            plan.alternatives(relu).unwrap().winner().unwrap().device,
            DeviceLocation::Cpu,
            "destructive op stays on the device owning its mutation target",
        );
    }

    /// Stage-2 fix of the picker-4b verifier minor: a pinned device
    /// whose registrations all fail the HARD filter chain falls
    /// back off-device like an empty enumeration (legacy/unpriced
    /// regime — no estimator configured).
    #[test]
    fn compile_plan_filter_rejected_pinned_device_falls_back() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        // The pinned CUDA device's only registration is NOT
        // bit-stable; the CPU sibling is.
        register_add_f32(
            &mut table,
            BackendId::Cuda,
            noop_kernel_b,
            PrecisionGuarantee {
                bit_stable_on_same_hardware: false,
                ..PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU
            },
        );
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, lhs],
            shape: Shape::from_dims(&[3]),
            dtype: DType::F32,
        });
        let order = topo_order(&g, add);
        let placements_fn = move |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == cuda0 {
                vec![BackendId::Cuda]
            } else {
                vec![]
            }
        };
        let fallback_fn = |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            vec![(BackendId::Cpu, DeviceLocation::Cpu)]
        };
        let opts = PlanOptions::new()
            .without_cost_population()
            .with_pinned_device(cuda0)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_precision_requirement(PrecisionRequirement::BIT_STABLE);
        let plan = compile_plan(&g, &order, &table, &opts)
            .expect("filter-rejected pin falls back instead of erroring");
        let alts = plan.alternatives(add).expect("Add planned via fallback");
        assert_eq!(alts.len(), 1, "fallback set frozen to one winner");
        let w = alts.winner().unwrap();
        assert_eq!(w.backend, BackendId::Cpu);
        assert_eq!(w.device, DeviceLocation::Cpu);
        assert!(
            w.precision.bit_stable_on_same_hardware,
            "the admissible off-device sibling won",
        );
    }

    /// Stage 2: without an estimator nothing prices — candidates
    /// keep `inbound_transfer_ns == 0` and the plan matches the
    /// estimator-less plan candidate-for-candidate even when an
    /// input-residency callback is supplied.
    #[test]
    fn compile_plan_without_estimator_prices_nothing() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let residency_fn = |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };

        let base_opts = PlanOptions::new().with_capabilities_for(&caps_fn);
        let base = compile_plan(&g, &order, &table, &base_opts).expect("base");

        let with_residency = PlanOptions::new()
            .with_capabilities_for(&caps_fn)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &with_residency).expect("plan");

        let a = base.alternatives(add_id).unwrap();
        let b = plan.alternatives(add_id).unwrap();
        assert_eq!(a.len(), b.len());
        for (ca, cb) in a.alternatives().iter().zip(b.alternatives()) {
            assert_eq!(ca.backend, cb.backend);
            assert_eq!(ca.device, cb.device);
            assert_eq!(ca.kernel as usize, cb.kernel as usize);
            assert_eq!(ca.inbound_transfer_ns, 0);
            assert_eq!(cb.inbound_transfer_ns, 0);
        }
    }

    /// The `require_transfer_pricing` guard: a plan that DECLARES it needs
    /// transfer pricing but has no estimator wired must fail fast at plan-build
    /// time — never silently price transfers at zero (latency-greedy). This is
    /// the production regression guard: dropping `.with_transfer_estimator(...)`
    /// from the realize path (while it still requires pricing) turns this red.
    #[test]
    fn compile_plan_require_transfer_pricing_errors_without_estimator() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);

        // Declares it requires pricing, but wires NO estimator — the exact
        // shape of a realize path that forgot `.with_transfer_estimator(...)`.
        let opts = PlanOptions::new()
            .with_capabilities_for(&caps_fn)
            .require_transfer_pricing();
        let err = compile_plan(&g, &order, &table, &opts).unwrap_err();
        let msg = format!("{err:?}");
        assert!(
            msg.contains("transfer") && (msg.contains("estimator") || msg.contains("pricing")),
            "error should name the missing transfer estimator / pricing; got: {msg}",
        );
    }

    /// The guard is satisfied when both `require_transfer_pricing` AND an
    /// estimator are set — planning proceeds normally.
    #[test]
    fn compile_plan_require_transfer_pricing_ok_with_estimator() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let est = FlatEstimator { latency_ns: 1000 };

        let opts = PlanOptions::new()
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .require_transfer_pricing();
        let plan = compile_plan(&g, &order, &table, &opts)
            .expect("require_transfer_pricing WITH an estimator must plan fine");
        assert!(plan.alternatives(add_id).is_some());
    }

    #[test]
    fn compile_plan_passes_input_layouts_to_filter_chain() {
        // Smoke test: a non-contiguous input + strided-input pref
        // in the default chain prefers the strided-capable kernel.
        // Register two CPU kernels, one with strided caps.
        let mut table = KernelBindingTable::new();
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
        );
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel_b,
            KernelCaps::strided_input(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            unknown_cost,
        );

        // Build a graph where the Add's LHS input is non-contiguous.
        let mut g = Graph::new();
        let lhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[6]),
            dtype: DType::F32,
        });
        // Force a non-contiguous layout on lhs by setting a custom
        // stride via the graph's layout side-table.
        let strides: StrideVec = smallvec![2isize];
        g.set_layout(lhs, Layout::new(Shape::from_dims(&[6]), strides, 0));
        let rhs = g.push(Node {
            op: Op::Const,
            inputs: vec![],
            shape: Shape::from_dims(&[6]),
            dtype: DType::F32,
        });
        let add = g.push(Node {
            op: Op::Add,
            inputs: vec![lhs, rhs],
            shape: Shape::from_dims(&[6]),
            dtype: DType::F32,
        });
        g.set_target_backend(add, BackendId::Cpu);
        let order = topo_order(&g, add);
        let opts = PlanOptions::new().without_cost_population();
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let alts = plan.alternatives(add).unwrap();
        // Strided-input preference narrowed to the strided-capable
        // kernel.
        assert_eq!(alts.len(), 1);
        assert!(alts.winner().unwrap().caps.strided_input);
    }

    // =====================================================================
    // Planner Stage 3: carry-forward placement DP
    // =====================================================================

    /// Byte-aware synthetic estimator: zero same-device, otherwise
    /// `latency + bytes·ns_per_byte`.
    struct BytesEstimator {
        latency_ns: u64,
        ns_per_byte: u64,
    }

    impl crate::ranker::TransferEstimator for BytesEstimator {
        fn estimate_transfer_ns(
            &self,
            src: DeviceLocation,
            dst: DeviceLocation,
            bytes: u64,
        ) -> u64 {
            if src == dst {
                return 0;
            }
            self.latency_ns
                .saturating_add(bytes.saturating_mul(self.ns_per_byte))
        }
    }

    fn cost_50(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 50,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    fn cost_100(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 100,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    fn cost_300(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 300,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    fn cost_800(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 800,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    fn cost_5000(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 5000,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }
    fn cost_100_000(
        _: &[Shape],
        _: &[DType],
        _: &OpParams,
        _: &BackendCapabilities,
    ) -> crate::fused::CostEstimate {
        crate::fused::CostEstimate {
            flops: 100_000,
            bytes_moved: 0,
            kernel_overhead_ns: 0,
        }
    }

    /// Register a unary `(F32 → F32)` kernel with an explicit cost.
    fn register_unary_f32(
        table: &mut KernelBindingTable,
        op: OpKind,
        backend: BackendId,
        kernel: crate::kernel::KernelRef,
        cost: crate::kernel::CostFn,
    ) {
        table.register_full(
            op,
            &[DType::F32, DType::F32],
            backend,
            kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost,
        );
    }

    fn push_f32(g: &mut Graph, op: Op, inputs: Vec<NodeId>, dims: &[usize]) -> NodeId {
        g.push(Node {
            op,
            inputs,
            shape: Shape::from_dims(dims),
            dtype: DType::F32,
        })
    }

    /// Stage 3 test (a): a mid-sequence GPU segment beats all-CPU
    /// and all-GPU, and the per-op GPU win (700 ns) is SMALLER than
    /// the crossing (1000 ns) — greedy would never move (the first
    /// op of the migration can't justify the crossing alone), but
    /// the accumulated DP state amortizes one entry + one exit
    /// crossing over the segment's combined win.
    ///
    /// Chain: c → n1(Sqr) → n2(Neg) → n3(Neg) → n4(Neg) → n5(Sqr).
    /// Sqr: CPU 100 / GPU 5000 (GPU-hostile endpoints).
    /// Neg: CPU 800 / GPU 100 (per-op win 700 < 1000 crossing).
    ///
    /// All-CPU = 100 + 3·800 + 100 = 2600.
    /// Mid-GPU = 100 + 1000 + 3·100 + 1000 + 100 = 2500. ← winner
    /// All-GPU = 1000 + 5000 + 3·100 + 5000 + 1000 = 12300.
    #[test]
    fn dp_mid_sequence_gpu_segment_beats_all_cpu_and_all_gpu() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        register_unary_f32(
            &mut table,
            OpKind::SqrElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_100,
        );
        register_unary_f32(
            &mut table,
            OpKind::SqrElementwise,
            BackendId::Cuda,
            noop_kernel_b,
            cost_5000,
        );
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_800,
        );
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cuda,
            noop_kernel_b,
            cost_100,
        );

        let mut g = Graph::new();
        let c = push_f32(&mut g, Op::Const, vec![], &[4]);
        let n1 = push_f32(&mut g, Op::Sqr, vec![c], &[4]);
        let n2 = push_f32(&mut g, Op::Neg, vec![n1], &[4]);
        let n3 = push_f32(&mut g, Op::Neg, vec![n2], &[4]);
        let n4 = push_f32(&mut g, Op::Neg, vec![n3], &[4]);
        let n5 = push_f32(&mut g, Op::Sqr, vec![n4], &[4]);
        let order = topo_order(&g, n5);

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![BackendId::Cuda]
            }
        };
        let fallback_fn = move |dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            if dev == DeviceLocation::Cpu {
                vec![(BackendId::Cuda, cuda0)]
            } else {
                vec![(BackendId::Cpu, DeviceLocation::Cpu)]
            }
        };
        let residency_fn = move |id: NodeId| -> Option<DeviceLocation> {
            (id == c).then_some(DeviceLocation::Cpu)
        };
        let est = FlatEstimator { latency_ns: 1_000 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");

        let dev_of = |id: NodeId| plan.alternatives(id).unwrap().winner().unwrap().device;
        assert_eq!(
            dev_of(n1),
            DeviceLocation::Cpu,
            "GPU-hostile entry stays CPU"
        );
        assert_eq!(dev_of(n2), cuda0, "segment start migrates");
        assert_eq!(dev_of(n3), cuda0, "segment interior stays GPU");
        assert_eq!(dev_of(n4), cuda0, "segment end stays GPU");
        assert_eq!(dev_of(n5), DeviceLocation::Cpu, "GPU-hostile exit returns");

        // Transfer diagnostics on the final sets: the two crossings
        // are priced exactly once each, on the segment boundary
        // winners.
        let inbound = |id: NodeId| {
            plan.alternatives(id)
                .unwrap()
                .winner()
                .unwrap()
                .inbound_transfer_ns
        };
        assert_eq!(
            inbound(n2),
            1_000,
            "entry crossing priced on the first GPU op"
        );
        assert_eq!(inbound(n3), 0);
        assert_eq!(inbound(n4), 0);
        assert_eq!(inbound(n5), 1_000, "exit crossing priced on the return op");

        // The surviving sets live on ONE device each (residency
        // stitch invariant).
        for id in [n1, n2, n3, n4, n5] {
            let d = dev_of(id);
            assert!(
                plan.alternatives(id)
                    .unwrap()
                    .alternatives()
                    .iter()
                    .all(|c| c.device == d),
                "set pruned to the DP-committed device",
            );
        }
    }

    /// Stage 3 test (b): a FUSED op that is locally faster on device
    /// A loses to staying on B when its stranding cost exceeds its
    /// win — the over-move case greedy inbound pricing cannot see.
    ///
    /// Chain: c → n1(Neg) → f(Fused SoftmaxLastDim) → n2(Sqr, CPU-only).
    /// Estimator: 10 ns latency + 1 ns/byte. n1 output is 4 bytes;
    /// f's output is 1024 bytes (shapes are synthetic — compile_plan
    /// never executes).
    ///
    /// n1: CPU 100 000 / GPU 100 → migrates to GPU regardless.
    /// f:  CPU 300 / GPU 100. Arriving states: CPU = 300 + 14 = 428
    /// (via GPU n1), GPU = 100 + 114 = 214 — locally GPU wins, and
    /// greedy would commit it (inbound favors GPU: 100 + 0 < 300 +
    /// 14). But f's 1024-byte output must land on CPU for n2:
    /// GPU 214 + 1034 = 1248 vs CPU 428 → the DP keeps f on CPU.
    #[test]
    fn dp_fused_op_loses_when_stranding_exceeds_local_win() {
        use fuel_graph::registry::{FusedOpParams, FusedOps};

        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_100_000,
        );
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cuda,
            noop_kernel_b,
            cost_100,
        );
        register_unary_f32(
            &mut table,
            OpKind::SoftmaxLastDim,
            BackendId::Cpu,
            noop_kernel,
            cost_300,
        );
        register_unary_f32(
            &mut table,
            OpKind::SoftmaxLastDim,
            BackendId::Cuda,
            noop_kernel_b,
            cost_100,
        );
        // Sqr exists ONLY on CPU — the chain's anchor.
        register_unary_f32(
            &mut table,
            OpKind::SqrElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_100,
        );

        let mut g = Graph::new();
        let c = push_f32(&mut g, Op::Const, vec![], &[1]);
        let n1 = push_f32(&mut g, Op::Neg, vec![c], &[1]);
        let f = push_f32(
            &mut g,
            Op::Fused(FusedOps::SOFTMAX_LAST_DIM, FusedOpParams::SoftmaxLastDim),
            vec![n1],
            &[256],
        );
        let n2 = push_f32(&mut g, Op::Sqr, vec![f], &[256]);
        let order = topo_order(&g, n2);

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![BackendId::Cuda]
            }
        };
        let fallback_fn = move |dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            if dev == DeviceLocation::Cpu {
                vec![(BackendId::Cuda, cuda0)]
            } else {
                vec![(BackendId::Cpu, DeviceLocation::Cpu)]
            }
        };
        let residency_fn = move |id: NodeId| -> Option<DeviceLocation> {
            (id == c).then_some(DeviceLocation::Cpu)
        };
        let est = BytesEstimator {
            latency_ns: 10,
            ns_per_byte: 1,
        };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");

        let dev_of = |id: NodeId| plan.alternatives(id).unwrap().winner().unwrap().device;
        assert_eq!(dev_of(n1), cuda0, "the huge-win producer migrates");
        assert_eq!(
            dev_of(f),
            DeviceLocation::Cpu,
            "the locally-faster fused op stays where its output is needed",
        );
        assert_eq!(dev_of(n2), DeviceLocation::Cpu);
        assert!(
            plan.alternatives(f)
                .unwrap()
                .alternatives()
                .iter()
                .all(|c| c.device == DeviceLocation::Cpu),
            "fused set pruned to the committed device",
        );
    }

    /// Stage 3 test (c): exit pricing — a final op stays on the
    /// realize target although moving would win locally, because
    /// the return crossing prices the move out.
    ///
    /// Inputs resident on GPU; realize target CPU; flat 1000 ns
    /// crossings. Local view: GPU 100 + 0 ≪ CPU 50 + 1000. With the
    /// exit: GPU 100 + 1000 = 1100 vs CPU 1050 → CPU.
    #[test]
    fn dp_exit_pricing_keeps_final_op_on_realize_target() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_50,
        );
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cuda,
            noop_kernel_b,
            cost_100,
        );

        let mut g = Graph::new();
        let c = push_f32(&mut g, Op::Const, vec![], &[4]);
        let n = push_f32(&mut g, Op::Neg, vec![c], &[4]);
        let order = topo_order(&g, n);

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![BackendId::Cuda]
            }
        };
        let fallback_fn = move |dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            if dev == DeviceLocation::Cpu {
                vec![(BackendId::Cuda, cuda0)]
            } else {
                vec![(BackendId::Cpu, DeviceLocation::Cpu)]
            }
        };
        let residency_fn =
            move |id: NodeId| -> Option<DeviceLocation> { (id == c).then_some(cuda0) };
        let est = FlatEstimator { latency_ns: 1_000 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let w = plan.alternatives(n).unwrap().winner().unwrap();
        assert_eq!(
            w.device,
            DeviceLocation::Cpu,
            "exit pricing flips the locally-winning GPU placement",
        );
        assert_eq!(
            w.inbound_transfer_ns, 1_000,
            "the GPU-resident input's crossing is priced on the CPU winner",
        );
    }

    /// Stage 3 test (c) inverse: when the kernel gap dwarfs both
    /// crossings, the final op legitimately moves despite the exit.
    #[test]
    fn dp_exit_pricing_does_not_pin_when_kernel_gap_dominates() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_10_000_000,
        );
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cuda,
            noop_kernel_b,
            cost_100,
        );

        let mut g = Graph::new();
        let c = push_f32(&mut g, Op::Const, vec![], &[4]);
        let n = push_f32(&mut g, Op::Neg, vec![c], &[4]);
        let order = topo_order(&g, n);

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![BackendId::Cuda]
            }
        };
        let fallback_fn = move |dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            if dev == DeviceLocation::Cpu {
                vec![(BackendId::Cuda, cuda0)]
            } else {
                vec![(BackendId::Cpu, DeviceLocation::Cpu)]
            }
        };
        let residency_fn =
            move |id: NodeId| -> Option<DeviceLocation> { (id == c).then_some(cuda0) };
        let est = FlatEstimator { latency_ns: 1_000 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        assert_eq!(
            plan.alternatives(n).unwrap().winner().unwrap().device,
            cuda0
        );
    }

    /// Stage 3 test (d): single-DEVICE plan equality. Two backends
    /// co-located on one device with the full Stage-3 option set
    /// wired (estimator + fallback + residency) produce the same
    /// plan, candidate-for-candidate, as the unwired baseline — the
    /// DP never opens a row when only one device has candidates.
    #[test]
    fn dp_single_device_plan_equals_greedy_plan() {
        let mut table = KernelBindingTable::new();
        register_add_f32(
            &mut table,
            BackendId::Cpu,
            noop_kernel,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        register_add_f32(
            &mut table,
            BackendId::Cuda,
            noop_kernel_b,
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
        );
        let (g, add_id) = build_add_graph();
        let order = topo_order(&g, add_id);
        // Both backends co-located at DeviceLocation::Cpu — one
        // device, two backends.
        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu, BackendId::Cuda]
            } else {
                vec![]
            }
        };
        let fallback_fn = |_dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> { Vec::new() };
        let residency_fn = |_id: NodeId| -> Option<DeviceLocation> { Some(DeviceLocation::Cpu) };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);

        let base_opts = PlanOptions::new()
            .with_placements_for_device(&placements_fn)
            .with_capabilities_for(&caps_fn);
        let base = compile_plan(&g, &order, &table, &base_opts).expect("base");

        let est = FlatEstimator {
            latency_ns: 1_000_000,
        };
        let wired_opts = PlanOptions::new()
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let wired = compile_plan(&g, &order, &table, &wired_opts).expect("wired");

        assert_eq!(base.alternatives.len(), wired.alternatives.len());
        let a = base.alternatives(add_id).unwrap();
        let b = wired.alternatives(add_id).unwrap();
        assert_eq!(a.len(), b.len(), "same candidate count");
        for (ca, cb) in a.alternatives().iter().zip(b.alternatives()) {
            assert_eq!(ca.backend, cb.backend);
            assert_eq!(ca.device, cb.device);
            assert_eq!(ca.kernel as usize, cb.kernel as usize, "same kernel ref");
            assert_eq!(ca.static_cost, cb.static_cost, "same Layer-1 cost");
            assert_eq!(cb.inbound_transfer_ns, 0, "no transfer term fires");
        }
        assert_eq!(a.context(), b.context(), "same decision context");
    }

    /// Stage 3 test (e) helper: build the diamond (a → b, a → c2,
    /// b+c2 → d), plan it with the given crossing latency, and
    /// assert every kernel-bearing node committed to `expect`.
    fn run_diamond_regime(latency_ns: u64, expect: DeviceLocation) {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        for op in [
            OpKind::NegElementwise,
            OpKind::SqrElementwise,
            OpKind::ReluElementwise,
        ] {
            register_unary_f32(&mut table, op, BackendId::Cpu, noop_kernel, cost_100);
            register_unary_f32(&mut table, op, BackendId::Cuda, noop_kernel_b, cost_10);
        }
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cpu,
            noop_kernel,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost_100,
        );
        table.register_full(
            OpKind::AddElementwise,
            &[DType::F32, DType::F32, DType::F32],
            BackendId::Cuda,
            noop_kernel_b,
            KernelCaps::empty(),
            PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            cost_10,
        );

        let mut g = Graph::new();
        let c0 = push_f32(&mut g, Op::Const, vec![], &[4]);
        let a = push_f32(&mut g, Op::Neg, vec![c0], &[4]);
        let b = push_f32(&mut g, Op::Sqr, vec![a], &[4]);
        let c2 = push_f32(&mut g, Op::Relu, vec![a], &[4]);
        let d = push_f32(&mut g, Op::Add, vec![b, c2], &[4]);
        let order = topo_order(&g, d);

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![BackendId::Cuda]
            }
        };
        let fallback_fn = move |dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            if dev == DeviceLocation::Cpu {
                vec![(BackendId::Cuda, cuda0)]
            } else {
                vec![(BackendId::Cpu, DeviceLocation::Cpu)]
            }
        };
        let residency_fn = move |id: NodeId| -> Option<DeviceLocation> {
            (id == c0).then_some(DeviceLocation::Cpu)
        };
        let est = FlatEstimator { latency_ns };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        for id in [a, b, c2, d] {
            assert_eq!(
                plan.alternatives(id).unwrap().winner().unwrap().device,
                expect,
                "diamond join must commit a consistent placement",
            );
        }
    }

    /// Stage 3 test (e): a diamond merges without panicking and
    /// commits a consistent placement. Both cost regimes exercised:
    /// transfers dominate → everything stays on CPU; kernel gap
    /// dominates with free transfers → everything moves to the GPU.
    #[test]
    fn dp_diamond_join_merges_consistently() {
        // 1 ms crossings — the 90 ns/op GPU win never pays.
        run_diamond_regime(1_000_000, DeviceLocation::Cpu);
        // Free transfers + 10× kernel gap.
        run_diamond_regime(0, DeviceLocation::Cuda { gpu_id: 0 });
    }

    /// Stage 3 perf guard: the DP is O(nodes × devices²). A
    /// 1000-node chain with two devices must plan comfortably under
    /// a wall-clock sanity bound (expected: low milliseconds — the
    /// generous bound only guards against accidental quadratic
    /// blowups in nodes).
    #[test]
    fn dp_thousand_node_chain_plans_within_sanity_bound() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        // Equal costs — ties must break toward the decision device
        // along the whole chain.
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_100,
        );
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cuda,
            noop_kernel_b,
            cost_100,
        );

        let mut g = Graph::new();
        let c = push_f32(&mut g, Op::Const, vec![], &[4]);
        let mut prev = c;
        let mut nodes = Vec::with_capacity(1000);
        for _ in 0..1000 {
            let n = push_f32(&mut g, Op::Neg, vec![prev], &[4]);
            nodes.push(n);
            prev = n;
        }
        let order = topo_order(&g, prev);

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![BackendId::Cuda]
            }
        };
        let fallback_fn = move |dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            if dev == DeviceLocation::Cpu {
                vec![(BackendId::Cuda, cuda0)]
            } else {
                vec![(BackendId::Cpu, DeviceLocation::Cpu)]
            }
        };
        let residency_fn = move |id: NodeId| -> Option<DeviceLocation> {
            (id == c).then_some(DeviceLocation::Cpu)
        };
        let est = FlatEstimator { latency_ns: 10 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);

        let start = std::time::Instant::now();
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");
        let elapsed = start.elapsed();
        assert!(
            elapsed.as_secs() < 5,
            "1000-node DP plan took {elapsed:?} — pathological scaling",
        );

        assert_eq!(plan.alternatives.len(), 1000);
        for &n in &nodes {
            assert_eq!(
                plan.alternatives(n).unwrap().winner().unwrap().device,
                DeviceLocation::Cpu,
                "equal-cost ties break toward the decision device end-to-end",
            );
        }
    }

    /// Stage 3: a view-shaped pass-through between two DP nodes
    /// keeps the chain connected (the alias map) — the segment still
    /// migrates as one unit instead of breaking at the Reshape.
    #[test]
    fn dp_chain_connects_through_view_passthroughs() {
        let cuda0 = DeviceLocation::Cuda { gpu_id: 0 };
        let mut table = KernelBindingTable::new();
        // Per-op win 700 < 1000 crossing; combined win 2100 > 2000 —
        // the segment only migrates if the chain survives the
        // Reshape between n1 and n2.
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cpu,
            noop_kernel,
            cost_800,
        );
        register_unary_f32(
            &mut table,
            OpKind::NegElementwise,
            BackendId::Cuda,
            noop_kernel_b,
            cost_100,
        );

        let mut g = Graph::new();
        let c = push_f32(&mut g, Op::Const, vec![], &[4]);
        let n1 = push_f32(&mut g, Op::Neg, vec![c], &[4]);
        let r = push_f32(
            &mut g,
            Op::Reshape(Shape::from_dims(&[2, 2])),
            vec![n1],
            &[2, 2],
        );
        let n2 = push_f32(&mut g, Op::Neg, vec![r], &[2, 2]);
        let n3 = push_f32(&mut g, Op::Neg, vec![n2], &[2, 2]);
        let order = topo_order(&g, n3);

        let placements_fn = |dev: DeviceLocation| -> Vec<BackendId> {
            if dev == DeviceLocation::Cpu {
                vec![BackendId::Cpu]
            } else {
                vec![BackendId::Cuda]
            }
        };
        let fallback_fn = move |dev: DeviceLocation| -> Vec<(BackendId, DeviceLocation)> {
            if dev == DeviceLocation::Cpu {
                vec![(BackendId::Cuda, cuda0)]
            } else {
                vec![(BackendId::Cpu, DeviceLocation::Cpu)]
            }
        };
        let residency_fn = move |id: NodeId| -> Option<DeviceLocation> {
            (id == c).then_some(DeviceLocation::Cpu)
        };
        let est = FlatEstimator { latency_ns: 1_000 };
        let cpu_caps_val = cpu_caps();
        let caps_fn = |_: BackendId| Some(&cpu_caps_val);
        let opts = PlanOptions::new()
            .with_pinned_device(DeviceLocation::Cpu)
            .with_placements_for_device(&placements_fn)
            .with_fallback_placements_for(&fallback_fn)
            .with_capabilities_for(&caps_fn)
            .with_transfer_estimator(&est)
            .with_input_residency(&residency_fn);
        let plan = compile_plan(&g, &order, &table, &opts).expect("compile");

        // All-CPU = 2400; migrate-at-n1 = 3×100 + 1000 (entry) +
        // 1000 (exit) = 2300 → the whole segment moves, THROUGH the
        // reshape.
        for id in [n1, n2, n3] {
            assert_eq!(
                plan.alternatives(id).unwrap().winner().unwrap().device,
                cuda0,
                "the chain must not break at the Reshape pass-through",
            );
        }
        assert!(
            plan.alternatives(r).is_none(),
            "Reshape carries no plan entry"
        );
    }

    // =====================================================================
    // Baracuda structural-miss telemetry: the LIVE wire-in at the dispatch
    // pick site (behind the `telemetry` feature).
    // =====================================================================
    #[cfg(feature = "telemetry")]
    mod telemetry_wiring {
        use super::*;
        use crate::telemetry::{
            Contiguity, FdxOperandDesc, HwStamp, StructureKeyProvider, StructureKeyToken,
            TelemetryConfig, TelemetryHooks, TelemetryMode, TelemetrySink,
        };
        use std::sync::Mutex;

        /// A stub provider that echoes the `op_class` + the LIVE operand count
        /// into the token, so a test can prove the live operands reached the
        /// provider verbatim (Fuel returns the token as-is).
        struct StubProvider;
        impl StructureKeyProvider for StubProvider {
            fn structure_key(
                &self,
                op_class: &str,
                operands: &[FdxOperandDesc],
                _arch: &str,
            ) -> Option<StructureKeyToken> {
                Some(StructureKeyToken(format!(
                    "{op_class}:stub:n={}",
                    operands.len()
                )))
            }
        }

        fn cpu_hw() -> HwStamp {
            HwStamp {
                compute_capability: None,
                hardware_sku: "test-cpu".into(),
                driver_version: "n/a".into(),
            }
        }

        /// A table with ONLY a generic-fallback Add binding at
        /// `(AddElementwise, [F32;3], Cpu)` — a generic-only cell. The
        /// `is_generic = true` bit is exactly what an FKC import of a
        /// generic-strided contract would stamp.
        fn generic_only_add_table() -> KernelBindingTable {
            let mut table = KernelBindingTable::new();
            table.register_full_with_source_generic(
                OpKind::AddElementwise,
                &[DType::F32, DType::F32, DType::F32],
                BackendId::Cpu,
                noop_kernel,
                KernelCaps::strided_input(),
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
                unknown_cost,
                "baracuda-generic-strided",
                true, // the FKC-computed generic-fallback bit
                0,    // revision UNTRACKED (test binding carries no revision)
                None, // no declared cost-expression AST (test binding)
            );
            table
        }

        /// BORN-RED: a generic-only cell + a stub provider + a sink threaded
        /// through `PlanOptions` ⇒ the realize/compile-plan path leaves the
        /// sink holding exactly ONE aggregated `MissRecord` whose `wanted`
        /// token reflects the LIVE operands, whose `fallback` is the generic
        /// binding's `ImplId`, and whose `hw` is the dispatching device's
        /// stamp.
        ///
        /// This was RED before the wire-in: `compile_plan` never called
        /// `detect_miss`, so the sink stayed empty. GREEN once the post-plan
        /// emission pass resolves each winner back to its binding entry and
        /// emits for the generic ones.
        #[test]
        fn generic_only_cell_emits_one_aggregated_miss_through_compile_plan() {
            let table = generic_only_add_table();
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);

            let sink = Mutex::new(TelemetrySink::new());
            let provider = StubProvider;
            let config = TelemetryConfig {
                mode: TelemetryMode::Coarse,
                out_path: None,
            };
            let hooks = TelemetryHooks {
                config: &config,
                sink: &sink,
                provider: &provider,
                hw: cpu_hw(),
            };
            let opts = PlanOptions::new()
                .without_cost_population()
                .with_telemetry(&hooks);
            let _plan = compile_plan(&g, &order, &table, &opts).expect("compile");

            let sink = sink.into_inner().expect("sink lock");
            assert_eq!(
                sink.miss_cell_count(),
                1,
                "a generic-only cell must emit exactly one aggregated miss cell",
            );
            let recs = sink.miss_records();
            assert_eq!(recs.len(), 1);
            // OpKind Debug = "AddElementwise"; the Add node has TWO inputs plus
            // its OUTPUT operand (inputs-then-output), all of which flowed to
            // the provider as FDX descriptors ⇒ n=3.
            assert_eq!(
                recs[0].wanted,
                StructureKeyToken("AddElementwise:stub:n=3".into()),
                "the wanted token must reflect the live op_class + operands",
            );
            assert_eq!(recs[0].fallback.kernel_source, "baracuda-generic-strided");
            assert_eq!(recs[0].fallback.op, OpKind::AddElementwise);
            assert_eq!(recs[0].fallback.backend, BackendId::Cpu);
            assert_eq!(
                recs[0].fallback.dtypes,
                vec![DType::F32, DType::F32, DType::F32],
            );
            assert_eq!(recs[0].count, 1);
            assert_eq!(recs[0].hw, cpu_hw());
        }

        /// GUARD: telemetry `Off` ⇒ zero emission and no behavior change. The
        /// same generic-only cell, but the config is the default (`Off`): the
        /// sink stays empty and the plan is unchanged (the Add still carries
        /// its single generic alternative). The Off gate is the first,
        /// branch-predictable check in the emission pass.
        #[test]
        fn telemetry_off_emits_nothing_and_does_not_change_the_plan() {
            let table = generic_only_add_table();
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);

            let sink = Mutex::new(TelemetrySink::new());
            let provider = StubProvider;
            let config = TelemetryConfig::default(); // Off
            let hooks = TelemetryHooks {
                config: &config,
                sink: &sink,
                provider: &provider,
                hw: cpu_hw(),
            };
            let opts = PlanOptions::new()
                .without_cost_population()
                .with_telemetry(&hooks);
            let plan = compile_plan(&g, &order, &table, &opts).expect("compile");

            assert!(
                sink.into_inner().expect("sink lock").is_empty(),
                "Off mode must emit nothing",
            );
            // No behavior change: the Add still carries its generic alternative.
            let alts = plan.alternatives(add_id).expect("Add alternatives present");
            assert_eq!(alts.len(), 1);
            assert_eq!(alts.winner().unwrap().backend, BackendId::Cpu);
        }

        /// A specialized (non-generic) winner emits NO miss: the same Add
        /// cell, but the binding carries `is_generic = false` (what a
        /// hand-written or structure-specialized registration stamps). The
        /// pick site reads the precomputed bool and skips emission.
        #[test]
        fn specialized_winner_emits_no_miss() {
            let mut table = KernelBindingTable::new();
            // Default (hand-written) registration ⇒ is_generic = false.
            register_add_f32(
                &mut table,
                BackendId::Cpu,
                noop_kernel,
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
            );
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);

            let sink = Mutex::new(TelemetrySink::new());
            let provider = StubProvider;
            let config = TelemetryConfig {
                mode: TelemetryMode::Coarse,
                out_path: None,
            };
            let hooks = TelemetryHooks {
                config: &config,
                sink: &sink,
                provider: &provider,
                hw: cpu_hw(),
            };
            let opts = PlanOptions::new()
                .without_cost_population()
                .with_telemetry(&hooks);
            let _plan = compile_plan(&g, &order, &table, &opts).expect("compile");

            let sink = sink.into_inner().expect("sink lock");
            assert!(
                sink.miss_records().is_empty(),
                "a specialized (is_generic=false) winner must emit no miss",
            );
            // The chosen signal STILL fires: a dispatch record is emitted for
            // every planned cell (generic or not); only the miss half is gated
            // on genericity.
            assert_eq!(
                sink.dispatch_cell_count(),
                1,
                "a specialized winner still emits its dispatch record",
            );
            let _ = add_id;
        }

        // -----------------------------------------------------------------
        // Dispatch-record half (the sibling of the miss half).
        // -----------------------------------------------------------------

        /// A table with TWO specialized (hand-written) Add siblings at one Cpu
        /// cell — distinct fn-pointers + sources. Both are planned candidates
        /// for the dispatch record; neither is generic, so neither emits a miss.
        fn two_sibling_add_table() -> KernelBindingTable {
            let mut table = KernelBindingTable::new();
            table.register_full_with_source(
                OpKind::AddElementwise,
                &[DType::F32, DType::F32, DType::F32],
                BackendId::Cpu,
                noop_kernel,
                KernelCaps::empty(),
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
                unknown_cost,
                "portable-cpu",
            );
            table.register_full_with_source(
                OpKind::AddElementwise,
                &[DType::F32, DType::F32, DType::F32],
                BackendId::Cpu,
                noop_kernel_b,
                KernelCaps::empty(),
                PrecisionGuarantee::PRIMITIVE_DETERMINISTIC_CPU,
                unknown_cost,
                "aocl",
            );
            table
        }

        /// BORN-RED (the headline): a planned cell with two candidates, a stub
        /// Judge measuring ONLY the winner, and Detailed-mode hooks threaded
        /// through `PlanOptions` ⇒ the compile-plan path leaves the sink holding
        /// exactly ONE aggregated `DispatchRecord` whose `chosen` matches the
        /// winner, whose `candidates[]` carries BOTH siblings with the measured
        /// one `Some(ns)` and the unmeasured one `None` (never 0).
        ///
        /// RED before the wire-in: `compile_plan` never recorded a dispatch
        /// record, so `dispatch_cell_count()` was 0. GREEN once the post-plan
        /// pass emits a `DispatchRecord` per winner and fills `candidates[]`
        /// from the plan-time Judge oracle.
        #[test]
        fn detailed_dispatch_record_emitted_with_per_candidate_judge_latency() {
            use crate::ranker::HashMapJudge;
            use fuel_ir::dispatch::SizeClass;

            let table = two_sibling_add_table();
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);

            // Measure ONLY the winner cell ("portable-cpu"); the sibling
            // ("aocl") is unmeasured ⇒ None (never a fabricated 0). The size
            // class mirrors the planner's key: first input elem_count = 3.
            let sc = SizeClass::from_elem_count(3);
            let mut judge = HashMapJudge::new();
            judge.insert(
                OpKind::AddElementwise,
                DType::F32,
                sc,
                BackendId::Cpu,
                "portable-cpu",
                41_230,
                0,
            );

            let sink = Mutex::new(TelemetrySink::new());
            let provider = StubProvider;
            let config = TelemetryConfig {
                mode: TelemetryMode::Detailed,
                out_path: None,
            };
            let hooks = TelemetryHooks {
                config: &config,
                sink: &sink,
                provider: &provider,
                hw: cpu_hw(),
            };
            let opts = PlanOptions::new()
                .without_cost_population()
                .with_telemetry(&hooks)
                .with_judge(&judge);
            let _plan = compile_plan(&g, &order, &table, &opts).expect("compile");

            let sink = sink.into_inner().expect("sink lock");
            assert_eq!(
                sink.dispatch_cell_count(),
                1,
                "one planned cell ⇒ exactly one aggregated dispatch record",
            );
            assert_eq!(
                sink.miss_cell_count(),
                0,
                "specialized winners emit no miss"
            );
            let recs = sink.dispatch_records();
            assert_eq!(recs.len(), 1);
            let rec = &recs[0];
            // chosen = the winner (first-registered sibling), revision untracked.
            assert_eq!(rec.chosen.kernel_source, "portable-cpu");
            assert_eq!(rec.chosen.op, OpKind::AddElementwise);
            assert_eq!(rec.chosen.backend, BackendId::Cpu);
            assert_eq!(rec.chosen.dtypes, vec![DType::F32, DType::F32, DType::F32]);
            assert_eq!(
                rec.chosen.kernel_revision_hash, 0,
                "hand-written ⇒ untracked"
            );
            assert_eq!(rec.count, 1);
            assert_eq!(rec.hw, cpu_hw());
            // structure_key from the stub provider (2 inputs + the output
            // operand reached it, inputs-then-output ⇒ n=3).
            assert_eq!(
                rec.structure_key,
                Some(StructureKeyToken("AddElementwise:stub:n=3".into())),
            );
            // candidates: BOTH siblings, each with its OWN latency (a loser
            // keeps its own number, never the winner's).
            assert_eq!(rec.candidates.len(), 2, "both admitted alternatives appear");
            let by_src = |src: &str| {
                rec.candidates
                    .iter()
                    .find(|c| c.impl_id.kernel_source == src)
                    .unwrap_or_else(|| panic!("candidate {src} present"))
            };
            assert_eq!(
                by_src("portable-cpu").latency_ns,
                Some(41_230),
                "the measured winner carries its own ns",
            );
            assert_eq!(
                by_src("aocl").latency_ns,
                None,
                "the unmeasured sibling is None, never a fabricated 0",
            );
        }

        /// GUARD — Coarse: the dispatch record still fires (the `chosen` signal
        /// for every planned cell) but carries NO `candidates[]` (no Judge
        /// reads), per the documented Coarse semantics.
        #[test]
        fn coarse_dispatch_record_has_no_candidates() {
            let table = two_sibling_add_table();
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);

            let sink = Mutex::new(TelemetrySink::new());
            let provider = StubProvider;
            let config = TelemetryConfig {
                mode: TelemetryMode::Coarse,
                out_path: None,
            };
            let hooks = TelemetryHooks {
                config: &config,
                sink: &sink,
                provider: &provider,
                hw: cpu_hw(),
            };
            let opts = PlanOptions::new()
                .without_cost_population()
                .with_telemetry(&hooks);
            let _plan = compile_plan(&g, &order, &table, &opts).expect("compile");

            let sink = sink.into_inner().expect("sink lock");
            assert_eq!(
                sink.dispatch_cell_count(),
                1,
                "Coarse still emits the chosen signal"
            );
            let recs = sink.dispatch_records();
            assert!(
                recs[0].candidates.is_empty(),
                "Coarse omits candidates (no Judge reads)"
            );
            assert_eq!(recs[0].chosen.kernel_source, "portable-cpu");
            assert_eq!(recs[0].count, 1);
        }

        /// GUARD — Off: neither a dispatch record nor a miss is emitted (the
        /// first, branch-predictable gate). The sink stays entirely empty.
        #[test]
        fn telemetry_off_emits_no_dispatch_record() {
            let table = two_sibling_add_table();
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);

            let sink = Mutex::new(TelemetrySink::new());
            let provider = StubProvider;
            let config = TelemetryConfig::default(); // Off
            let hooks = TelemetryHooks {
                config: &config,
                sink: &sink,
                provider: &provider,
                hw: cpu_hw(),
            };
            let opts = PlanOptions::new()
                .without_cost_population()
                .with_telemetry(&hooks);
            let _plan = compile_plan(&g, &order, &table, &opts).expect("compile");

            assert!(
                sink.into_inner().expect("sink lock").is_empty(),
                "Off emits neither dispatch nor miss",
            );
            let _ = add_id;
        }

        // -----------------------------------------------------------------
        // OUTPUT operand folded into the structure-key slice (Baracuda's
        // inputs-THEN-output OperandDesc convention).
        // -----------------------------------------------------------------

        /// A recording provider: captures the exact `FdxOperandDesc` slice it
        /// was handed, so a test can prove the node's OUTPUT operand was folded
        /// in AFTER the inputs (baracuda keys on inputs-then-output).
        struct RecordingProvider {
            seen: Mutex<Vec<FdxOperandDesc>>,
        }
        impl StructureKeyProvider for RecordingProvider {
            fn structure_key(
                &self,
                op_class: &str,
                operands: &[FdxOperandDesc],
                _arch: &str,
            ) -> Option<StructureKeyToken> {
                *self.seen.lock().expect("record lock") = operands.to_vec();
                Some(StructureKeyToken(format!(
                    "{op_class}:n={}",
                    operands.len()
                )))
            }
        }

        /// BORN-RED: the operand slice handed to the structure-key provider at
        /// the emission site must be node INPUTS followed by the node's own
        /// OUTPUT operand (baracuda's inputs-THEN-output `OperandDesc`
        /// convention). The Add cell has two inputs, so the slice must be
        /// length 3 with the trailing operand describing the output tensor.
        ///
        /// RED before the fix: the emission site built the slice from
        /// `node.inputs` only ⇒ length 2, no output operand. GREEN once the
        /// output desc (from the node's own layout/dtype) is appended.
        #[test]
        fn structure_key_operand_slice_folds_in_the_output() {
            let table = generic_only_add_table();
            let (g, add_id) = build_add_graph();
            let order = topo_order(&g, add_id);

            let sink = Mutex::new(TelemetrySink::new());
            let provider = RecordingProvider {
                seen: Mutex::new(Vec::new()),
            };
            let config = TelemetryConfig {
                mode: TelemetryMode::Coarse,
                out_path: None,
            };
            let hooks = TelemetryHooks {
                config: &config,
                sink: &sink,
                provider: &provider,
                hw: cpu_hw(),
            };
            let opts = PlanOptions::new()
                .without_cost_population()
                .with_telemetry(&hooks);
            let _plan = compile_plan(&g, &order, &table, &opts).expect("compile");

            let seen = provider.seen.into_inner().expect("record lock");
            // 2 inputs + 1 output ⇒ 3 operands, output LAST.
            assert_eq!(
                seen.len(),
                3,
                "operand slice must be node inputs (2) then the output (1)",
            );
            let out = seen.last().expect("output operand present");
            // The Add output is a contiguous [3] F32 tensor.
            assert_eq!(
                out.dtype,
                DType::F32,
                "output operand carries the node dtype"
            );
            assert_eq!(
                out.shape,
                vec![3i64],
                "output operand carries the node shape"
            );
            assert_eq!(
                out.contiguity,
                Contiguity::Contiguous,
                "the Add output is contiguous",
            );
            let _ = add_id;
        }
    }
}