net-mesh 0.21.0

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

use std::time::Duration;

use serde::{Deserialize, Serialize};

use super::error::MeshError;
use super::query::{AggregateFn, ChainRef, Expr, JoinKey, JoinKind, MeshQuery, QueryV1, SeqNum};
use crate::adapter::net::behavior::capability::CapabilityIndex;
use crate::adapter::net::behavior::predicate::PredicateWire;
use crate::adapter::net::behavior::query::CapabilityQuery;
use crate::adapter::net::behavior::tag::{Tag, TaxonomyAxis};

/// A planned-but-not-yet-executed query tree. Each node is
/// annotated with execution metadata (target nodes, capability
/// requirements, cost estimate, result schema). The executor
/// walks this tree.
///
/// Carries `Serialize + Deserialize` so plans can ride the wire
/// to remote executors (Phase B's federation layer + the cache
/// invalidation key both consume the encoded form).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ExecutionPlan {
    /// Top-level operator node. The tree is acyclic: composite
    /// operators reference children via [`OperatorNode`]'s
    /// `inputs` field.
    pub root: OperatorNode,
    /// Total estimated cost summed across all nodes in the
    /// tree. Operators on the cost-driven path (join strategy
    /// selection in Phase D) consult this.
    pub total_cost: CostEstimate,
}

/// One node in the [`ExecutionPlan`] tree. Carries the operator
/// shape + the executor-targeting metadata.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct OperatorNode {
    /// The operator + its operator-specific parameters.
    pub operator: OperatorPlan,
    /// `node_id`s that hold the data this operator reads.
    /// Empty for nodes that don't touch the substrate
    /// directly (e.g. a top-of-tree `Project` that runs at
    /// the caller). Ordered lexicographically for
    /// determinism.
    pub target_nodes: Vec<u64>,
    /// Cost estimate for this operator alone (not the
    /// subtree). Phase A populates conservatively; phases
    /// B–E refine.
    pub cost: CostEstimate,
}

/// Operator-specific shape inside an [`OperatorNode`]. Mirrors
/// the [`QueryV1`] variants with planner annotations baked in.
///
/// Composite operators (`Filter`, `Aggregate`, `Project`,
/// `OrderBy`, `Join`, `Lineage*`) carry their inputs as
/// `Box<OperatorNode>` so the tree is fully typed. Phase A
/// ships the operator-plan shape; Phases B–E populate it for
/// every variant.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum OperatorPlan {
    /// Read a single event at `seq` from one of the
    /// `target_nodes`.
    AtRead {
        /// Resolved chain origin hash (post-`ChainRef::Discovered`
        /// resolution; substrate `u64`).
        origin: u64,
        /// Sequence number to read.
        seq: SeqNum,
    },
    /// Read events in `[start, end)` from one of the
    /// `target_nodes`. Half-open range.
    BetweenRead {
        /// Resolved chain origin hash.
        origin: u64,
        /// Lower bound (inclusive).
        start: SeqNum,
        /// Upper bound (exclusive).
        end: SeqNum,
    },
    /// Read the tip event from one of the `target_nodes`.
    LatestRead {
        /// Resolved chain origin hash.
        origin: u64,
    },
    /// Composite — Filter inner rows by predicate. Phase E
    /// territory; the planner surface lands now so downstream
    /// code can pattern-match against the variant.
    Filter {
        /// Inner sub-plan.
        input: Box<OperatorNode>,
        /// Filter predicate (wire form).
        predicate: PredicateWire,
    },
    /// Materialized lineage walk — the planner snapshotted the
    /// `fork-of:` graph (backward or forward) and produced
    /// `entries` in walk order. The executor emits one
    /// [`ResultRow`] per entry: `origin = entry.origin`,
    /// `seq = entry.tip_seq.unwrap_or(SeqNum(0))`, payload
    /// empty. Callers wanting full event content compose with
    /// `At` / `Between` against each entry's origin.
    ///
    /// Walk-at-plan-time uses the local capability index as a
    /// snapshot, matching the plan's "no lineage streaming"
    /// scope for Phase C. Drift between snapshot + read time
    /// is bounded by the CAP-ANN broadcast cadence.
    ///
    /// [`ResultRow`]: super::query::ResultRow
    LineageEmit {
        /// Start origin of the walk (post-`ChainRef::Discovered`
        /// resolution).
        origin: u64,
        /// Walk direction.
        direction: LineageDirection,
        /// One entry per chain reached. Ordered: ancestors-
        /// first for `Back`, BFS-asc-depth for `Forward`. Always
        /// includes the start origin at index 0 with `depth = 0`.
        entries: Vec<LineageEntry>,
    },
    /// Phase E-1 count aggregate. Executor collects the inner
    /// sub-plan's rows, groups by the row-intrinsic key (or
    /// uses a single bucket when `group_by` is `None`), and
    /// emits one [`super::query::ResultRow`] per group whose
    /// `payload` is a postcard-encoded
    /// [`super::query::AggregateRowPayload`] carrying the
    /// group identifier + count.
    AggregateCount {
        /// Inner sub-plan whose rows are counted.
        input: Box<OperatorNode>,
        /// Row-intrinsic group key. `None` = single bucket
        /// (total count). Phase E-1 supports only the row-
        /// intrinsic modes; payload-keyed grouping lands in
        /// Phase E-2 alongside row-schema decoding.
        group_by: Option<JoinKeyMode>,
    },
    /// Phase E-3 numeric aggregate (Sum / Avg) over a
    /// row-intrinsic or JSON-payload field. The executor
    /// extracts a `f64` per row via
    /// [`super::row::extract_numeric`], skips rows whose
    /// extraction returns `None`, and emits one
    /// [`super::query::ResultRow`] per group carrying a
    /// postcard-encoded
    /// [`super::query::AggregateRowPayload`].
    AggregateNumeric {
        /// Inner sub-plan whose rows are aggregated.
        input: Box<OperatorNode>,
        /// Row-intrinsic group key, or `None` for a single
        /// bucket.
        group_by: Option<JoinKeyMode>,
        /// Field path to extract the numeric value from. See
        /// [`super::row::extract_numeric`] for resolution.
        field_path: String,
        /// Which numeric function to apply.
        kind: super::query::NumericAggregateKind,
    },
    /// Phase E-4 numeric reduction (Min / Max / exact
    /// Percentile). Collects every per-row numeric value into
    /// the group, then reduces. `Percentile` sorts the bag and
    /// picks the nearest-rank quantile.
    AggregateReduction {
        /// Inner sub-plan whose rows are reduced.
        input: Box<OperatorNode>,
        /// Row-intrinsic group key.
        group_by: Option<JoinKeyMode>,
        /// Field path.
        field_path: String,
        /// Reduction kind.
        kind: super::query::NumericReductionKind,
    },
    /// Phase E-4 exact distinct count over a row-intrinsic /
    /// JSON field. Tracks the canonical string form of each
    /// leaf value in a per-group `BTreeSet`. Bounded by the
    /// executor's per-query memory budget.
    AggregateDistinct {
        /// Inner sub-plan whose rows are aggregated.
        input: Box<OperatorNode>,
        /// Row-intrinsic group key.
        group_by: Option<JoinKeyMode>,
        /// Field path. The leaf value's canonical string form
        /// is what gets hashed for distinctness.
        field_path: String,
    },
    /// Phase E-5 tumbling window operator. The executor reads
    /// `input`'s rows, buckets them by
    /// [`super::query::WindowSpec`], and emits one sentinel
    /// [`super::query::ResultRow`] per non-empty bucket whose
    /// `payload` is a postcard-encoded
    /// [`super::query::WindowBoundary`].
    Window {
        /// Inner sub-plan whose rows are bucketed.
        input: Box<OperatorNode>,
        /// Window strategy.
        spec: super::query::WindowSpec,
    },
    /// Inner hash-join of two sub-plans. Phase D-1 ships the
    /// in-memory hash-build-on-left / probe-on-right strategy
    /// against row-intrinsic keys (`origin` / `seq`); richer
    /// key extraction over event payloads lands in Phase E
    /// once row schemas are decoded.
    ///
    /// The executor emits one [`super::query::ResultRow`] per
    /// matched pair: `origin = 0` (sentinel), `seq = SeqNum(0)`
    /// (sentinel), `payload =` postcard-encoded
    /// [`super::query::JoinedRowPayload`] carrying the
    /// original `(left, right)` rows.
    HashJoin {
        /// Left sub-plan.
        left: Box<OperatorNode>,
        /// Right sub-plan.
        right: Box<OperatorNode>,
        /// How to extract the join key from each side's rows.
        key_mode: JoinKeyMode,
        /// Inner / outer semantics. All four kinds ship in
        /// Phase D-2.
        kind: JoinKind,
        /// Algorithm picker. Phase D-1 shipped
        /// `HashBroadcast`; Phase D-2 adds `SortMerge` for
        /// the unbounded-build-side case.
        strategy: JoinStrategy,
        /// Late-arrival watermark per locked decision #2.
        /// MeshDB's executor runs over snapshot inputs, so
        /// the watermark is informational only at present —
        /// streaming-window joins are a Phase F+ extension
        /// once a consumer drives the streaming semantics.
        watermark: Duration,
    },
    /// Placeholder operator — emitted by the planner when an
    /// operator's executor hasn't been wired yet. Carries a
    /// diagnostic so the executor can surface a useful
    /// `MeshError::PlannerError` at run time, and the inner
    /// sub-plan so the rest of the tree still type-checks /
    /// optimizes / tests.
    NotYetImplemented {
        /// Diagnostic shown to the operator (e.g. "Join not
        /// yet implemented in Phase A").
        detail: String,
        /// Inner sub-plan (None for atomic operators, Some
        /// for composites whose inner already planned).
        input: Option<Box<OperatorNode>>,
    },
}

/// Join-key extraction mode. Started in Phase D-1 with row-
/// intrinsic-only modes; Phase D-2 adds payload-keyed extraction
/// via [`JoinKeyMode::Field`].
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum JoinKeyMode {
    /// Hash on `ResultRow.origin` (8-byte LE encoding).
    Origin,
    /// Hash on `ResultRow.seq.0` (8-byte LE encoding).
    Seq,
    /// Hash on the `(origin, seq)` tuple (16-byte LE encoding).
    OriginSeq,
    /// Hash on the canonical string projection of a row's
    /// payload field at `path`. JSON payloads only; rows whose
    /// path resolves to a missing key, non-JSON payload, or
    /// non-scalar leaf are silently dropped from the build
    /// side (so unmatched-build outer-join semantics treat
    /// them as if the row never existed).
    Field(String),
}

/// Hash-join algorithm picker. Phase D-1 shipped broadcast
/// hash; Phase D-2 adds sort-merge as an alternative for very
/// large inputs whose build side wouldn't fit in memory.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum JoinStrategy {
    /// In-memory hash table on the build side, probe on the
    /// other side. Best for small-or-medium build sides;
    /// surfaces [`super::error::MeshError::JoinMemoryExceeded`]
    /// past the configured bound.
    HashBroadcast,
    /// Sort both sides on the join key, then two-pointer
    /// merge. Better for very large inputs that won't fit the
    /// hash table's memory bound. Phase D-2's planner picks
    /// hash by default; consumers driving the choice point at
    /// sort-merge via the explicit operator.
    SortMerge,
}

/// Direction of a [`OperatorPlan::LineageEmit`] walk.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum LineageDirection {
    /// Walk `fork-of:` parents toward ancestors. The
    /// `CapabilityIndex` answer is direct: each chain's
    /// `fork-of:<parent_hash>` tag names its parent.
    Back,
    /// Walk `fork-of:` descendants. Scans every entry in the
    /// capability index for a `fork-of:<this_origin>` tag,
    /// BFS-style, sorted by chain hash for determinism.
    Forward,
}

/// One chain reached during a [`OperatorPlan::LineageEmit`] walk.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct LineageEntry {
    /// Chain origin hash (substrate `u64`).
    pub origin: u64,
    /// Hops from the walk's start. `0` for the start origin.
    pub depth: u32,
    /// Best-known tip seq from the holders' `causal:` claims.
    /// `None` when no holder advertises a `Tip` or `Range`
    /// claim (e.g. presence-only).
    pub tip_seq: Option<SeqNum>,
}

/// Planner cost estimate. Phase A uses a conservative
/// proximity + heuristic-bandwidth stub; later phases refine
/// against the capability index's `aggregate` primitive.
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct CostEstimate {
    /// Estimated bytes the operator will read / produce.
    pub bandwidth_bytes: u64,
    /// Estimated latency in milliseconds. Phase A uses the
    /// proximity-graph RTT to the target node.
    pub latency_ms: u64,
}

/// Heuristic bandwidth per atomic read in Phase A. Chain reads
/// are typically small; refining this requires cardinality
/// estimates that ship in Phase B.
const PHASE_A_ATOMIC_BANDWIDTH_BYTES: u64 = 64 * 1024;

/// The planner. Borrows the capability index for holder
/// lookups + `ChainRef::Discovered` resolution. Borrows an RTT
/// lookup closure so the cost-estimate path stays decoupled
/// from the proximity-graph internals (mirrors the
/// `CapabilityQuery::nearest` pattern).
///
/// Lifetimes: `'a` ties the borrows together; one planner per
/// `plan()` call typically. The planner itself holds no state
/// — same inputs produce the same plan.
pub struct MeshQueryPlanner<'a, F>
where
    F: Fn(u64) -> Option<Duration>,
{
    /// Capability index used for `Discovered` resolution + holder
    /// lookup.
    pub capability_index: &'a CapabilityIndex,
    /// RTT lookup closure. Same shape as
    /// `CapabilityQuery::nearest`.
    pub rtt_lookup: F,
}

impl<'a, F> MeshQueryPlanner<'a, F>
where
    F: Fn(u64) -> Option<Duration>,
{
    /// Construct a planner. Doesn't allocate.
    pub fn new(capability_index: &'a CapabilityIndex, rtt_lookup: F) -> Self {
        Self {
            capability_index,
            rtt_lookup,
        }
    }

    /// Translate a [`MeshQuery`] into an [`ExecutionPlan`].
    /// Phase A handles atomic operators end-to-end; composite
    /// operators recurse into their inner sub-queries and
    /// emit a `NotYetImplemented` wrapper for the outer.
    ///
    /// Errors per `MeshError`:
    /// - `PlannerError { detail: "unsupported query version" }`
    ///   if the outer enum version isn't `V1`.
    /// - `NoCapableHolder { ... }` if a `Discovered` predicate
    ///   resolves to zero origin hashes.
    /// - `HistoricalRangeUnavailable { ... }` if a `Between`
    ///   query's range isn't covered by any reachable holder.
    pub fn plan(&self, query: &MeshQuery) -> Result<ExecutionPlan, MeshError> {
        let root = match query {
            MeshQuery::V1(v1) => self.plan_v1(v1)?,
            // Locked decision #1: future versions reject
            // cleanly. The non-exhaustive match below is for
            // forward-compat — adding `V2` lands the same
            // error path with no source-side break.
            #[allow(unreachable_patterns)]
            _ => {
                return Err(MeshError::PlannerError {
                    detail: "unsupported query version".to_string(),
                });
            }
        };
        let total_cost = sum_cost(&root);
        Ok(ExecutionPlan { root, total_cost })
    }

    /// Dispatch a single `QueryV1` variant. Recursive for
    /// composite operators.
    fn plan_v1(&self, q: &QueryV1) -> Result<OperatorNode, MeshError> {
        match q {
            QueryV1::At { origin, seq } => self.plan_at(origin, *seq),
            QueryV1::Between { origin, start, end } => self.plan_between(origin, *start, *end),
            QueryV1::Latest { origin } => self.plan_latest(origin),

            QueryV1::Filter { inner, predicate } => {
                // Phase A: plan the inner; wrap with a Filter
                // operator-plan node. The Filter executor
                // itself lands in Phase E (per the plan).
                let input = self.plan(inner)?;
                let cost = CostEstimate {
                    // Predicate evaluation is local — no
                    // additional bandwidth beyond reading.
                    bandwidth_bytes: 0,
                    latency_ms: 0,
                };
                Ok(OperatorNode {
                    operator: OperatorPlan::Filter {
                        input: Box::new(input.root),
                        predicate: predicate.clone(),
                    },
                    target_nodes: vec![],
                    cost,
                })
            }

            QueryV1::LineageBack { origin, max_depth } => {
                self.plan_lineage(origin, *max_depth, LineageDirection::Back)
            }
            QueryV1::LineageForward { origin, max_depth } => {
                self.plan_lineage(origin, *max_depth, LineageDirection::Forward)
            }
            QueryV1::Join {
                left,
                right,
                on,
                kind,
                watermark,
            } => self.plan_join(left, right, on, *kind, *watermark),
            QueryV1::Aggregate {
                inner,
                group_by,
                agg_fn,
            } => self.plan_aggregate(inner, group_by, agg_fn),
            QueryV1::Project { inner, .. } => {
                let input = self.plan(inner)?;
                self.plan_not_yet_implemented("Project (Phase A.2)", Some(Box::new(input.root)))
            }
            QueryV1::OrderBy { inner, .. } => {
                let input = self.plan(inner)?;
                self.plan_not_yet_implemented("OrderBy (Phase A.2)", Some(Box::new(input.root)))
            }
            QueryV1::Window { inner, spec } => self.plan_window(inner, spec),
        }
    }

    /// Plan a tumbling-window operator. Phase E-5 ships
    /// `WindowSpec::TumblingSeq { size }` with `size >= 1`.
    fn plan_window(
        &self,
        inner: &MeshQuery,
        spec: &super::query::WindowSpec,
    ) -> Result<OperatorNode, MeshError> {
        match spec {
            super::query::WindowSpec::TumblingSeq { size } if *size == 0 => {
                return Err(MeshError::PlannerError {
                    detail: "Window size must be >= 1".to_string(),
                });
            }
            _ => {}
        }
        let inner_plan = self.plan(inner)?;
        let cost = CostEstimate {
            bandwidth_bytes: inner_plan.total_cost.bandwidth_bytes,
            latency_ms: inner_plan.total_cost.latency_ms,
        };
        Ok(OperatorNode {
            operator: OperatorPlan::Window {
                input: Box::new(inner_plan.root),
                spec: spec.clone(),
            },
            target_nodes: vec![],
            cost,
        })
    }

    /// Plan an atomic `At(origin, seq)` read. Resolves the
    /// origin, then picks holders whose advertised
    /// `causal:` coverage includes `seq` — either an
    /// inclusive range covering `seq`, or a tip-form holder
    /// whose tip is `>= seq` (tip-form implies the holder
    /// has the full prefix up to that tip), or a presence-
    /// form holder (which makes no range claim and is taken
    /// as a permissive fallback). Targets are ordered by
    /// proximity (RTT-asc; ties broken lex-NodeId).
    ///
    /// Returns `HistoricalRangeUnavailable` when no holder's
    /// advertised coverage includes `seq`, with hints
    /// extracted from every advertised range / tip.
    fn plan_at(&self, origin: &ChainRef, seq: SeqNum) -> Result<OperatorNode, MeshError> {
        let origin_hash = self.resolve_origin(origin)?;
        let coverage = self.collect_coverage(origin_hash);
        let targets = self.select_targets_at(&coverage, seq);
        if targets.is_empty() && !coverage.is_empty() {
            return Err(MeshError::HistoricalRangeUnavailable {
                origin: origin_hash,
                requested: seq..SeqNum(seq.0.saturating_add(1)),
                available: coverage
                    .into_iter()
                    .filter_map(|c| c.claim.advertised())
                    .collect(),
            });
        }
        let cost = self.atomic_cost(&targets);
        Ok(OperatorNode {
            operator: OperatorPlan::AtRead {
                origin: origin_hash,
                seq,
            },
            target_nodes: targets,
            cost,
        })
    }

    /// Plan an atomic `Between(origin, start, end)` read.
    /// Range gate: `start < end` (otherwise typed
    /// `PlannerError`). Holder selection requires coverage
    /// of the full requested range:
    ///
    /// - A tip-form holder (`causal:<hex>:<tip_seq>`) covers
    ///   `[0, tip_seq + 1)`.
    /// - A range-form holder (`causal:<hex>[s..e]`) covers
    ///   `[s, e)` exactly.
    /// - A presence-form holder (`causal:<hex>` bare) is
    ///   admitted permissively — it makes no range claim
    ///   and is treated as best-effort.
    ///
    /// Surfaces `HistoricalRangeUnavailable` when no holder
    /// covers the full requested range, with available-range
    /// hints from every covering / partial holder.
    fn plan_between(
        &self,
        origin: &ChainRef,
        start: SeqNum,
        end: SeqNum,
    ) -> Result<OperatorNode, MeshError> {
        if start >= end {
            return Err(MeshError::PlannerError {
                detail: format!("Between requires start < end; got {start:?} >= {end:?}"),
            });
        }
        let origin_hash = self.resolve_origin(origin)?;
        let coverage = self.collect_coverage(origin_hash);
        let targets = self.select_targets_between(&coverage, start, end);
        if targets.is_empty() && !coverage.is_empty() {
            return Err(MeshError::HistoricalRangeUnavailable {
                origin: origin_hash,
                requested: start..end,
                available: coverage
                    .into_iter()
                    .filter_map(|c| c.claim.advertised())
                    .collect(),
            });
        }
        let cost = self.atomic_cost(&targets);
        Ok(OperatorNode {
            operator: OperatorPlan::BetweenRead {
                origin: origin_hash,
                start,
                end,
            },
            target_nodes: targets,
            cost,
        })
    }

    /// Plan an atomic `Latest(origin)` read. Any holder with
    /// the chain in its capability index qualifies; tip-form
    /// holders are preferred (their tip is the candidate
    /// latest), then range-form (highest end-of-range), then
    /// presence-form (no claim — best-effort fallback).
    fn plan_latest(&self, origin: &ChainRef) -> Result<OperatorNode, MeshError> {
        let origin_hash = self.resolve_origin(origin)?;
        let coverage = self.collect_coverage(origin_hash);
        let targets = self.select_targets_latest(&coverage);
        let cost = self.atomic_cost(&targets);
        Ok(OperatorNode {
            operator: OperatorPlan::LatestRead {
                origin: origin_hash,
            },
            target_nodes: targets,
            cost,
        })
    }

    /// Plan a `LineageBack` / `LineageForward` walk against
    /// the local capability-index snapshot.
    ///
    /// Errors per `MeshError`:
    /// - `LineageCycleDetected` if the walk revisits a chain
    ///   (the `fork-of:` graph should be a DAG; cycles indicate
    ///   broken upstream applications).
    /// - `LineageMaxDepthExceeded` if the walk hits `max_depth`
    ///   with more candidates still queued.
    fn plan_lineage(
        &self,
        origin: &ChainRef,
        max_depth: u32,
        direction: LineageDirection,
    ) -> Result<OperatorNode, MeshError> {
        let origin_hash = self.resolve_origin(origin)?;
        let entries = match direction {
            LineageDirection::Back => self.walk_lineage_back(origin_hash, max_depth)?,
            LineageDirection::Forward => self.walk_lineage_forward(origin_hash, max_depth)?,
        };
        // Lineage cost is a function of how many chains we
        // touch; conservative bandwidth estimate is one
        // ResultRow per entry (small, no payload), zero RTT
        // since the walk already happened at plan time.
        let cost = CostEstimate {
            bandwidth_bytes: entries.len() as u64 * 64,
            latency_ms: 0,
        };
        Ok(OperatorNode {
            operator: OperatorPlan::LineageEmit {
                origin: origin_hash,
                direction,
                entries,
            },
            target_nodes: vec![],
            cost,
        })
    }

    /// Plan a Phase D-1 hash join. Recurses on both sides,
    /// then derives the [`JoinKeyMode`] from the [`JoinKey`].
    /// Surfaces `PlannerError` when the key references
    /// payload fields (deferred to Phase E) or the sides
    /// disagree on which intrinsic field to key on.
    fn plan_join(
        &self,
        left: &MeshQuery,
        right: &MeshQuery,
        on: &JoinKey,
        kind: JoinKind,
        watermark: Duration,
    ) -> Result<OperatorNode, MeshError> {
        let key_mode = key_mode_for_join(on)?;
        let left_plan = self.plan(left)?;
        let right_plan = self.plan(right)?;
        let cost = CostEstimate {
            bandwidth_bytes: left_plan
                .total_cost
                .bandwidth_bytes
                .saturating_add(right_plan.total_cost.bandwidth_bytes),
            latency_ms: left_plan
                .total_cost
                .latency_ms
                .max(right_plan.total_cost.latency_ms),
        };
        // Phase D-2 strategy pick: hash-broadcast by default
        // (matches the original Phase D-1 behaviour). A future
        // refinement consults cardinality estimates from the
        // capability index's `aggregate` primitive — see the
        // plan's Cost Model section.
        let strategy = JoinStrategy::HashBroadcast;
        Ok(OperatorNode {
            operator: OperatorPlan::HashJoin {
                left: Box::new(left_plan.root),
                right: Box::new(right_plan.root),
                key_mode,
                kind,
                strategy,
                watermark,
            },
            target_nodes: vec![],
            cost,
        })
    }

    /// Plan an aggregate. Phase E-1 shipped `Count`; Phase E-3
    /// adds `Sum` and `Avg`. Other aggregate functions (sketches
    /// in Phase E-4) still surface `PlannerError`.
    fn plan_aggregate(
        &self,
        inner: &MeshQuery,
        group_by: &[Expr],
        agg_fn: &AggregateFn,
    ) -> Result<OperatorNode, MeshError> {
        let key_mode = group_by_mode(group_by)?;
        let inner_plan = self.plan(inner)?;
        let cost = CostEstimate {
            bandwidth_bytes: inner_plan.total_cost.bandwidth_bytes,
            latency_ms: inner_plan.total_cost.latency_ms,
        };
        let operator = match agg_fn {
            AggregateFn::Count => OperatorPlan::AggregateCount {
                input: Box::new(inner_plan.root),
                group_by: key_mode,
            },
            AggregateFn::Sum { field } => {
                let path = field_path_required(field, "Sum")?;
                OperatorPlan::AggregateNumeric {
                    input: Box::new(inner_plan.root),
                    group_by: key_mode,
                    field_path: path,
                    kind: super::query::NumericAggregateKind::Sum,
                }
            }
            AggregateFn::Avg { field } => {
                let path = field_path_required(field, "Avg")?;
                OperatorPlan::AggregateNumeric {
                    input: Box::new(inner_plan.root),
                    group_by: key_mode,
                    field_path: path,
                    kind: super::query::NumericAggregateKind::Avg,
                }
            }
            AggregateFn::Min { field } => {
                let path = field_path_required(field, "Min")?;
                OperatorPlan::AggregateReduction {
                    input: Box::new(inner_plan.root),
                    group_by: key_mode,
                    field_path: path,
                    kind: super::query::NumericReductionKind::Min,
                }
            }
            AggregateFn::Max { field } => {
                let path = field_path_required(field, "Max")?;
                OperatorPlan::AggregateReduction {
                    input: Box::new(inner_plan.root),
                    group_by: key_mode,
                    field_path: path,
                    kind: super::query::NumericReductionKind::Max,
                }
            }
            AggregateFn::PercentileExact { field, p } => {
                if !p.is_finite() || !(0.0..=1.0).contains(p) {
                    return Err(MeshError::PlannerError {
                        detail: format!("PercentileExact p must be in [0.0, 1.0], got {p}"),
                    });
                }
                let path = field_path_required(field, "PercentileExact")?;
                OperatorPlan::AggregateReduction {
                    input: Box::new(inner_plan.root),
                    group_by: key_mode,
                    field_path: path,
                    kind: super::query::NumericReductionKind::Percentile { p: *p },
                }
            }
            AggregateFn::DistinctCountExact { field } => {
                let path = field_path_required(field, "DistinctCountExact")?;
                OperatorPlan::AggregateDistinct {
                    input: Box::new(inner_plan.root),
                    group_by: key_mode,
                    field_path: path,
                }
            }
            AggregateFn::DistinctCountHll { .. } | AggregateFn::PercentileTDigest { .. } => {
                return Err(MeshError::PlannerError {
                    detail: format!(
                        "aggregate function {agg_fn:?} requires a sketch implementation (HLL p=14 / T-Digest c=100 per locked decision #3); deferred to Phase F. Use DistinctCountExact / PercentileExact for now."
                    ),
                });
            }
        };
        Ok(OperatorNode {
            operator,
            target_nodes: vec![],
            cost,
        })
    }

    /// Walk `fork-of:` parents backward from `start`. Returns
    /// entries in walk order (start first).
    fn walk_lineage_back(
        &self,
        start: u64,
        max_depth: u32,
    ) -> Result<Vec<LineageEntry>, MeshError> {
        let mut visited: std::collections::HashSet<u64> = std::collections::HashSet::new();
        visited.insert(start);
        let mut entries = vec![LineageEntry {
            origin: start,
            depth: 0,
            tip_seq: self.best_tip(start),
        }];
        let mut current = start;
        for depth in 1..=max_depth {
            let Some(parent) = self.parent_of(current) else {
                return Ok(entries);
            };
            if !visited.insert(parent) {
                // Cycle: parent already on the walk. Compute the
                // path from the first occurrence so the error
                // carries the cycle for debugging.
                let mut cycle: Vec<u64> = entries
                    .iter()
                    .map(|e| e.origin)
                    .skip_while(|o| *o != parent)
                    .collect();
                cycle.push(parent);
                return Err(MeshError::LineageCycleDetected {
                    origin: start,
                    cycle,
                });
            }
            entries.push(LineageEntry {
                origin: parent,
                depth,
                tip_seq: self.best_tip(parent),
            });
            current = parent;
        }
        // Reached max_depth: if a further parent still exists,
        // surface the bound. If the walk genuinely terminates
        // exactly at the boundary, no error.
        //
        // `max_depth == 0` is "just-the-origin" — the caller
        // explicitly didn't ask for a walk, so a present parent
        // is not a bound violation. Treat that case as a
        // successful single-entry result.
        if max_depth > 0 && self.parent_of(current).is_some() {
            return Err(MeshError::LineageMaxDepthExceeded {
                origin: start,
                depth: max_depth,
            });
        }
        Ok(entries)
    }

    /// Walk `fork-of:` descendants forward from `start`. BFS
    /// with descendants sorted lex by chain hash so the result
    /// is deterministic.
    fn walk_lineage_forward(
        &self,
        start: u64,
        max_depth: u32,
    ) -> Result<Vec<LineageEntry>, MeshError> {
        use std::collections::VecDeque;
        let mut visited: std::collections::HashSet<u64> = std::collections::HashSet::new();
        visited.insert(start);
        let mut entries = vec![LineageEntry {
            origin: start,
            depth: 0,
            tip_seq: self.best_tip(start),
        }];
        let mut frontier: VecDeque<(u64, u32)> = VecDeque::new();
        frontier.push_back((start, 0));
        while let Some((current, depth)) = frontier.pop_front() {
            // Compute children once per dequeue — previous
            // implementation did it twice (depth check + walk).
            let mut children = self.children_of(current);
            if depth >= max_depth {
                // `max_depth == 0` is "just-the-origin" — the
                // caller explicitly didn't ask for a walk, so
                // present children don't trip the bound. The
                // start node is the only thing on the frontier
                // at depth=0 in that case.
                if max_depth > 0 && !children.is_empty() {
                    return Err(MeshError::LineageMaxDepthExceeded {
                        origin: start,
                        depth: max_depth,
                    });
                }
                continue;
            }
            children.sort_unstable();
            for child in children {
                if !visited.insert(child) {
                    // In a DAG, no cycles. Defensive: a
                    // multi-parent diamond shows up here as a
                    // revisit, which is benign (we just don't
                    // re-add). Treat this case as silently
                    // pruned, not a cycle.
                    continue;
                }
                entries.push(LineageEntry {
                    origin: child,
                    depth: depth + 1,
                    tip_seq: self.best_tip(child),
                });
                frontier.push_back((child, depth + 1));
            }
        }
        Ok(entries)
    }

    /// Find the parent origin for `child` in the capability
    /// index. Scans every indexed node for the one that
    /// advertises `child` via `causal:<hex>` and reads its
    /// `fork-of:<parent_hex>` tag.
    ///
    /// Returns `None` when no node hosts `child` or no hosting
    /// node carries a fork-of declaration.
    ///
    /// Multi-chain hosts — a node with several `fork-of:` tags
    /// — are a Phase C ambiguity. Same for multi-host
    /// replication — two different nodes might both advertise
    /// `causal:<child>` with different `fork-of:` tags.
    /// `CapabilityIndex::all_nodes` iterates a `DashMap` whose
    /// order is unstable across runs, AND `caps.tags` is a
    /// `HashSet` with the same property. We collect every
    /// `(parent_hash, node_id)` candidate across ALL hosting
    /// nodes, sort it, and pick the smallest. That keeps both
    /// the plan AND the load-bearing cache key (locked
    /// decision #4) byte-stable across runs. Mirrors the
    /// symmetric approach `children_of` already uses.
    fn parent_of(&self, child: u64) -> Option<u64> {
        let mut candidates: Vec<(u64, u64)> = Vec::new();
        for node_id in self.capability_index.all_nodes() {
            let Some(caps) = self.capability_index.get(node_id) else {
                continue;
            };
            let mut hosts_child = false;
            let mut fork_candidates: Vec<u64> = Vec::new();
            for tag in &caps.tags {
                let Tag::Reserved { prefix, body } = tag else {
                    continue;
                };
                match prefix.as_str() {
                    "causal:" if parse_causal_body(body) == Some(child) => {
                        hosts_child = true;
                    }
                    "fork-of:" => {
                        if let Some(p) = parse_fork_body(body) {
                            fork_candidates.push(p);
                        }
                    }
                    _ => {}
                }
            }
            if hosts_child {
                for parent in fork_candidates {
                    candidates.push((parent, node_id));
                }
            }
        }
        candidates.sort_unstable();
        candidates.first().map(|(parent, _)| *parent)
    }

    /// Find all chains advertising `fork-of:<parent>` — i.e.,
    /// the direct descendants. Scans every node in the
    /// capability index; the result is sorted by caller (BFS
    /// needs deterministic order).
    ///
    /// Multi-chain hosts — a node with several `causal:` tags
    /// — are a Phase C ambiguity. `caps.tags` is a `HashSet`,
    /// so we collect every causal body, sort numerically, and
    /// pick the smallest to keep the plan + cache key
    /// deterministic across runs.
    fn children_of(&self, parent: u64) -> Vec<u64> {
        let mut out = Vec::new();
        for node_id in self.capability_index.all_nodes() {
            let Some(caps) = self.capability_index.get(node_id) else {
                continue;
            };
            let mut has_fork_to_parent = false;
            let mut causal_candidates: Vec<u64> = Vec::new();
            for tag in &caps.tags {
                let Tag::Reserved { prefix, body } = tag else {
                    continue;
                };
                match prefix.as_str() {
                    "fork-of:" if parse_fork_body(body) == Some(parent) => {
                        has_fork_to_parent = true;
                    }
                    "causal:" => {
                        if let Some(origin) = parse_causal_body(body) {
                            causal_candidates.push(origin);
                        }
                    }
                    _ => {}
                }
            }
            if has_fork_to_parent {
                causal_candidates.sort_unstable();
                if let Some(&chain) = causal_candidates.first() {
                    if chain != parent {
                        out.push(chain);
                    }
                }
            }
        }
        out.sort_unstable();
        out.dedup();
        out
    }

    /// Best-known tip seq for `chain` across its holders. Picks
    /// the highest [`CausalClaim::latest_tip`] across all
    /// holders advertising the chain; `None` if all claims are
    /// presence-only (no tip information).
    fn best_tip(&self, chain: u64) -> Option<SeqNum> {
        self.collect_coverage(chain)
            .into_iter()
            .filter_map(|c| c.claim.latest_tip())
            .max()
    }

    /// Resolve a `ChainRef::OriginHash` or `ChainRef::Discovered`
    /// to a concrete `u64` origin hash.
    ///
    /// For `Discovered`: rebuilds the typed `Predicate` from
    /// the stored `PredicateWire`, calls
    /// [`CapabilityQuery::filter`] on the capability index,
    /// then walks each matched node's `causal:<hex>` tags to
    /// extract origin hashes.
    ///
    /// Errors:
    /// - `PlannerError` if the stored `PredicateWire` fails
    ///   to rebuild as a `Predicate`.
    /// - `NoCapableHolder` if the predicate matches zero
    ///   nodes OR every matched node's caps carry no
    ///   `causal:` tag.
    /// - `AmbiguousDiscovery` if the predicate resolves to
    ///   more than one origin. Until Phase B's fan-out
    ///   lands the planner refuses rather than silently
    ///   truncating to the first match — the caller should
    ///   either tighten the predicate or wait for the
    ///   implicit-Union resolution path.
    #[expect(
        clippy::expect_used,
        reason = "match arm `1` guarantees the iterator yields exactly one element"
    )]
    fn resolve_origin(&self, origin: &ChainRef) -> Result<u64, MeshError> {
        match origin {
            ChainRef::OriginHash(h) => Ok(*h),
            ChainRef::Discovered(wire) => {
                let predicate =
                    wire.clone()
                        .into_predicate()
                        .map_err(|e| MeshError::PlannerError {
                            detail: format!("Discovered predicate rebuild failed: {e:?}"),
                        })?;
                let candidates = self.capability_index.filter(&predicate);
                // Walk every matched node's caps + extract
                // origins from `causal:<hex>*` tags. Dedupe +
                // sort lex for determinism (BTreeSet is
                // already iterated in sorted order).
                let mut origins: std::collections::BTreeSet<u64> =
                    std::collections::BTreeSet::new();
                for (_node_id, caps) in &candidates {
                    for tag in &caps.tags {
                        if let Some(hash) = parse_causal_origin(tag) {
                            origins.insert(hash);
                        }
                    }
                }
                match origins.len() {
                    0 => Err(MeshError::NoCapableHolder {
                        origin: 0,
                        requirement: format!("{:?}", predicate),
                    }),
                    1 => Ok(*origins.iter().next().expect("len == 1")),
                    _ => Err(MeshError::AmbiguousDiscovery {
                        matches: origins.into_iter().collect(),
                        requirement: format!("{:?}", predicate),
                    }),
                }
            }
        }
    }

    /// Walk the capability index for every node advertising
    /// `causal:<hex>*` for `origin_hash`. Each match emits one
    /// [`HolderCoverage`] carrying the node_id, its RTT (if
    /// the proximity graph has measured one), and the
    /// advertised range / tip / presence form. Used by the
    /// atomic operators' target-selection paths to narrow
    /// to coverage-satisfying holders.
    ///
    /// Result is sorted in the canonical priority order
    /// (RTT-asc, lex-NodeId tiebreak) so target selection is
    /// deterministic across runs (load-bearing for the
    /// locked-decision-#4 cache key).
    fn collect_coverage(&self, origin_hash: u64) -> Vec<HolderCoverage> {
        let hex = chain_hex(origin_hash);
        let mut out: Vec<HolderCoverage> = Vec::new();
        for node_id in self.capability_index.all_nodes() {
            // Each node may advertise multiple `causal:`
            // variants for the same chain (presence + tip +
            // range during transitions). Pick the most
            // specific one — range > tip > presence — so
            // the planner gets the tightest claim. Tie-break
            // on the claim itself so two Range or two Tip
            // claims on the same node resolve deterministically
            // (caps.tags is a HashSet whose iteration order
            // is RNG-dependent; the cache key per locked
            // decision #4 needs byte-stable plans).
            let claim = self
                .capability_index
                .with_caps(node_id, |caps| {
                    caps.tags
                        .iter()
                        .filter_map(|t| parse_causal_claim(t, &hex))
                        .max_by(|a, b| {
                            specificity_rank(a)
                                .cmp(&specificity_rank(b))
                                .then_with(|| claim_cmp_key(a).cmp(&claim_cmp_key(b)))
                        })
                })
                .unwrap_or(None);
            if let Some(claim) = claim {
                out.push(HolderCoverage {
                    node_id,
                    rtt: (self.rtt_lookup)(node_id),
                    claim,
                });
            }
        }
        sort_by_proximity(&mut out);
        out
    }

    /// Select target node_ids for an `At(seq)` query. Walks
    /// the pre-sorted (proximity-first, lex tiebreak)
    /// coverage list and keeps holders whose claim covers
    /// `seq`. Result preserves the priority order.
    fn select_targets_at(&self, coverage: &[HolderCoverage], seq: SeqNum) -> Vec<u64> {
        coverage
            .iter()
            .filter(|c| c.claim.covers_seq(seq))
            .map(|c| c.node_id)
            .collect()
    }

    /// Select target node_ids for a `Between(start, end)`
    /// query. Walks the pre-sorted coverage list and keeps
    /// holders whose claim covers the full `[start, end)`
    /// requested range.
    fn select_targets_between(
        &self,
        coverage: &[HolderCoverage],
        start: SeqNum,
        end: SeqNum,
    ) -> Vec<u64> {
        coverage
            .iter()
            .filter(|c| c.claim.covers_range(start, end))
            .map(|c| c.node_id)
            .collect()
    }

    /// Select target node_ids for a `Latest` query. Any
    /// holder with the chain qualifies — there's no
    /// coverage requirement since "latest" is whatever the
    /// holder has on top. Order: holders advertising the
    /// **highest** known tip first (most-current data); then
    /// remaining holders in proximity order. Within
    /// equal-tip holders, proximity-asc with lex-NodeId
    /// tiebreak (inherited from `coverage`'s pre-sort).
    fn select_targets_latest(&self, coverage: &[HolderCoverage]) -> Vec<u64> {
        let mut with_tip: Vec<&HolderCoverage> = coverage
            .iter()
            .filter(|c| c.claim.latest_tip().is_some())
            .collect();
        // Stable sort so the proximity-sort within
        // equal-tip groups carries through. Descending tip
        // = larger first.
        with_tip.sort_by_key(|c| std::cmp::Reverse(c.claim.latest_tip()));
        let mut out: Vec<u64> = with_tip.iter().map(|c| c.node_id).collect();
        // Append presence-only holders (no tip claim) in the
        // pre-sorted order — they're the best-effort fallback.
        for c in coverage {
            if c.claim.latest_tip().is_none() {
                out.push(c.node_id);
            }
        }
        out
    }

    /// Cost-estimate stub for atomic operators.
    /// Bandwidth: heuristic constant per target node.
    /// Latency: proximity RTT to the nearest target, or
    /// `0` if no RTT data exists for any target.
    fn atomic_cost(&self, targets: &[u64]) -> CostEstimate {
        let bandwidth_bytes = (targets.len() as u64) * PHASE_A_ATOMIC_BANDWIDTH_BYTES;
        let latency_ms = targets
            .iter()
            .filter_map(|nid| (self.rtt_lookup)(*nid))
            .map(|d| d.as_millis() as u64)
            .min()
            .unwrap_or(0);
        CostEstimate {
            bandwidth_bytes,
            latency_ms,
        }
    }

    /// Helper for composite operators whose executor hasn't
    /// landed yet. Wraps the planned inner sub-plan (if any)
    /// in a `NotYetImplemented` operator-plan node so the
    /// tree shape stays consistent.
    fn plan_not_yet_implemented(
        &self,
        detail: &str,
        input: Option<Box<OperatorNode>>,
    ) -> Result<OperatorNode, MeshError> {
        Ok(OperatorNode {
            operator: OperatorPlan::NotYetImplemented {
                detail: detail.to_string(),
                input,
            },
            target_nodes: vec![],
            cost: CostEstimate::default(),
        })
    }
}

/// Sum cost across a subtree. Walks the operator-plan
/// recursively. Used by `plan()` to populate
/// `ExecutionPlan.total_cost`.
fn sum_cost(node: &OperatorNode) -> CostEstimate {
    let mut acc = node.cost;
    match &node.operator {
        OperatorPlan::Filter { input, .. } => {
            let inner = sum_cost(input);
            acc.bandwidth_bytes = acc.bandwidth_bytes.saturating_add(inner.bandwidth_bytes);
            acc.latency_ms = acc.latency_ms.saturating_add(inner.latency_ms);
        }
        OperatorPlan::HashJoin { left, right, .. } => {
            let l = sum_cost(left);
            let r = sum_cost(right);
            // Bandwidth sums (both sides fully materialize);
            // latency is the slower of the two (both fetched
            // concurrently at execute time).
            acc.bandwidth_bytes = acc
                .bandwidth_bytes
                .saturating_add(l.bandwidth_bytes)
                .saturating_add(r.bandwidth_bytes);
            acc.latency_ms = acc
                .latency_ms
                .saturating_add(l.latency_ms.max(r.latency_ms));
        }
        OperatorPlan::AggregateCount { input, .. } => {
            let inner = sum_cost(input);
            acc.bandwidth_bytes = acc.bandwidth_bytes.saturating_add(inner.bandwidth_bytes);
            acc.latency_ms = acc.latency_ms.saturating_add(inner.latency_ms);
        }
        OperatorPlan::AggregateNumeric { input, .. } => {
            let inner = sum_cost(input);
            acc.bandwidth_bytes = acc.bandwidth_bytes.saturating_add(inner.bandwidth_bytes);
            acc.latency_ms = acc.latency_ms.saturating_add(inner.latency_ms);
        }
        OperatorPlan::AggregateReduction { input, .. }
        | OperatorPlan::AggregateDistinct { input, .. }
        | OperatorPlan::Window { input, .. } => {
            let inner = sum_cost(input);
            acc.bandwidth_bytes = acc.bandwidth_bytes.saturating_add(inner.bandwidth_bytes);
            acc.latency_ms = acc.latency_ms.saturating_add(inner.latency_ms);
        }
        OperatorPlan::NotYetImplemented {
            input: Some(input), ..
        } => {
            let inner = sum_cost(input);
            acc.bandwidth_bytes = acc.bandwidth_bytes.saturating_add(inner.bandwidth_bytes);
            acc.latency_ms = acc.latency_ms.saturating_add(inner.latency_ms);
        }
        // Atomic + leaf operators (`LineageEmit` is leaf:
        // walk happens at plan time, no children to sum).
        OperatorPlan::AtRead { .. }
        | OperatorPlan::BetweenRead { .. }
        | OperatorPlan::LatestRead { .. }
        | OperatorPlan::LineageEmit { .. }
        | OperatorPlan::NotYetImplemented { input: None, .. } => {}
    }
    acc
}

/// One holder's seq-coverage claim for an origin. Built by
/// [`parse_causal_claim`] from a single `causal:<hex>*`
/// reserved tag; carried alongside the holder's node_id +
/// RTT inside [`HolderCoverage`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CausalClaim {
    /// Bare `causal:<hex>` — no range claim. Permissive
    /// fallback: the holder has the chain in some form,
    /// but doesn't advertise what range.
    Presence,
    /// `causal:<hex>:<tip_seq>` — the holder advertises a
    /// full prefix up to and including `tip_seq`. Covers
    /// `[0, tip_seq + 1)`.
    Tip { tip_seq: SeqNum },
    /// `causal:<hex>[start..end]` — the holder advertises
    /// exactly the half-open range `[start, end)`.
    Range { start: SeqNum, end: SeqNum },
}

impl CausalClaim {
    /// Does this claim cover the requested single `seq`?
    /// `Presence` is permissive (best-effort).
    fn covers_seq(&self, seq: SeqNum) -> bool {
        match self {
            Self::Presence => true,
            Self::Tip { tip_seq } => seq.0 <= tip_seq.0,
            Self::Range { start, end } => seq.0 >= start.0 && seq.0 < end.0,
        }
    }

    /// Does this claim cover the half-open requested range
    /// `[start, end)` in full? `Presence` is permissive.
    fn covers_range(&self, start: SeqNum, end: SeqNum) -> bool {
        match self {
            Self::Presence => true,
            Self::Tip { tip_seq } => end.0 <= tip_seq.0.saturating_add(1),
            Self::Range { start: s, end: e } => s.0 <= start.0 && end.0 <= e.0,
        }
    }

    /// Render the claim as an advertised half-open range
    /// (for `HistoricalRangeUnavailable.available` hints).
    /// `None` for `Presence` (no advertised range).
    fn advertised(&self) -> Option<std::ops::Range<SeqNum>> {
        match self {
            Self::Presence => None,
            Self::Tip { tip_seq } => Some(SeqNum(0)..SeqNum(tip_seq.0.saturating_add(1))),
            Self::Range { start, end } => Some(*start..*end),
        }
    }

    /// Highest seq the claim implies the holder has. `None`
    /// for `Presence` (no claim). Used by `Latest` target
    /// selection to prefer the most-current data.
    fn latest_tip(&self) -> Option<SeqNum> {
        match self {
            Self::Presence => None,
            Self::Tip { tip_seq } => Some(*tip_seq),
            Self::Range { end, .. } => Some(SeqNum(end.0.saturating_sub(1))),
        }
    }
}

/// One node's coverage record for a particular origin —
/// node_id, measured RTT (if any), and the parsed
/// `causal:` claim. Carried in the planner's coverage list.
#[derive(Clone, Debug)]
struct HolderCoverage {
    /// node_id of the holder.
    node_id: u64,
    /// Round-trip-time from the local node to this holder
    /// per the proximity graph. `None` when no
    /// measurement exists yet.
    rtt: Option<Duration>,
    /// What the holder advertises about its coverage.
    claim: CausalClaim,
}

/// Specificity rank for `max_by_key` selection within a
/// single holder's `causal:` tag set. Higher = tighter
/// coverage claim. `Range` > `Tip` > `Presence`.
fn specificity_rank(claim: &CausalClaim) -> u8 {
    match claim {
        CausalClaim::Range { .. } => 2,
        CausalClaim::Tip { .. } => 1,
        CausalClaim::Presence => 0,
    }
}

/// Total-order tie-break key for `CausalClaim`. Used to pick
/// a canonical winner when two claims share the same
/// `specificity_rank` — `caps.tags` is a `HashSet`, so the
/// "natural" iteration order is RNG-dependent. The key is
/// shaped so that within each rank the smaller starting seq
/// (and smaller end on a tie) wins; the variant tag is
/// included only as a final tiebreaker so the function is
/// total across all variants.
fn claim_cmp_key(claim: &CausalClaim) -> (u64, u64, u8) {
    match claim {
        CausalClaim::Range { start, end } => (start.0, end.0, 2),
        CausalClaim::Tip { tip_seq } => (0, tip_seq.0, 1),
        CausalClaim::Presence => (u64::MAX, u64::MAX, 0),
    }
}

/// Sort `coverage` in-place by canonical priority:
/// RTT-asc (closer first), unmeasured-RTT last, lex-NodeId
/// tiebreak. Stable so equal-priority holders stay in
/// node_id order across runs (load-bearing for the locked-
/// decision-#4 cache key).
fn sort_by_proximity(coverage: &mut [HolderCoverage]) {
    coverage.sort_by(|a, b| match (a.rtt, b.rtt) {
        (Some(ra), Some(rb)) => ra.cmp(&rb).then_with(|| a.node_id.cmp(&b.node_id)),
        (Some(_), None) => std::cmp::Ordering::Less,
        (None, Some(_)) => std::cmp::Ordering::Greater,
        (None, None) => a.node_id.cmp(&b.node_id),
    });
}

/// Lowercase hex digits keyed by nibble value `0..=15`. Per
/// meshdb perf #211 — same lookup-table pattern as the
/// dataforts #171 hex-decode fix, but applied to the encode
/// side of the planner's `causal:<hex>` tag stem.
const HEX_NIBBLES: &[u8; 16] = b"0123456789abcdef";

/// Lowercase 16-char hex of a `u64` origin hash. Mirrors
/// `MeshNode::chain_hex` (which is private); duplicated
/// here so the planner doesn't depend on the mesh layer.
///
/// Per perf #211 — pre-fix `format!("{origin_hash:016x}")`
/// routed through `core::fmt::Formatter`, which copies into a
/// stack buffer and walks the format-string machinery before
/// the `String` materializes. Post-fix is a direct 16-shift /
/// nibble-lookup unroll into a fresh 16-byte buffer that's
/// transmuted into `String` via `from_utf8(...).unwrap()` (the
/// lookup table only emits ASCII hex digits, so the UTF-8
/// validation is infallible by construction).
///
/// `collect_coverage` calls this exactly once per planning
/// call (line 1104) and threads the result through every per-
/// node tag scan; the speedup is sub-microsecond per planning
/// call, but the change keeps the hex-formatting pattern
/// consistent with dataforts #171 and removes a
/// `core::fmt`-shaped allocation from the planner hot path.
fn chain_hex(origin_hash: u64) -> String {
    let mut buf = [0u8; 16];
    let mut h = origin_hash;
    // Unrolled MSB-first nibble walk: byte 0 holds the most-
    // significant nibble of `origin_hash`, byte 15 the least.
    // Matches the `{:016x}` ordering byte-for-byte (pinned by
    // `chain_hex_matches_format_macro_byte_for_byte`).
    for i in (0..16).rev() {
        buf[i] = HEX_NIBBLES[(h & 0xF) as usize];
        h >>= 4;
    }
    // SAFETY: `HEX_NIBBLES` contains only ASCII hex digits, so
    // every byte written into `buf` is a valid UTF-8 byte and
    // `buf` is a valid UTF-8 string by construction. Skipping
    // the `from_utf8` validation walk is the whole point of the
    // lookup-table fix — the validation would re-scan 16 bytes
    // we already know are ASCII.
    unsafe { String::from_utf8_unchecked(buf.to_vec()) }
}

/// Parse a `causal:<hex>*` reserved tag, matching on the
/// supplied `origin_hex` stem. Returns `None` if the tag
/// isn't a `causal:` tag, the body's stem doesn't match,
/// or the variant suffix doesn't parse cleanly.
///
/// Recognized shapes (per `CAPABILITY_SYSTEM_PLAN.md` § 2):
///
/// - `causal:<hex>` → [`CausalClaim::Presence`]
/// - `causal:<hex>:<tip_seq>` → [`CausalClaim::Tip`]
/// - `causal:<hex>[<start>..<end>]` → [`CausalClaim::Range`]
fn parse_causal_claim(tag: &Tag, origin_hex: &str) -> Option<CausalClaim> {
    let Tag::Reserved { prefix, body } = tag else {
        return None;
    };
    if prefix != "causal:" {
        return None;
    }
    if !body.starts_with(origin_hex) {
        return None;
    }
    let rest = &body[origin_hex.len()..];
    if rest.is_empty() {
        return Some(CausalClaim::Presence);
    }
    if let Some(tip_str) = rest.strip_prefix(':') {
        // tip-form: parse the rest as decimal u64
        let tip: u64 = tip_str.parse().ok()?;
        return Some(CausalClaim::Tip {
            tip_seq: SeqNum(tip),
        });
    }
    if let Some(range_body) = rest.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
        // range-form: `<start>..<end>` half-open
        let (start_str, end_str) = range_body.split_once("..")?;
        let start: u64 = start_str.parse().ok()?;
        let end: u64 = end_str.parse().ok()?;
        if start >= end {
            // Substrate emits half-open with `start < end`; a
            // degenerate range is malformed.
            return None;
        }
        return Some(CausalClaim::Range {
            start: SeqNum(start),
            end: SeqNum(end),
        });
    }
    // Unknown suffix shape — reject rather than partial-match.
    None
}

/// Extract the `u64` origin hash from a `causal:<hex>*`
/// reserved tag. Returns `None` if the tag isn't a `causal:`
/// reserved tag, the body's stem isn't 16 hex chars, or any
/// nibble fails to parse.
///
/// Used by `ChainRef::Discovered` resolution to map every
/// matched node's caps to its set of advertised origin hashes.
fn parse_causal_origin(tag: &Tag) -> Option<u64> {
    let Tag::Reserved { prefix, body } = tag else {
        return None;
    };
    if prefix != "causal:" {
        return None;
    }
    parse_causal_body(body)
}

/// Parse a `causal:` body (everything after the `causal:`
/// prefix) into a `u64` origin hash. Strips the optional
/// `:<tip>` or `[start..end]` suffix before validating the
/// 16-hex-char stem.
fn parse_causal_body(body: &str) -> Option<u64> {
    let stem = body.split_once([':', '[']).map(|(s, _)| s).unwrap_or(body);
    if stem.len() != 16 {
        return None;
    }
    u64::from_str_radix(stem, 16).ok()
}

/// Parse a `fork-of:<16-hex>` reserved tag's body into a `u64`
/// parent origin hash. Returns `None` for any non-conforming
/// body (wrong length, non-hex). Mirrors [`parse_causal_origin`]'s
/// strictness so the lineage walk has the same shape contract as
/// causal-tag parsing.
fn parse_fork_body(body: &str) -> Option<u64> {
    if body.len() != 16 {
        return None;
    }
    u64::from_str_radix(body, 16).ok()
}

/// Derive the [`JoinKeyMode`] from a [`JoinKey`].
///
/// Both sides must agree on the same field name. Row-intrinsic
/// names (`"origin"`, `"seq"`, `"origin,seq"`) map to the
/// matching `JoinKeyMode` enum variant; anything else is
/// treated as a JSON payload path and maps to
/// `JoinKeyMode::Field(path)` (Phase D-2 row-schema decoding).
fn key_mode_for_join(on: &JoinKey) -> Result<JoinKeyMode, MeshError> {
    let left = field_name(&on.left_field).ok_or_else(|| MeshError::PlannerError {
        detail: format!(
            "join left key must be a field reference, got {:?}",
            on.left_field
        ),
    })?;
    let right = field_name(&on.right_field).ok_or_else(|| MeshError::PlannerError {
        detail: format!(
            "join right key must be a field reference, got {:?}",
            on.right_field
        ),
    })?;
    if left != right {
        return Err(MeshError::PlannerError {
            detail: format!(
                "join key sides must reference the same field name (left='{left}', right='{right}')"
            ),
        });
    }
    Ok(match left {
        "origin" => JoinKeyMode::Origin,
        "seq" => JoinKeyMode::Seq,
        "origin,seq" => JoinKeyMode::OriginSeq,
        other => JoinKeyMode::Field(other.to_string()),
    })
}

/// Borrow the inner `&str` of an [`Expr::Field`]. Returns
/// `None` for any other variant.
fn field_name(e: &Expr) -> Option<&str> {
    match e {
        Expr::Field(s) => Some(s.as_str()),
        _ => None,
    }
}

/// Pull the field path out of an `Expr::Field` (which numeric
/// aggregates require for their target field). Surfaces a
/// `PlannerError` for any other expression shape.
fn field_path_required(e: &Expr, op_name: &str) -> Result<String, MeshError> {
    match e {
        Expr::Field(s) => Ok(s.clone()),
        other => Err(MeshError::PlannerError {
            detail: format!(
                "{op_name} requires Expr::Field(<path>) for its field argument, got {other:?}"
            ),
        }),
    }
}

/// Resolve a `group_by: Vec<Expr>` clause to a Phase E-1
/// row-intrinsic [`JoinKeyMode`].
///
/// Returns `Ok(None)` for an empty `group_by` (single-bucket
/// aggregate). Surfaces `PlannerError` for any payload-keyed
/// or composite group_by we don't support yet.
fn group_by_mode(group_by: &[Expr]) -> Result<Option<JoinKeyMode>, MeshError> {
    if group_by.is_empty() {
        return Ok(None);
    }
    if group_by.len() == 1 {
        let name = field_name(&group_by[0]).ok_or_else(|| MeshError::PlannerError {
            detail: format!(
                "group_by[0] must be a field reference, got {:?}",
                group_by[0]
            ),
        })?;
        return match name {
            "origin" => Ok(Some(JoinKeyMode::Origin)),
            "seq" => Ok(Some(JoinKeyMode::Seq)),
            other => Err(MeshError::PlannerError {
                detail: format!(
                    "group_by field '{other}' is not a row-intrinsic key; only 'origin' / 'seq' supported in Phase E-1"
                ),
            }),
        };
    }
    if group_by.len() == 2 {
        let l = field_name(&group_by[0]);
        let r = field_name(&group_by[1]);
        if matches!(
            (l, r),
            (Some("origin"), Some("seq")) | (Some("seq"), Some("origin"))
        ) {
            return Ok(Some(JoinKeyMode::OriginSeq));
        }
    }
    Err(MeshError::PlannerError {
        detail: format!(
            "group_by shape {group_by:?} not supported in Phase E-1; only [origin], [seq], or [origin, seq] are row-intrinsic"
        ),
    })
}

// Silence unused-import warning under feature-conditional
// configurations of the planner. `TaxonomyAxis` is held for
// future reference by Phase B's discovery-time match_axis
// path; `CapabilityQuery` is the trait the index implements.
#[allow(dead_code)]
const _PLANNER_USES_TAXONOMY_AXIS: TaxonomyAxis = TaxonomyAxis::Dataforts;
#[allow(dead_code)]
fn _planner_uses_capability_query<Q: CapabilityQuery>(_q: &Q) {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapter::net::behavior::capability::{
        CapabilityAnnouncement, CapabilityIndex, CapabilitySet,
    };
    use crate::adapter::net::identity::EntityId;

    /// Pin meshdb perf #211: the nibble-lookup `chain_hex` must
    /// produce the SAME 16-char lowercase-hex string as the
    /// legacy `format!("{origin_hash:016x}")` for every input.
    /// `causal:<hex>` tag bodies are matched on by stem (see
    /// `parse_causal_claim`), so a single-byte divergence
    /// would silently break causal-claim discovery — every
    /// announced `causal:<hex>` tag would fail to match the
    /// planner's derived stem and the federated query layer
    /// would behave as if no holders existed for that chain.
    /// Exhaustive comparison across boundary, single-bit, and
    /// arbitrary u64 inputs.
    #[test]
    fn chain_hex_matches_format_macro_byte_for_byte() {
        let cases: &[u64] = &[
            0,
            1,
            0xF,
            0x10,
            0xFF,
            0x100,
            0xDEAD_BEEF,
            0x8000_0000_0000_0000,
            0x7FFF_FFFF_FFFF_FFFF,
            u64::MAX,
            u64::MAX - 1,
            // A bunch of arbitrary mid-range values to exercise
            // each of the 16 nibble positions.
            0x0123_4567_89AB_CDEF,
            0xFEDC_BA98_7654_3210,
            0xCAFE_BABE_DEAD_BEEF,
            0x1234_5678_9ABC_DEF0,
        ];
        for &h in cases {
            let reference = format!("{h:016x}");
            let actual = chain_hex(h);
            assert_eq!(
                actual, reference,
                "chain_hex({h:#x}) diverged from format!(\"{{:016x}}\")",
            );
            assert_eq!(actual.len(), 16, "chain_hex must always emit 16 chars");
        }
    }

    /// Build a single `causal:<body>` reserved tag from the
    /// supplied body string. The `add_tag` builder silently
    /// drops reserved-prefix tags (it routes through
    /// `Tag::parse_user`), so the tests build the tag directly
    /// via `Tag::Reserved` to mimic what
    /// `MeshNode::announce_chain` / `announce_chain_range`
    /// emits at runtime.
    fn causal_tag(body: impl Into<String>) -> Tag {
        Tag::Reserved {
            prefix: "causal:".to_string(),
            body: body.into(),
        }
    }

    /// Build a fresh capability set carrying a single
    /// `causal:<hex>` presence-form tag for `origin_hash`.
    fn caps_with_causal_presence(origin_hash: u64) -> CapabilitySet {
        let mut caps = CapabilitySet::new();
        caps.tags.insert(causal_tag(chain_hex(origin_hash)));
        caps
    }

    /// Build a fresh capability set carrying a single
    /// `causal:<hex>:<tip>` tip-form tag for `origin_hash`.
    fn caps_with_causal_tip(origin_hash: u64, tip: u64) -> CapabilitySet {
        let mut caps = CapabilitySet::new();
        caps.tags
            .insert(causal_tag(format!("{}:{}", chain_hex(origin_hash), tip)));
        caps
    }

    /// Build a fresh capability set carrying a single
    /// `causal:<hex>[start..end]` range-form tag.
    fn caps_with_causal_range(origin_hash: u64, start: u64, end: u64) -> CapabilitySet {
        let mut caps = CapabilitySet::new();
        caps.tags.insert(causal_tag(format!(
            "{}[{}..{}]",
            chain_hex(origin_hash),
            start,
            end
        )));
        caps
    }

    fn index_with(holders: Vec<(u64, CapabilitySet)>) -> CapabilityIndex {
        let index = CapabilityIndex::new();
        for (node_id, caps) in holders {
            index.index(CapabilityAnnouncement::new(
                node_id,
                EntityId::from_bytes([node_id as u8; 32]),
                1,
                caps,
            ));
        }
        index
    }

    fn make_index_with_holder(node_id: u64, origin_hash: u64) -> CapabilityIndex {
        index_with(vec![(node_id, caps_with_causal_presence(origin_hash))])
    }

    fn empty_index() -> CapabilityIndex {
        CapabilityIndex::new()
    }

    fn rtt_none(_nid: u64) -> Option<Duration> {
        None
    }

    // ========================================================================
    // CausalClaim parsing + coverage semantics
    // ========================================================================

    #[test]
    fn parse_causal_presence_form() {
        let origin = 0xDEAD_BEEF_CAFE_BABE_u64;
        let hex = chain_hex(origin);
        let claim = parse_causal_claim(&causal_tag(hex.clone()), &hex);
        assert_eq!(claim, Some(CausalClaim::Presence));
    }

    #[test]
    fn parse_causal_tip_form() {
        let origin = 0x1234_5678_9ABC_DEF0_u64;
        let hex = chain_hex(origin);
        let claim = parse_causal_claim(&causal_tag(format!("{hex}:1000")), &hex);
        assert_eq!(
            claim,
            Some(CausalClaim::Tip {
                tip_seq: SeqNum(1000)
            })
        );
    }

    #[test]
    fn parse_causal_range_form() {
        let origin = 0xAAAA_BBBB_CCCC_DDDD_u64;
        let hex = chain_hex(origin);
        let claim = parse_causal_claim(&causal_tag(format!("{hex}[100..500]")), &hex);
        assert_eq!(
            claim,
            Some(CausalClaim::Range {
                start: SeqNum(100),
                end: SeqNum(500),
            })
        );
    }

    #[test]
    fn parse_causal_rejects_inverted_range() {
        // Degenerate `[start..end]` with `start >= end` is
        // malformed per the substrate emitter's contract.
        let hex = chain_hex(1);
        let claim = parse_causal_claim(&causal_tag(format!("{hex}[500..100]")), &hex);
        assert_eq!(claim, None);
    }

    #[test]
    fn parse_causal_rejects_unknown_suffix() {
        let hex = chain_hex(1);
        let claim = parse_causal_claim(&causal_tag(format!("{hex}?weird")), &hex);
        assert_eq!(claim, None);
    }

    #[test]
    fn parse_causal_rejects_wrong_hash() {
        // `causal:<otherhex>:42` shouldn't match a query for
        // a different chain even if it parses.
        let other_hex = chain_hex(0xFFFF);
        let claim = parse_causal_claim(&causal_tag(format!("{other_hex}:42")), &chain_hex(0xAAAA));
        assert_eq!(claim, None);
    }

    #[test]
    fn causal_claim_covers_seq_semantics() {
        assert!(CausalClaim::Presence.covers_seq(SeqNum(0)));
        assert!(CausalClaim::Presence.covers_seq(SeqNum(u64::MAX)));

        let tip = CausalClaim::Tip {
            tip_seq: SeqNum(100),
        };
        assert!(tip.covers_seq(SeqNum(0)));
        assert!(tip.covers_seq(SeqNum(100)));
        assert!(!tip.covers_seq(SeqNum(101)));

        let range = CausalClaim::Range {
            start: SeqNum(50),
            end: SeqNum(150),
        };
        assert!(!range.covers_seq(SeqNum(49)));
        assert!(range.covers_seq(SeqNum(50)));
        assert!(range.covers_seq(SeqNum(149)));
        assert!(!range.covers_seq(SeqNum(150))); // half-open
    }

    #[test]
    fn causal_claim_covers_range_semantics() {
        assert!(CausalClaim::Presence.covers_range(SeqNum(0), SeqNum(1_000)));

        let tip = CausalClaim::Tip {
            tip_seq: SeqNum(100),
        };
        // Tip covers [0, 101); requested end must be <= 101.
        assert!(tip.covers_range(SeqNum(0), SeqNum(101)));
        assert!(tip.covers_range(SeqNum(50), SeqNum(101)));
        assert!(!tip.covers_range(SeqNum(0), SeqNum(102)));

        let range = CausalClaim::Range {
            start: SeqNum(100),
            end: SeqNum(200),
        };
        assert!(range.covers_range(SeqNum(100), SeqNum(200)));
        assert!(range.covers_range(SeqNum(150), SeqNum(175)));
        assert!(!range.covers_range(SeqNum(50), SeqNum(150))); // starts below
        assert!(!range.covers_range(SeqNum(150), SeqNum(250))); // ends above
    }

    #[test]
    fn causal_claim_advertised_renders_half_open_range() {
        assert_eq!(CausalClaim::Presence.advertised(), None);
        assert_eq!(
            (CausalClaim::Tip {
                tip_seq: SeqNum(99)
            })
            .advertised(),
            Some(SeqNum(0)..SeqNum(100))
        );
        assert_eq!(
            (CausalClaim::Range {
                start: SeqNum(10),
                end: SeqNum(50),
            })
            .advertised(),
            Some(SeqNum(10)..SeqNum(50))
        );
    }

    #[test]
    fn causal_claim_latest_tip_ordering() {
        assert_eq!(CausalClaim::Presence.latest_tip(), None);
        assert_eq!(
            (CausalClaim::Tip {
                tip_seq: SeqNum(42)
            })
            .latest_tip(),
            Some(SeqNum(42))
        );
        // Range advertises `[start, end)` — latest is end-1.
        assert_eq!(
            (CausalClaim::Range {
                start: SeqNum(10),
                end: SeqNum(50),
            })
            .latest_tip(),
            Some(SeqNum(49))
        );
    }

    #[test]
    fn parse_causal_origin_extracts_u64_from_each_form() {
        let origin = 0xCAFE_BABE_DEAD_BEEF_u64;
        let hex = chain_hex(origin);

        // presence form
        assert_eq!(parse_causal_origin(&causal_tag(hex.clone())), Some(origin));
        // tip form
        assert_eq!(
            parse_causal_origin(&causal_tag(format!("{hex}:42"))),
            Some(origin)
        );
        // range form
        assert_eq!(
            parse_causal_origin(&causal_tag(format!("{hex}[0..100]"))),
            Some(origin)
        );
        // non-causal tag
        assert_eq!(
            parse_causal_origin(&Tag::Reserved {
                prefix: "heat:".to_string(),
                body: hex.clone(),
            }),
            None
        );
        // wrong-length stem
        assert_eq!(parse_causal_origin(&causal_tag("abc".to_string())), None);
    }

    // ========================================================================
    // Atomic-operator planning (At / Between / Latest)
    // ========================================================================

    #[test]
    fn plan_latest_returns_atomic_with_holder() {
        let origin = 0xABAB_ABAB_ABAB_ABAB_u64;
        let index = make_index_with_holder(42, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            }))
            .expect("plan ok");
        match plan.root.operator {
            OperatorPlan::LatestRead { origin: o } => assert_eq!(o, origin),
            other => panic!("expected LatestRead; got {other:?}"),
        }
        assert_eq!(plan.root.target_nodes, vec![42]);
    }

    #[test]
    fn plan_latest_with_no_holders_returns_empty_targets() {
        // When no holder advertises the chain at all, the
        // planner emits an empty target list rather than
        // failing — the executor surfaces
        // `HistoricalRangeUnavailable` against that empty
        // set. (Phase A semantics; preserved in Phase B.)
        let index = empty_index();
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(0),
            }))
            .expect("plan ok");
        assert!(plan.root.target_nodes.is_empty());
    }

    #[test]
    fn plan_between_rejects_inverted_range() {
        let index = empty_index();
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::Between {
                origin: ChainRef::OriginHash(0),
                start: SeqNum(100),
                end: SeqNum(50),
            }))
            .expect_err("inverted range must fail");
        match err {
            MeshError::PlannerError { detail } => assert!(detail.contains("start < end")),
            other => panic!("expected PlannerError; got {other:?}"),
        }
    }

    #[test]
    fn plan_between_accepts_valid_range_with_covering_holder() {
        // Holder advertises tip 1000 → covers [0, 1001). The
        // requested [0, 1000) fits.
        let origin = 0x4242_4242_4242_4242_u64;
        let index = index_with(vec![(7, caps_with_causal_tip(origin, 1000))]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Between {
                origin: ChainRef::OriginHash(origin),
                start: SeqNum(0),
                end: SeqNum(1000),
            }))
            .expect("plan ok");
        match plan.root.operator {
            OperatorPlan::BetweenRead { start, end, .. } => {
                assert_eq!(start, SeqNum(0));
                assert_eq!(end, SeqNum(1000));
            }
            other => panic!("expected BetweenRead; got {other:?}"),
        }
        assert_eq!(plan.root.target_nodes, vec![7]);
    }

    #[test]
    fn plan_at_routes_to_holder() {
        let origin = 0xCCCC_CCCC_CCCC_CCCC_u64;
        let index = make_index_with_holder(99, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::At {
                origin: ChainRef::OriginHash(origin),
                seq: SeqNum(7),
            }))
            .expect("plan ok");
        match plan.root.operator {
            OperatorPlan::AtRead { origin: o, seq } => {
                assert_eq!(o, origin);
                assert_eq!(seq, SeqNum(7));
            }
            other => panic!("expected AtRead; got {other:?}"),
        }
        assert_eq!(plan.root.target_nodes, vec![99]);
    }

    #[test]
    fn plan_holders_lex_sorted_when_no_rtt() {
        // No RTT data → fall back to lex-NodeId order for
        // determinism. Three holders inserted in non-monotonic
        // order; planner sort restores lex.
        let origin = 0xEEEE_EEEE_EEEE_EEEE_u64;
        let caps = caps_with_causal_presence(origin);
        let index = index_with(vec![(200, caps.clone()), (50, caps.clone()), (100, caps)]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            }))
            .unwrap();
        assert_eq!(plan.root.target_nodes, vec![50, 100, 200]);
    }

    // ========================================================================
    // Phase B — replica-aware routing
    // ========================================================================

    #[test]
    fn at_picks_holder_whose_tip_covers_seq() {
        // Two holders: one with tip 50, one with tip 200.
        // Query `At(100)` — only the tip-200 holder covers.
        let origin = 0x1111_2222_3333_4444_u64;
        let index = index_with(vec![
            (50, caps_with_causal_tip(origin, 50)),
            (200, caps_with_causal_tip(origin, 200)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::At {
                origin: ChainRef::OriginHash(origin),
                seq: SeqNum(100),
            }))
            .unwrap();
        assert_eq!(plan.root.target_nodes, vec![200]);
    }

    #[test]
    fn between_picks_only_holders_with_full_coverage() {
        // Three holders. Query `Between(100, 500)`.
        // - holder A: range [0..400] — doesn't cover (end<500)
        // - holder B: range [50..600] — covers
        // - holder C: tip 700 — covers (full prefix up to 700)
        let origin = 0xFEED_FACE_FEED_FACE_u64;
        let index = index_with(vec![
            (1, caps_with_causal_range(origin, 0, 400)),
            (2, caps_with_causal_range(origin, 50, 600)),
            (3, caps_with_causal_tip(origin, 700)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Between {
                origin: ChainRef::OriginHash(origin),
                start: SeqNum(100),
                end: SeqNum(500),
            }))
            .unwrap();
        // Holders B + C qualify; lex-sort puts them as [2, 3].
        assert_eq!(plan.root.target_nodes, vec![2, 3]);
    }

    #[test]
    fn between_surfaces_historical_range_unavailable_with_hints() {
        // No holder covers the full requested range; planner
        // surfaces `HistoricalRangeUnavailable` carrying the
        // available-range hints for caller renegotiation.
        let origin = 0xDEAD_DEAD_DEAD_DEAD_u64;
        let index = index_with(vec![
            (1, caps_with_causal_range(origin, 0, 100)),
            (2, caps_with_causal_tip(origin, 50)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::Between {
                origin: ChainRef::OriginHash(origin),
                start: SeqNum(0),
                end: SeqNum(500),
            }))
            .expect_err("no holder covers [0, 500)");
        match err {
            MeshError::HistoricalRangeUnavailable {
                origin: o,
                requested,
                available,
            } => {
                assert_eq!(o, origin);
                assert_eq!(requested, SeqNum(0)..SeqNum(500));
                // Both holders' advertised ranges surface as
                // hints. Order: per-coverage-list (proximity
                // then lex); both unmeasured here so lex.
                assert_eq!(
                    available,
                    vec![SeqNum(0)..SeqNum(100), SeqNum(0)..SeqNum(51)]
                );
            }
            other => panic!("expected HistoricalRangeUnavailable; got {other:?}"),
        }
    }

    #[test]
    fn at_surfaces_historical_range_unavailable_when_no_coverage() {
        // Holder advertises tip 50; query asks for seq 100.
        let origin = 0xBABE_BABE_BABE_BABE_u64;
        let index = index_with(vec![(1, caps_with_causal_tip(origin, 50))]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::At {
                origin: ChainRef::OriginHash(origin),
                seq: SeqNum(100),
            }))
            .expect_err("seq beyond tip");
        match err {
            MeshError::HistoricalRangeUnavailable {
                requested,
                available,
                ..
            } => {
                // Requested rendered as a single-seq range.
                assert_eq!(requested, SeqNum(100)..SeqNum(101));
                assert_eq!(available, vec![SeqNum(0)..SeqNum(51)]);
            }
            other => panic!("expected HistoricalRangeUnavailable; got {other:?}"),
        }
    }

    #[test]
    fn presence_form_holder_is_permissive_fallback() {
        // A holder advertising bare `causal:<hex>` (no range
        // claim) is admitted permissively — it makes no
        // claim about coverage, so the executor will
        // attempt the read and surface
        // HistoricalRangeUnavailable if the read actually
        // fails. Phase B planner trusts the presence claim.
        let origin = 0xFADE_FADE_FADE_FADE_u64;
        let index = index_with(vec![(1, caps_with_causal_presence(origin))]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::At {
                origin: ChainRef::OriginHash(origin),
                seq: SeqNum(999_999),
            }))
            .unwrap();
        assert_eq!(plan.root.target_nodes, vec![1]);
    }

    #[test]
    fn latest_prefers_holder_with_highest_tip() {
        // Three holders with tips 50, 500, 200. Latest picks
        // the holder with the highest tip first.
        let origin = 0xCAFE_CAFE_CAFE_CAFE_u64;
        let index = index_with(vec![
            (1, caps_with_causal_tip(origin, 50)),
            (2, caps_with_causal_tip(origin, 500)),
            (3, caps_with_causal_tip(origin, 200)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            }))
            .unwrap();
        // Descending tip: 500 (node 2) > 200 (node 3) > 50 (node 1).
        assert_eq!(plan.root.target_nodes, vec![2, 3, 1]);
    }

    #[test]
    fn proximity_ordering_breaks_lex_default() {
        // Three holders, all with bare presence claims. RTTs
        // are 30ms, 10ms, 20ms for node_ids 100, 50, 200
        // respectively. Lex order would be [50, 100, 200];
        // proximity puts them in [50, 200, 100] order.
        let origin = 0x3030_3030_3030_3030_u64;
        let caps = caps_with_causal_presence(origin);
        let index = index_with(vec![(100, caps.clone()), (50, caps.clone()), (200, caps)]);
        let rtt = |nid: u64| {
            Some(match nid {
                50 => Duration::from_millis(10),
                200 => Duration::from_millis(20),
                100 => Duration::from_millis(30),
                _ => return None::<Duration>,
            })
        };
        let planner = MeshQueryPlanner::new(&index, rtt);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            }))
            .unwrap();
        assert_eq!(plan.root.target_nodes, vec![50, 200, 100]);
    }

    #[test]
    fn unmeasured_rtt_falls_last_lex_among_themselves() {
        // RTT data exists for some holders, not others.
        // Measured holders sort by RTT; unmeasured holders
        // sort lex and land after every measured one.
        let origin = 0x7070_7070_7070_7070_u64;
        let caps = caps_with_causal_presence(origin);
        let index = index_with(vec![
            (1, caps.clone()),
            (2, caps.clone()),
            (3, caps.clone()),
            (4, caps),
        ]);
        let rtt = |nid: u64| match nid {
            2 => Some(Duration::from_millis(5)),
            3 => Some(Duration::from_millis(15)),
            _ => None,
        };
        let planner = MeshQueryPlanner::new(&index, rtt);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            }))
            .unwrap();
        // Measured: [2 (5ms), 3 (15ms)]; unmeasured: [1, 4].
        assert_eq!(plan.root.target_nodes, vec![2, 3, 1, 4]);
    }

    #[test]
    fn coverage_picks_most_specific_claim_when_holder_advertises_multiple() {
        // One holder advertises BOTH presence AND tip 100.
        // The planner picks the most specific (tip) so the
        // coverage check uses the tighter claim.
        let origin = 0x6060_6060_6060_6060_u64;
        let hex = chain_hex(origin);
        let mut caps = CapabilitySet::new();
        caps.tags.insert(causal_tag(hex.clone())); // presence
        caps.tags.insert(causal_tag(format!("{hex}:100"))); // tip 100
        let index = index_with(vec![(7, caps)]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        // Query At(150) — only presence would qualify
        // (permissive), but tip's `seq <= 100` rejects.
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::At {
                origin: ChainRef::OriginHash(origin),
                seq: SeqNum(150),
            }))
            .expect_err("most-specific claim (tip) should not cover seq 150");
        assert!(matches!(err, MeshError::HistoricalRangeUnavailable { .. }));
    }

    // ========================================================================
    // ChainRef::Discovered resolution
    // ========================================================================

    #[test]
    fn plan_chainref_discovered_resolves_via_filter() {
        use crate::adapter::net::behavior::predicate::Predicate;
        use crate::adapter::net::behavior::tag::{TagKey, TaxonomyAxis};

        let origin = 0xCAFE_BEEF_CAFE_BEEF_u64;
        let hex = chain_hex(origin);
        let mut caps = CapabilitySet::new().add_tag("dataforts.blob.storage");
        caps.tags.insert(causal_tag(hex));
        let index = index_with(vec![(42, caps)]);
        let pred = Predicate::Exists {
            key: TagKey {
                axis: TaxonomyAxis::Dataforts,
                key: "blob.storage".to_string(),
            },
        };
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::Discovered(pred.to_wire()),
            }))
            .expect("Discovered resolution should succeed");
        match plan.root.operator {
            OperatorPlan::LatestRead { origin: o } => assert_eq!(o, origin),
            other => panic!("expected LatestRead; got {other:?}"),
        }
        assert_eq!(plan.root.target_nodes, vec![42]);
    }

    #[test]
    fn plan_chainref_discovered_no_match_returns_no_capable_holder() {
        use crate::adapter::net::behavior::predicate::Predicate;
        use crate::adapter::net::behavior::tag::{TagKey, TaxonomyAxis};

        let pred = Predicate::Exists {
            key: TagKey {
                axis: TaxonomyAxis::Dataforts,
                key: "blob.storage".to_string(),
            },
        };
        let index = empty_index();
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::Discovered(pred.to_wire()),
            }))
            .expect_err("Discovered against empty index must surface NoCapableHolder");
        match err {
            MeshError::NoCapableHolder { requirement, .. } => {
                assert!(requirement.contains("Exists"));
            }
            other => panic!("expected NoCapableHolder; got {other:?}"),
        }
    }

    #[test]
    fn plan_chainref_discovered_multiple_origins_surfaces_ambiguous_error() {
        // Regression: previously resolve_origin silently took
        // the lex-smallest match when the predicate hit more
        // than one origin. Until Phase B's fan-out lands, that
        // returns wrong rows; we now surface AmbiguousDiscovery
        // and let the caller tighten the predicate.
        use crate::adapter::net::behavior::predicate::Predicate;
        use crate::adapter::net::behavior::tag::{TagKey, TaxonomyAxis};

        let origin_a = 0x0000_AAAA_AAAA_AAAA_u64;
        let origin_b = 0x0000_BBBB_BBBB_BBBB_u64;
        let mut caps_a = CapabilitySet::new().add_tag("dataforts.blob.storage");
        caps_a.tags.insert(causal_tag(chain_hex(origin_a)));
        let mut caps_b = CapabilitySet::new().add_tag("dataforts.blob.storage");
        caps_b.tags.insert(causal_tag(chain_hex(origin_b)));
        let index = index_with(vec![(1, caps_a), (2, caps_b)]);
        let pred = Predicate::Exists {
            key: TagKey {
                axis: TaxonomyAxis::Dataforts,
                key: "blob.storage".to_string(),
            },
        };
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::Discovered(pred.to_wire()),
            }))
            .expect_err("two-origin discovery should surface AmbiguousDiscovery");
        match err {
            MeshError::AmbiguousDiscovery { matches, .. } => {
                assert_eq!(matches.len(), 2);
                assert!(matches.contains(&origin_a));
                assert!(matches.contains(&origin_b));
            }
            other => panic!("expected AmbiguousDiscovery; got {other:?}"),
        }
    }

    #[test]
    fn plan_chainref_discovered_match_with_no_causal_tag_surfaces_no_capable_holder() {
        use crate::adapter::net::behavior::predicate::Predicate;
        use crate::adapter::net::behavior::tag::{TagKey, TaxonomyAxis};

        let caps = CapabilitySet::new().add_tag("dataforts.blob.storage");
        let index = index_with(vec![(7, caps)]);
        let pred = Predicate::Exists {
            key: TagKey {
                axis: TaxonomyAxis::Dataforts,
                key: "blob.storage".to_string(),
            },
        };
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::Discovered(pred.to_wire()),
            }))
            .expect_err("missing causal: tag should surface NoCapableHolder");
        assert!(matches!(err, MeshError::NoCapableHolder { .. }));
    }

    // ========================================================================
    // Composite operators + determinism + round-trip
    // ========================================================================

    #[test]
    fn plan_composite_operator_surfaces_not_yet_implemented() {
        // Project remains deferred (Phase A.2 placeholder until a
        // consumer drives column-extraction semantics). Use it as
        // the canonical "wrapped sub-plan flows through
        // NotYetImplemented" test now that Aggregate Count ships.
        let origin = 0x9999_9999_9999_9999_u64;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let q = MeshQuery::V1(QueryV1::Project {
            inner: Box::new(MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            })),
            columns: vec![Expr::Field("origin".to_string())],
        });
        let plan = planner.plan(&q).unwrap();
        match plan.root.operator {
            OperatorPlan::NotYetImplemented { detail, input } => {
                assert!(detail.contains("Project"));
                assert!(input.is_some(), "Project's inner sub-plan must be carried");
            }
            other => panic!("expected NotYetImplemented; got {other:?}"),
        }
    }

    #[test]
    fn plan_is_deterministic() {
        // Same query + same index → byte-identical encoded
        // plan. Load-bearing for the locked-decision-#4
        // cache key.
        let origin = 0x5555_5555_5555_5555_u64;
        let index = make_index_with_holder(11, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let q = MeshQuery::V1(QueryV1::Latest {
            origin: ChainRef::OriginHash(origin),
        });
        let p1 = planner.plan(&q).unwrap();
        let p2 = planner.plan(&q).unwrap();
        let e1 = postcard::to_allocvec(&p1).unwrap();
        let e2 = postcard::to_allocvec(&p2).unwrap();
        assert_eq!(e1, e2, "plan must be deterministic byte-by-byte");
    }

    #[test]
    fn lineage_back_with_multiple_fork_of_tags_is_deterministic() {
        // Regression: caps.tags is a HashSet. A node carrying
        // several `fork-of:` tags previously surfaced "first
        // in iteration order" which varies run-to-run. The
        // planner now sorts the candidates numerically. Build
        // a host that lists three forks and assert the BFS
        // plan + cache key are byte-stable across 32 fresh
        // planners.
        let child = 0x0000_0000_0000_00CC_u64;
        let parent_a = 0x0000_0000_0000_0001_u64;
        let parent_b = 0x0000_0000_0000_0002_u64;
        let parent_c = 0x0000_0000_0000_0003_u64;
        let mut caps = CapabilitySet::new();
        caps.tags.insert(causal_tag(chain_hex(child)));
        caps.tags.insert(fork_tag(parent_a));
        caps.tags.insert(fork_tag(parent_b));
        caps.tags.insert(fork_tag(parent_c));
        // Root nodes for each candidate parent so the BFS
        // resolves a parent regardless of which one wins.
        let index = index_with(vec![
            (1, caps),
            (2, caps_chain_only(parent_a)),
            (3, caps_chain_only(parent_b)),
            (4, caps_chain_only(parent_c)),
        ]);

        let mut last_encoding: Option<Vec<u8>> = None;
        for _ in 0..32 {
            let planner = MeshQueryPlanner::new(&index, rtt_none);
            let plan = planner
                .plan(&MeshQuery::V1(QueryV1::LineageBack {
                    origin: ChainRef::OriginHash(child),
                    max_depth: 1,
                }))
                .unwrap();
            let bytes = postcard::to_allocvec(&plan).unwrap();
            if let Some(prev) = &last_encoding {
                assert_eq!(
                    prev, &bytes,
                    "BFS plan must not depend on HashSet iter order"
                );
            }
            last_encoding = Some(bytes);
        }
    }

    #[test]
    fn lineage_back_across_multiple_replica_hosts_is_deterministic() {
        // Regression: pass-1 M1 made intra-node tag selection
        // deterministic, but the outer parent_of loop still
        // short-circuited on the first DashMap-iterated node
        // that hosted the child via a `causal:` tag. Two nodes
        // replicating the same chain origin with DIFFERENT
        // fork-of declarations could produce different plans
        // across runs.
        let child = 0x0000_0000_0000_DDDD_u64;
        let parent_a = 0x0000_0000_0000_0001_u64;
        let parent_b = 0x0000_0000_0000_0002_u64;
        // Two replicas of `child`; each declares a different parent.
        let index = index_with(vec![
            (1, caps_chain_forked_from(child, parent_a)),
            (2, caps_chain_forked_from(child, parent_b)),
            (3, caps_chain_only(parent_a)),
            (4, caps_chain_only(parent_b)),
        ]);

        let mut last_encoding: Option<Vec<u8>> = None;
        for _ in 0..32 {
            let planner = MeshQueryPlanner::new(&index, rtt_none);
            let plan = planner
                .plan(&MeshQuery::V1(QueryV1::LineageBack {
                    origin: ChainRef::OriginHash(child),
                    max_depth: 1,
                }))
                .unwrap();
            let bytes = postcard::to_allocvec(&plan).unwrap();
            if let Some(prev) = &last_encoding {
                assert_eq!(
                    prev, &bytes,
                    "parent_of must not depend on DashMap iter order across replicas",
                );
            }
            last_encoding = Some(bytes);
        }
    }

    #[test]
    fn execution_plan_round_trips_through_postcard() {
        let origin = 0x1111_1111_1111_1111_u64;
        let index = make_index_with_holder(3, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let p = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            }))
            .unwrap();
        let bytes = postcard::to_allocvec(&p).unwrap();
        let back: ExecutionPlan = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(back, p);
    }

    #[test]
    fn cost_estimate_propagates_rtt() {
        let origin = 0x2222_2222_2222_2222_u64;
        let index = make_index_with_holder(5, origin);
        let rtt = |nid: u64| {
            if nid == 5 {
                Some(Duration::from_millis(15))
            } else {
                None
            }
        };
        let planner = MeshQueryPlanner::new(&index, rtt);
        let p = planner
            .plan(&MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            }))
            .unwrap();
        assert_eq!(p.root.cost.latency_ms, 15);
        assert_eq!(p.root.cost.bandwidth_bytes, PHASE_A_ATOMIC_BANDWIDTH_BYTES);
    }

    // ========================================================================
    // Phase C — lineage walks
    // ========================================================================

    /// Build a `fork-of:<hex>` reserved tag from a parent
    /// origin hash. Mirrors `causal_tag` — `add_tag` would
    /// silently drop reserved-prefix tags (routes through
    /// `parse_user`), so build `Tag::Reserved` directly.
    fn fork_tag(parent_hash: u64) -> Tag {
        Tag::Reserved {
            prefix: "fork-of:".to_string(),
            body: chain_hex(parent_hash),
        }
    }

    /// Capability set advertising `chain` plus a `fork-of:`
    /// declaration pointing at `parent`. Models "this host
    /// holds chain X which is forked from chain P".
    fn caps_chain_forked_from(chain: u64, parent: u64) -> CapabilitySet {
        let mut caps = CapabilitySet::new();
        caps.tags.insert(causal_tag(chain_hex(chain)));
        caps.tags.insert(fork_tag(parent));
        caps
    }

    /// Capability set advertising `chain` plus a tip + a
    /// `fork-of:` declaration. Used to verify `tip_seq`
    /// propagation through lineage entries.
    fn caps_chain_tip_forked_from(chain: u64, tip: u64, parent: u64) -> CapabilitySet {
        let mut caps = CapabilitySet::new();
        caps.tags
            .insert(causal_tag(format!("{}:{}", chain_hex(chain), tip)));
        caps.tags.insert(fork_tag(parent));
        caps
    }

    /// Capability set advertising just `chain` (no fork-of:).
    /// Models the "root chain" — has no parent.
    fn caps_chain_only(chain: u64) -> CapabilitySet {
        let mut caps = CapabilitySet::new();
        caps.tags.insert(causal_tag(chain_hex(chain)));
        caps
    }

    #[test]
    fn parse_fork_body_round_trips_16_hex() {
        assert_eq!(parse_fork_body("00000000deadbeef"), Some(0xDEAD_BEEF));
        assert_eq!(
            parse_fork_body(&chain_hex(0x1234_5678_9ABC_DEF0)),
            Some(0x1234_5678_9ABC_DEF0)
        );
    }

    #[test]
    fn parse_fork_body_rejects_short_or_non_hex() {
        assert!(parse_fork_body("deadbeef").is_none()); // too short
        assert!(parse_fork_body("deadbeefcafebabe0").is_none()); // too long
        assert!(parse_fork_body("zzzzzzzzzzzzzzzz").is_none()); // non-hex
    }

    #[test]
    fn lineage_back_single_root_returns_only_start() {
        let root = 0x0000_0000_0000_0001_u64;
        let index = index_with(vec![(1, caps_chain_only(root))]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(root),
                max_depth: 5,
            }))
            .unwrap();
        match plan.root.operator {
            OperatorPlan::LineageEmit {
                origin,
                direction,
                entries,
            } => {
                assert_eq!(origin, root);
                assert_eq!(direction, LineageDirection::Back);
                assert_eq!(entries.len(), 1);
                assert_eq!(entries[0].origin, root);
                assert_eq!(entries[0].depth, 0);
                assert_eq!(entries[0].tip_seq, None);
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_back_walks_a_long_linear_chain_without_stack_overflow() {
        // Pin: pass-2 NEW-m4 flagged "very-large lineage chains:
        // largest test is 3 entries." Build a 500-deep linear
        // fork-of chain and walk back from the tail. The
        // planner's BFS uses `VecDeque::pop_front` and
        // `parent_of` is sort-based (not recursive), so this is
        // a depth-doesn't-overflow / time-bounded assertion.
        //
        // `parent_of` is O(nodes-in-index); BFS does O(depth)
        // steps; total is O(depth²). 500 is bounded enough to
        // run in single-digit seconds in dev profile while still
        // exercising depths well past anything a recursive
        // implementation would survive.
        const N: u64 = 500;
        let mut holders = Vec::with_capacity(N as usize);
        holders.push((1, caps_chain_only(1)));
        for i in 1..N {
            holders.push((i + 1, caps_chain_forked_from(i + 1, i)));
        }
        let index = index_with(holders);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(N),
                max_depth: (N + 10) as u32,
            }))
            .expect("10k-deep walk plans cleanly");
        match plan.root.operator {
            OperatorPlan::LineageEmit { entries, .. } => {
                assert_eq!(entries.len(), N as usize);
                // First entry is the tail; last is the root.
                assert_eq!(entries[0].origin, N);
                assert_eq!(entries[0].depth, 0);
                assert_eq!(entries[(N - 1) as usize].origin, 1);
                assert_eq!(entries[(N - 1) as usize].depth, (N - 1) as u32);
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_forward_walks_a_wide_fanout_without_stack_overflow() {
        // Symmetric stress: a single root with 1k direct
        // children (no grandchildren). BFS over `children_of` +
        // lex sort. Pins the wide-fanout shape; the linear chain
        // pin above covers the deep shape.
        const N: u64 = 1_000;
        let root = 1;
        let mut holders = Vec::with_capacity((N + 1) as usize);
        holders.push((1, caps_chain_only(root)));
        for i in 0..N {
            let child = 2 + i;
            holders.push((100 + i, caps_chain_forked_from(child, root)));
        }
        let index = index_with(holders);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageForward {
                origin: ChainRef::OriginHash(root),
                max_depth: 5,
            }))
            .expect("1k-wide fan-out plans cleanly");
        match plan.root.operator {
            OperatorPlan::LineageEmit { entries, .. } => {
                // Root + N children.
                assert_eq!(entries.len(), (N + 1) as usize);
                assert_eq!(entries[0].origin, root);
                assert_eq!(entries[0].depth, 0);
                // Children all at depth 1.
                assert!(entries[1..].iter().all(|e| e.depth == 1));
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_back_walks_through_three_generations() {
        // Grandparent (g) <- parent (p) <- child (c).
        let g = 0x0000_0000_0000_00AA_u64;
        let p = 0x0000_0000_0000_00BB_u64;
        let c = 0x0000_0000_0000_00CC_u64;
        let index = index_with(vec![
            (10, caps_chain_only(g)),
            (20, caps_chain_forked_from(p, g)),
            (30, caps_chain_forked_from(c, p)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(c),
                max_depth: 5,
            }))
            .unwrap();
        match plan.root.operator {
            OperatorPlan::LineageEmit { entries, .. } => {
                let chain: Vec<u64> = entries.iter().map(|e| e.origin).collect();
                assert_eq!(chain, vec![c, p, g]);
                let depths: Vec<u32> = entries.iter().map(|e| e.depth).collect();
                assert_eq!(depths, vec![0, 1, 2]);
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_back_propagates_tip_seq_from_holders() {
        // Holder advertises chain + tip + fork-of: — tip
        // surfaces in the LineageEntry.
        let parent = 0xAA;
        let child = 0xBB;
        let index = index_with(vec![
            (1, caps_chain_tip_forked_from(parent, 99, 0)), // root: fork-of:0 ignored
            (2, caps_chain_tip_forked_from(child, 42, parent)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(child),
                max_depth: 2,
            }))
            .unwrap();
        match plan.root.operator {
            OperatorPlan::LineageEmit { entries, .. } => {
                // child entry carries tip 42; parent entry
                // carries tip 99 (fork-of:0 from "root" doesn't
                // chain further since no host advertises chain 0).
                assert_eq!(entries[0].origin, child);
                assert_eq!(entries[0].tip_seq, Some(SeqNum(42)));
                assert_eq!(entries[1].origin, parent);
                assert_eq!(entries[1].tip_seq, Some(SeqNum(99)));
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_back_detects_cycle() {
        // Pathological: A -> B -> A. Cycle should surface.
        let a = 0x000A;
        let b = 0x000B;
        let index = index_with(vec![
            (1, caps_chain_forked_from(a, b)),
            (2, caps_chain_forked_from(b, a)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(a),
                max_depth: 10,
            }))
            .unwrap_err();
        match err {
            MeshError::LineageCycleDetected { origin, cycle } => {
                assert_eq!(origin, a);
                // Cycle must contain both a and b.
                assert!(cycle.contains(&a), "cycle missing a: {cycle:?}");
                assert!(cycle.contains(&b), "cycle missing b: {cycle:?}");
            }
            other => panic!("expected LineageCycleDetected; got {other:?}"),
        }
    }

    #[test]
    fn lineage_back_surfaces_max_depth_exceeded_when_walk_could_continue() {
        // 4-generation chain, max_depth=2: walk is truncated
        // and the planner surfaces the bound.
        let g0 = 0x10;
        let g1 = 0x11;
        let g2 = 0x12;
        let g3 = 0x13;
        let index = index_with(vec![
            (1, caps_chain_only(g0)),
            (2, caps_chain_forked_from(g1, g0)),
            (3, caps_chain_forked_from(g2, g1)),
            (4, caps_chain_forked_from(g3, g2)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(g3),
                max_depth: 2,
            }))
            .unwrap_err();
        match err {
            MeshError::LineageMaxDepthExceeded { origin, depth } => {
                assert_eq!(origin, g3);
                assert_eq!(depth, 2);
            }
            other => panic!("expected LineageMaxDepthExceeded; got {other:?}"),
        }
    }

    #[test]
    fn lineage_back_terminates_exactly_at_max_depth_without_error() {
        // 3-generation chain, max_depth=2: walk is g2 -> g1 -> g0
        // and at depth 2 the parent_of g0 is None — no error.
        let g0 = 0x20;
        let g1 = 0x21;
        let g2 = 0x22;
        let index = index_with(vec![
            (1, caps_chain_only(g0)),
            (2, caps_chain_forked_from(g1, g0)),
            (3, caps_chain_forked_from(g2, g1)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(g2),
                max_depth: 2,
            }))
            .unwrap();
        if let OperatorPlan::LineageEmit { entries, .. } = plan.root.operator {
            assert_eq!(
                entries.iter().map(|e| e.origin).collect::<Vec<_>>(),
                vec![g2, g1, g0]
            );
        } else {
            panic!("expected LineageEmit");
        }
    }

    #[test]
    fn lineage_forward_emits_descendants_bfs_sorted() {
        // Root has two children (c1 < c2 by hash). c1 has one
        // grandchild gc. BFS order: root, c1, c2, gc.
        let root = 0x100;
        let c1 = 0x110;
        let c2 = 0x120;
        let gc = 0x130;
        let index = index_with(vec![
            (1, caps_chain_only(root)),
            (2, caps_chain_forked_from(c1, root)),
            (3, caps_chain_forked_from(c2, root)),
            (4, caps_chain_forked_from(gc, c1)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageForward {
                origin: ChainRef::OriginHash(root),
                max_depth: 5,
            }))
            .unwrap();
        match plan.root.operator {
            OperatorPlan::LineageEmit {
                direction, entries, ..
            } => {
                assert_eq!(direction, LineageDirection::Forward);
                let chain: Vec<u64> = entries.iter().map(|e| e.origin).collect();
                // BFS asc-depth, lex-sorted within a depth.
                assert_eq!(chain, vec![root, c1, c2, gc]);
                let depths: Vec<u32> = entries.iter().map(|e| e.depth).collect();
                assert_eq!(depths, vec![0, 1, 1, 2]);
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_forward_surfaces_max_depth_when_descendants_remain() {
        // root -> c1 -> gc. max_depth=1: should surface bound
        // because gc is still reachable beyond.
        let root = 0x200;
        let c1 = 0x210;
        let gc = 0x220;
        let index = index_with(vec![
            (1, caps_chain_only(root)),
            (2, caps_chain_forked_from(c1, root)),
            (3, caps_chain_forked_from(gc, c1)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&MeshQuery::V1(QueryV1::LineageForward {
                origin: ChainRef::OriginHash(root),
                max_depth: 1,
            }))
            .unwrap_err();
        match err {
            MeshError::LineageMaxDepthExceeded { origin, depth } => {
                assert_eq!(origin, root);
                assert_eq!(depth, 1);
            }
            other => panic!("expected LineageMaxDepthExceeded; got {other:?}"),
        }
    }

    #[test]
    fn lineage_forward_with_no_descendants_returns_only_start() {
        let leaf = 0x300;
        let index = index_with(vec![(1, caps_chain_only(leaf))]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageForward {
                origin: ChainRef::OriginHash(leaf),
                max_depth: 10,
            }))
            .unwrap();
        if let OperatorPlan::LineageEmit { entries, .. } = plan.root.operator {
            assert_eq!(entries.len(), 1);
            assert_eq!(entries[0].origin, leaf);
        } else {
            panic!("expected LineageEmit");
        }
    }

    #[test]
    fn lineage_back_with_max_depth_zero_returns_only_start_no_error() {
        // Regression: max_depth=0 = "just-the-origin". A present
        // parent must NOT trip LineageMaxDepthExceeded, because
        // the caller explicitly asked for zero steps.
        let g0 = 0x40;
        let g1 = 0x41;
        let index = index_with(vec![
            (1, caps_chain_only(g0)),
            (2, caps_chain_forked_from(g1, g0)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(g1),
                max_depth: 0,
            }))
            .expect("max_depth=0 must succeed even when a parent exists");
        match plan.root.operator {
            OperatorPlan::LineageEmit { entries, .. } => {
                assert_eq!(entries.len(), 1);
                assert_eq!(entries[0].origin, g1);
                assert_eq!(entries[0].depth, 0);
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_forward_with_max_depth_zero_returns_only_start_no_error() {
        // Same regression as above, forward variant. The start
        // node has a descendant; max_depth=0 says "don't descend".
        let parent = 0x50;
        let child = 0x51;
        let index = index_with(vec![
            (1, caps_chain_only(parent)),
            (2, caps_chain_forked_from(child, parent)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageForward {
                origin: ChainRef::OriginHash(parent),
                max_depth: 0,
            }))
            .expect("max_depth=0 must succeed even when descendants exist");
        match plan.root.operator {
            OperatorPlan::LineageEmit { entries, .. } => {
                assert_eq!(entries.len(), 1);
                assert_eq!(entries[0].origin, parent);
                assert_eq!(entries[0].depth, 0);
            }
            other => panic!("expected LineageEmit; got {other:?}"),
        }
    }

    #[test]
    fn lineage_emit_round_trips_through_postcard() {
        // Pin the wire-encodability of LineageEmit so the
        // protocol layer can carry it inside an ExecutionPlan
        // without surprises.
        let parent = 0xAA;
        let child = 0xBB;
        let index = index_with(vec![
            (1, caps_chain_only(parent)),
            (2, caps_chain_forked_from(child, parent)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&MeshQuery::V1(QueryV1::LineageBack {
                origin: ChainRef::OriginHash(child),
                max_depth: 5,
            }))
            .unwrap();
        let bytes = postcard::to_allocvec(&plan).unwrap();
        let decoded: ExecutionPlan = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(decoded, plan);
    }

    // ========================================================================
    // Phase D — hash-join planning
    // ========================================================================

    fn join_query(
        left_field: &str,
        right_field: &str,
        left_chain: u64,
        right_chain: u64,
    ) -> MeshQuery {
        MeshQuery::V1(QueryV1::Join {
            left: Box::new(MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(left_chain),
            })),
            right: Box::new(MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(right_chain),
            })),
            on: JoinKey {
                left_field: Expr::Field(left_field.to_string()),
                right_field: Expr::Field(right_field.to_string()),
            },
            kind: JoinKind::Inner,
            watermark: Duration::from_secs(5),
        })
    }

    #[test]
    fn plan_join_on_origin_produces_hash_join_with_origin_key() {
        let l = 0x1111;
        let r = 0x2222;
        let index = index_with(vec![
            (1, caps_with_causal_presence(l)),
            (2, caps_with_causal_presence(r)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner.plan(&join_query("origin", "origin", l, r)).unwrap();
        match plan.root.operator {
            OperatorPlan::HashJoin {
                key_mode,
                kind,
                strategy,
                watermark,
                left,
                right,
            } => {
                assert_eq!(key_mode, JoinKeyMode::Origin);
                assert_eq!(kind, JoinKind::Inner);
                assert_eq!(strategy, JoinStrategy::HashBroadcast);
                assert_eq!(watermark, Duration::from_secs(5));
                assert!(matches!(left.operator, OperatorPlan::LatestRead { .. }));
                assert!(matches!(right.operator, OperatorPlan::LatestRead { .. }));
            }
            other => panic!("expected HashJoin; got {other:?}"),
        }
    }

    #[test]
    fn plan_join_on_seq_produces_seq_key_mode() {
        let l = 0x3333;
        let r = 0x4444;
        let index = index_with(vec![
            (1, caps_with_causal_presence(l)),
            (2, caps_with_causal_presence(r)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner.plan(&join_query("seq", "seq", l, r)).unwrap();
        if let OperatorPlan::HashJoin { key_mode, .. } = plan.root.operator {
            assert_eq!(key_mode, JoinKeyMode::Seq);
        } else {
            panic!("expected HashJoin");
        }
    }

    #[test]
    fn plan_join_with_mismatched_field_names_surfaces_planner_error() {
        let l = 0x5555;
        let r = 0x6666;
        let index = index_with(vec![
            (1, caps_with_causal_presence(l)),
            (2, caps_with_causal_presence(r)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&join_query("origin", "seq", l, r))
            .unwrap_err();
        match err {
            MeshError::PlannerError { detail } => {
                assert!(detail.contains("same field name"), "got: {detail}");
            }
            other => panic!("expected PlannerError; got {other:?}"),
        }
    }

    #[test]
    fn plan_join_on_payload_field_produces_field_key_mode() {
        // Phase D-2 finish: payload-keyed joins resolve to
        // JoinKeyMode::Field(<path>), not a PlannerError.
        let l = 0x7777;
        let r = 0x8888;
        let index = index_with(vec![
            (1, caps_with_causal_presence(l)),
            (2, caps_with_causal_presence(r)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&join_query(
                "payload.request_id",
                "payload.request_id",
                l,
                r,
            ))
            .unwrap();
        match plan.root.operator {
            OperatorPlan::HashJoin { key_mode, .. } => {
                assert_eq!(
                    key_mode,
                    JoinKeyMode::Field("payload.request_id".to_string())
                );
            }
            other => panic!("expected HashJoin; got {other:?}"),
        }
    }

    #[test]
    fn plan_join_with_non_field_expression_surfaces_planner_error() {
        let l = 0x9999;
        let r = 0xAAAA;
        let index = index_with(vec![
            (1, caps_with_causal_presence(l)),
            (2, caps_with_causal_presence(r)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let q = MeshQuery::V1(QueryV1::Join {
            left: Box::new(MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(l),
            })),
            right: Box::new(MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(r),
            })),
            on: JoinKey {
                left_field: Expr::LitString("origin".to_string()),
                right_field: Expr::Field("origin".to_string()),
            },
            kind: JoinKind::Inner,
            watermark: Duration::from_secs(5),
        });
        let err = planner.plan(&q).unwrap_err();
        match err {
            MeshError::PlannerError { detail } => {
                assert!(detail.contains("field reference"));
            }
            other => panic!("expected PlannerError; got {other:?}"),
        }
    }

    #[test]
    fn plan_join_round_trips_through_postcard() {
        let l = 0xCCCC;
        let r = 0xDDDD;
        let index = index_with(vec![
            (1, caps_with_causal_presence(l)),
            (2, caps_with_causal_presence(r)),
        ]);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner.plan(&join_query("origin", "origin", l, r)).unwrap();
        let bytes = postcard::to_allocvec(&plan).unwrap();
        let decoded: ExecutionPlan = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(decoded, plan);
    }

    // ========================================================================
    // Phase E — aggregate planning (Count only in E-1)
    // ========================================================================

    fn aggregate_query(origin: u64, group_by: Vec<Expr>, agg_fn: AggregateFn) -> MeshQuery {
        MeshQuery::V1(QueryV1::Aggregate {
            inner: Box::new(MeshQuery::V1(QueryV1::Latest {
                origin: ChainRef::OriginHash(origin),
            })),
            group_by,
            agg_fn,
        })
    }

    #[test]
    fn plan_aggregate_count_no_group_by() {
        let origin = 0x1111;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&aggregate_query(origin, vec![], AggregateFn::Count))
            .unwrap();
        match plan.root.operator {
            OperatorPlan::AggregateCount { input, group_by } => {
                assert_eq!(group_by, None);
                assert!(matches!(input.operator, OperatorPlan::LatestRead { .. }));
            }
            other => panic!("expected AggregateCount; got {other:?}"),
        }
    }

    #[test]
    fn plan_aggregate_count_group_by_origin() {
        let origin = 0x2222;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&aggregate_query(
                origin,
                vec![Expr::Field("origin".to_string())],
                AggregateFn::Count,
            ))
            .unwrap();
        if let OperatorPlan::AggregateCount { group_by, .. } = plan.root.operator {
            assert_eq!(group_by, Some(JoinKeyMode::Origin));
        } else {
            panic!("expected AggregateCount");
        }
    }

    #[test]
    fn plan_aggregate_count_group_by_seq() {
        let origin = 0x3333;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&aggregate_query(
                origin,
                vec![Expr::Field("seq".to_string())],
                AggregateFn::Count,
            ))
            .unwrap();
        if let OperatorPlan::AggregateCount { group_by, .. } = plan.root.operator {
            assert_eq!(group_by, Some(JoinKeyMode::Seq));
        } else {
            panic!("expected AggregateCount");
        }
    }

    #[test]
    fn plan_aggregate_count_group_by_origin_seq_composite() {
        let origin = 0x4444;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&aggregate_query(
                origin,
                vec![
                    Expr::Field("origin".to_string()),
                    Expr::Field("seq".to_string()),
                ],
                AggregateFn::Count,
            ))
            .unwrap();
        if let OperatorPlan::AggregateCount { group_by, .. } = plan.root.operator {
            assert_eq!(group_by, Some(JoinKeyMode::OriginSeq));
        } else {
            panic!("expected AggregateCount");
        }
    }

    #[test]
    fn plan_aggregate_sum_produces_aggregate_numeric() {
        let origin = 0x5555;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&aggregate_query(
                origin,
                vec![],
                AggregateFn::Sum {
                    field: Expr::Field("amount".to_string()),
                },
            ))
            .unwrap();
        match plan.root.operator {
            OperatorPlan::AggregateNumeric {
                group_by,
                field_path,
                kind,
                ..
            } => {
                assert_eq!(group_by, None);
                assert_eq!(field_path, "amount");
                assert_eq!(kind, super::super::query::NumericAggregateKind::Sum);
            }
            other => panic!("expected AggregateNumeric; got {other:?}"),
        }
    }

    #[test]
    fn plan_aggregate_avg_with_group_by_produces_aggregate_numeric() {
        let origin = 0x5556;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&aggregate_query(
                origin,
                vec![Expr::Field("origin".to_string())],
                AggregateFn::Avg {
                    field: Expr::Field("latency".to_string()),
                },
            ))
            .unwrap();
        if let OperatorPlan::AggregateNumeric {
            group_by,
            field_path,
            kind,
            ..
        } = plan.root.operator
        {
            assert_eq!(group_by, Some(JoinKeyMode::Origin));
            assert_eq!(field_path, "latency");
            assert_eq!(kind, super::super::query::NumericAggregateKind::Avg);
        } else {
            panic!("expected AggregateNumeric");
        }
    }

    #[test]
    fn plan_aggregate_non_field_arg_surfaces_planner_error() {
        let origin = 0x5557;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&aggregate_query(
                origin,
                vec![],
                AggregateFn::Sum {
                    field: Expr::LitInt(42),
                },
            ))
            .unwrap_err();
        match err {
            MeshError::PlannerError { detail } => {
                assert!(detail.contains("Expr::Field"), "got: {detail}");
            }
            other => panic!("expected PlannerError; got {other:?}"),
        }
    }

    #[test]
    fn plan_aggregate_sketch_function_still_surfaces_planner_error() {
        // HLL / T-Digest sketch implementations are deferred to
        // Phase F (or whenever a consumer's data volumes justify
        // them); exact equivalents ship via DistinctCountExact /
        // PercentileExact.
        let origin = 0x5558;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&aggregate_query(
                origin,
                vec![],
                AggregateFn::DistinctCountHll {
                    field: Expr::Field("user_id".to_string()),
                },
            ))
            .unwrap_err();
        match err {
            MeshError::PlannerError { detail } => {
                assert!(detail.contains("sketch implementation"), "got: {detail}");
                assert!(detail.contains("DistinctCountExact"), "got: {detail}");
            }
            other => panic!("expected PlannerError; got {other:?}"),
        }
    }

    #[test]
    fn plan_aggregate_group_by_payload_field_surfaces_planner_error() {
        let origin = 0x6666;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let err = planner
            .plan(&aggregate_query(
                origin,
                vec![Expr::Field("payload.severity".to_string())],
                AggregateFn::Count,
            ))
            .unwrap_err();
        match err {
            MeshError::PlannerError { detail } => {
                assert!(detail.contains("row-intrinsic"));
            }
            other => panic!("expected PlannerError; got {other:?}"),
        }
    }

    #[test]
    fn plan_aggregate_round_trips_through_postcard() {
        let origin = 0x7777;
        let index = make_index_with_holder(1, origin);
        let planner = MeshQueryPlanner::new(&index, rtt_none);
        let plan = planner
            .plan(&aggregate_query(
                origin,
                vec![Expr::Field("origin".to_string())],
                AggregateFn::Count,
            ))
            .unwrap();
        let bytes = postcard::to_allocvec(&plan).unwrap();
        let decoded: ExecutionPlan = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(decoded, plan);
    }
}